Moved tell and last commands from FunCommands to MiscCommands.

This commit is contained in:
Jeremy Fincher 2003-09-07 05:26:18 +00:00
parent 5db67b0b2c
commit 5ef3d7c5f5
4 changed files with 91 additions and 77 deletions

View File

@ -629,59 +629,6 @@ class FunCommands(callbacks.Privmsg):
dicts, lists, tuples, refcounts) dicts, lists, tuples, refcounts)
irc.reply(msg, response) irc.reply(msg, response)
def last(self, irc, msg, args):
"""[--{from,in,to,with,regexp,fancy}] <args>
Returns the last message matching the given criteria. --from requires
a nick from whom the message came; --in and --to require a channel the
message was sent to; --with requires some string that had to be in the
message; --regexp requires a regular expression the message must match
--fancy determines whether or not to show the nick; the default is not
"""
(optlist, rest) = getopt.getopt(args, '', ['from=', 'in=', 'to=',
'with=', 'regexp=',
'fancy'])
fancy = False
predicates = []
for (option, arg) in optlist:
option = option.strip('-')
if option == 'fancy':
fancy = True
elif option == 'from':
predicates.append(lambda m, arg=arg: m.nick == arg)
elif option == 'in' or option == 'to':
if not ircutils.isChannel(arg):
irc.error(msg, 'Argument to --%s must be a channel.' % arg)
return
predicates.append(lambda m, arg=arg: m.args[0] == arg)
elif option == 'with':
predicates.append(lambda m, arg=arg: arg in m.args[1])
elif option == 'regexp':
try:
r = utils.perlReToPythonRe(arg)
except ValueError, e:
irc.error(msg, str(e))
return
predicates.append(lambda m: r.search(m.args[1]))
first = True
for m in reviter(irc.state.history):
if first:
first = False
continue
if not m.prefix or m.command != 'PRIVMSG':
continue
for predicate in predicates:
if not predicate(m):
break
else:
if fancy:
irc.reply(msg, ircmsgs.prettyPrint(m))
else:
irc.reply(msg, m.args[1])
return
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>
@ -741,18 +688,7 @@ class FunCommands(callbacks.Privmsg):
return return
irc.reply(msg, s) irc.reply(msg, s)
def tell(self, irc, msg, args): _eightballs = (
"""<nick|channel> <text>
Tells the <nick|channel> whatever <text> is. Use nested commands to
your benefit here.
"""
(target, text) = privmsgs.getArgs(args, needed=2)
s = '%s wants me to tell you: %s' % (msg.nick, text)
irc.queueMsg(ircmsgs.privmsg(target, s))
raise callbacks.CannotNest
_eightballs = [
'outlook not so good.', 'outlook not so good.',
'my reply is no.', 'my reply is no.',
'don\'t count on it.', 'don\'t count on it.',
@ -772,8 +708,7 @@ class FunCommands(callbacks.Privmsg):
'without a doubt.', 'without a doubt.',
'reply hazy, try again.', 'reply hazy, try again.',
'as I see it, yes.', 'as I see it, yes.',
] )
def eightball(self, irc, msg, args): def eightball(self, irc, msg, args):
"""[<question>] """[<question>]

View File

@ -33,11 +33,17 @@
Miscellaneous commands. Miscellaneous commands.
""" """
from fix import *
import os import os
import getopt
import pprint import pprint
import conf import conf
import debug import debug
import utils
import ircmsgs
import ircutils
import privmsgs import privmsgs
import callbacks import callbacks
@ -228,6 +234,71 @@ class MiscCommands(callbacks.Privmsg):
except IndexError: except IndexError:
irc.error(msg, 'That\'s all, there is no more.') irc.error(msg, 'That\'s all, there is no more.')
def last(self, irc, msg, args):
"""[--{from,in,to,with,regexp,fancy}] <args>
Returns the last message matching the given criteria. --from requires
a nick from whom the message came; --in and --to require a channel the
message was sent to; --with requires some string that had to be in the
message; --regexp requires a regular expression the message must match
--fancy determines whether or not to show the nick; the default is not
"""
(optlist, rest) = getopt.getopt(args, '', ['from=', 'in=', 'to=',
'with=', 'regexp=',
'fancy'])
fancy = False
predicates = []
for (option, arg) in optlist:
option = option.strip('-')
if option == 'fancy':
fancy = True
elif option == 'from':
predicates.append(lambda m, arg=arg: m.nick == arg)
elif option == 'in' or option == 'to':
if not ircutils.isChannel(arg):
irc.error(msg, 'Argument to --%s must be a channel.' % arg)
return
predicates.append(lambda m, arg=arg: m.args[0] == arg)
elif option == 'with':
predicates.append(lambda m, arg=arg: arg in m.args[1])
elif option == 'regexp':
try:
r = utils.perlReToPythonRe(arg)
except ValueError, e:
irc.error(msg, str(e))
return
predicates.append(lambda m: r.search(m.args[1]))
first = True
for m in reviter(irc.state.history):
if first:
first = False
continue
if not m.prefix or m.command != 'PRIVMSG':
continue
for predicate in predicates:
if not predicate(m):
break
else:
if fancy:
irc.reply(msg, ircmsgs.prettyPrint(m))
else:
irc.reply(msg, m.args[1])
return
irc.error(msg, 'I couldn\'t find a message matching that criteria.')
def tell(self, irc, msg, args):
"""<nick|channel> <text>
Tells the <nick|channel> whatever <text> is. Use nested commands to
your benefit here.
"""
(target, text) = privmsgs.getArgs(args, needed=2)
s = '%s wants me to tell you: %s' % (msg.nick, text)
irc.queueMsg(ircmsgs.privmsg(target, s))
raise callbacks.CannotNest
Class = MiscCommands Class = MiscCommands

View File

@ -86,14 +86,6 @@ class FunCommandsTest(PluginTestCase, PluginDocumentation):
i = ord(c) i = ord(c)
self.assertResponse('ord %s' % utils.dqrepr(c), str(i)) self.assertResponse('ord %s' % utils.dqrepr(c), str(i))
def testTell(self):
m = self.getMsg('tell foo [rot13 foobar]')
self.failUnless(m.args[0] == 'foo')
self.failUnless('sbbone' in m.args[1])
m = self.getMsg('tell #foo [rot13 foobar]')
self.failUnless(m.args[0] == '#foo')
self.failUnless('sbbone' in m.args[1])
def testZen(self): def testZen(self):
self.assertNotError('zen') self.assertNotError('zen')

View File

@ -60,8 +60,24 @@ class MiscCommandsTestCase(PluginTestCase, PluginDocumentation):
def testGetprefixchar(self): def testGetprefixchar(self):
self.assertNotError('getprefixchar') self.assertNotError('getprefixchar')
def testModuleof(self): def testPlugin(self):
self.assertResponse('moduleof moduleof', 'MiscCommands') self.assertResponse('plugin plugin', 'MiscCommands')
def testTell(self):
m = self.getMsg('tell foo [plugin tell]')
self.failUnless(m.args[0] == 'foo')
self.failUnless('MiscCommands' in m.args[1])
m = self.getMsg('tell #foo [plugin tell]')
self.failUnless(m.args[0] == '#foo')
self.failUnless('MiscCommands' in m.args[1])
def testLast(self):
self.feedMsg('foo bar baz')
self.assertResponse('last', 'foo bar baz')
self.assertResponse('last', 'last')
self.assertResponse('last --with foo', 'foo bar baz')
self.assertResponse('last --regexp m/\s+/', 'last --with foo')
self.assertResponse('last --regexp m/bar/', 'foo bar baz')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: