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:55:08 +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 = {}
|
|
|
|
|
2015-10-24 23:55:08 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.data = {
|
|
|
|
"prefix": "",
|
|
|
|
"subject": "",
|
|
|
|
"command": "",
|
|
|
|
"params": "",
|
|
|
|
"trailing": ""
|
|
|
|
}
|
2015-10-24 22:20:20 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_string(cls, string):
|
|
|
|
data = parse(string)
|
2015-10-24 23:55:08 +02:00
|
|
|
instance = cls._command_map.get(data["command"].upper(), cls)()
|
|
|
|
instance.set_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
|
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 23:55:08 +02:00
|
|
|
return result+"\r\n"
|
|
|
|
|
|
|
|
def get(self, attr):
|
|
|
|
return self.data[attr]
|
|
|
|
|
|
|
|
def set_data(self, data):
|
|
|
|
self.data = data
|
2015-10-24 22:20:20 +02:00
|
|
|
|
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
|
|
|
|
|
2015-10-24 23:55:08 +02:00
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
class Nick(Message, metaclass=register_derivative):
|
|
|
|
def __init__(self, nick=None, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.data.update({
|
|
|
|
"command": "NICK",
|
|
|
|
"trailing": nick
|
|
|
|
})
|
|
|
|
|
|
|
|
class Ping(Message, metaclass=register_derivative):
|
2015-10-24 23:05:55 +02:00
|
|
|
pass
|
|
|
|
|
2015-10-24 23:55:08 +02:00
|
|
|
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"]
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2015-10-24 23:05:55 +02:00
|
|
|
class Kick(Message, metaclass=register_derivative):
|
|
|
|
def __init__(self, channel=None, user=None, message="KICK", *args, **kwargs):
|
2015-10-24 23:55:08 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
2015-10-24 23:05:55 +02:00
|
|
|
self.data.update({
|
|
|
|
"command": "KICK",
|
|
|
|
"params": [channel, user],
|
|
|
|
"trailing": message
|
|
|
|
})
|
|
|
|
|
2015-10-24 23:55:08 +02:00
|
|
|
class Join(Message, metaclass=register_derivative):
|
|
|
|
def __init__(self, channel=None, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.data.update({
|
|
|
|
"command": "JOIN",
|
|
|
|
"trailing": channel
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
|
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:55:08 +02:00
|
|
|
line3 = Join("#botted")
|
|
|
|
print(str(line3))
|
2015-10-24 23:05:55 +02:00
|
|
|
|
2015-10-24 22:20:20 +02:00
|
|
|
exit()
|
|
|
|
|
|
|
|
|