Skip to content
Browse files

Make sed smarter.

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...
1 parent f07a353 commit e8c9129dd671db54323c8b2277b4d562e2413372 @mythmon mythmon committed Jul 17, 2011
Showing with 26 additions and 12 deletions.
  1. +9 −8 hamper/commander.py
  2. +17 −4 hamper/commands.py
View
17 hamper/commander.py
@@ -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)
@@ -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()
@@ -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())
View
21 hamper/commands.py
@@ -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):

0 comments on commit e8c9129

Please sign in to comment.
Something went wrong with that request. Please try again.