def handle_command(self, cmd, args, context): """Execute a command.""" proxy = SendMessageProxy(context) try: handler = self.handlers[cmd] except KeyError: proxy.send_message("No such command") return try: deferred = handler(proxy, cmd, args, context) except AuthenticationError: deferred = make_deferred(get_auth_failure_msg()) except WaitingForAuthenticationNotice as e: deferred = e.args[0] @simple_callback def callback(data): proxy.send_message(data) if deferred: deferred.addCallback(callback) return deferred
def handle_restart(self, cmd, args, context): """!restart\nRestart me, bro!""" Globals.restart = True reactor.callLater(1, reactor.stop) return make_deferred("yessir")
def handle_help(self, cmd, args, context): """!help [command name]\nShows you help n' stuff.""" if len(args) > 0: name = args[0] try: handler = self.handlers[name] except KeyError: return make_deferred("No such command.") if handler == self.run_handler: path = self.scriptPaths[name] + ".help" if os.path.exists(path): with open(path, "r") as f: return make_deferred(f.read()) else: return make_deferred("I don't know what {0} does.\ntry !{0} help".format(name)) else: if hasattr(handler, "__doc__") and type(handler.__doc__) is str: return make_deferred(handler.__doc__) else: return make_deferred("I don't know what {0} does.".format(name)) else: return make_deferred("I know about the following commands: " + ", ".join(self.handlers.keys()))
def run_handler(self, cmd, args, context): """Runs a handler script.""" path = self.scriptPaths[cmd] cmdline = [path] + args if sys.platform == "win32": if path.endswith(".py"): #we can at least run python scripts cmdline = [sys.executable] + cmdline else: return make_deferred("I can't run %s (I'm on Windows)" % (cmd,)) context.update(os.environ) return SubprocessProtocol(cmdline, context).deferred
def handle_quit(self, cmd, args, context): """!quit\nMake me go away!""" reactor.callLater(1, reactor.stop) return make_deferred("later, bitches")