-
Notifications
You must be signed in to change notification settings - Fork 1
/
bouncer.py
1183 lines (1072 loc) · 60.4 KB
/
bouncer.py
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
import socket
import ssl
import os
import re
import time
import sys
import string
import hashlib
import traceback
import irc
import getpass
from threading import Thread
from threading import RLock as Lock
import Queue
import chardet
import modjson
dec = modjson.ModJSONDecoder()
enc = modjson.ModJSONEncoder(indent=3)
# TODO: Rewrite this *entire* module and make more efficient.
_listnumerics = dict(b=(367, 368, "channel ban list"),
e=(348, 349, "Channel Exception List"),
I=(346, 347, "Channel Invite Exception List"),
w=(910, 911, "Channel Access List"),
g=(941, 940, "chanel spamfilter list"),
X=(954, 953, "channel exemptchanops list"))
def BouncerReload(BNC):
networks, configs = zip(*BNC.conf.items())
json = enc.encode([BNC, configs])
if BNC.isAlive():
BNC.stop()
newBNC, newconfs = dec.decode(json)
for network, newconf in zip(networks, newconfs):
network.rmAddon(BNC)
network.addAddon(**newconf)
return newBNC
class Bouncer (Thread):
__name__ = "Bouncer for pyIRC"
__version__ = "2.0"
__author__ = "Brian Sherson"
__date__ = "February 21, 2014"
def __init__(self, addr="", port=16667, secure=False, ipv6=False, certfile=None, keyfile=None, ignore=None, debug=False, timeout=300, autoaway=None, servname="bouncer.site"):
self.addr = addr
self.port = port
self.conf = {}
self.passwd = {}
self.socket = None
self.secure = secure
self.ipv6 = ipv6
self.certfile = certfile
self.keyfile = keyfile
self.clients = []
self.ignore = ignore
self.debug = debug
self.timeout = timeout
self.autoaway = autoaway
self.servname = servname
self._stopexpected = False
# Keep track of what extensions/clients are requesting WHO, WHOIS, and LIST, because we don't want to spam every bouncer connection with the server's replies.
# In the future, MAY implement this idea in the irc module.
self._whoexpected = {}
self._whoisexpected = {}
self._listexpected = {}
self.lock = Lock()
self.starttime = int(time.time())
Thread.__init__(self)
self.daemon = True
self.start()
def __repr__(self):
h = hash(self)
return "<Bouncer listening on {self.addr}:{self.port} at 0x{h:x}0>".format(**vars())
def run(self):
self.socket = socket.socket(
socket.AF_INET6 if self.ipv6 else socket.AF_INET)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((self.addr, self.port))
self.socket.listen(5)
#print ((self,"Now listening on port "+str(self.port)))
while True:
try:
(connection, addr) = self.socket.accept()
if self.secure:
connection = ssl.wrap_socket(
connection, server_side=True, certfile=self.certfile, keyfile=self.keyfile, ssl_version=ssl.PROTOCOL_SSLv23)
#print ((self,"New client connecting from %s:%s"%addr))
except socket.error:
# print "Shutting down Listener"
self.socket.close()
if not self._stopexpected:
raise
sys.exit()
except:
tb = traceback.format_exc()
print >>sys.stderr, tb
continue
connection.settimeout(self.timeout)
bouncer = BouncerConnection(
self, connection, addr, debug=self.debug)
time.sleep(0.5)
try:
self.socket.close()
except:
pass
self.socket = None
Thread.__init__(self)
self.daemon = True
def onAddonAdd(self, context, label, passwd=None, hashtype="sha512", ignore=None, autoaway=None, translations=[], hidden=[]):
for (context2, conf2) in self.conf.items():
if context == context2:
raise ValueError, "Context already exists in config."
if label == conf2.label:
raise ValueError, "Unique label required."
if passwd == None:
while True:
passwd = getpass.getpass("Enter new password: ")
if passwd == getpass.getpass("Confirm new password: "):
break
print "Passwords do not match!"
passwd = hashlib.new(hashtype, passwd).hexdigest()
conf = irc.Config(self, label=label, passwd=passwd, hashtype=hashtype, ignore=ignore, autoaway=autoaway, translations=[
(key if type(key) == irc.Channel else context[key], value) for key, value in translations], hidden=irc.ChanList(hidden, context=context))
self.conf[context] = conf
self._whoexpected[context] = []
if self.debug:
context.logwrite(
"dbg [Bouncer.onAddonAdd] Clearing WHO expected list." % vars())
self._whoisexpected[context] = []
self._listexpected[context] = []
return conf
def onAddonRem(self, context):
for client in self.clients:
if client.context == context:
client.quit(quitmsg="Bouncer extension removed")
del self.conf[context]
del self._whoexpected[context], self._whoisexpected[
context], self._listexpected[context]
def stop(self, disconnectall=False):
self._stopexpected = True
self.socket.shutdown(0)
if disconnectall:
self.disconnectall()
def disconnectall(self, quitmsg="Disconnecting all sessions"):
for client in self.clients:
client.quit(quitmsg=quitmsg)
def onDisconnect(self, context, expected=False):
self._whoexpected[context] = []
self._whoisexpected[context] = []
self._listexpected[context] = []
if context.identity:
for channel in context.identity.channels:
self.broadcast(context, origin=context.identity, cmd="PART", target=channel, extinfo="Bouncer Connection Lost", clients=[
client for client in self.clients if channel not in client.hidden])
self.broadcast(context, origin=context.identity,
cmd="QUIT", extinfo="Bouncer Connection Lost")
self.broadcast(
context, origin=self.servname, cmd="NOTICE", target=context.identity,
extinfo=":Connection to %s:%s has been lost." % (context.server, context.port))
def onQuit(self, context, user, quitmsg):
# For some odd reason, certain networks (*cough*Freenode*cough*) will send a quit message for the user, causing context.identity.channels to be cleared
# before onDisconnect can be executed. This is the remedy.
if user == context.identity:
for channel in context.identity.channels:
self.broadcast(context, origin=user, cmd="PART", target=channel, extinfo="Bouncer Connection Lost", clients=[
client for client in self.clients if channel not in client.hidden])
self.broadcast(context, origin=user, cmd="QUIT", extinfo=quitmsg, clients=[
client for client in self.clients if any([user in channel for channel in context.channels if channel not in client.hidden])])
def onConnectAttempt(self, context):
self.broadcast(
context, origin=self.servname, cmd="NOTICE", target=context.identity,
extinfo="Attempting connection to %s:%s." % (context.server, context.port))
def onConnect(self, context):
self.broadcast(
context, origin=self.servname, cmd="NOTICE", target=context.identity,
extinfo="Connection to %s:%s established." % (context.server, context.port))
def onMeNickChange(self, context, newnick):
for client in self.clients:
if client.context == context:
client.send(
origin=context.identity, cmd="NICK", target=newnick)
client.nick = newnick
def onNickChange(self, context, user, newnick):
self.broadcast(context, origin=user, cmd="NICK", target=newnick, clients=[
client for client in self.clients if any([user in channel for channel in context.channels if channel not in client.hidden])])
def onRegistered(self, context):
for client in self.clients:
if client.context == context:
if client.nick != context.identity.nick:
client.send(origin="%s!%s@%s" %
(client.nick, client.username, client.host), cmd="NICK", target=context.identity.nick)
client.nick = context.identity.nick
def onConnectFail(self, context, exc, excmsg, tb):
for client in self.clients:
if client.context == context:
client.send(
origin=self.servname, cmd="NOTICE", target=client.nick,
extinfo="Connection to %s:%s failed: %s." % (context.server, context.port, excmsg))
def onSendChanMsg(self, context, origin, channel, targetprefix, msg):
# Called when bot sends a PRIVMSG to channel.
# The variable origin refers to a class instance voluntarily
# identifying itself as that which requested data be sent.
self.broadcast(
context, origin=context.identity, cmd="PRIVMSG", targetprefix=targetprefix,
target=channel, extinfo=msg, clients=[client for client in self.clients if client != origin])
def onSendChanAction(self, context, origin, channel, targetprefix, action):
self.onSendChanMsg(
context, origin, channel, targetprefix, u"\x01ACTION {action}\x01".format(**vars()))
def onSendChanNotice(self, context, origin, channel, targetprefix, msg):
# Called when bot sends a NOTICE to channel.
# The variable origin refers to a class instance voluntarily
# identifying itself as that which requested data be sent.
self.broadcast(
context, origin=context.identity, cmd="NOTICE", targetprefix=targetprefix,
target=channel, extinfo=msg, clients=[client for client in self.clients if client != origin])
def onSend(self, context, origin, line, cmd, target, targetprefix, params, extinfo):
if cmd.upper() == "WHO":
self._whoexpected[context].append(origin)
if self.debug:
if issubclass(type(origin), Thread):
name = origin.name
context.logwrite(
"dbg [Bouncer.onSend] Adding {origin} ({name}) to WHO expected list.".format(**vars()))
else:
context.logwrite(
"dbg [Bouncer.onSend] Adding %(origin)s to WHO expected list." % vars())
context.logwrite(
"dbg [Bouncer.onSend] WHO expected list size: %d" % len(self._whoexpected[context]))
elif cmd.upper() == "WHOIS":
self._whoisexpected[context].append(origin)
elif cmd.upper() == "LIST":
self._listexpected[context].append(origin)
def onWhoEntry(self, context, origin, channel, user, channame, username, host, serv, nick, flags, hops, realname):
# Called when a WHO list is received.
if len(self._whoexpected[context]):
client = self._whoexpected[context][0]
if client in self.clients:
client.send(origin=origin, cmd=352, target=context.identity, params=u"{channame} {username} {host} {serv} {nick} {flags}".format(
**vars()), extinfo=u"{hops} {realname}".format(**vars()))
# client.send(":%s 352 %s %s %s %s %s %s %s :%s %s\n"%(origin, context.identity.nick, channame, username, host, serv, nick, flags, hops, realname))
def onWhoEnd(self, context, origin, param, endmsg):
# Called when a WHO list is received.
if len(self._whoexpected[context]) and self._whoexpected[context][0] in self.clients:
client = self._whoexpected[context][0]
client.send(
origin=origin, cmd=315, target=context.identity, params=param, extinfo=endmsg)
#client.send(":%s 315 %s %s :%s\n"%(origin, context.identity.nick, param, endmsg))
if self.debug:
if issubclass(type(self._whoexpected[context][0]), Thread):
name = self._whoexpected[context][0].name
context.logwrite(
"dbg [Bouncer.onWhoEnd] Removing %s (%s) from WHO expected list." %
(self._whoexpected[context][0], name))
else:
context.logwrite(
"dbg [Bouncer.onWhoEnd] Removing %s from WHO expected list." % self._whoexpected[context][0])
del self._whoexpected[context][0]
if self.debug:
context.logwrite(
"dbg [Bouncer.onWhoEnd] WHO expected list size: %d" %
len(self._whoexpected[context]))
def onListStart(self, context, origin, params, extinfo):
# Called when a WHO list is received.
if len(self._listexpected[context]) and self._listexpected[context][0] in self.clients:
client = self._listexpected[context][0]
client.send(origin=origin, cmd=321,
target=context.identity, params=params, extinfo=extinfo)
#client.send(":%s 321 %s %s :%s\n"%(origin, context.identity.nick, params, extinfo))
def onListEntry(self, context, origin, channel, population, extinfo):
# Called when a WHO list is received.
if len(self._listexpected[context]) and self._listexpected[context][0] in self.clients:
client = self._listexpected[context][0]
client.send(origin=origin, cmd=322, target=context.identity,
params=u"{channel.name} {population}".format(**vars()), extinfo=extinfo)
# client.send(":%s 322 %s %s %d :%s\n"%(origin, context.identity.nick, channame, population, extinfo))
def onListEnd(self, context, origin, endmsg):
# Called when a WHO list is received.
if len(self._listexpected[context]) and self._listexpected[context][0] in self.clients:
client = self._listexpected[context][0]
client.send(
origin=origin, cmd=323, target=context.identity, extinfo=endmsg)
# client.send(":%s 323 %s :%s\n"%(origin, context.identity.nick, endmsg))
del self._listexpected[context][0]
def onWhoisStart(self, context, origin, user, nickname, username, host, realname):
# Called when a WHOIS reply is received.
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(origin=origin, cmd=311, target=context.identity,
params=u"{nickname} {username} {host} *".format(**vars()), extinfo=realname)
# client.send(":%s 311 %s %s %s %s * :%s\n" % (origin, context.identity.nick, nickname, username, host, realname))
def onWhoisRegisteredNick(self, context, origin, user, nickname, msg):
# Called when a WHOIS reply is received.
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(
origin=origin, cmd=307, target=context.identity, params=nickname, extinfo=msg)
# client.send(":%s 307 %s %s :%s\n" % (origin, context.identity.nick, nickname, msg))
def onWhoisConnectingFrom(self, context, origin, user, nickname, msg):
# Called when a WHOIS reply is received.
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(origin=origin, cmd=378,
target=context.identity, params=nickname, extinfo=msg)
# client.send(":%s 378 %s %s :%s\n" % (origin, context.identity.nick, nickname, msg))
def onWhoisChannels(self, context, origin, user, nickname, chanlist):
# Called when a WHOIS reply is received.
# TODO: Translations implementation
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(origin=origin, cmd=319, target=context.identity,
params=nickname, extinfo=" ".join(chanlist))
# client.send(":%s 319 %s %s :%s\n" % (origin, context.identity.nick, nickname, " ".join(chanlist)))
def onWhoisAvailability(self, context, origin, user, nickname, msg):
# Called when a WHOIS reply is received.
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(
origin=origin, cmd=310, target=context.identity, params=nickname, extinfo=msg)
# client.send(":%s 310 %s %s :%s\n" % (origin, context.identity.nick, nickname, msg))
def onWhoisServer(self, context, origin, user, nickname, server, servername):
# Called when a WHOIS reply is received.
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(origin=origin, cmd=312, target=context.identity,
params=u"{nickname} {server}".format(**vars()), extinfo=servername)
# client.send(":%s 312 %s %s %s :%s\n" % (origin, context.identity.nick, nickname, server, servername))
def onWhoisOp(self, context, origin, user, nickname, msg):
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(
origin=origin, cmd=313, target=context.identity, params=nickname, extinfo=msg)
# client.send(":%s 313 %s %s :%s\n" % (origin, context.identity.nick, nickname, msg))
def onWhoisAway(self, context, origin, user, nickname, awaymsg):
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(origin=origin, cmd=301, target=context.identity,
params=u"{nickname} {idletime} {signontime}".format(**vars()), extinfo=awaymsg)
# client.send(":%s 301 %s %s :%s\n" % (origin, context.identity.nick, nickname, awaymsg))
def onWhoisTimes(self, context, origin, user, nickname, idletime, signontime, msg):
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(origin=origin, cmd=317, target=context.identity,
params=u"{nickname} {idletime} {signontime}".format(**vars()), extinfo=msg)
# client.send(":%s 317 %s %s %d %d :%s\n" % (origin, context.identity.nick, nickname, idletime, signontime, msg))
def onWhoisSSL(self, context, origin, user, nickname, msg):
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(origin=origin, cmd=671,
target=context.identity, params=nickname, extinfo=msg)
# client.send(":%s 671 %s %s :%s\n" % (origin, context.identity.nick, nickname, msg))
def onWhoisModes(self, context, origin, user, nickname, msg):
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(
origin=origin, cmd=339, target=context.identity, params=nickname, extinfo=msg)
# ":%s 339 %s %s :%s\n" % (origin, context.identity.nick, nickname, msg))
def onWhoisLoggedInAs(self, context, origin, user, nickname, loggedinas, msg):
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(origin=origin, cmd=330, target=context.identity,
params=" ".join((nickname, loggedinas)), extinfo=msg)
# ":%s 330 %s %s %s :%s\n" % (origin, context.identity.nick, nickname, loggedinas, msg))
def onWhoisEnd(self, context, origin, user, nickname, msg):
if len(self._whoisexpected[context]) and self._whoisexpected[context][0] in self.clients:
client = self._whoisexpected[context][0]
client.send(origin=origin, cmd=318,
target=context.identity, params=nickname, extinfo=msg)
# ":%s 318 %s %s :%s\n" % (origin, context.identity.nick, nickname, msg)
del self._whoisexpected[context][0]
def onJoin(self, context, user, channel):
self.broadcast(context, origin=user, cmd="JOIN", target=channel, clients=[
client for client in self.clients if channel not in client.hidden])
def onOther(self, context, line, origin, cmd, target, params, extinfo, targetprefix):
conf = self.conf[context]
self.broadcast(
context, origin=origin, cmd=cmd, target=target, params=params, extinfo=extinfo,
targetprefix=targetprefix, clients=[client for client in self.clients if target not in client.hidden])
def broadcast(self, context, origin=None, cmd=None, target=None, params=None, extinfo=None, targetprefix=None, clients=None):
if clients == None:
clients = self.clients
for client in clients:
with client.lock:
if client.context == context and not client.quitting:
client.send(
origin, cmd, target, params, extinfo, targetprefix)
class BouncerConnection (Thread):
def __init__(self, bouncer, connection, addr, debug=False):
# print "Initializing ListenThread..."
self.bouncer = bouncer
self.connection = connection
self.host, self.port = self.addr = addr[:2]
self.context = None
self.pwd = None
self.nick = None
self.label = None
self.username = None
self.realname = None
self.addr = addr
self.debug = debug
self.lock = Lock()
self.quitmsg = "Connection Closed"
self.quitting = False
self.hidden = irc.ChanList()
self.translations = {}
self.namesx = False
self.uhnames = False
Thread.__init__(self)
self.daemon = True
self.start()
def sendstr(self, data, flags=0):
with self.lock:
try:
self.connection.send(data.encode("utf8"))
except socket.error:
exc, excmsg, tb = sys.exc_info()
print >>self.context.logwrite(*["!!! [BouncerConnection.send] Exception in thread %(self)s" % vars()] + [
"!!! [BouncerConnection.send] %(tbline)s" % vars() for tbline in traceback.format_exc().split("\n")])
self.quit(quitmsg=excmsg.message)
# Format and send a string to the client
def send(self, origin=None, cmd=None, target=None, params=None, extinfo=None, targetprefix=None, flags=0):
if type(target) == irc.Channel:
if targetprefix == None:
targetprefix = ""
# if target in self.translations.keys():
# target=targetprefix+self.translations[target]
# else:
# target=targetprefix+target.name
target = targetprefix + target.name
elif type(target) == irc.User:
target = target.nick
if type(cmd) == int:
cmd = "%03d" % cmd
# translated=[]
# if params:
# for param in params.split(" "):
#chantypes=self.context.supports.get("CHANTYPES", irc._defaultchantypes)
# if re.match(irc._chanmatch % re.escape(chantypes), param) and self.context[param] in self.translations.keys():
# translated.append(self.translations[self.context[param]])
# else:
# translated.append(param)
#params=" ".join(translated)
if params:
line = u"{cmd} {target} {params}".format(**vars())
elif target:
line = u"{cmd} {target}".format(**vars())
else:
line = cmd
if extinfo != None:
line = u"{line} :{extinfo}".format(**vars())
if type(origin) == irc.User:
line = u":{origin:full} {line}".format(**vars())
elif origin:
line = u":{origin} {line}".format(**vars())
self.sendstr(u"{line}\n".format(**vars()))
#server=self.context.server if self.context else "*"
#port=self.context.port if self.context else "*"
# if self.context and self.context.identity:
# nick=self.context.identity.nick
#ident=self.context.identity.username if self.context.identity.username else "*"
#host=self.context.identity.host if self.context.identity.host else "*"
# else:
# nick="*"
# ident="*"
# host="*"
# if self.context.ssl and self.context.ipv6:
# protocol="ircs6"
# elif self.context.ssl:
# protocol="ircs"
# elif self.context.ipv6:
# protocol="irc6"
# else:
# protocol="irc"
# addr=self.host
def __repr__(self):
return "<Bouncer connection from {self.host} to {self.context.identity} on {self.context:uri}>".format(**vars())
def quit(self, quitmsg="Disconnected"):
with self.lock:
if not self.quitting:
self.quitmsg = quitmsg
try:
self.send(cmd="ERROR", extinfo="Closing link: (%s@%s) [%s]\n" % (
self.context.identity.nick if self.context else "*", self.host, quitmsg))
except:
pass
try:
self.connection.shutdown(socket.SHUT_WR)
self.connection.close()
except:
pass
self.quitting = True
def showchannel(self, channel):
with self.context.lock, self.lock:
if channel in self.hidden:
self.hidden.remove(channel)
if self.context.identity in channel.users:
self.send(
origin=self.context.identity, cmd="JOIN", target=channel)
self.sendchanneltopic(channel)
self.sendchannelnames(channel)
def sendchanneltopic(self, channel):
with self.context.lock, self.lock:
if channel.topic and channel.topictime:
self.send(origin=self.bouncer.servname, cmd=332,
target=self.context.identity, params=channel.name, extinfo=channel.topic)
# u":{self.context.serv} 332 {self.context.identity.nick} {self.name} :{self.topic}".format(**vars())
self.send(
origin=self.bouncer.servname, cmd=333, target=self.context.identity,
params="{channel.name} {channel.topicsetby} {channel.topictime}".format(**vars()))
# u":{self.context.serv} 333 {self.context.identity.nick} {self.name} {self.topicsetby.nick} {self.topictime}".format(**vars())
else:
self.send(origin=self.bouncer.servname, cmd=331,
target=self.context.identity, params=channel.name, extinfo="No topic is set")
# u":{self.context.serv} 331 {self.context.identity.nick}
# {self.name} :No topic is set".format(**vars())]
def sendchannelnames(self, channel):
with self.context.lock, self.lock:
secret = "s" in channel.modes.keys() and channel.modes["s"]
private = "p" in channel.modes.keys() and channel.modes["p"]
flag = "@" if secret else ("*" if private else "=")
modes, symbols = supports = self.context.supports.get(
"PREFIX", irc._defaultprefix)
users = list(channel.users)
users.sort(key=lambda user: ([user not in channel.modes.get(mode, [])
for mode, char in zip(*supports)], user.nick.lower()))
if self.uhnames:
template = u"{prefixes}{user:full}"
else:
template = u"{prefixes}{user}"
nameslist = []
for user in users:
prefixes = u"".join(
[prefix if mode in channel.modes.keys() and user in channel.modes[mode] else "" for prefix, mode in zip(symbols, modes)])
if not self.namesx:
prefixes = prefixes[:1]
nameslist.append(template.format(**vars()))
names = " ".join(nameslist)
lines = []
while len(names) > 196:
index = names.rfind(" ", 0, 196)
slice = names[:index]
self.send(
origin=self.bouncer.servname, cmd=353, target=self.context.identity,
params="{flag} {channel.name}".format(**vars()), extinfo=slice)
#u":{channel.context.serv} 353 {channel.context.identity.nick} {flag} {channel.name} :{slice}".format(**vars())
names = names[index + 1:]
if len(names):
self.send(
origin=self.bouncer.servname, cmd=353, target=self.context.identity,
params="{flag} {channel.name}".format(**vars()), extinfo=names)
#u":{channel.context.serv} 353 {channel.context.identity.nick} {flag} {channel.name} :{names}".format(**vars())
self.send(
origin=self.bouncer.servname, cmd=366, target=self.context.identity,
params=channel.name, extinfo="End of /NAMES list.")
# u":{channel.context.serv} 366 {channel.context.identity.nick} {channel.name} :End of /NAMES list.".format(**vars())
def sendchannelmodes(self, channel, modechars=None):
with self.context.lock, self.lock:
if modechars:
for mode in modechars:
if mode not in _listnumerics.keys():
continue
i, e, l = _listnumerics[mode]
if mode in channel.modes.keys():
for (mask, setby, settime) in channel.modes[mode]:
self.send(
origin=self.bouncer.servname, cmd=i, target=self.context.identity,
params=u"{channel.name} {mask} {setby} {settime}".format(**vars()))
self.send(origin=self.bouncer.servname, cmd=e,
target=self.context.identity, params=u"{channel.name} {l}".format(**vars()))
else:
items = channel.modes.items()
chanmodes = self.context.supports.get(
"CHANMODES", irc._defaultchanmodes)
prefix = self.context.supports.get(
"PREFIX", irc._defaultprefix)
modes = "".join(
[mode for (mode, val) in items if mode not in chanmodes[0] + prefix[0] and val])
params = " ".join(
[val for (mode, val) in items if mode in chanmodes[1] + chanmodes[2] and val])
if modes and params:
self.send(
origin=self.bouncer.servname, cmd=324, target=self.context.identity,
params="{channel.name} +{modes} {params}".format(**vars()))
# u":{channel.context.identity.server} 324 {channel.context.identity.nick} {channel.name} +{modes} {params}".format(**vars())
elif modes:
self.send(
origin=self.bouncer.servname, cmd=324, target=self.context.identity,
params="{channel.name} +{modes}".format(**vars()))
# u":{channel.context.identity.server} 324 {channel.context.identity.nick} {channel.name} +{modes}".format(**vars())
def sendsupports(self):
with self.context.lock, self.lock:
supports = [
"CHANMODES=%s" % (",".join(value)) if name == "CHANMODES" else "PREFIX=(%s)%s" %
value if name == "PREFIX" else "%s=%s" % (name, value) if value else name for name, value in self.context.supports.items()]
if "UHNAMES" not in supports:
supports.append("UHNAMES")
if "NAMESX" not in supports:
supports.append("NAMESX")
supports.sort()
supports = " ".join(supports)
lines = []
while len(supports) > 196:
index = supports.rfind(" ", 0, 196)
slice = supports[:index]
self.send(
origin=self.bouncer.servname, cmd=5, target=self.context.identity,
params=slice, extinfo="are supported by this server")
# u":{self.context.serv} 005 {self.context.identity.nick} {slice} :are supported by this server".format(**vars())
supports = supports[index + 1:]
if supports:
self.send(
origin=self.bouncer.servname, cmd=5, target=self.context.identity,
params=supports, extinfo="are supported by this server")
# u":{self.context.serv} 005 {self.context.identity.nick} {supports} :are supported by this server".format(**vars())
def sendgreeting(self):
with self.context.lock, self.lock:
if self.context.welcome:
self.send(origin=self.bouncer.servname, cmd=1,
target=self.context.identity, extinfo=self.context.welcome)
# u":{self.context.serv} 001 {self.context.identity.nick} :{self.context.welcome}".format(**vars())
if self.context.hostinfo:
self.send(origin=self.bouncer.servname, cmd=2,
target=self.context.identity, extinfo=self.context.hostinfo)
# u":{self.context.serv} 002 {self.context.identity.nick} :{self.context.hostinfo}".format(**vars())
if self.context.servcreated:
self.send(origin=self.bouncer.servname, cmd=3,
target=self.context.identity, extinfo=self.context.servcreated)
# u":{self.context.serv} 003 {self.context.identity.nick} :{self.context.servcreated}".format(**vars())
if self.context.servinfo:
self.send(origin=self.bouncer.servname, cmd=4,
target=self.context.identity, params=self.context.servinfo)
# u":{self.context.serv} 004 {self.context.identity.nick} {self.context.servinfo}".format(**vars())
def sendmotd(self):
with self.context.lock, self.lock:
if self.context.motdgreet and self.context.motd and self.context.motdend:
self.send(origin=self.bouncer.servname, cmd=375,
target=self.context.identity, extinfo=self.context.motdgreet)
# u":{server} 375 {self.identity.nick} :{self.motdgreet}".format(**vars())
for motdline in self.context.motd:
self.send(origin=self.bouncer.servname, cmd=372,
target=self.context.identity, extinfo=motdline)
# u":{server} 372 {self.identity.nick} :{motdline}".format(**vars())
self.send(origin=self.bouncer.servname, cmd=376,
target=self.context.identity, extinfo=self.context.motdend)
# u":{server} 376 {self.identity.nick} :{self.motdend}".format(**vars())
else:
self.send(origin=self.bouncer.servname, cmd=422,
target=self.context.identity, extinfo="MOTD File is missing")
# u":{server} 422 {self.identity.nick} :MOTD File is missing".format(**vars())
def sendusermodes(self):
with self.context.lock, self.lock:
self.send(
origin=self.bouncer.servname, cmd=221, target=self.context.identity,
params="+{self.context.identity.modes}".format(**vars()))
if "s" in self.context.identity.modes:
self.send(
origin=self.bouncer.servname, cmd=8, target=self.context.identity,
params="+{self.context.identity.snomask}".format(**vars()), extinfo="Server notice mask")
def run(self):
# Name loopup should happen here instead
ipv4match = re.findall(
r"^::ffff:((\d+)\.(\d+)\.(\d+)\.(\d+))$", self.host)
if self.bouncer.ipv6 and ipv4match:
addr, a, b, c, d = ipv4match[0]
if max(int(a), int(b), int(c), int(d)) < 256:
self.host = addr
self.ipv6 = False
elif self.bouncer.ipv6:
self.ipv6 = True
try:
self.host, aliaslist, addresslist = socket.gethostbyaddr(self.host)
self.addr = (self.host, addr[1])
except:
pass
# Add connection to connection list.
passwd = None
nick = None
user = None
addr = self.host
readbuf = ""
linebuf = []
try:
while True:
# Read data (appending) into readbuf, then break lines and
# append lines to linebuf
while len(linebuf) == 0:
timestamp = irc.timestamp()
try:
read = self.connection.recv(512)
except socket.error, msg:
self.quit(msg)
sys.exit()
except ssl.SSLError, msg:
self.quit(msg)
sys.exit()
if read == "" and len(linebuf) == 0: # No more data to process.
#self.quitmsg="Connection Closed"
sys.exit()
readbuf += read
lastlf = readbuf.rfind("\n")
if lastlf >= 0:
linebuf.extend(string.split(readbuf[0:lastlf], "\n"))
readbuf = readbuf[lastlf + 1:]
line = string.rstrip(linebuf.pop(0))
try:
line = line.decode("utf8")
except UnicodeDecodeError:
# Attempt to figure encoding
charset = chardet.detect(line)['encoding']
line = line.decode(charset)
match = re.findall(
"^(.+?)(?:\\s+(.+?)(?:\\s+(.+?))??)??(?:\\s+:(.*))?$", line, re.I)
# print match
if len(match) == 0:
continue
(cmd, target, params, extinfo) = match[0]
if not passwd: # Bouncer expects a password
if cmd.upper() == "PASS":
passwd = target if target else extinfo
else:
self.quit("Access Denied")
print "*** [BouncerConnection] Incoming connection from %s failed: Expected PASS." % (self.host)
break
elif not self.nick: # Bouncer expects a NICK command
if cmd.upper() == "NICK":
self.nick = target if target else extinfo
else:
self.quit("Access Denied")
print "*** [BouncerConnection] Incoming connection from %s failed: Expected NICK." % (self.host)
break
elif not self.username: # Bouncer expects a USER command to finish registration
if cmd.upper() == "USER":
self.username = target
contextfound = False
for self.context, conf in self.bouncer.conf.items():
# print conf.label, self.username
if conf.label == self.username:
contextfound = True
break
if not contextfound:
self.quit("Access Denied")
print >>sys.stderr, "*** [BouncerConnection] Incoming connection from %s denied: Context not found." % (
self.host)
break
passmatch = hashlib.new(
conf.hashtype, passwd).hexdigest() == conf.passwd
with self.context.lock:
if not passmatch:
self.quit("Access Denied")
self.context.logwrite(
"*** [BouncerConnection] Incoming connection from %s to %s denied: Invalid password." % (self.host, self.context))
self.bouncer.broadcast(
self.context, origin=self.bouncer.servname, cmd="NOTICE", target=self.context.identity,
extinfo="Incoming connection from %s to %s denied: Invalid password." % (self.host, self.context))
# for client in self.bouncer.clients:
# if client.context!=self.context:
# continue
# if not client.quitting:
#client.send(origin=self.bouncer.servname, cmd="NOTICE", target=client.context.identity, extinfo="Incoming connection from %s to %s dened: Invalid password.\n" % (self.host, self.context))
break
self.context.logwrite(
"*** [BouncerConnection] Incoming connection from %s to %s established." % (self.host, self.context))
with self.bouncer.lock:
self.translations = dict(
self.bouncer.conf[self.context].translations)
# Announce connection to all other bouncer
# clients.
self.bouncer.broadcast(
self.context, origin=self.bouncer.servname, cmd="NOTICE", target=self.context.identity,
extinfo="Incoming connection from %s to %s established." % (self.host, self.context))
# for client in self.bouncer.clients:
# if client.context!=self.context:
# continue
# if not client.quitting:
#client.send(":*Bouncer* NOTICE %s :Incoming connection from %s to %s\n" % (client.context.identity.nick, self.host, self.context))
if len([client for client in self.bouncer.clients if client.context == self.context]) == 0 and self.context.registered and type(self.context.identity) == irc.User and self.context.identity.away:
# Bouncer connection should
# automatically return from away
# status.
self.context.raw("AWAY")
self.hidden = irc.ChanList(
self.bouncer.conf[self.context].hidden, context=self.context)
self.bouncer.clients.append(self)
if self.context.registered:
# Send Greeting.
with self.lock:
self.sendgreeting()
self.sendsupports()
self.sendmotd()
self.sendusermodes()
# Join user to channels.
for channel in self.context.identity.channels:
if channel not in self.hidden:
self.showchannel(channel)
else:
self.send(
origin=self.bouncer.servname, cmd="NOTICE", target=self.nick,
extinfo="Not connected to server. Type /bncconnect to attempt connection.")
#self.send(u":%s 001 %s :Welcome to the Bouncer context Network %s!%s@%s\n" % ("*Bouncer*", self.nick, self.nick, self.username, self.host))
else: # Client did not send USER command when expected
self.quit("Access Denied")
print "*** [BouncerConnection] Incoming connection from %s failed: Expected USER." % (self.host)
break
else:
chantypes = self.context.supports.get(
"CHANTYPES", irc._defaultchantypes)
# Disable translating for now.
if False and cmd.upper() not in ("SETTRANSLATE", "RMTRANSLATE"):
translated = []
for targ in target.split(","):
translatefound = False
if re.match(irc._chanmatch % re.escape(chantypes), targ):
for channel, translate in self.translations.items():
if targ.lower() == translate.lower():
translated.append(channel.name)
translatefound = True
break
if not translatefound:
translated.append(targ)
target = ",".join(translated)
translated = []
for param in params.split(" "):
translatefound = False
if re.match(irc._chanmatch % re.escape(chantypes), param):
for channel, translate in self.translations.items():
if param.lower() == translate.lower():
translated.append(channel.name)
translatefound = True
break
if not translatefound:
translated.append(param)
params = " ".join(translated)
if params:
line = u"{cmd} {target} {params}".format(**vars())
elif target:
line = u"{cmd} {target}".format(**vars())
else:
line = cmd
if extinfo:
line = u"{line} :{extinfo}".format(**vars())
cmdmethod = "cmd%s" % cmd.upper()
if hasattr(self, cmdmethod):
method = getattr(self, cmdmethod)
try:
method(line, target, params, extinfo)
except SystemExit:
sys.exit()
except:
if self.context:
exc, excmsg, tb = sys.exc_info()
self.context.logwrite(*[u"!!! [BouncerConnection] Exception in thread %(self)s" % vars()] + [
u"!!! [BouncerConnection] %(tbline)s" % vars() for tbline in traceback.format_exc().split("\n")])
print >>sys.stderr, "Exception in thread %(self)s" % vars(
)
print >>sys.stderr, traceback.format_exc()
elif not self.context.connected:
self.send(
origin=self.bouncer.servname, cmd="NOTICE", target=self.nick,
extinfo="Not connected to server. Type /bncconnect to attempt connection.")
continue
elif not self.context.registered:
self.send(origin=self.bouncer.servname, cmd="NOTICE",
target=self.nick, extinfo="Not registered.")
continue
else:
self.context.raw(line, origin=self)
except SystemExit:
pass # No need to pass error message if break resulted from sys.exit()
except:
exc, excmsg, tb = sys.exc_info()
self.quitmsg = str(excmsg)
if self.context:
exc, excmsg, tb = sys.exc_info()
self.context.logwrite(*["!!! [BouncerConnection] Exception in thread %(self)s" % vars()] + [
"!!! [BouncerConnection] %(tbline)s" % vars() for tbline in traceback.format_exc().split("\n")])
print >>sys.stderr, "Exception in thread %(self)s" % vars()
print >>sys.stderr, traceback.format_exc()
finally:
# Juuuuuuust in case.
with self.lock:
try:
self.connection.shutdown(1)
self.connection.close()
except:
pass
if self.context:
self.context.logwrite(
"*** [BouncerConnection] Connection from %s terminated (%s)." % (self.host, self.quitmsg))
with self.bouncer.lock:
if self in self.bouncer.clients:
self.bouncer.clients.remove(self)
if self.context.connected and self.context.identity and len([client for client in self.bouncer.clients if client.context == self.context]) == 0 and self.context.registered and type(self.context.identity) == irc.User and not self.context.identity.away and self.bouncer.autoaway:
# Bouncer automatically sets away status.
self.context.raw("AWAY :%s" % self.bouncer.autoaway)
self.bouncer.broadcast(
self.context, origin=self.bouncer.servname, cmd="NOTICE", target=self.context.identity,
extinfo="Connection from %s to %s terminated (%s)\n" % (self.host, self.context, self.quitmsg))
# ":*Bouncer* NOTICE %s :Connection from %s to %s terminated (%s)\n" % (client.context.identity.nick, self.host, self.context, self.quitmsg))
def cmdQUIT(self, line, target, params, extinfo):
self.quit(extinfo)
sys.exit()
def cmdPROTOCTL(self, line, target, params, extinfo):
protoparams = [target.upper()] + params.upper().split()
if "NAMESX" in protoparams: