2004-09-21 05:31:19 +02:00
|
|
|
###
|
|
|
|
# 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 time
|
|
|
|
import types
|
2004-10-19 18:43:09 +02:00
|
|
|
import getopt
|
2004-09-21 05:31:19 +02:00
|
|
|
import threading
|
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
import supybot.log as log
|
2004-09-21 05:31:19 +02:00
|
|
|
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
|
2004-09-30 11:57:17 +02:00
|
|
|
import supybot.webutils as webutils
|
2004-09-21 05:31:19 +02:00
|
|
|
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.
|
|
|
|
###
|
2004-10-12 02:41:40 +02:00
|
|
|
|
|
|
|
# Thread has to be a non-arg wrapper because by the time we're parsing and
|
|
|
|
# validating arguments, we're inside the function we'd want to thread.
|
2004-09-21 05:31:19 +02:00
|
|
|
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():
|
2004-09-21 05:31:19 +02:00
|
|
|
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):
|
2004-09-21 05:31:19 +02:00
|
|
|
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)
|
2004-09-21 05:31:19 +02:00
|
|
|
self.setDaemon(True)
|
|
|
|
|
2004-10-22 07:56:55 +02:00
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
super(UrlSnarfThread, self).run()
|
|
|
|
except webutils.WebError, e:
|
|
|
|
log.debug('Exception in urlSnarfer: %s' % utils.exnToString(e))
|
|
|
|
|
2004-09-21 05:31:19 +02:00
|
|
|
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)
|
2004-09-30 11:57:17 +02:00
|
|
|
return self.irc.reply(*args, **kwargs)
|
2004-09-23 18:13:00 +02:00
|
|
|
|
2004-09-21 05:31:19 +02:00
|
|
|
# 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]
|
2004-10-15 13:18:55 +02:00
|
|
|
if not irc.isChannel(channel):
|
2004-09-21 05:31:19 +02:00
|
|
|
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-10-12 02:41:40 +02:00
|
|
|
# Converters, which take irc, msg, args, and a state object, and build up the
|
|
|
|
# validated and converted args for the method in state.args.
|
2004-09-21 05:31:19 +02:00
|
|
|
###
|
2004-09-28 09:10:27 +02:00
|
|
|
|
|
|
|
# This is just so we can centralize this, since it may change.
|
2004-11-28 03:23:59 +01:00
|
|
|
def _int(s, Long=False):
|
|
|
|
if not Long:
|
|
|
|
Type = int
|
|
|
|
else:
|
|
|
|
Type = long
|
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:
|
2004-11-28 03:23:59 +01:00
|
|
|
return Type(s, base)
|
2004-10-02 19:31:58 +02:00
|
|
|
except ValueError:
|
|
|
|
if base == 10:
|
2004-11-28 03:23:59 +01:00
|
|
|
return Type(float(s))
|
2004-10-02 19:31:58 +02:00
|
|
|
else:
|
|
|
|
raise
|
2004-09-28 09:10:27 +02:00
|
|
|
|
2004-11-28 03:23:59 +01:00
|
|
|
def getInt(irc, msg, args, state, Type='integer', p=None, Long=False):
|
2004-09-21 05:31:19 +02:00
|
|
|
try:
|
2004-11-28 03:23:59 +01:00
|
|
|
i = _int(args[0], Long)
|
2004-10-19 14:13:41 +02:00
|
|
|
if p is not None:
|
|
|
|
if not p(i):
|
2004-11-28 03:23:59 +01:00
|
|
|
irc.errorInvalid(Type, args[0])
|
2004-10-06 06:32:03 +02:00
|
|
|
state.args.append(i)
|
2004-09-28 09:10:27 +02:00
|
|
|
del args[0]
|
2004-09-21 05:31:19 +02:00
|
|
|
except ValueError:
|
2004-11-28 03:23:59 +01:00
|
|
|
irc.errorInvalid(Type, args[0])
|
2004-09-23 18:13:00 +02:00
|
|
|
|
2004-11-28 03:23:59 +01:00
|
|
|
def getNonInt(irc, msg, args, state, Type='non-integer value'):
|
2004-09-30 09:43:24 +02:00
|
|
|
try:
|
|
|
|
i = _int(args[0])
|
2004-11-28 03:23:59 +01:00
|
|
|
irc.errorInvalid(Type, args[0])
|
2004-09-30 09:43:24 +02:00
|
|
|
except ValueError:
|
|
|
|
state.args.append(args.pop(0))
|
|
|
|
|
2004-11-28 03:23:59 +01:00
|
|
|
def getLong(irc, msg, args, state, Type='long number'):
|
|
|
|
getInt(irc, msg, args, state, Type, Long=True)
|
|
|
|
|
|
|
|
def getFloat(irc, msg, args, state, Type='floating point number'):
|
2004-09-30 09:43:24 +02:00
|
|
|
try:
|
2004-10-03 11:03:34 +02:00
|
|
|
state.args.append(float(args[0]))
|
2004-09-30 09:43:24 +02:00
|
|
|
del args[0]
|
|
|
|
except ValueError:
|
2004-11-28 03:23:59 +01:00
|
|
|
irc.errorInvalid(Type, args[0])
|
2004-09-30 09:43:24 +02:00
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
def getPositiveInt(irc, msg, args, state, *L):
|
|
|
|
getInt(irc, msg, args, state,
|
2004-11-28 03:23:59 +01:00
|
|
|
p=lambda i: i>0, Type='positive integer', *L)
|
2004-09-21 05:31:19 +02:00
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
def getNonNegativeInt(irc, msg, args, state, *L):
|
|
|
|
getInt(irc, msg, args, state,
|
2004-11-28 03:23:59 +01:00
|
|
|
p=lambda i: i>=0, Type='non-negative integer', *L)
|
2004-09-28 09:10:27 +02:00
|
|
|
|
2004-10-19 13:56:34 +02:00
|
|
|
def getIndex(irc, msg, args, state):
|
2004-11-28 03:23:59 +01:00
|
|
|
getInt(irc, msg, args, state, Type='index')
|
2004-10-19 13:56:34 +02:00
|
|
|
if state.args[-1] > 0:
|
|
|
|
state.args[-1] -= 1
|
|
|
|
|
2004-10-01 23:27:22 +02:00
|
|
|
def getId(irc, msg, args, state, kind=None):
|
2004-11-28 03:23:59 +01:00
|
|
|
Type = 'id'
|
2004-10-23 22:32:29 +02:00
|
|
|
if kind is not None and not kind.endswith('id'):
|
2004-11-28 03:23:59 +01:00
|
|
|
Type = kind + ' id'
|
2004-10-24 09:35:45 +02:00
|
|
|
original = args[0]
|
2004-10-23 22:32:29 +02:00
|
|
|
try:
|
|
|
|
args[0] = args[0].lstrip('#')
|
2004-11-28 03:23:59 +01:00
|
|
|
getInt(irc, msg, args, state, Type=Type)
|
2004-10-23 22:32:29 +02:00
|
|
|
except Exception, e:
|
|
|
|
args[0] = original
|
2004-10-27 09:50:47 +02:00
|
|
|
raise
|
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())
|
2004-09-23 18:13:00 +02:00
|
|
|
try:
|
2004-09-28 09:10:27 +02:00
|
|
|
expires = _int(args[0])
|
|
|
|
if expires:
|
|
|
|
expires += now
|
|
|
|
state.args.append(expires)
|
|
|
|
del args[0]
|
2004-09-23 18:13:00 +02:00
|
|
|
except ValueError:
|
2004-09-30 06:14:44 +02:00
|
|
|
irc.errorInvalid('number of seconds', args[0])
|
2004-09-23 18:13:00 +02:00
|
|
|
|
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):
|
2004-09-21 05:31:19 +02:00
|
|
|
if not conf.supybot.databases.plugins.channelSpecific():
|
2004-09-28 09:10:27 +02:00
|
|
|
state.args.append(None)
|
|
|
|
state.channel = None
|
2004-09-21 05:31:19 +02:00
|
|
|
else:
|
2004-09-28 09:10:27 +02:00
|
|
|
getChannel(irc, msg, args, state, **kwargs)
|
2004-09-21 05:31:19 +02:00
|
|
|
|
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)
|
2004-11-30 05:47:21 +01:00
|
|
|
if not irc.state.channels[state.channel].isOp(irc.nick):
|
2004-09-28 09:10:27 +02:00
|
|
|
irc.error('I need to be opped to %s.' % action, Raise=True)
|
2004-09-21 20:36:40 +02:00
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
def validChannel(irc, msg, args, state):
|
2004-10-15 13:18:55 +02:00
|
|
|
if irc.isChannel(args[0]):
|
2004-09-28 09:10:27 +02:00
|
|
|
state.args.append(args.pop(0))
|
2004-09-21 05:31:19 +02:00
|
|
|
else:
|
2004-09-28 09:10:27 +02:00
|
|
|
irc.errorInvalid('channel', args[0])
|
2004-09-21 05:31:19 +02:00
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
def getHostmask(irc, msg, args, state):
|
2004-09-23 18:13:00 +02:00
|
|
|
if ircutils.isUserHostmask(args[0]):
|
2004-09-28 09:10:27 +02:00
|
|
|
state.args.append(args.pop(0))
|
2004-09-23 18:13:00 +02:00
|
|
|
else:
|
|
|
|
try:
|
2004-09-28 09:10:27 +02:00
|
|
|
hostmask = irc.state.nickToHostmask(args[0])
|
|
|
|
state.args.append(hostmask)
|
|
|
|
del args[0]
|
2004-09-23 18:13:00 +02:00
|
|
|
except KeyError:
|
2004-09-28 09:10:27 +02:00
|
|
|
irc.errorInvalid('nick or hostmask', args[0])
|
2004-09-23 18:13:00 +02:00
|
|
|
|
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):
|
2004-09-21 05:31:19 +02:00
|
|
|
try:
|
2004-09-28 09:10:27 +02:00
|
|
|
state.args.append(ircdb.users.getUser(msg.prefix))
|
2004-09-21 05:31:19 +02:00
|
|
|
except KeyError:
|
|
|
|
irc.errorNotRegistered(Raise=True)
|
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
def getOtherUser(irc, msg, args, state):
|
2004-10-26 20:32:11 +02:00
|
|
|
if ircutils.isUserHostmask(args[0]):
|
|
|
|
irc.errorNoUser(args[0])
|
2004-09-21 05:31:19 +02:00
|
|
|
try:
|
2004-09-28 09:10:27 +02:00
|
|
|
state.args.append(ircdb.users.getUser(args[0]))
|
|
|
|
del args[0]
|
2004-09-21 05:31:19 +02:00
|
|
|
except KeyError:
|
|
|
|
try:
|
2004-10-26 23:01:05 +02:00
|
|
|
getHostmask(irc, msg, [args[0]], state)
|
2004-09-28 09:10:27 +02:00
|
|
|
hostmask = state.args.pop()
|
|
|
|
state.args.append(ircdb.users.getUser(hostmask))
|
2004-10-26 23:01:05 +02:00
|
|
|
del args[0]
|
2004-10-26 20:32:11 +02:00
|
|
|
except (KeyError, callbacks.Error):
|
2004-10-26 23:01:05 +02:00
|
|
|
irc.errorNoUser(name=args[0])
|
2004-09-21 05:31:19 +02:00
|
|
|
|
|
|
|
def _getRe(f):
|
2004-10-26 22:49:20 +02:00
|
|
|
def get(irc, msg, args, state, convert=True):
|
2004-09-28 09:10:27 +02:00
|
|
|
original = args[:]
|
2004-09-21 05:31:19 +02:00
|
|
|
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:
|
2004-10-19 18:43:09 +02:00
|
|
|
while len(s) < 512 and not isRe(s):
|
2004-09-28 09:10:27 +02:00
|
|
|
s += ' ' + args.pop(0)
|
2004-10-19 18:43:09 +02:00
|
|
|
if len(s) < 512:
|
2004-10-26 22:49:20 +02:00
|
|
|
if convert:
|
|
|
|
state.args.append(f(s))
|
|
|
|
else:
|
|
|
|
state.args.append(s)
|
2004-10-19 18:43:09 +02:00
|
|
|
else:
|
2004-10-22 21:50:32 +02:00
|
|
|
irc.errorInvalid('regular expression', s)
|
2004-09-28 09:10:27 +02:00
|
|
|
except IndexError:
|
|
|
|
args[:] = original
|
2004-10-22 21:50:32 +02:00
|
|
|
irc.errorInvalid('regular expression', s)
|
2004-09-21 05:31:19 +02:00
|
|
|
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]):
|
2004-09-21 05:31:19 +02:00
|
|
|
if 'nicklen' in irc.state.supported:
|
2004-09-28 09:10:27 +02:00
|
|
|
if len(args[0]) > irc.state.supported['nicklen']:
|
2004-10-19 14:01:33 +02:00
|
|
|
irc.errorInvalid('nick', args[0],
|
2004-09-28 09:10:27 +02:00
|
|
|
'That nick is too long for this server.')
|
|
|
|
state.args.append(args.pop(0))
|
2004-09-21 05:31:19 +02:00
|
|
|
else:
|
2004-09-28 09:10:27 +02:00
|
|
|
irc.errorInvalid('nick', s)
|
2004-09-21 05:31:19 +02:00
|
|
|
|
2004-10-02 22:12:48 +02:00
|
|
|
def getSeenNick(irc, msg, args, state, errmsg=None):
|
|
|
|
try:
|
|
|
|
_ = irc.state.nickToHostmask(args[0])
|
|
|
|
state.args.append(args.pop(0))
|
|
|
|
except KeyError:
|
|
|
|
if errmsg is None:
|
|
|
|
errmsg = 'I haven\'t seen %s.' % args[0]
|
|
|
|
irc.error(errmsg)
|
2004-10-06 06:32:03 +02:00
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
def getChannel(irc, msg, args, state):
|
2004-10-15 13:18:55 +02:00
|
|
|
if args and irc.isChannel(args[0]):
|
2004-09-21 05:31:19 +02:00
|
|
|
channel = args.pop(0)
|
2004-10-15 13:18:55 +02:00
|
|
|
elif irc.isChannel(msg.args[0]):
|
2004-09-21 05:31:19 +02:00
|
|
|
channel = msg.args[0]
|
|
|
|
else:
|
2004-09-28 09:10:27 +02:00
|
|
|
state.log.debug('Raising ArgumentError because there is no channel.')
|
2004-09-21 05:31:19 +02:00
|
|
|
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):
|
2004-10-02 18:52:26 +02:00
|
|
|
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-10-15 13:18:55 +02:00
|
|
|
def onlyInChannel(irc, msg, args, state):
|
|
|
|
if not (irc.isChannel(msg.args[0]) and msg.args[0] in irc.state.channels):
|
|
|
|
irc.error('This command may only be given in a channel that I am in.',
|
|
|
|
Raise=True)
|
|
|
|
else:
|
|
|
|
state.channel = msg.args[0]
|
|
|
|
state.args.append(state.channel)
|
|
|
|
|
2004-10-19 05:10:58 +02:00
|
|
|
def callerInGivenChannel(irc, msg, args, state):
|
2004-10-02 22:12:48 +02:00
|
|
|
channel = args[0]
|
2004-10-15 13:18:55 +02:00
|
|
|
if irc.isChannel(channel):
|
2004-10-02 22:12:48 +02:00
|
|
|
if channel in irc.state.channels:
|
|
|
|
if msg.nick in irc.state.channels[channel].users:
|
|
|
|
state.args.append(args.pop(0))
|
|
|
|
else:
|
|
|
|
irc.error('You must be in %s.' % channel, Raise=True)
|
|
|
|
else:
|
|
|
|
irc.error('I\'m not in %s.' % channel, Raise=True)
|
|
|
|
else:
|
|
|
|
irc.errorInvalid('channel', args[0])
|
|
|
|
|
2004-10-19 05:10:58 +02:00
|
|
|
def nickInChannel(irc, msg, args, state):
|
|
|
|
inChannel(irc, msg, args, state)
|
|
|
|
if args[0] not in irc.state.channels[state.channel].users:
|
|
|
|
irc.error('%s is not in %s.' % (args[0], state.channel), Raise=True)
|
|
|
|
state.args.append(args.pop(0))
|
|
|
|
|
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):
|
2004-10-02 18:52:26 +02:00
|
|
|
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)
|
2004-10-06 06:32:03 +02:00
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
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-23 18:13:00 +02:00
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
def private(irc, msg, args, state):
|
2004-10-15 13:18:55 +02:00
|
|
|
if irc.isChannel(msg.args[0]):
|
2004-09-28 09:10:27 +02:00
|
|
|
irc.errorRequiresPrivacy(Raise=True)
|
|
|
|
|
2004-10-02 19:31:58 +02:00
|
|
|
def public(irc, msg, args, state, errmsg=None):
|
2004-10-15 13:18:55 +02:00
|
|
|
if not irc.isChannel(msg.args[0]):
|
2004-10-02 19:31:58 +02:00
|
|
|
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):
|
|
|
|
irc.errorNoCapability(cap, Raise=True)
|
2004-10-06 06:32:03 +02:00
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
def anything(irc, msg, args, state):
|
|
|
|
state.args.append(args.pop(0))
|
2004-09-30 11:57:17 +02:00
|
|
|
|
2004-10-26 00:59:03 +02:00
|
|
|
def getGlob(irc, msg, args, state):
|
|
|
|
glob = args.pop(0)
|
|
|
|
if '*' not in glob and '?' not in glob:
|
|
|
|
glob = '*%s*' % glob
|
|
|
|
state.args.append(glob)
|
|
|
|
|
2004-09-30 11:57:17 +02:00
|
|
|
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
|
|
|
|
2004-10-23 22:54:28 +02:00
|
|
|
def getHttpUrl(irc, msg, args, state):
|
|
|
|
if webutils.urlRe.match(args[0]) and args[0].startswith('http://'):
|
|
|
|
state.args.append(args.pop(0))
|
|
|
|
else:
|
|
|
|
irc.errorInvalid('http url', args[0])
|
|
|
|
|
2004-10-01 23:27:22 +02:00
|
|
|
def getNow(irc, msg, args, state):
|
|
|
|
state.args.append(int(time.time()))
|
2004-10-02 18:52:26 +02:00
|
|
|
|
|
|
|
def getCommandName(irc, msg, args, state):
|
2004-11-11 17:37:47 +01:00
|
|
|
if ' ' in args[0]:
|
|
|
|
irc.errorInvalid('command name', args[0])
|
|
|
|
else:
|
|
|
|
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):
|
2004-10-27 01:17:58 +02:00
|
|
|
# ??? Should we allow abbreviations?
|
2004-10-02 19:31:58 +02:00
|
|
|
if isinstance(literals, basestring):
|
|
|
|
literals = (literals,)
|
2004-10-27 01:17:58 +02:00
|
|
|
abbrevs = utils.abbrev(literals)
|
|
|
|
if args[0] in abbrevs:
|
|
|
|
state.args.append(abbrevs[args.pop(0)])
|
2004-10-02 19:31:58 +02:00
|
|
|
elif errmsg is not None:
|
|
|
|
irc.error(errmsg, Raise=True)
|
|
|
|
else:
|
|
|
|
raise callbacks.ArgumentError
|
2004-10-02 22:12:48 +02:00
|
|
|
|
2004-11-21 14:18:58 +01:00
|
|
|
def getTo(irc, msg, args, state):
|
|
|
|
if args[0].lower() == 'to':
|
|
|
|
args.pop(0)
|
|
|
|
|
2004-10-02 22:12:48 +02:00
|
|
|
def getPlugin(irc, msg, args, state, require=True):
|
|
|
|
cb = irc.getCallback(args[0])
|
|
|
|
if cb is not None:
|
|
|
|
state.args.append(cb)
|
|
|
|
del args[0]
|
|
|
|
elif require:
|
|
|
|
irc.errorInvalid('plugin', args[0])
|
|
|
|
else:
|
|
|
|
state.args.append(None)
|
2004-10-06 06:32:03 +02:00
|
|
|
|
2004-10-19 13:56:34 +02:00
|
|
|
def getIrcColor(irc, msg, args, state):
|
|
|
|
if args[0] in ircutils.mircColors:
|
|
|
|
state.args.append(ircutils.mircColors[args.pop(0)])
|
|
|
|
else:
|
|
|
|
irc.errorInvalid('irc color')
|
|
|
|
|
2004-10-26 22:49:20 +02:00
|
|
|
def getText(irc, msg, args, state):
|
|
|
|
if args:
|
|
|
|
state.args.append(' '.join(args))
|
|
|
|
args[:] = []
|
|
|
|
else:
|
|
|
|
raise IndexError
|
|
|
|
|
2004-09-28 09:10:27 +02:00
|
|
|
wrappers = ircutils.IrcDict({
|
2004-09-21 05:31:19 +02:00
|
|
|
'id': getId,
|
2004-10-02 19:31:58 +02:00
|
|
|
'ip': getIp,
|
2004-09-21 05:31:19 +02:00
|
|
|
'int': getInt,
|
2004-10-19 13:56:34 +02:00
|
|
|
'index': getIndex,
|
|
|
|
'color': getIrcColor,
|
2004-10-01 23:27:22 +02:00
|
|
|
'now': getNow,
|
2004-09-30 11:57:17 +02:00
|
|
|
'url': getUrl,
|
2004-10-23 22:54:28 +02:00
|
|
|
'httpUrl': getHttpUrl,
|
2004-09-30 09:43:24 +02:00
|
|
|
'float': getFloat,
|
2004-11-28 03:23:59 +01:00
|
|
|
'long': getLong,
|
2004-09-30 09:43:24 +02:00
|
|
|
'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,
|
2004-09-23 18:13:00 +02:00
|
|
|
'expiry': getExpiry,
|
2004-10-02 19:31:58 +02:00
|
|
|
'literal': getLiteral,
|
2004-11-21 14:18:58 +01:00
|
|
|
'to': getTo,
|
2004-09-21 05:31:19 +02:00
|
|
|
'nick': getNick,
|
2004-10-02 22:12:48 +02:00
|
|
|
'seenNick': getSeenNick,
|
2004-09-23 00:43:23 +02:00
|
|
|
'channel': getChannel,
|
2004-10-02 04:21:26 +02:00
|
|
|
'inChannel': inChannel,
|
2004-10-15 13:18:55 +02:00
|
|
|
'onlyInChannel': onlyInChannel,
|
2004-10-19 05:10:58 +02:00
|
|
|
'nickInChannel': nickInChannel,
|
|
|
|
'callerInGivenChannel': callerInGivenChannel,
|
2004-09-21 20:36:40 +02:00
|
|
|
'plugin': getPlugin,
|
2004-09-23 18:13:00 +02:00
|
|
|
'boolean': getBoolean,
|
2004-09-21 05:31:19 +02:00
|
|
|
'lowered': getLowered,
|
2004-09-28 09:10:27 +02:00
|
|
|
'anything': anything,
|
2004-09-21 20:36:40 +02:00
|
|
|
'something': getSomething,
|
2004-10-14 10:41:08 +02:00
|
|
|
'filename': getSomething, # XXX Check for validity.
|
2004-10-02 18:52:26 +02:00
|
|
|
'commandName': getCommandName,
|
2004-10-26 22:49:20 +02:00
|
|
|
'text': getText,
|
2004-10-26 00:59:03 +02:00
|
|
|
'glob': getGlob,
|
2004-09-28 09:10:27 +02:00
|
|
|
'somethingWithoutSpaces': getSomethingNoSpaces,
|
2004-10-02 05:07:19 +02:00
|
|
|
'capability': getSomethingNoSpaces,
|
2004-09-23 00:43:23 +02:00
|
|
|
'channelDb': getChannelDb,
|
2004-09-21 05:31:19 +02:00
|
|
|
'hostmask': getHostmask,
|
2004-09-23 18:13:00 +02:00
|
|
|
'banmask': getBanmask,
|
2004-09-21 05:31:19 +02:00
|
|
|
'user': getUser,
|
2004-10-19 05:10:58 +02:00
|
|
|
'matches': getMatch,
|
2004-10-02 19:31:58 +02:00
|
|
|
'public': public,
|
2004-09-28 09:10:27 +02:00
|
|
|
'private': private,
|
2004-09-21 05:31:19 +02:00
|
|
|
'otherUser': getOtherUser,
|
|
|
|
'regexpMatcher': getMatcher,
|
2004-09-21 20:36:40 +02:00
|
|
|
'validChannel': validChannel,
|
2004-09-23 18:13:00 +02:00
|
|
|
'regexpReplacer': getReplacer,
|
2004-09-28 09:10:27 +02:00
|
|
|
'checkCapability': checkCapability,
|
|
|
|
'checkChannelCapability': checkChannelCapability,
|
2004-09-21 05:31:19 +02:00
|
|
|
})
|
|
|
|
|
2004-10-03 11:03:34 +02:00
|
|
|
def addConverter(name, wrapper):
|
2004-10-02 04:21:26 +02:00
|
|
|
wrappers[name] = wrapper
|
|
|
|
|
2004-10-19 05:10:58 +02:00
|
|
|
class UnknownConverter(KeyError):
|
|
|
|
pass
|
|
|
|
|
2004-10-03 11:03:34 +02:00
|
|
|
def getConverter(name):
|
2004-10-19 05:10:58 +02:00
|
|
|
try:
|
|
|
|
return wrappers[name]
|
|
|
|
except KeyError, e:
|
|
|
|
raise UnknownConverter, str(e)
|
2004-10-03 11:03:34 +02:00
|
|
|
|
|
|
|
def callConverter(name, irc, msg, args, state, *L):
|
|
|
|
getConverter(name)(irc, msg, args, state, *L)
|
|
|
|
|
2004-10-12 02:41:40 +02:00
|
|
|
###
|
|
|
|
# Contexts. These determine what the nature of conversions is; whether they're
|
|
|
|
# defaulted, or many of them are allowed, etc. Contexts should be reusable;
|
|
|
|
# i.e., they should not maintain state between calls.
|
|
|
|
###
|
|
|
|
def contextify(spec):
|
|
|
|
if not isinstance(spec, context):
|
|
|
|
spec = context(spec)
|
|
|
|
return spec
|
2004-10-02 22:12:48 +02:00
|
|
|
|
2004-10-13 16:37:52 +02:00
|
|
|
def setDefault(state, default):
|
|
|
|
if callable(default):
|
|
|
|
state.args.append(default())
|
|
|
|
else:
|
|
|
|
state.args.append(default)
|
|
|
|
|
2004-10-03 11:03:34 +02:00
|
|
|
class context(object):
|
|
|
|
def __init__(self, spec):
|
|
|
|
self.args = ()
|
2004-10-12 13:58:08 +02:00
|
|
|
self.spec = spec # for repr
|
2004-10-03 11:03:34 +02:00
|
|
|
if isinstance(spec, tuple):
|
|
|
|
assert spec, 'tuple spec must not be empty.'
|
|
|
|
self.args = spec[1:]
|
|
|
|
self.converter = getConverter(spec[0])
|
|
|
|
elif spec is None:
|
|
|
|
self.converter = getConverter('anything')
|
2004-10-28 19:08:23 +02:00
|
|
|
elif isinstance(spec, basestring):
|
2004-10-03 11:03:34 +02:00
|
|
|
self.args = ()
|
|
|
|
self.converter = getConverter(spec)
|
2004-10-28 19:08:23 +02:00
|
|
|
else:
|
|
|
|
assert isinstance(spec, context)
|
|
|
|
self.converter = spec
|
2004-10-03 11:03:34 +02:00
|
|
|
|
|
|
|
def __call__(self, irc, msg, args, state):
|
2004-10-12 13:58:08 +02:00
|
|
|
log.debug('args before %r: %r', self, args)
|
2004-10-03 11:03:34 +02:00
|
|
|
self.converter(irc, msg, args, state, *self.args)
|
2004-10-12 13:58:08 +02:00
|
|
|
log.debug('args after %r: %r', self, args)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<%s for %s>' % (self.__class__.__name__, self.spec)
|
2004-10-03 11:03:34 +02:00
|
|
|
|
2004-10-26 22:49:20 +02:00
|
|
|
class rest(context):
|
|
|
|
def __call__(self, irc, msg, args, state):
|
2004-11-08 21:51:39 +01:00
|
|
|
if args:
|
|
|
|
original = args[:]
|
|
|
|
args[:] = [' '.join(args)]
|
|
|
|
try:
|
|
|
|
super(rest, self).__call__(irc, msg, args, state)
|
|
|
|
except Exception, e:
|
|
|
|
args[:] = original
|
|
|
|
else:
|
|
|
|
raise callbacks.ArgumentError
|
2004-10-26 22:49:20 +02:00
|
|
|
|
2004-10-13 16:57:25 +02:00
|
|
|
# additional means: Look for this (and make sure it's of this type). If
|
|
|
|
# there are no arguments for us to check, then use our default.
|
2004-10-04 00:38:10 +02:00
|
|
|
class additional(context):
|
2004-10-19 05:10:58 +02:00
|
|
|
# XXX We should allow contexts as well as specs.
|
2004-10-04 00:38:10 +02:00
|
|
|
def __init__(self, spec, default=None):
|
|
|
|
self.__parent = super(additional, self)
|
|
|
|
self.__parent.__init__(spec)
|
|
|
|
self.default = default
|
|
|
|
|
|
|
|
def __call__(self, irc, msg, args, state):
|
|
|
|
try:
|
|
|
|
self.__parent.__call__(irc, msg, args, state)
|
|
|
|
except IndexError:
|
|
|
|
log.debug('Got IndexError, returning default.')
|
2004-10-13 16:37:52 +02:00
|
|
|
setDefault(state, self.default)
|
2004-10-04 00:38:10 +02:00
|
|
|
|
2004-10-13 16:57:25 +02:00
|
|
|
# optional means: Look for this, but if it's not the type I'm expecting or
|
|
|
|
# there are no arguments for us to check, then use the default value.
|
2004-10-04 00:38:10 +02:00
|
|
|
class optional(additional):
|
|
|
|
def __call__(self, irc, msg, args, state):
|
|
|
|
try:
|
2004-10-12 02:41:40 +02:00
|
|
|
super(optional, self).__call__(irc, msg, args, state)
|
2004-10-04 00:38:10 +02:00
|
|
|
except (callbacks.ArgumentError, callbacks.Error), e:
|
2004-10-12 02:41:40 +02:00
|
|
|
log.debug('Got %s, returning default.', utils.exnToString(e))
|
2004-10-13 16:37:52 +02:00
|
|
|
setDefault(state, self.default)
|
2004-10-04 00:38:10 +02:00
|
|
|
|
|
|
|
class any(context):
|
2004-11-30 06:15:41 +01:00
|
|
|
def __init__(self, spec, continueOnError=False):
|
|
|
|
self.__parent = super(any, self)
|
|
|
|
self.__parent.__init__(spec)
|
|
|
|
self.continueOnError = continueOnError
|
|
|
|
|
2004-10-04 00:38:10 +02:00
|
|
|
def __call__(self, irc, msg, args, state):
|
2004-10-14 10:41:08 +02:00
|
|
|
st = state.essence()
|
2004-10-04 00:38:10 +02:00
|
|
|
try:
|
2004-10-14 10:41:08 +02:00
|
|
|
while args:
|
2004-11-30 06:15:41 +01:00
|
|
|
self.__parent.__call__(irc, msg, args, st)
|
2004-10-14 10:41:08 +02:00
|
|
|
except IndexError:
|
|
|
|
pass
|
2004-11-30 06:15:41 +01:00
|
|
|
except (callbacks.ArgumentError, callbacks.Error), e:
|
|
|
|
if not self.continueOnError:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
log.debug('Got %s, returning default.', utils.exnToString(e))
|
|
|
|
pass
|
2004-10-14 10:41:08 +02:00
|
|
|
state.args.append(st.args)
|
2004-10-06 06:32:03 +02:00
|
|
|
|
2004-10-04 00:38:10 +02:00
|
|
|
class many(any):
|
|
|
|
def __call__(self, irc, msg, args, state):
|
|
|
|
super(many, self).__call__(irc, msg, args, state)
|
2004-10-14 10:41:08 +02:00
|
|
|
if not state.args[-1]:
|
|
|
|
state.args.pop()
|
|
|
|
raise callbacks.ArgumentError
|
2004-10-06 06:32:03 +02:00
|
|
|
|
2004-10-19 18:43:09 +02:00
|
|
|
class first(context):
|
|
|
|
def __init__(self, *specs, **kw):
|
|
|
|
if 'default' in kw:
|
|
|
|
self.default = kw.pop('default')
|
|
|
|
assert not kw, 'Bad kwargs for first.__init__'
|
|
|
|
self.specs = map(contextify, specs)
|
|
|
|
|
|
|
|
def __call__(self, irc, msg, args, state):
|
|
|
|
for spec in self.specs:
|
|
|
|
try:
|
|
|
|
spec(irc, msg, args, state)
|
|
|
|
return
|
|
|
|
except Exception, e:
|
|
|
|
continue
|
|
|
|
if hasattr(self, 'default'):
|
|
|
|
state.args.append(self.default)
|
|
|
|
else:
|
|
|
|
raise e
|
2004-10-24 00:07:50 +02:00
|
|
|
|
2004-10-24 05:53:56 +02:00
|
|
|
class reverse(context):
|
2004-10-24 00:07:50 +02:00
|
|
|
def __call__(self, irc, msg, args, state):
|
|
|
|
args[:] = args[::-1]
|
2004-10-24 05:53:56 +02:00
|
|
|
super(reverse, self).__call__(irc, msg, args, state)
|
2004-10-24 00:07:50 +02:00
|
|
|
args[:] = args[::-1]
|
2004-10-19 18:43:09 +02:00
|
|
|
|
2004-10-26 22:49:20 +02:00
|
|
|
class commalist(context):
|
|
|
|
def __call__(self, irc, msg, args, state):
|
|
|
|
original = args[:]
|
|
|
|
st = state.essence()
|
|
|
|
trailingComma = True
|
|
|
|
try:
|
|
|
|
while trailingComma:
|
|
|
|
arg = args.pop(0)
|
|
|
|
if not arg.endswith(','):
|
|
|
|
trailingComma = False
|
|
|
|
for part in arg.split(','):
|
|
|
|
if part: # trailing commas
|
|
|
|
super(commalist, self).__call__(irc, msg, [part], st)
|
|
|
|
state.args.append(st.args)
|
|
|
|
except Exception, e:
|
|
|
|
args[:] = original
|
|
|
|
raise
|
2004-11-28 03:23:59 +01:00
|
|
|
|
2004-10-12 02:41:40 +02:00
|
|
|
class getopts(context):
|
|
|
|
"""The empty string indicates that no argument is taken; None indicates
|
|
|
|
that there is no converter for the argument."""
|
|
|
|
def __init__(self, getopts):
|
2004-10-12 13:58:08 +02:00
|
|
|
self.spec = getopts # for repr
|
2004-10-12 02:41:40 +02:00
|
|
|
self.getopts = {}
|
|
|
|
self.getoptL = []
|
|
|
|
for (name, spec) in getopts.iteritems():
|
|
|
|
if spec == '':
|
|
|
|
self.getoptL.append(name)
|
|
|
|
self.getopts[name] = None
|
2004-09-28 09:10:27 +02:00
|
|
|
else:
|
2004-10-12 02:41:40 +02:00
|
|
|
self.getoptL.append(name + '=')
|
|
|
|
self.getopts[name] = contextify(spec)
|
2004-10-12 13:58:08 +02:00
|
|
|
log.debug('getopts: %r', self.getopts)
|
|
|
|
log.debug('getoptL: %r', self.getoptL)
|
2004-09-22 13:51:06 +02:00
|
|
|
|
2004-10-12 02:41:40 +02:00
|
|
|
def __call__(self, irc, msg, args, state):
|
2004-10-12 13:58:08 +02:00
|
|
|
log.debug('args before %r: %r', self, args)
|
|
|
|
(optlist, rest) = getopt.getopt(args, '', self.getoptL)
|
2004-10-12 02:41:40 +02:00
|
|
|
getopts = []
|
2004-09-22 13:51:06 +02:00
|
|
|
for (opt, arg) in optlist:
|
|
|
|
opt = opt[2:] # Strip --
|
2004-10-12 13:58:08 +02:00
|
|
|
log.debug('opt: %r, arg: %r', opt, arg)
|
2004-10-12 02:41:40 +02:00
|
|
|
context = self.getopts[opt]
|
|
|
|
if context is not None:
|
|
|
|
st = state.essence()
|
2004-10-12 13:58:08 +02:00
|
|
|
context(irc, msg, [arg], st)
|
2004-10-12 02:41:40 +02:00
|
|
|
assert len(st.args) == 1
|
|
|
|
getopts.append((opt, st.args[0]))
|
2004-09-22 13:51:06 +02:00
|
|
|
else:
|
2004-10-12 02:41:40 +02:00
|
|
|
getopts.append((opt, True))
|
|
|
|
state.args.append(getopts)
|
2004-10-12 13:58:08 +02:00
|
|
|
args[:] = rest
|
|
|
|
log.debug('args after %r: %r', self, args)
|
2004-11-28 03:23:59 +01:00
|
|
|
|
2004-10-12 02:41:40 +02:00
|
|
|
###
|
|
|
|
# This is our state object, passed to converters along with irc, msg, and args.
|
|
|
|
###
|
2004-10-14 10:41:08 +02:00
|
|
|
|
2004-10-12 02:41:40 +02:00
|
|
|
class State(object):
|
|
|
|
log = log
|
2004-10-14 10:41:08 +02:00
|
|
|
def __init__(self, types):
|
2004-10-12 02:41:40 +02:00
|
|
|
self.args = []
|
|
|
|
self.kwargs = {}
|
2004-10-14 10:41:08 +02:00
|
|
|
self.types = types
|
2004-10-12 02:41:40 +02:00
|
|
|
self.channel = None
|
|
|
|
|
|
|
|
def essence(self):
|
2004-10-14 10:41:08 +02:00
|
|
|
st = State(self.types)
|
2004-10-12 02:41:40 +02:00
|
|
|
for (attr, value) in self.__dict__.iteritems():
|
2004-11-30 06:15:41 +01:00
|
|
|
if attr not in ('args', 'kwargs'):
|
2004-10-12 02:41:40 +02:00
|
|
|
setattr(st, attr, value)
|
|
|
|
return st
|
|
|
|
|
2004-10-14 10:41:08 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return '%s(args=%r, kwargs=%r, channel=%r)' % (self.__class__.__name__,
|
|
|
|
self.args, self.kwargs,
|
|
|
|
self.channel)
|
2004-11-28 03:23:59 +01:00
|
|
|
|
2004-10-14 10:41:08 +02:00
|
|
|
|
2004-10-12 02:41:40 +02:00
|
|
|
###
|
|
|
|
# This is a compiled Spec object.
|
|
|
|
###
|
|
|
|
class Spec(object):
|
2004-10-14 10:41:08 +02:00
|
|
|
def _state(self, types, attrs={}):
|
|
|
|
st = State(types)
|
2004-10-12 02:41:40 +02:00
|
|
|
st.__dict__.update(attrs)
|
2004-10-19 03:43:58 +02:00
|
|
|
st.allowExtra = self.allowExtra
|
2004-10-12 02:41:40 +02:00
|
|
|
return st
|
2004-10-13 16:37:52 +02:00
|
|
|
|
2004-10-14 10:41:08 +02:00
|
|
|
def __init__(self, types, allowExtra=False):
|
2004-10-12 02:41:40 +02:00
|
|
|
self.types = types
|
|
|
|
self.allowExtra = allowExtra
|
|
|
|
utils.mapinto(contextify, self.types)
|
|
|
|
|
|
|
|
def __call__(self, irc, msg, args, stateAttrs={}):
|
2004-10-15 13:18:55 +02:00
|
|
|
state = self._state(self.types[:], stateAttrs)
|
|
|
|
while state.types:
|
|
|
|
context = state.types.pop(0)
|
2004-10-19 03:43:58 +02:00
|
|
|
try:
|
|
|
|
context(irc, msg, args, state)
|
|
|
|
except IndexError:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
if args and not state.allowExtra:
|
2004-10-14 10:41:08 +02:00
|
|
|
log.debug('args and not self.allowExtra: %r', args)
|
2004-10-12 02:41:40 +02:00
|
|
|
raise callbacks.ArgumentError
|
|
|
|
return state
|
|
|
|
|
2004-10-22 07:56:55 +02:00
|
|
|
def wrap(f, specList=[], **kw):
|
2004-10-12 02:41:40 +02:00
|
|
|
spec = Spec(specList, **kw)
|
2004-09-22 13:51:06 +02:00
|
|
|
def newf(self, irc, msg, args, **kwargs):
|
2004-10-13 16:37:52 +02:00
|
|
|
state = spec(irc, msg, args, stateAttrs={'cb': self, 'log': self.log})
|
2004-10-27 09:50:47 +02:00
|
|
|
self.log.debug('State before call: %s' % state)
|
2004-10-12 02:41:40 +02:00
|
|
|
f(self, irc, msg, args, *state.args, **state.kwargs)
|
2004-10-22 07:56:55 +02:00
|
|
|
return utils.changeFunctionName(newf, f.func_name, f.__doc__)
|
2004-09-23 18:13:00 +02:00
|
|
|
|
2004-09-21 05:31:19 +02:00
|
|
|
|
2004-10-26 22:49:20 +02:00
|
|
|
__all__ = [
|
|
|
|
# Contexts.
|
|
|
|
'any', 'many',
|
|
|
|
'optional', 'additional',
|
|
|
|
'rest', 'getopts',
|
|
|
|
'first', 'reverse',
|
|
|
|
'commalist',
|
|
|
|
# Converter helpers.
|
|
|
|
'getConverter', 'addConverter', 'callConverter',
|
|
|
|
# Decorators.
|
|
|
|
'urlSnarfer', 'thread',
|
|
|
|
# Functions.
|
|
|
|
'wrap',
|
|
|
|
# Stuff for testing.
|
|
|
|
'Spec',
|
|
|
|
]
|
|
|
|
|
|
|
|
# This doesn't work. Suck.
|
|
|
|
## if world.testing:
|
|
|
|
## __all__.append('Spec')
|
2004-10-12 02:41:40 +02:00
|
|
|
|
2004-10-27 03:19:39 +02:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|