bitbot-3.11-fork/modules/signals.py

41 lines
1.3 KiB
Python
Raw Normal View History

2016-03-29 11:56:58 +00:00
import signal
from src import Config, ModuleManager, utils
2016-03-29 11:56:58 +00:00
@utils.export("serverset", {"setting": "quit-quote",
"help": "Set whether I pick a random quote to /quit with",
"validate": utils.bool_or_none})
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)
def SIGINT(self, signum, frame):
print()
self.bot.trigger(lambda: self._kill(signum))
def _kill(self, signum):
2018-12-03 19:46:15 +00:00
if self._exited:
return
self._exited = True
self.events.on("signal.interrupt").call(signum=signum)
2016-03-29 11:56:58 +00:00
for server in self.bot.servers.values():
reason = "Leaving"
if server.get_setting("quit-quote", True):
reason = self.events.on("get.quit-quote"
).call_for_result(default=reason)
server.send_quit(reason)
self.events.on("writebuffer.empty").hook(
lambda event: self.bot.disconnect(event["server"]))
2016-03-29 11:56:58 +00:00
def SIGUSR1(self, signum, frame):
self.bot.trigger(self._reload_config)
def _reload_config(self):
2018-09-02 10:43:52 +00:00
self.bot.log.info("Reloading config file", [])
self.bot.config.load()
2018-09-02 10:43:52 +00:00
self.bot.log.info("Reloaded config file", [])