2018-10-30 14:58:48 +00:00
|
|
|
import string, re, typing
|
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+"~"
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def remove_colon(s: str) -> str:
|
2018-10-03 12:22:37 +00:00
|
|
|
if s.startswith(":"):
|
|
|
|
s = s[1:]
|
|
|
|
return s
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
MULTI_REPLACE_ITERABLE = typing.Iterable[str]
|
2018-10-03 12:22:37 +00:00
|
|
|
# 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
|
|
|
hostmask = remove_colon(hostmask)
|
|
|
|
nickname, _, username = hostmask.partition("!")
|
|
|
|
username, _, hostname = username.partition("@")
|
|
|
|
return IRCHostmask(nickname, username, hostname, hostmask)
|
|
|
|
|
|
|
|
class IRCLine(object):
|
2018-10-30 17:49:35 +00:00
|
|
|
def __init__(self, tags: dict, prefix: typing.Optional[str], command: str,
|
2018-11-04 15:35:52 +00:00
|
|
|
args: IRCArgs,
|
2018-10-03 12:22:37 +00:00
|
|
|
self.tags = tags
|
|
|
|
self.prefix = prefix
|
|
|
|
self.command = command
|
|
|
|
self.args = args
|
2018-11-04 15:35:52 +00:00
|
|
|
|
|
|
|
class IRCArgs(object):
|
|
|
|
def __init__(self, args: typing.List[str]):
|
|
|
|
self._args = args
|
|
|
|
def __getitem__(self, index) -> str:
|
|
|
|
return self_args[index]
|
|
|
|
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-10-30 14:58:48 +00:00
|
|
|
def parse_line(line: str) -> IRCLine:
|
2018-10-03 12:22:37 +00:00
|
|
|
tags = {}
|
2018-10-30 17:49:35 +00:00
|
|
|
prefix = typing.Optional[IRCHostmask]
|
2018-10-03 12:22:37 +00:00
|
|
|
command = None
|
|
|
|
|
|
|
|
if line[0] == "@":
|
|
|
|
tags_prefix, line = line[1:].split(" ", 1)
|
|
|
|
for tag in filter(None, tags_prefix.split(";")):
|
|
|
|
tag, _, value = tag.partition("=")
|
|
|
|
tags[tag] = value
|
|
|
|
|
2018-10-30 17:49:35 +00:00
|
|
|
line, _, arbitrary_split = line.partition(" :")
|
|
|
|
arbitrary = arbitrary_split or None
|
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)
|
|
|
|
prefix = seperate_hostmask(prefix_str)
|
2018-10-03 12:22:37 +00:00
|
|
|
command, _, line = line.partition(" ")
|
|
|
|
|
|
|
|
args = line.split(" ")
|
2018-11-04 15:35:52 +00:00
|
|
|
if arbitrary:
|
|
|
|
args.append(arbitrary)
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2018-11-04 15:35:52 +00:00
|
|
|
return IRCLine(tags, prefix, command, IRCArgs(args))
|
2018-10-03 12:22:37 +00:00
|
|
|
|
|
|
|
COLOR_WHITE, COLOR_BLACK, COLOR_BLUE, COLOR_GREEN = 0, 1, 2, 3
|
|
|
|
COLOR_RED, COLOR_BROWN, COLOR_PURPLE, COLOR_ORANGE = 4, 5, 6, 7
|
|
|
|
COLOR_YELLOW, COLOR_LIGHTGREEN, COLOR_CYAN, COLOR_LIGHTCYAN = (8, 9,
|
|
|
|
10, 11)
|
|
|
|
COLOR_LIGHTBLUE, COLOR_PINK, COLOR_GREY, COLOR_LIGHTGREY = (12, 13,
|
|
|
|
14, 15)
|
|
|
|
FONT_BOLD, FONT_ITALIC, FONT_UNDERLINE, FONT_INVERT = ("\x02", "\x1D",
|
|
|
|
"\x1F", "\x16")
|
|
|
|
FONT_COLOR, FONT_RESET = "\x03", "\x0F"
|
|
|
|
REGEX_COLOR = re.compile("%s\d\d(?:,\d\d)?" % FONT_COLOR)
|
|
|
|
|
2018-11-02 13:14:42 +00:00
|
|
|
def color(s: str, foreground: int, background: int=None) -> str:
|
2018-10-03 12:22:37 +00:00
|
|
|
foreground = str(foreground).zfill(2)
|
|
|
|
if background:
|
|
|
|
background = str(background).zfill(2)
|
|
|
|
return "%s%s%s%s%s" % (FONT_COLOR, foreground,
|
|
|
|
"" if not background else ",%s" % background, s, FONT_COLOR)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def bold(s: str) -> str:
|
2018-10-03 12:22:37 +00:00
|
|
|
return "%s%s%s" % (FONT_BOLD, s, FONT_BOLD)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def underline(s: str) -> str:
|
2018-10-03 12:22:37 +00:00
|
|
|
return "%s%s%s" % (FONT_UNDERLINE, s, FONT_UNDERLINE)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def strip_font(s: str) -> str:
|
2018-10-03 12:22:37 +00:00
|
|
|
s = s.replace(FONT_BOLD, "")
|
|
|
|
s = s.replace(FONT_ITALIC, "")
|
|
|
|
s = REGEX_COLOR.sub("", s)
|
|
|
|
s = s.replace(FONT_COLOR, "")
|
|
|
|
return s
|
|
|
|
|