[TASK] Beautify log, add __repr__ to Message, parse Nick.

This commit is contained in:
Jan Philipp Timme 2015-10-25 12:51:17 +01:00
parent b34817331e
commit 906f2a6fa5
2 changed files with 26 additions and 8 deletions

16
bot.py
View File

@ -22,30 +22,30 @@ class ManagedProtocol(asyncio.Protocol):
self._transport = None
self._config = config
def _log(self, msg):
def log(self, msg):
host, port = self._endpoint
logger.info("[{}:{}] ".format(host, port)+str(msg))
def connection_made(self, transport):
self._connection_manager.register_active_connection(self._endpoint, self)
self._transport = transport
self._log("Connection made!")
self.log("Connection made!")
host, port = transport.get_extra_info("peername")
self._log("Connected to: {}:{}".format(host, port))
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!")
self.log("Eof received!")
def connection_lost(self, exc):
self._log("Connection lost! ("+str(exc)+")")
self.log("Connection lost! ("+str(exc)+")")
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):
@ -95,10 +95,12 @@ class IrcProtocol(ManagedProtocol):
def send_msg(self, msg):
if isinstance(msg, irc.Message):
self.log(msg.__repr__())
data = self.encode(str(msg)+"\r\n")
self.send_data(data)
def msg_received(self, msg):
self.log(msg.__repr__())
if isinstance(msg, irc.Ping):
self.send_msg(irc.Pong(msg))
if isinstance(msg, irc.Message) and msg.get('command') == "376":

18
irc.py
View File

@ -64,6 +64,15 @@ class Message(object):
return instance
def __repr__(self):
items = self.__dict__.copy()
e = []
for key in items:
if key == "data":
continue
e.append(key+"="+str(items[key]))
return "<" + self.__class__.__name__ + " " + ", ".join(e) + ">"
def __str__(self):
data = self.data
e = []
if data["subject"]:
@ -113,6 +122,9 @@ class User(Message, metaclass=register_derivative):
"params": [ident, "*", "*"],
"trailing": realname
})
def parse(self):
self.ident = self.get("params")[0]
self.realname = self.get("trailing")
class Nick(Message, metaclass=register_derivative):
def __init__(self, nick="", *args, **kwargs):
@ -122,6 +134,9 @@ class Nick(Message, metaclass=register_derivative):
"command": "NICK",
"trailing": nick
})
def parse(self):
self.old_nick = self.get("nick")
self.nick = self.get("trailing")
class Ping(Message, metaclass=register_derivative):
def parse(self):
@ -262,13 +277,14 @@ class Topic(Message, metaclass=register_derivative):
if __name__ == "__main__":
buffer = b":irc.inn.at.euirc.net 001 JPT|NC :Welcome to the euIRCnet IRC Network JPT|NC!~AS@dslc-082-082-091-237.pools.arcor-ip.net\r\n:JPT|NC MODE JPT|NC :+ix\r\n:SpamScanner!service@central.euirc.net PRIVMSG JPT|NC :\x01VERSION\x01\r\n:JPT|NC!~AS@euirc-6f528752.pools.arcor-ip.net JOIN :#Tonari.\r\n:Lunlun!~l00n@euirc-e7be0d00.dip0.t-ipconnect.de JOIN :#Tonari.\r\n:ChanServ!services@euirc.net MODE #Tonari. +ao Lunlun Lunlun\r\n:Nitori!~kappa@chireiden.net PRIVMSG JPT|NC :\x01VERSION\x01\r\nPING :irc.inn.at.euirc.net\r\n:JPT|NC!~ADS@dslc-082-082-091-237.pools.arcor-ip.net JOIN #botted\r\n:JPT|NC!~ADS@dslc-082-082-091-237.pools.arcor-ip.net QUIT :Ping timeout: 272 seconds\r\nERROR :Closing Link: dslc-082-082-091-237.pools.arcor-ip.net (Ping timeout: 272 seconds)\r\n:JPT!~jpt@jpt.lu MODE #botted -h Pb42\r\n:JPT!~jpt@jpt.lu MODE #botted +v Pb42\r\n:JPT!~jpt@jpt.lu MODE #botted +o Pb42\r\n:JPT!~jpt@jpt.lu MODE #botted -vo Pb42 Pb42\r\n:JPT!~jpt@jpt.lu MODE #botted +b *illegal*!*@*\r\n:JPT!~jpt@jpt.lu MODE #botted -b *illegal*!*@*\r\n:JPT!~jpt@jpt.lu TOPIC #botted :#botted - edited\r\nPING :irc.hes.de.euirc.net\r\nPONG :irc.hes.de.euirc.net\r\n:JPT!~jpt@jpt.lu TOPIC #botted :#botted\r\n:JPT!~jpt@jpt.lu MODE #botted +i\r\n:JPT!~jpt@jpt.lu MODE #botted -i\r\n"
buffer = b":irc.inn.at.euirc.net 001 JPT|NC :Welcome to the euIRCnet IRC Network JPT|NC!~AS@dslc-082-082-091-237.pools.arcor-ip.net\r\n:JPT|NC MODE JPT|NC :+ix\r\n:SpamScanner!service@central.euirc.net PRIVMSG JPT|NC :\x01VERSION\x01\r\n:JPT|NC!~AS@euirc-6f528752.pools.arcor-ip.net JOIN :#Tonari.\r\n:Lunlun!~l00n@euirc-e7be0d00.dip0.t-ipconnect.de JOIN :#Tonari.\r\n:ChanServ!services@euirc.net MODE #Tonari. +ao Lunlun Lunlun\r\n:Nitori!~kappa@chireiden.net PRIVMSG JPT|NC :\x01VERSION\x01\r\nPING :irc.inn.at.euirc.net\r\n:JPT|NC!~ADS@dslc-082-082-091-237.pools.arcor-ip.net JOIN #botted\r\n:JPT|NC!~ADS@dslc-082-082-091-237.pools.arcor-ip.net QUIT :Ping timeout: 272 seconds\r\nERROR :Closing Link: dslc-082-082-091-237.pools.arcor-ip.net (Ping timeout: 272 seconds)\r\n:JPT!~jpt@jpt.lu MODE #botted -h Pb42\r\n:JPT!~jpt@jpt.lu MODE #botted +v Pb42\r\n:JPT!~jpt@jpt.lu MODE #botted +o Pb42\r\n:JPT!~jpt@jpt.lu MODE #botted -vo Pb42 Pb42\r\n:JPT!~jpt@jpt.lu MODE #botted +b *illegal*!*@*\r\n:JPT!~jpt@jpt.lu MODE #botted -b *illegal*!*@*\r\n:JPT!~jpt@jpt.lu TOPIC #botted :#botted - edited\r\nPING :irc.hes.de.euirc.net\r\nPONG :irc.hes.de.euirc.net\r\n:JPT!~jpt@jpt.lu TOPIC #botted :#botted\r\n:JPT!~jpt@jpt.lu MODE #botted +i\r\n:JPT!~jpt@jpt.lu MODE #botted -i\r\n:JPT!~jpt@jpt.lu NICK :whoops\r\n"
while b"\r\n" in buffer:
line, buffer = buffer.split(b"\r\n", 1)
if line == b"":
continue
line = line.decode("utf-8")
msg = Message.from_string(line)
print(msg.__repr__())
print(str(msg))
if msg.__class__.__name__ == 'Message':
print(msg.__dict__)