diff -r 6c0609395889 -r dbf49689af0d modulecore.py --- a/modulecore.py Mon Jan 12 10:55:45 2015 +0200 +++ b/modulecore.py Thu Jan 15 19:06:14 2015 +0200 @@ -5,6 +5,7 @@ Modules = {} Commands = {} +Hooks = {} class CommandError (Exception): def __init__ (self, value): @@ -21,6 +22,7 @@ global Commands global Modules files = os.listdir ('.') + numHooks = 0 for fn in files: if fn[0:4] != 'mod_' or fn[-3:] != '.py': @@ -51,9 +53,19 @@ cmd['argnames'].append (argname) + if 'hooks' in module.ModuleData: + for key, hooks in module.ModuleData['hooks'].iteritems(): + for hook in hooks: + if key not in Hooks: + Hooks[key] = [] + + Hooks[key].append ({'name': hook, 'func': getattr (module, hook)}) + numHooks += 1 + print "Loaded module %s" % fn - print 'Loaded %d commands in %d modules' % (len (Commands), len (Modules)) + print ('Loaded %d commands and %d hooks in %d modules' % + (len (Commands), numHooks, len (Modules))) # # command_error (message) @@ -178,7 +190,6 @@ if match == None: # didn't match - print "regex: %s" % cmd['regex'] command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args'])) # .more is special as it is an interface to the page system. @@ -205,12 +216,30 @@ commandObject['commandObject'] = commandObject commandObject['info'] = cmd commandObject['module'] = cmd['module'] + commandObject['error'] = command_error exec_command (commandObject) # Print the first page of responses. if cmdname != 'more': print_responses (commandObject) +def call_hook (bot, hookname, **kvargs): + global g_responsePages + global g_responsePageNum + hookObject = kvargs + hookObject['bot'] = bot + g_responsePages = [[]] + g_responsePageNum = 0 + + if 'replyto' in hookObject: + hookObject['reply'] = response_function + + if hookname in Hooks: + for hook in Hooks[hookname]: + hook['func'] (**hookObject) + + print_responses (hookObject) + def confirm (cmd, yes): global g_confirmCommand