Skip to content

Commit

Permalink
Make sed smarter.
Browse files Browse the repository at this point in the history
Ignore other users if 'g' not specified, make it complain when it can't
find a match, and make it ignore other invocations of sed.

TODO: should probably add a flag to make it unignore sed invocations.
  • Loading branch information
mythmon committed Jul 17, 2011
1 parent f07a353 commit e8c9129
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
17 changes: 9 additions & 8 deletions hamper/commander.py
Expand Up @@ -48,11 +48,6 @@ def privmsg(self, user, channel, msg):
'channel': channel,
}

key = channel if channel else user
if not key in self.factory.history:
self.factory.history[key] = deque(maxlen=100)
self.factory.history[key].append(comm)

matchedPlugins = []
for cmd in self.factory.commands:
match = cmd.regex.match(msg)
Expand All @@ -63,11 +58,17 @@ def privmsg(self, user, channel, msg):
matchedPlugins.sort(key=lambda x: x[1].priority, reverse=True)

for match, cmd in matchedPlugins:
comm.update({'groups': match.groups()})
if not cmd(self, comm):
proc_comm = comm.copy()
proc_comm.update({'groups': match.groups()})
if not cmd(self, proc_comm):
# The plugin asked us to not run any more.
break

key = channel if channel else user
if not key in self.factory.history:
self.factory.history[key] = deque(maxlen=100)
self.factory.history[key].append(comm)

def connectionLost(self, reason):
reactor.stop()

Expand Down Expand Up @@ -95,7 +96,7 @@ def clientConnectionFailed(self, connector, reason):
@classmethod
def registerCommand(cls, Command):
"""Register a command. To be used as a decorator."""
options = re.I if not Command.caseSensitive else None
options = re.I if not Command.caseSensitive else 0
Command.regex = re.compile(Command.regex, options)

cls.commands.add(Command())
Expand Down
21 changes: 17 additions & 4 deletions hamper/commands.py
Expand Up @@ -64,18 +64,31 @@ class Sed(Command):
onlyDirected = False
priority = -1

def __call__(self, commander, options):
usr_regex = re.compile(options['groups'][0])
usr_replace = options['groups'][1]
def __call__(self, commander, opts):
regex_opts = re.I if 'i' in opts['groups'][2] else 0
usr_regex = re.compile(opts['groups'][0], regex_opts)
usr_replace = opts['groups'][1]

key = opts['channel'] if opts['channel'] else opts['user']

key = options['channel'] if options['channel'] else options['user']
if key not in commander.factory.history:
commander.say('Who are you?! How did you get in my house?!')
return

for comm in reversed(commander.factory.history[key]):
# Only look at our own, unless global was specified
if 'g' not in opts['groups'][2] and comm['user'] != opts['user']:
continue
# Don't look at other sed commands
if comm['message'].startswith('!s/'):
continue
if usr_regex.search(comm['message']):
new_msg = usr_regex.sub(usr_replace, comm['message'])
commander.say('{0} actually meant: {1}'
.format(comm['user'], new_msg))
break
else:
commander.say("Sorry, I couldn't match /{0}/.".format(usr_regex.pattern))

@CommanderFactory.registerCommand
class LetMeGoogleThatForYou(Command):
Expand Down

0 comments on commit e8c9129

Please sign in to comment.