Limnoria/src/commands.py

633 lines
21 KiB
Python
Raw Normal View History

#!/usr/bin/env python
###
# Copyright (c) 2002-2004, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
"""
Includes wrappers for commands.
"""
__revision__ = "$Id$"
import supybot.fix as fix
import getopt
import time
import types
import threading
2004-09-28 09:10:27 +02:00
import supybot.log as log
import supybot.conf as conf
import supybot.utils as utils
import supybot.world as world
import supybot.ircdb as ircdb
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
import supybot.webutils as webutils
import supybot.callbacks as callbacks
import supybot.structures as structures
###
# Non-arg wrappers -- these just change the behavior of a command without
# changing the arguments given to it.
###
def thread(f):
"""Makes sure a command spawns a thread when called."""
def newf(self, irc, msg, args, *L, **kwargs):
2004-09-30 06:14:44 +02:00
if world.isMainThread():
t = callbacks.CommandThread(target=irc._callCommand,
args=(f.func_name, self),
kwargs=kwargs)
t.start()
else:
f(self, irc, msg, args, *L, **kwargs)
return utils.changeFunctionName(newf, f.func_name, f.__doc__)
2004-09-30 06:14:44 +02:00
class UrlSnarfThread(world.SupyThread):
def __init__(self, *args, **kwargs):
assert 'url' in kwargs
kwargs['name'] = 'Thread #%s (for snarfing %s)' % \
(world.threadsSpawned, kwargs.pop('url'))
2004-09-30 06:14:44 +02:00
super(UrlSnarfThread, self).__init__(*args, **kwargs)
self.setDaemon(True)
class SnarfQueue(ircutils.FloodQueue):
timeout = conf.supybot.snarfThrottle
def key(self, channel):
return channel
_snarfed = SnarfQueue()
class SnarfIrc(object):
def __init__(self, irc, channel, url):
self.irc = irc
self.url = url
self.channel = channel
def __getattr__(self, attr):
return getattr(self.irc, attr)
def reply(self, *args, **kwargs):
_snarfed.enqueue(self.channel, self.url)
return self.irc.reply(*args, **kwargs)
# This lock is used to serialize the calls to snarfers, so
# earlier snarfers are guaranteed to beat out later snarfers.
_snarfLock = threading.Lock()
def urlSnarfer(f):
"""Protects the snarfer from loops (with other bots) and whatnot."""
def newf(self, irc, msg, match, *L, **kwargs):
url = match.group(0)
channel = msg.args[0]
if not ircutils.isChannel(channel):
return
if ircdb.channels.getChannel(channel).lobotomized:
self.log.info('Not snarfing in %s: lobotomized.', channel)
return
if _snarfed.has(channel, url):
self.log.info('Throttling snarf of %s in %s.', url, channel)
return
irc = SnarfIrc(irc, channel, url)
def doSnarf():
_snarfLock.acquire()
try:
if msg.repliedTo:
self.log.debug('Not snarfing, msg is already repliedTo.')
return
f(self, irc, msg, match, *L, **kwargs)
finally:
_snarfLock.release()
if threading.currentThread() is not world.mainThread:
doSnarf()
else:
L = list(L)
t = UrlSnarfThread(target=doSnarf, url=url)
t.start()
newf = utils.changeFunctionName(newf, f.func_name, f.__doc__)
return newf
2004-09-28 09:10:27 +02:00
decorators = ircutils.IrcDict({
'thread': thread,
'urlSnarfer': urlSnarfer,
})
###
2004-09-28 09:10:27 +02:00
# Arg wrappers, wrappers that add arguments to the command. They accept the
# irc, msg, and args, of course, as well as a State object which holds the args
# (and kwargs, though none currently take advantage of that) to be given to the
# command being decorated, as well as the name of the command, the plugin, the
# log, etc.
###
2004-09-28 09:10:27 +02:00
# This is just so we can centralize this, since it may change.
def _int(s):
2004-10-02 19:31:58 +02:00
base = 10
if s.startswith('0x'):
base = 16
s = s[2:]
elif s.startswith('0b'):
base = 2
s = s[2:]
elif s.startswith('0') and len(s) > 1:
base = 8
s = s[1:]
try:
return int(s, base)
except ValueError:
if base == 10:
return int(float(s))
else:
raise
2004-09-28 09:10:27 +02:00
2004-09-30 06:14:44 +02:00
def getInt(irc, msg, args, state, type='integer', p=None):
try:
2004-09-28 09:10:27 +02:00
i = _int(args[0])
if p is not None:
if not p(i):
raise ValueError
state.args.append(_int(args[0]))
del args[0]
except ValueError:
2004-09-30 06:14:44 +02:00
irc.errorInvalid(type, args[0])
2004-09-30 09:43:24 +02:00
def getNonInt(irc, msg, args, state, type='non-integer value'):
try:
i = _int(args[0])
irc.errorInvalid(type, args[0])
except ValueError:
state.args.append(args.pop(0))
def getFloat(irc, msg, args, state):
try:
x = float(args[0])
del args[0]
return x
except ValueError:
irc.errorInvalid('floating point number', args[0])
2004-09-28 09:10:27 +02:00
def getPositiveInt(irc, msg, args, state, *L):
getInt(irc, msg, args, state,
p=lambda i: i<=0, type='positive integer', *L)
2004-09-28 09:10:27 +02:00
def getNonNegativeInt(irc, msg, args, state, *L):
getInt(irc, msg, args, state,
p=lambda i: i<0, type='non-negative integer', *L)
2004-10-01 23:27:22 +02:00
def getId(irc, msg, args, state, kind=None):
type = 'id'
if kind is not None:
type = kind + ' id'
getInt(irc, msg, args, state, type=type)
2004-09-28 09:10:27 +02:00
2004-09-30 06:14:44 +02:00
def getExpiry(irc, msg, args, state):
2004-09-28 09:10:27 +02:00
now = int(time.time())
try:
2004-09-28 09:10:27 +02:00
expires = _int(args[0])
if expires:
expires += now
state.args.append(expires)
del args[0]
except ValueError:
2004-09-30 06:14:44 +02:00
irc.errorInvalid('number of seconds', args[0])
2004-09-30 06:14:44 +02:00
def getBoolean(irc, msg, args, state):
2004-09-28 09:10:27 +02:00
try:
state.args.append(utils.toBool(args[0]))
del args[0]
except ValueError:
2004-09-30 06:14:44 +02:00
irc.errorInvalid('boolean', args[0])
2004-09-28 09:10:27 +02:00
def getChannelDb(irc, msg, args, state, **kwargs):
if not conf.supybot.databases.plugins.channelSpecific():
2004-09-28 09:10:27 +02:00
state.args.append(None)
state.channel = None
else:
2004-09-28 09:10:27 +02:00
getChannel(irc, msg, args, state, **kwargs)
2004-09-28 09:10:27 +02:00
def getHaveOp(irc, msg, args, state, action='do that'):
if state.channel not in irc.state.channels:
irc.error('I\'m not even in %s.' % state.channel, Raise=True)
if irc.nick not in irc.state.channels[state.channel].ops:
irc.error('I need to be opped to %s.' % action, Raise=True)
2004-09-28 09:10:27 +02:00
def validChannel(irc, msg, args, state):
if ircutils.isChannel(args[0]):
# XXX Check maxlength in irc.state.supported.
state.args.append(args.pop(0))
else:
2004-09-28 09:10:27 +02:00
irc.errorInvalid('channel', args[0])
2004-09-28 09:10:27 +02:00
def getHostmask(irc, msg, args, state):
if ircutils.isUserHostmask(args[0]):
2004-09-28 09:10:27 +02:00
state.args.append(args.pop(0))
else:
try:
2004-09-28 09:10:27 +02:00
hostmask = irc.state.nickToHostmask(args[0])
state.args.append(hostmask)
del args[0]
except KeyError:
2004-09-28 09:10:27 +02:00
irc.errorInvalid('nick or hostmask', args[0])
2004-09-28 09:10:27 +02:00
def getBanmask(irc, msg, args, state):
getHostmask(irc, msg, args, state)
# XXX Channel-specific stuff.
state.args[-1] = ircutils.banmask(state.args[-1])
def getUser(irc, msg, args, state):
try:
2004-09-28 09:10:27 +02:00
state.args.append(ircdb.users.getUser(msg.prefix))
except KeyError:
irc.errorNotRegistered(Raise=True)
2004-09-28 09:10:27 +02:00
def getOtherUser(irc, msg, args, state):
try:
2004-09-28 09:10:27 +02:00
state.args.append(ircdb.users.getUser(args[0]))
del args[0]
except KeyError:
try:
2004-09-28 09:10:27 +02:00
getHostmask(irc, msg, args, state)
hostmask = state.args.pop()
state.args.append(ircdb.users.getUser(hostmask))
except (KeyError, IndexError, callbacks.Error):
irc.errorNoUser(Raise=True)
def _getRe(f):
2004-09-28 09:10:27 +02:00
def get(irc, msg, args, state):
original = args[:]
s = args.pop(0)
def isRe(s):
try:
_ = f(s)
return True
except ValueError:
return False
2004-09-28 09:10:27 +02:00
try:
while not isRe(s):
s += ' ' + args.pop(0)
state.args.append(f(s))
except IndexError:
args[:] = original
raise
return get
getMatcher = _getRe(utils.perlReToPythonRe)
getReplacer = _getRe(utils.perlReToReplacer)
2004-09-28 09:10:27 +02:00
def getNick(irc, msg, args, state):
if ircutils.isNick(args[0]):
if 'nicklen' in irc.state.supported:
2004-09-28 09:10:27 +02:00
if len(args[0]) > irc.state.supported['nicklen']:
irc.errorInvalid('nick', s,
2004-09-28 09:10:27 +02:00
'That nick is too long for this server.')
state.args.append(args.pop(0))
else:
2004-09-28 09:10:27 +02:00
irc.errorInvalid('nick', s)
2004-09-28 09:10:27 +02:00
def getChannel(irc, msg, args, state):
if args and ircutils.isChannel(args[0]):
channel = args.pop(0)
elif ircutils.isChannel(msg.args[0]):
channel = msg.args[0]
else:
2004-09-28 09:10:27 +02:00
state.log.debug('Raising ArgumentError because there is no channel.')
raise callbacks.ArgumentError
2004-09-28 09:10:27 +02:00
state.channel = channel
state.args.append(channel)
2004-10-02 04:21:26 +02:00
def inChannel(irc, msg, args, state):
if not state.channel:
getChannel(irc, msg, args, state)
2004-10-02 04:21:26 +02:00
if state.channel not in irc.state.channels:
irc.error('I\'m not in %s.' % state.channel, Raise=True)
2004-09-30 09:43:24 +02:00
def getChannelOrNone(irc, msg, args, state):
try:
getChannel(irc, msg, args, state)
except callbacks.ArgumentError:
state.args.append(None)
2004-09-28 09:10:27 +02:00
def checkChannelCapability(irc, msg, args, state, cap):
if not state.channel:
getChannel(irc, msg, args, state)
2004-09-28 09:10:27 +02:00
cap = ircdb.canonicalCapability(cap)
cap = ircdb.makeChannelCapability(state.channel, cap)
if not ircdb.checkCapability(msg.prefix, cap):
irc.errorNoCapability(cap, Raise=True)
def getLowered(irc, msg, args, state):
state.args.append(ircutils.toLower(args.pop(0)))
def getSomething(irc, msg, args, state, errorMsg=None, p=None):
if p is None:
p = lambda _: True
if not args[0] or not p(args[0]):
if errorMsg is None:
errorMsg = 'You must not give the empty string as an argument.'
irc.error(errorMsg, Raise=True)
else:
state.args.append(args.pop(0))
def getSomethingNoSpaces(irc, msg, args, state, *L):
def p(s):
return len(s.split(None, 1)) == 1
getSomething(irc, msg, args, state, p=p, *L)
2004-09-28 09:10:27 +02:00
def getPlugin(irc, msg, args, state, requirePresent=False):
cb = irc.getCallback(args[0])
if requirePresent and cb is None:
irc.errorInvalid('plugin', s)
state.args.append(cb)
del args[0]
def private(irc, msg, args, state):
if ircutils.isChannel(msg.args[0]):
irc.errorRequiresPrivacy(Raise=True)
2004-10-02 19:31:58 +02:00
def public(irc, msg, args, state, errmsg=None):
if not ircutils.isChannel(msg.args[0]):
if errmsg is None:
errmsg = 'This message must be sent in a channel.'
irc.error(errmsg, Raise=True)
2004-09-28 09:10:27 +02:00
def checkCapability(irc, msg, args, state, cap):
cap = ircdb.canonicalCapability(cap)
if not ircdb.checkCapability(msg.prefix, cap):
state.log.warning('%s tried %s without %s.',
msg.prefix, state.name, cap)
irc.errorNoCapability(cap, Raise=True)
def anything(irc, msg, args, state):
state.args.append(args.pop(0))
def getUrl(irc, msg, args, state):
if webutils.urlRe.match(args[0]):
state.args.append(args.pop(0))
else:
irc.errorInvalid('url', args[0])
2004-10-01 23:27:22 +02:00
def getNow(irc, msg, args, state):
state.args.append(int(time.time()))
def getCommandName(irc, msg, args, state):
state.args.append(callbacks.canonicalName(args.pop(0)))
2004-10-02 19:31:58 +02:00
def getIp(irc, msg, args, state):
if utils.isIP(args[0]):
state.args.append(args.pop(0))
else:
irc.errorInvalid('ip', args[0])
def getLetter(irc, msg, args, state):
if len(args[0]) == 1:
state.args.append(args.pop(0))
else:
irc.errorInvalid('letter', args[0])
def getMatch(irc, msg, args, state, regexp, errmsg):
m = regexp.search(args[0])
if m is not None:
state.args.append(m)
del args[0]
else:
irc.error(errmsg, Raise=True)
def getLiteral(irc, msg, args, state, literals, errmsg=None):
if isinstance(literals, basestring):
literals = (literals,)
if args[0] in literals:
state.args.append(args.pop(0))
elif errmsg is not None:
irc.error(errmsg, Raise=True)
else:
raise callbacks.ArgumentError
2004-09-28 09:10:27 +02:00
wrappers = ircutils.IrcDict({
'id': getId,
2004-10-02 19:31:58 +02:00
'ip': getIp,
'int': getInt,
2004-10-01 23:27:22 +02:00
'now': getNow,
'url': getUrl,
2004-09-30 09:43:24 +02:00
'float': getFloat,
'nonInt': getNonInt,
2004-09-28 09:10:27 +02:00
'positiveInt': getPositiveInt,
'nonNegativeInt': getNonNegativeInt,
2004-10-02 19:31:58 +02:00
'letter': getLetter,
2004-09-28 09:10:27 +02:00
'haveOp': getHaveOp,
'expiry': getExpiry,
2004-10-02 19:31:58 +02:00
'literal': getLiteral,
'nick': getNick,
2004-09-23 00:43:23 +02:00
'channel': getChannel,
2004-10-02 04:21:26 +02:00
'inChannel': inChannel,
'plugin': getPlugin,
'boolean': getBoolean,
'lowered': getLowered,
2004-09-28 09:10:27 +02:00
'anything': anything,
'something': getSomething,
'commandName': getCommandName,
2004-10-01 23:27:22 +02:00
'text': anything,
2004-09-28 09:10:27 +02:00
'somethingWithoutSpaces': getSomethingNoSpaces,
'capability': getSomethingNoSpaces,
2004-09-23 00:43:23 +02:00
'channelDb': getChannelDb,
'hostmask': getHostmask,
'banmask': getBanmask,
'user': getUser,
2004-10-02 19:31:58 +02:00
'public': public,
2004-09-28 09:10:27 +02:00
'private': private,
'otherUser': getOtherUser,
'regexpMatcher': getMatcher,
'validChannel': validChannel,
'regexpReplacer': getReplacer,
2004-09-28 09:10:27 +02:00
'checkCapability': checkCapability,
'checkChannelCapability': checkChannelCapability,
})
2004-10-02 04:21:26 +02:00
def addWrapper(name, wrapper):
wrappers[name] = wrapper
2004-09-28 09:10:27 +02:00
class State(object):
def __init__(self, name=None, logger=None):
if logger is None:
logger = log
self.args = []
self.kwargs = {}
self.name = name
self.log = logger
self.getopts = []
self.channel = None
def args(irc,msg,args, types=[], state=None,
getopts=None, allowExtra=False, requireExtra=False, combineRest=True):
2004-09-28 09:10:27 +02:00
if state is None:
state = State(name='unknown', logger=log)
if requireExtra:
combineRest = False # Implied by requireExtra.
types = types[:] # We're going to destroy this.
if getopts is not None:
getoptL = []
for (key, value) in getopts.iteritems():
if value != '': # value can be None, remember.
key += '='
getoptL.append(key)
2004-10-02 00:33:02 +02:00
log.debug(str(getoptL))
2004-09-28 09:10:27 +02:00
def callWrapper(spec):
if isinstance(spec, tuple):
assert spec, 'tuple specification cannot be empty.'
name = spec[0]
specArgs = spec[1:]
else:
2004-09-28 09:10:27 +02:00
assert isinstance(spec, basestring) or spec is None
name = spec
specArgs = ()
if name is None:
name = 'anything'
enforce = True
optional = False
if name.startswith('?'):
optional = True
name = name[1:]
elif name.endswith('?'):
optional = True
enforce = False
name = name[:-1]
2004-10-02 04:21:26 +02:00
elif name[-1] in '*+':
name = name[:-1]
2004-09-30 09:43:24 +02:00
default = ''
2004-09-28 09:10:27 +02:00
wrapper = wrappers[name]
2004-09-30 06:14:44 +02:00
if optional and specArgs:
# First arg is default.
default = specArgs[0]
specArgs = specArgs[1:]
if callable(default):
default = default()
2004-09-28 09:10:27 +02:00
try:
wrapper(irc, msg, args, state, *specArgs)
except (callbacks.Error, ValueError, callbacks.ArgumentError), e:
state.log.debug('%r when calling wrapper.', utils.exnToString(e))
if not enforce:
2004-09-30 09:43:24 +02:00
state.args.append(default)
2004-09-28 09:10:27 +02:00
else:
state.log.debug('Re-raising %s because of enforce.', e)
raise
except IndexError, e:
state.log.debug('%r when calling wrapper.', utils.exnToString(e))
if optional:
2004-09-30 09:43:24 +02:00
state.args.append(default)
2004-09-28 09:10:27 +02:00
else:
state.log.debug('Raising ArgumentError because of '
'non-optional args: %r', spec)
2004-09-28 09:10:27 +02:00
raise callbacks.ArgumentError
# First, we getopt stuff.
if getopts is not None:
(optlist, args) = getopt.getopt(args, '', getoptL)
for (opt, arg) in optlist:
opt = opt[2:] # Strip --
assert opt in getopts
2004-10-02 00:33:02 +02:00
log.debug('%s: %r', opt, arg)
if arg is not None:
# This is a MESS. But I can't think of a better way to do it.
assert getopts[opt] != ''
originalArgs = args
args = [arg]
originalLen = len(state.args)
callWrapper(getopts[opt])
args = originalArgs
assert originalLen == len(state.args)-1
assert not args
state.getopts.append((opt, state.args.pop()))
else:
assert getopts[opt] == ''
2004-09-28 09:10:27 +02:00
state.getopts.append((opt, True))
#log.debug('Finished getopts: %s', state.getopts)
2004-09-28 09:10:27 +02:00
# Second, we get out everything but the last argument (or, if combineRest
# is False, we'll clear out all the types).
while len(types) > 1 or (types and not combineRest):
callWrapper(types.pop(0))
# Third, if there is a remaining required or optional argument
# (there's a possibility that there were no required or optional
# arguments) then we join the remaining args and work convert that.
if types:
2004-10-02 04:21:26 +02:00
name = types[0]
if isinstance(name, tuple):
name = name[0]
if name[-1] in '*+':
originalStateArgs = state.args
state.args = []
if name.endswith('+'):
callWrapper(types[0]) # So it raises an error if no args.
while args:
callWrapper(types[0])
lastArgs = state.args
state.args = originalStateArgs
state.args.append(lastArgs)
else:
if args:
rest = ' '.join(args)
args = [rest]
callWrapper(types.pop(0))
if args and not allowExtra:
log.debug('args but not allowExtra: %r', args)
raise callbacks.ArgumentError
2004-09-28 09:10:27 +02:00
if requireExtra and not args:
log.debug('requireExtra and not args: %r', args)
log.debug('command.args args: %r' % args)
log.debug('command.args state.args: %r' % state.args)
log.debug('command.args state.getopts: %r' % state.getopts)
2004-09-28 09:10:27 +02:00
return state
# These are used below, but we need to rename them so their names aren't
# shadowed by our locals.
_args = args
2004-09-28 09:10:27 +02:00
_decorators = decorators
def wrap(f, *argsArgs, **argsKwargs):
def newf(self, irc, msg, args, **kwargs):
2004-09-28 09:10:27 +02:00
state = State('%s.%s' % (self.name(), f.func_name), self.log)
state.cb = self # This should probably be in State.__init__.
_args(irc,msg,args, state=state, *argsArgs, **argsKwargs)
2004-10-02 00:33:02 +02:00
if 'getopts' in argsKwargs:
2004-09-28 09:10:27 +02:00
f(self, irc, msg, args, state.getopts, *state.args, **state.kwargs)
else:
f(self, irc, msg, args, *state.args, **state.kwargs)
2004-09-30 06:14:44 +02:00
newf = utils.changeFunctionName(newf, f.func_name, f.__doc__)
2004-09-28 09:10:27 +02:00
decorators = argsKwargs.pop('decorators', None)
if decorators is not None:
decorators = map(_decorators.__getitem__, decorators)
for decorator in decorators:
newf = decorator(newf)
2004-09-30 06:14:44 +02:00
return newf
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: