2009-04-19 11:42:48 +00:00
|
|
|
import thread
|
2009-04-23 02:49:06 +00:00
|
|
|
import traceback
|
2009-04-19 11:42:48 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
thread.stack_size(1024 * 512) # reduce vm size
|
2010-03-06 19:41:54 +00:00
|
|
|
|
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
class Input(dict):
|
2014-01-14 21:12:37 +00:00
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
def __init__(self, conn, raw, prefix, command, params,
|
2014-01-14 21:12:37 +00:00
|
|
|
nick, user, host, paraml, msg):
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-08-24 22:14:55 +00:00
|
|
|
chan = paraml[0].lower()
|
|
|
|
if chan == conn.nick.lower(): # is a PM
|
2010-02-02 04:42:34 +00:00
|
|
|
chan = nick
|
|
|
|
|
|
|
|
def say(msg):
|
|
|
|
conn.msg(chan, msg)
|
|
|
|
|
|
|
|
def reply(msg):
|
2010-03-27 08:42:27 +00:00
|
|
|
if chan == nick: # PMs don't need prefixes
|
2010-03-16 19:56:51 +00:00
|
|
|
conn.msg(chan, msg)
|
|
|
|
else:
|
|
|
|
conn.msg(chan, nick + ': ' + msg)
|
2010-02-02 04:42:34 +00:00
|
|
|
|
|
|
|
def pm(msg):
|
|
|
|
conn.msg(nick, msg)
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-08-16 08:36:47 +00:00
|
|
|
def set_nick(nick):
|
|
|
|
conn.set_nick(nick)
|
2010-04-23 03:50:56 +00:00
|
|
|
|
2010-07-14 06:57:43 +00:00
|
|
|
def me(msg):
|
|
|
|
conn.msg(chan, "\x01%s %s\x01" % ("ACTION", msg))
|
|
|
|
|
2010-04-13 21:39:40 +00:00
|
|
|
def notice(msg):
|
|
|
|
conn.cmd('NOTICE', [nick, msg])
|
2010-02-02 04:42:34 +00:00
|
|
|
|
|
|
|
dict.__init__(self, conn=conn, raw=raw, prefix=prefix, command=command,
|
2014-01-14 21:12:37 +00:00
|
|
|
params=params, nick=nick, user=user, host=host,
|
|
|
|
paraml=paraml, msg=msg, server=conn.server, chan=chan,
|
|
|
|
notice=notice, say=say, reply=reply, pm=pm, bot=bot,
|
|
|
|
me=me, set_nick=set_nick, lastparam=paraml[-1])
|
2010-03-05 05:04:57 +00:00
|
|
|
|
2010-03-13 14:02:05 +00:00
|
|
|
# make dict keys accessible as attributes
|
2010-03-05 05:04:57 +00:00
|
|
|
def __getattr__(self, key):
|
|
|
|
return self[key]
|
|
|
|
|
2010-03-05 05:15:29 +00:00
|
|
|
def __setattr__(self, key, value):
|
2010-03-05 05:04:57 +00:00
|
|
|
self[key] = value
|
2009-11-19 03:20:59 +00:00
|
|
|
|
2009-11-19 01:42:37 +00:00
|
|
|
|
|
|
|
def run(func, input):
|
2010-03-11 23:34:54 +00:00
|
|
|
args = func._args
|
2010-03-12 00:28:17 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
if 'inp' not in input:
|
2010-03-12 00:28:17 +00:00
|
|
|
input.inp = input.paraml
|
2010-03-11 23:34:54 +00:00
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
if args:
|
2010-03-13 14:24:19 +00:00
|
|
|
if 'db' in args and 'db' not in input:
|
2010-03-13 14:02:05 +00:00
|
|
|
input.db = get_db_connection(input.conn)
|
2010-03-11 23:34:54 +00:00
|
|
|
if 'input' in args:
|
2010-03-13 14:02:05 +00:00
|
|
|
input.input = input
|
2010-02-02 04:42:34 +00:00
|
|
|
if 0 in args:
|
2010-03-13 14:02:05 +00:00
|
|
|
out = func(input.inp, **input)
|
2010-02-02 04:42:34 +00:00
|
|
|
else:
|
|
|
|
kw = dict((key, input[key]) for key in args if key in input)
|
2010-03-13 14:02:05 +00:00
|
|
|
out = func(input.inp, **kw)
|
2010-02-02 04:42:34 +00:00
|
|
|
else:
|
2010-03-13 14:02:05 +00:00
|
|
|
out = func(input.inp)
|
2009-11-19 01:42:37 +00:00
|
|
|
if out is not None:
|
2010-03-13 14:02:05 +00:00
|
|
|
input.reply(unicode(out))
|
2009-04-19 11:18:27 +00:00
|
|
|
|
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
def do_sieve(sieve, bot, input, func, type, args):
|
|
|
|
try:
|
|
|
|
return sieve(bot, input, func, type, args)
|
2010-03-18 21:11:20 +00:00
|
|
|
except Exception:
|
2010-03-11 23:34:54 +00:00
|
|
|
print 'sieve error',
|
2010-03-18 21:11:20 +00:00
|
|
|
traceback.print_exc()
|
2010-03-11 23:34:54 +00:00
|
|
|
return None
|
|
|
|
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
class Handler(object):
|
2014-01-14 21:12:37 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
'''Runs plugins in their own threads (ensures order)'''
|
2014-01-14 21:12:37 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
def __init__(self, func):
|
|
|
|
self.func = func
|
|
|
|
self.input_queue = Queue.Queue()
|
|
|
|
thread.start_new_thread(self.start, ())
|
|
|
|
|
|
|
|
def start(self):
|
2010-03-13 14:24:19 +00:00
|
|
|
uses_db = 'db' in self.func._args
|
|
|
|
db_conns = {}
|
2010-03-11 23:34:54 +00:00
|
|
|
while True:
|
|
|
|
input = self.input_queue.get()
|
|
|
|
|
|
|
|
if input == StopIteration:
|
2009-04-19 11:18:27 +00:00
|
|
|
break
|
2010-03-11 23:34:54 +00:00
|
|
|
|
2010-03-13 14:24:19 +00:00
|
|
|
if uses_db:
|
|
|
|
db = db_conns.get(input.conn)
|
|
|
|
if db is None:
|
|
|
|
db = bot.get_db_connection(input.conn)
|
|
|
|
db_conns[input.conn] = db
|
|
|
|
input.db = db
|
|
|
|
|
2012-08-24 20:31:38 +00:00
|
|
|
try:
|
|
|
|
run(self.func, input)
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2010-03-11 23:34:54 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.input_queue.put(StopIteration)
|
|
|
|
|
|
|
|
def put(self, value):
|
|
|
|
self.input_queue.put(value)
|
|
|
|
|
|
|
|
|
2010-05-07 23:16:44 +00:00
|
|
|
def dispatch(input, kind, func, args, autohelp=False):
|
2010-03-11 23:34:54 +00:00
|
|
|
for sieve, in bot.plugs['sieve']:
|
|
|
|
input = do_sieve(sieve, bot, input, func, kind, args)
|
2009-04-19 11:18:27 +00:00
|
|
|
if input == None:
|
2010-03-11 23:34:54 +00:00
|
|
|
return
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-06-29 07:02:48 +00:00
|
|
|
if autohelp and args.get('autohelp', True) and not input.inp \
|
2014-01-14 21:12:37 +00:00
|
|
|
and func.__doc__ is not None:
|
2010-05-07 23:16:44 +00:00
|
|
|
input.reply(func.__doc__)
|
|
|
|
return
|
|
|
|
|
2013-01-27 22:40:31 +00:00
|
|
|
if hasattr(func, '_apikey'):
|
|
|
|
key = bot.config.get('api_keys', {}).get(func._apikey, None)
|
|
|
|
if key is None:
|
|
|
|
input.reply('error: missing api key')
|
|
|
|
return
|
|
|
|
input.api_key = key
|
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
if func._thread:
|
|
|
|
bot.threads[func].put(input)
|
|
|
|
else:
|
2009-11-19 01:42:37 +00:00
|
|
|
thread.start_new_thread(run, (func, input))
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
|
2010-05-07 23:16:44 +00:00
|
|
|
def match_command(command):
|
|
|
|
commands = list(bot.commands)
|
|
|
|
|
|
|
|
# do some fuzzy matching
|
|
|
|
prefix = filter(lambda x: x.startswith(command), commands)
|
|
|
|
if len(prefix) == 1:
|
|
|
|
return prefix[0]
|
2010-07-08 18:27:37 +00:00
|
|
|
elif prefix and command not in prefix:
|
2010-07-08 17:22:43 +00:00
|
|
|
return prefix
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-05-07 23:16:44 +00:00
|
|
|
return command
|
|
|
|
|
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
def main(conn, out):
|
|
|
|
inp = Input(conn, *out)
|
|
|
|
|
|
|
|
# EVENTS
|
|
|
|
for func, args in bot.events[inp.command] + bot.events['*']:
|
|
|
|
dispatch(Input(conn, *out), "event", func, args)
|
|
|
|
|
|
|
|
if inp.command == 'PRIVMSG':
|
2010-03-12 00:19:36 +00:00
|
|
|
# COMMANDS
|
2010-03-27 08:42:27 +00:00
|
|
|
if inp.chan == inp.nick: # private message, no command prefix
|
2010-05-07 23:16:44 +00:00
|
|
|
prefix = r'^(?:[.]?|'
|
2010-03-11 23:34:54 +00:00
|
|
|
else:
|
2010-05-07 23:16:44 +00:00
|
|
|
prefix = r'^(?:[.]|'
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-12 00:19:36 +00:00
|
|
|
command_re = prefix + inp.conn.nick
|
2010-11-12 05:18:32 +00:00
|
|
|
command_re += r'[:,]+\s+)(\w+)(?:$|\s+)(.*)'
|
2010-03-11 23:34:54 +00:00
|
|
|
|
|
|
|
m = re.match(command_re, inp.lastparam)
|
|
|
|
|
|
|
|
if m:
|
2010-05-07 23:16:44 +00:00
|
|
|
trigger = m.group(1).lower()
|
|
|
|
command = match_command(trigger)
|
|
|
|
|
2010-07-08 17:22:43 +00:00
|
|
|
if isinstance(command, list): # multiple potential matches
|
|
|
|
input = Input(conn, *out)
|
2010-08-30 03:35:27 +00:00
|
|
|
input.reply("did you mean %s or %s?" %
|
2010-07-08 17:22:43 +00:00
|
|
|
(', '.join(command[:-1]), command[-1]))
|
|
|
|
elif command in bot.commands:
|
2010-03-11 23:34:54 +00:00
|
|
|
input = Input(conn, *out)
|
2010-05-07 23:16:44 +00:00
|
|
|
input.trigger = trigger
|
2010-03-11 23:34:54 +00:00
|
|
|
input.inp_unstripped = m.group(2)
|
2010-05-07 23:16:44 +00:00
|
|
|
input.inp = input.inp_unstripped.strip()
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
func, args = bot.commands[command]
|
2010-05-07 23:16:44 +00:00
|
|
|
dispatch(input, "command", func, args, autohelp=True)
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-12 00:19:36 +00:00
|
|
|
# REGEXES
|
|
|
|
for func, args in bot.plugs['regex']:
|
|
|
|
m = args['re'].search(inp.lastparam)
|
|
|
|
if m:
|
|
|
|
input = Input(conn, *out)
|
|
|
|
input.inp = m
|
|
|
|
|
|
|
|
dispatch(input, "regex", func, args)
|