h/core/main.py

215 lines
5.9 KiB
Python
Raw Normal View History

2015-11-11 08:46:22 +00:00
import fuckit
2009-04-19 11:42:48 +00:00
import thread
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-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):
if chan == nick: # PMs don't need prefixes
2014-04-24 21:15:58 +00:00
self.say(msg)
else:
2015-11-05 01:03:22 +00:00
self.say('> ' + msg)
2010-02-02 04:42:34 +00:00
2014-04-24 21:15:58 +00:00
def pm(msg, nick=nick):
2010-02-02 04:42:34 +00:00
conn.msg(nick, msg)
2010-08-30 03:35:27 +00:00
def set_nick(nick):
conn.set_nick(nick)
2010-04-23 03:50:56 +00:00
def me(msg):
2014-04-24 21:15:58 +00:00
self.say("\x01%s %s\x01" % ("ACTION", msg))
def notice(msg):
conn.cmd('NOTICE', [nick, msg])
2010-02-02 04:42:34 +00:00
2014-05-13 05:33:47 +00:00
def kick(target=None, reason=None):
conn.cmd('KICK', [chan, target or nick, reason or ''])
def ban(target=None):
conn.cmd('MODE', [chan, '+b', target or host])
def unban(target=None):
conn.cmd('MODE', [chan, '-b', target or host])
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,
2014-05-13 05:33:47 +00:00
kick=kick, ban=ban, unban=unban, me=me,
set_nick=set_nick, lastparam=paraml[-1])
# make dict keys accessible as attributes
def __getattr__(self, key):
return self[key]
2010-03-05 05:15:29 +00:00
def __setattr__(self, key, value):
self[key] = value
2015-11-29 00:59:32 +00:00
#@fuckit
def run(func, input):
2010-03-11 23:34:54 +00:00
args = func._args
2010-03-11 23:34:54 +00:00
if 'inp' not in input:
input.inp = input.paraml
2010-03-11 23:34:54 +00:00
2010-02-02 04:42:34 +00:00
if args:
if 'db' in args and 'db' not in input:
input.db = get_db_connection(input.conn)
2010-03-11 23:34:54 +00:00
if 'input' in args:
input.input = input
2010-02-02 04:42:34 +00:00
if 0 in args:
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)
out = func(input.inp, **kw)
2010-02-02 04:42:34 +00:00
else:
out = func(input.inp)
if out is not None:
input.reply(unicode(out))
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)
except Exception:
2010-03-11 23:34:54 +00:00
print 'sieve error',
traceback.print_exc()
2010-03-11 23:34:54 +00:00
return None
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):
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:
break
2010-03-11 23:34:54 +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
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)
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)
if input == None:
2010-03-11 23:34:54 +00:00
return
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:
input.reply(func.__doc__)
return
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:
thread.start_new_thread(run, (func, input))
2010-03-11 23:34:54 +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]
elif prefix and command not in prefix:
return prefix
2010-08-30 03:35:27 +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':
# COMMANDS
2014-05-09 03:09:58 +00:00
bot_prefix = re.escape(bot.config.get("prefix", "."))
if inp.chan == inp.nick: # private message, no command prefix required
prefix = r'^(?:(?:'+bot_prefix+')?|'
2010-03-11 23:34:54 +00:00
else:
prefix = r'^(?:'+bot_prefix+'|'
command_re = prefix + inp.conn.nick
command_re += r'[:,]+\s+)(\w+)(?:$|\s+)(.*)'
2010-03-11 23:34:54 +00:00
m = re.match(command_re, inp.lastparam)
if m:
trigger = m.group(1).lower()
command = match_command(trigger)
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?" %
(', '.join(command[:-1]), command[-1]))
elif command in bot.commands:
2010-03-11 23:34:54 +00:00
input = Input(conn, *out)
input.trigger = trigger
2010-03-11 23:34:54 +00:00
input.inp_unstripped = m.group(2)
input.inp = input.inp_unstripped.strip()
2010-03-11 23:34:54 +00:00
func, args = bot.commands[command]
dispatch(input, "command", func, args, autohelp=True)
# 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)