return matching string from buffer.find() as most uses were redundantly regexing

This commit is contained in:
jesopo 2019-08-13 13:48:03 +01:00
parent 9a51ac18ea
commit 03c7e8f066
8 changed files with 32 additions and 26 deletions

View file

@ -7,7 +7,7 @@ import dns.resolver
URL_GEOIP = "http://ip-api.com/json/%s" URL_GEOIP = "http://ip-api.com/json/%s"
REGEX_IPv6 = r"(?:(?:[a-f0-9]{1,4}:){2,}|[a-f0-9:]*::)[a-f0-9:]*" 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_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): class DnsSetting(utils.Setting):
def parse(self, value: str) -> typing.Any: def parse(self, value: str) -> typing.Any:
@ -109,8 +109,7 @@ class Module(ModuleManager.BaseModule):
if not ip: if not ip:
line = event["target"].buffer.find(REGEX_IP) line = event["target"].buffer.find(REGEX_IP)
if line: if line:
match = REGEX_IP.search(line.message) ip = line.match
ip = match.group(1) or match.group(2)
if not ip: if not ip:
raise utils.EventError("No IP provided") raise utils.EventError("No IP provided")

View file

@ -54,12 +54,13 @@ class Module(ModuleManager.BaseModule):
for_user = event["user"].nickname if self._closest_setting(event, for_user = event["user"].nickname if self._closest_setting(event,
"sed-sender-only", False) else None "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) for_user=for_user, not_pattern=REGEX_SED)
if line: if match:
new_message = re.sub(pattern, replace, line.message, count) new_message = re.sub(pattern, replace, match.line.message,
if line.action: count)
prefix = "* %s" % line.sender if match.line.action:
prefix = "* %s" % match.line.sender
else: else:
prefix = "<%s>" % line.sender prefix = "<%s>" % match.line.sender
event["stdout"].write("%s %s" % (prefix, new_message)) event["stdout"].write("%s %s" % (prefix, new_message))

View file

@ -72,8 +72,7 @@ class Module(ModuleManager.BaseModule):
else: else:
url = target.buffer.find(utils.http.REGEX_URL) url = target.buffer.find(utils.http.REGEX_URL)
if url: if url:
url = re.search(utils.http.REGEX_URL, url.message).group(0) url = utils.http.url_sanitise(url.match)
url = utils.http.url_sanitise(url)
if not url: if not url:
raise utils.EventError("No URL provided/found.") raise utils.EventError("No URL provided/found.")
return url return url

View file

@ -30,8 +30,7 @@ class Module(ModuleManager.BaseModule):
else: else:
last_soundcloud = event["target"].buffer.find(REGEX_SOUNDCLOUD) last_soundcloud = event["target"].buffer.find(REGEX_SOUNDCLOUD)
if last_soundcloud: if last_soundcloud:
url = re.match(REGEX_SOUNDCLOUD, url = last_soundcloud.match
last_soundcloud.message).string
if not query and not url: if not query and not url:
raise utils.EventError("no search phrase provided") raise utils.EventError("no search phrase provided")

View file

@ -84,9 +84,9 @@ class Module(ModuleManager.BaseModule):
if len(event["args"]) > 0: if len(event["args"]) > 0:
url = event["args_split"][0] url = event["args_split"][0]
else: else:
url = event["target"].buffer.find(utils.http.REGEX_URL) match = event["target"].buffer.find(utils.http.REGEX_URL)
if url: if match:
url = re.search(utils.http.REGEX_URL, url.message).group(0) url = match.match
if not url: if not url:
raise utils.EventError("No URL provided/found.") raise utils.EventError("No URL provided/found.")

View file

@ -152,7 +152,8 @@ class Module(ModuleManager.BaseModule):
else: else:
target = event["target"].buffer.find(REGEX_TWITTERURL) target = event["target"].buffer.find(REGEX_TWITTERURL)
if target: if target:
target = target.message target = target.match
if target: if target:
url_match = re.search(REGEX_TWITTERURL, target) url_match = re.search(REGEX_TWITTERURL, target)
if url_match or target.isdigit(): if url_match or target.isdigit():

View file

@ -124,7 +124,7 @@ class Module(ModuleManager.BaseModule):
search = event["args"] search = event["args"]
else: else:
url = event["target"].buffer.find(REGEX_YOUTUBE) url = event["target"].buffer.find(REGEX_YOUTUBE)
url = url.message if url else None url = url.match if url else None
if not url: if not url:
safe_setting = event["target"].get_setting("youtube-safesearch", True) safe_setting = event["target"].get_setting("youtube-safesearch", True)

View file

@ -13,6 +13,11 @@ class BufferLine(object):
self.from_self = from_self self.from_self = from_self
self.method = method self.method = method
class BufferLineMatch(object):
def __init__(self, line: BufferLine, match: str):
self.line = line
self.match = match
class Buffer(object): class Buffer(object):
def __init__(self, bot: "IRCBot.Bot", server: "IRCServer.Server"): def __init__(self, bot: "IRCBot.Bot", server: "IRCServer.Server"):
self.bot = bot self.bot = bot
@ -42,7 +47,7 @@ class Buffer(object):
return line return line
return None return None
def find(self, pattern: typing.Union[str, typing.Pattern[str]], **kwargs def find(self, pattern: typing.Union[str, typing.Pattern[str]], **kwargs
) -> typing.Optional[BufferLine]: ) -> typing.Optional[BufferLineMatch]:
from_self = kwargs.get("from_self", True) from_self = kwargs.get("from_self", True)
for_user = kwargs.get("for_user", "") for_user = kwargs.get("for_user", "")
for_user = self.server.irc_lower(for_user) if for_user else None 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: for line in self._lines:
if line.from_self and not from_self: if line.from_self and not from_self:
continue continue
elif re.search(pattern, line.message): else:
match = re.search(pattern, line.message)
if match:
if not_pattern and re.search(not_pattern, line.message): if not_pattern and re.search(not_pattern, line.message):
continue continue
if for_user and not self.server.irc_lower(line.sender if for_user and not self.server.irc_lower(line.sender
) == for_user: ) == for_user:
continue continue
return line return BufferLineMatch(line, match.group(0))
return None return None
def find_from(self, nickname: str) -> typing.Optional[BufferLine]: def find_from(self, nickname: str) -> typing.Optional[BufferLine]: