diff --git a/bot.py b/bot.py index c3f35e7..da73b0a 100644 --- a/bot.py +++ b/bot.py @@ -52,6 +52,7 @@ class Bot: # Keep global context: servers and modules self.servers = dict() self.modules = dict() + self.modules_configuration = dict() # Context paths self.modules_path = mp diff --git a/bot_sample.xml b/bot_sample.xml index 5343a87..37f334f 100644 --- a/bot_sample.xml +++ b/bot_sample.xml @@ -10,19 +10,16 @@ --> - + - - - - - - - - - - - - + + + + + diff --git a/importer.py b/importer.py index 7795547..240af79 100644 --- a/importer.py +++ b/importer.py @@ -42,28 +42,22 @@ class ModuleFinder(Finder): if path is None: for mpath in self.context.modules_path: #print ("looking for", fullname, "in", mpath) - if os.path.isfile(mpath + fullname + ".xml"): - return ModuleLoader(self.context, self.prompt, fullname, - mpath, mpath + fullname + ".xml") - elif (os.path.isfile(mpath + fullname + ".py") or - os.path.isfile(mpath + fullname + "/__init__.py")): + if (os.path.isfile(mpath + fullname + ".py") or + os.path.isfile(mpath + fullname + "/__init__.py")): return ModuleLoader(self.context, self.prompt, - fullname, mpath, None) + fullname, mpath) #print ("not found") return None class ModuleLoader(SourceLoader): - def __init__(self, context, prompt, fullname, path, config_path): + def __init__(self, context, prompt, fullname, path): self.context = context self.prompt = prompt self.name = fullname - self.config_path = config_path - if config_path is not None: - self.config = xmlparser.parse_file(config_path) - if self.config.hasAttribute("name"): - self.name = self.config["name"] + if self.name in self.context.modules_configuration: + self.config = self.context.modules_configuration[self.name] else: self.config = None @@ -251,25 +245,8 @@ def register_hooks(module, context, prompt): """Register all available hooks""" # Register decorated functions for s, h in hooks.last_registered: - context.hooks.add_hook(s, h, module) + if s == "prompt_cmd": + prompt.add_cap_hook(h.name, h.call) + else: + context.hooks.add_hook(s, h, module) hooks.last_registered = [] - - if module.CONF is not None: - # Register command hooks - if module.CONF.hasNode("command"): - for cmd in module.CONF.getNodes("command"): - if cmd.hasAttribute("name") and cmd.hasAttribute("call"): - add_cap_hook(prompt, module, cmd) - - # Register message hooks - if module.CONF.hasNode("message"): - for msg in module.CONF.getNodes("message"): - context.hooks.register_hook(module, msg) - - # Register legacy hooks - if hasattr(module, "parseanswer"): - context.hooks.add_hook("cmd_default", hooks.Hook(module.parseanswer), module) - if hasattr(module, "parseask"): - context.hooks.add_hook("ask_default", hooks.Hook(module.parseask), module) - if hasattr(module, "parselisten"): - context.hooks.add_hook("msg_default", hooks.Hook(module.parselisten), module) diff --git a/modules/cmd_server.py b/modules/cmd_server.py index b898546..b781088 100644 --- a/modules/cmd_server.py +++ b/modules/cmd_server.py @@ -20,6 +20,7 @@ import traceback import sys from networkbot import NetworkBot +from hooks import hook nemubotversion = 3.4 NODATA = True @@ -33,6 +34,7 @@ def getserver(toks, context, prompt): else: return (None, toks) +@hook("prompt_cmd", "close") def close(data, toks, context, prompt): """Disconnect and forget (remove from the servers list) the server""" if len(toks) > 1: @@ -48,6 +50,7 @@ def close(data, toks, context, prompt): prompt.selectedServer = None return +@hook("prompt_cmd", "connect") def connect(data, toks, context, prompt): """Make the connexion to a server""" if len(toks) > 1: @@ -62,6 +65,7 @@ def connect(data, toks, context, prompt): else: print (" Please SELECT a server or give its name in argument.") +@hook("prompt_cmd", "disconnect") def disconnect(data, toks, context, prompt): """Close the connection to a server""" if len(toks) > 1: @@ -78,6 +82,7 @@ def disconnect(data, toks, context, prompt): else: print (" Please SELECT a server or give its name in argument.") +@hook("prompt_cmd", "discover") def discover(data, toks, context, prompt): """Discover a new bot on a server""" (srv, toks) = getserver(toks, context, prompt) @@ -91,6 +96,7 @@ def discover(data, toks, context, prompt): else: print (" Please SELECT a server or give its name in first argument.") +@hook("prompt_cmd", "hotswap") def hotswap(data, toks, context, prompt): """Reload a server class""" if len(toks) > 1: @@ -107,6 +113,7 @@ def hotswap(data, toks, context, prompt): else: print (" Please SELECT a server or give its name in argument.") +@hook("prompt_cmd", "join") def join(data, toks, context, prompt): """Join or leave a channel""" rd = 1 @@ -136,6 +143,7 @@ def join(data, toks, context, prompt): srv.leave(toks[rd]) return +@hook("prompt_cmd", "save") def save_mod(data, toks, context, prompt): """Force save module data""" if len(toks) < 2: @@ -150,6 +158,7 @@ def save_mod(data, toks, context, prompt): print ("save: no module named `%s´" % mod) return +@hook("prompt_cmd", "send") def send(data, toks, context, prompt): """Send a message on a channel""" rd = 1 @@ -190,6 +199,7 @@ def send(data, toks, context, prompt): srv.send_msg_final(chan, toks[rd]) return "done" +@hook("prompt_cmd", "zap") def zap(data, toks, context, prompt): """Hard change connexion state""" if len(toks) > 1: @@ -203,6 +213,7 @@ def zap(data, toks, context, prompt): else: print (" Please SELECT a server or give its name in argument.") +@hook("prompt_cmd", "top") def top(data, toks, context, prompt): """Display consumers load information""" print("Queue size: %d, %d thread(s) running (counter: %d)" % (context.cnsr_queue.qsize(), len(context.cnsr_thrd), context.cnsr_thrd_size)) @@ -216,6 +227,7 @@ def top(data, toks, context, prompt): print("################ Stack trace for thread %u ################" % th.ident) traceback.print_stack(sys._current_frames()[th.ident]) +@hook("prompt_cmd", "netstat") def netstat(data, toks, context, prompt): """Display sockets in use and many other things""" if len(context.network) > 0: diff --git a/modules/cmd_server.xml b/modules/cmd_server.xml deleted file mode 100644 index f87789a..0000000 --- a/modules/cmd_server.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/prompt/builtins.py b/prompt/builtins.py index 4dd294f..dd5308a 100644 --- a/prompt/builtins.py +++ b/prompt/builtins.py @@ -77,13 +77,15 @@ def load_file(filename, context): print("Server `%s:%s' already added, skiped." % (server["server"], server["port"])) - # Load files asked by the configuration file - for load in config.getNodes("load"): - load_file(load["path"], context) + # Load module and their configuration + for mod in config.getNodes("module"): + context.modules_configuration[mod["name"]] = mod + if not mod.hasAttribute("autoload") or (mod["autoload"].lower() != "false" and mod["autoload"].lower() != "off"): + __import__(mod["name"]) - # This is a nemubot module configuration file, load the module - elif config.getName() == "nemubotmodule": - __import__(config["name"]) + # Load files asked by the configuration file + for load in config.getNodes("include"): + load_file(load["path"], context) # Other formats else: