diff -r 9e483447654b -r 6c0609395889 modulecore.py --- a/modulecore.py Mon Jan 12 02:44:56 2015 -0500 +++ b/modulecore.py Mon Jan 12 10:55:45 2015 +0200 @@ -1,5 +1,6 @@ import os import re +import time from configfile import Config Modules = {} @@ -79,6 +80,15 @@ # g_responsePages = [[]] g_responsePageNum = 0 +g_lastConfirm = 0 +g_confirmCommand = None + +class Confirmxeption (Exception): + def __init__ (self, id, value): + self.id = id + self.message = value + def __str__ (self): + return self.message def response_function (message): global g_responsePages @@ -88,9 +98,14 @@ else: g_responsePages[-1].append (message) -def print_responses (bot, replyto): +def confirm_function (id, message): + raise Confirmxeption (id, message) + +def print_responses (commandObject): global g_responsePages global g_responsePageNum + bot = commandObject['bot'] + replyto = commandObject['replyto'] # Check bounds if g_responsePageNum >= len (g_responsePages): @@ -111,6 +126,38 @@ % (num, 's' if num != 1 else '')) # +# check_same_caller (comm1, comm2) +# +# Are the two commands called by the same person? +# +def check_same_caller (comm1, comm2): + return comm1['bot'].name == comm2['bot'].name \ + and comm1['sender'] == comm2['sender'] \ + and comm1['ident'] == comm2['ident'] \ + and comm1['host'] == comm2['host'] + +def exec_command (commandObject): + global g_lastConfirm + global g_confirmCommand + cmdname = commandObject['cmdname'] + + try: + func = getattr (commandObject['module'], 'cmd_' + cmdname) + except AttributeError: + command_error ('command "%s" is not defined!' % cmdname) + + try: + func (**commandObject) + except Confirmxeption as e: + if time.time() - g_lastConfirm < 15 and g_confirmCommand != None: + command_error ('another confirm is underway') + + g_lastConfirm = time.time() + response_function (str (e) + ' (.yes/.no)') + commandObject['confirmed'] = e.id + g_confirmCommand = commandObject + +# # call_command (bot, message, cmdname, **kvargs) # # Calls a cobalt command @@ -122,13 +169,13 @@ try: cmd = Commands[cmdname] except KeyError: - return False - + return + if not is_available (cmd, kvargs['ident'], kvargs['host']): command_error ("you may not use %s" % cmdname) - + match = re.compile (cmd['regex']).match (message) - + if match == None: # didn't match print "regex: %s" % cmd['regex'] @@ -148,13 +195,38 @@ i += 1 print "ModuleCore: %s called by %s" % (cmdname, kvargs['sender']) - getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, reply=response_function, **kvargs) + commandObject = kvargs + commandObject['bot'] = bot + commandObject['cmdname'] = cmdname + commandObject['args'] = args + commandObject['reply'] = response_function + commandObject['confirm'] = confirm_function + commandObject['confirmed'] = 0 + commandObject['commandObject'] = commandObject + commandObject['info'] = cmd + commandObject['module'] = cmd['module'] + exec_command (commandObject) # Print the first page of responses. if cmdname != 'more': - print_responses (bot, kvargs['replyto']) + print_responses (commandObject) + +def confirm (cmd, yes): + global g_confirmCommand + + if g_confirmCommand == None: + cmd['reply'] ('%s to what?' % cmd['cmdname']) + return - return True + if not check_same_caller (cmd, g_confirmCommand): + return + + if yes: + exec_command (g_confirmCommand) + else: + cmd['reply'] ('okay then') + + g_confirmCommand = None # # get_available_commands