diff --git a/.gitignore b/.gitignore index 3605b70..2584d24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # App-specific settings.json settings.demo.json +settings.test.json data/*.json logs/*.log* logs/*.log.* diff --git a/actions/web.py b/actions/web.py index a9cdfd9..1d59bf8 100644 --- a/actions/web.py +++ b/actions/web.py @@ -75,9 +75,8 @@ def summon(self, name, source, response): self.bot.send_message(source, confirmation) def how_dare_you(self, name, source, response): - user = response.split("!summon ")[1] rude = "{}: You think you can just summon someone without a reason? Rude." - self.bot.send_message(source, rude.format(user)) + self.bot.send_message(source, rude.format(name)) def whois(self, name, source, response): botnick = self.bot.botnick @@ -104,4 +103,4 @@ def whois(self, name, source, response): 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 + self.bot.send_message(source, "{} might be available".format(domain)) diff --git a/app.py b/app.py index 2d9a71a..b16c1d5 100755 --- a/app.py +++ b/app.py @@ -65,7 +65,9 @@ def handle_invite(channel, name): if changed: bot.thread(bot.save_memories) -def handle_kick(name): +def handle_kick(name, source): + if source in bot.settings.get("extras", dict()).get("rejoin", list()): + bot.join(source) users = bot.memories["users"] if name not in users: bot.memories["users"][name] = dict() @@ -79,8 +81,8 @@ def handle_message(name, source, response): bot.logger.debug(":: {}".format(bot.memories)) def handle_raw(message): - with open("/tmp/babs", "a") as babs: - babs.write(repr(message) + "\n") + if "KICK #chaos {}".format(bot.author) in message: + bot.send("INVITE {} :#chaos".format(bot.author)) def handle_crashed(): bot.logger.debug("Rebooting") diff --git a/bot/core.py b/bot/core.py index 86899af..654b6cc 100644 --- a/bot/core.py +++ b/bot/core.py @@ -1,4 +1,6 @@ import re +import ssl +import time import json import socket import os.path @@ -13,11 +15,12 @@ logging.basicConfig( ) class Bot: - def __init__(self, server, port): + def __init__(self, server, port, secure=False): self.ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.logger = logging.getLogger("") self.server = server self.port = port + self.secure = secure self.channels = [] self.running = True self.crashed = False @@ -31,6 +34,9 @@ class Bot: self.recv_size = 2048 self.splitter = "\r\n" + if self.secure: + self.ircsock = ssl.wrap_socket(self.ircsock) + def send(self, message, *args): response = message.format(*args) + "\n" password = self.settings.get("password", None) @@ -65,9 +71,13 @@ class Bot: message = "" magic_string = "End of /NAMES list." + banned = "Cannot join channel (You're banned)" while magic_string not in message: try: message = self.ircsock.recv(self.recv_size).decode() + if banned in message: + self.places.remove(chan) + return # message = message.strip(self.splitter) self.logger.debug(message) except UnicodeDecodeError: @@ -141,8 +151,9 @@ class Bot: def handle_kick(self, message): before, after = message.split("KICK ", 1) + source = after.split(" ", 1)[0] name = self.parse_name(self.get_name(before)) - return name + return name, source def handle_join(self, message): before, after = message.split("JOIN ", 1) @@ -231,9 +242,10 @@ class Bot: "needs_to_register": "choose a different nick", "needs_to_confirm": "Your account will expire" } - + + authenticate = len(password) > 0 and len(confirm) > 0 magic_string = "MODE {} +r".format(self.botnick) - while magic_string not in message: + while magic_string not in message and authenticate: try: message = self.ircsock.recv(self.recv_size).decode() except UnicodeDecodeError: @@ -248,7 +260,9 @@ class Bot: if not confirmed and magic_phrase["needs_to_confirm"] in message: self.send_message("NickServ", "CONFIRM {}", self.confirm) confirmed = True - + if not authenticate: + time.sleep(3) + self.send("MODE {} +B".format(self.botnick)) print("DEBUG: Joining") @@ -298,9 +312,9 @@ class Bot: if "nick" in callback: callback["nick"](old_name, new_name) elif "KICK " in message: - kicker = self.handle_kick(message) + kicker, source = self.handle_kick(message) if "kick" in callback: - callback["kick"](kicker) + callback["kick"](kicker, source) elif "JOIN " in message: user = self.handle_join(message) if "join" in callback: diff --git a/bot/responses.py b/bot/responses.py index b96a62d..b014ae9 100644 --- a/bot/responses.py +++ b/bot/responses.py @@ -20,10 +20,6 @@ class Responses: if name not in users: return False - if name in users and "blacklist" in users[name]: - reason = users[name]["blacklist"]["reason"] - return False - last_response = 0 if "last_response" in self.bot.memories["users"][name]: last_response = self.bot.memories["users"][name]["last_response"] @@ -59,13 +55,26 @@ class Responses: for trigger in list(self.triggers.keys()): for pattern, callback in self.triggers[trigger].items(): if pattern[0] != "/" and pattern[-1] != "/": - if pattern == check and self.allowed(name, source): - callback(self, name, source, response) + if pattern == check: + if self.allowed(name, source): + callback(self, name, source, response) + elif "blacklist" in users[name]: + reason = users[name]["blacklist"]["reason"] + message = "You were banished for reason '{}'" + message = message.format(reason) + #self.bot.send_message(name, message) + return False else: regex = re.compile(pattern[1:-1]) if regex.match(trig[trigger]) is not None: if self.allowed(name, source): callback(self, name, source, response) + elif "blacklist" in users[name]: + reason = users[name]["blacklist"]["reason"] + message = "You were banished for reason '{}'" + message = message.format(reason) + #self.bot.send_message(name, message) + return False now = datetime.now().timestamp() - self.bot.memories["users"][name]["last_response"] = now \ No newline at end of file + self.bot.memories["users"][name]["last_response"] = now diff --git a/coroutines/__init__.py b/coroutines/__init__.py index 14af24d..8e438c0 100644 --- a/coroutines/__init__.py +++ b/coroutines/__init__.py @@ -18,7 +18,7 @@ coroutines = [ "state": { "alias": "bbj", "source": "http://localhost:7099/api", - "channels": ["#team"] + "channels": ["#team", "#tildeverse"] } }, { @@ -41,4 +41,4 @@ coroutines = [ "channels": ["#tildeverse"] } } -] \ No newline at end of file +] diff --git a/settings.example.json b/settings.example.json old mode 100644 new mode 100755