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-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$"
|
|
|
|
|
2003-10-05 14:47:19 +02:00
|
|
|
import 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-10-29 14:06:17 +01:00
|
|
|
import types
|
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
|
2003-09-07 06:05:34 +02:00
|
|
|
import textwrap
|
2003-03-12 07:26:59 +01:00
|
|
|
import threading
|
2003-10-24 13:31:09 +02:00
|
|
|
from itertools import imap, ifilter
|
2003-03-12 07:26:59 +01:00
|
|
|
from cStringIO import StringIO
|
|
|
|
|
2003-11-26 19:21:12 +01:00
|
|
|
import log
|
2003-03-12 07:26:59 +01:00
|
|
|
import conf
|
2003-08-23 01:15:29 +02:00
|
|
|
import utils
|
2003-04-21 08:17:19 +02:00
|
|
|
import world
|
2003-03-12 07:26:59 +01:00
|
|
|
import ircdb
|
|
|
|
import irclib
|
|
|
|
import ircmsgs
|
|
|
|
import ircutils
|
|
|
|
|
|
|
|
def addressed(nick, msg):
|
|
|
|
"""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
|
|
|
"""
|
2003-10-09 00:38:27 +02:00
|
|
|
nick = ircutils.toLower(nick)
|
|
|
|
if ircutils.nickEqual(msg.args[0], nick):
|
2003-03-12 07:26:59 +01:00
|
|
|
if msg.args[1][0] in conf.prefixChars:
|
|
|
|
return msg.args[1][1:].strip()
|
|
|
|
else:
|
|
|
|
return msg.args[1].strip()
|
2003-12-07 00:52:23 +01:00
|
|
|
elif conf.replyWhenAddressedByNick and \
|
|
|
|
ircutils.toLower(msg.args[1]).startswith(nick):
|
2003-03-12 07:26:59 +01:00
|
|
|
try:
|
2003-09-25 18:07:41 +02:00
|
|
|
(maybeNick, rest) = msg.args[1].split(None, 1)
|
|
|
|
while not ircutils.isNick(maybeNick):
|
|
|
|
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 ''
|
|
|
|
elif msg.args[1] and msg.args[1][0] in conf.prefixChars:
|
|
|
|
return msg.args[1][1:].strip()
|
2003-12-17 14:22:21 +01:00
|
|
|
elif conf.replyWhenNotAddressed:
|
|
|
|
return msg.args[1]
|
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.
|
|
|
|
"""
|
2003-11-15 04:01:01 +01:00
|
|
|
assert not isinstance(command, unicode)
|
|
|
|
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
|
|
|
|
2003-11-25 12:08:57 +01:00
|
|
|
def reply(msg, s, prefixName=True, private=False, notice=False, to=None):
|
2003-03-15 12:09:52 +01:00
|
|
|
"""Makes a reply to msg with the payload s"""
|
2003-04-03 10:31:47 +02:00
|
|
|
s = ircutils.safeArgument(s)
|
2003-11-25 12:08:57 +01:00
|
|
|
to = to or msg.nick
|
2003-09-23 22:45:00 +02:00
|
|
|
if ircutils.isChannel(msg.args[0]) and not private:
|
2003-10-20 12:10:46 +02:00
|
|
|
if notice or conf.replyWithPrivateNotice:
|
2003-11-25 12:08:57 +01:00
|
|
|
m = ircmsgs.notice(to, s)
|
2003-10-20 12:10:46 +02:00
|
|
|
elif prefixName:
|
2003-11-25 12:08:57 +01:00
|
|
|
m = ircmsgs.privmsg(msg.args[0], '%s: %s' % (to, s))
|
2003-09-08 10:44:51 +02:00
|
|
|
else:
|
|
|
|
m = ircmsgs.privmsg(msg.args[0], s)
|
2003-03-15 12:09:52 +01:00
|
|
|
else:
|
2003-11-25 12:08:57 +01:00
|
|
|
m = ircmsgs.privmsg(to, s)
|
2003-03-15 12:09:52 +01:00
|
|
|
return m
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-08-23 06:42:04 +02:00
|
|
|
def error(msg, s):
|
|
|
|
"""Makes an error reply to msg with the appropriate error payload."""
|
|
|
|
return reply(msg, 'Error: ' + s)
|
|
|
|
|
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.
|
2003-10-30 06:27:25 +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)
|
|
|
|
|
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:
|
2003-03-12 07:26:59 +01:00
|
|
|
raise SyntaxError, 'Missing "]"'
|
|
|
|
elif token == ']':
|
|
|
|
return ret
|
|
|
|
elif token == '[':
|
2003-10-04 13:34:44 +02: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):
|
2003-10-05 22:40:45 +02:00
|
|
|
"""Tokenizes a string according to supybot's nested argument format."""
|
2003-03-12 07:26:59 +01:00
|
|
|
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
|
2003-09-07 11:41:47 +02:00
|
|
|
elif token == '|':
|
|
|
|
if not args:
|
|
|
|
raise SyntaxError, '"|" with nothing preceding'
|
|
|
|
ends.append(args)
|
|
|
|
args = []
|
2003-03-12 07:26:59 +01:00
|
|
|
elif token == '[':
|
2003-10-04 13:34:44 +02:00
|
|
|
args.append(self._insideBrackets(lexer))
|
2003-03-12 07:26:59 +01:00
|
|
|
elif token == ']':
|
|
|
|
raise SyntaxError, 'Spurious "["'
|
|
|
|
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:
|
|
|
|
raise SyntaxError, '"|" with nothing following'
|
|
|
|
args.append(ends.pop())
|
|
|
|
while ends:
|
|
|
|
args[-1].append(ends.pop())
|
2003-03-12 07:26:59 +01:00
|
|
|
return args
|
|
|
|
|
2003-10-20 06:16:44 +02:00
|
|
|
_lastTokenized = None
|
|
|
|
_lastTokenizeResult = None
|
|
|
|
def tokenize(s):
|
|
|
|
"""A utility function to create a Tokenizer and tokenize a string."""
|
|
|
|
global _lastTokenized, _lastTokenizeResult
|
|
|
|
start = time.time()
|
|
|
|
try:
|
|
|
|
if s != _lastTokenized:
|
|
|
|
_lastTokenized = s
|
|
|
|
if conf.enablePipeSyntax:
|
|
|
|
tokens = '|'
|
|
|
|
else:
|
|
|
|
tokens = ''
|
|
|
|
_lastTokenizeResult = Tokenizer(tokens).tokenize(s)
|
|
|
|
except ValueError, e:
|
|
|
|
_lastTokenized = None
|
|
|
|
_lastTokenizedResult = None
|
|
|
|
raise SyntaxError, str(e)
|
2004-01-01 21:15:25 +01:00
|
|
|
log.debug('tokenize took %s seconds.' % (time.time() - start))
|
2003-10-20 06:26:37 +02:00
|
|
|
return copy.deepcopy(_lastTokenizeResult)
|
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
|
|
|
|
2003-10-21 09:20:54 +02:00
|
|
|
def formatArgumentError(method, name=None):
|
|
|
|
if name is None:
|
|
|
|
name = method.__name__
|
|
|
|
if hasattr(method, '__doc__') and method.__doc__:
|
2003-10-24 13:31:09 +02:00
|
|
|
if conf.showOnlySyntax:
|
|
|
|
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):
|
2003-12-07 00:52:23 +01:00
|
|
|
anticap = ircdb.makeAntiCapability(command)
|
|
|
|
if ircdb.checkCapability(msg.prefix, anticap):
|
|
|
|
log.info('Preventing because of anticap: %s', msg.prefix)
|
|
|
|
return False
|
|
|
|
if ircutils.isChannel(msg.args[0]):
|
|
|
|
channel = msg.args[0]
|
|
|
|
antichancap = ircdb.makeChannelCapability(channel, anticap)
|
|
|
|
if ircdb.checkCapability(msg.prefix, antichancap):
|
|
|
|
log.info('Preventing because of antichancap: %s', msg.prefix)
|
|
|
|
return False
|
|
|
|
return conf.defaultAllow or \
|
|
|
|
ircdb.checkCapability(msg.prefix, command) or \
|
|
|
|
ircdb.checkCapability(msg.prefix, chancap)
|
|
|
|
|
2004-01-07 20:09:24 +01:00
|
|
|
|
|
|
|
class RichReplyMethods(object):
|
|
|
|
"""This is a mixin so these replies need only be defined once."""
|
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
|
|
|
|
return s
|
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def replySuccess(self, s='', **kwargs):
|
2004-01-15 13:54:10 +01:00
|
|
|
self.reply(self.__makeReply(conf.replySuccess, s), **kwargs)
|
2004-01-07 20:09:24 +01:00
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def replyError(self, s='', **kwargs):
|
2004-01-15 13:54:10 +01:00
|
|
|
self.reply(self.__makeReply(conf.replyError, s), **kwargs)
|
2004-01-07 20:09:24 +01:00
|
|
|
|
2004-01-08 16:24:56 +01:00
|
|
|
def errorNoCapability(self, capability, s='', **kwargs):
|
2004-01-12 20:19:47 +01:00
|
|
|
log.warning('Denying %s for lacking %r capability',
|
|
|
|
self.msg.prefix, capability)
|
2004-01-14 04:27:45 +01:00
|
|
|
s = self.__makeReply(conf.replyNoCapability % capability, s)
|
2004-01-09 15:20:00 +01:00
|
|
|
self.error(s, **kwargs)
|
2004-01-08 16:24:56 +01:00
|
|
|
|
2004-01-09 00:03:48 +01:00
|
|
|
def errorPossibleBug(self, s='', **kwargs):
|
|
|
|
if s:
|
|
|
|
s += ' (%s)' % conf.replyPossibleBug
|
|
|
|
self.error(s, **kwargs)
|
|
|
|
|
2004-01-08 16:24:56 +01:00
|
|
|
def errorNotRegistered(self, s='', **kwargs):
|
2004-01-15 13:54:10 +01:00
|
|
|
self.error(self.__makeReply(conf.replyNotRegistered, s), **kwargs)
|
2004-01-08 16:24:56 +01:00
|
|
|
|
|
|
|
def errorNoUser(self, s='', **kwargs):
|
2004-01-15 13:54:10 +01:00
|
|
|
self.error(self.__makeReply(conf.replyNoUser, s), **kwargs)
|
2004-01-08 16:24:56 +01:00
|
|
|
|
|
|
|
def errorRequiresPrivacy(self, s='', **kwargs):
|
2004-01-15 13:54:10 +01:00
|
|
|
self.error(self.__makeReply(conf.replyRequiresPrivacy, s), **kwargs)
|
2004-01-08 16:24:56 +01:00
|
|
|
|
2003-12-07 00:52:23 +01: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-04 20:35:02 +01:00
|
|
|
self.args = copy.deepcopy(args)
|
2003-10-30 00:02:27 +01:00
|
|
|
self.counter = 0
|
2003-11-25 12:08:57 +01:00
|
|
|
self.to = None
|
2003-10-30 00:02:27 +01:00
|
|
|
self.action = False
|
|
|
|
self.notice = False
|
|
|
|
self.private = False
|
|
|
|
self.finished = False
|
2003-12-07 00:52:23 +01:00
|
|
|
self.prefixName = conf.replyWithNickPrefix
|
2003-10-30 00:02:27 +01:00
|
|
|
self.noLengthCheck = False
|
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
|
|
|
|
|
|
|
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-01-07 13:00:59 +01:00
|
|
|
if ircutils.isCtcp(self.msg):
|
|
|
|
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-01-01 21:15:25 +01:00
|
|
|
log.debug('Trying to call %s.invalidCommand' % cb.name())
|
2003-10-30 00:02:27 +01:00
|
|
|
if self.finished:
|
2004-01-01 21:15:25 +01:00
|
|
|
log.debug('Finished calling invalidCommand: %s', cb.name())
|
|
|
|
return
|
2003-10-30 00:02:27 +01:00
|
|
|
if hasattr(cb, 'invalidCommand'):
|
2003-12-11 15:32:45 +01:00
|
|
|
try:
|
|
|
|
cb.invalidCommand(self, self.msg, self.args)
|
|
|
|
except Exception, e:
|
|
|
|
cb.log.exception('Uncaught exception in invalidCommand:')
|
|
|
|
log.warning('Uncaught exception in %s.invalidCommand, '
|
|
|
|
'continuing to call other invalidCommands.' %
|
|
|
|
cb.name())
|
2003-10-30 00:02:27 +01: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-30 00:40:14 +01:00
|
|
|
if self.irc.nick == self.msg.nick and not world.testing:
|
2003-10-28 07:57:52 +01:00
|
|
|
return
|
2003-10-28 01:22:15 +01:00
|
|
|
for cb in self.irc.callbacks:
|
|
|
|
if isinstance(cb, PrivmsgRegexp):
|
|
|
|
for (r, _) 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:
|
|
|
|
try:
|
2003-11-25 12:08:57 +01:00
|
|
|
if len(cbs) > 1:
|
|
|
|
for cb in cbs:
|
|
|
|
if cb.name().lower() == name:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
assert False, 'Non-disambiguated command.'
|
|
|
|
else:
|
|
|
|
del self.args[0]
|
|
|
|
cb = cbs[0]
|
2004-01-08 22:49:10 +01:00
|
|
|
if not checkCommandCapability(self.msg, cb, name):
|
|
|
|
self.errorNoCapability(name)
|
2003-04-01 07:39:36 +02:00
|
|
|
return
|
2003-09-17 10:12:59 +02:00
|
|
|
command = getattr(cb, name)
|
2003-11-22 08:16:34 +01:00
|
|
|
Privmsg.handled = True
|
2003-09-17 10:12:59 +02:00
|
|
|
if cb.threaded:
|
|
|
|
t = CommandThread(cb.callCommand, command,
|
|
|
|
self, self.msg, self.args)
|
|
|
|
t.start()
|
|
|
|
else:
|
|
|
|
cb.callCommand(command, self, self.msg, self.args)
|
2003-10-20 12:25:13 +02:00
|
|
|
except (getopt.GetoptError, ArgumentError):
|
2004-01-08 04:12:14 +01:00
|
|
|
self.reply(formatArgumentError(command, name=name))
|
2003-10-20 12:25:13 +02:00
|
|
|
except CannotNest, e:
|
2003-08-25 09:23:36 +02:00
|
|
|
if not isinstance(self.irc, irclib.Irc):
|
2004-01-08 04:12:14 +01:00
|
|
|
self.error('Command %r cannot be nested.' % name)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def reply(self, s, noLengthCheck=False, prefixName=True,
|
2003-11-25 12:08:57 +01:00
|
|
|
action=False, private=False, notice=False, to=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
|
|
|
"""
|
2003-09-18 09:26:21 +02:00
|
|
|
# These use |= or &= based on whether or not they default to True or
|
|
|
|
# False. Those that default to True use &=; those that default to
|
|
|
|
# False use |=.
|
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-01-08 04:12:14 +01:00
|
|
|
msg = self.msg
|
2003-09-18 09:26:21 +02:00
|
|
|
self.action |= action
|
2003-10-20 12:10:46 +02:00
|
|
|
self.notice |= notice
|
2003-11-25 12:08:57 +01:00
|
|
|
self.private |= private
|
|
|
|
self.to = to or self.to
|
2003-09-08 10:44:51 +02:00
|
|
|
self.prefixName &= prefixName
|
2003-09-18 09:26:21 +02:00
|
|
|
self.noLengthCheck |= noLengthCheck
|
2003-03-12 07:26:59 +01:00
|
|
|
if self.finalEvaled:
|
|
|
|
if isinstance(self.irc, self.__class__):
|
2004-01-08 04:12:14 +01:00
|
|
|
self.irc.reply(s, self.noLengthCheck, self.prefixName,
|
2003-11-25 12:08:57 +01:00
|
|
|
self.action, self.private, self.notice, self.to)
|
2003-09-08 10:44:51 +02:00
|
|
|
elif self.noLengthCheck:
|
2003-11-25 12:08:57 +01:00
|
|
|
self.irc.queueMsg(reply(msg, s, self.prefixName,
|
|
|
|
self.private, self.notice, self.to))
|
2003-09-18 09:26:21 +02:00
|
|
|
elif self.action:
|
2003-11-25 12:08:57 +01:00
|
|
|
if self.private:
|
|
|
|
target = msg.nick
|
|
|
|
else:
|
|
|
|
target = msg.args[0]
|
|
|
|
if self.to:
|
|
|
|
target = self.to
|
|
|
|
self.irc.queueMsg(ircmsgs.action(target, s))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-04-03 10:31:47 +02:00
|
|
|
s = ircutils.safeArgument(s)
|
2003-09-23 22:45:00 +02:00
|
|
|
allowedLength = 450 - len(self.irc.prefix)
|
2003-12-03 06:42:55 +01:00
|
|
|
if len(s) > allowedLength*50:
|
|
|
|
log.warning('Cowardly refusing to "more" %s bytes.'%len(s))
|
|
|
|
s = s[:allowedLength*50]
|
2003-12-10 20:17:48 +01:00
|
|
|
if len(s) < allowedLength:
|
|
|
|
self.irc.queueMsg(reply(msg, s, self.prefixName,
|
|
|
|
self.private,self.notice,self.to))
|
2003-12-11 15:32:45 +01:00
|
|
|
self.finished = True
|
2003-12-10 20:17:48 +01:00
|
|
|
return
|
2003-09-07 06:56:26 +02:00
|
|
|
msgs = textwrap.wrap(s, allowedLength-30) # -30 is for "nick:"
|
2003-09-07 06:05:34 +02:00
|
|
|
msgs.reverse()
|
|
|
|
response = msgs.pop()
|
|
|
|
if msgs:
|
2003-12-12 16:41:33 +01:00
|
|
|
n = ircutils.bold('(%s)')
|
|
|
|
n %= utils.nItems('message', len(msgs), 'more')
|
|
|
|
response = '%s %s' % (response, n)
|
2004-01-04 12:23:23 +01:00
|
|
|
prefix = msg.prefix
|
|
|
|
if self.to and ircutils.isNick(self.to):
|
2004-01-07 13:00:59 +01:00
|
|
|
### TODO: catch this KeyError.
|
2004-01-04 12:23:23 +01:00
|
|
|
prefix = self.getRealIrc().state.nickToHostmask(self.to)
|
|
|
|
mask = prefix.split('!', 1)[1]
|
2003-09-07 06:05:34 +02:00
|
|
|
Privmsg._mores[mask] = msgs
|
2003-09-18 09:26:21 +02:00
|
|
|
private = self.private or not ircutils.isChannel(msg.args[0])
|
|
|
|
Privmsg._mores[msg.nick] = (private, msgs)
|
2003-11-25 12:08:57 +01:00
|
|
|
self.irc.queueMsg(reply(msg, response, self.prefixName,
|
|
|
|
self.private, self.notice, self.to))
|
2003-10-28 07:06:21 +01:00
|
|
|
self.finished = True
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
self.args[self.counter] = s
|
|
|
|
self.evalArgs()
|
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def error(self, s, private=False):
|
|
|
|
"""error(text) -> replies to msg with an error message of text.
|
2003-10-04 13:34:44 +02:00
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
private=False: True if the error should be given in private.
|
|
|
|
"""
|
2003-08-27 09:45:48 +02:00
|
|
|
if isinstance(self.irc, self.__class__):
|
2004-01-08 04:12:14 +01:00
|
|
|
self.irc.error(s, private)
|
2003-08-27 09:45:48 +02:00
|
|
|
else:
|
2003-09-22 11:45:23 +02:00
|
|
|
s = 'Error: ' + s
|
2003-10-03 00:37:36 +02:00
|
|
|
if private or conf.errorReplyPrivate:
|
2004-01-08 04:12:14 +01:00
|
|
|
self.irc.queueMsg(ircmsgs.privmsg(self.msg.nick, s))
|
2003-09-22 11:45:23 +02:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
self.irc.queueMsg(reply(self.msg, s))
|
2003-10-28 07:06:21 +01:00
|
|
|
self.finished = True
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def killProxy(self):
|
2003-10-04 13:34:44 +02:00
|
|
|
"""Kills this proxy object and all its parents."""
|
2003-03-12 07:26:59 +01:00
|
|
|
if not isinstance(self.irc, irclib.Irc):
|
|
|
|
self.irc.killProxy()
|
|
|
|
self.__dict__ = {}
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2003-09-18 01:31:45 +02:00
|
|
|
def __init__(self, callCommand, command, irc, msg, args, *L):
|
2003-04-19 23:42:55 +02:00
|
|
|
self.command = command
|
2003-04-20 18:15:35 +02:00
|
|
|
world.threadsSpawned += 1
|
2003-08-25 21:50:46 +02:00
|
|
|
try:
|
|
|
|
self.commandName = command.im_func.func_name
|
|
|
|
except AttributeError:
|
|
|
|
self.commandName = command.__name__
|
|
|
|
try:
|
|
|
|
self.className = command.im_class.__name__
|
|
|
|
except AttributeError:
|
|
|
|
self.className = '<unknown>'
|
2003-04-07 17:23:12 +02:00
|
|
|
name = '%s.%s with args %r' % (self.className, self.commandName, args)
|
2003-09-17 10:12:59 +02:00
|
|
|
threading.Thread.__init__(self, target=callCommand, name=name,
|
2003-09-18 01:31:45 +02:00
|
|
|
args=(command, irc, msg, args)+L)
|
2004-01-01 21:15:25 +01:00
|
|
|
log.debug('Spawning thread %s' % name)
|
2003-03-26 03:30:05 +01:00
|
|
|
self.irc = irc
|
|
|
|
self.msg = msg
|
2003-03-27 07:04:56 +01:00
|
|
|
self.setDaemon(True)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-26 03:30:05 +01:00
|
|
|
def run(self):
|
|
|
|
try:
|
2004-01-01 21:15:25 +01:00
|
|
|
try:
|
|
|
|
original = self.command.im_class.threaded
|
|
|
|
self.command.im_class.threaded = True
|
|
|
|
threading.Thread.run(self)
|
|
|
|
finally:
|
|
|
|
self.command.im_class.threaded = original
|
2003-08-26 15:44:32 +02:00
|
|
|
except (getopt.GetoptError, ArgumentError):
|
2003-10-21 09:20:54 +02:00
|
|
|
name = self.commandName
|
2004-01-08 04:12:14 +01:00
|
|
|
self.irc.reply(formatArgumentError(self.command, name))
|
2003-08-27 09:45:48 +02:00
|
|
|
except CannotNest:
|
|
|
|
if not isinstance(self.irc.irc, irclib.Irc):
|
|
|
|
s = 'Command %r cannot be nested.' % self.commandName
|
2004-01-08 04:12:14 +01:00
|
|
|
self.irc.error(s)
|
2003-03-26 03:30:05 +01:00
|
|
|
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-01-07 20:09:24 +01:00
|
|
|
class ConfigIrcProxy(RichReplyMethods):
|
2003-08-26 19:18:35 +02:00
|
|
|
"""Used as a proxy Irc object during configuration. """
|
2003-08-23 06:42:04 +02:00
|
|
|
def __init__(self, irc):
|
|
|
|
self.__dict__['irc'] = irc
|
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def reply(self, s, *args, **kwargs):
|
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.'
|
2003-08-23 06:42:04 +02:00
|
|
|
return None
|
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def error(self, s, *args, **kwargs):
|
2003-12-05 12:52:50 +01:00
|
|
|
log.warning('ConfigIrcProxy saw an error: %s' % s)
|
2003-08-23 06:42:04 +02:00
|
|
|
|
|
|
|
def getRealIrc(self):
|
|
|
|
irc = self.__dict__['irc']
|
2003-12-05 12:52:50 +01:00
|
|
|
if hasattr(irc, 'getRealIrc'):
|
|
|
|
return irc.getRealIrc()
|
|
|
|
else:
|
|
|
|
return irc
|
2003-08-23 06:42:04 +02:00
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.getRealIrc(), attr)
|
|
|
|
|
|
|
|
def __setattr__(self, attr, value):
|
|
|
|
setattr(self.getRealIrc(), attr, value)
|
|
|
|
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
class Privmsg(irclib.IrcCallback):
|
|
|
|
"""Base class for all Privmsg handlers."""
|
|
|
|
threaded = False
|
|
|
|
public = True
|
2003-10-21 23:01:43 +02:00
|
|
|
alwaysCall = ()
|
2003-11-04 09:05:16 +01:00
|
|
|
noIgnore = False
|
2003-11-22 03:10:51 +01:00
|
|
|
handled = False
|
2004-01-01 21:15:25 +01:00
|
|
|
errored = False
|
2004-01-08 04:12:14 +01:00
|
|
|
Proxy = IrcObjectProxy
|
2003-03-26 08:02:09 +01:00
|
|
|
commandArgs = ['self', 'irc', 'msg', 'args']
|
2003-12-16 14:32:31 +01:00
|
|
|
# This must be class-scope, so all subclasses use the same one.
|
|
|
|
_mores = ircutils.IrcDict()
|
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> ...]
|
|
|
|
|
2003-10-23 10:43:50 +02:00
|
|
|
Command dispatcher for the %s plugin. Use 'list %s' to see the
|
|
|
|
commands provided by this plugin. In most cases this dispatcher
|
|
|
|
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
|
2003-11-26 19:21:12 +01:00
|
|
|
to use.""" % (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')
|
|
|
|
cb.help(irc, msg, [self.name()])
|
2003-10-20 13:34:21 +02:00
|
|
|
if args:
|
|
|
|
name = canonicalName(args[0])
|
2003-10-22 19:19:08 +02:00
|
|
|
if name == canonicalName(self.name()):
|
|
|
|
handleBadArgs()
|
|
|
|
elif self.isCommand(name):
|
2004-01-08 22:49:10 +01:00
|
|
|
if not checkCommandCapability(msg, self, name):
|
|
|
|
irc.errorNoCapability(name)
|
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:
|
|
|
|
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()
|
2003-10-29 14:06:17 +01:00
|
|
|
dispatcher = types.FunctionType(dispatcher.func_code,
|
|
|
|
dispatcher.func_globals, canonicalname)
|
2003-10-20 13:34:21 +02:00
|
|
|
if self._original:
|
|
|
|
dispatcher.__doc__ = self._original.__doc__
|
|
|
|
else:
|
|
|
|
dispatcher.__doc__ = docstring
|
|
|
|
setattr(self.__class__, canonicalname, dispatcher)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-08-23 06:42:04 +02:00
|
|
|
def configure(self, irc):
|
|
|
|
fakeIrc = ConfigIrcProxy(irc)
|
2003-08-28 18:33:45 +02:00
|
|
|
for args in conf.commandsOnStart:
|
2003-08-23 01:15:29 +02:00
|
|
|
args = args[:]
|
2003-10-24 20:53:34 +02:00
|
|
|
command = canonicalName(args.pop(0))
|
2003-08-23 01:15:29 +02:00
|
|
|
if self.isCommand(command):
|
2003-11-26 19:21:12 +01:00
|
|
|
self.log.debug('%s: %r', command, args)
|
2003-08-23 01:15:29 +02:00
|
|
|
method = getattr(self, command)
|
2003-11-15 05:37:04 +01:00
|
|
|
line = '%s %s' % (command, ' '.join(imap(utils.dqrepr, args)))
|
2003-08-23 01:15:29 +02:00
|
|
|
msg = ircmsgs.privmsg(fakeIrc.nick, line, fakeIrc.prefix)
|
|
|
|
try:
|
|
|
|
world.startup = True
|
|
|
|
method(fakeIrc, msg, args)
|
|
|
|
finally:
|
|
|
|
world.startup = False
|
|
|
|
|
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-26 19:46:47 +01:00
|
|
|
self.log.log(0, 'Ignoring %s', msg.prefix)
|
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)
|
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)
|
|
|
|
|
2003-10-14 01:20:15 +02:00
|
|
|
def callCommand(self, f, irc, msg, *L):
|
2003-12-04 01:29:06 +01:00
|
|
|
name = f.im_func.func_name
|
|
|
|
assert L, 'Odd, nothing in L. This can\'t happen.'
|
|
|
|
self.log.info('Command %s called with args %s by %s',
|
|
|
|
name, L[0], msg.prefix)
|
2003-09-17 10:12:59 +02:00
|
|
|
start = time.time()
|
2004-01-01 21:15:25 +01:00
|
|
|
try:
|
|
|
|
f(irc, msg, *L)
|
|
|
|
except (getopt.GetoptError, ArgumentError, CannotNest):
|
|
|
|
raise
|
|
|
|
except (SyntaxError, Error), e:
|
|
|
|
self.log.info('Error return: %s', e)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(str(e))
|
2004-01-01 21:15:25 +01:00
|
|
|
except Exception, e:
|
|
|
|
self.log.exception('Uncaught exception:')
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(utils.exnToString(e))
|
2004-01-01 21:15:25 +01:00
|
|
|
# Not catching getopt.GetoptError, ArgumentError, CannotNest -- those
|
|
|
|
# are handled by IrcObjectProxy.
|
2003-09-17 10:12:59 +02:00
|
|
|
elapsed = time.time() - start
|
2003-12-04 01:29:06 +01:00
|
|
|
self.log.info('%s took %s seconds', name, elapsed)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
|
2004-01-07 20:09:24 +01:00
|
|
|
class IrcObjectProxyRegexp(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-01-08 04:12:14 +01:00
|
|
|
def error(self, s, **kwargs):
|
|
|
|
self.reply('Error: ' + s, **kwargs)
|
2003-03-15 12:09:52 +01:00
|
|
|
|
2004-01-08 04:12:14 +01:00
|
|
|
def reply(self, s, action=False, **kwargs):
|
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.'
|
2003-09-23 22:45:00 +02:00
|
|
|
if action:
|
2004-01-08 04:12:14 +01:00
|
|
|
self.irc.queueMsg(ircmsgs.action(ircutils.replyTo(self.msg), s))
|
2003-09-23 22:45:00 +02:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
self.irc.queueMsg(reply(self.msg, s, **kwargs))
|
2003-03-15 12:09:52 +01:00
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.irc, attr)
|
|
|
|
|
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-01-08 04:12:14 +01:00
|
|
|
Proxy = IrcObjectProxyRegexp
|
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:
|
2003-11-26 19:21:12 +01:00
|
|
|
self.log.exception('Uncaught exception from callCommand:')
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(utils.exnToString(e))
|
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-01-08 04:12:14 +01:00
|
|
|
Proxy = IrcObjectProxyRegexp
|
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-08 04:12:14 +01:00
|
|
|
irc.error(utils.exnToString(e))
|
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
|
2003-10-03 00:37:36 +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:
|