diff --git a/modules/ip_addresses.py b/modules/ip_addresses.py index 1f4fd6d7..07b1a463 100644 --- a/modules/ip_addresses.py +++ b/modules/ip_addresses.py @@ -7,7 +7,7 @@ import dns.resolver URL_GEOIP = "http://ip-api.com/json/%s" REGEX_IPv6 = r"(?:(?:[a-f0-9]{1,4}:){2,}|[a-f0-9:]*::)[a-f0-9:]*" REGEX_IPv4 = r"(?:\d{1,3}\.){3}\d{1,3}" -REGEX_IP = re.compile("(%s)|(%s)" % (REGEX_IPv4, REGEX_IPv6), re.I) +REGEX_IP = re.compile("%s|%s" % (REGEX_IPv4, REGEX_IPv6), re.I) class DnsSetting(utils.Setting): def parse(self, value: str) -> typing.Any: @@ -109,8 +109,7 @@ class Module(ModuleManager.BaseModule): if not ip: line = event["target"].buffer.find(REGEX_IP) if line: - match = REGEX_IP.search(line.message) - ip = match.group(1) or match.group(2) + ip = line.match if not ip: raise utils.EventError("No IP provided") diff --git a/modules/sed.py b/modules/sed.py index 0716d04d..c54c56fd 100644 --- a/modules/sed.py +++ b/modules/sed.py @@ -54,12 +54,13 @@ class Module(ModuleManager.BaseModule): for_user = event["user"].nickname if self._closest_setting(event, "sed-sender-only", False) else None - line = event["target"].buffer.find(pattern, from_self=False, + match = event["target"].buffer.find(pattern, from_self=False, for_user=for_user, not_pattern=REGEX_SED) - if line: - new_message = re.sub(pattern, replace, line.message, count) - if line.action: - prefix = "* %s" % line.sender + if match: + new_message = re.sub(pattern, replace, match.line.message, + count) + if match.line.action: + prefix = "* %s" % match.line.sender else: - prefix = "<%s>" % line.sender + prefix = "<%s>" % match.line.sender event["stdout"].write("%s %s" % (prefix, new_message)) diff --git a/modules/shorturl.py b/modules/shorturl.py index 668ac0a4..77a9a352 100644 --- a/modules/shorturl.py +++ b/modules/shorturl.py @@ -72,8 +72,7 @@ class Module(ModuleManager.BaseModule): else: url = target.buffer.find(utils.http.REGEX_URL) if url: - url = re.search(utils.http.REGEX_URL, url.message).group(0) - url = utils.http.url_sanitise(url) + url = utils.http.url_sanitise(url.match) if not url: raise utils.EventError("No URL provided/found.") return url diff --git a/modules/soundcloud.py b/modules/soundcloud.py index 5b24b28e..a91faa10 100644 --- a/modules/soundcloud.py +++ b/modules/soundcloud.py @@ -30,8 +30,7 @@ class Module(ModuleManager.BaseModule): else: last_soundcloud = event["target"].buffer.find(REGEX_SOUNDCLOUD) if last_soundcloud: - url = re.match(REGEX_SOUNDCLOUD, - last_soundcloud.message).string + url = last_soundcloud.match if not query and not url: raise utils.EventError("no search phrase provided") diff --git a/modules/title.py b/modules/title.py index 703ea476..9308dddc 100644 --- a/modules/title.py +++ b/modules/title.py @@ -84,9 +84,9 @@ class Module(ModuleManager.BaseModule): if len(event["args"]) > 0: url = event["args_split"][0] else: - url = event["target"].buffer.find(utils.http.REGEX_URL) - if url: - url = re.search(utils.http.REGEX_URL, url.message).group(0) + match = event["target"].buffer.find(utils.http.REGEX_URL) + if match: + url = match.match if not url: raise utils.EventError("No URL provided/found.") diff --git a/modules/tweets/__init__.py b/modules/tweets/__init__.py index 9aaaab17..c07cd739 100644 --- a/modules/tweets/__init__.py +++ b/modules/tweets/__init__.py @@ -152,7 +152,8 @@ class Module(ModuleManager.BaseModule): else: target = event["target"].buffer.find(REGEX_TWITTERURL) if target: - target = target.message + target = target.match + if target: url_match = re.search(REGEX_TWITTERURL, target) if url_match or target.isdigit(): diff --git a/modules/youtube.py b/modules/youtube.py index 1746c7e1..8675ff78 100644 --- a/modules/youtube.py +++ b/modules/youtube.py @@ -124,7 +124,7 @@ class Module(ModuleManager.BaseModule): search = event["args"] else: url = event["target"].buffer.find(REGEX_YOUTUBE) - url = url.message if url else None + url = url.match if url else None if not url: safe_setting = event["target"].get_setting("youtube-safesearch", True) diff --git a/src/IRCBuffer.py b/src/IRCBuffer.py index 3dccb07b..7f309f4d 100644 --- a/src/IRCBuffer.py +++ b/src/IRCBuffer.py @@ -13,6 +13,11 @@ class BufferLine(object): self.from_self = from_self self.method = method +class BufferLineMatch(object): + def __init__(self, line: BufferLine, match: str): + self.line = line + self.match = match + class Buffer(object): def __init__(self, bot: "IRCBot.Bot", server: "IRCServer.Server"): self.bot = bot @@ -42,7 +47,7 @@ class Buffer(object): return line return None def find(self, pattern: typing.Union[str, typing.Pattern[str]], **kwargs - ) -> typing.Optional[BufferLine]: + ) -> typing.Optional[BufferLineMatch]: from_self = kwargs.get("from_self", True) for_user = kwargs.get("for_user", "") for_user = self.server.irc_lower(for_user) if for_user else None @@ -50,13 +55,15 @@ class Buffer(object): for line in self._lines: if line.from_self and not from_self: continue - elif re.search(pattern, line.message): - if not_pattern and re.search(not_pattern, line.message): - continue - if for_user and not self.server.irc_lower(line.sender - ) == for_user: - continue - return line + else: + match = re.search(pattern, line.message) + if match: + if not_pattern and re.search(not_pattern, line.message): + continue + if for_user and not self.server.irc_lower(line.sender + ) == for_user: + continue + return BufferLineMatch(line, match.group(0)) return None def find_from(self, nickname: str) -> typing.Optional[BufferLine]: