2018-11-05 17:44:21 +00:00
|
|
|
import json, string, re, typing
|
2018-11-13 16:02:26 +00:00
|
|
|
from src import utils
|
2018-10-03 12:22:37 +00:00
|
|
|
|
|
|
|
ASCII_UPPER = string.ascii_uppercase
|
|
|
|
ASCII_LOWER = string.ascii_lowercase
|
|
|
|
STRICT_RFC1459_UPPER = ASCII_UPPER+r'\[]'
|
|
|
|
STRICT_RFC1459_LOWER = ASCII_LOWER+r'|{}'
|
|
|
|
RFC1459_UPPER = STRICT_RFC1459_UPPER+"^"
|
|
|
|
RFC1459_LOWER = STRICT_RFC1459_LOWER+"~"
|
|
|
|
|
|
|
|
# case mapping lowercase/uppcase logic
|
2018-10-30 14:58:48 +00:00
|
|
|
def _multi_replace(s: str,
|
|
|
|
chars1: typing.Iterable[str],
|
|
|
|
chars2: typing.Iterable[str]) -> str:
|
2018-10-03 12:22:37 +00:00
|
|
|
for char1, char2 in zip(chars1, chars2):
|
|
|
|
s = s.replace(char1, char2)
|
|
|
|
return s
|
2018-10-30 14:58:48 +00:00
|
|
|
def lower(case_mapping: str, s: str) -> str:
|
|
|
|
if case_mapping == "ascii":
|
2018-10-03 12:22:37 +00:00
|
|
|
return _multi_replace(s, ASCII_UPPER, ASCII_LOWER)
|
2018-10-30 14:58:48 +00:00
|
|
|
elif case_mapping == "rfc1459":
|
2018-10-03 12:22:37 +00:00
|
|
|
return _multi_replace(s, RFC1459_UPPER, RFC1459_LOWER)
|
2018-10-30 14:58:48 +00:00
|
|
|
elif case_mapping == "strict-rfc1459":
|
2018-10-03 12:22:37 +00:00
|
|
|
return _multi_replace(s, STRICT_RFC1459_UPPER, STRICT_RFC1459_LOWER)
|
|
|
|
else:
|
2018-10-30 14:58:48 +00:00
|
|
|
raise ValueError("unknown casemapping '%s'" % case_mapping)
|
2018-10-03 12:22:37 +00:00
|
|
|
|
|
|
|
# compare a string while respecting case mapping
|
2018-10-30 14:58:48 +00:00
|
|
|
def equals(case_mapping: str, s1: str, s2: str) -> bool:
|
|
|
|
return lower(case_mapping, s1) == lower(case_mapping, s2)
|
2018-10-03 12:22:37 +00:00
|
|
|
|
|
|
|
class IRCHostmask(object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, nickname: str, username: str, hostname: str,
|
|
|
|
hostmask: str):
|
2018-10-03 12:22:37 +00:00
|
|
|
self.nickname = nickname
|
|
|
|
self.username = username
|
|
|
|
self.hostname = hostname
|
|
|
|
self.hostmask = hostmask
|
|
|
|
def __repr__(self):
|
|
|
|
return "IRCHostmask(%s)" % self.__str__()
|
|
|
|
def __str__(self):
|
|
|
|
return self.hostmask
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def seperate_hostmask(hostmask: str) -> IRCHostmask:
|
2018-10-03 12:22:37 +00:00
|
|
|
nickname, _, username = hostmask.partition("!")
|
|
|
|
username, _, hostname = username.partition("@")
|
|
|
|
return IRCHostmask(nickname, username, hostname, hostmask)
|
|
|
|
|
2018-11-04 15:35:52 +00:00
|
|
|
class IRCArgs(object):
|
|
|
|
def __init__(self, args: typing.List[str]):
|
|
|
|
self._args = args
|
2018-11-04 17:06:50 +00:00
|
|
|
|
2018-11-04 15:35:52 +00:00
|
|
|
def get(self, index: int) -> typing.Optional[str]:
|
|
|
|
if len(self._args) > index:
|
|
|
|
return self._args[index]
|
|
|
|
return None
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-11-05 10:03:20 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return "IRCArgs(%s)" % self._args
|
2018-11-04 17:06:50 +00:00
|
|
|
def __len__(self) -> int:
|
|
|
|
return len(self._args)
|
|
|
|
def __getitem__(self, index) -> str:
|
|
|
|
return self._args[index]
|
|
|
|
|
|
|
|
|
2018-11-04 17:03:01 +00:00
|
|
|
class IRCLine(object):
|
2018-11-20 17:23:14 +00:00
|
|
|
def __init__(self, tags: dict, prefix: typing.Optional[IRCHostmask],
|
|
|
|
command: str, args: IRCArgs, has_arbitrary: bool):
|
2018-11-04 17:03:01 +00:00
|
|
|
self.tags = tags
|
|
|
|
self.prefix = prefix
|
|
|
|
self.command = command
|
|
|
|
self.args = args
|
2018-11-08 10:36:40 +00:00
|
|
|
self.has_arbitrary = has_arbitrary
|
2018-11-04 17:03:01 +00:00
|
|
|
|
2018-11-08 11:37:23 +00:00
|
|
|
MESSAGE_TAG_ESCAPED = [r"\:", r"\s", r"\\", r"\r", r"\n"]
|
|
|
|
MESSAGE_TAG_UNESCAPED = [";", " ", "\\", "\r", "\n"]
|
|
|
|
def message_tag_escape(s):
|
|
|
|
return _multi_replace(s, MESSAGE_TAG_UNESCAPED, MESSAGE_TAG_ESCAPED)
|
|
|
|
def message_tag_unescape(s):
|
|
|
|
return _multi_replace(s, MESSAGE_TAG_ESCAPED, MESSAGE_TAG_UNESCAPED)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def parse_line(line: str) -> IRCLine:
|
2018-12-02 09:56:57 +00:00
|
|
|
tags = {} # type: typing.Dict[str, typing.Any]
|
2018-11-20 17:23:14 +00:00
|
|
|
prefix = None # type: typing.Optional[IRCHostmask]
|
2018-10-03 12:22:37 +00:00
|
|
|
command = None
|
|
|
|
|
|
|
|
if line[0] == "@":
|
2018-11-08 11:13:24 +00:00
|
|
|
tags_prefix, line = line[1:].split(" ", 1)
|
2018-11-08 11:37:23 +00:00
|
|
|
|
2018-11-08 11:13:24 +00:00
|
|
|
if tags_prefix[0] == "{":
|
2018-11-08 11:43:32 +00:00
|
|
|
tags_prefix = message_tag_unescape(tags_prefix)
|
2018-11-08 11:13:24 +00:00
|
|
|
tags = json.loads(tags_prefix)
|
2018-11-05 15:15:08 +00:00
|
|
|
else:
|
2018-11-08 11:52:53 +00:00
|
|
|
for tag in filter(None, tags_prefix.split(";")):
|
2018-11-05 15:15:08 +00:00
|
|
|
tag, sep, value = tag.partition("=")
|
|
|
|
if sep:
|
2018-11-08 11:52:53 +00:00
|
|
|
tags[tag] = message_tag_unescape(value)
|
2018-11-05 15:15:08 +00:00
|
|
|
else:
|
|
|
|
tags[tag] = None
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-11-08 10:36:40 +00:00
|
|
|
line, arb_sep, arbitrary_split = line.partition(" :")
|
|
|
|
has_arbitrary = bool(arb_sep)
|
|
|
|
arbitrary = None # type: typing.Optional[str]
|
|
|
|
if has_arbitrary:
|
|
|
|
arbitrary = arbitrary_split
|
2018-10-03 12:22:37 +00:00
|
|
|
|
|
|
|
if line[0] == ":":
|
2018-10-30 17:49:35 +00:00
|
|
|
prefix_str, line = line[1:].split(" ", 1)
|
2018-11-13 15:04:11 +00:00
|
|
|
prefix = seperate_hostmask(prefix_str)
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-11-04 17:23:34 +00:00
|
|
|
command, sep, line = line.partition(" ")
|
2018-12-03 18:25:57 +00:00
|
|
|
args = [] # type: typing.List[str]
|
|
|
|
if line:
|
|
|
|
# this is so that `args` is empty if `line` is empty
|
|
|
|
args = line.split(" ")
|
2018-11-04 17:23:34 +00:00
|
|
|
|
2018-11-04 15:35:52 +00:00
|
|
|
if arbitrary:
|
|
|
|
args.append(arbitrary)
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-11-08 10:36:40 +00:00
|
|
|
return IRCLine(tags, prefix, command, IRCArgs(args), has_arbitrary)
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-11-13 17:23:28 +00:00
|
|
|
|
|
|
|
REGEX_COLOR = re.compile("%s(?:(\d{1,2})(?:,(\d{1,2}))?)?" % utils.consts.COLOR)
|
2018-11-13 16:02:26 +00:00
|
|
|
|
|
|
|
def color(s: str, foreground: utils.consts.IRCColor,
|
|
|
|
background: utils.consts.IRCColor=None) -> str:
|
|
|
|
foreground_s = str(foreground.irc).zfill(2)
|
|
|
|
background_s = ""
|
2018-10-03 12:22:37 +00:00
|
|
|
if background:
|
2018-11-13 16:02:26 +00:00
|
|
|
background_s = ",%s" % str(background.irc).zfill(2)
|
|
|
|
|
|
|
|
return "%s%s%s%s%s" % (utils.consts.COLOR, foreground_s, background_s, s,
|
|
|
|
utils.consts.COLOR)
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def bold(s: str) -> str:
|
2018-11-13 16:02:26 +00:00
|
|
|
return "%s%s%s" % (utils.consts.BOLD, s, utils.consts.BOLD)
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def underline(s: str) -> str:
|
2018-11-13 16:02:26 +00:00
|
|
|
return "%s%s%s" % (utils.consts.UNDERLINE, s, utils.consts.UNDERLINE)
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def strip_font(s: str) -> str:
|
2018-11-13 16:02:26 +00:00
|
|
|
s = s.replace(utils.consts.BOLD, "")
|
|
|
|
s = s.replace(utils.consts.ITALIC, "")
|
2018-10-03 12:22:37 +00:00
|
|
|
s = REGEX_COLOR.sub("", s)
|
2018-11-13 16:02:26 +00:00
|
|
|
s = s.replace(utils.consts.COLOR, "")
|
2018-10-03 12:22:37 +00:00
|
|
|
return s
|
|
|
|
|
2018-11-14 12:12:32 +00:00
|
|
|
FORMAT_TOKENS = [
|
|
|
|
utils.consts.BOLD,
|
2018-11-30 16:30:47 +00:00
|
|
|
utils.consts.RESET,
|
|
|
|
utils.consts.UNDERLINE
|
2018-11-14 12:12:32 +00:00
|
|
|
]
|
2019-01-13 21:56:36 +00:00
|
|
|
FORMAT_STRIP = [
|
|
|
|
"\x08" # backspace
|
|
|
|
]
|
|
|
|
def _format_tokens(s: str) -> typing.List[str]:
|
2018-11-13 17:23:28 +00:00
|
|
|
is_color = False
|
|
|
|
foreground = ""
|
|
|
|
background = ""
|
2018-11-25 13:21:15 +00:00
|
|
|
is_background = False
|
2018-11-14 10:28:38 +00:00
|
|
|
matches = [] # type: typing.List[str]
|
2018-11-13 17:23:28 +00:00
|
|
|
|
|
|
|
for char in s:
|
2018-11-14 11:44:56 +00:00
|
|
|
if is_color:
|
2019-01-13 01:54:02 +00:00
|
|
|
can_add = False
|
|
|
|
current_color = background if is_background else foreground
|
|
|
|
if char.isdigit() and len(current_color) < 2:
|
2018-11-26 17:23:11 +00:00
|
|
|
if current_color:
|
2018-12-12 11:49:42 +00:00
|
|
|
next_color = int(current_color + char)
|
2019-01-16 11:18:51 +00:00
|
|
|
can_add = next_color <= 99
|
2019-01-13 01:54:02 +00:00
|
|
|
else:
|
|
|
|
can_add = True
|
2018-11-25 13:21:15 +00:00
|
|
|
|
2018-11-26 17:23:11 +00:00
|
|
|
if can_add:
|
2018-11-25 13:21:15 +00:00
|
|
|
if is_background:
|
2018-11-13 17:23:28 +00:00
|
|
|
background += char
|
|
|
|
else:
|
|
|
|
foreground += char
|
2018-11-14 11:44:56 +00:00
|
|
|
continue
|
2018-11-25 13:21:15 +00:00
|
|
|
elif char == "," and not is_background:
|
|
|
|
is_background = True
|
2018-11-14 11:44:56 +00:00
|
|
|
continue
|
2018-11-13 17:23:28 +00:00
|
|
|
else:
|
2018-11-14 22:35:13 +00:00
|
|
|
color = foreground
|
2018-11-25 13:21:15 +00:00
|
|
|
if background:
|
|
|
|
color += ","+background
|
2018-11-14 22:35:13 +00:00
|
|
|
|
2018-11-14 22:50:04 +00:00
|
|
|
matches.append("\x03%s" % color)
|
2018-11-13 17:23:28 +00:00
|
|
|
is_color = False
|
|
|
|
foreground = ""
|
|
|
|
background = ""
|
2018-11-25 13:21:15 +00:00
|
|
|
is_background = False
|
2018-11-14 11:44:56 +00:00
|
|
|
|
|
|
|
if char == utils.consts.COLOR:
|
|
|
|
if is_color:
|
|
|
|
matches.append(char)
|
|
|
|
else:
|
|
|
|
is_color = True
|
2018-11-14 12:12:32 +00:00
|
|
|
elif char in FORMAT_TOKENS:
|
2018-11-14 11:44:56 +00:00
|
|
|
matches.append(char)
|
2019-01-13 21:56:36 +00:00
|
|
|
elif char in FORMAT_STRIP:
|
|
|
|
matches.append(char)
|
2018-11-13 17:23:28 +00:00
|
|
|
return matches
|
|
|
|
|
2018-11-14 22:07:34 +00:00
|
|
|
def _color_match(code: typing.Optional[str], foreground: bool) -> str:
|
|
|
|
if not code:
|
|
|
|
return ""
|
|
|
|
color = utils.consts.COLOR_CODES[int(code)]
|
2019-01-16 11:18:51 +00:00
|
|
|
return color.to_ansi(not foreground)
|
2018-11-14 22:07:34 +00:00
|
|
|
|
2019-01-13 21:56:36 +00:00
|
|
|
def parse_format(s: str) -> str:
|
2018-11-14 22:07:34 +00:00
|
|
|
has_foreground = False
|
|
|
|
has_background = False
|
2018-11-13 17:23:28 +00:00
|
|
|
bold = False
|
2018-11-30 16:32:18 +00:00
|
|
|
underline = False
|
2018-11-13 17:23:28 +00:00
|
|
|
|
2019-01-13 21:56:36 +00:00
|
|
|
for token in _format_tokens(s):
|
2018-11-13 17:23:28 +00:00
|
|
|
replace = ""
|
|
|
|
type = token[0]
|
|
|
|
|
|
|
|
if type == utils.consts.COLOR:
|
|
|
|
match = REGEX_COLOR.match(token)
|
2018-11-14 22:07:34 +00:00
|
|
|
|
|
|
|
if match and (match.group(1) or match.group(2)):
|
|
|
|
foreground = _color_match(match.group(1), True)
|
|
|
|
background = _color_match(match.group(2), False)
|
|
|
|
|
|
|
|
if foreground:
|
2019-01-16 11:24:47 +00:00
|
|
|
replace += foreground
|
2018-11-14 22:07:34 +00:00
|
|
|
has_foreground = True
|
|
|
|
if background:
|
2019-01-16 11:24:47 +00:00
|
|
|
replace += background
|
2018-11-14 22:07:34 +00:00
|
|
|
has_background = True
|
2018-11-13 17:23:28 +00:00
|
|
|
else:
|
2018-11-14 22:07:34 +00:00
|
|
|
if has_foreground:
|
2018-11-17 12:48:56 +00:00
|
|
|
has_foreground = False
|
2018-11-14 22:07:34 +00:00
|
|
|
replace += utils.consts.ANSI_FOREGROUND_RESET
|
|
|
|
if has_background:
|
2018-11-17 12:48:56 +00:00
|
|
|
has_background = False
|
2018-11-14 22:07:34 +00:00
|
|
|
replace += utils.consts.ANSI_BACKGROUND_RESET
|
2018-11-13 17:23:28 +00:00
|
|
|
elif type == utils.consts.BOLD:
|
|
|
|
if bold:
|
2018-11-14 22:07:34 +00:00
|
|
|
replace += utils.consts.ANSI_BOLD_RESET
|
2018-11-13 18:05:25 +00:00
|
|
|
else:
|
2018-11-13 17:23:28 +00:00
|
|
|
replace += utils.consts.ANSI_BOLD
|
|
|
|
bold = not bold
|
2018-11-14 12:12:32 +00:00
|
|
|
elif type == utils.consts.RESET:
|
2018-11-14 13:16:11 +00:00
|
|
|
replace += utils.consts.ANSI_RESET
|
2018-11-30 16:30:47 +00:00
|
|
|
elif type == utils.consts.UNDERLINE:
|
|
|
|
if underline:
|
|
|
|
replace += utils.consts.ANSI_UNDERLINE_RESET
|
|
|
|
else:
|
|
|
|
replace += utils.consts.ANSI_UNDERLINE
|
|
|
|
underline = not underline
|
2019-01-13 21:56:36 +00:00
|
|
|
elif type in FORMAT_STRIP:
|
|
|
|
replace = ""
|
2018-11-13 17:23:28 +00:00
|
|
|
|
|
|
|
s = s.replace(token, replace, 1)
|
|
|
|
|
|
|
|
return s + utils.consts.ANSI_RESET
|
|
|
|
|
2018-11-05 18:23:02 +00:00
|
|
|
OPT_STR = typing.Optional[str]
|
|
|
|
class IRCConnectionParameters(object):
|
|
|
|
def __init__(self, id: int, alias: OPT_STR, hostname: str, port: int,
|
2018-11-05 18:30:14 +00:00
|
|
|
password: OPT_STR, tls: bool, ipv4: bool, bindhost: OPT_STR,
|
2018-11-08 17:09:07 +00:00
|
|
|
nickname: str, username: OPT_STR, realname: OPT_STR,
|
|
|
|
args: typing.Dict[str, str]={}):
|
2018-11-05 18:23:02 +00:00
|
|
|
self.id = id
|
|
|
|
self.alias = alias
|
|
|
|
self.hostname = hostname
|
|
|
|
self.port = port
|
|
|
|
self.tls = tls
|
|
|
|
self.ipv4 = ipv4
|
|
|
|
self.bindhost = bindhost
|
|
|
|
self.password = password
|
|
|
|
self.nickname = nickname
|
|
|
|
self.username = username
|
|
|
|
self.realname = realname
|
2018-11-08 17:09:07 +00:00
|
|
|
self.args = args
|
2018-11-14 21:28:27 +00:00
|
|
|
|
|
|
|
class CTCPMessage(object):
|
|
|
|
def __init__(self, command: str, message: str):
|
|
|
|
self.command = command
|
|
|
|
self.message = message
|
|
|
|
def parse_ctcp(s: str) -> typing.Optional[CTCPMessage]:
|
|
|
|
ctcp = s.startswith("\x01")
|
|
|
|
if s.startswith("\x01"):
|
|
|
|
ctcp_command, sep, ctcp_message = s[1:].partition(" ")
|
2018-11-14 22:10:45 +00:00
|
|
|
if ctcp_message.endswith("\x01"):
|
2018-11-14 22:13:31 +00:00
|
|
|
ctcp_message = ctcp_message[:-1]
|
2018-11-14 21:28:27 +00:00
|
|
|
return CTCPMessage(ctcp_command, ctcp_message)
|
|
|
|
|
|
|
|
return None
|