From 83f953aaa83235c13ab4d0a0a24220694d0b3a2a Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 25 Oct 2015 00:51:57 +0200 Subject: [PATCH] [TASK] Disable noisy logging, begin parsing Messages. --- bot.py | 13 ++++-- irc.py | 123 ++++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 87 insertions(+), 49 deletions(-) diff --git a/bot.py b/bot.py index b1aad78..f35e95e 100644 --- a/bot.py +++ b/bot.py @@ -34,7 +34,8 @@ class ManagedProtocol(asyncio.Protocol): self._log("Connected to: {}:{}".format(host, port)) def data_received(self, data): - self._log("[R] "+str(data)) + #self._log("[R] "+str(data)) + pass def eof_received(self): self._log("Eof received!") @@ -44,7 +45,7 @@ class ManagedProtocol(asyncio.Protocol): self._connection_manager.unregister_active_connection(self._endpoint) def send_data(self, data): - self._log("[W] "+str(data)) + #self._log("[W] "+str(data)) self._transport.write(data) def destroy(self): @@ -85,12 +86,14 @@ class IrcProtocol(ManagedProtocol): while b"\r\n" in self._buffer: line, self._buffer = self._buffer.split(b"\r\n", 1) line = self.decode(line.strip()) + if line == "": + continue msg = irc.Message.from_string(line) self.msg_received(msg) def send_msg(self, msg): if isinstance(msg, irc.Message): - data = self.encode(str(msg)) + data = self.encode(str(msg)+"\r\n") self.send_data(data) def msg_received(self, msg): @@ -98,6 +101,10 @@ class IrcProtocol(ManagedProtocol): self.send_msg(irc.Pong(msg)) if isinstance(msg, irc.Message) and msg.get('command') == "376": self.ready() + if isinstance(msg, irc.Privmsg): + if msg.message == "-cycle": + self.send_msg(irc.Part(msg.target, "Hop!")) + self.send_msg(irc.Join(msg.target)) def ready(self): for channel in self._config["channels"]: diff --git a/irc.py b/irc.py index 2458dba..f2f8b49 100644 --- a/irc.py +++ b/irc.py @@ -20,7 +20,7 @@ def parse(line): class Message(object): - """Handles translation between strings and IrcLines + """Handles translation between strings and Message instances """ _command_map = {} @@ -37,7 +37,7 @@ class Message(object): def from_string(cls, string): data = parse(string) instance = cls._command_map.get(data["command"].upper(), cls)() - instance.set_data(data) + instance.update(data) return instance def __repr__(self): @@ -47,20 +47,27 @@ class Message(object): e.append(data["subject"]) if data["command"]: e.append(data["command"]) - if data["params"]: + if data["params"] and len(data["params"]) > 0: e.append(" ".join(data["params"])) if data["trailing"]: e.append(":{}".format(data["trailing"])) result = " ".join(e) if data["prefix"]: result = "".join([data["prefix"], result]) - return result+"\r\n" + return result def get(self, attr): return self.data[attr] - def set_data(self, data): - self.data = data + def parse(self): + """Empty parse method to override by subclasses for THEIR CUSTOM FIELDS.""" + pass + + def update(self, data): + self.data.update(data) + # Reparse self in order to get consistent data for subclasses + self.data.update(parse(str(self))) + self.parse() def register_derivative(name, bases, attr): new_cls = type(name, bases, attr) @@ -76,74 +83,100 @@ def register_derivative(name, bases, attr): class User(Message, metaclass=register_derivative): def __init__(self, ident=None, realname=None, *args, **kwargs): super().__init__(*args, **kwargs) - self.data.update({ - "command": "USER", - "params": [ident, "*", "*"], - "trailing": realname - }) + if ident != None and realname != None: + self.update({ + "command": "USER", + "params": [ident, "*", "*"], + "trailing": realname + }) class Nick(Message, metaclass=register_derivative): def __init__(self, nick=None, *args, **kwargs): super().__init__(*args, **kwargs) - self.data.update({ - "command": "NICK", - "trailing": nick - }) + if nick != None: + self.update({ + "command": "NICK", + "trailing": nick + }) class Ping(Message, metaclass=register_derivative): - pass + def parse(self): + self.payload = self.get("trailing") class Pong(Message, metaclass=register_derivative): def __init__(self, ping=None, *args, **kwargs): super().__init__(*args, **kwargs) - self.data.update({ - "command": "PONG", - "trailing": ping.data["trailing"] - }) + if ping != None: + self.update({ + "command": "PONG", + "trailing": ping.data["trailing"] + }) + def parse(self): + self.payload = self.get("trailing") class Privmsg(Message, metaclass=register_derivative): def __init__(self, target=None, message=None, *args, **kwargs): super().__init__(*args, **kwargs) - self.data.update({ - "command": "PRIVMSG", - "params": [target], - "trailing": message - }) + if target != None and message != None: + self.update({ + "command": "PRIVMSG", + "params": [target], + "trailing": message + }) + def parse(self): + self.source = self.get("subject") + self.target = self.get("params")[0] + self.message = self.get("trailing") class Notice(Message, metaclass=register_derivative): def __init__(self, target=None, message=None, *args, **kwargs): super().__init__(*args, **kwargs) - self.data.update({ - "command": "NOTICE", - "params": [target], - "trailing": message - }) + if target != None and message != None: + self.update({ + "command": "NOTICE", + "params": [target], + "trailing": message + }) + def parse(self): + self.source = self.get("subject") + self.target = self.get("params")[0] + self.message = self.get("trailing") class Kick(Message, metaclass=register_derivative): def __init__(self, channel=None, user=None, message="KICK", *args, **kwargs): super().__init__(*args, **kwargs) - self.data.update({ - "command": "KICK", - "params": [channel, user], - "trailing": message - }) + if channel != None and user != None: + self.update({ + "command": "KICK", + "params": [channel, user], + "trailing": message + }) + def parse(self): + self.source = self.get("subject") + self.target = self.get("params")[0] + self.message = self.get("trailing") class Join(Message, metaclass=register_derivative): def __init__(self, channel=None, *args, **kwargs): super().__init__(*args, **kwargs) - self.data.update({ - "command": "JOIN", - "trailing": channel - }) + if channel != None: + self.update({ + "command": "JOIN", + "trailing": channel + }) + def parse(self): + self.nick = self.get("subject") + self.channel = self.get("trailing") class Part(Message, metaclass=register_derivative): def __init__(self, channel=None, message="PART", *args, **kwargs): super().__init__(*args, **kwargs) - self.data.update({ - "command": "PART", - "params": [channel], - "trailing": message - }) + if channel != None: + self.update({ + "command": "PART", + "params": [channel], + "trailing": message + }) @@ -160,5 +193,3 @@ if __name__ == "__main__": print(str(line3)) exit() - -