2019-05-29 15:11:47 +00:00
|
|
|
import signal, sys
|
2018-10-12 17:07:23 +00:00
|
|
|
from src import Config, ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-10-12 17:07:23 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
def on_load(self):
|
2018-12-03 19:46:15 +00:00
|
|
|
self._exited = False
|
2016-03-29 11:56:58 +00:00
|
|
|
signal.signal(signal.SIGINT, self.SIGINT)
|
|
|
|
signal.signal(signal.SIGUSR1, self.SIGUSR1)
|
2019-03-13 18:27:34 +00:00
|
|
|
signal.signal(signal.SIGHUP, self.SIGHUP)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def SIGINT(self, signum, frame):
|
|
|
|
print()
|
2019-10-11 14:41:51 +00:00
|
|
|
self.bot.trigger(lambda: self._kill(signum))
|
|
|
|
|
|
|
|
def _kill(self, signum):
|
|
|
|
if self._exited:
|
|
|
|
return
|
|
|
|
self._exited = True
|
|
|
|
|
|
|
|
self.events.on("signal.interrupt").call(signum=signum)
|
|
|
|
|
|
|
|
written = False
|
|
|
|
for server in list(self.bot.servers.values()):
|
|
|
|
if server.connected:
|
|
|
|
server.socket.clear_send_buffer()
|
|
|
|
|
|
|
|
line = utils.irc.protocol.quit("Shutting down")
|
|
|
|
sent_line = server.send(line, immediate=True)
|
|
|
|
sent_line.events.on("send").hook(self._make_hook(server))
|
|
|
|
|
|
|
|
server.send_enabled = False
|
|
|
|
written = True
|
|
|
|
|
|
|
|
if not written:
|
|
|
|
sys.exit()
|
2018-07-15 14:15:52 +00:00
|
|
|
|
2019-02-10 14:32:04 +00:00
|
|
|
def _make_hook(self, server):
|
2019-10-11 16:06:37 +00:00
|
|
|
return lambda e: self._disconnect_hook(server)
|
|
|
|
def _disconnect_hook(self, server):
|
|
|
|
self.bot.disconnect(server)
|
|
|
|
if not self.bot.servers:
|
|
|
|
sys.exit()
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def SIGUSR1(self, signum, frame):
|
2018-11-27 15:10:49 +00:00
|
|
|
self.bot.trigger(self._reload_config)
|
|
|
|
|
2019-03-13 18:27:34 +00:00
|
|
|
def SIGHUP(self, signum, frame):
|
|
|
|
self.bot.trigger(self._SIGHUP)
|
|
|
|
def _SIGHUP(self):
|
|
|
|
self._reload_config()
|
|
|
|
self._reload_modules()
|
|
|
|
|
2018-11-27 15:10:49 +00:00
|
|
|
def _reload_config(self):
|
2019-06-06 11:59:47 +00:00
|
|
|
self.bot.log.info("Reloading config file")
|
2018-09-29 19:27:20 +00:00
|
|
|
self.bot.config.load()
|
2019-06-06 11:59:47 +00:00
|
|
|
self.bot.log.info("Reloaded config file")
|
2019-03-13 18:27:34 +00:00
|
|
|
|
|
|
|
def _reload_modules(self):
|
2019-06-06 11:59:47 +00:00
|
|
|
self.bot.log.info("Reloading modules")
|
2019-03-13 18:27:34 +00:00
|
|
|
|
2019-10-14 11:55:32 +00:00
|
|
|
result = self.bot.try_reload_modules()
|
|
|
|
if result.success:
|
|
|
|
self.bot.log.info(result.message)
|
|
|
|
else:
|
|
|
|
self.bot.log.warn(result.message)
|