[TASK] Disable noisy logging, begin parsing Messages.
This commit is contained in:
parent
aed245779f
commit
83f953aaa8
13
bot.py
13
bot.py
|
@ -34,7 +34,8 @@ class ManagedProtocol(asyncio.Protocol):
|
||||||
self._log("Connected to: {}:{}".format(host, port))
|
self._log("Connected to: {}:{}".format(host, port))
|
||||||
|
|
||||||
def data_received(self, data):
|
def data_received(self, data):
|
||||||
self._log("[R] "+str(data))
|
#self._log("[R] "+str(data))
|
||||||
|
pass
|
||||||
|
|
||||||
def eof_received(self):
|
def eof_received(self):
|
||||||
self._log("Eof received!")
|
self._log("Eof received!")
|
||||||
|
@ -44,7 +45,7 @@ class ManagedProtocol(asyncio.Protocol):
|
||||||
self._connection_manager.unregister_active_connection(self._endpoint)
|
self._connection_manager.unregister_active_connection(self._endpoint)
|
||||||
|
|
||||||
def send_data(self, data):
|
def send_data(self, data):
|
||||||
self._log("[W] "+str(data))
|
#self._log("[W] "+str(data))
|
||||||
self._transport.write(data)
|
self._transport.write(data)
|
||||||
|
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
|
@ -85,12 +86,14 @@ class IrcProtocol(ManagedProtocol):
|
||||||
while b"\r\n" in self._buffer:
|
while b"\r\n" in self._buffer:
|
||||||
line, self._buffer = self._buffer.split(b"\r\n", 1)
|
line, self._buffer = self._buffer.split(b"\r\n", 1)
|
||||||
line = self.decode(line.strip())
|
line = self.decode(line.strip())
|
||||||
|
if line == "":
|
||||||
|
continue
|
||||||
msg = irc.Message.from_string(line)
|
msg = irc.Message.from_string(line)
|
||||||
self.msg_received(msg)
|
self.msg_received(msg)
|
||||||
|
|
||||||
def send_msg(self, msg):
|
def send_msg(self, msg):
|
||||||
if isinstance(msg, irc.Message):
|
if isinstance(msg, irc.Message):
|
||||||
data = self.encode(str(msg))
|
data = self.encode(str(msg)+"\r\n")
|
||||||
self.send_data(data)
|
self.send_data(data)
|
||||||
|
|
||||||
def msg_received(self, msg):
|
def msg_received(self, msg):
|
||||||
|
@ -98,6 +101,10 @@ class IrcProtocol(ManagedProtocol):
|
||||||
self.send_msg(irc.Pong(msg))
|
self.send_msg(irc.Pong(msg))
|
||||||
if isinstance(msg, irc.Message) and msg.get('command') == "376":
|
if isinstance(msg, irc.Message) and msg.get('command') == "376":
|
||||||
self.ready()
|
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):
|
def ready(self):
|
||||||
for channel in self._config["channels"]:
|
for channel in self._config["channels"]:
|
||||||
|
|
123
irc.py
123
irc.py
|
@ -20,7 +20,7 @@ def parse(line):
|
||||||
|
|
||||||
|
|
||||||
class Message(object):
|
class Message(object):
|
||||||
"""Handles translation between strings and IrcLines
|
"""Handles translation between strings and Message instances
|
||||||
"""
|
"""
|
||||||
_command_map = {}
|
_command_map = {}
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ class Message(object):
|
||||||
def from_string(cls, string):
|
def from_string(cls, string):
|
||||||
data = parse(string)
|
data = parse(string)
|
||||||
instance = cls._command_map.get(data["command"].upper(), cls)()
|
instance = cls._command_map.get(data["command"].upper(), cls)()
|
||||||
instance.set_data(data)
|
instance.update(data)
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -47,20 +47,27 @@ class Message(object):
|
||||||
e.append(data["subject"])
|
e.append(data["subject"])
|
||||||
if data["command"]:
|
if data["command"]:
|
||||||
e.append(data["command"])
|
e.append(data["command"])
|
||||||
if data["params"]:
|
if data["params"] and len(data["params"]) > 0:
|
||||||
e.append(" ".join(data["params"]))
|
e.append(" ".join(data["params"]))
|
||||||
if data["trailing"]:
|
if data["trailing"]:
|
||||||
e.append(":{}".format(data["trailing"]))
|
e.append(":{}".format(data["trailing"]))
|
||||||
result = " ".join(e)
|
result = " ".join(e)
|
||||||
if data["prefix"]:
|
if data["prefix"]:
|
||||||
result = "".join([data["prefix"], result])
|
result = "".join([data["prefix"], result])
|
||||||
return result+"\r\n"
|
return result
|
||||||
|
|
||||||
def get(self, attr):
|
def get(self, attr):
|
||||||
return self.data[attr]
|
return self.data[attr]
|
||||||
|
|
||||||
def set_data(self, data):
|
def parse(self):
|
||||||
self.data = data
|
"""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):
|
def register_derivative(name, bases, attr):
|
||||||
new_cls = type(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):
|
class User(Message, metaclass=register_derivative):
|
||||||
def __init__(self, ident=None, realname=None, *args, **kwargs):
|
def __init__(self, ident=None, realname=None, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.data.update({
|
if ident != None and realname != None:
|
||||||
"command": "USER",
|
self.update({
|
||||||
"params": [ident, "*", "*"],
|
"command": "USER",
|
||||||
"trailing": realname
|
"params": [ident, "*", "*"],
|
||||||
})
|
"trailing": realname
|
||||||
|
})
|
||||||
|
|
||||||
class Nick(Message, metaclass=register_derivative):
|
class Nick(Message, metaclass=register_derivative):
|
||||||
def __init__(self, nick=None, *args, **kwargs):
|
def __init__(self, nick=None, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.data.update({
|
if nick != None:
|
||||||
"command": "NICK",
|
self.update({
|
||||||
"trailing": nick
|
"command": "NICK",
|
||||||
})
|
"trailing": nick
|
||||||
|
})
|
||||||
|
|
||||||
class Ping(Message, metaclass=register_derivative):
|
class Ping(Message, metaclass=register_derivative):
|
||||||
pass
|
def parse(self):
|
||||||
|
self.payload = self.get("trailing")
|
||||||
|
|
||||||
class Pong(Message, metaclass=register_derivative):
|
class Pong(Message, metaclass=register_derivative):
|
||||||
def __init__(self, ping=None, *args, **kwargs):
|
def __init__(self, ping=None, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.data.update({
|
if ping != None:
|
||||||
"command": "PONG",
|
self.update({
|
||||||
"trailing": ping.data["trailing"]
|
"command": "PONG",
|
||||||
})
|
"trailing": ping.data["trailing"]
|
||||||
|
})
|
||||||
|
def parse(self):
|
||||||
|
self.payload = self.get("trailing")
|
||||||
|
|
||||||
class Privmsg(Message, metaclass=register_derivative):
|
class Privmsg(Message, metaclass=register_derivative):
|
||||||
def __init__(self, target=None, message=None, *args, **kwargs):
|
def __init__(self, target=None, message=None, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.data.update({
|
if target != None and message != None:
|
||||||
"command": "PRIVMSG",
|
self.update({
|
||||||
"params": [target],
|
"command": "PRIVMSG",
|
||||||
"trailing": message
|
"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):
|
class Notice(Message, metaclass=register_derivative):
|
||||||
def __init__(self, target=None, message=None, *args, **kwargs):
|
def __init__(self, target=None, message=None, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.data.update({
|
if target != None and message != None:
|
||||||
"command": "NOTICE",
|
self.update({
|
||||||
"params": [target],
|
"command": "NOTICE",
|
||||||
"trailing": message
|
"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):
|
class Kick(Message, metaclass=register_derivative):
|
||||||
def __init__(self, channel=None, user=None, message="KICK", *args, **kwargs):
|
def __init__(self, channel=None, user=None, message="KICK", *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.data.update({
|
if channel != None and user != None:
|
||||||
"command": "KICK",
|
self.update({
|
||||||
"params": [channel, user],
|
"command": "KICK",
|
||||||
"trailing": message
|
"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):
|
class Join(Message, metaclass=register_derivative):
|
||||||
def __init__(self, channel=None, *args, **kwargs):
|
def __init__(self, channel=None, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.data.update({
|
if channel != None:
|
||||||
"command": "JOIN",
|
self.update({
|
||||||
"trailing": channel
|
"command": "JOIN",
|
||||||
})
|
"trailing": channel
|
||||||
|
})
|
||||||
|
def parse(self):
|
||||||
|
self.nick = self.get("subject")
|
||||||
|
self.channel = self.get("trailing")
|
||||||
|
|
||||||
class Part(Message, metaclass=register_derivative):
|
class Part(Message, metaclass=register_derivative):
|
||||||
def __init__(self, channel=None, message="PART", *args, **kwargs):
|
def __init__(self, channel=None, message="PART", *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.data.update({
|
if channel != None:
|
||||||
"command": "PART",
|
self.update({
|
||||||
"params": [channel],
|
"command": "PART",
|
||||||
"trailing": message
|
"params": [channel],
|
||||||
})
|
"trailing": message
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -160,5 +193,3 @@ if __name__ == "__main__":
|
||||||
print(str(line3))
|
print(str(line3))
|
||||||
|
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue