From db29965a33b0d8fdf9d33b809641b9ab7f639cfb Mon Sep 17 00:00:00 2001 From: Krytarik Raido Date: Sun, 13 Jun 2021 23:34:04 +0200 Subject: [PATCH] Encyclopedia: Rework message parsing. --- Encyclopedia/__init__.py | 2 +- Encyclopedia/plugin.py | 72 +++++++++++++++++++--------------------- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/Encyclopedia/__init__.py b/Encyclopedia/__init__.py index ad8c13b..eb608dc 100644 --- a/Encyclopedia/__init__.py +++ b/Encyclopedia/__init__.py @@ -24,7 +24,7 @@ import supybot import supybot.world as world from importlib import reload -__version__ = "3.6.0" +__version__ = "3.7.0" __author__ = supybot.Author("Krytarik Raido", "krytarik", "krytarik@gmail.com") __contributors__ = { supybot.Author("Dennis Kaarsemaker", "Seveas", "dennis@kaarsemaker.net"): ['Original Author'], diff --git a/Encyclopedia/plugin.py b/Encyclopedia/plugin.py index 5d84105..bcf1c04 100644 --- a/Encyclopedia/plugin.py +++ b/Encyclopedia/plugin.py @@ -449,7 +449,6 @@ class Encyclopedia(callbacks.Plugin): if text.split()[0].lower() in self.registryValue('ignores', channel): return - doChanMsg = True # Send message to channel display = None # Set display type edit = None if not channel: @@ -466,44 +465,43 @@ class Encyclopedia(callbacks.Plugin): return irc = callbacks.ReplyIrcProxy(irc, msg) + ret, retmsg = '', '' # Now switch between actions - ret, retmsg = '', '' - term = self.get_target(irc, msg.nick, text, target) - lower_term = term[0].lower() - - if re.match(r"please\s+see\s+((%s|(the\s+)?bot)(\s*'?s)?\s+\S+|\S+.*\s+\S+\s+(%s|(the\s+)?bot)\b)" % (re.escape(irc.nick), re.escape(irc.nick)), text, re.I): - doChanMsg = False - - if lower_term == "search": # Usage info for the !search command - ret = "Search factoids for term: !search " - target = term[1] - retmsg = term[2] - elif re.match(r"^seen\b", lower_term): # Some people expect a '!seen ' command - ret = "I have no seen command" - retmsg = "%s: " % msg.nick if term[2] else '' # Redirect back at the caller, rather than the target - elif lower_term.startswith("google "): # Some people expect a '!google ' command - ret = "I have no google command, use https://www.google.com/" - retmsg = "%s: " % msg.nick if term[2] else '' # Redirect back at the caller, rather than the target - elif re.match(r"^what'?s?\b", lower_term): # Try and catch people saying "ubottu: what is ...?" - ret = "I am only a bot, please don't think I'm intelligent :)" - retmsg = "%s: " % msg.nick if channel else '' - - # Lookup, search or edit? - elif lower_term.startswith('search '): - ret = self.search_factoid(text[7:].strip(), channel) - elif lower_term.startswith('delete '): - edit = 'delete' - elif re.match(r'^no[\s,]+[^|\s]+[^|]*?\s+is(\s+|\b)\S+', text, re.I) \ - or re.match(r'^[^|\s]+[^|]*?\s*(\s+is\s*|=~|~=)\s*\S+', text, re.I) \ - or lower_term.startswith(('forget ', 'unforget ')): - edit = 'edit' - elif re.match(r'^[^|\s]+[^|]*?\s+is(\s+|\b)\S+', text, re.I): - edit = 'add' - else: - if not display: - text, target, retmsg = term + if display: ret = self.factoid_lookup(text, channel, msg.nick, display) + else: + term = self.get_target(irc, msg.nick, text, target) + lower_term = term[0].lower() + + if lower_term == "search": # Usage info for the !search command + ret = "Search factoids for term: !search " + target = term[1] + retmsg = term[2] + elif re.match(r"^seen\b", lower_term): # Some people expect a '!seen ' command + ret = "I have no seen command" + retmsg = "%s: " % msg.nick if term[2] else '' # Redirect back at the caller, rather than the target + elif lower_term.startswith("google "): # Some people expect a '!google ' command + ret = "I have no google command, use https://www.google.com/" + retmsg = "%s: " % msg.nick if term[2] else '' # Redirect back at the caller, rather than the target + elif re.match(r"^what'?s?\b", lower_term): # Try and catch people saying "ubottu: what is ...?" + ret = "I am only a bot, please don't think I'm intelligent :)" + retmsg = "%s: " % msg.nick if channel else '' + + # Lookup, search or edit? + elif lower_term.startswith('search '): + ret = self.search_factoid(text[7:].strip(), channel) + elif lower_term.startswith('delete '): + edit = 'delete' + elif re.match(r'^no[\s,]+[^|\s]+[^|]*?\s+is(\s+|\b)\S+', text, re.I) \ + or re.match(r'^[^|\s]+[^|]*?\s*(\s+is\s*|=~|~=)\s*\S+', text, re.I) \ + or lower_term.startswith(('forget ', 'unforget ')): + edit = 'edit' + elif re.match(r'^[^|\s]+[^|]*?\s+is(\s+|\b)\S+', text, re.I): + edit = 'add' + else: + text, target, retmsg = term + ret = self.factoid_lookup(text, channel, msg.nick, display) if edit: check = getattr(self, 'factoid_%s_check' % edit)(text, channel, msg.nick) @@ -559,7 +557,7 @@ class Encyclopedia(callbacks.Plugin): if retmsg and checkUrl(retmsg): # !ops factoid called with a URL, most likely spam return - if doChanMsg and channel and not irc.isChannel(target) \ + if channel and not irc.isChannel(target) \ and target in irc.state.channels[channel].users: myqueue(irc, channel, "%s: Please see my private message" % target) myqueue(irc, target, retmsg + ret[0])