From f1a7d54208a08963914a4443cef4985b81853e44 Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 12 Oct 2014 21:33:10 -0700 Subject: [PATCH] Added quit/shutdown call to RPC/core --- pyircbot/core/pyircbot.py | 22 ++++++++++++++++++---- pyircbot/core/rpc.py | 10 ++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/pyircbot/core/pyircbot.py b/pyircbot/core/pyircbot.py index f36464c..2f4f1ce 100644 --- a/pyircbot/core/pyircbot.py +++ b/pyircbot/core/pyircbot.py @@ -52,6 +52,9 @@ class PyIRCBot(asynchat.async_chat): self.buffer = StringIO() """cSTringIO used as a buffer""" + self.alive = True + """ True if we should try to stay connected""" + # IRC Messages are terminated with \r\n self.set_terminator(b"\r\n") @@ -81,6 +84,10 @@ class PyIRCBot(asynchat.async_chat): #Close all modules self.closeAllModules() + # Mark for shutdown + self.alive = False + # Exit + sys.exit(0) " Net related code here on down " @@ -118,10 +125,10 @@ class PyIRCBot(asynchat.async_chat): self.log.debug("handle_close") self.connected=False self.close() - - self.log.warning("Connection was lost. Reconnecting in 5 seconds.") - time.sleep(5) - self._connect() + if self.alive: + self.log.warning("Connection was lost. Reconnecting in 5 seconds.") + time.sleep(5) + self._connect() def handle_error(self, *args, **kwargs): """Called on fatal network errors.""" @@ -652,3 +659,10 @@ class PyIRCBot(asynchat.async_chat): :type comment: str""" self.sendRaw("KICK %s %s :%s" % (channel, who, comment)) + def act_QUIT(self, message): + """Use the `/quit` command + + :param message: quit message + :type message: str""" + self.sendRaw("QUIT :%s" % message) + diff --git a/pyircbot/core/rpc.py b/pyircbot/core/rpc.py index 99f6402..2fec77d 100644 --- a/pyircbot/core/rpc.py +++ b/pyircbot/core/rpc.py @@ -28,6 +28,7 @@ class BotRPC(Thread): self.server.register_function( self.pluginCommand ) self.server.register_function( self.setPluginVar ) self.server.register_function( self.getPluginVar ) + self.server.register_function( self.quit ) self.start() @@ -143,4 +144,13 @@ class BotRPC(Thread): self.log.info("RPC: setting %s.%s = %s )" % (moduleName, moduleVarName, value)) setattr(plugin, moduleVarName, value) return (True, "Var set") + + def quit(self, message): + """Tell the bot to quit IRC and exir + :param message: Quit message + :type moduleName: str""" + self.bot.act_QUIT(message) + self.bot.kill() + return (True, "Shutdown ordered") + \ No newline at end of file