[TASK] Disable noisy logging, begin parsing Messages.

This commit is contained in:
Jan Philipp Timme 2015-10-25 00:51:57 +02:00
parent aed245779f
commit 83f953aaa8
2 changed files with 87 additions and 49 deletions

13
bot.py
View File

@ -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"]:

65
irc.py
View File

@ -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,7 +83,8 @@ 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({
if ident != None and realname != None:
self.update({
"command": "USER",
"params": [ident, "*", "*"],
"trailing": realname
@ -85,61 +93,86 @@ class User(Message, metaclass=register_derivative):
class Nick(Message, metaclass=register_derivative):
def __init__(self, nick=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.data.update({
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({
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({
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({
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({
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({
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({
if channel != None:
self.update({
"command": "PART",
"params": [channel],
"trailing": message
@ -160,5 +193,3 @@ if __name__ == "__main__":
print(str(line3))
exit()