/
cannula.go
761 lines (609 loc) · 19.9 KB
/
cannula.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
package cannula
import (
"errors"
"fmt"
"net"
"regexp"
"strings"
"sync"
"time"
"github.com/sorcix/irc"
"golang.org/x/oauth2"
"google.golang.org/api/youtube/v3"
)
var versionString = "v0.3"
var startTime = time.Now()
// These lines contain zero width spaces.
var YTToIRC = strings.NewReplacer(" ", " ", "!", "❢", "@", "᪤", "+", "+", "&", "&", "%", "%", ":", ":")
var IRCToYT = strings.NewReplacer(" ", " ", "❢", "!", "᪤", "@", "+", "+", "&", "&", "%", "%", ":", ":")
var StripColors = regexp.MustCompile("\x1f|\x02|\x12|\x0f|\x16|\x03(?:\\d{1,2}(?:,\\d{1,2})?)?")
type ServerClose struct {
}
type Cannula struct {
sync.RWMutex
config *oauth2.Config
token *oauth2.Token
tokens map[string]*oauth2.Token
service *youtube.Service
listener net.Listener
prefix *irc.Prefix
in chan interface{}
clients map[*irc.Prefix]*Client
ytClients map[string]*YTClient
names map[string]*Client
channels map[string]*Channel
rateLimit chan func()
}
func New() (*Cannula, error) {
c := &Cannula{}
err := c.ytAuth()
if err != nil {
return nil, err
}
return c, nil
}
func (c *Cannula) handleConn(conn net.Conn) {
cl := NewClient(&irc.Prefix{}, conn, c.in)
c.Lock()
defer c.Unlock()
c.clients[cl.Prefix] = cl
go cl.handle()
}
func (c *Cannula) handle() {
for i := range c.in {
switch i := i.(type) {
case *irc.Message:
c.handleMessage(i)
}
}
}
func (c *Cannula) handleMessage(m *irc.Message) {
c.Lock()
defer c.Unlock()
cl := c.clients[m.Prefix]
if cl == nil {
return
}
switch m.Command {
case irc.QUIT:
c.quit(cl, m)
case irc.PASS:
c.pass(cl, m)
case irc.NICK:
c.nick(cl, m)
case irc.USER:
c.user(cl, m)
case irc.JOIN:
c.join(cl, m)
case irc.PART:
c.part(cl, m)
case irc.PRIVMSG:
c.privmsg(cl, m)
case irc.NOTICE:
c.notice(cl, m)
case irc.PING:
c.ping(cl, m)
case irc.MODE:
c.mode(cl, m)
case irc.KICK:
c.kick(cl, m)
}
}
func (c *Cannula) verifyChannelTarget(cl *Client, m *irc.Message, command string) string {
if !cl.Authorized {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTREGISTERED, []string{}, "You have not registered", false}
return ""
}
if len(m.Params) == 0 {
cl.in <- &irc.Message{c.prefix, irc.ERR_NEEDMOREPARAMS, []string{command}, "Not enough parameters", false}
return ""
}
verifiedTargets := []string{}
target := m.Params[0]
targets := strings.Split(target, ",")
for _, target := range targets {
if strings.Index(target, "#") != 0 {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHCHANNEL, []string{target}, "No such channel", false}
continue
}
if c.channels[target] == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHCHANNEL, []string{target}, "No such channel", false}
continue
}
if c.channels[target].clients[cl] == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTONCHANNEL, []string{target}, "You're not on that channel", false}
continue
}
verifiedTargets = append(verifiedTargets, target)
}
return strings.Join(verifiedTargets, ",")
}
func (c *Cannula) verifyTarget(cl *Client, m *irc.Message, command string) string {
if !cl.Authorized {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTREGISTERED, []string{}, "You have not registered", false}
return ""
}
if len(m.Params) == 0 {
cl.in <- &irc.Message{c.prefix, irc.ERR_NORECIPIENT, []string{command}, "No recipient given", false}
return ""
}
verifiedTargets := []string{}
target := m.Params[0]
targets := strings.Split(target, ",")
for _, target := range targets {
if c.names[target] == nil && c.channels[target] == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHNICK, []string{target}, "No such nick/channel", false}
continue
}
if strings.Index(target, "#") == 0 && c.channels[target].clients[cl] == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTONCHANNEL, []string{target}, "You're not on that channel", false}
continue
}
verifiedTargets = append(verifiedTargets, target)
}
return strings.Join(verifiedTargets, ",")
}
func (c *Cannula) quit(cl *Client, m *irc.Message) {
delete(c.clients, cl.Prefix)
delete(c.names, cl.Prefix.Name)
for ch := range cl.Channels {
c.channels[ch].Quit(c, cl, m)
}
}
func (c *Cannula) checkAuth(cl *Client) {
if cl.Prefix.User == "" || cl.Prefix.Host == "" || cl.Prefix.Name == "" {
return
}
firstTime := false
if cl.Pass != "" {
if c.tokens[cl.Pass] == nil {
tok, err := c.config.Exchange(oauth2.NoContext, cl.Pass)
if err != nil {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{cl.Prefix.Name}, fmt.Sprintf("Error linking YouTube account. %s", strings.Replace(err.Error(), "\n", "", -1)), false}
} else {
c.tokens[cl.Pass] = tok
c.ytSaveTokens()
firstTime = true
}
}
if c.tokens[cl.Pass] != nil {
cl.Service, _ = c.ytCreateService(c.tokens[cl.Pass])
}
}
if cl.Service == nil {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{cl.Prefix.Name}, fmt.Sprintf("You have not linked your YouTube account properly. Visit: %s and follow the instructions to link your account.", c.ytGenerateAuthURL()), false}
} else {
res, err := cl.Service.Channels.List([]string{"id","snippet"}).Mine(true).Do()
if err != nil || len(res.Items) == 0 {
if err == nil {
err = errors.New("Could not find your YouTube Channel, have you created one?")
}
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{cl.Prefix.Name}, fmt.Sprintf("Error linking YouTube account. %s", strings.Replace(err.Error(), "\n", "", -1)), false}
cl.Service = nil
} else {
ytc := res.Items[0]
ytcl := c.ytClient(ytc.Snippet.Title, ytc.Id)
old := cl.Prefix.Name
// Delete the current client, as the nick and yt nick may match.
delete(c.clients, cl.Prefix)
delete(c.names, cl.Prefix.Name)
// If the name for the youtube prefix already exist, we are free to kill this connection as we have already deleted it from the client list.
if c.names[ytcl.Prefix.Name] != nil {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{cl.Prefix.Name}, "You are already logged in.", false}
cl.in <- &ServerClose{}
return
}
cl.Prefix = ytcl.Prefix
cl.YTClient = ytcl
c.clients[cl.Prefix] = cl
c.names[cl.Prefix.Name] = cl
cl.in <- &irc.Message{c.prefix, irc.NICK, []string{old}, cl.Prefix.Name, false}
if firstTime {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{cl.Prefix.Name}, "YouTube account linked!", false}
}
}
}
cl.Authorized = true
cl.in <- &irc.Message{c.prefix, irc.RPL_WELCOME, []string{cl.Prefix.Name}, fmt.Sprintf("Welcome to the IRC Network %s", cl.Prefix), false}
cl.in <- &irc.Message{c.prefix, irc.RPL_YOURHOST, []string{cl.Prefix.Name}, fmt.Sprintf("Your host is %s running Cannula %s", c.prefix.Name, versionString), false}
cl.in <- &irc.Message{c.prefix, irc.RPL_CREATED, []string{cl.Prefix.Name}, fmt.Sprintf("This server was created %s at %s", startTime.Format("Mon Jan 2 2006"), startTime.Format("15:04:05 MST")), false}
cl.in <- &irc.Message{c.prefix, irc.RPL_BOUNCE, []string{cl.Prefix.Name}, "PREFIX=&@\\%+ STATUSMSG=&@\\%+ CHANTYPES=# CHANMODES=,,,m :are supported on this server", false}
cl.in <- &irc.Message{c.prefix, irc.RPL_MYINFO, []string{cl.Prefix.Name}, fmt.Sprintf("%s %s", c.prefix.Name, versionString), false}
cl.in <- &irc.Message{c.prefix, irc.RPL_MOTDSTART, []string{cl.Prefix.Name}, fmt.Sprintf("- %s Message of the day -", c.prefix.Name), false}
cl.in <- &irc.Message{c.prefix, irc.RPL_MOTD, []string{cl.Prefix.Name}, "- If you get a lot of rate limit errors, consider adding your account as a moderator.", false}
cl.in <- &irc.Message{c.prefix, irc.RPL_MOTD, []string{cl.Prefix.Name}, "- Moderation actions are now supported. /kick will time out a user for 5 minutes. +b (ban) will ban a user.", false}
cl.in <- &irc.Message{c.prefix, irc.RPL_ENDOFMOTD, []string{cl.Prefix.Name}, "End of MOTD command", false}
}
func (c *Cannula) pass(cl *Client, m *irc.Message) {
if len(m.Params) == 0 && m.Trailing == "" {
cl.in <- &irc.Message{c.prefix, irc.ERR_NEEDMOREPARAMS, []string{irc.PASS}, "Not enough parameters", false}
return
}
if cl.Authorized {
cl.in <- &irc.Message{c.prefix, irc.ERR_ALREADYREGISTRED, []string{}, "You may not reregister", false}
return
}
if len(m.Params) == 1 {
cl.Pass = m.Params[0]
} else {
cl.Pass = m.Trailing
}
}
func (c *Cannula) user(cl *Client, m *irc.Message) {
if len(m.Params) != 3 {
cl.in <- &irc.Message{c.prefix, irc.ERR_NEEDMOREPARAMS, []string{irc.USER}, "Not enough parameters", false}
return
}
if cl.Authorized {
cl.in <- &irc.Message{c.prefix, irc.ERR_ALREADYREGISTRED, []string{}, "You may not reregister", false}
return
}
cl.Prefix.User = m.Params[0]
c.checkAuth(cl)
}
func (c *Cannula) nick(cl *Client, m *irc.Message) {
if cl.Service != nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_ERRONEUSNICKNAME, []string{}, "Linked accounts can't change their nick", false}
return
}
name := ""
if len(m.Params) == 0 && m.Trailing == "" {
cl.in <- &irc.Message{c.prefix, irc.ERR_NONICKNAMEGIVEN, []string{}, "No nickname given", false}
return
}
if len(m.Params) == 1 {
name = m.Params[0]
} else {
name = m.Trailing
}
if len(name) == 0 || strings.Index(name, " ") != -1 {
cl.in <- &irc.Message{c.prefix, irc.ERR_ERRONEUSNICKNAME, []string{name}, "Erroneous nickname", false}
return
}
if c.names[name] != nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NICKNAMEINUSE, []string{name}, "Nickname is already in use", false}
return
}
if cl.Prefix.Name != "" {
delete(c.names, cl.Prefix.Name)
}
c.names[name] = cl
m.Prefix = &irc.Prefix{cl.Prefix.Name, cl.Prefix.User, cl.Prefix.Host}
for ch := range cl.Channels {
c.channels[ch].Nick(c, cl, m)
}
cl.Prefix.Name = name
if !cl.Authorized {
c.checkAuth(cl)
}
}
func (c *Cannula) join(cl *Client, m *irc.Message) {
if !cl.Authorized {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTREGISTERED, []string{}, "You have not registered", false}
return
}
if len(m.Params) < 1 {
cl.in <- &irc.Message{c.prefix, irc.ERR_NEEDMOREPARAMS, []string{irc.JOIN}, "Not enough parameters", false}
return
}
target := m.Params[0]
targets := strings.Split(target, ",")
for _, target := range targets {
if strings.Index(target, "#") != 0 {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHCHANNEL, []string{target}, "No such channel", false}
continue
}
ch := c.channels[target]
if ch == nil {
ch = &Channel{
Name: target,
clients: make(map[*Client]*ChannelClient),
ytClients: make(map[*YTClient]*ChannelClient),
ignore: make(map[string]bool),
}
c.channels[target] = ch
go ch.Init(c)
}
ch.Join(c, cl, &irc.Message{cl.Prefix, m.Command, []string{target}, m.Trailing, m.EmptyTrailing})
}
}
func (c *Cannula) part(cl *Client, m *irc.Message) {
target := c.verifyChannelTarget(cl, m, m.Command)
if target == "" {
return
}
targets := strings.Split(target, ",")
for _, target := range targets {
ch := c.channels[target]
ch.Part(c, cl, &irc.Message{cl.Prefix, m.Command, []string{target}, m.Trailing, m.EmptyTrailing})
}
}
func (c *Cannula) privmsg(cl *Client, m *irc.Message) {
target := c.verifyTarget(cl, m, m.Command)
if target == "" {
return
}
if m.Trailing == "" {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTEXTTOSEND, []string{}, "No text to send", false}
return
}
targets := strings.Split(target, ",")
ignore := cl.Prefix
if len(targets) > 1 {
ignore = nil
}
for _, target := range targets {
c.broadcast(&irc.Message{cl.Prefix, m.Command, []string{target}, m.Trailing, m.EmptyTrailing}, ignore)
}
}
func (c *Cannula) notice(cl *Client, m *irc.Message) {
target := c.verifyTarget(cl, m, m.Command)
if target == "" {
return
}
if m.Trailing == "" {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTEXTTOSEND, []string{}, "No text to send", false}
return
}
targets := strings.Split(target, ",")
ignore := cl.Prefix
if len(targets) > 1 {
ignore = nil
}
for _, target := range targets {
c.broadcast(&irc.Message{cl.Prefix, m.Command, []string{target}, m.Trailing, m.EmptyTrailing}, ignore)
}
}
func (c *Cannula) ping(cl *Client, m *irc.Message) {
cl.in <- &irc.Message{nil, irc.PONG, m.Params, m.Trailing, m.EmptyTrailing}
}
func (c *Cannula) kick(cl *Client, m *irc.Message) {
if !cl.Authorized {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTREGISTERED, []string{}, "You have not registered", false}
return
}
if len(m.Params) != 2 {
cl.in <- &irc.Message{c.prefix, irc.ERR_NEEDMOREPARAMS, []string{m.Command}, "Not enough parameters", false}
return
}
channels := strings.Split(m.Params[0], ",")
targets := strings.Split(m.Params[1], ",")
if len(channels) != len(targets) {
cl.in <- &irc.Message{c.prefix, irc.ERR_NEEDMOREPARAMS, []string{m.Command}, "Not enough parameters", false}
return
}
for i := range channels {
channel := channels[i]
target := targets[i]
if strings.Index(channel, "#") != 0 {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHCHANNEL, []string{channel}, "No such channel", false}
continue
}
ch := c.channels[channel]
if ch == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHCHANNEL, []string{channel}, "No such channel", false}
continue
}
ccl := ch.clients[cl]
if ccl == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTONCHANNEL, []string{channel}, "You're not on that channel", false}
continue
}
if !ccl.Moderator && !ccl.Owner {
cl.in <- &irc.Message{c.prefix, irc.ERR_CHANOPRIVSNEEDED, []string{channel}, "You're not channel operator", false}
continue
}
ytc := ch.YTClient(c, target)
if ytc == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHNICK, []string{target}, "No such nick/channel", false}
continue
}
c.rateLimit <- func() {
_, err := cl.Service.LiveChatBans.Insert([]string{"snippet"}, &youtube.LiveChatBan{
Snippet: &youtube.LiveChatBanSnippet{
LiveChatId: ch.liveChatId,
BannedUserDetails: &youtube.ChannelProfileDetails{
ChannelId: ytc.ChannelID,
},
Type: "temporary",
BanDurationSeconds: 5 * 60,
},
}).Do()
if err != nil {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{channel}, fmt.Sprintf("Error timing out user: %s (%s): %s", ytc.Prefix.Name, ytc.ChannelID, strings.Replace(err.Error(), "\n", "", -1)), false}
} else {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{channel}, fmt.Sprintf("%s has been timed out.", ytc.Prefix.Name), false}
}
}
}
}
var channelIDRegexp = regexp.MustCompile("@(UC.{22})\\.youtube\\.com")
func (c *Cannula) mode(cl *Client, m *irc.Message) {
if !cl.Authorized {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTREGISTERED, []string{}, "You have not registered", false}
return
}
if len(m.Params) == 0 {
// MODE not supported
return
}
target := m.Params[0]
if c.names[target] != nil {
// MODE <user> not supported
return
}
if strings.Index(target, "#") != 0 {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHNICK, []string{target}, "No such nick/channel", false}
return
}
ch := c.channels[target]
if ch == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHCHANNEL, []string{target}, "No such channel", false}
return
}
ccl := ch.clients[cl]
if ccl == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOTONCHANNEL, []string{target}, "You're not on that channel", false}
return
}
if len(m.Params) == 1 {
// MODE <channel> not supported
return
}
mode := m.Params[1]
switch mode {
case "-b":
if !ccl.Moderator && !ccl.Owner {
cl.in <- &irc.Message{c.prefix, irc.ERR_CHANOPRIVSNEEDED, []string{target}, "You're not channel operator", false}
return
}
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{target}, "YouTube does not provide a reliable way to remove a ban at this time, manage your bans in the address book: https://www.youtube.com/address_book", false}
case "+b":
if len(m.Params) < 3 {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{target}, "YouTube does not provide a reliable way to list bans at this time, manage your bans in the address book: https://www.youtube.com/address_book", false}
return
}
if !ccl.Moderator && !ccl.Owner {
cl.in <- &irc.Message{c.prefix, irc.ERR_CHANOPRIVSNEEDED, []string{target}, "You're not channel operator", false}
return
}
target2 := m.Params[2]
var ytc *YTClient
t := c.names[target2]
if t == nil {
// Try get the target from the hostmask.
match := channelIDRegexp.FindStringSubmatch(target2)
if match != nil {
ytc = c.ytClients[match[1]]
}
} else {
ytc = t.YTClient
}
if ytc == nil {
cl.in <- &irc.Message{c.prefix, irc.ERR_NOSUCHNICK, []string{target2}, "No such nick/channel", false}
return
}
c.rateLimit <- func() {
_, err := cl.Service.LiveChatBans.Insert([]string{"snippet"}, &youtube.LiveChatBan{
Snippet: &youtube.LiveChatBanSnippet{
LiveChatId: ch.liveChatId,
BannedUserDetails: &youtube.ChannelProfileDetails{
ChannelId: ytc.ChannelID,
},
Type: "permanent",
},
}).Do()
if err != nil {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{target}, fmt.Sprintf("Error banning user: %s (%s): %s", ytc.Prefix.Name, ytc.ChannelID, strings.Replace(err.Error(), "\n", "", -1)), false}
} else {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{target}, fmt.Sprintf("%s has been banned.", ytc.Prefix.Name), false}
}
}
default:
cl.in <- &irc.Message{c.prefix, irc.ERR_UMODEUNKNOWNFLAG, []string{}, "Unknown MODE flag", false}
}
}
func (c *Cannula) broadcast(m *irc.Message, ignore *irc.Prefix) {
if len(m.Params) != 1 {
return
}
target := m.Params[0]
ch := c.channels[target]
if ch != nil {
cl := c.clients[m.Prefix]
ch.Broadcast(m, ignore)
if m.Command == irc.PRIVMSG && ch.liveChatId != "" && cl != nil && cl.Service != nil {
message := m.Trailing
if strings.Index(message, "") == 0 {
message = strings.Trim(message, "")
if strings.Index(message, "ACTION ") == 0 {
message = message[7:]
} else {
return
}
}
message = IRCToYT.Replace(message)
message = StripColors.ReplaceAllString(message, "")
// Send messages of 200 characters.
for i := 0; i < len(message); i += 200 {
m := i + 200
if m > len(message) {
m = len(message)
}
me := message[i:m]
c.rateLimit <- func() {
res, err := cl.Service.LiveChatMessages.Insert([]string{"snippet"}, &youtube.LiveChatMessage{
Snippet: &youtube.LiveChatMessageSnippet{
LiveChatId: ch.liveChatId,
Type: "textMessageEvent",
TextMessageDetails: &youtube.LiveChatTextMessageDetails{
MessageText: me,
},
},
}).Do()
if err != nil {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{target}, fmt.Sprintf("Error sending message: %s", strings.Replace(err.Error(), "\n", "", -1)), false}
return
}
ch.Lock()
defer ch.Unlock()
ch.ignore[res.Id] = true
}
}
}
} else {
cl := c.names[target]
if cl != nil {
cl.in <- m
}
}
}
func getHost(a net.Addr) string {
addr := a.String()
if host, _, err := net.SplitHostPort(addr); err == nil {
addr = host
}
if domains, err := net.LookupAddr(addr); err == nil {
if len(domains) > 0 {
return strings.TrimSuffix(domains[0], ".")
}
}
return addr
}
func (c *Cannula) handleRateLimit() {
for f := range c.rateLimit {
go f()
time.Sleep(time.Second)
}
}
func (c *Cannula) Listen(laddr string) error {
l, err := net.Listen("tcp", laddr)
if err != nil {
return err
}
c.listener = l
defer l.Close()
c.prefix = &irc.Prefix{
Name: "irc.septapus.com",
}
c.in = make(chan interface{}, 1000)
c.clients = make(map[*irc.Prefix]*Client)
c.ytClients = make(map[string]*YTClient)
c.channels = make(map[string]*Channel)
c.names = make(map[string]*Client)
c.rateLimit = make(chan func(), 1000)
go c.handle()
go c.handleRateLimit()
for {
conn, err := l.Accept()
if err != nil {
return err
}
go c.handleConn(conn)
}
}
func (c *Cannula) Exit() {
for _, cl := range c.clients {
cl.in <- &irc.Message{c.prefix, irc.NOTICE, []string{cl.Prefix.Name}, "IRC server is being restarted.", false}
}
}