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"
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")

View file

@ -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))

View file

@ -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

View file

@ -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")

View file

@ -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.")

View file

@ -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():

View file

@ -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)

View file

@ -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]: