3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +01:00

coreplugin: modularize rehash, and add a SIGHUP handler for it

Closes #179.
This commit is contained in:
James Lu 2016-04-09 10:08:12 -07:00
parent 8f32727a3f
commit 264c803d8e

View File

@ -5,6 +5,7 @@ coreplugin.py - Implements core PyLink functions as a plugin.
import gc
import sys
import signal
import os
import utils
import conf
@ -334,29 +335,18 @@ def reload(irc, source, args):
if unload(irc, source, args):
load(irc, source, args)
@utils.add_cmd
def rehash(irc, source, args):
"""takes no arguments.
Reloads the configuration file for PyLink, (dis)connecting added/removed networks.
Plugins must be manually reloaded."""
utils.checkAuthenticated(irc, source, allowOper=False)
def _rehash():
"""Rehashes the PyLink daemon."""
old_conf = conf.conf.copy()
fname = conf.fname
try:
new_conf = conf.loadConf(fname, errors_fatal=False)
except Exception as e: # Something went wrong, abort.
log.exception("Error REHASH'ing config: ")
irc.reply("Error loading configuration file: %s: %s" % (type(e).__name__, e))
return
else:
new_conf = conf.validateConf(new_conf)
conf.conf = new_conf
for network, ircobj in world.networkobjects.copy().items():
# Server was removed from the config file, disconnect them.
log.debug('(%s) rehash: checking if %r is in new conf still.', irc.name, network)
log.debug('rehash: checking if %r is in new conf still.', network)
if network not in new_conf['servers']:
log.debug('(%s) rehash: removing connection to %r (removed from config).', irc.name, network)
log.debug('rehash: removing connection to %r (removed from config).', network)
# Disable autoconnect first.
ircobj.serverdata['autoconnect'] = -1
ircobj.disconnect()
@ -380,6 +370,30 @@ def rehash(irc, source, args):
if network not in world.networkobjects:
proto = utils.getProtocolModule(sdata['protocol'])
world.networkobjects[network] = classes.Irc(network, proto, new_conf)
if os.name == 'posix':
# Only register SIGHUP on *nix.
def sighup_handler(_signo, _stack_frame):
"""Handles SIGHUP by rehashing the PyLink daemon."""
log.info("SIGHUP received, reloading config.")
_rehash()
signal.signal(signal.SIGHUP, sighup_handler)
@utils.add_cmd
def rehash(irc, source, args):
"""takes no arguments.
Reloads the configuration file for PyLink, (dis)connecting added/removed networks.
Plugins must be manually reloaded."""
utils.checkAuthenticated(irc, source, allowOper=False)
try:
_rehash()
except Exception as e: # Something went wrong, abort.
log.exception("Error REHASHing config: ")
irc.reply("Error loading configuration file: %s: %s" % (type(e).__name__, e))
return
else:
irc.reply("Done.")
def main(irc=None):