h/core/main.py

154 lines
4.1 KiB
Python
Raw Normal View History

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):
def __init__(self, conn, raw, prefix, command, params,
nick, user, host, paraml, msg):
2010-03-01 02:32:41 +00:00
2010-02-02 04:42:34 +00:00
chan = paraml[0].lower()
2010-03-01 02:32:41 +00:00
if chan == conn.nick: # 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
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)
dict.__init__(self, conn=conn, raw=raw, prefix=prefix, command=command,
params=params, nick=nick, user=user, host=host,
2010-03-01 02:32:41 +00:00
paraml=paraml, msg=msg, server=conn.server, chan=chan,
2010-03-11 23:34:54 +00:00
say=say, reply=reply, pm=pm, bot=bot, 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
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, e:
print 'sieve error',
traceback.print_exc(Exception)
return None
class Handler(object):
'''Runs plugins in their own threads (ensures order)'''
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
2010-03-11 23:34:54 +00:00
run(self.func, input)
def stop(self):
self.input_queue.put(StopIteration)
def put(self, value):
self.input_queue.put(value)
def dispatch(input, kind, func, args):
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
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 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)
2010-03-11 23:34:54 +00:00
if inp.command == 'PRIVMSG':
# COMMANDS
2010-03-11 23:34:54 +00:00
if inp.chan == inp.nick: # private message, no command prefix
prefix = r'^(?:[.!]?|'
else:
prefix = r'^(?:[.!]|'
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:
command = m.group(1).lower()
if command in bot.commands:
input = Input(conn, *out)
input.inp_unstripped = m.group(2)
input.inp = m.group(2).strip()
func, args = bot.commands[command]
dispatch(input, "command", func, args)
# 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)