2019-06-21 09:33:59 +00:00
|
|
|
import datetime, typing, uuid
|
2019-10-25 22:32:00 +00:00
|
|
|
from src import EventManager, IRCObject, utils
|
2019-02-10 18:43:04 +00:00
|
|
|
|
2019-02-10 22:52:23 +00:00
|
|
|
# this should be 510 (RFC1459, 512 with \r\n) but a server BitBot uses is broken
|
2019-06-18 13:14:37 +00:00
|
|
|
LINE_MAX = 470
|
2019-02-10 14:18:33 +00:00
|
|
|
|
2019-02-23 21:33:04 +00:00
|
|
|
class IRCArgs(object):
|
|
|
|
def __init__(self, args: typing.List[str]):
|
|
|
|
self._args = args
|
|
|
|
|
|
|
|
def get(self, index: int) -> typing.Optional[str]:
|
2019-03-08 23:35:52 +00:00
|
|
|
if index < 0:
|
|
|
|
if len(self._args) > (abs(index)-1):
|
|
|
|
return self._args[index]
|
|
|
|
elif len(self._args) > index:
|
2019-02-23 21:33:04 +00:00
|
|
|
return self._args[index]
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "IRCArgs(%s)" % self._args
|
|
|
|
def __len__(self) -> int:
|
|
|
|
return len(self._args)
|
2019-02-23 21:39:46 +00:00
|
|
|
def __getitem__(self, index: int) -> str:
|
2019-02-23 21:33:04 +00:00
|
|
|
return self._args[index]
|
2019-02-23 21:39:46 +00:00
|
|
|
def __setitem__(self, index: int, value: str):
|
|
|
|
self._args[index] = value
|
2019-02-23 21:33:04 +00:00
|
|
|
|
2019-11-08 17:41:18 +00:00
|
|
|
def append(self, value: str):
|
|
|
|
self._args.append(value)
|
|
|
|
|
2019-02-23 21:33:04 +00:00
|
|
|
class Hostmask(object):
|
|
|
|
def __init__(self, nickname: str, username: str, hostname: str,
|
|
|
|
hostmask: str):
|
|
|
|
self.nickname = nickname
|
|
|
|
self.username = username
|
|
|
|
self.hostname = hostname
|
|
|
|
self.hostmask = hostmask
|
|
|
|
def __repr__(self):
|
|
|
|
return "Hostmask(%s)" % self.__str__()
|
|
|
|
def __str__(self):
|
|
|
|
return self.hostmask
|
|
|
|
|
2019-10-27 10:19:00 +00:00
|
|
|
def parse_hostmask(hostmask: str) -> Hostmask:
|
|
|
|
nickname, _, username = hostmask.partition("!")
|
|
|
|
username, _, hostname = username.partition("@")
|
|
|
|
return Hostmask(nickname, username, hostname, hostmask)
|
|
|
|
|
|
|
|
MESSAGE_TAG_ESCAPED = [r"\:", r"\s", r"\\", r"\r", r"\n"]
|
|
|
|
MESSAGE_TAG_UNESCAPED = [";", " ", "\\", "\r", "\n"]
|
|
|
|
def message_tag_escape(s):
|
|
|
|
return utils.irc.multi_replace(s, MESSAGE_TAG_UNESCAPED,
|
|
|
|
MESSAGE_TAG_ESCAPED)
|
|
|
|
def message_tag_unescape(s):
|
|
|
|
unescaped = utils.irc.multi_replace(s, MESSAGE_TAG_ESCAPED,
|
|
|
|
MESSAGE_TAG_UNESCAPED)
|
|
|
|
return unescaped.replace("\\", "")
|
|
|
|
|
2019-02-23 21:33:04 +00:00
|
|
|
class ParsedLine(object):
|
|
|
|
def __init__(self, command: str, args: typing.List[str],
|
2019-06-03 12:30:39 +00:00
|
|
|
source: Hostmask=None,
|
2019-05-30 14:30:15 +00:00
|
|
|
tags: typing.Dict[str, str]=None):
|
2019-06-21 09:33:59 +00:00
|
|
|
self.id = str(uuid.uuid4())
|
2019-02-23 21:33:04 +00:00
|
|
|
self.command = command
|
|
|
|
self._args = args
|
|
|
|
self.args = IRCArgs(args)
|
2019-06-03 12:30:39 +00:00
|
|
|
self.source = source
|
2019-06-16 08:52:25 +00:00
|
|
|
self.tags = tags or {} # type: typing.Dict[str, str]
|
2019-06-10 09:11:31 +00:00
|
|
|
self._valid = True
|
2019-06-10 09:45:02 +00:00
|
|
|
self._assured = False
|
2019-02-23 21:33:04 +00:00
|
|
|
|
2019-06-01 21:28:37 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return "ParsedLine(%s)" % self.__str__()
|
|
|
|
def __str__(self):
|
|
|
|
return self.format()
|
|
|
|
|
2019-06-10 09:11:31 +00:00
|
|
|
def valid(self) -> bool:
|
|
|
|
return self._valid
|
|
|
|
def invalidate(self):
|
|
|
|
self._valid = False
|
|
|
|
|
2019-06-10 09:45:02 +00:00
|
|
|
def assured(self) -> bool:
|
|
|
|
return self._assured
|
|
|
|
def assure(self):
|
|
|
|
self._assured = True
|
|
|
|
|
2019-06-16 08:52:25 +00:00
|
|
|
def add_tag(self, tag: str, value: str=None):
|
|
|
|
self.tags[tag] = value or ""
|
2019-06-18 14:18:44 +00:00
|
|
|
def has_tag(self, tag: str) -> bool:
|
|
|
|
return "tag" in self.tags
|
|
|
|
def get_tag(self, tag: str) -> typing.Optional[str]:
|
|
|
|
return self.tags[tag]
|
2019-06-16 08:52:25 +00:00
|
|
|
|
2019-02-23 21:33:04 +00:00
|
|
|
def _tag_str(self, tags: typing.Dict[str, str]) -> str:
|
2019-02-27 22:14:53 +00:00
|
|
|
tag_pieces = []
|
2019-02-23 21:33:04 +00:00
|
|
|
for tag, value in tags.items():
|
|
|
|
if value:
|
2019-10-27 10:19:00 +00:00
|
|
|
value_escaped = message_tag_escape(value)
|
2019-06-06 08:29:17 +00:00
|
|
|
tag_pieces.append("%s=%s" % (tag, value_escaped))
|
2019-02-27 22:14:53 +00:00
|
|
|
else:
|
|
|
|
tag_pieces.append(tag)
|
|
|
|
|
|
|
|
if tag_pieces:
|
2019-06-06 16:19:27 +00:00
|
|
|
return "@%s" % ";".join(tag_pieces)
|
2019-02-27 22:14:53 +00:00
|
|
|
return ""
|
2019-02-23 21:33:04 +00:00
|
|
|
|
2019-06-18 13:33:51 +00:00
|
|
|
def _format(self) -> typing.Tuple[str, str]:
|
2019-02-27 22:14:53 +00:00
|
|
|
pieces = []
|
2019-06-18 13:33:51 +00:00
|
|
|
tags = ""
|
2019-02-23 21:33:04 +00:00
|
|
|
if self.tags:
|
2019-06-18 13:33:51 +00:00
|
|
|
tags = self._tag_str(self.tags)
|
2019-02-23 21:33:04 +00:00
|
|
|
|
2019-06-03 12:30:39 +00:00
|
|
|
if self.source:
|
2019-07-28 19:55:54 +00:00
|
|
|
pieces.append(":%s" % str(self.source))
|
2019-02-10 18:43:04 +00:00
|
|
|
|
2019-02-27 22:14:53 +00:00
|
|
|
pieces.append(self.command.upper())
|
2019-02-10 18:43:04 +00:00
|
|
|
|
2019-02-23 21:33:04 +00:00
|
|
|
if self.args:
|
2019-02-27 22:14:53 +00:00
|
|
|
for i, arg in enumerate(self._args):
|
2019-03-08 23:23:09 +00:00
|
|
|
if arg and i == len(self._args)-1 and (
|
|
|
|
" " in arg or arg[0] == ":"):
|
2019-02-27 22:14:53 +00:00
|
|
|
pieces.append(":%s" % arg)
|
|
|
|
else:
|
|
|
|
pieces.append(arg)
|
2019-02-23 21:33:04 +00:00
|
|
|
|
2019-07-15 13:26:45 +00:00
|
|
|
return tags, " ".join(pieces).replace("\r", "")
|
2019-06-18 13:33:51 +00:00
|
|
|
def format(self) -> str:
|
|
|
|
tags, line = self._format()
|
2019-07-15 13:26:45 +00:00
|
|
|
line, _ = self._newline_truncate(line)
|
2019-06-18 13:33:51 +00:00
|
|
|
if tags:
|
|
|
|
return "%s %s" % (tags, line)
|
|
|
|
else:
|
|
|
|
return line
|
2019-02-23 21:33:04 +00:00
|
|
|
|
2019-07-15 13:26:45 +00:00
|
|
|
def _newline_truncate(self, line: str) -> typing.Tuple[str, str]:
|
|
|
|
line, sep, overflow = line.partition("\n")
|
2019-10-25 22:32:00 +00:00
|
|
|
return (line, overflow)
|
2019-06-19 09:34:42 +00:00
|
|
|
def _line_max(self, hostmask: str, margin: int) -> int:
|
|
|
|
return LINE_MAX-len((":%s " % hostmask).encode("utf8"))-margin
|
|
|
|
def truncate(self, hostmask: str, margin: int=0) -> typing.Tuple[str, str]:
|
2019-06-18 13:14:37 +00:00
|
|
|
valid_bytes = b""
|
|
|
|
valid_index = -1
|
|
|
|
|
2019-06-19 09:34:42 +00:00
|
|
|
line_max = self._line_max(hostmask, margin)
|
2019-06-18 13:14:37 +00:00
|
|
|
|
2019-06-18 13:33:51 +00:00
|
|
|
tags_formatted, line_formatted = self._format()
|
|
|
|
for i, char in enumerate(line_formatted):
|
2019-06-18 13:14:37 +00:00
|
|
|
encoded_char = char.encode("utf8")
|
2019-07-15 13:26:45 +00:00
|
|
|
if (len(valid_bytes)+len(encoded_char) > line_max
|
|
|
|
or encoded_char == b"\n"):
|
2019-06-18 13:14:37 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
valid_bytes += encoded_char
|
|
|
|
valid_index = i
|
|
|
|
valid_index += 1
|
|
|
|
|
2019-06-18 13:33:51 +00:00
|
|
|
valid = line_formatted[:valid_index]
|
|
|
|
if tags_formatted:
|
2019-06-18 17:14:04 +00:00
|
|
|
valid = "%s %s" % (tags_formatted, valid)
|
2019-06-18 13:33:51 +00:00
|
|
|
overflow = line_formatted[valid_index:]
|
2019-07-15 13:26:45 +00:00
|
|
|
if overflow and overflow[0] == "\n":
|
|
|
|
overflow = overflow[1:]
|
2019-06-18 13:33:51 +00:00
|
|
|
|
|
|
|
return valid, overflow
|
2019-06-18 13:14:37 +00:00
|
|
|
|
2019-10-27 10:19:00 +00:00
|
|
|
def parse_line(line: str) -> ParsedLine:
|
|
|
|
tags = {} # type: typing.Dict[str, typing.Any]
|
|
|
|
source = None # type: typing.Optional[Hostmask]
|
|
|
|
command = None
|
|
|
|
|
|
|
|
if line[0] == "@":
|
|
|
|
tags_prefix, line = line[1:].split(" ", 1)
|
|
|
|
|
|
|
|
for tag in filter(None, tags_prefix.split(";")):
|
|
|
|
tag, sep, value = tag.partition("=")
|
|
|
|
if value:
|
|
|
|
tags[tag] = message_tag_unescape(value)
|
|
|
|
else:
|
|
|
|
tags[tag] = None
|
|
|
|
|
|
|
|
line, trailing_separator, trailing_split = line.partition(" :")
|
|
|
|
|
|
|
|
trailing = None # type: typing.Optional[str]
|
|
|
|
if trailing_separator:
|
|
|
|
trailing = trailing_split
|
|
|
|
|
|
|
|
if line[0] == ":":
|
|
|
|
source_str, line = line[1:].split(" ", 1)
|
|
|
|
source = parse_hostmask(source_str)
|
|
|
|
|
|
|
|
command, sep, line = line.partition(" ")
|
|
|
|
args = [] # type: typing.List[str]
|
|
|
|
if line:
|
|
|
|
# this is so that `args` is empty if `line` is empty
|
|
|
|
args = line.split(" ")
|
|
|
|
|
|
|
|
if not trailing == None:
|
|
|
|
args.append(typing.cast(str, trailing))
|
|
|
|
return ParsedLine(command, args, source, tags)
|
|
|
|
|
2019-11-27 17:38:17 +00:00
|
|
|
def is_human(line: str):
|
|
|
|
return len(line) > 1 and line[0] == "/"
|
|
|
|
def parse_human(line: str) -> typing.Optional[ParsedLine]:
|
|
|
|
command, _, args = line[1:].partition(" ")
|
|
|
|
if command == "msg":
|
|
|
|
target, _, message = args.partition(" ")
|
|
|
|
return ParsedLine("PRIVMSG", [target, message])
|
|
|
|
return None
|
|
|
|
|
2019-02-24 08:24:58 +00:00
|
|
|
class SentLine(IRCObject.Object):
|
2019-06-26 10:01:09 +00:00
|
|
|
def __init__(self, events: "EventManager.Events",
|
2019-06-04 16:51:20 +00:00
|
|
|
send_time: datetime.datetime, hostmask: str, line: ParsedLine):
|
|
|
|
self.events = events
|
2019-02-23 21:33:04 +00:00
|
|
|
self.send_time = send_time
|
|
|
|
self._hostmask = hostmask
|
|
|
|
self.parsed_line = line
|
|
|
|
|
2019-02-10 14:18:33 +00:00
|
|
|
def __repr__(self) -> str:
|
2019-02-24 08:24:58 +00:00
|
|
|
return "IRCLine.SentLine(%s)" % self.__str__()
|
2019-02-10 14:18:33 +00:00
|
|
|
def __str__(self) -> str:
|
2019-06-18 13:14:37 +00:00
|
|
|
return self._for_wire()
|
2019-02-23 21:33:04 +00:00
|
|
|
|
2019-06-18 13:14:37 +00:00
|
|
|
def _for_wire(self) -> str:
|
|
|
|
return self.parsed_line.truncate(self._hostmask)[0]
|
2019-06-04 13:28:35 +00:00
|
|
|
def for_wire(self) -> bytes:
|
2019-06-18 13:14:37 +00:00
|
|
|
return b"%s\r\n" % self._for_wire().encode("utf8")
|
2019-10-27 10:19:00 +00:00
|
|
|
|
|
|
|
class IRCBatch(object):
|
|
|
|
def __init__(self, identifier: str, batch_type: str, args: typing.List[str],
|
|
|
|
tags: typing.Dict[str, str]=None, source: Hostmask=None):
|
|
|
|
self.identifier = identifier
|
|
|
|
self.type = batch_type
|
|
|
|
self.args = args
|
|
|
|
self.tags = tags or {}
|
|
|
|
self.source = source
|
|
|
|
self._lines = [] # type: typing.List[ParsedLine]
|
|
|
|
def add_line(self, line: ParsedLine):
|
|
|
|
self._lines.append(line)
|
|
|
|
def get_lines(self) -> typing.List[ParsedLine]:
|
|
|
|
return self._lines
|
|
|
|
|
|
|
|
class IRCSendBatch(IRCBatch):
|
|
|
|
def __init__(self, batch_type: str, args: typing.List[str],
|
|
|
|
tags: typing.Dict[str, str]=None):
|
|
|
|
IRCBatch.__init__(self, str(uuid.uuid4()), batch_type, args, tags)
|
|
|
|
def get_lines(self) -> typing.List[ParsedLine]:
|
|
|
|
lines = []
|
|
|
|
for line in self._lines:
|
|
|
|
line.add_tag("batch", self.identifier)
|
|
|
|
lines.append(line)
|
|
|
|
|
|
|
|
lines.insert(0, ParsedLine("BATCH",
|
|
|
|
["+%s" % self.identifier, self.type]))
|
|
|
|
lines.append(ParsedLine("BATCH", ["-%s" % self.identifier]))
|
|
|
|
return lines
|