From 721215b013b1de121a780b70dbc880bed0941d42 Mon Sep 17 00:00:00 2001 From: aewens Date: Fri, 2 Nov 2018 22:25:23 -0400 Subject: [PATCH] Various bug fixes --- actions/control.py | 10 +++++++--- actions/web.py | 15 ++++++++------- app.py | 27 ++++++++++++++++----------- bot/core.py | 20 ++++++++++---------- bot/tasks.py | 4 ++++ coroutines/rss.py | 4 +++- 6 files changed, 48 insertions(+), 32 deletions(-) diff --git a/actions/control.py b/actions/control.py index 73df618..24c4a73 100644 --- a/actions/control.py +++ b/actions/control.py @@ -2,12 +2,17 @@ def puppet(self, name, source, response): botnick = self.bot.botnick author = self.bot.author command = response.split("!puppet ")[1] - place, message = command.split(" ", 1) + mode, place, message = command.split(" ", 2) if name != author: return - self.bot.send_message(place, message) + modes = { + "say": self.bot.send_message, + "act": self.bot.send_action + } + default = lambda _, msg: self.bot.send_message(source, "Invalid action!") + modes.get(mode, default)(place, message) def inject(self, name, source, response): botnick = self.bot.botnick @@ -33,5 +38,4 @@ def nomad(self, name, source, response): "leave": self.bot.leave } default = lambda p: self.bot.send_message(source, "Invalid action!") - actions.get(action, default)(place) \ No newline at end of file diff --git a/actions/web.py b/actions/web.py index 47904d7..a9cdfd9 100644 --- a/actions/web.py +++ b/actions/web.py @@ -94,13 +94,14 @@ def whois(self, name, source, response): except HTTPError: self.bot.send_message(source, "{} cannot exist".format(domain)) return + + registered = data.get("registered", False) + nameservers = len(data.get("nameservers", list())) > 0 + self.bot.logger.debug("WHOIS: {}".format(data)) - registered = data.get("registered", None) - if registered is not None: - nameservers = len(data.get("nameservers", "")) - registrar = data.get("registrar", dict()) - is_registered = "id" in registrar or nameservers > 0 - status = "registered" if is_registered else "available" - self.bot.send_message(source, "{} is '{}'".format(domain, status)) + if registered and nameservers: + self.bot.send_message(source, "{} is '{}'".format(domain, "registered")) + elif not (registered or nameservers): + self.bot.send_message(source, "{} is '{}'".format(domain, "available")) else: self.bot.send_message(source, "{} might be available".format(domain)) \ No newline at end of file diff --git a/app.py b/app.py index 3d5b394..765941d 100755 --- a/app.py +++ b/app.py @@ -21,14 +21,6 @@ bot = Bot("127.0.0.1", 6667) responses = Responses(bot) tasks = Tasks(bot) -for action in actions: - if "type" in action and "pattern" in action and "callback" in action: - responses.add_trigger( - action["type"], - action["pattern"], - action["callback"] - ) - # for coro in coroutines: # worker = coro["worker"] # interval = coro["interval"] @@ -37,6 +29,14 @@ for action in actions: # tasks.add_coroutine(worker, interval, coro_state) tasks.coroutines = coroutines +for action in actions: + if "type" in action and "pattern" in action and "callback" in action: + responses.add_trigger( + action["type"], + action["pattern"], + action["callback"] + ) + def try_to_king_me(channel): bot.send_message("ChanServ", "REGISTER {}", channel) bot.send_message("ChanServ", "SET Successor {} {}", channel, bot.botnick) @@ -83,11 +83,16 @@ def handle_kick(name): def handle_message(name, source, response): responses.parse(name, source, response) if response == "!debug": - print("::", bot.memories) + bot.logger.debug(":: {}".format(bot.memories)) def handle_crashed(): - bot.crashed = False - bot.stop() + bot.logger.debug("Rebooting") + bot.crashed = True + bot.tasks.stop() + + tasks = Tasks(bot) + tasks.coroutines = coroutines + bot.tasks = tasks bot.start(arguments.config, dirname(realpath(__file__)), { "pm": handle_pm, diff --git a/bot/core.py b/bot/core.py index e96f855..8802c28 100644 --- a/bot/core.py +++ b/bot/core.py @@ -195,8 +195,8 @@ class Bot: setattr(self, name, attr) def stop(self): + self.send("QUIT :Overheating, powering down") self.running = False - self.send("QUIT") def start(self, config, location, callback): message = "" @@ -255,7 +255,7 @@ class Bot: if getattr(self.tasks, "run", None) is not None: self.tasks.run() - while self.running or not self.crashed: + while self.running: message = "" while self.splitter not in message: message = self.ircsock.recv(self.recv_size).decode() @@ -263,9 +263,9 @@ class Bot: message = message.strip(self.splitter) self.logger.debug("{}".format(message)) - if "ERROR" in message or ":Closing link:" in message: + if ":Closing link:" in message: self.logger.warning(message) - self.crashed = True + self.stop() if "crashed" in callback: callback["crashed"]() break @@ -277,6 +277,12 @@ class Bot: self.ping(message) if "ping" in callback: callback["ping"]() + elif "PRIVMSG " in message: + name, source, response = self.parse(message) + if source == self.botnick and "pm" in callback: + callback["pm"](name, response) + elif "message" in callback: + callback["message"](name, source, response) elif "MODE " in message: channel, mode = self.handle_mode(message) if "mode" in callback: @@ -301,11 +307,5 @@ class Bot: channel, name = self.handle_invite(message) if "invite" in callback: callback["invite"](channel, name) - elif "PRIVMSG " in message: - name, source, response = self.parse(message) - if source == self.botnick and "pm" in callback: - callback["pm"](name, response) - elif "message" in callback: - callback["message"](name, source, response) elif "unhandled" in callback: callback["unhandled"](message) \ No newline at end of file diff --git a/bot/tasks.py b/bot/tasks.py index 4718c7d..744a800 100644 --- a/bot/tasks.py +++ b/bot/tasks.py @@ -32,6 +32,10 @@ class Tasks: "state": state }) + def stop(self): + list(map(self.scheduler.cancel, self.scheduler.queue)) + self.thread.stop() + def run(self): self.thread.daemon = True self.thread.start() \ No newline at end of file diff --git a/coroutines/rss.py b/coroutines/rss.py index dc1377b..6e3a2e3 100644 --- a/coroutines/rss.py +++ b/coroutines/rss.py @@ -1,6 +1,6 @@ from xml.etree import ElementTree as etree from urllib.request import Request, urlopen -from urllib.error import HTTPError +from urllib.error import HTTPError, URLError from json import loads, dumps from re import sub @@ -62,6 +62,8 @@ class RSS: response = urlopen(req).read() except HTTPError: return + except URLError: + return feed = etree.fromstring(response) items = feed.findall("channel/item")