From e24c1c0cc69747f82c357067c0c898fd8325a5cf Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Sat, 23 Oct 2004 04:55:52 +0000 Subject: [PATCH] Updated to use commands.wrap. --- plugins/Relay.py | 38 +++++++++++--------------------------- plugins/Scheduler.py | 33 +++++++++------------------------ plugins/Seen.py | 36 +++++++++++++----------------------- plugins/Tail.py | 18 ++++++++++-------- plugins/Unix.py | 23 +++++++++++------------ 5 files changed, 54 insertions(+), 94 deletions(-) diff --git a/plugins/Relay.py b/plugins/Relay.py index f8f37957d..d0e34dc67 100644 --- a/plugins/Relay.py +++ b/plugins/Relay.py @@ -46,11 +46,11 @@ from itertools import imap, ifilter import supybot.conf as conf import supybot.utils as utils import supybot.world as world +from supybot.commands import * import supybot.irclib as irclib import supybot.drivers as drivers import supybot.ircmsgs as ircmsgs import supybot.ircutils as ircutils -import supybot.privmsgs as privmsgs import supybot.registry as registry import supybot.callbacks as callbacks from supybot.structures import RingBuffer @@ -146,45 +146,38 @@ class Relay(callbacks.Privmsg): irc.queueMsg(ircmsgs.who(channel)) irc.queueMsg(ircmsgs.names(channel)) - def join(self, irc, msg, args): - """ + def join(self, irc, msg, args, channel): + """[] Starts relaying between the channel on all networks. If on a network the bot isn't in , he'll join. This commands is required even if the bot is in the channel on both networks; he won't relay between those channels unless he's told to join both - channels. + channels. If is not given, starts relaying on the channel + the message was sent in. """ - channel = privmsgs.getArgs(args) - if not ircutils.isChannel(channel): - irc.error('%r is not a valid channel.' % channel) - return self.registryValue('channels').add(channel) for otherIrc in world.ircs: # Should we abstract this? if channel not in otherIrc.state.channels: otherIrc.queueMsg(ircmsgs.join(channel)) irc.replySuccess() - join = privmsgs.checkCapability(join, 'owner') + join = wrap(join, ['channel']) - def part(self, irc, msg, args): + def part(self, irc, msg, args, channel): """ Ceases relaying between the channel on all networks. The bot will part from the channel on all networks in which it is on the channel. """ - channel = privmsgs.getArgs(args) - if not ircutils.isChannel(channel): - irc.error('%r is not a valid channel.' % channel) - return - self.registryValue('channels').remove(channel) + self.registryValue('channels').discard(channel) for otherIrc in world.ircs: if channel in otherIrc.state.channels: otherIrc.queueMsg(ircmsgs.part(channel)) irc.replySuccess() - part = privmsgs.checkCapability(part, 'owner') + part = wrap(part, ['channel']) - def nicks(self, irc, msg, args): + def nicks(self, irc, msg, args, channel): """[] Returns the nicks of the people in the channel on the various networks @@ -192,7 +185,6 @@ class Relay(callbacks.Privmsg): isn't sent on the channel itself. """ realIrc = self._getRealIrc(irc) - channel = privmsgs.getChannel(msg, args) if channel not in self.registryValue('channels'): irc.error('I\'m not relaying in %s.' % channel) return @@ -234,15 +226,7 @@ class Relay(callbacks.Privmsg): (ircutils.bold(network), numUsers, usersS)) users.sort() irc.reply('; '.join(users)) - - def ignore(self, irc, msg, args): - """[] - - Ignores everything said or done by in . - is only necessary if the message isn't sent in the channel - itself. - """ - pass + nicks = wrap(nicks, ['channel']) def do311(self, irc, msg): irc = self._getRealIrc(irc) diff --git a/plugins/Scheduler.py b/plugins/Scheduler.py index 3b3118bd3..7983bdf3b 100644 --- a/plugins/Scheduler.py +++ b/plugins/Scheduler.py @@ -44,6 +44,7 @@ import time import supybot.conf as conf import supybot.utils as utils +from supybot.commands import * import supybot.privmsgs as privmsgs import supybot.schedule as schedule import supybot.callbacks as callbacks @@ -63,7 +64,7 @@ class Scheduler(callbacks.Privmsg): self.Proxy(irc.irc, msg, tokens) return f - def add(self, irc, msg, args): + def add(self, irc, msg, args, seconds, command): """ Schedules the command string to run seconds in the @@ -72,25 +73,18 @@ class Scheduler(callbacks.Privmsg): command was given in (with no prefixed nick, a consequence of using echo). Do pay attention to the quotes in that example. """ - (seconds, command) = privmsgs.getArgs(args, required=2) - try: - seconds = int(seconds) - except ValueError: - irc.error('Invalid seconds value: %r' % seconds) - return f = self._makeCommandFunction(irc, msg, command) id = schedule.addEvent(f, time.time() + seconds) f.eventId = id self.events[str(id)] = command irc.replySuccess('Event #%s added.' % id) + add = wrap(add, ['positiveInt', 'text']) - def remove(self, irc, msg, args): + def remove(self, irc, msg, args, id): """ Removes the event scheduled with id from the schedule. """ - id = privmsgs.getArgs(args) - id = id.lower() if id in self.events: del self.events[id] try: @@ -104,8 +98,9 @@ class Scheduler(callbacks.Privmsg): irc.error('Invalid event id.') else: irc.error('Invalid event id.') + remove = wrap(remove, ['lowered']) - def repeat(self, irc, msg, args): + def repeat(self, irc, msg, args, name, seconds, command): """ Schedules the command to run every seconds, @@ -113,19 +108,7 @@ class Scheduler(callbacks.Privmsg): thereafter). is a name by which the command can be unscheduled. """ - (name, seconds, command) = privmsgs.getArgs(args, required=3) - name = name.lower() - try: - seconds = int(seconds) - except ValueError: - irc.error('Invalid seconds: %r' % seconds) - return - try: - name = int(name) - irc.error('Names must not be an integer.') - return - except ValueError: - pass + name = name.lower() # XXX We should have a "compose" context for this. self.events[name] = command f = self._makeCommandFunction(irc, msg, command, remove=False) id = schedule.addPeriodicEvent(f, seconds, name) @@ -133,6 +116,7 @@ class Scheduler(callbacks.Privmsg): # We don't reply because the command runs immediately. # But should we? What if the command doesn't have visible output? # irc.replySuccess() + repeat = wrap(repeat, ['nonInt', 'positiveInt', 'text']) def list(self, irc, msg, args): """takes no arguments @@ -147,6 +131,7 @@ class Scheduler(callbacks.Privmsg): irc.reply(utils.commaAndify(L)) else: irc.reply('There are currently no scheduled commands.') + list = wrap(list) Class = Scheduler diff --git a/plugins/Seen.py b/plugins/Seen.py index e67968f73..8baad6e65 100644 --- a/plugins/Seen.py +++ b/plugins/Seen.py @@ -51,10 +51,10 @@ import supybot.conf as conf import supybot.utils as utils import supybot.world as world import supybot.ircdb as ircdb +from supybot.commands import * import supybot.ircmsgs as ircmsgs import supybot.plugins as plugins import supybot.ircutils as ircutils -import supybot.privmsgs as privmsgs import supybot.registry as registry import supybot.callbacks as callbacks @@ -130,15 +130,13 @@ class Seen(callbacks.Privmsg): except KeyError: pass # Not in the database. - def seen(self, irc, msg, args): + def seen(self, irc, msg, args, channel, name): """[] Returns the last time was seen and what was last seen saying. is only necessary if the message isn't sent on the channel itself. """ - channel = privmsgs.getChannel(msg, args) - name = privmsgs.getArgs(args) try: results = [] if '*' in name: @@ -156,29 +154,31 @@ class Seen(callbacks.Privmsg): (when, said) = info L.append('%s (%s ago)' % (nick, utils.timeElapsed(time.time()-when))) - irc.reply('%s could be %s' % (name, utils.commaAndify(L, And='or'))) + irc.reply('%s could be %s' % + (name, utils.commaAndify(L, And='or'))) else: - irc.reply('I haven\'t seen anyone matching %s' % name) - + irc.reply('I haven\'t seen anyone matching %s.' % name) except KeyError: irc.reply('I have not seen %s.' % name) + # XXX This should be channeldb, but ChannelUserDictionary does't support it. + seen = wrap(seen, ['channel', 'nick']) - def last(self, irc, msg, args): + def last(self, irc, msg, args, channel): """[] Returns the last thing said in . is only necessary if the message isn't sent in the channel itself. """ - channel = privmsgs.getChannel(msg, args) try: (when, said) = self.db.seen(channel, '') irc.reply('Someone was last seen here %s ago saying: %s' % (utils.timeElapsed(time.time()-when), said)) except KeyError: irc.reply('I have never seen anyone.') + last = wrap(last, ['channel']) - def user(self, irc, msg, args): + def user(self, irc, msg, args, channel, user): """[] Returns the last time was seen and what was last seen @@ -187,23 +187,13 @@ class Seen(callbacks.Privmsg): is only necessary if the message isn't sent in the channel itself. """ - channel = privmsgs.getChannel(msg, args) - name = privmsgs.getArgs(args) try: - id = ircdb.users.getUserId(name) - except KeyError: - try: - hostmask = irc.state.nickToHostmask(name) - id = ircdb.users.getUserId(hostmask) - except KeyError: - irc.errorNoUser() - return - try: - (when, said) = self.db.seen(channel, id) + (when, said) = self.db.seen(channel, user.id) irc.reply('%s was last seen here %s ago saying: %s' % - (name, utils.timeElapsed(time.time()-when), said)) + (user.name, utils.timeElapsed(time.time()-when), said)) except KeyError: irc.reply('I have not seen %s.' % name) + user = wrap(user, ['channel', 'otherUser']) Class = Seen diff --git a/plugins/Tail.py b/plugins/Tail.py index 7e05f3b4a..fad28f355 100644 --- a/plugins/Tail.py +++ b/plugins/Tail.py @@ -44,6 +44,7 @@ import getopt import supybot.conf as conf import supybot.utils as utils +from supybot.commands import * import supybot.ircutils as ircutils import supybot.privmsgs as privmsgs import supybot.registry as registry @@ -138,32 +139,32 @@ class Tail(privmsgs.CapabilityCheckingPrivmsg): for target in self.registryValue('targets'): irc.reply(payload, to=target, notice=notice, private=True) - def add(self, irc, msg, args): + def add(self, irc, msg, args, filename): """ Basically does the equivalent of tail -f to the targets. """ - filename = privmsgs.getArgs(args) try: self._add(filename) except EnvironmentError, e: irc.error(utils.exnTostring(e)) return irc.replySuccess() + add = wrap(add, ['filename']) - def remove(self, irc, msg, args): + def remove(self, irc, msg, args, filename): """ Stops announcing the lines appended to . """ - filename = privmsgs.getArgs(args) try: self._remove(filename) irc.replySuccess() except KeyError: irc.error('I\'m not currently announcing %s.' % filename) + remove = wrap(remove, ['filename']) - def target(self, irc, msg, args): + def target(self, irc, msg, args, optlist, targets): """[--remove] [ ...] If given no arguments, returns the current list of targets for this @@ -171,12 +172,11 @@ class Tail(privmsgs.CapabilityCheckingPrivmsg): the current list of targets. If given --remove and any number of targets, will remove those targets from the current list of targets. """ - (optlist, args) = getopt.getopt(args, '', ['remove']) remove = False for (option, arg) in optlist: - if option == '--remove': + if option == 'remove': remove = True - if not args: + if not targets: L = self.registryValue('targets') if L: utils.sortBy(ircutils.toLower, L) @@ -185,6 +185,8 @@ class Tail(privmsgs.CapabilityCheckingPrivmsg): irc.reply('I\'m not currently targeting anywhere.') elif remove: pass #XXX + + target = wrap(target, [getopts({'remove': ''}), any('something')]) Class = Tail diff --git a/plugins/Unix.py b/plugins/Unix.py index 6cb0084e9..c2caf601b 100644 --- a/plugins/Unix.py +++ b/plugins/Unix.py @@ -49,6 +49,7 @@ import struct import supybot.conf as conf import supybot.utils as utils +from supybot.commands import * import supybot.privmsgs as privmsgs import supybot.registry as registry import supybot.callbacks as callbacks @@ -121,12 +122,11 @@ conf.registerGlobalValue(conf.supybot.plugins.Unix.wtf, 'command', class Unix(callbacks.Privmsg): - def errno(self, irc, msg, args): + def errno(self, irc, msg, args, s): """ Returns the number of an errno code, or the errno code of a number. """ - s = privmsgs.getArgs(args) try: i = int(s) name = errno.errorcode[i] @@ -140,6 +140,7 @@ class Unix(callbacks.Privmsg): except KeyError: name = '(unknown)' irc.reply('%s (#%s): %s' % (name, i, os.strerror(i))) + errno = wrap(errno, ['something']) def progstats(self, irc, msg, args): """takes no arguments @@ -154,10 +155,10 @@ class Unix(callbacks.Privmsg): Returns the current pid of the process for this Supybot. """ irc.reply(str(os.getpid()), private=True) - pid = privmsgs.checkCapability(pid, 'owner') + pid = wrap(pid, [('checkCapability', 'owner')]) _cryptre = re.compile(r'[./0-9A-Za-z]') - def crypt(self, irc, msg, args): + def crypt(self, irc, msg, args, password, salt): """ [] Returns the resulting of doing a crypt() on If is @@ -170,12 +171,12 @@ class Unix(callbacks.Privmsg): while self._cryptre.sub('', s) != '': s = struct.pack(' Returns the result of passing to aspell/ispell. The results @@ -189,7 +190,6 @@ class Unix(callbacks.Privmsg): 'installed on this computer. If one is installed, ' 'reconfigure supybot.plugins.Unix.spell.command ' 'appropriately.', Raise=True) - word = privmsgs.getArgs(args) if word and not word[0].isalpha(): irc.error(' must begin with an alphabet character.') return @@ -228,6 +228,7 @@ class Unix(callbacks.Privmsg): else: resp = 'Something unexpected was seen in the [ai]spell output.' irc.reply(resp) + spell = wrap(spell, ['something']) def fortune(self, irc, msg, args): """takes no arguments @@ -259,7 +260,7 @@ class Unix(callbacks.Privmsg): 'supybot.plugins.Unix.fortune.command configuration ' 'variable appropriately.') - def wtf(self, irc, msg, args): + def wtf(self, irc, msg, args, _, something): """[is] Returns wtf is. 'wtf' is a *nix command that first @@ -268,9 +269,6 @@ class Unix(callbacks.Privmsg): """ wtfCmd = self.registryValue('wtf.command') if wtfCmd: - if args and args[0] == 'is': - del args[0] - something = privmsgs.getArgs(args) something = something.rstrip('?') (r, w) = popen2.popen4([wtfCmd, something]) try: @@ -284,6 +282,7 @@ class Unix(callbacks.Privmsg): 'If it is installed on this system, reconfigure the ' 'supybot.plugins.Unix.wtf.command configuration ' 'variable appropriately.') + wtf = wrap(wtf, [optional(('literal', ['is'])), 'something']) Class = Unix