-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbotcore.py
550 lines (452 loc) · 18.2 KB
/
botcore.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
# -*- coding: utf-8 -*-
"""Bot core"""
# twisted imports
from twisted.words.protocols import irc
from twisted.internet import reactor, threads
from twisted.python import rebuild
from types import FunctionType
import logging
import fnmatch
from util import pyfiurl
# line splitting
import textwrap
__pychecker__ = 'unusednames=i, classattr'
log = logging.getLogger("bot")
class CoreCommands(object):
def privcommand_ping(self, user, channel, args):
self.say(channel, "%s: My current ping is %.0fms" % (
self.factory.getNick(user), self.pingAve * 100.0)
)
def privcommand_rehash(self, user, channel, args):
"""Reload modules. Usage: rehash [debug]"""
lvl = self.checkValidHostmask(user)
if lvl and lvl > 4:
try:
# rebuild core & update
log.info("rebuilding %r" % self)
rebuild.updateInstance(self)
# unload removed modules
self.factory._unload_removed_modules()
# reload modules
self.factory._loadmodules()
except Exception, e:
self.say(channel, "Rehash error: %s" % e)
log.error("Rehash error: " + e)
else:
self.say(channel, "Rehash OK")
log.info("Rehash OK")
else:
log.debug("%s tried priveleged !rehash command" %
self.factory.getNick(user)
)
def privcommand_join(self, user, channel, args):
"""
Usage: join <channel>[@network] [password] - Join the specified channel
"""
lvl = self.checkValidHostmask(user)
if not lvl or lvl < 4:
return
password = None
# see if we have multiple arguments
try:
args, password = args.split(' ', 1)
except ValueError, e:
pass
# see if the user specified a network
try:
newchannel, network = args.split('@', 1)
except ValueError, e:
log.info("%s" % e)
newchannel, network = args, self.network.alias
try:
bot = self.factory.allBots[network]
except KeyError:
self.say(channel, "I am not on that network.")
else:
log.debug("Attempting to join channel %s", newchannel)
if newchannel in bot.network.channels:
self.say(channel, "I am already in %s on %s." % (
newchannel, network
))
log.debug("Already on channel %s" % channel)
log.debug("Chans I'm on this netw: %s" % bot.network.channels)
else:
if password:
bot.join(newchannel, key=password)
log.debug("Joined")
else:
bot.join(newchannel)
log.debug("Joined %s" % newchannel)
# alias of part
def privcommand_leave(self, user, channel, args):
"""Usage: leave <channel>[@network] - Leave the specified channel"""
self.privcommand_part(user, channel, args)
def privcommand_part(self, user, channel, args):
"""Usage: part <channel>[@network] - Leave the specified channel"""
lvl = self.checkValidHostmask(user)
if not lvl or lvl < 4:
return
# part what and where?
try:
newchannel, network = args.split('@', 1)
except ValueError, e:
log.info("%s" % e)
newchannel, network = args, self.network.alias
# get the bot instance for this chat network
try:
bot = self.factory.allBots[network]
except KeyError:
self.say(channel, "I am not on that network.")
else:
if newchannel not in bot.network.channels:
self.say(channel, "I am not in %s on %s." % (
newchannel, network
))
self.say(channel, "I am on %s" % bot.network.channels)
else:
bot.network.channels.remove(newchannel)
bot.part(newchannel)
def privcommand_quit(self, user, channel, args):
"""Usage: logoff - Leave this network"""
lvl = self.checkValidHostmask(user)
if not lvl or lvl < 4:
return
self.quit("cya")
self.hasQuit = 1
def privcommand_channels(self, user, channel, args):
"""Usage: channels <network> - List channels the bot is on"""
if not args:
self.say(channel, "Please specify a network")
return
# bot = self.factory.allBots[args]
self.say(channel, "I am on %s" % self.network.channels)
def command_help(self, user, channel, cmnd):
"""
Get help on all commands or a specific one. Usage: help [<command>]
"""
commands = []
for module, env in self.factory.ns.items():
myglobals, mylocals = env
commands += [(c.replace("command_", ""), ref) for c, ref
in mylocals.items() if c.startswith("command_%s" % cmnd)]
# help for a specific command
if len(cmnd) > 0:
for cname, ref in commands:
if cname == cmnd:
helptext = ref.__doc__.split("\n", 1)[0]
self.say(channel, "Help for %s: %s" % (cmnd, helptext))
return
# generic help
else:
commandlist = ", ".join([c for c, ref in commands])
self.say(
channel,
"Available commands: %s" % commandlist.encode('utf-8')
)
def privcommand_help(self, user, channel, cmnd):
"""
Get help on all commands or a specific one. Usage: help [<command>]
"""
commands = []
for module, env in self.factory.ns.items():
myglobals, mylocals = env
commands += [(c.replace("privcommand_", ""), ref) for c, ref
in mylocals.items() if c.startswith("privcommand_%s" % cmnd)]
# help for a specific command
if len(cmnd) > 0:
for cname, ref in commands:
if cname == cmnd:
helptext = ref.__doc__.split("\n", 1)[0]
self.say(channel, "Help for %s: %s" % (cmnd, helptext))
return
# generic help
else:
commandlist = ", ".join([c for c, ref in commands])
self.say(channel, "Available commands: %s" % commandlist)
class pungaBot(irc.IRCClient, CoreCommands):
"""pungaBot"""
nickname = "pichu"
realname = "pungabot"
# send 1 msg per 1/2 sec
lineRate = 0.5
hasQuit = False
# Rolling ping time average
pingAve = 0.0
def __init__(self, network):
self.network = network
self.nickname = self.network.nickname
self.authenticated = {}
# text wrapper to clip overly long answers
self.tw = textwrap.TextWrapper(width=400, break_long_words=True)
log.info("bot initialized")
def __repr__(self):
return 'pungaBot(%r, %r)' % (self.nickname, self.network.address)
###### CORE
def printResult(self, msg, info):
# Don't print results if there is nothing to say
if msg:
log.debug("Result %s %s" % (msg, info))
pass
def printError(self, msg, info):
log.error("ERROR %s %s" % (msg, info))
pass
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.repeatingPing(300)
log.info("connection made")
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
log.info("connection lost: %s" % reason)
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self.reloadUsers()
for chan in self.network.channels:
# defined as a tuple, channel has a key
if type(chan) == list:
self.join(chan[0], key=chan[1])
else:
self.join(chan)
log.info("joined %d channel(s): %s" % (
len(self.network.channels),
", ".join(self.network.channels)
))
def pong(self, user, secs):
self.pingAve = ((self.pingAve * 5) + secs) / 6.0
def repeatingPing(self, delay):
reactor.callLater(delay, self.repeatingPing, delay)
self.ping(self.nickname)
def say(self, channel, message, length="510"):
"""Override default say to make replying to private messages easier"""
# wrap long text into suitable fragments
msg = self.tw.wrap(message)
cont = False
for m in msg:
if cont:
m = "..." + m
self.msg(channel, m, int(length))
cont = True
return ('botcore.say', channel, message)
def log(self, message):
botId = "%s@%s" % (self.nickname, self.network.alias)
log.info("%s: %s", botId, message)
def checkValidHostmask(self, user):
"""
Based in the user hostmask, check if he/it do auth.
Return user level
"""
for pattern, level in self.authenticated.items():
if fnmatch.fnmatch(self.factory.getHostmask(user), pattern):
log.debug("pattern matched = %s" % pattern)
return level
return False
def reloadUsers(self):
""" (re)load users from database. used for authentication """
self.authenticated = {}
# first we should get all the hostmask for the admins/bots
c = self.factory.dbCursor.execute(
"SELECT hostmask,level,name FROM users ORDER BY level"
)
for mask in c:
if mask[0] is not None:
log.debug("loaded user: %s(%s)" % (mask[2], mask[0]))
self.authenticated[mask[0]] = mask[1]
###### COMMUNICATION
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message.
@param user: nick!user@host
@param channel: Channel where the message originated from
@param msg: The actual message
"""
channel = channel.lower()
lnick = self.nickname.lower()
if channel == lnick:
# bot private commands (privcommand)
if msg.startswith(self.factory.config['commandchar']):
cmnd = msg[len(self.factory.config['commandchar']):]
self._privcommand(user, self.factory.getNick(user), cmnd)
else:
# channel public commands (command)
if msg.startswith(self.factory.config['commandchar']):
cmnd = msg[len(self.factory.config['commandchar']):]
self._command(user, channel, cmnd)
# run URL handlers
urls = pyfiurl.grab(msg)
if urls:
for url in urls:
self._runhandler("url", user, channel, url, msg)
# run privmsg handlers
self._runhandler("privmsg", user, channel, msg)
def _runhandler(self, handler, *args, **kwargs):
"""Run a handler for an event"""
handler = "handle_%s" % handler
# module commands
for module, env in self.factory.ns.items():
myglobals, mylocals = env
# find all matching command functions
handlers = [(h, ref) for h, ref in mylocals.items()
if h == handler and type(ref) == FunctionType]
for hname, func in handlers:
# defer each handler to a separate thread, assign callbacks
# to see when they end
# TODO: Profiling: add time.time() to callback params,
# to calculate difference
d = threads.deferToThread(func, self, *args, **kwargs)
d.addCallback(self.printResult, "handler %s completed" % hname)
d.addErrback(self.printError, "handler %s error" % hname)
def _command(self, user, channel, cmnd):
"""Handles bot commands.
This function calls the appropriate method for the given command.
The command methods are formatted as "command_<commandname>"
"""
# split arguments from the command part
try:
cmnd, args = cmnd.split(" ", 1)
except ValueError:
args = ""
# core commands
method = getattr(self, "command_%s" % cmnd, None)
if method is not None:
log.info("internal command %s called by %s on %s" % (
cmnd,
user,
channel
))
method(user, channel, args)
return
# module commands
for module, env in self.factory.ns.items():
myglobals, mylocals = env
# find all matching command functions
commands = [(c, ref) for c, ref in mylocals.items()
if c == "command_%s" % cmnd]
for cname, command in commands:
log.info("module %s called by %s on %s" % (
cname, user, channel
))
# Defer commands to threads
d = threads.deferToThread(command, self, user, channel, args)
d.addCallback(self.printResult, "command %s completed" % cname)
d.addErrback(self.printError, "command %s error" % cname)
def _privcommand(self, user, channel, cmnd):
"""Handles private bot commands.
This function calls the appropriate method for the given command.
The command methods are formatted as "privcommand_<commandname>"
"""
# split arguments from the command part
try:
cmnd, args = cmnd.split(" ", 1)
except ValueError:
args = ""
# core commands
method = getattr(self, "privcommand_%s" % cmnd, None)
if method is not None:
log.info("internal privcommand %s called by %s on %s" % (
cmnd, user, channel
))
method(user, channel, args)
return
for module, env in self.factory.ns.items():
myglobals, mylocals = env
# find all matching command functions
commands = [(c, ref) for c, ref in mylocals.items()
if c == "privcommand_%s" % cmnd]
for cname, command in commands:
log.info("module %s called by %s on %s" % (
cname, user, channel)
)
# Defer commands to threads
d = threads.deferToThread(command, self, user, channel, args)
d.addCallback(
self.printResult,
"privcommand %s completed" % cname
)
d.addErrback(self.printError, "privcommand %s error" % cname)
### LOW-LEVEL IRC HANDLERS ###
def irc_JOIN(self, prefix, params):
"""override the twisted version to preserve full userhost info"""
nick = self.factory.getNick(prefix)
channel = params[-1]
if nick == self.nickname:
self.joined(channel)
else:
self.userJoined(prefix, channel)
if nick.lower() != self.nickname.lower():
pass
elif channel not in self.network.channels:
self.network.channels.append(channel)
self.factory.setNetwork(self.network)
def irc_PART(self, prefix, params):
"""override the twisted version to preserve full userhost info"""
nick = self.factory.getNick(prefix)
channel = params[0]
if nick == self.nickname:
self.left(channel)
else:
# some clients don't send a part message at all, compensate
if len(params) == 1:
params.append("")
self.userLeft(prefix, channel, params[1])
def irc_QUIT(self, prefix, params):
"""QUIT-handler.
Twisted IRCClient doesn't handle this at all.."""
nick = self.factory.getNick(prefix)
if nick == self.nickname:
self.left(params[0])
else:
self.userLeft(prefix, None, params[0])
###### HANDLERS ######
## ME
def joined(self, channel):
"""I joined a channel"""
self._runhandler("joined", channel)
def left(self, channel):
"""I left a channel"""
self._runhandler("left", channel)
def noticed(self, user, channel, message):
"""I received a notice"""
self._runhandler("noticed", user, channel, message)
def modeChanged(self, user, channel, set, modes, args):
"""Mode changed on user or channel"""
self._runhandler("modeChanged", user, channel, set, modes, args)
def kickedFrom(self, channel, kicker, message):
"""I was kicked from a channel"""
self._runhandler("kickedFrom", channel, kicker, message)
def nickChanged(self, nick):
"""I changed my nick"""
self._runhandler("nickChanged", nick)
## OTHER PEOPLE
def userJoined(self, user, channel):
"""Someone joined"""
self._runhandler("userJoined", user, channel)
def userLeft(self, user, channel, message):
"""Someone left"""
self._runhandler("userLeft", user, channel, message)
def userKicked(self, kickee, channel, kicker, message):
"""Someone got kicked by someone"""
self._runhandler("userKicked", kickee, channel, kicker, message)
def action(self, user, channel, data):
"""An action"""
self._runhandler("action", user, channel, data)
def topicUpdated(self, user, channel, topic):
"""Save topic to maindb when it changes"""
self._runhandler("topicUpdated", user, channel, topic)
def userRenamed(self, oldnick, newnick):
"""Someone changed their nick"""
self._runhandler("userRenamed", oldnick, newnick)
def receivedMOTD(self, motd):
"""MOTD"""
self._runhandler("receivedMOTD", motd)
## SERVER INFORMATION
def created(self, when):
log.info(self.network.alias + " CREATED: " + when)
def yourHost(self, info):
log.info(self.network.alias + " YOURHOST: " + info)
def myInfo(self, servername, version, umodes, cmodes):
log.info(self.network.alias + " MYINFO: %s %s %s %s" % (
servername,
version,
umodes,
cmodes
))
def luserMe(self, info):
log.info(self.network.alias + " LUSERME: " + info)