2015-10-24 22:20:20 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
def parse(line):
|
2015-10-24 22:27:08 +02:00
|
|
|
prefix = ""
|
|
|
|
if line[0:1] == ":":
|
|
|
|
prefix = ":"
|
|
|
|
subject, line = line.split(None, 1)
|
|
|
|
subject = subject[1:]
|
2015-10-24 22:20:20 +02:00
|
|
|
else:
|
2015-10-24 22:27:08 +02:00
|
|
|
subject = ""
|
|
|
|
if " :" in line:
|
|
|
|
tmp_str, trailing = line.split(" :", 1)
|
2015-10-24 22:20:20 +02:00
|
|
|
tmp_args = tmp_str.split()
|
|
|
|
else:
|
|
|
|
trailing = ""
|
|
|
|
tmp_args = line.split()
|
|
|
|
command, *middle = tmp_args
|
|
|
|
params = middle[:]
|
2015-10-24 23:05:55 +02:00
|
|
|
return {"prefix": prefix, "subject": subject, "command": command, "params": params, "trailing": trailing}
|
2015-10-24 22:20:20 +02:00
|
|
|
|
2015-10-24 23:05:55 +02:00
|
|
|
class Message(object):
|
2015-10-24 22:20:20 +02:00
|
|
|
"""Handles translation between strings and IrcLines
|
|
|
|
"""
|
2015-10-24 23:05:55 +02:00
|
|
|
_command_map = {}
|
|
|
|
|
|
|
|
def __init__(self, data={"prefix": "", "subject": "", "command": "", "params": "", "trailing": ""}):
|
|
|
|
self.data = data
|
2015-10-24 22:20:20 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_string(cls, string):
|
|
|
|
data = parse(string)
|
2015-10-24 23:05:55 +02:00
|
|
|
instance = cls._command_map.get(data["command"].upper(), cls)(data)
|
|
|
|
instance.data = data
|
2015-10-24 22:20:20 +02:00
|
|
|
return instance
|
|
|
|
|
|
|
|
def __repr__(self):
|
2015-10-24 23:05:55 +02:00
|
|
|
data = self.data
|
|
|
|
print(data)
|
2015-10-24 22:20:20 +02:00
|
|
|
e = []
|
2015-10-24 23:05:55 +02:00
|
|
|
if data["subject"]:
|
|
|
|
e.append(data["subject"])
|
|
|
|
if data["command"]:
|
|
|
|
e.append(data["command"])
|
|
|
|
if data["params"]:
|
|
|
|
e.append(" ".join(data["params"]))
|
|
|
|
if data["trailing"]:
|
|
|
|
e.append(":{}".format(data["trailing"]))
|
2015-10-24 22:20:20 +02:00
|
|
|
result = " ".join(e)
|
2015-10-24 23:05:55 +02:00
|
|
|
if data["prefix"]:
|
|
|
|
result = "".join([data["prefix"], result])
|
2015-10-24 22:20:20 +02:00
|
|
|
return result
|
|
|
|
|
2015-10-24 23:05:55 +02:00
|
|
|
def register_derivative(name, bases, attr):
|
|
|
|
new_cls = type(name, bases, attr)
|
|
|
|
for cls in bases:
|
|
|
|
cmd_map = getattr(cls, '_command_map', None)
|
|
|
|
if cmd_map is not None:
|
|
|
|
command = name.upper()
|
|
|
|
if command in cmd_map:
|
|
|
|
raise KeyError('command {} is already registered to this class'.format(command))
|
|
|
|
cmd_map[command] = new_cls
|
|
|
|
return new_cls
|
|
|
|
|
|
|
|
class Privmsg(Message, metaclass=register_derivative):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Kick(Message, metaclass=register_derivative):
|
|
|
|
def __init__(self, channel=None, user=None, message="KICK", *args, **kwargs):
|
|
|
|
super(Kick, self).__init__(*args, **kwargs)
|
|
|
|
self.data.update({
|
|
|
|
"command": "KICK",
|
|
|
|
"params": [channel, user],
|
|
|
|
"trailing": message
|
|
|
|
})
|
|
|
|
|
2015-10-24 22:20:20 +02:00
|
|
|
|
2015-10-24 22:27:08 +02:00
|
|
|
if __name__ == "__main__":
|
2015-10-24 23:05:55 +02:00
|
|
|
l = Message.from_string(":JPT|NC!~AS@euirc-6f528752.pools.arcor-ip.net JOIN :#euirc")
|
2015-10-24 22:20:20 +02:00
|
|
|
print(str(l))
|
|
|
|
print()
|
|
|
|
|
2015-10-24 23:05:55 +02:00
|
|
|
l = Message.from_string(":ChanServ!services@euirc.net MODE #Tonari. +ao JPT JPT")
|
2015-10-24 22:20:20 +02:00
|
|
|
print(str(l))
|
|
|
|
print()
|
|
|
|
|
2015-10-24 23:05:55 +02:00
|
|
|
line = Kick("#botted", "JPT", "Du Sack!")
|
2015-10-24 22:20:20 +02:00
|
|
|
print(str(line))
|
2015-10-24 23:05:55 +02:00
|
|
|
|
|
|
|
line2 = Message.from_string(str(line))
|
2015-10-24 22:20:20 +02:00
|
|
|
print(str(line2))
|
|
|
|
exit()
|
|
|
|
|
|
|
|
|