2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-03-12 07:26:59 +01:00
|
|
|
# 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-07-21 08:10:35 +02:00
|
|
|
"""
|
|
|
|
This module contains the basic callbacks for handling PRIVMSGs. Both Privmsg
|
|
|
|
and PrivmsgRegexp classes are provided; for offering callbacks based on
|
|
|
|
commands and their arguments (much like *nix command line programs) use the
|
|
|
|
Privmsg class; for offering callbacks based on regular expressions, use the
|
|
|
|
PrivmsgRegexp class. Read their respective docstrings for more information on
|
|
|
|
how to use them.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:38:19 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.fix as fix
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import re
|
2003-10-20 06:26:37 +02:00
|
|
|
import copy
|
2003-08-11 05:34:54 +02:00
|
|
|
import sets
|
2003-03-12 07:26:59 +01:00
|
|
|
import time
|
|
|
|
import shlex
|
2003-08-26 15:44:32 +02:00
|
|
|
import getopt
|
2003-10-04 14:29:58 +02:00
|
|
|
import string
|
2003-03-12 07:26:59 +01:00
|
|
|
import inspect
|
|
|
|
import threading
|
|
|
|
from cStringIO import StringIO
|
2004-05-07 13:30:42 +02:00
|
|
|
from itertools import imap, ifilter
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-07-24 07:18:26 +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.irclib as irclib
|
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.ircutils as ircutils
|
2004-07-26 07:39:44 +02:00
|
|
|
import supybot.registry as registry
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-08-26 01:03:42 +02:00
|
|
|
def addressed(nick, msg, prefixChars=None,
|
|
|
|
prefixStrings=None, whenAddressedByNick=None):
|
2003-03-12 07:26:59 +01:00
|
|
|
"""If msg is addressed to 'name', returns the portion after the address.
|
2003-09-07 06:05:34 +02:00
|
|
|
Otherwise returns the empty string.
|
2003-03-12 07:26:59 +01:00
|
|
|
"""
|
2004-08-02 13:19:58 +02:00
|
|
|
def get(group):
|
|
|
|
if ircutils.isChannel(target):
|
|
|
|
group = group.get(target)
|
|
|
|
return group()
|
2004-08-26 01:03:42 +02:00
|
|
|
def stripPrefixStrings(payload):
|
|
|
|
for prefixString in prefixStrings:
|
|
|
|
if payload.startswith(prefixString):
|
|
|
|
payload = payload[len(prefixString):].lstrip()
|
|
|
|
return payload
|
|
|
|
|
|
|
|
assert msg.command == 'PRIVMSG'
|
|
|
|
(target, payload) = msg.args
|
2004-05-06 17:48:09 +02:00
|
|
|
if prefixChars is None:
|
2004-08-26 01:03:42 +02:00
|
|
|
prefixChars = get(conf.supybot.reply.whenAddressedBy.chars)
|
2004-05-06 17:48:09 +02:00
|
|
|
if whenAddressedByNick is None:
|
2004-08-26 01:03:42 +02:00
|
|
|
whenAddressedByNick = get(conf.supybot.reply.whenAddressedBy.nick)
|
|
|
|
if prefixStrings is None:
|
|
|
|
prefixStrings = get(conf.supybot.reply.whenAddressedBy.strings)
|
2003-10-09 00:38:27 +02:00
|
|
|
nick = ircutils.toLower(nick)
|
2004-05-06 17:48:09 +02:00
|
|
|
# Ok, let's see if it's a private message.
|
|
|
|
if ircutils.nickEqual(target, nick):
|
2004-08-26 01:03:42 +02:00
|
|
|
payload = stripPrefixStrings(payload)
|
|
|
|
while payload and payload[0] in prefixChars:
|
|
|
|
payload = payload[1:].lstrip()
|
|
|
|
return payload
|
2004-05-06 17:48:09 +02:00
|
|
|
# Ok, not private. Does it start with our nick?
|
|
|
|
elif whenAddressedByNick and \
|
|
|
|
ircutils.toLower(payload).startswith(nick):
|
2003-03-12 07:26:59 +01:00
|
|
|
try:
|
2004-05-06 17:48:09 +02:00
|
|
|
(maybeNick, rest) = payload.split(None, 1)
|
|
|
|
while not ircutils.isNick(maybeNick, strictRfc=True):
|
2004-02-02 03:36:53 +01:00
|
|
|
if maybeNick[-1].isalnum():
|
|
|
|
return ''
|
2003-09-25 18:07:41 +02:00
|
|
|
maybeNick = maybeNick[:-1]
|
2003-10-09 00:38:27 +02:00
|
|
|
if ircutils.nickEqual(maybeNick, nick):
|
2003-09-25 18:07:41 +02:00
|
|
|
return rest
|
|
|
|
else:
|
|
|
|
return ''
|
2003-09-25 18:09:18 +02:00
|
|
|
except ValueError: # split didn't work.
|
2003-03-12 07:26:59 +01:00
|
|
|
return ''
|
2004-08-26 01:03:42 +02:00
|
|
|
elif payload and any(payload.startswith, prefixStrings):
|
|
|
|
return stripPrefixStrings(payload)
|
2004-05-06 17:48:09 +02:00
|
|
|
elif payload and payload[0] in prefixChars:
|
|
|
|
return payload[1:].strip()
|
2004-01-18 08:58:26 +01:00
|
|
|
elif conf.supybot.reply.whenNotAddressed():
|
2004-05-06 17:48:09 +02:00
|
|
|
return payload
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def canonicalName(command):
|
|
|
|
"""Turn a command into its canonical form.
|
|
|
|
|
|
|
|
Currently, this makes everything lowercase and removes all dashes and
|
|
|
|
underscores.
|
|
|
|
"""
|
2004-02-13 05:43:39 +01:00
|
|
|
if isinstance(command, unicode):
|
|
|
|
command = command.encode('utf-8')
|
2003-11-15 04:01:01 +01:00
|
|
|
special = '\t -_'
|
|
|
|
reAppend = ''
|
|
|
|
while command and command[-1] in special:
|
|
|
|
reAppend = command[-1] + reAppend
|
|
|
|
command = command[:-1]
|
|
|
|
return command.translate(string.ascii, special).lower() + reAppend
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-08-08 02:41:13 +02:00
|
|
|
def reply(msg, s, prefixName=True, private=None,
|
2004-08-08 19:48:33 +02:00
|
|
|
notice=None, to=None, action=None, error=False):
|
2004-04-09 04:55:03 +02:00
|
|
|
# Ok, let's make the target:
|
|
|
|
target = ircutils.replyTo(msg)
|
2004-08-08 19:48:33 +02:00
|
|
|
def getConfig(wrapper):
|
|
|
|
if ircutils.isChannel(target):
|
|
|
|
return wrapper.get(target)()
|
|
|
|
else:
|
|
|
|
return wrapper()
|
|
|
|
if ircutils.isChannel(target):
|
|
|
|
channel = target
|
|
|
|
else:
|
|
|
|
channel = None
|
2004-08-08 19:34:59 +02:00
|
|
|
if notice is None:
|
2004-08-08 19:48:33 +02:00
|
|
|
notice = getConfig(conf.supybot.reply.withNotice)
|
2004-08-08 19:34:59 +02:00
|
|
|
if private is None:
|
2004-08-08 19:48:33 +02:00
|
|
|
private = getConfig(conf.supybot.reply.inPrivate)
|
|
|
|
if error:
|
|
|
|
notice = getConfig(conf.supybot.reply.errorWithNotice) or notice
|
|
|
|
private = getConfig(conf.supybot.reply.errorInPrivate) or private
|
|
|
|
s = 'Error: ' + s
|
2004-06-10 01:48:33 +02:00
|
|
|
if private:
|
2004-08-03 06:44:12 +02:00
|
|
|
prefixName = False
|
2004-08-08 19:34:59 +02:00
|
|
|
if to is None:
|
2004-06-10 01:48:33 +02:00
|
|
|
target = msg.nick
|
2004-08-08 19:34:59 +02:00
|
|
|
else:
|
|
|
|
target = to
|
|
|
|
# XXX: User value for reply.withNoticeWhenPrivate.
|
2004-08-24 21:48:01 +02:00
|
|
|
if getConfig(conf.supybot.reply.withNoticeWhenPrivate):
|
|
|
|
notice = True
|
2004-04-09 04:55:03 +02:00
|
|
|
if to is None:
|
|
|
|
to = msg.nick
|
|
|
|
# Ok, now let's make the payload:
|
|
|
|
s = ircutils.safeArgument(s)
|
2004-08-01 16:38:37 +02:00
|
|
|
if not s and not action:
|
2004-04-09 04:55:03 +02:00
|
|
|
s = 'Error: I tried to send you an empty message.'
|
2004-07-23 06:50:32 +02:00
|
|
|
if prefixName and ircutils.isChannel(target):
|
2004-08-08 19:34:59 +02:00
|
|
|
# Let's may sure we don't do, "#channel: foo.".
|
2004-07-23 06:50:32 +02:00
|
|
|
if not ircutils.isChannel(to):
|
|
|
|
s = '%s: %s' % (to, s)
|
2004-04-09 04:55:03 +02:00
|
|
|
# And now, let's decide whether it's a PRIVMSG or a NOTICE.
|
|
|
|
msgmaker = ircmsgs.privmsg
|
2004-07-07 20:04:55 +02:00
|
|
|
if notice:
|
2004-04-09 04:55:03 +02:00
|
|
|
msgmaker = ircmsgs.notice
|
2004-08-08 19:34:59 +02:00
|
|
|
# We don't use elif here because actions can't be sent as NOTICEs.
|
2004-04-09 04:55:03 +02:00
|
|
|
if action:
|
|
|
|
msgmaker = ircmsgs.action
|
|
|
|
# Finally, we'll return the actual message.
|
|
|
|
return msgmaker(target, s)
|
|
|
|
|
2004-08-08 19:48:33 +02:00
|
|
|
def error(msg, s, **kwargs):
|
2003-08-23 06:42:04 +02:00
|
|
|
"""Makes an error reply to msg with the appropriate error payload."""
|
2004-08-08 19:48:33 +02:00
|
|
|
kwargs['error'] = True
|
|
|
|
return reply(msg, s, **kwargs)
|
2003-08-23 06:42:04 +02:00
|
|
|
|
2003-10-24 13:31:09 +02:00
|
|
|
def getHelp(method, name=None):
|
|
|
|
if name is None:
|
|
|
|
name = method.__name__
|
|
|
|
doclines = method.__doc__.splitlines()
|
2003-10-25 01:14:27 +02:00
|
|
|
s = '%s %s' % (name, doclines.pop(0))
|
2003-10-24 13:31:09 +02:00
|
|
|
if doclines:
|
|
|
|
help = ' '.join(doclines)
|
2003-10-24 13:47:45 +02:00
|
|
|
s = '(%s) -- %s' % (ircutils.bold(s), help)
|
2003-12-03 23:02:29 +01:00
|
|
|
return utils.normalizeWhitespace(s)
|
2003-10-24 13:31:09 +02:00
|
|
|
|
|
|
|
def getSyntax(method, name=None):
|
|
|
|
if name is None:
|
|
|
|
name = method.__name__
|
|
|
|
doclines = method.__doc__.splitlines()
|
2003-10-25 01:14:27 +02:00
|
|
|
return '%s %s' % (name, doclines[0])
|
2003-10-24 13:31:09 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
class Error(Exception):
|
|
|
|
"""Generic class for errors in Privmsg callbacks."""
|
|
|
|
pass
|
|
|
|
|
2003-04-01 00:22:59 +02:00
|
|
|
class ArgumentError(Error):
|
2003-08-26 19:18:35 +02:00
|
|
|
"""The bot replies with a help message when this is raised."""
|
2003-04-01 00:22:59 +02:00
|
|
|
pass
|
|
|
|
|
2003-08-27 09:45:48 +02:00
|
|
|
class CannotNest(Error):
|
|
|
|
"""Exception to be raised by commands that cannot be nested."""
|
|
|
|
pass
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
class Tokenizer:
|
2003-07-21 08:10:35 +02:00
|
|
|
# This will be used as a global environment to evaluate strings in.
|
|
|
|
# Evaluation is, of course, necessary in order to allowed escaped
|
|
|
|
# characters to be properly handled.
|
|
|
|
#
|
|
|
|
# These are the characters valid in a token. Everything printable except
|
|
|
|
# double-quote, left-bracket, and right-bracket.
|
2004-02-18 16:39:30 +01:00
|
|
|
validChars = string.ascii.translate(string.ascii, '\x00\r\n \t"')
|
2003-11-12 22:57:21 +01:00
|
|
|
quotes = '"'
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self, tokens=''):
|
2003-09-17 21:19:38 +02:00
|
|
|
# Add a '|' to tokens to have the pipe syntax.
|
2003-03-12 07:26:59 +01:00
|
|
|
self.validChars = self.validChars.translate(string.ascii, tokens)
|
2004-03-30 10:27:05 +02:00
|
|
|
if len(tokens) >= 2:
|
|
|
|
self.left = tokens[0]
|
|
|
|
self.right = tokens[1]
|
2004-04-22 07:02:46 +02:00
|
|
|
else:
|
|
|
|
self.left = ''
|
|
|
|
self.right = ''
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-04 13:34:44 +02:00
|
|
|
def _handleToken(self, token):
|
2003-11-12 22:57:21 +01:00
|
|
|
if token[0] == token[-1] and token[0] in self.quotes:
|
|
|
|
token = token[1:-1]
|
|
|
|
token = token.decode('string-escape')
|
2003-03-12 07:26:59 +01:00
|
|
|
return token
|
|
|
|
|
2003-10-04 13:34:44 +02:00
|
|
|
def _insideBrackets(self, lexer):
|
2003-03-12 07:26:59 +01:00
|
|
|
ret = []
|
|
|
|
while True:
|
|
|
|
token = lexer.get_token()
|
2003-08-17 04:02:53 +02:00
|
|
|
if not token:
|
2004-04-13 18:03:05 +02:00
|
|
|
raise SyntaxError, 'Missing "%s". You may want to ' \
|
|
|
|
'quote your arguments with double ' \
|
|
|
|
'quotes in order to prevent extra ' \
|
|
|
|
'brackets from being evaluated ' \
|
|
|
|
'as nested commands.' % self.right
|
2004-03-30 10:27:05 +02:00
|
|
|
elif token == self.right:
|
2004-02-18 17:29:44 +01:00
|
|
|
return ret
|
2004-03-30 10:27:05 +02:00
|
|
|
elif token == self.left:
|
2004-02-18 17:29:44 +01:00
|
|
|
ret.append(self._insideBrackets(lexer))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-10-04 13:34:44 +02:00
|
|
|
ret.append(self._handleToken(token))
|
2003-03-12 07:26:59 +01:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def tokenize(self, s):
|
|
|
|
lexer = shlex.shlex(StringIO(s))
|
|
|
|
lexer.commenters = ''
|
2003-11-12 22:57:21 +01:00
|
|
|
lexer.quotes = self.quotes
|
2003-03-12 07:26:59 +01:00
|
|
|
lexer.wordchars = self.validChars
|
|
|
|
args = []
|
2003-09-07 11:41:47 +02:00
|
|
|
ends = []
|
2003-03-12 07:26:59 +01:00
|
|
|
while True:
|
|
|
|
token = lexer.get_token()
|
2003-08-17 04:02:53 +02:00
|
|
|
if not token:
|
2003-03-12 07:26:59 +01:00
|
|
|
break
|
2004-05-07 18:14:02 +02:00
|
|
|
elif token == '|' and conf.supybot.reply.pipeSyntax():
|
2003-09-07 11:41:47 +02:00
|
|
|
if not args:
|
2004-04-13 18:03:05 +02:00
|
|
|
raise SyntaxError, '"|" with nothing preceding. I ' \
|
|
|
|
'obviously can\'t do a pipe with ' \
|
|
|
|
'nothing before the |.'
|
2003-09-07 11:41:47 +02:00
|
|
|
ends.append(args)
|
|
|
|
args = []
|
2004-05-07 18:14:02 +02:00
|
|
|
elif token == self.left:
|
|
|
|
args.append(self._insideBrackets(lexer))
|
|
|
|
elif token == self.right:
|
|
|
|
raise SyntaxError, 'Spurious "%s". You may want to ' \
|
|
|
|
'quote your arguments with double ' \
|
|
|
|
'quotes in order to prevent extra ' \
|
|
|
|
'brackets from being evaluated ' \
|
|
|
|
'as nested commands.' % self.right
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-10-04 13:34:44 +02:00
|
|
|
args.append(self._handleToken(token))
|
2003-09-07 11:41:47 +02:00
|
|
|
if ends:
|
|
|
|
if not args:
|
2004-04-13 18:03:05 +02:00
|
|
|
raise SyntaxError, '"|" with nothing following. I ' \
|
|
|
|
'obviously can\'t do a pipe with ' \
|
|
|
|
'nothing before the |.'
|
2003-09-07 11:41:47 +02:00
|
|
|
args.append(ends.pop())
|
|
|
|
while ends:
|
|
|
|
args[-1].append(ends.pop())
|
2003-03-12 07:26:59 +01:00
|
|
|
return args
|
|
|
|
|
2004-07-23 08:54:29 +02:00
|
|
|
def tokenize(s, brackets=None, channel=None):
|
2003-10-20 06:16:44 +02:00
|
|
|
"""A utility function to create a Tokenizer and tokenize a string."""
|
|
|
|
start = time.time()
|
|
|
|
try:
|
2004-05-07 18:14:02 +02:00
|
|
|
if brackets is None:
|
2004-07-23 08:54:29 +02:00
|
|
|
tokens = conf.channelValue(conf.supybot.reply.brackets, channel)
|
|
|
|
else:
|
|
|
|
tokens = brackets
|
|
|
|
if conf.channelValue(conf.supybot.reply.pipeSyntax, channel):
|
2004-04-13 18:03:05 +02:00
|
|
|
tokens = '%s|' % tokens
|
2004-08-11 09:43:28 +02:00
|
|
|
log.stat('tokenize took %s seconds.' % (time.time() - start))
|
2004-04-13 18:03:05 +02:00
|
|
|
return Tokenizer(tokens).tokenize(s)
|
2003-10-20 06:16:44 +02:00
|
|
|
except ValueError, e:
|
|
|
|
raise SyntaxError, str(e)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-09-10 10:32:20 +02:00
|
|
|
def getCommands(tokens):
|
2003-10-04 13:34:44 +02:00
|
|
|
"""Given tokens as output by tokenize, returns the command names."""
|
2003-09-10 10:32:20 +02:00
|
|
|
L = []
|
|
|
|
if tokens and isinstance(tokens, list):
|
|
|
|
L.append(tokens[0])
|
|
|
|
for elt in tokens:
|
|
|
|
L.extend(getCommands(elt))
|
|
|
|
return L
|
|
|
|
|
2003-08-25 09:23:36 +02:00
|
|
|
def findCallbackForCommand(irc, commandName):
|
2003-10-20 12:25:13 +02:00
|
|
|
"""Given a command name and an Irc object, returns a list of callbacks that
|
|
|
|
commandName is in."""
|
|
|
|
L = []
|
2003-08-25 09:23:36 +02:00
|
|
|
for callback in irc.callbacks:
|
2003-09-02 09:30:35 +02:00
|
|
|
if not isinstance(callback, PrivmsgRegexp):
|
|
|
|
if hasattr(callback, 'isCommand'):
|
|
|
|
if callback.isCommand(commandName):
|
2003-10-20 12:25:13 +02:00
|
|
|
L.append(callback)
|
|
|
|
return L
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-07-23 08:54:29 +02:00
|
|
|
def formatArgumentError(method, name=None, channel=None):
|
2003-10-21 09:20:54 +02:00
|
|
|
if name is None:
|
|
|
|
name = method.__name__
|
|
|
|
if hasattr(method, '__doc__') and method.__doc__:
|
2004-07-23 08:54:29 +02:00
|
|
|
if conf.channelValue(conf.supybot.reply.showSimpleSyntax, channel):
|
2003-10-24 13:31:09 +02:00
|
|
|
return getSyntax(method, name=name)
|
|
|
|
else:
|
|
|
|
return getHelp(method, name=name)
|
2003-10-21 09:20:54 +02:00
|
|
|
else:
|
2003-10-24 13:31:09 +02:00
|
|
|
return 'Invalid arguments for %s.' % method.__name__
|
2003-10-21 09:20:54 +02:00
|
|
|
|
2004-01-08 22:49:10 +01:00
|
|
|
def checkCommandCapability(msg, cb, command):
|
2004-01-30 23:14:39 +01:00
|
|
|
plugin = cb.name().lower()
|
|
|
|
pluginCommand = '%s.%s' % (plugin, command)
|
|
|
|
def checkCapability(capability):
|
|
|
|
assert ircdb.isAntiCapability(capability)
|
|
|
|
if ircdb.checkCapability(msg.prefix, capability):
|
|
|
|
log.info('Preventing %s from calling %s because of %s.',
|
|
|
|
msg.prefix, pluginCommand, capability)
|
|
|
|
raise RuntimeError, capability
|
|
|
|
try:
|
|
|
|
antiPlugin = ircdb.makeAntiCapability(plugin)
|
|
|
|
antiCommand = ircdb.makeAntiCapability(command)
|
|
|
|
antiPluginCommand = ircdb.makeAntiCapability(pluginCommand)
|
|
|
|
checkCapability(antiPlugin)
|
|
|
|
checkCapability(antiCommand)
|
|
|
|
checkCapability(antiPluginCommand)
|
|
|
|
checkAtEnd = [command, pluginCommand]
|
2004-07-31 07:00:43 +02:00
|
|
|
default = conf.supybot.capabilities.default()
|
2004-01-30 23:14:39 +01:00
|
|
|
if ircutils.isChannel(msg.args[0]):
|
|
|
|
channel = msg.args[0]
|
|
|
|
checkCapability(ircdb.makeChannelCapability(channel, antiCommand))
|
|
|
|
checkCapability(ircdb.makeChannelCapability(channel, antiPlugin))
|
|
|
|
checkCapability(ircdb.makeChannelCapability(channel,
|
|
|
|
antiPluginCommand))
|
|
|
|
chanPlugin = ircdb.makeChannelCapability(channel, plugin)
|
|
|
|
chanCommand = ircdb.makeChannelCapability(channel, command)
|
|
|
|
chanPluginCommand = ircdb.makeChannelCapability(channel,
|
|
|
|
pluginCommand)
|
2004-08-11 09:43:28 +02:00
|
|
|
checkAtEnd += [chanCommand, chanPlugin, chanPluginCommand]
|
2004-01-30 23:14:39 +01:00
|
|
|
default &= ircdb.channels.getChannel(channel).defaultAllow
|
|
|
|
return not (default or \
|
|
|
|
any(lambda x: ircdb.checkCapability(msg.prefix, x),
|
|
|
|
checkAtEnd))
|
|
|
|
except RuntimeError, e:
|
|
|
|
s = ircdb.unAntiCapability(str(e))
|
|
|
|
return s
|
|
|
|
|
2003-12-07 00:52:23 +01:00
|
|
|
|
2004-01-07 20:09:24 +01:00
|
|
|
class RichReplyMethods(object):
|
2004-02-24 22:20:31 +01:00
|
|
|
"""This is a mixin so these replies need only be defined once. It operates
|
2004-04-17 18:45:56 +02:00
|
|
|
under several assumptions, including the fact that 'self' is an Irc object
|
2004-02-24 22:20:31 +01:00
|
|
|
of some sort and there is a self.msg that is an IrcMsg."""
|
2004-01-15 13:54:10 +01:00
|
|
|
def __makeReply(self, prefix, s):
|
2004-01-07 20:09:24 +01:00
|
|
|
if s:
|
|
|
|
s = '%s %s' % (prefix, s)
|
|
|
|
else:
|
|
|
|
s = prefix
|
2004-02-24 22:20:31 +01:00
|
|
|
return plugins.standardSubstitute(self, self.msg, s)
|
2004-01-07 20:09:24 +01:00
|
|
|
|
2004-08-18 20:44:34 +02:00
|
|
|
def _getConfig(self, wrapper):
|
|
|
|
return wrapper.get(self.msg.args[0])()
|
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def replySuccess(self, s='', **kwargs):
|
2004-08-18 20:44:34 +02:00
|
|
|
v = self._getConfig(conf.supybot.replies.success)
|
2004-01-20 17:17:12 +01:00
|
|
|
s = self.__makeReply(v, s)
|
2004-01-19 21:51:04 +01:00
|
|
|
self.reply(s, **kwargs)
|
2004-01-07 20:09:24 +01:00
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def replyError(self, s='', **kwargs):
|
2004-08-18 20:44:34 +02:00
|
|
|
v = self._getConfig(conf.supybot.replies.error)
|
2004-01-20 17:17:12 +01:00
|
|
|
s = self.__makeReply(v, s)
|
2004-01-19 21:51:04 +01:00
|
|
|
self.reply(s, **kwargs)
|
|
|
|
|
2004-04-17 16:07:55 +02:00
|
|
|
def replies(self, L, prefixer=None, joiner=None,
|
|
|
|
onlyPrefixFirst=False, **kwargs):
|
2004-01-19 21:51:04 +01:00
|
|
|
if prefixer is None:
|
|
|
|
prefixer = ''
|
|
|
|
if joiner is None:
|
|
|
|
joiner = utils.commaAndify
|
|
|
|
if isinstance(prefixer, basestring):
|
|
|
|
prefixer = prefixer.__add__
|
|
|
|
if isinstance(joiner, basestring):
|
|
|
|
joiner = joiner.join
|
|
|
|
if conf.supybot.reply.oneToOne():
|
2004-02-07 13:38:31 +01:00
|
|
|
self.reply(prefixer(joiner(L)), **kwargs)
|
2004-01-19 21:51:04 +01:00
|
|
|
else:
|
|
|
|
first = True
|
|
|
|
for s in L:
|
|
|
|
if onlyPrefixFirst:
|
|
|
|
if first:
|
2004-02-07 13:38:31 +01:00
|
|
|
self.reply(prefixer(s), **kwargs)
|
2004-01-19 21:51:04 +01:00
|
|
|
first = False
|
|
|
|
else:
|
2004-02-07 13:38:31 +01:00
|
|
|
self.reply(s, **kwargs)
|
2004-01-19 21:51:04 +01:00
|
|
|
else:
|
2004-02-07 13:38:31 +01:00
|
|
|
self.reply(prefixer(s), **kwargs)
|
2004-01-07 20:09:24 +01:00
|
|
|
|
2004-08-18 20:44:34 +02:00
|
|
|
def _error(self, s, Raise, **kwargs):
|
|
|
|
if Raise:
|
2004-08-19 21:48:00 +02:00
|
|
|
raise Error, s
|
2004-08-18 20:44:34 +02:00
|
|
|
else:
|
|
|
|
self.error(s, **kwargs)
|
|
|
|
|
|
|
|
def errorNoCapability(self, capability, s='', Raise=False, **kwargs):
|
2004-01-30 23:14:39 +01:00
|
|
|
if isinstance(capability, basestring): # checkCommandCapability!
|
2004-08-19 14:19:18 +02:00
|
|
|
log.warning('Denying %s for lacking %r capability.',
|
2004-01-30 23:14:39 +01:00
|
|
|
self.msg.prefix, capability)
|
2004-08-18 20:44:34 +02:00
|
|
|
if not self._getConfig(conf.supybot.reply.noCapabilityError):
|
|
|
|
v = self._getConfig(conf.supybot.replies.noCapability)
|
2004-01-30 23:14:39 +01:00
|
|
|
s = self.__makeReply(v % capability, s)
|
2004-08-18 20:44:34 +02:00
|
|
|
self._error(s, Raise, **kwargs)
|
2004-01-30 23:14:39 +01:00
|
|
|
else:
|
|
|
|
log.warning('Denying %s for some unspecified capability '
|
2004-08-19 14:19:18 +02:00
|
|
|
'(or a default).', self.msg.prefix)
|
2004-08-18 20:44:34 +02:00
|
|
|
v = self._getConfig(conf.supybot.replies.genericNoCapability)
|
|
|
|
self._error(self.__makeReply(v, s), Raise, **kwargs)
|
2004-01-08 16:24:56 +01:00
|
|
|
|
2004-08-18 20:44:34 +02:00
|
|
|
def errorPossibleBug(self, s='', Raise=False, **kwargs):
|
|
|
|
v = self._getConfig(conf.supybot.replies.possibleBug)
|
2004-01-09 00:03:48 +01:00
|
|
|
if s:
|
2004-01-20 17:17:12 +01:00
|
|
|
s += ' (%s)' % v
|
2004-01-18 08:58:26 +01:00
|
|
|
else:
|
2004-01-20 17:17:12 +01:00
|
|
|
s = v
|
2004-08-18 20:44:34 +02:00
|
|
|
self._error(s, Raise, **kwargs)
|
2004-01-09 00:03:48 +01:00
|
|
|
|
2004-08-18 20:44:34 +02:00
|
|
|
def errorNotRegistered(self, s='', Raise=False, **kwargs):
|
|
|
|
v = self._getConfig(conf.supybot.replies.notRegistered)
|
|
|
|
self._error(self.__makeReply(v, s), Raise, **kwargs)
|
2004-01-08 16:24:56 +01:00
|
|
|
|
2004-08-18 20:44:34 +02:00
|
|
|
def errorNoUser(self, s='', name='that user', Raise=False, **kwargs):
|
|
|
|
v = self._getConfig(conf.supybot.replies.noUser)
|
2004-07-24 04:29:26 +02:00
|
|
|
try:
|
|
|
|
v = v % name
|
|
|
|
except TypeError:
|
|
|
|
log.warning('supybot.replies.noUser should have one "%s" in it.')
|
2004-08-18 20:44:34 +02:00
|
|
|
self._error(self.__makeReply(v, s), Raise, **kwargs)
|
2004-01-08 16:24:56 +01:00
|
|
|
|
2004-08-18 20:44:34 +02:00
|
|
|
def errorRequiresPrivacy(self, s='', Raise=False, **kwargs):
|
|
|
|
v = self._getConfig(conf.supybot.replies.requiresPrivacy)
|
|
|
|
self._error(self.__makeReply(v, s), Raise, **kwargs)
|
2004-01-08 16:24:56 +01:00
|
|
|
|
2004-07-07 20:04:55 +02:00
|
|
|
|
2004-01-07 20:09:24 +01:00
|
|
|
class IrcObjectProxy(RichReplyMethods):
|
2003-08-26 19:18:35 +02:00
|
|
|
"A proxy object to allow proper nested of commands (even threaded ones)."
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self, irc, msg, args):
|
2004-01-01 21:15:25 +01:00
|
|
|
log.debug('IrcObjectProxy.__init__: %s' % args)
|
2003-10-30 00:02:27 +01:00
|
|
|
self.irc = irc
|
|
|
|
self.msg = msg
|
2004-01-16 13:42:52 +01:00
|
|
|
# The deepcopy here is necessary for Scheduler; it re-runs already
|
|
|
|
# tokenized commands.
|
2004-01-04 20:35:02 +01:00
|
|
|
self.args = copy.deepcopy(args)
|
2003-10-30 00:02:27 +01:00
|
|
|
self.counter = 0
|
2004-04-17 16:07:55 +02:00
|
|
|
self.finished = False # Used in _callInvalidCommands.
|
2004-04-21 06:31:42 +02:00
|
|
|
self.commandMethod = None # Used in error.
|
2004-04-17 16:07:55 +02:00
|
|
|
self._resetReplyAttributes()
|
2003-09-05 09:26:55 +02:00
|
|
|
if not args:
|
2003-10-30 00:02:27 +01:00
|
|
|
self.finalEvaled = True
|
|
|
|
self._callInvalidCommands()
|
2003-09-05 09:26:55 +02:00
|
|
|
else:
|
2003-10-28 07:06:21 +01:00
|
|
|
self.finalEvaled = False
|
2003-09-05 09:26:55 +02:00
|
|
|
world.commandsProcessed += 1
|
|
|
|
self.evalArgs()
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-04-17 16:07:55 +02:00
|
|
|
def _resetReplyAttributes(self):
|
|
|
|
self.to = None
|
2004-08-08 02:41:13 +02:00
|
|
|
self.action = None
|
|
|
|
self.notice = None
|
|
|
|
self.private = None
|
|
|
|
self.noLengthCheck = None
|
2004-04-17 16:07:55 +02:00
|
|
|
self.prefixName = conf.supybot.reply.withNickPrefix()
|
2004-07-07 20:04:55 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def evalArgs(self):
|
|
|
|
while self.counter < len(self.args):
|
|
|
|
if type(self.args[self.counter]) == str:
|
|
|
|
self.counter += 1
|
|
|
|
else:
|
|
|
|
IrcObjectProxy(self, self.msg, self.args[self.counter])
|
|
|
|
return
|
|
|
|
self.finalEval()
|
|
|
|
|
2003-10-30 00:02:27 +01:00
|
|
|
def _callInvalidCommands(self):
|
2004-07-31 05:45:25 +02:00
|
|
|
if ircmsgs.isCtcp(self.msg):
|
2004-01-07 13:00:59 +01:00
|
|
|
log.debug('Skipping invalidCommand, msg is CTCP.')
|
|
|
|
return
|
|
|
|
log.debug('Calling invalidCommands.')
|
2003-10-30 00:02:27 +01:00
|
|
|
for cb in self.irc.callbacks:
|
2004-08-19 14:19:18 +02:00
|
|
|
log.debug('Trying to call %s.invalidCommand.' % cb.name())
|
2003-10-30 00:02:27 +01:00
|
|
|
if self.finished:
|
2004-08-19 14:19:18 +02:00
|
|
|
log.debug('Finished calling invalidCommand: %s.', cb.name())
|
2004-01-01 21:15:25 +01:00
|
|
|
return
|
2003-10-30 00:02:27 +01:00
|
|
|
if hasattr(cb, 'invalidCommand'):
|
2004-08-26 06:19:06 +02:00
|
|
|
try:
|
|
|
|
# I think I took out this try/except block because we
|
|
|
|
# firewalled invalidCommand, but we've no guarantee that
|
|
|
|
# other classes won't have firewalled it. Better safe
|
|
|
|
# than sorry, I say.
|
|
|
|
cb.invalidCommand(self, self.msg, self.args)
|
|
|
|
except Exception, e:
|
|
|
|
log.exception('Uncaught exception in %s.invalidCommand',
|
|
|
|
cb.name())
|
2004-01-16 13:42:52 +01:00
|
|
|
|
|
|
|
def _callCommand(self, name, command, cb):
|
|
|
|
try:
|
2004-04-21 06:31:42 +02:00
|
|
|
self.commandMethod = command
|
|
|
|
try:
|
|
|
|
cb.callCommand(command, self, self.msg, self.args)
|
2004-08-16 15:22:08 +02:00
|
|
|
except (getopt.GetoptError, ArgumentError):
|
2004-04-21 06:31:42 +02:00
|
|
|
self.reply(formatArgumentError(command, name=name))
|
|
|
|
except CannotNest, e:
|
|
|
|
if not isinstance(self.irc, irclib.Irc):
|
|
|
|
self.error('Command %r cannot be nested.' % name)
|
|
|
|
except (SyntaxError, Error), e:
|
|
|
|
cb.log.info('Error return: %s', e)
|
|
|
|
self.error(str(e))
|
|
|
|
except Exception, e:
|
|
|
|
cb.log.exception('Uncaught exception:')
|
|
|
|
if conf.supybot.reply.detailedErrors():
|
|
|
|
self.error(utils.exnToString(e))
|
|
|
|
else:
|
|
|
|
self.replyError()
|
|
|
|
finally:
|
|
|
|
self.commandMethod = None
|
2004-07-07 20:04:55 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def finalEval(self):
|
2003-10-28 01:22:15 +01:00
|
|
|
assert not self.finalEvaled, 'finalEval called twice.'
|
2003-03-12 07:26:59 +01:00
|
|
|
self.finalEvaled = True
|
2003-10-28 01:22:15 +01:00
|
|
|
name = canonicalName(self.args[0])
|
2003-10-20 12:25:13 +02:00
|
|
|
cbs = findCallbackForCommand(self, name)
|
|
|
|
if len(cbs) == 0:
|
2003-10-28 01:22:15 +01:00
|
|
|
for cb in self.irc.callbacks:
|
|
|
|
if isinstance(cb, PrivmsgRegexp):
|
2004-01-16 22:12:56 +01:00
|
|
|
for (r, m) in cb.res:
|
2003-10-28 06:16:17 +01:00
|
|
|
if r.search(self.msg.args[1]):
|
2004-01-01 21:15:25 +01:00
|
|
|
log.debug('Skipping invalidCommand: %s.%s',
|
|
|
|
m.im_class.__name__,m.im_func.func_name)
|
2003-10-28 01:22:15 +01:00
|
|
|
return
|
2003-10-28 06:16:17 +01:00
|
|
|
elif isinstance(cb, PrivmsgCommandAndRegexp):
|
2004-01-01 21:15:25 +01:00
|
|
|
for (r, m) in cb.res:
|
2003-10-28 06:16:17 +01:00
|
|
|
if r.search(self.msg.args[1]):
|
2004-01-01 21:15:25 +01:00
|
|
|
log.debug('Skipping invalidCommand: %s.%s',
|
|
|
|
m.im_class.__name__,m.im_func.func_name)
|
2003-10-28 01:22:15 +01:00
|
|
|
return
|
2004-01-01 21:15:25 +01:00
|
|
|
payload = addressed(self.irc.nick, self.msg)
|
|
|
|
for (r, m) in cb.addressedRes:
|
|
|
|
if r.search(payload):
|
|
|
|
log.debug('Skipping invalidCommand: %s.%s',
|
|
|
|
m.im_class.__name__,m.im_func.func_name)
|
2003-10-28 01:22:15 +01:00
|
|
|
return
|
|
|
|
# Ok, no regexp-based things matched.
|
2003-10-30 00:02:27 +01:00
|
|
|
self._callInvalidCommands()
|
2003-10-20 12:25:13 +02:00
|
|
|
else:
|
2004-01-16 17:14:46 +01:00
|
|
|
if len(cbs) > 1:
|
|
|
|
for cb in cbs:
|
2004-03-30 10:02:59 +02:00
|
|
|
if canonicalName(cb.name()) == name:
|
2004-07-23 21:55:13 +02:00
|
|
|
del self.args[0]
|
2004-01-16 17:14:46 +01:00
|
|
|
break
|
2003-11-25 12:08:57 +01:00
|
|
|
else:
|
2004-01-16 17:14:46 +01:00
|
|
|
# This should've been caught earlier, that's why we
|
|
|
|
# assert instead of raising a ValueError or something.
|
|
|
|
assert False, 'Non-disambiguated command.'
|
|
|
|
else:
|
|
|
|
del self.args[0]
|
|
|
|
cb = cbs[0]
|
2004-01-30 23:14:39 +01:00
|
|
|
cap = checkCommandCapability(self.msg, cb, name)
|
|
|
|
if cap:
|
|
|
|
self.errorNoCapability(cap)
|
2004-01-16 17:14:46 +01:00
|
|
|
return
|
|
|
|
command = getattr(cb, name)
|
|
|
|
Privmsg.handled = True
|
2004-04-30 04:13:42 +02:00
|
|
|
if cb.threaded or conf.supybot.debug.threadAllCommands():
|
2004-01-16 17:14:46 +01:00
|
|
|
t = CommandThread(target=self._callCommand,
|
|
|
|
args=(name, command, cb))
|
|
|
|
t.start()
|
|
|
|
else:
|
|
|
|
self._callCommand(name, command, cb)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def reply(self, s, noLengthCheck=False, prefixName=True,
|
2004-08-08 02:41:13 +02:00
|
|
|
action=None, private=None, notice=None, to=None, msg=None):
|
2004-01-08 04:12:14 +01:00
|
|
|
"""reply(s) -> replies to msg with s
|
2003-10-04 13:34:44 +02:00
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
noLengthCheck=False: True if the length shouldn't be checked
|
|
|
|
(used for 'more' handling)
|
|
|
|
prefixName=True: False if the nick shouldn't be prefixed to the
|
|
|
|
reply.
|
|
|
|
action=False: True if the reply should be an action.
|
|
|
|
private=False: True if the reply should be in private.
|
2003-10-20 12:10:46 +02:00
|
|
|
notice=False: True if the reply should be noticed when the
|
|
|
|
bot is configured to do so.
|
2003-11-25 12:08:57 +01:00
|
|
|
to=<nick|channel>: The nick or channel the reply should go to.
|
|
|
|
Defaults to msg.args[0] (or msg.nick if private)
|
2003-10-04 13:34:44 +02:00
|
|
|
"""
|
2004-01-18 08:58:26 +01:00
|
|
|
# These use and or or based on whether or not they default to True or
|
|
|
|
# False. Those that default to True use and; those that default to
|
|
|
|
# False use or.
|
2004-01-09 00:14:40 +01:00
|
|
|
assert not isinstance(s, ircmsgs.IrcMsg), \
|
|
|
|
'Old code alert: there is no longer a "msg" argument to reply.'
|
2004-07-31 08:46:19 +02:00
|
|
|
if msg is None:
|
|
|
|
msg = self.msg
|
2004-08-08 02:41:13 +02:00
|
|
|
if action is not None:
|
|
|
|
self.action = self.action or action
|
|
|
|
if notice is not None:
|
|
|
|
self.notice = self.notice or notice
|
|
|
|
if private is not None:
|
|
|
|
self.private = self.private or private
|
|
|
|
if to is not None:
|
|
|
|
self.to = self.to or to
|
2004-04-09 17:30:34 +02:00
|
|
|
# action=True implies noLengthCheck=True and prefixName=False
|
2004-04-09 04:55:03 +02:00
|
|
|
self.prefixName = prefixName and self.prefixName and not self.action
|
|
|
|
self.noLengthCheck=noLengthCheck or self.noLengthCheck or self.action
|
2003-03-12 07:26:59 +01:00
|
|
|
if self.finalEvaled:
|
2004-04-17 16:07:55 +02:00
|
|
|
try:
|
|
|
|
if not isinstance(self.irc, irclib.Irc):
|
|
|
|
self.irc.reply(s, to=self.to,
|
|
|
|
notice=self.notice,
|
|
|
|
action=self.action,
|
|
|
|
private=self.private,
|
|
|
|
prefixName=self.prefixName,
|
|
|
|
noLengthCheck=self.noLengthCheck)
|
|
|
|
elif self.noLengthCheck:
|
|
|
|
# noLengthCheck only matters to IrcObjectProxy, so it's not
|
|
|
|
# used here. Just in case you were wondering.
|
|
|
|
self.irc.queueMsg(reply(msg, s, to=self.to,
|
|
|
|
notice=self.notice,
|
|
|
|
action=self.action,
|
|
|
|
private=self.private,
|
|
|
|
prefixName=self.prefixName))
|
|
|
|
else:
|
|
|
|
s = ircutils.safeArgument(s)
|
|
|
|
allowedLength = 450 - len(self.irc.prefix)
|
2004-07-31 14:38:38 +02:00
|
|
|
maximumMores = conf.supybot.reply.mores.maximum()
|
2004-04-17 16:07:55 +02:00
|
|
|
maximumLength = allowedLength * maximumMores
|
|
|
|
if len(s) > maximumLength:
|
2004-08-19 14:19:18 +02:00
|
|
|
log.warning('Truncating to %s bytes from %s bytes.',
|
2004-04-17 16:07:55 +02:00
|
|
|
maximumLength, len(s))
|
|
|
|
s = s[:maximumLength]
|
|
|
|
if len(s) < allowedLength or conf.supybot.reply.truncate():
|
|
|
|
# In case we're truncating, we add 20 to allowedLength,
|
|
|
|
# because our allowedLength is shortened for the
|
|
|
|
# "(XX more messages)" trailer.
|
|
|
|
s = s[:allowedLength+20]
|
|
|
|
# There's no need for action=self.action here because
|
|
|
|
# action implies noLengthCheck, which has already been
|
|
|
|
# handled. Let's stick an assert in here just in case.
|
|
|
|
assert not self.action
|
|
|
|
self.irc.queueMsg(reply(msg, s, to=self.to,
|
|
|
|
notice=self.notice,
|
|
|
|
private=self.private,
|
|
|
|
prefixName=self.prefixName))
|
|
|
|
self.finished = True
|
|
|
|
return
|
2004-07-22 18:16:01 +02:00
|
|
|
msgs = ircutils.wrap(s, allowedLength-30) # -30 is for nick:
|
2004-04-17 16:07:55 +02:00
|
|
|
msgs.reverse()
|
2004-07-31 14:38:38 +02:00
|
|
|
instant = conf.supybot.reply.mores.instant()
|
|
|
|
while instant > 1 and msgs:
|
|
|
|
instant -= 1
|
|
|
|
response = msgs.pop()
|
|
|
|
self.irc.queueMsg(reply(msg, response, to=self.to,
|
|
|
|
notice=self.notice,
|
|
|
|
private=self.private,
|
|
|
|
prefixName=self.prefixName))
|
|
|
|
if not msgs:
|
|
|
|
return
|
2004-04-17 16:07:55 +02:00
|
|
|
response = msgs.pop()
|
|
|
|
if msgs:
|
|
|
|
n = ircutils.bold('(%s)')
|
|
|
|
n %= utils.nItems('message', len(msgs), 'more')
|
|
|
|
response = '%s %s' % (response, n)
|
|
|
|
prefix = msg.prefix
|
|
|
|
if self.to and ircutils.isNick(self.to):
|
|
|
|
try:
|
|
|
|
state = self.getRealIrc().state
|
|
|
|
prefix = state.nickToHostmask(self.to)
|
|
|
|
except KeyError:
|
|
|
|
pass # We'll leave it as it is.
|
|
|
|
mask = prefix.split('!', 1)[1]
|
|
|
|
Privmsg._mores[mask] = msgs
|
|
|
|
public = ircutils.isChannel(msg.args[0])
|
|
|
|
private = self.private or not public
|
|
|
|
Privmsg._mores[msg.nick] = (private, msgs)
|
|
|
|
self.irc.queueMsg(reply(msg, response, to=self.to,
|
|
|
|
action=self.action,
|
|
|
|
notice=self.notice,
|
|
|
|
private=self.private,
|
|
|
|
prefixName=self.prefixName))
|
|
|
|
self.finished = True
|
|
|
|
finally:
|
|
|
|
self._resetReplyAttributes()
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
self.args[self.counter] = s
|
|
|
|
self.evalArgs()
|
|
|
|
|
2004-08-24 16:03:24 +02:00
|
|
|
def error(self, s='', Raise=False, **kwargs):
|
|
|
|
if Raise:
|
|
|
|
if s:
|
|
|
|
raise Error, s
|
|
|
|
else:
|
|
|
|
raise ArgumentError
|
2004-04-21 06:31:42 +02:00
|
|
|
if s:
|
|
|
|
if not isinstance(self.irc, irclib.Irc):
|
2004-04-27 19:14:54 +02:00
|
|
|
self.irc.error(s, **kwargs)
|
2004-04-21 06:31:42 +02:00
|
|
|
else:
|
2004-04-27 19:14:54 +02:00
|
|
|
self.irc.queueMsg(error(self.msg, s, **kwargs))
|
2003-08-27 09:45:48 +02:00
|
|
|
else:
|
2004-04-21 06:31:42 +02:00
|
|
|
if self.commandMethod is not None:
|
|
|
|
# We can recurse here because it only gets called once.
|
2004-04-27 19:14:54 +02:00
|
|
|
self.error(formatArgumentError(self.commandMethod), **kwargs)
|
2004-08-08 19:48:33 +02:00
|
|
|
else:
|
|
|
|
raise ArgumentError # We shouldn't get here, but just in case.
|
2003-10-28 07:06:21 +01:00
|
|
|
self.finished = True
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def getRealIrc(self):
|
2003-10-04 13:34:44 +02:00
|
|
|
"""Returns the real irclib.Irc object underlying this proxy chain."""
|
2003-03-26 08:39:34 +01:00
|
|
|
if isinstance(self.irc, irclib.Irc):
|
2003-03-12 07:26:59 +01:00
|
|
|
return self.irc
|
|
|
|
else:
|
|
|
|
return self.irc.getRealIrc()
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.irc, attr)
|
|
|
|
|
2003-03-15 12:09:52 +01:00
|
|
|
|
2003-03-26 03:30:05 +01:00
|
|
|
class CommandThread(threading.Thread):
|
2003-08-26 19:18:35 +02:00
|
|
|
"""Just does some extra logging and error-recovery for commands that need
|
|
|
|
to run in threads.
|
|
|
|
"""
|
2004-08-26 04:29:48 +02:00
|
|
|
def __init__(self, target=None, args=(), kwargs={}):
|
2004-01-20 23:09:10 +01:00
|
|
|
(self.name, self.command, self.cb) = args
|
2003-04-20 18:15:35 +02:00
|
|
|
world.threadsSpawned += 1
|
2004-08-18 20:44:34 +02:00
|
|
|
threadName = 'Thread #%s (for %s.%s)' % (world.threadsSpawned,
|
|
|
|
self.cb.name(), self.name)
|
2004-01-16 17:14:46 +01:00
|
|
|
log.debug('Spawning thread %s' % threadName)
|
|
|
|
threading.Thread.__init__(self, target=target,
|
2004-08-26 03:04:23 +02:00
|
|
|
name=threadName, args=args, kwargs=kwargs)
|
2003-03-27 07:04:56 +01:00
|
|
|
self.setDaemon(True)
|
2004-01-20 23:09:10 +01:00
|
|
|
self.originalThreaded = self.cb.threaded
|
|
|
|
self.cb.threaded = True
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
threading.Thread.run(self)
|
|
|
|
finally:
|
|
|
|
self.cb.threaded = self.originalThreaded
|
2003-08-20 18:26:23 +02:00
|
|
|
|
|
|
|
|
2004-07-29 11:46:22 +02:00
|
|
|
class CanonicalString(registry.NormalizedString):
|
|
|
|
def normalize(self, s):
|
|
|
|
return canonicalName(s)
|
|
|
|
|
|
|
|
class CanonicalNameSet(utils.NormalizingSet):
|
|
|
|
def normalize(self, s):
|
|
|
|
return canonicalName(s)
|
2004-08-11 14:57:52 +02:00
|
|
|
|
2004-07-29 11:53:13 +02:00
|
|
|
class CanonicalNameDict(utils.InsensitivePreservingDict):
|
|
|
|
def key(self, s):
|
|
|
|
return canonicalName(s)
|
|
|
|
|
2004-07-29 11:46:22 +02:00
|
|
|
class Disabled(registry.SpaceSeparatedListOf):
|
|
|
|
Value = CanonicalString
|
|
|
|
List = CanonicalNameSet
|
2004-08-11 14:57:52 +02:00
|
|
|
|
2004-07-29 11:46:22 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot.commands, 'disabled',
|
|
|
|
Disabled([], """Determines what commands are currently disabled. Such
|
|
|
|
commands will not appear in command lists, etc. They will appear not even
|
|
|
|
to exist."""))
|
2004-08-11 14:57:52 +02:00
|
|
|
|
2004-07-29 11:46:22 +02:00
|
|
|
class DisabledCommands(object):
|
|
|
|
def __init__(self):
|
2004-07-29 11:53:13 +02:00
|
|
|
self.d = CanonicalNameDict()
|
2004-07-29 11:46:22 +02:00
|
|
|
for name in conf.supybot.commands.disabled():
|
|
|
|
if '.' in name:
|
|
|
|
(plugin, command) = name.split('.', 1)
|
|
|
|
if command in self.d:
|
|
|
|
if self.d[command] is not None:
|
|
|
|
self.d[command].add(plugin)
|
|
|
|
else:
|
|
|
|
self.d[command] = CanonicalNameSet([plugin])
|
|
|
|
else:
|
|
|
|
self.d[name] = None
|
|
|
|
|
|
|
|
def disabled(self, command, plugin=None):
|
|
|
|
if command in self.d:
|
|
|
|
if self.d[command] is None:
|
|
|
|
return True
|
|
|
|
elif plugin in self.d[command]:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def add(self, command, plugin=None):
|
|
|
|
if plugin is None:
|
|
|
|
self.d[command] = None
|
|
|
|
else:
|
2004-08-02 13:39:16 +02:00
|
|
|
if command in self.d:
|
|
|
|
if self.d[command] is not None:
|
|
|
|
self.d[command].add(plugin)
|
|
|
|
else:
|
|
|
|
self.d[command] = CanonicalNameSet([plugin])
|
2004-07-29 11:46:22 +02:00
|
|
|
|
|
|
|
def remove(self, command, plugin=None):
|
|
|
|
if plugin is None:
|
|
|
|
del self.d[command]
|
|
|
|
else:
|
|
|
|
if self.d[command] is not None:
|
|
|
|
self.d[command].remove(plugin)
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
class Privmsg(irclib.IrcCallback):
|
|
|
|
"""Base class for all Privmsg handlers."""
|
2004-02-06 10:20:47 +01:00
|
|
|
__metaclass__ = log.MetaFirewall
|
2004-08-26 06:19:06 +02:00
|
|
|
# For awhile, a comment stood here to say, "Eventually callCommand." But
|
|
|
|
# that's wrong, because we can't do generic error handling in this
|
|
|
|
# callCommand -- plugins need to be able to override callCommand and do
|
|
|
|
# error handling there (see the Http plugin for an example).
|
2004-02-07 12:48:37 +01:00
|
|
|
__firewalled__ = {'isCommand': None,
|
2004-08-26 06:19:06 +02:00
|
|
|
'invalidCommand': None}
|
2003-03-12 07:26:59 +01:00
|
|
|
public = True
|
2003-11-22 03:10:51 +01:00
|
|
|
handled = False
|
2004-01-01 21:15:25 +01:00
|
|
|
errored = False
|
2004-08-19 14:19:18 +02:00
|
|
|
alwaysCall = ()
|
|
|
|
threaded = False
|
|
|
|
noIgnore = False
|
2004-01-08 04:12:14 +01:00
|
|
|
Proxy = IrcObjectProxy
|
2003-03-26 08:02:09 +01:00
|
|
|
commandArgs = ['self', 'irc', 'msg', 'args']
|
2004-07-29 11:46:22 +02:00
|
|
|
# This must be class-scope, so all plugins use the same one.
|
2003-12-16 14:32:31 +01:00
|
|
|
_mores = ircutils.IrcDict()
|
2004-07-29 11:46:22 +02:00
|
|
|
_disabled = DisabledCommands()
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self):
|
2003-11-04 09:13:22 +01:00
|
|
|
self.__parent = super(Privmsg, self)
|
2003-11-26 19:21:12 +01:00
|
|
|
myName = self.name()
|
|
|
|
self.log = log.getPluginLogger(myName)
|
|
|
|
### Setup the dispatcher command.
|
|
|
|
canonicalname = canonicalName(myName)
|
2003-10-20 13:34:21 +02:00
|
|
|
self._original = getattr(self, canonicalname, None)
|
|
|
|
docstring = """<command> [<args> ...]
|
2004-07-07 20:04:55 +02:00
|
|
|
|
2003-10-23 10:43:50 +02:00
|
|
|
Command dispatcher for the %s plugin. Use 'list %s' to see the
|
2004-04-18 10:00:29 +02:00
|
|
|
commands provided by this plugin. Use 'config list plugins.%s' to see
|
|
|
|
the configuration values for this plugin. In most cases this dispatcher
|
2003-10-23 10:43:50 +02:00
|
|
|
command is unnecessary; in cases where more than one plugin defines a
|
|
|
|
given command, use this command to tell the bot which plugin's command
|
2004-04-18 10:00:29 +02:00
|
|
|
to use.""" % (myName, myName, myName)
|
2003-10-20 13:34:21 +02:00
|
|
|
def dispatcher(self, irc, msg, args):
|
|
|
|
def handleBadArgs():
|
|
|
|
if self._original:
|
|
|
|
self._original(irc, msg, args)
|
|
|
|
else:
|
2003-10-22 19:19:08 +02:00
|
|
|
cb = irc.getCallback('Misc')
|
2004-08-16 22:03:11 +02:00
|
|
|
if cb is not None:
|
|
|
|
cb.help(irc, msg, [self.name()])
|
2003-10-20 13:34:21 +02:00
|
|
|
if args:
|
|
|
|
name = canonicalName(args[0])
|
2004-08-16 15:22:08 +02:00
|
|
|
if name == canonicalName(self.name()):
|
|
|
|
handleBadArgs()
|
|
|
|
elif self.isCommand(name):
|
2004-01-30 23:14:39 +01:00
|
|
|
cap = checkCommandCapability(msg, self, name)
|
|
|
|
if cap:
|
|
|
|
irc.errorNoCapability(cap)
|
2003-12-07 00:52:23 +01:00
|
|
|
return
|
2003-10-20 13:34:21 +02:00
|
|
|
del args[0]
|
|
|
|
method = getattr(self, name)
|
2003-10-21 09:20:54 +02:00
|
|
|
try:
|
2004-07-31 14:14:13 +02:00
|
|
|
realname = '%s.%s' % (canonicalname, name)
|
2003-10-21 09:20:54 +02:00
|
|
|
method(irc, msg, args)
|
|
|
|
except (getopt.GetoptError, ArgumentError):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(formatArgumentError(method, name))
|
2003-10-20 13:34:21 +02:00
|
|
|
else:
|
|
|
|
handleBadArgs()
|
|
|
|
else:
|
|
|
|
handleBadArgs()
|
2004-01-16 17:39:00 +01:00
|
|
|
dispatcher = utils.changeFunctionName(dispatcher, canonicalname)
|
2003-10-20 13:34:21 +02:00
|
|
|
if self._original:
|
|
|
|
dispatcher.__doc__ = self._original.__doc__
|
2004-06-11 22:30:13 +02:00
|
|
|
dispatcher.isDispatcher = False
|
2003-10-20 13:34:21 +02:00
|
|
|
else:
|
|
|
|
dispatcher.__doc__ = docstring
|
2004-06-11 22:30:13 +02:00
|
|
|
dispatcher.isDispatcher = True
|
2003-10-20 13:34:21 +02:00
|
|
|
setattr(self.__class__, canonicalname, dispatcher)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def __call__(self, irc, msg):
|
2003-11-04 09:05:16 +01:00
|
|
|
if msg.command == 'PRIVMSG':
|
|
|
|
if self.noIgnore or not ircdb.checkIgnored(msg.prefix,msg.args[0]):
|
2003-11-04 09:13:22 +01:00
|
|
|
self.__parent.__call__(irc, msg)
|
2003-11-04 09:05:16 +01:00
|
|
|
else:
|
2003-11-04 09:13:22 +01:00
|
|
|
self.__parent.__call__(irc, msg)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def isCommand(self, methodName):
|
2004-01-04 12:23:23 +01:00
|
|
|
"""Returns whether a given method name is a command in this plugin."""
|
2003-03-12 07:26:59 +01:00
|
|
|
# This function is ugly, but I don't want users to call methods like
|
|
|
|
# doPrivmsg or __init__ or whatever, and this is good to stop them.
|
2004-01-04 12:52:12 +01:00
|
|
|
|
|
|
|
# Don't canonicalize this name: consider outFilter(self, irc, msg).
|
|
|
|
# methodName = canonicalName(methodName)
|
2004-07-29 11:46:22 +02:00
|
|
|
if self._disabled.disabled(methodName, plugin=self.name()):
|
|
|
|
return False
|
2003-03-12 07:26:59 +01:00
|
|
|
if hasattr(self, methodName):
|
|
|
|
method = getattr(self, methodName)
|
|
|
|
if inspect.ismethod(method):
|
|
|
|
code = method.im_func.func_code
|
2003-09-18 01:31:45 +02:00
|
|
|
return inspect.getargs(code)[0] == self.commandArgs
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2004-01-04 12:23:23 +01:00
|
|
|
def getCommand(self, methodName):
|
|
|
|
"""Gets the given command from this plugin."""
|
|
|
|
assert self.isCommand(methodName)
|
|
|
|
methodName = canonicalName(methodName)
|
|
|
|
return getattr(self, methodName)
|
|
|
|
|
2004-01-15 20:09:40 +01:00
|
|
|
def callCommand(self, method, irc, msg, *L):
|
|
|
|
name = method.im_func.func_name
|
2003-12-04 01:29:06 +01:00
|
|
|
assert L, 'Odd, nothing in L. This can\'t happen.'
|
2004-07-31 10:27:25 +02:00
|
|
|
self.log.info('%r called by %s', name, msg.prefix)
|
|
|
|
self.log.debug('args: %s', L[0])
|
2003-09-17 10:12:59 +02:00
|
|
|
start = time.time()
|
2004-01-16 17:14:46 +01:00
|
|
|
method(irc, msg, *L)
|
2003-09-17 10:12:59 +02:00
|
|
|
elapsed = time.time() - start
|
2004-08-10 09:39:23 +02:00
|
|
|
log.stat('%s took %s seconds', name, elapsed)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-07-29 09:40:32 +02:00
|
|
|
def registryValue(self, name, channel=None, value=True):
|
2004-01-20 17:17:12 +01:00
|
|
|
plugin = self.name()
|
2004-01-21 17:25:38 +01:00
|
|
|
group = conf.supybot.plugins.get(plugin)
|
2004-07-25 13:06:00 +02:00
|
|
|
names = registry.split(name)
|
2004-01-21 17:25:38 +01:00
|
|
|
for name in names:
|
|
|
|
group = group.get(name)
|
2004-07-29 09:40:32 +02:00
|
|
|
if channel is not None:
|
|
|
|
group = group.get(channel)
|
|
|
|
if value:
|
2004-01-21 17:25:38 +01:00
|
|
|
return group()
|
2004-01-20 17:17:12 +01:00
|
|
|
else:
|
2004-07-29 09:40:32 +02:00
|
|
|
return group
|
2004-01-20 17:17:12 +01:00
|
|
|
|
2004-07-25 13:06:00 +02:00
|
|
|
def setRegistryValue(self, name, value, channel=None):
|
|
|
|
plugin = self.name()
|
|
|
|
group = conf.supybot.plugins.get(plugin)
|
|
|
|
names = registry.split(name)
|
|
|
|
for name in names:
|
|
|
|
group = group.get(name)
|
|
|
|
if channel is None:
|
|
|
|
group.setValue(value)
|
|
|
|
else:
|
|
|
|
group.get(channel).setValue(value)
|
|
|
|
|
2004-04-30 20:24:35 +02:00
|
|
|
def userValue(self, name, prefixOrName, default=None):
|
|
|
|
try:
|
|
|
|
id = str(ircdb.users.getUserId(prefixOrName))
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
plugin = self.name()
|
|
|
|
group = conf.users.plugins.get(plugin)
|
2004-07-25 13:06:00 +02:00
|
|
|
names = registry.split(name)
|
2004-04-30 20:24:35 +02:00
|
|
|
for name in names:
|
|
|
|
group = group.get(name)
|
|
|
|
return group.get(id)()
|
|
|
|
|
2004-07-20 09:34:22 +02:00
|
|
|
def setUserValue(self, name, prefixOrName, value,
|
2004-07-28 08:12:02 +02:00
|
|
|
ignoreNoUser=True, setValue=True):
|
2004-04-30 21:19:46 +02:00
|
|
|
try:
|
|
|
|
id = str(ircdb.users.getUserId(prefixOrName))
|
|
|
|
except KeyError:
|
|
|
|
if ignoreNoUser:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise
|
2004-04-30 20:24:35 +02:00
|
|
|
plugin = self.name()
|
|
|
|
group = conf.users.plugins.get(plugin)
|
2004-07-25 13:06:00 +02:00
|
|
|
names = registry.split(name)
|
2004-04-30 20:24:35 +02:00
|
|
|
for name in names:
|
|
|
|
group = group.get(name)
|
|
|
|
group = group.get(id)
|
2004-04-30 21:19:46 +02:00
|
|
|
if setValue:
|
|
|
|
group.setValue(value)
|
|
|
|
else:
|
|
|
|
group.set(value)
|
2004-07-07 20:04:55 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-07-31 14:14:13 +02:00
|
|
|
class SimpleProxy(RichReplyMethods):
|
2004-01-08 04:12:14 +01:00
|
|
|
def __init__(self, irc, msg):
|
2003-03-15 12:09:52 +01:00
|
|
|
self.irc = irc
|
2004-01-08 04:12:14 +01:00
|
|
|
self.msg = msg
|
2003-03-15 12:09:52 +01:00
|
|
|
|
2004-07-31 08:46:19 +02:00
|
|
|
def error(self, s, msg=None, **kwargs):
|
|
|
|
if msg is None:
|
|
|
|
msg = self.msg
|
|
|
|
self.irc.queueMsg(error(msg, s, **kwargs))
|
2003-03-15 12:09:52 +01:00
|
|
|
|
2004-07-31 08:46:19 +02:00
|
|
|
def reply(self, s, msg=None, **kwargs):
|
|
|
|
if msg is None:
|
|
|
|
msg = self.msg
|
2004-01-09 00:14:40 +01:00
|
|
|
assert not isinstance(s, ircmsgs.IrcMsg), \
|
|
|
|
'Old code alert: there is no longer a "msg" argument to reply.'
|
2004-07-31 08:46:19 +02:00
|
|
|
self.irc.queueMsg(reply(msg, s, **kwargs))
|
2003-03-15 12:09:52 +01:00
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.irc, attr)
|
|
|
|
|
2004-07-31 14:14:13 +02:00
|
|
|
IrcObjectProxyRegexp = SimpleProxy
|
2003-03-22 04:16:20 +01:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
class PrivmsgRegexp(Privmsg):
|
|
|
|
"""A class to allow a person to create regular expression callbacks.
|
|
|
|
|
|
|
|
Much more primitive, but more flexible than the 'normal' method of using
|
|
|
|
the Privmsg class and its lexer, PrivmsgRegexp allows you to write
|
|
|
|
callbacks that aren't addressed to the bot, for instance. There are, of
|
|
|
|
course, several other possibilities. Callbacks are registered with a
|
|
|
|
string (the regular expression) and a function to be called (with the Irc
|
|
|
|
object, the IrcMsg object, and the match object) when the regular
|
|
|
|
expression matches. Callbacks must have the signature (self, irc, msg,
|
|
|
|
match) to be counted as such.
|
|
|
|
|
2003-04-01 09:59:17 +02:00
|
|
|
A class-level flags attribute is used to determine what regexp flags to
|
|
|
|
compile the regular expressions with. By default, it's re.I, which means
|
|
|
|
regular expressions are by default case-insensitive.
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
If you have a standard command-type callback, though, Privmsg is a much
|
|
|
|
better class to use, at the very least for consistency's sake, but also
|
|
|
|
because it's much more easily coded and maintained.
|
|
|
|
"""
|
2003-04-01 09:59:17 +02:00
|
|
|
flags = re.I
|
2004-07-31 14:14:13 +02:00
|
|
|
Proxy = SimpleProxy
|
2003-08-23 08:05:01 +02:00
|
|
|
commandArgs = ['self', 'irc', 'msg', 'match']
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self):
|
2003-11-04 09:13:22 +01:00
|
|
|
self.__parent = super(PrivmsgRegexp, self)
|
|
|
|
self.__parent.__init__()
|
2003-03-22 04:16:20 +01:00
|
|
|
self.res = []
|
|
|
|
#for name, value in self.__class__.__dict__.iteritems():
|
|
|
|
for name, value in self.__class__.__dict__.items():
|
|
|
|
value = getattr(self, name)
|
2003-04-14 09:01:20 +02:00
|
|
|
if self.isCommand(name):
|
2003-03-22 04:16:20 +01:00
|
|
|
try:
|
2003-04-01 09:59:17 +02:00
|
|
|
r = re.compile(value.__doc__, self.flags)
|
2003-03-22 04:16:20 +01:00
|
|
|
self.res.append((r, value))
|
2003-08-26 19:08:46 +02:00
|
|
|
except re.error, e:
|
2003-11-26 19:21:12 +01:00
|
|
|
self.log.warning('Invalid regexp: %r (%s)',value.__doc__,e)
|
2003-08-23 01:15:29 +02:00
|
|
|
self.res.sort(lambda (r1, m1), (r2, m2): cmp(m1.__name__, m2.__name__))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-23 16:46:56 +02:00
|
|
|
def callCommand(self, method, irc, msg, *L):
|
2003-10-14 01:20:15 +02:00
|
|
|
try:
|
2003-11-04 09:13:22 +01:00
|
|
|
self.__parent.callCommand(method, irc, msg, *L)
|
2003-10-14 01:20:15 +02:00
|
|
|
except Exception, e:
|
2004-01-16 17:14:46 +01:00
|
|
|
# We catch exceptions here because IrcObjectProxy isn't doing our
|
|
|
|
# dirty work for us anymore.
|
2003-11-26 19:21:12 +01:00
|
|
|
self.log.exception('Uncaught exception from callCommand:')
|
2004-01-20 23:26:48 +01:00
|
|
|
if conf.supybot.reply.detailedErrors():
|
|
|
|
irc.error(utils.exnToString(e))
|
|
|
|
else:
|
|
|
|
irc.replyError()
|
2003-10-14 01:20:15 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def doPrivmsg(self, irc, msg):
|
2004-01-01 21:15:25 +01:00
|
|
|
if Privmsg.errored:
|
|
|
|
self.log.info('%s not running due to Privmsg.errored.',
|
|
|
|
self.name())
|
|
|
|
return
|
2003-03-22 04:16:20 +01:00
|
|
|
for (r, method) in self.res:
|
2003-10-04 11:59:06 +02:00
|
|
|
spans = sets.Set()
|
2003-10-03 00:37:36 +02:00
|
|
|
for m in r.finditer(msg.args[1]):
|
2003-10-04 11:59:06 +02:00
|
|
|
# There's a bug in finditer: http://www.python.org/sf/817234
|
|
|
|
if m.span() in spans:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
spans.add(m.span())
|
2004-01-08 04:12:14 +01:00
|
|
|
proxy = self.Proxy(irc, msg)
|
2003-11-04 09:05:16 +01:00
|
|
|
self.callCommand(method, proxy, msg, m)
|
2003-03-26 03:41:39 +01:00
|
|
|
|
2003-04-18 10:24:04 +02:00
|
|
|
|
2003-08-11 05:34:54 +02:00
|
|
|
class PrivmsgCommandAndRegexp(Privmsg):
|
2003-08-19 12:46:52 +02:00
|
|
|
"""Same as Privmsg, except allows the user to also include regexp-based
|
|
|
|
callbacks. All regexp-based callbacks must be specified in a sets.Set
|
|
|
|
(or list) attribute "regexps".
|
|
|
|
"""
|
2003-08-11 05:34:54 +02:00
|
|
|
flags = re.I
|
2003-11-04 09:05:16 +01:00
|
|
|
regexps = ()
|
|
|
|
addressedRegexps = ()
|
2004-07-31 14:14:13 +02:00
|
|
|
Proxy = SimpleProxy
|
2003-08-11 05:34:54 +02:00
|
|
|
def __init__(self):
|
2003-11-04 09:13:22 +01:00
|
|
|
self.__parent = super(PrivmsgCommandAndRegexp, self)
|
|
|
|
self.__parent.__init__()
|
2003-08-11 05:34:54 +02:00
|
|
|
self.res = []
|
2003-10-09 06:29:37 +02:00
|
|
|
self.addressedRes = []
|
2003-08-11 05:34:54 +02:00
|
|
|
for name in self.regexps:
|
|
|
|
method = getattr(self, name)
|
|
|
|
r = re.compile(method.__doc__, self.flags)
|
|
|
|
self.res.append((r, method))
|
2003-10-09 06:29:37 +02:00
|
|
|
for name in self.addressedRegexps:
|
|
|
|
method = getattr(self, name)
|
|
|
|
r = re.compile(method.__doc__, self.flags)
|
|
|
|
self.addressedRes.append((r, method))
|
2003-10-20 09:31:17 +02:00
|
|
|
|
|
|
|
def callCommand(self, f, irc, msg, *L, **kwargs):
|
2003-10-14 01:20:15 +02:00
|
|
|
try:
|
2003-11-04 09:13:22 +01:00
|
|
|
self.__parent.callCommand(f, irc, msg, *L)
|
2003-10-14 01:20:15 +02:00
|
|
|
except Exception, e:
|
2003-10-20 09:31:17 +02:00
|
|
|
if 'catchErrors' in kwargs and kwargs['catchErrors']:
|
2003-11-26 19:21:12 +01:00
|
|
|
self.log.exception('Uncaught exception in callCommand:')
|
2004-01-20 23:26:48 +01:00
|
|
|
if conf.supybot.reply.detailedErrors():
|
|
|
|
irc.error(utils.exnToString(e))
|
|
|
|
else:
|
|
|
|
irc.replyError()
|
2003-10-20 09:31:17 +02:00
|
|
|
else:
|
|
|
|
raise
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-08-11 05:34:54 +02:00
|
|
|
def doPrivmsg(self, irc, msg):
|
2004-01-01 21:15:25 +01:00
|
|
|
if Privmsg.errored:
|
|
|
|
self.log.info('%s not running due to Privmsg.errored.',
|
|
|
|
self.name())
|
|
|
|
return
|
2003-08-11 05:34:54 +02:00
|
|
|
for (r, method) in self.res:
|
2003-10-21 23:01:43 +02:00
|
|
|
name = method.__name__
|
2003-10-03 00:37:36 +02:00
|
|
|
for m in r.finditer(msg.args[1]):
|
2004-01-08 04:12:14 +01:00
|
|
|
proxy = self.Proxy(irc, msg)
|
2003-11-04 09:05:16 +01:00
|
|
|
self.callCommand(method, proxy, msg, m, catchErrors=True)
|
2003-11-22 03:10:51 +01:00
|
|
|
if not Privmsg.handled:
|
|
|
|
s = addressed(irc.nick, msg)
|
|
|
|
if s:
|
|
|
|
for (r, method) in self.addressedRes:
|
|
|
|
name = method.__name__
|
2003-11-22 08:16:34 +01:00
|
|
|
if Privmsg.handled and name not in self.alwaysCall:
|
2003-10-21 23:01:43 +02:00
|
|
|
continue
|
2003-11-22 03:10:51 +01:00
|
|
|
for m in r.finditer(s):
|
2004-01-08 04:12:14 +01:00
|
|
|
proxy = self.Proxy(irc, msg)
|
2003-11-22 03:10:51 +01:00
|
|
|
self.callCommand(method,proxy,msg,m,catchErrors=True)
|
2003-11-22 08:16:34 +01:00
|
|
|
Privmsg.handled = True
|
2004-07-07 20:04:55 +02:00
|
|
|
|
2003-04-18 10:24:04 +02:00
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|