Skip to content

Commit

Permalink
[find] Fix the absolutely trivial issue with certain uses of s/
Browse files Browse the repository at this point in the history
See issue sopel-irc#188
  • Loading branch information
embolalia committed Feb 28, 2013
1 parent 10fea68 commit affd175
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions find.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import re
from willie.tools import Nick

def setup(willie):
willie.memory['find_lines'] = dict()
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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'


0 comments on commit affd175

Please sign in to comment.