From 847a98755fec7795a2f5982375fb29dd80b98213 Mon Sep 17 00:00:00 2001 From: James Lu Date: Fri, 1 Jul 2016 20:08:38 -0700 Subject: [PATCH] core: use a shared function for disconnecting + removing networks --- coremods/control.py | 18 ++++++++++-------- plugins/networks.py | 10 ++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/coremods/control.py b/coremods/control.py index 3a23d1d..6090a99 100644 --- a/coremods/control.py +++ b/coremods/control.py @@ -7,6 +7,13 @@ import os from pylinkirc import world, utils, conf, classes from pylinkirc.log import log +def remove_network(ircobj): + """Removes a network object from the pool.""" + # Disable autoconnect first by setting the delay negative. + ircobj.serverdata['autoconnect'] = -1 + ircobj.disconnect() + del world.networkobjects[ircobj.name] + def _shutdown(irc=None): """Shuts down the Pylink daemon.""" for name, plugin in world.plugins.items(): @@ -19,10 +26,8 @@ def _shutdown(irc=None): log.exception('coreplugin: Error occurred in die() of plugin %s, skipping...', name) for ircobj in world.networkobjects.values(): - # Disconnect all our networks. Disable auto-connect first by setting - # the time to negative. - ircobj.serverdata['autoconnect'] = -1 - ircobj.disconnect() + # Disconnect all our networks. + remove_network(ircobj) def sigterm_handler(_signo, _stack_frame): """Handles SIGTERM gracefully by shutting down the PyLink daemon.""" @@ -43,10 +48,7 @@ def _rehash(): log.debug('rehash: checking if %r is in new conf still.', network) if network not in new_conf['servers']: log.debug('rehash: removing connection to %r (removed from config).', network) - # Disable autoconnect first. - ircobj.serverdata['autoconnect'] = -1 - ircobj.disconnect() - del world.networkobjects[network] + remove_network(ircobj) else: ircobj.conf = new_conf ircobj.serverdata = new_conf['servers'][network] diff --git a/plugins/networks.py b/plugins/networks.py index 4411594..b0b1f0b 100644 --- a/plugins/networks.py +++ b/plugins/networks.py @@ -3,6 +3,7 @@ import threading from pylinkirc import utils, world, conf, classes from pylinkirc.log import log +from pylinkirc.coremods import control @utils.add_cmd def disconnect(irc, source, args): @@ -22,14 +23,7 @@ def disconnect(irc, source, args): return irc.reply("Done. If you want to reconnect this network, use the 'rehash' command.") - # Cancel autoconnect. - network.serverdata["autoconnect"] = -1 - - # Abort the connection. - network.disconnect() - - # Remove the dead network object. - del world.networkobjects[netname] + control.remove_network(network) @utils.add_cmd def autoconnect(irc, source, args):