diff --git a/plugins/Seen/plugin.py b/plugins/Seen/plugin.py index 291027b54..23e1f45e9 100644 --- a/plugins/Seen/plugin.py +++ b/plugins/Seen/plugin.py @@ -291,6 +291,47 @@ class Seen(callbacks.Plugin): self._user(irc, channel, user) user = wrap(user, ['channel', 'otherUser']) + def since(self, irc, msg, args, channel, nick): + """[] + + Returns the messages since last left the channel. + """ + if nick is None: + nick = msg.nick + if nick not in irc.state.channels[channel].users: + irc.error(format('You must be in %s to use this command.', channel)) + return + end = None # By default, up until the most recent message. + for (i, m) in utils.seq.renumerate(irc.state.history): + if end is None and m.command == 'JOIN' and \ + ircutils.strEqual(m.args[0], channel) and \ + ircutils.strEqual(m.nick, nick): + end = i + if m.command == 'PART' and \ + ircutils.strEqual(m.nick, nick) and \ + ircutils.strEqual(m.args[0], channel): + break + elif m.command == 'QUIT' and ircutils.strEqual(m.nick, nick): + # XXX We assume the person was in-channel at this point. + break + elif m.command == 'KICK' and \ + ircutils.strEqual(m.args[1], nick) and \ + ircutils.strEqual(m.args[0], channel): + break + else: # I never use this; it only kicks in when the for loop exited normally. + irc.error(format('I couldn\'t find in my history of %s messages ' + 'where %r last left the %s', + len(irc.state.history), nick, channel)) + return + msgs = [m for m in irc.state.history[i:end] + if m.command == 'PRIVMSG' and ircutils.strEqual(m.args[0], channel)] + if msgs: + irc.reply(format('%L', map(ircmsgs.prettyPrint, msgs))) + else: + irc.reply(format('Either %s didn\'t leave, ' + 'or no messages were sent while %s was gone.', nick, nick)) + since = wrap(since, ['channel', additional('nick')]) + Class = Seen # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/src/utils/seq.py b/src/utils/seq.py index cd5cefe7a..8df15b226 100644 --- a/src/utils/seq.py +++ b/src/utils/seq.py @@ -41,6 +41,10 @@ def mapinto(f, L): for (i, x) in enumerate(L): L[i] = f(x) +def renumerate(L): + for i in xrange(len(L)-1, -1, -1): + yield (i, L[i]) + # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/test/test_utils.py b/test/test_utils.py index c61132181..c71703924 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -46,6 +46,12 @@ class UtilsTest(SupyTestCase): class SeqTest(SupyTestCase): + def testRenumerate(self): + for i in xrange(5): + L = list(enumerate(range(i))) + LL = list(utils.seq.renumerate(range(i))) + self.assertEqual(L, LL[::-1]) + def testWindow(self): L = range(10) def wwindow(*args):