-
Notifications
You must be signed in to change notification settings - Fork 7
/
IRCHandler.py
691 lines (564 loc) · 27.5 KB
/
IRCHandler.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
# coding=utf-8
import threading
import Logger
import re
import os
import traceback
import time
from collections import OrderedDict
EOL = os.linesep
CTCPTAG = chr(1)
###########################################################################################################
class Sender(object):
def __init__(self, sender):
self.nick = ""
self.regnick = ""
self.ident = ""
self.host = ""
self.auth = -1
self.authEvent = threading.Event()
self.whoisEvent = threading.Event()
self.dccSocket = None
self.lastAuth = 0
self.ctcpData = {}
self.ctcpEvent = {}
self.__msgQueue__ = []
self.lastMoreCmd = time.time() - 30
if not '!' in sender:
self.nick = sender[1:]
else:
self.nick = sender.split('!')[0][1:]
self.ident = sender.split('!')[1].split('@')[0]
self.host = sender.split('!')[1].split('@')[1]
def __repr__(self):
return "%s!%s@%s"%(self.nick, self.ident, self.host)
def __eq__(self, other):
return self.nick == other.nick and self.ident == other.ident and self.host == other.host
def __ne__(self, other):
return self.nick != other.nick or self.ident != other.ident or self.host != other.host
def toString(self):
return "%s!%s@%s"%(self.nick, self.ident, self.host)
def unauthenticate(self):
self.auth = -1
self.regnick = ""
self.authEvent.clear()
self.whoisEvent.clear()
def authenticate(self, level):
self.auth = level
self.authEvent.set()
self.lastAuth = time.time()
def addToMsgQueue(self, msg):
if self.__msgQueue__:
self.__msgQueue__.append(msg)
else:
self.__msgQueue__ = [msg]
def hasQueuedMsgs(self):
return self.__msgQueue__ and len(self.__msgQueue__) > 0
def getQueuedMsgCount(self):
return len(self.__msgQueue__) if self.__msgQueue__ else 0
def popQueuedMsg(self):
if self.hasQueuedMsgs():
return self.__msgQueue__.pop(0)
else:
return None
def clearMsgQueue(self):
self.__msgQueue__ = []
###########################################################################################################
class CmdGenerator(object):
@classmethod
def getPASS(cls, password):
return "PASS {password}".format(password=password) + EOL
@classmethod
def getNICK(cls, oldnick, nick):
return "NICK {nick}{eol}".format(nick = nick, eol = EOL)
@classmethod
def getUSER(cls, nick):
return "USER {username} {hostname} {servername} :{realname}".format(nick = nick,
username=nick,
hostname="none",
servername="none",
realname=nick) + EOL
@classmethod
def getPONG(cls, msg):
return "PONG {msg}".format(msg=msg) + EOL
@classmethod
def getJOIN(cls, channel, key=None):
if key:
return "JOIN {channel}; {key}".format(channel=channel, key=key) + EOL
else:
return "JOIN {channel}".format(channel=channel) + EOL
@classmethod
def getNOTICE(cls, dest, msg):
ret_val = ''
for line in msg.split('\n'):
if line and line.strip('\r') != '':
ret_val += "NOTICE {dest} :{msg}".format(dest = dest, msg = line.strip('\r')) + EOL
return ret_val
@classmethod
def getPRIVMSG(cls, dest, msg):
ret_val = ''
for line in msg.split('\n'):
if line and line.strip('\r') != '':
ret_val += "PRIVMSG {dest} :{msg}".format(dest = dest, msg = line.strip('\r')) + EOL
return ret_val
@classmethod
def getCTCP(cls, msg):
return "%s%s%s"%(CTCPTAG,msg,CTCPTAG)
@classmethod
def getWHOIS(cls, nick):
return "WHOIS {nick}".format(nick = nick) + EOL
@classmethod
def getDCCCHAT(cls, dest, addr, port):
ctcpmsg = CmdGenerator.getCTCP("DCC CHAT chat {addr} {port}".format(addr=CmdGenerator.conv_ip_std_long(addr), port=port))
privmsg = CmdGenerator.getPRIVMSG(dest, ctcpmsg)
return privmsg
@classmethod
def conv_ip_long_std(cls, longip):
try:
ip = long(longip)
except ValueError:
return longip
if ip >= 2 ** 32:
return longip
address = [str(ip >> shift & 0xFF) for shift in [24, 16, 8, 0]]
return '.'.join(address)
@classmethod
def conv_ip_std_long(cls, stdip):
if stdip.find(':') != -1:
return stdip
address = stdip.split('.')
if len(address) != 4:
return 0
longip = 0
for part, shift in zip(address, [24, 16, 8, 0]):
ip_part = int(part)
if ip_part >= 2 ** 8:
return stdip
longip += ip_part << shift
return longip
###########################################################################################################
class Color(object):
colors = {
'§B': '\x02',
'§U': '\x1f',
'§R': '\x16',
'§N': '\x0f',
'§C': '\x03',
'§I': '\x1D',
}
@classmethod
def doColors(cls, text):
out_text = text
for code, char in cls.colors.items():
out_text = out_text.replace(code, char)
return out_text
###########################################################################################################
def getDurationStr(timeint):
formatstr = '%M:%S'
if timeint >= 3600:
formatstr = '%H:%M:%S'
return time.strftime(formatstr, time.gmtime(timeint))
class CmdHandler(object):
def __init__(self, bot):
self.bot = bot
self.sender = ""
self.cmd = ""
self.params = []
self.logger = Logger.getLogger("%s-%s-%s"%(__name__, self.bot.nick, self.bot.host)+".CmdHandler", bot.lognormal, bot.logerrors)
self.commands = OrderedDict()
self.callbacks = {}
self.irccmds = {
'PING': {'callback':self.onPING},
'NOTICE': {'callback':self.onNOTICE},
'PRIVMSG':{'callback':self.onPRIVMSG},
'INVITE': {'callback':self.onINVITE},
'JOIN': {'callback':self.onJOIN},
'PART': {'callback':self.onPART},
'MODE': {'callback':self.onMODE},
'KICK': {'callback':self.onKICK},
'QUIT': {'callback':self.onQUIT},
'KILL': {'callback':self.onKILL},
'NICK': {'callback':self.onNICK},
}
self.rplcmds = {
001 : {'name':'RPL_WELCOME', 'callback':self.onRPL_WELCOME},
311 : {'name':'RPL_WHOISUSER', 'callback':self.onRPL_WHOISUSER},
312 : {'name':'RPL_WHOISSERVER', 'callback':self.onRPL_WHOISSERVER},
317 : {'name':'RPL_WHOISIDLE', 'callback':self.onRPL_WHOISIDLE},
319 : {'name':'RPL_WHOISCHANNELS', 'callback':self.onRPL_WHOISCHANNELS},
330 : {'name':'RPL_WHOISACCOUNT', 'callback':self.onRPL_WHOISACCOUNT},
376 : {'name':'RPL_ENDOFMOTD', 'callback':self.onRPL_ENDOFMOTD},
}
self.monitor_thread = None
self.last_event_time = time.time()
self.next_event_check = time.time()
if self.bot.monitorevents:
self.monitorEvents()
def monitorEvents(self):
delta = time.time() - self.last_event_time
if delta >= self.bot.monitortimeout:
self.logger.warning('Event Monitor: time since last event (%s) exceeds timeout (%s).' % (getDurationStr(delta), getDurationStr(self.bot.monitortimeout)))
self.bot.onShuttingDown()
else:
# self.logger.debug('Event Monitor: time since last event: %s' % getDurationStr(delta))
self.next_event_check = self.next_event_check + self.bot.monitorperiod
self.monitor_thread = threading.Timer(self.next_event_check - time.time(), self.monitorEvents)
self.monitor_thread.start()
def registerCommand(self, command, callback, groups, minarg, maxarg, descargs = None, desccmd=None, showhelp=True, allowpub=False, allowduringreadonly=True):
if not groups:
groups = ['any']
self.commands[command.lower()] = {'command':command.lower(), 'callback':callback, 'groups':groups, 'minarg':minarg, 'maxarg':maxarg,
'descargs':descargs, 'desccmd':desccmd, 'showhelp':showhelp, 'allowpub': allowpub, 'allowduringreadonly': allowduringreadonly}
for group in groups:
if not group in self.bot.groups:
#self.bot.groups[group]= set()
self.bot.groups[group] = {'commands':set()}
self.bot.groups[group]['commands'].add(command.lower())
self.bot.updateConfig()
def registerEvent(self, event, callback):
if not event in self.callbacks.keys():
self.callbacks[event] = []
self.callbacks[event].append(callback)
def handleEvents(self, event, sender, params):
self.last_event_time = time.time()
if event in self.callbacks:
for callback in self.callbacks[event]:
cmdThread = threading.Thread(target=self.threadedEvent, name="%s:%s"%(sender.toString(),event), args=(event, callback, self.bot, sender, params))
cmdThread.start()
def threadedEvent(self, event, callback, bot, sender, params):
try:
callback(bot, sender, params)
except Exception as e:
self.logger.error("Error while handling event [ %s ] with params %s from user [ %s ]"%(event, params, sender))
self.logger.error("%s"%e)
for line in traceback.format_exc().split('\n'):
self.logger.error(line)
def parseCmd(self, msg):
self.last_event_time = time.time()
msg = msg.strip()
if msg == "":
return
elems = msg.split()
if elems[0][0] == ':':
self.sender = Sender(elems[0])
elems.pop(0)
else:
self.sender = Sender("")
self.cmd = elems[0]
if len(elems) > 1:
self.params = elems[1:]
try:
cmd = int(self.cmd)
self.onNUMERICAL(self.sender, cmd, self.params)
return
except Exception as e:
pass
if self.cmd in self.irccmds:
try:
self.irccmds[self.cmd]['callback'](self.sender, self.params)
except Exception as e:
self.logger.error("Error while handling command [ %s ] with params %s from user [ %s ]"%(self.cmd, self.params, self.sender))
self.logger.error("%s"%e)
for line in traceback.format_exc().split('\n'):
self.logger.error(line)
else:
self.logger.debug("Unhandled Cmd > %s %s %s"%(self.sender, self.cmd, self.params))
#######################################################
#IRC Commands
def onPING(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
self.bot.sendRaw(CmdGenerator.getPONG(params[0]))
self.handleEvents('Ping', sender, params)
def onNOTICE(self, sender, params):
self.logger.info("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
#We received a notice from NickServ. We check if it is an ACC notice
if sender.nick.lower() == self.bot.nickserv.lower():
reMatch = re.match(self.bot.authRegex, " ".join(params[1:])[1:])
if reMatch:
#We check that the user exists in our tables, we setup the lvl value & unlock the command thread.
if not reMatch.group('nick') in self.bot.users:
self.logger.error("Received unkown user auth level : %s %s"%(reMatch.group('nick'), reMatch.group('level')))
else:
self.bot.users[reMatch.group('nick')].authenticate(int(reMatch.group('level')))
self.logger.info("Auth notice for %s with level %s"%(reMatch.group('nick'), reMatch.group('level')))
reMatch = re.match(self.bot.nsmarker, " ".join(params[1:])[1:])
if reMatch:
self.bot.sendRaw(self.bot.nsreply.format(nickserv=self.bot.nickserv, nspass=self.bot.nspass) + EOL)
reMatch = re.match(self.bot.nsidentify, " ".join(params[1:])[1:])
if reMatch:
for chan in self.bot.channels:
if not chan.strip() or not chan[0] == '#':
continue
self.bot.join(chan)
dest = params[0] #Chan or private dest
msg = ' '.join(params[1:]) #We stich the chat msg back together
msg = msg[1:] #We remove the leading :
if len(msg) == 0: return
if msg[0] == CTCPTAG and msg[-1] == CTCPTAG:
self.onCTCP(sender, dest, msg[1:-1].split())
else:
self.handleEvents('Notice', sender, params)
def onINVITE(self, sender, params):
self.logger.info("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
if self.bot.autoInvite:
self.bot.join(params[1])
self.handleEvents('Invite', sender, params)
def onJOIN(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
if sender.nick == self.bot.nick:
self.logger.info("Adding chan %s to chan list"%(params[0]))
if not params[0] in self.bot.channels:
self.bot.channels.add(params[0])
self.bot.updateConfig()
self.handleEvents('Join', sender, params)
def onPART(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
if sender.nick == self.bot.nick:
self.logger.info("Removing chan %s from chan list"%(params[0]))
self.bot.channels.remove(params[0])
self.bot.updateConfig()
if sender.nick in self.bot.users:
self.bot.users[sender.nick].unauthenticate()
self.bot.users[sender.nick].whoisEvent.clear()
self.handleEvents('Part', sender, params)
def onMODE(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
self.handleEvents('Mode', sender, params)
def onKICK(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
if params[1] == self.bot.nick:
self.logger.info("Removing chan %s from chan list"%(params[0]))
self.bot.channels.remove(params[0])
self.bot.updateConfig()
if sender.nick in self.bot.users:
self.bot.users[sender.nick].unauthenticate()
self.bot.users[sender.nick].whoisEvent.clear()
self.handleEvents('Kick', sender, params)
def onNICK(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
if sender.nick in self.bot.users:
self.bot.users[sender.nick].unauthenticate()
self.bot.users[sender.nick].whoisEvent.clear()
self.handleEvents('Nick', sender, params)
def onQUIT(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
self.handleEvents('Quit', sender, params)
if sender.nick in self.bot.users:
self.bot.users[sender.nick].unauthenticate()
self.bot.users[sender.nick].whoisEvent.clear()
def onKILL(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
self.handleEvents('Kill', sender, params)
def onPRIVMSG(self, sender, params):
dest = params[0] #Chan or private dest
msg = ' '.join(params[1:]) #We stitch the chat msg back together
msg = msg.lstrip(':') #We remove the leading :
if len(msg) > 0 and (msg[0] == self.bot.cmdChar or dest == self.bot.nick):
stripped = msg.lstrip(self.bot.cmdChar)
self.onCOMMAND(sender, dest, stripped, len(msg) - len(stripped)) #We remove the cmd symbol before passing the cmd to the bot
elif len(msg) > 0 and msg[0] == CTCPTAG and msg[-1] == CTCPTAG:
self.onCTCP(sender, dest, msg[1:-1].split())
else:
self.logger.debug("%s %s"%(sender.nick, " ".join(params)))
self.handleEvents('Privmsg', sender, params)
#######################################################
# Numerical RPL handling
def onNUMERICAL(self, sender, cmd, params):
if int(cmd) in self.rplcmds.keys():
self.rplcmds[cmd]['callback'](sender, params)
else:
self.logger.debug("%03d [S: %s] [M: %s]"%(cmd, sender.nick, " ".join(params)))
self.handleEvents(int(cmd), sender, params)
#RPL_001
def onRPL_WELCOME(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
if self.bot.autoJoin and not self.bot.isReady:
self.bot.isReady = True
if not self.bot.nspass:
for chan in self.bot.channels:
if not chan.strip() or not chan[0] == '#':
continue
self.bot.join(chan)
#RPL_311
def onRPL_WHOISUSER(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
nick = params[1]
if nick in self.bot.usersInfo:
self.bot.usersInfo[nick].ident = params[2]
self.bot.usersInfo[nick].host = params[3]
self.bot.usersInfo[nick].whoisEvent.set()
self.logger.debug("%s user added to user list with %s %s"%(nick, self.bot.usersInfo[nick].ident, self.bot.usersInfo[nick].host))
else:
self.logger.error("%s not found in the user list"%nick)
if params[1] in self.bot.users:
self.bot.users[params[1]].whoisEvent.set()
#RPL_312
def onRPL_WHOISSERVER(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
#RPL_317
def onRPL_WHOISIDLE(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
#RPL_319
def onRPL_WHOISCHANNELS(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
#RPL_330
def onRPL_WHOISACCOUNT(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
if params[1] in self.bot.users:
self.bot.users[params[1]].regnick = params[2]
self.bot.users[params[1]].whoisEvent.set()
# New auth based only on whois reply
self.bot.users[params[1]].authenticate(3)
self.logger.info("Auth notice for %s with level %s"%(params[1], 3))
#RPL_376
def onRPL_ENDOFMOTD(self, sender, params):
self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
#######################################################
# User defined commands
def onCOMMAND(self, sender, dest, command, cmd_char_cnt=0):
self.logger.info("COMMAND> %s"%command)
if command.strip() == "":
return
if not command.split()[0].lower() in self.commands.keys():
return
cmd = self.commands[command.split()[0].lower()]
args = command.split()[1:] if len(command.split()) > 1 else []
callback = cmd['callback']
# All commands that support public output should just use the dest param as the output target
# This allows commands to support public output when a command is prefixed with two command chars, eg "!!gm"
if cmd_char_cnt <= 1 or not cmd['allowpub']:
dest = sender.nick
if not cmd['allowduringreadonly'] and self.bot.readOnly:
self.bot.sendOutput(dest, '%s is currently in read-only mode. Please try your command again later.' % self.bot.nick)
return
if len(args) < cmd['minarg'] or len(args) > cmd['maxarg']:
if cmd['descargs']:
self.bot.sendOutput(dest, "Wrong syntax : %s" % cmd['descargs'])
return
else:
self.bot.sendOutput(dest, "Wrong number of arguments : min = %s, max = %s" % (cmd['minarg'], cmd['maxarg']))
return
#We already know this sender
if sender.nick in self.bot.users and sender == self.bot.users[sender.nick]:
self.logger.info("Found user %s in current users" % sender.nick)
if -1 < self.bot.authtimeout < time.time() - self.bot.users[sender.nick].lastAuth:
self.bot.users[sender.nick].unauthenticate()
# If current auth is not 3, we retry to authenticate the account but resending the request and resetting the flags
if self.bot.users[sender.nick].auth != 3:
self.logger.info("Reauthenticating user %s" % sender.nick)
self.bot.users[sender.nick].unauthenticate()
#self.bot.sendRaw(self.bot.nickAuth.format(nickserv=self.bot.nickserv, nick=sender.nick) + EOL)
self.bot.sendRaw(CmdGenerator.getWHOIS(sender.nick))
cmdThread = threading.Thread(target=self.threadedCommand, name=sender.toString(), args=(callback, cmd, self.bot, self.bot.users[sender.nick], dest, args))
cmdThread.start()
else:
self.logger.info("Adding and authenticating user %s" % sender.nick)
self.bot.users[sender.nick] = sender
#self.bot.sendRaw(self.bot.nickAuth.format(nickserv=self.bot.nickserv, nick=sender.nick) + EOL) # Removed to only allow whois auth verification
self.bot.sendRaw(CmdGenerator.getWHOIS(sender.nick))
cmdThread = threading.Thread(target=self.threadedCommand, name=sender.toString(), args=(callback, cmd, self.bot, self.bot.users[sender.nick], dest, args))
cmdThread.start()
def threadedCommand(self, callback, cmd, bot, sender, dest, args):
try:
#We request the AUTH level of the nick
if not sender.whoisEvent.wait(5):
bot.sendNotice(sender.nick, "Error doing a whois. Please retry later.")
return
if sender.whoisEvent.wait(5) and not sender.authEvent.wait(2):
sender.authenticate(0)
#If we don't accept unregistered usage, we check for AUTH 3 and the WHOIS event
if not bot.allowunregistered:
if not sender.auth == 3:
bot.sendNotice(sender.nick, "You need to register or group your nick before using the bot. Use '/msg NickServ help register' to learn how to register or '/msg NickServ group' to try grouping your current nick to your account.")
return
#if sender.auth == 3:
# if not sender.whoisEvent.wait(5):
# bot.sendNotice(sender.nick, "Error doing a whois. Please retry later.")
# return
#If we allow unregistered usage, we use the current nick as the registered nick for later on
if sender.auth == 0 and bot.allowunregistered and not 'AnonUser_' in sender.nick :
sender.regnick = sender.nick
bot.sendNotice(sender.nick, "You should really register your nick! Use '/msg NickServ help register' to learn how to register.")
#If we allow unregistered usage, we use the current nick as the registered nick for later on
if sender.auth == 0 and bot.dccAllowAnon and 'AnonUser_' in sender.nick :
sender.regnick = sender.nick
#If the AUTH is not 0 (unregistered) or 3 (authenticated), it means we have a weird account, we return
if not sender.auth in [0,3]:
bot.sendNotice(sender.nick, "You are not properly identified. Use '/msg NickServ help' to learn how to identify with services.")
return
userValid = False
if (sender.nick.lower() in bot.banList and cmd['command'] in bot.banList[sender.nick.lower()]) or \
(sender.host.lower() in bot.banList and cmd['command'] in bot.banList[sender.host.lower()]) or \
(sender.regnick.lower() in bot.banList and cmd['command'] in bot.banList[sender.regnick.lower()]):
bot.sendNotice(sender.nick, "You are prevented from using this command.")
return
if cmd['command'] in bot.groups['any']['commands']:
userValid = True
else:
for group, data in bot.groups.items():
if cmd['command'].lower() in data['commands'] and sender.regnick.lower() in bot.authUsers:
if group in bot.authUsers[sender.regnick.lower()]:
userValid = True
# giving admins the right to run ANY command is probably not a good idea...
# if sender.regnick.lower() in bot.authUsers and 'admin' in bot.authUsers[sender.regnick.lower()]:
# userValid = True
if userValid:
callback(bot, sender, dest, cmd, args)
else:
self.logger.info("User %s with regnick %s and auth %s tried to use command %s : No enough privileges"%(sender.nick, sender.regnick, sender.auth, cmd))
bot.sendNotice(sender.regnick, "You don't have the right to run this command")
except Exception as e:
self.logger.error("Error while handling command [ %s ] from user [ %s ] to [ %s ] with args [ %s ]"%(cmd, sender, dest, args))
self.logger.error("%s"%e)
for line in traceback.format_exc().split('\n'):
self.logger.error(line)
#######################################################
# CTCP Commands
def onCTCP(self, sender, dest, msg):
#self.logger.debug("[S : %s] [M : %s]"%(sender.nick, " ".join(params)))
if msg[0] == 'VERSION':
self.bot.sendRaw(CmdGenerator.getNOTICE(sender.nick, "MCPBot v4.0 'You were not expecting me !' by ProfMobius"))
self.handleEvents('Version', sender, msg)
elif msg[0] == 'USERINFO':
self.bot.sendRaw(CmdGenerator.getNOTICE(sender.nick, "MCPBot v4.0 'You were not expecting me !' by ProfMobius"))
self.handleEvents('Userinfo', sender, msg)
elif msg[0] == 'FINGER':
self.bot.sendRaw(CmdGenerator.getNOTICE(sender.nick, "Don't do that !"))
self.handleEvents('Finger', sender, msg)
elif msg[0] == 'DCC':
if self.bot.dccActive:
self.onDCC(sender, dest, msg)
elif msg[0] == 'TIME' and len(msg) > 1:
self.bot.usersInfo[sender.nick].ctcpData['TIME'] = ' '.join(msg[1:])
self.bot.usersInfo[sender.nick].ctcpEvent['TIME'].set()
else:
self.handleEvents('CTCP', sender, msg)
#######################################################
# DCC Commands
def onDCC(self, sender, dest, msg):
self.logger.info("%s %s %s"%(sender, msg, CmdGenerator.conv_ip_long_std(msg[3])))
self.bot.sendRaw(CmdGenerator.getNOTICE(sender.nick, "Don't call me, I will call you."))
host, port = self.bot.dccSocket.getAddr()
self.bot.dccSocket.addPending(sender)
self.bot.sendRaw(CmdGenerator.getDCCCHAT(sender.nick, host, port))
self.handleEvents('DCC', sender, msg)
# UNIMPLEMENTED COMMANDS :
# PASSWORD
# SERVER
# OPER
# SQUIT
# TOPIC
# NAMES
# LIST
# VERSION
# STATS
# LINKS
# TIME
# CONNECT
# TRACE
# ADMIN
# INFO
# WHO
# WHOIS
# WHOWAS
# ERROR