diff --git a/find.py b/find.py index 1285bd611f..0b186f112c 100644 --- a/find.py +++ b/find.py @@ -12,6 +12,7 @@ """ import re +from willie.tools import Nick def setup(willie): willie.memory['find_lines'] = dict() @@ -26,11 +27,11 @@ def collectlines(willie, trigger): # Add a log for the channel and nick, if there isn't already one if trigger.sender not in willie.memory['find_lines']: willie.memory['find_lines'][trigger.sender] = dict() - if trigger.nick not in willie.memory['find_lines'][trigger.sender]: - willie.memory['find_lines'][trigger.sender][trigger.nick] = list() + if Nick(trigger.nick) not in willie.memory['find_lines'][trigger.sender]: + willie.memory['find_lines'][trigger.sender][Nick(trigger.nick)] = list() # Create a temporary list of the user's lines in a channel - templist = willie.memory['find_lines'][trigger.sender][trigger.nick] + templist = willie.memory['find_lines'][trigger.sender][Nick(trigger.nick)] line = trigger.group() if line.startswith("s/"): # Don't remember substitutions return @@ -42,37 +43,31 @@ def collectlines(willie, trigger): del templist[:-10] # Keep the log to 10 lines per person - willie.memory['find_lines'][trigger.sender][trigger.nick] = templist + willie.memory['find_lines'][trigger.sender][Nick(trigger.nick)] = templist collectlines.rule = r'.*' collectlines.priority = 'low' def findandreplace(willie, trigger): # Don't bother in PM - if not trigger.sender.startswith('#'): return - - rnick = trigger.group(1) or trigger.nick # Correcting other person vs self. + if not trigger.sender.startswith('#'): + return + + # Correcting other person vs self. + rnick = Nick(trigger.group(1) or trigger.nick) search_dict = willie.memory['find_lines'] # only do something if there is conversation to work with if trigger.sender not in search_dict: return - if rnick not in search_dict[trigger.sender]: + if Nick(rnick) not in search_dict[trigger.sender]: return - sep = trigger.group(2) - rest = trigger.group(3).split(sep) + #TODO rest[0] is find, rest[1] is replace. These should be made variables of + #their own at some point. + rest = [trigger.group(2), trigger.group(3)] me = False # /me command - flags = '' - - # Account for if extra flags are given (just g and i for now), or a search - # and substitution pattern aren't given. - if len(rest) < 2: - return - elif len(rest) > 2: - # Word characters immediately after the second separator - # are considered flags (only g and i now have meaning) - flags = re.match(r'\w*',rest[2], re.U).group(0) + flags = (trigger.group(4) or '') # If g flag is given, replace all. Otherwise, replace once. if 'g' in flags: @@ -120,8 +115,12 @@ def findandreplace(willie, trigger): willie.say(phrase) -# Matches optional whitespace + 's' + optional whitespace + separator character -findandreplace.rule = r'(?u)(?:([^\s:]+)[\s:])?\s*s\s*([^\s\w])(.*)' # May work for both this and "meant" (requires trigger.group(i+1)) +#Match nick, s/find/replace/flags. Flags and nick are optional, nick can be +#followed by comma or colon, anything after the first space after the third +#slash is ignored, you can escape slashes with backslashes, and if you want to +#search for an actual backslash followed by an actual slash, you're shit out of +#luck because this is the fucking regex of death as it is. +findandreplace.rule = r'(?:(\S+)[:,]\s+)?s/((?:[^/]|\\/)+)/((?:[^/]|\\/)+)(?:/(\S+))?' findandreplace.priority = 'high'