Various bug fixes

This commit is contained in:
aewens 2018-11-02 22:25:23 -04:00
parent 57419aa78a
commit 721215b013
6 changed files with 48 additions and 32 deletions

View File

@ -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)

View File

@ -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))

27
app.py
View File

@ -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,

View File

@ -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)

View File

@ -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()

View File

@ -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")