2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, 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.
|
|
|
|
###
|
|
|
|
|
2003-10-04 16:57:55 +02:00
|
|
|
"""
|
|
|
|
Includes various accessories for callbacks.Privmsg based callbacks.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:38:19 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:47:19 +02:00
|
|
|
import fix
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-11-08 01:10:09 +01:00
|
|
|
import time
|
2003-10-29 14:06:17 +01:00
|
|
|
import types
|
2003-11-17 07:01:50 +01:00
|
|
|
import threading
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import conf
|
|
|
|
import ircdb
|
2003-11-17 07:01:50 +01:00
|
|
|
import world
|
2003-03-12 07:26:59 +01:00
|
|
|
import ircutils
|
|
|
|
import callbacks
|
2003-11-08 01:10:09 +01:00
|
|
|
import structures
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def getChannel(msg, args):
|
|
|
|
"""Returns the channel the msg came over or the channel given in args.
|
|
|
|
|
|
|
|
If the channel was given in args, args is modified (the channel is
|
|
|
|
removed).
|
|
|
|
"""
|
2003-04-11 23:10:17 +02:00
|
|
|
if args and ircutils.isChannel(args[0]):
|
2004-01-01 21:10:52 +01:00
|
|
|
if conf.requireChannelCommandsToBeSentInChannel:
|
|
|
|
if args[0] != msg.args[0]:
|
|
|
|
s = 'Channel commands must be sent in the channel to which ' \
|
|
|
|
'they apply.'
|
|
|
|
raise callbacks.Error, s
|
2003-04-11 23:10:17 +02:00
|
|
|
return args.pop(0)
|
|
|
|
elif ircutils.isChannel(msg.args[0]):
|
2003-03-12 07:26:59 +01:00
|
|
|
return msg.args[0]
|
|
|
|
else:
|
2003-04-11 23:10:17 +02:00
|
|
|
raise callbacks.Error, 'Command must be sent in a channel or ' \
|
|
|
|
'include a channel in its arguments.'
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-11-11 14:20:06 +01:00
|
|
|
def getArgs(args, required=1, optional=0):
|
|
|
|
"""Take the required/optional arguments from args.
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-11-11 14:20:06 +01:00
|
|
|
Always returns a list of size required + optional, filling it with however
|
2003-12-09 16:58:05 +01:00
|
|
|
many empty strings is necessary to fill the tuple to the right size. If
|
|
|
|
there is only one argument, a string containing that argument is returned.
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-11-11 14:20:06 +01:00
|
|
|
If there aren't enough args even to satisfy required, raise an error and
|
2003-03-12 07:26:59 +01:00
|
|
|
let the caller handle sending the help message.
|
|
|
|
"""
|
2003-12-08 02:10:27 +01:00
|
|
|
assert not isinstance(args, str), 'args should be a list.'
|
2003-11-11 14:20:06 +01:00
|
|
|
if len(args) < required:
|
2003-04-01 00:22:59 +02:00
|
|
|
raise callbacks.ArgumentError
|
2003-11-11 14:20:06 +01:00
|
|
|
if len(args) < required + optional:
|
|
|
|
ret = list(args) + ([''] * (required + optional - len(args)))
|
|
|
|
elif len(args) >= required + optional:
|
|
|
|
ret = list(args[:required + optional - 1])
|
|
|
|
ret.append(' '.join(args[required + optional - 1:]))
|
2003-03-12 07:26:59 +01:00
|
|
|
if len(ret) == 1:
|
|
|
|
return ret[0]
|
|
|
|
else:
|
|
|
|
return ret
|
|
|
|
|
2003-03-28 02:16:46 +01:00
|
|
|
def checkCapability(f, capability):
|
2003-08-25 22:19:42 +02:00
|
|
|
"""Makes sure a user has a certain capability before a command will run."""
|
2003-03-28 02:16:46 +01:00
|
|
|
def newf(self, irc, msg, args):
|
|
|
|
if ircdb.checkCapability(msg.prefix, capability):
|
|
|
|
f(self, irc, msg, args)
|
|
|
|
else:
|
2003-12-04 09:11:34 +01:00
|
|
|
self.log.warning('%r attempted %s without %s.',
|
|
|
|
msg.prefix, f.func_name, capability)
|
2004-01-08 16:24:56 +01:00
|
|
|
irc.errorNoCapability(capability)
|
2003-10-29 14:06:17 +01:00
|
|
|
newf = types.FunctionType(newf.func_code, newf.func_globals,
|
|
|
|
f.func_name, closure=newf.func_closure)
|
2003-03-28 02:16:46 +01:00
|
|
|
newf.__doc__ = f.__doc__
|
|
|
|
return newf
|
2003-03-27 07:02:37 +01:00
|
|
|
|
2003-09-18 00:38:03 +02:00
|
|
|
def checkChannelCapability(f, capability):
|
2003-09-29 07:41:03 +02:00
|
|
|
"""Makes sure a user has a certain channel capability before running f.
|
|
|
|
|
|
|
|
Do note that you need to add a "channel" argument to your argument list.
|
|
|
|
"""
|
2003-09-18 01:31:45 +02:00
|
|
|
def newf(self, irc, msg, args, *L):
|
2003-10-15 05:32:18 +02:00
|
|
|
channel = getChannel(msg, args)
|
2003-09-18 00:38:03 +02:00
|
|
|
chancap = ircdb.makeChannelCapability(channel, capability)
|
|
|
|
if ircdb.checkCapability(msg.prefix, chancap):
|
2003-09-18 01:31:45 +02:00
|
|
|
L += (channel,)
|
2003-10-29 14:06:17 +01:00
|
|
|
ff = types.MethodType(f, self, self.__class__)
|
2003-09-18 01:31:45 +02:00
|
|
|
ff(irc, msg, args, *L)
|
2003-09-18 00:38:03 +02:00
|
|
|
else:
|
2003-12-04 09:11:34 +01:00
|
|
|
self.log.warning('%r attempted %s without %s.',
|
|
|
|
msg.prefix, f.func_name, capability)
|
2004-01-08 16:24:56 +01:00
|
|
|
irc.errorNoCapability(chancap)
|
2003-10-29 14:06:17 +01:00
|
|
|
newf = types.FunctionType(newf.func_code, newf.func_globals,
|
|
|
|
f.func_name, closure=newf.func_closure)
|
2003-09-18 00:38:03 +02:00
|
|
|
newf.__doc__ = f.__doc__
|
|
|
|
return newf
|
|
|
|
|
2003-08-25 22:19:42 +02:00
|
|
|
def thread(f):
|
|
|
|
"""Makes sure a command spawns a thread when called."""
|
2003-09-18 01:31:45 +02:00
|
|
|
def newf(self, irc, msg, args, *L):
|
2003-10-29 14:06:17 +01:00
|
|
|
ff = types.MethodType(f, self, self.__class__)
|
2003-09-18 01:31:45 +02:00
|
|
|
t = callbacks.CommandThread(self.callCommand, ff, irc, msg, args, *L)
|
2003-08-25 22:19:42 +02:00
|
|
|
t.start()
|
2003-10-29 14:06:17 +01:00
|
|
|
newf = types.FunctionType(newf.func_code, newf.func_globals,
|
|
|
|
f.func_name, closure=newf.func_closure)
|
2003-08-25 22:19:42 +02:00
|
|
|
newf.__doc__ = f.__doc__
|
|
|
|
return newf
|
|
|
|
|
2003-09-18 00:38:03 +02:00
|
|
|
def name(f):
|
|
|
|
"""Makes sure a name is available based on conf.requireRegistration."""
|
2003-09-18 01:31:45 +02:00
|
|
|
def newf(self, irc, msg, args, *L):
|
2003-09-18 00:38:03 +02:00
|
|
|
try:
|
|
|
|
name = ircdb.users.getUser(msg.prefix).name
|
|
|
|
except KeyError:
|
|
|
|
if conf.requireRegistration:
|
2004-01-08 22:49:10 +01:00
|
|
|
irc.errorNotRegistered()
|
2003-09-18 00:38:03 +02:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
name = msg.prefix
|
2003-09-18 01:31:45 +02:00
|
|
|
L = (name,) + L
|
2003-10-29 14:06:17 +01:00
|
|
|
ff = types.MethodType(f, self, self.__class__)
|
2003-09-18 01:31:45 +02:00
|
|
|
ff(irc, msg, args, *L)
|
2003-10-29 14:06:17 +01:00
|
|
|
newf = types.FunctionType(newf.func_code, newf.func_globals,
|
|
|
|
f.func_name, closure=newf.func_closure)
|
2003-09-18 00:38:03 +02:00
|
|
|
newf.__doc__ = f.__doc__
|
|
|
|
return newf
|
|
|
|
|
|
|
|
def channel(f):
|
|
|
|
"""Gives the command an extra channel arg as if it had called getChannel"""
|
2003-09-18 01:31:45 +02:00
|
|
|
def newf(self, irc, msg, args, *L):
|
2003-09-18 00:38:03 +02:00
|
|
|
channel = getChannel(msg, args)
|
2003-09-18 01:31:45 +02:00
|
|
|
L = (channel,) + L
|
2003-10-29 14:06:17 +01:00
|
|
|
ff = types.MethodType(f, self, self.__class__)
|
2003-09-18 01:31:45 +02:00
|
|
|
ff(irc, msg, args, *L)
|
2003-10-29 14:06:17 +01:00
|
|
|
newf = types.FunctionType(newf.func_code, newf.func_globals,
|
|
|
|
f.func_name, closure=newf.func_closure)
|
2003-09-18 00:38:03 +02:00
|
|
|
newf.__doc__ = f.__doc__
|
|
|
|
return newf
|
2003-11-07 13:40:18 +01:00
|
|
|
|
|
|
|
def urlSnarfer(f):
|
|
|
|
"""Protects the snarfer from loops and whatnot."""
|
|
|
|
def newf(self, irc, msg, match, *L):
|
|
|
|
now = time.time()
|
|
|
|
cutoff = now - conf.snarfThrottle
|
2003-11-07 15:10:28 +01:00
|
|
|
q = getattr(self, '_snarfedUrls', None)
|
|
|
|
if q is None:
|
2003-11-08 01:10:09 +01:00
|
|
|
q = structures.smallqueue()
|
2003-11-17 08:27:21 +01:00
|
|
|
self._snarfedUrls = q
|
2003-11-07 15:10:28 +01:00
|
|
|
while q and q[0][2] < cutoff:
|
|
|
|
q.dequeue()
|
2003-11-07 13:40:18 +01:00
|
|
|
url = match.group(0)
|
2003-11-08 01:10:09 +01:00
|
|
|
if any(lambda t: t[0] == url and t[1] == msg.args[0], q) and \
|
|
|
|
not world.testing:
|
2003-12-04 09:11:34 +01:00
|
|
|
self.log.warning('Refusing to snarf %s from %r.', url, msg.prefix)
|
2003-11-07 13:40:18 +01:00
|
|
|
else:
|
2003-11-07 15:10:28 +01:00
|
|
|
q.enqueue((url, msg.args[0], now))
|
2003-11-17 07:01:50 +01:00
|
|
|
if self.threaded:
|
|
|
|
f(self, irc, msg, match, *L)
|
|
|
|
else:
|
|
|
|
L = list(L)
|
|
|
|
t = threading.Thread(target=f, args=[self, irc, msg, match]+L)
|
|
|
|
t.setDaemon(True)
|
|
|
|
t.start()
|
|
|
|
world.threadsSpawned += 1
|
2003-11-07 13:40:18 +01:00
|
|
|
newf = types.FunctionType(newf.func_code, newf.func_globals,
|
|
|
|
f.func_name, closure=newf.func_closure)
|
|
|
|
newf.__doc__ = f.__doc__
|
|
|
|
return newf
|
2003-09-18 00:38:03 +02:00
|
|
|
|
|
|
|
|
2003-03-27 08:26:46 +01:00
|
|
|
class CapabilityCheckingPrivmsg(callbacks.Privmsg):
|
2003-09-06 02:37:28 +02:00
|
|
|
"""A small subclass of callbacks.Privmsg that checks self.capability
|
|
|
|
before allowing any command to be called.
|
|
|
|
"""
|
2003-03-27 12:33:05 +01:00
|
|
|
capability = '' # To satisfy PyChecker
|
2003-03-27 07:02:37 +01:00
|
|
|
def callCommand(self, f, irc, msg, args):
|
|
|
|
if ircdb.checkCapability(msg.prefix, self.capability):
|
|
|
|
callbacks.Privmsg.callCommand(self, f, irc, msg, args)
|
|
|
|
else:
|
2003-12-04 09:11:34 +01:00
|
|
|
self.log.warning('%r tried to call %s without %s.',
|
|
|
|
msg.prefix, f.im_func.func_name, self.capability)
|
2004-01-08 16:24:56 +01:00
|
|
|
irc.errorNoCapability(self.capability)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-09-17 10:12:59 +02:00
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|