Changed last/lastfrom to the new method as per RFE 798374.

This commit is contained in:
Jeremy Fincher 2003-09-03 17:18:38 +00:00
parent 9256468885
commit 0791d9cbea

View File

@ -47,6 +47,7 @@ import sha
import time import time
import math import math
import cmath import cmath
import getopt
import socket import socket
import string import string
import random import random
@ -620,54 +621,50 @@ class FunCommands(callbacks.Privmsg):
irc.reply(msg, response) irc.reply(msg, response)
def last(self, irc, msg, args): def last(self, irc, msg, args):
"""[<channel>] <message number> """[--{from,in,to,with,regexp}] <args>
Gets message number <message number> from the bot's history. Returns the last message matching the given criteria. --from requires
<message number> defaults to 1, the last message prior to this command a nick from whom the message came; --in and --to require a channel the
itself. <channel> is only necessary if the message isn't sent in the message was sent to; --with requires some string that had to be in the
channel itself. message; --regexp requires a regular expression the message must match
""" """
channel = privmsgs.getChannel(msg, args)
n = privmsgs.getArgs(args, needed=0, optional=1)
if n == '':
n = 1
else:
try:
n = int(n)
except ValueError:
irc.error(msg, '<message number> must be an integer.')
return
n += 1 # To remove the last question asked.
for msg in reviter(irc.state.history):
if msg.command == 'PRIVMSG' and msg.args[0] == channel and n == 1:
irc.reply(msg, msg.args[1])
return
else:
n -= 1
if n > 1:
s = 'I don\'t have a history of that many messages.'
irc.error(msg, s)
def lastfrom(self, irc, msg, args): (optlist, rest) = getopt.getopt(args, '', ['from=', 'in=', 'to=',
"""[<channel>] <nick> 'with=', 'regexp='])
undecorated = False
Returns the last message in <channel> from <nick>. <channel> is only predicates = []
necessary if the message isn't sent in the channel itself. for (option, arg) in optlist:
""" option = option.strip('-')
channel = privmsgs.getChannel(msg, args) if option == 'from':
nick = privmsgs.getArgs(args) predicates.append(lambda m: m.nick == arg)
elif option == 'in' or option == 'to':
if not ircutils.isChannel(argument):
irc.error(msg, 'Argument to --%s must be a channel.' % arg)
return
predicates.append(lambda m: m.args[0] == arg)
elif option == 'with':
predicates.append(lambda m: arg in m.args[1])
elif option == 'regexp':
try:
r = utils.perlReToPythonRe(arg)
except ValueError:
irc.error(msg, str(e))
return
predicates.append(lambda m: r.search(m.args[1]))
first = True
for m in reviter(irc.state.history): for m in reviter(irc.state.history):
if m.command == 'PRIVMSG' and \ if first:
m.nick == nick and \ first = False
m.args[0] == channel: continue
if ircmsgs.isAction(m): if m.command != 'PRIVMSG':
irc.reply(msg, '* %s %s' % (nick, ircmsgs.unAction(m))) continue
return if all(bool, [f(m) for f in predicates]):
if undecorated:
irc.reply(msg, m.args[1])
else: else:
irc.reply(msg, '<%s> %s' % (nick, m.args[1])) irc.reply(msg, ircmsgs.prettyPrint(m))
return
return return
irc.error(msg, 'I don\'t remember a message from that person.') irc.error(msg, 'I couldn\'t find a message matching that criteria.')
def levenshtein(self, irc, msg, args): def levenshtein(self, irc, msg, args):
"""<string1> <string2> """<string1> <string2>