2003-08-28 18:33:45 +02: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-09-03 11:50:04 +02:00
|
|
|
"""
|
|
|
|
Provides commands useful to the owner of the bot; the commands here require
|
|
|
|
their caller to have the 'owner' capability. This plugin is loaded by default.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:38:19 +01:00
|
|
|
__revision__ = "$Id$"
|
2004-04-28 08:30:55 +02:00
|
|
|
__author__ = 'Jeremy Fincher (jemfinch) <jemfinch@users.sf.net>'
|
2003-11-25 09:38:19 +01:00
|
|
|
|
2003-10-05 14:47:19 +02:00
|
|
|
import fix
|
2003-09-12 20:07:04 +02:00
|
|
|
|
2003-08-28 18:33:45 +02:00
|
|
|
import gc
|
2003-10-14 05:34:47 +02:00
|
|
|
import os
|
2003-08-28 18:33:45 +02:00
|
|
|
import imp
|
|
|
|
import sys
|
2004-01-15 13:55:37 +01:00
|
|
|
import sets
|
2004-01-15 15:08:14 +01:00
|
|
|
import getopt
|
2004-03-28 14:11:09 +02:00
|
|
|
import logging
|
2003-08-28 18:33:45 +02:00
|
|
|
import linecache
|
|
|
|
|
2003-11-26 19:21:12 +01:00
|
|
|
import log
|
2003-08-28 18:33:45 +02:00
|
|
|
import conf
|
2003-09-04 23:41:31 +02:00
|
|
|
import utils
|
2003-08-28 18:33:45 +02:00
|
|
|
import world
|
2003-09-13 17:13:46 +02:00
|
|
|
import ircdb
|
2003-09-25 09:57:17 +02:00
|
|
|
import irclib
|
2003-08-28 18:33:45 +02:00
|
|
|
import ircmsgs
|
|
|
|
import drivers
|
|
|
|
import privmsgs
|
2004-01-18 08:58:26 +01:00
|
|
|
import registry
|
2003-08-28 18:33:45 +02:00
|
|
|
import callbacks
|
|
|
|
|
2004-01-15 15:08:14 +01:00
|
|
|
class Deprecated(ImportError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def loadPluginModule(name, ignoreDeprecation=False):
|
2003-10-04 15:24:51 +02:00
|
|
|
"""Loads (and returns) the module for the plugin with the given name."""
|
2003-10-14 05:34:47 +02:00
|
|
|
files = []
|
2004-01-18 08:58:26 +01:00
|
|
|
pluginDirs = conf.supybot.directories.plugins()
|
|
|
|
for dir in pluginDirs:
|
2003-11-22 18:06:23 +01:00
|
|
|
try:
|
|
|
|
files.extend(os.listdir(dir))
|
2004-01-15 15:08:14 +01:00
|
|
|
except EnvironmentError: # OSError, IOError superclass.
|
2003-11-26 19:21:12 +01:00
|
|
|
log.warning('Invalid plugin directory: %s', dir)
|
2003-10-14 05:34:47 +02:00
|
|
|
loweredFiles = map(str.lower, files)
|
|
|
|
try:
|
2003-11-15 05:37:04 +01:00
|
|
|
index = loweredFiles.index(name.lower()+'.py')
|
2003-10-14 05:34:47 +02:00
|
|
|
name = os.path.splitext(files[index])[0]
|
2004-02-04 19:01:00 +01:00
|
|
|
if name in sys.modules:
|
|
|
|
m = sys.modules[name]
|
|
|
|
if not hasattr(m, 'Class'):
|
|
|
|
raise ImportError, 'Module is not a plugin.'
|
2003-10-14 05:34:47 +02:00
|
|
|
except ValueError: # We'd rather raise the ImportError, so we'll let go...
|
|
|
|
pass
|
2004-01-18 08:58:26 +01:00
|
|
|
moduleInfo = imp.find_module(name, pluginDirs)
|
2004-02-06 10:19:21 +01:00
|
|
|
try:
|
|
|
|
module = imp.load_module(name, *moduleInfo)
|
|
|
|
except:
|
2004-02-16 17:16:13 +01:00
|
|
|
if name in sys.modules:
|
|
|
|
del sys.modules[name]
|
2004-02-06 10:19:21 +01:00
|
|
|
raise
|
2004-01-15 15:08:14 +01:00
|
|
|
if 'deprecated' in module.__dict__ and module.deprecated:
|
|
|
|
if ignoreDeprecation:
|
|
|
|
log.warning('Deprecated plugin loaded: %s', name)
|
|
|
|
else:
|
2004-01-15 15:27:22 +01:00
|
|
|
raise Deprecated, 'Attempted to load deprecated plugin %r' % name
|
2003-12-09 22:33:13 +01:00
|
|
|
if module.__name__ in sys.modules:
|
|
|
|
sys.modules[module.__name__] = module
|
2003-09-18 07:47:42 +02:00
|
|
|
linecache.checkcache()
|
|
|
|
return module
|
|
|
|
|
2004-04-28 10:42:01 +02:00
|
|
|
def loadPluginClass(irc, module, register=None):
|
2003-10-04 15:24:51 +02:00
|
|
|
"""Loads the plugin Class from the given module into the given irc."""
|
2004-04-28 10:42:01 +02:00
|
|
|
cb = module.Class()
|
|
|
|
name = cb.name()
|
|
|
|
public = True
|
|
|
|
if hasattr(cb, 'public'):
|
|
|
|
public = cb.public
|
|
|
|
conf.registerPlugin(name, register)
|
|
|
|
conf.supybot.plugins.get(name).register('public',
|
|
|
|
registry.Boolean(public, """Determines whether this plugin is
|
|
|
|
publically visible."""))
|
|
|
|
assert not irc.getCallback(name)
|
|
|
|
irc.addCallback(cb)
|
|
|
|
return cb
|
|
|
|
|
|
|
|
conf.registerPlugin('Owner', True)
|
|
|
|
conf.supybot.plugins.Owner.register('public', registry.Boolean(True,
|
|
|
|
"""Determines whether this plugin is publically visible."""))
|
2004-01-25 09:22:50 +01:00
|
|
|
conf.registerGroup(conf.supybot, 'commands')
|
2004-02-08 00:22:47 +01:00
|
|
|
conf.registerGroup(conf.supybot.commands, 'defaultPlugins')
|
|
|
|
conf.supybot.commands.defaultPlugins.help = utils.normalizeWhitespace("""
|
|
|
|
Determines what commands have default plugins set, and which plugins are set to
|
|
|
|
be the default for each of those commands.""".strip())
|
2003-09-18 07:47:42 +02:00
|
|
|
|
2004-02-08 00:22:47 +01:00
|
|
|
def registerDefaultPlugin(command, plugin):
|
|
|
|
command = callbacks.canonicalName(command)
|
|
|
|
conf.registerGlobalValue(conf.supybot.commands.defaultPlugins,
|
|
|
|
command, registry.String(plugin, ''))
|
2004-04-29 13:38:02 +02:00
|
|
|
# This must be set, or the quotes won't be removed.
|
|
|
|
conf.supybot.commands.defaultPlugins.get(command).set(plugin)
|
2004-02-08 00:22:47 +01:00
|
|
|
|
|
|
|
registerDefaultPlugin('ignore', 'Admin')
|
2004-02-20 07:39:35 +01:00
|
|
|
registerDefaultPlugin('unignore', 'Admin')
|
2004-02-08 00:22:47 +01:00
|
|
|
registerDefaultPlugin('addcapability', 'Admin')
|
|
|
|
registerDefaultPlugin('removecapability', 'Admin')
|
|
|
|
registerDefaultPlugin('list', 'Misc')
|
|
|
|
registerDefaultPlugin('help', 'Misc')
|
|
|
|
registerDefaultPlugin('reload', 'Owner')
|
|
|
|
registerDefaultPlugin('capabilities', 'User')
|
2004-02-07 12:48:03 +01:00
|
|
|
|
|
|
|
class holder(object):
|
|
|
|
pass
|
|
|
|
|
2004-03-28 14:11:09 +02:00
|
|
|
# This is used so we can support a "log" command as well as a "self.log"
|
|
|
|
# Logger.
|
2004-02-07 12:48:03 +01:00
|
|
|
class LogProxy(object):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Logs <text> to the global supybot log at critical priority. Useful for
|
|
|
|
marking logfiles for later searching.
|
|
|
|
"""
|
2004-04-20 12:59:20 +02:00
|
|
|
__name__ = 'log' # Necessary for help.
|
2004-02-07 12:48:03 +01:00
|
|
|
def __init__(self, log):
|
|
|
|
self.log = log
|
|
|
|
self.im_func = holder()
|
|
|
|
self.im_func.func_name = 'log'
|
|
|
|
|
|
|
|
def __call__(self, irc, msg, args):
|
|
|
|
text = privmsgs.getArgs(args)
|
|
|
|
log.critical(text)
|
|
|
|
irc.replySuccess()
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.log, attr)
|
|
|
|
|
|
|
|
|
2004-03-28 14:11:09 +02:00
|
|
|
class LogErrorHandler(logging.Handler):
|
|
|
|
irc = None
|
|
|
|
def handle(self, record):
|
|
|
|
if record.levelno >= logging.ERROR:
|
|
|
|
if record.exc_info:
|
|
|
|
(_, e, _) = record.exc_info
|
|
|
|
s = 'Uncaught exception in %s: %s' % (record.module, e)
|
|
|
|
else:
|
|
|
|
s = record.msg
|
|
|
|
# Send to the owner dudes.
|
2004-06-18 22:18:14 +02:00
|
|
|
|
2004-03-28 14:11:09 +02:00
|
|
|
|
2003-10-21 08:03:57 +02:00
|
|
|
class Owner(privmsgs.CapabilityCheckingPrivmsg):
|
2003-10-21 23:01:43 +02:00
|
|
|
# This plugin must be first; its priority must be lowest; otherwise odd
|
|
|
|
# things will happen when adding callbacks.
|
|
|
|
priority = ~sys.maxint-1 # This must be first!
|
2003-08-28 18:33:45 +02:00
|
|
|
capability = 'owner'
|
2004-01-19 23:38:09 +01:00
|
|
|
_srcPlugins = ('Admin', 'Channel', 'Config', 'Misc', 'Owner', 'User')
|
2003-08-28 18:33:45 +02:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
2004-02-07 12:48:03 +01:00
|
|
|
self.log = LogProxy(self.log)
|
2004-01-20 17:16:25 +01:00
|
|
|
setattr(self.__class__, 'exec', self.__class__._exec)
|
2004-01-20 13:09:54 +01:00
|
|
|
for (name, s) in registry._cache.iteritems():
|
2004-01-18 08:58:26 +01:00
|
|
|
if name.startswith('supybot.plugins'):
|
|
|
|
try:
|
|
|
|
(_, _, name) = name.split('.')
|
2004-02-08 10:58:45 +01:00
|
|
|
except ValueError: # unpack list of wrong size.
|
2004-01-18 08:58:26 +01:00
|
|
|
continue
|
2004-04-30 20:24:35 +02:00
|
|
|
if name == name.lower(): # This can't be right.
|
|
|
|
name = name.capitalize() # Let's at least capitalize it.
|
2004-01-19 23:38:09 +01:00
|
|
|
conf.registerPlugin(name)
|
2004-02-08 10:58:45 +01:00
|
|
|
if name.startswith('supybot.commands.defaultPlugins'):
|
|
|
|
try:
|
|
|
|
(_, _, _, name) = name.split('.')
|
|
|
|
except ValueError: # unpack list of wrong size.
|
|
|
|
continue
|
|
|
|
registerDefaultPlugin(name, s)
|
2004-01-18 08:58:26 +01:00
|
|
|
|
2004-02-07 12:48:03 +01:00
|
|
|
def isCommand(self, methodName):
|
|
|
|
return methodName == 'log' or \
|
|
|
|
privmsgs.CapabilityCheckingPrivmsg.isCommand(self, methodName)
|
|
|
|
|
2004-02-13 08:24:30 +01:00
|
|
|
def reset(self):
|
|
|
|
# This has to be done somewhere, I figure here is as good place as any.
|
|
|
|
callbacks.Privmsg._mores.clear()
|
|
|
|
privmsgs.CapabilityCheckingPrivmsg.reset(self)
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
def do001(self, irc, msg):
|
2004-04-28 10:42:01 +02:00
|
|
|
if len(irc.callbacks) < 6:
|
|
|
|
self.log.info('Loading other src/ plugins.')
|
2004-01-18 08:58:26 +01:00
|
|
|
for s in ('Admin', 'Channel', 'Config', 'Misc', 'User'):
|
2004-01-21 20:13:20 +01:00
|
|
|
if irc.getCallback(s) is None:
|
|
|
|
self.log.info('Loading %s.' % s)
|
|
|
|
m = loadPluginModule(s)
|
|
|
|
loadPluginClass(irc, m)
|
|
|
|
self.log.info('Loading plugins/ plugins.')
|
2004-01-27 12:29:54 +01:00
|
|
|
for (name, value) in conf.supybot.plugins.getValues(fullNames=False):
|
2004-04-28 10:42:01 +02:00
|
|
|
if name.lower() == 'owner':
|
|
|
|
continue # Just in case.
|
2004-02-04 06:56:51 +01:00
|
|
|
if irc.getCallback(name) is None:
|
|
|
|
if value():
|
|
|
|
if not irc.getCallback(name):
|
|
|
|
self.log.info('Loading %s.' % name)
|
|
|
|
try:
|
|
|
|
m = loadPluginModule(name)
|
|
|
|
loadPluginClass(irc, m)
|
2004-04-01 13:47:02 +02:00
|
|
|
except ImportError, e:
|
|
|
|
log.warning('Failed to load %s: %s', name, e)
|
2004-02-04 06:56:51 +01:00
|
|
|
except Exception, e:
|
2004-02-12 01:49:41 +01:00
|
|
|
log.exception('Failed to load %s:', name)
|
2004-02-04 06:56:51 +01:00
|
|
|
else:
|
|
|
|
# Let's import the module so configuration is preserved.
|
2004-02-12 01:49:41 +01:00
|
|
|
try:
|
|
|
|
_ = loadPluginModule(name)
|
|
|
|
except Exception, e:
|
2004-04-19 07:06:36 +02:00
|
|
|
log.info('Attempted to load %s to preserve its '
|
|
|
|
'configuration, but load failed: %s',
|
|
|
|
name, e)
|
2004-02-12 01:49:41 +01:00
|
|
|
world.starting = False
|
2003-10-30 04:08:52 +01:00
|
|
|
|
2003-11-12 03:18:22 +01:00
|
|
|
def disambiguate(self, irc, tokens, ambiguousCommands=None):
|
2004-01-01 21:16:45 +01:00
|
|
|
"""Disambiguates the given tokens based on the plugins loaded and
|
|
|
|
commands available in the given irc. Returns a dictionary of
|
|
|
|
ambiguous commands, mapping the command to the plugins it's
|
|
|
|
available in."""
|
2003-11-12 03:18:22 +01:00
|
|
|
if ambiguousCommands is None:
|
|
|
|
ambiguousCommands = {}
|
2003-10-30 04:08:52 +01:00
|
|
|
if tokens:
|
|
|
|
command = callbacks.canonicalName(tokens[0])
|
2004-01-25 09:22:50 +01:00
|
|
|
try:
|
|
|
|
plugin = conf.supybot.commands.defaultPlugins.get(command)()
|
2004-01-26 16:10:04 +01:00
|
|
|
if plugin and plugin != '(Unused)':
|
2004-01-25 09:22:50 +01:00
|
|
|
tokens.insert(0, plugin)
|
|
|
|
else:
|
|
|
|
raise registry.NonExistentRegistryEntry
|
|
|
|
except registry.NonExistentRegistryEntry:
|
2003-11-03 05:36:40 +01:00
|
|
|
cbs = callbacks.findCallbackForCommand(irc, command)
|
|
|
|
if len(cbs) > 1:
|
|
|
|
names = [cb.name() for cb in cbs]
|
2003-11-09 15:01:36 +01:00
|
|
|
srcs = [name for name in names if name in self._srcPlugins]
|
|
|
|
if len(srcs) == 1:
|
|
|
|
tokens.insert(0, srcs[0])
|
2003-11-03 05:36:40 +01:00
|
|
|
else:
|
|
|
|
ambiguousCommands[command] = names
|
2003-10-30 04:08:52 +01:00
|
|
|
for elt in tokens:
|
|
|
|
if isinstance(elt, list):
|
2003-11-12 03:18:22 +01:00
|
|
|
self.disambiguate(irc, elt, ambiguousCommands)
|
2004-01-01 21:16:45 +01:00
|
|
|
return ambiguousCommands
|
2003-08-28 18:33:45 +02:00
|
|
|
|
2004-02-20 06:33:12 +01:00
|
|
|
def processTokens(self, irc, msg, tokens):
|
|
|
|
ambiguousCommands = self.disambiguate(irc, tokens)
|
|
|
|
if ambiguousCommands:
|
|
|
|
if len(ambiguousCommands) == 1: # Common case.
|
|
|
|
(command, names) = ambiguousCommands.popitem()
|
|
|
|
names.sort()
|
|
|
|
s = 'The command %r is available in the %s plugins. ' \
|
|
|
|
'Please specify the plugin whose command you ' \
|
|
|
|
'wish to call by using its name as a command ' \
|
|
|
|
'before calling it.' % \
|
|
|
|
(command, utils.commaAndify(names))
|
|
|
|
else:
|
|
|
|
L = []
|
|
|
|
for (command, names) in ambiguousCommands.iteritems():
|
|
|
|
names.sort()
|
|
|
|
L.append('The command %r is available in the %s '
|
|
|
|
'plugins' %
|
|
|
|
(command, utils.commaAndify(names)))
|
|
|
|
s = '%s; please specify from which plugins to ' \
|
|
|
|
'call these commands.' % '; '.join(L)
|
|
|
|
irc.queueMsg(callbacks.error(msg, s))
|
|
|
|
else:
|
|
|
|
callbacks.IrcObjectProxy(irc, msg, tokens)
|
|
|
|
|
2003-10-21 23:01:43 +02:00
|
|
|
def doPrivmsg(self, irc, msg):
|
|
|
|
callbacks.Privmsg.handled = False
|
2004-01-01 21:16:45 +01:00
|
|
|
callbacks.Privmsg.errored = False
|
2003-11-04 18:34:48 +01:00
|
|
|
if ircdb.checkIgnored(msg.prefix):
|
|
|
|
return
|
2003-10-28 01:22:15 +01:00
|
|
|
s = callbacks.addressed(irc.nick, msg)
|
|
|
|
if s:
|
2004-05-07 18:14:02 +02:00
|
|
|
brackets = conf.supybot.reply.brackets.get(msg.args[0])()
|
2003-10-31 19:18:04 +01:00
|
|
|
try:
|
2004-05-07 18:14:02 +02:00
|
|
|
tokens = callbacks.tokenize(s, brackets=brackets)
|
2003-11-25 23:52:04 +01:00
|
|
|
if tokens and isinstance(tokens[0], list):
|
|
|
|
s = 'The command called may not be the result ' \
|
|
|
|
'of a nested command.'
|
|
|
|
irc.queueMsg(callbacks.error(msg, s))
|
|
|
|
return
|
2004-02-20 06:33:12 +01:00
|
|
|
self.processTokens(irc, msg, tokens)
|
2003-10-31 19:18:04 +01:00
|
|
|
except SyntaxError, e:
|
2004-01-01 21:16:45 +01:00
|
|
|
callbacks.Privmsg.errored = True
|
2003-10-31 19:18:04 +01:00
|
|
|
irc.queueMsg(callbacks.error(msg, str(e)))
|
|
|
|
return
|
2003-10-30 04:08:52 +01:00
|
|
|
|
2004-01-18 20:35:36 +01:00
|
|
|
if conf.allowEval:
|
|
|
|
def eval(self, irc, msg, args):
|
|
|
|
"""<expression>
|
2003-09-02 21:55:53 +02:00
|
|
|
|
2004-04-15 08:22:01 +02:00
|
|
|
Evaluates <expression> (which should be a Python expression) and
|
|
|
|
returns its value. If an exception is raised, reports the
|
|
|
|
exception.
|
2004-01-18 20:35:36 +01:00
|
|
|
"""
|
|
|
|
if conf.allowEval:
|
|
|
|
s = privmsgs.getArgs(args)
|
|
|
|
try:
|
|
|
|
irc.reply(repr(eval(s)))
|
|
|
|
except SyntaxError, e:
|
|
|
|
irc.reply('%s: %r' % (utils.exnToString(e), s))
|
|
|
|
except Exception, e:
|
|
|
|
irc.reply(utils.exnToString(e))
|
|
|
|
else:
|
2004-01-20 17:16:25 +01:00
|
|
|
# This should never happen, so I haven't bothered updating
|
2004-02-05 08:53:00 +01:00
|
|
|
# this error string to say --allow-eval.
|
|
|
|
irc.error('You must run supybot with the --allow-eval '
|
|
|
|
'option for this command to be enabled.')
|
2003-08-28 18:33:45 +02:00
|
|
|
|
2004-01-18 20:35:36 +01:00
|
|
|
def _exec(self, irc, msg, args):
|
|
|
|
"""<statement>
|
2003-09-02 21:55:53 +02:00
|
|
|
|
2004-01-18 20:35:36 +01:00
|
|
|
Execs <code>. Returns success if it didn't raise any exceptions.
|
|
|
|
"""
|
|
|
|
if conf.allowEval:
|
|
|
|
s = privmsgs.getArgs(args)
|
|
|
|
try:
|
|
|
|
exec s
|
|
|
|
irc.replySuccess()
|
|
|
|
except Exception, e:
|
|
|
|
irc.reply(utils.exnToString(e))
|
|
|
|
else:
|
2004-04-18 02:38:20 +02:00
|
|
|
# This should never happen.
|
2004-02-05 08:53:00 +01:00
|
|
|
irc.error('You must run supybot with the --allow-eval '
|
|
|
|
'option for this command to be enabled.')
|
2004-01-20 17:16:25 +01:00
|
|
|
else:
|
|
|
|
def eval(self, irc, msg, args):
|
2004-02-05 08:53:00 +01:00
|
|
|
"""Run your bot with --allow-eval if you want this to work."""
|
|
|
|
irc.error('You must give your bot the --allow-eval option for '
|
2004-01-20 17:16:25 +01:00
|
|
|
'this command to be enabled.')
|
|
|
|
_exec = eval
|
2004-06-18 22:18:14 +02:00
|
|
|
|
2004-04-18 02:38:20 +02:00
|
|
|
def announce(self, irc, msg, args):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Sends <text> to all channels the bot is currently on and not
|
|
|
|
lobotomized in.
|
|
|
|
"""
|
|
|
|
text = privmsgs.getArgs(args)
|
|
|
|
u = ircdb.users.getUser(msg.prefix)
|
|
|
|
text = 'Announcement from my owner (%s): %s' % (u.name, text)
|
|
|
|
for channel in irc.state.channels:
|
|
|
|
c = ircdb.channels.getChannel(channel)
|
|
|
|
if not c.lobotomized:
|
|
|
|
irc.queueMsg(ircmsgs.privmsg(channel, text))
|
2004-06-18 22:18:14 +02:00
|
|
|
|
2004-02-08 00:22:47 +01:00
|
|
|
def defaultplugin(self, irc, msg, args):
|
|
|
|
"""[--remove] <command> [<plugin>]
|
2003-08-28 18:33:45 +02:00
|
|
|
|
2004-02-08 00:22:47 +01:00
|
|
|
Sets the default plugin for <command> to <plugin>. If --remove is
|
|
|
|
given, removes the current default plugin for <command>. If no plugin
|
|
|
|
is given, returns the current default plugin set for <command>.
|
|
|
|
"""
|
|
|
|
remove = False
|
|
|
|
(optlist, rest) = getopt.getopt(args, '', ['remove'])
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == '--remove':
|
|
|
|
remove = True
|
|
|
|
(command, plugin) = privmsgs.getArgs(rest, optional=1)
|
|
|
|
command = callbacks.canonicalName(command)
|
|
|
|
cbs = callbacks.findCallbackForCommand(irc, command)
|
2004-06-18 22:18:14 +02:00
|
|
|
def isDispatcher(cb):
|
|
|
|
name = callbacks.canonicalName(getattr(cb, 'name')())
|
|
|
|
return getattr(cb, name).isDispatcher
|
|
|
|
# Ensure someone isn't trying to use a plugin for their command
|
|
|
|
cbs = [cb for cb in cbs if not isDispatcher(cb)]
|
2004-02-08 00:22:47 +01:00
|
|
|
if remove:
|
2004-06-18 22:18:14 +02:00
|
|
|
try:
|
|
|
|
conf.supybot.commands.defaultPlugins.unregister(command)
|
|
|
|
irc.replySuccess()
|
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
raise callbacks.ArgumentError
|
2004-02-08 00:22:47 +01:00
|
|
|
elif not cbs:
|
|
|
|
irc.error('That\'s not a valid command.')
|
|
|
|
return
|
|
|
|
elif plugin:
|
|
|
|
registerDefaultPlugin(command, plugin)
|
|
|
|
irc.replySuccess()
|
|
|
|
else:
|
2004-04-28 12:31:15 +02:00
|
|
|
try:
|
|
|
|
irc.reply(conf.supybot.commands.defaultPlugins.get(command)())
|
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
s = 'I don\'t have a default plugin set for that command.'
|
|
|
|
irc.error(s)
|
2004-04-19 07:06:36 +02:00
|
|
|
|
2003-08-28 18:33:45 +02:00
|
|
|
def ircquote(self, irc, msg, args):
|
|
|
|
"""<string to be sent to the server>
|
|
|
|
|
|
|
|
Sends the raw string given to the server.
|
|
|
|
"""
|
|
|
|
s = privmsgs.getArgs(args)
|
|
|
|
try:
|
|
|
|
m = ircmsgs.IrcMsg(s)
|
2003-11-26 19:21:12 +01:00
|
|
|
except Exception, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(utils.exnToString(e))
|
2003-11-26 19:21:12 +01:00
|
|
|
else:
|
2003-08-28 18:33:45 +02:00
|
|
|
irc.queueMsg(m)
|
|
|
|
|
|
|
|
def quit(self, irc, msg, args):
|
2004-01-15 00:51:58 +01:00
|
|
|
"""takes no arguments
|
2003-08-28 18:33:45 +02:00
|
|
|
|
2004-01-15 00:51:58 +01:00
|
|
|
Exits the bot.
|
2003-08-28 18:33:45 +02:00
|
|
|
"""
|
2004-04-07 17:52:28 +02:00
|
|
|
world.ircs[:] = []
|
2003-08-28 18:33:45 +02:00
|
|
|
|
|
|
|
def flush(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
2004-03-28 14:11:09 +02:00
|
|
|
Runs all the periodic flushers in world.flushers. This includes
|
|
|
|
flushing all logs and all configuration changes to disk.
|
2003-08-28 18:33:45 +02:00
|
|
|
"""
|
|
|
|
world.flush()
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-08-28 18:33:45 +02:00
|
|
|
|
|
|
|
def upkeep(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Runs the standard upkeep stuff (flushes and gc.collects()).
|
|
|
|
"""
|
2004-02-17 19:10:27 +01:00
|
|
|
collected = world.upkeep(scheduleNext=False)
|
2003-08-28 18:33:45 +02:00
|
|
|
if gc.garbage:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('Garbage! %r' % gc.garbage)
|
2003-08-28 18:33:45 +02:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('%s collected.' % utils.nItems('object', collected))
|
2003-08-28 18:33:45 +02:00
|
|
|
|
|
|
|
def load(self, irc, msg, args):
|
2004-01-15 15:08:14 +01:00
|
|
|
"""[--deprecated] <plugin>
|
2003-08-28 18:33:45 +02:00
|
|
|
|
2003-10-14 05:06:47 +02:00
|
|
|
Loads the plugin <plugin> from any of the directories in
|
2004-01-18 08:58:26 +01:00
|
|
|
conf.supybot.directories.plugins; usually this includes the main
|
|
|
|
installed directory and 'plugins' in the current directory.
|
|
|
|
--deprecated is necessary if you wish to load deprecated plugins.
|
2003-08-28 18:33:45 +02:00
|
|
|
"""
|
2004-01-15 15:08:14 +01:00
|
|
|
(optlist, args) = getopt.getopt(args, '', ['deprecated'])
|
|
|
|
ignoreDeprecation = False
|
|
|
|
for (option, argument) in optlist:
|
|
|
|
if option == '--deprecated':
|
|
|
|
ignoreDeprecation = True
|
2003-08-28 18:33:45 +02:00
|
|
|
name = privmsgs.getArgs(args)
|
2004-01-15 15:08:14 +01:00
|
|
|
if name.endswith('.py'):
|
|
|
|
name = name[:-3]
|
2003-10-24 11:03:34 +02:00
|
|
|
if irc.getCallback(name):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('That module is already loaded.')
|
2003-10-24 11:03:34 +02:00
|
|
|
return
|
2003-08-28 18:33:45 +02:00
|
|
|
try:
|
2004-01-15 15:08:14 +01:00
|
|
|
module = loadPluginModule(name, ignoreDeprecation)
|
|
|
|
except Deprecated:
|
|
|
|
irc.error('Plugin %r is deprecated. '
|
2004-02-03 01:16:07 +01:00
|
|
|
'Use --deprecated to force it to load.' % name)
|
2004-01-15 15:08:14 +01:00
|
|
|
return
|
2003-10-04 00:28:05 +02:00
|
|
|
except ImportError, e:
|
|
|
|
if name in str(e):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('No plugin %s exists.' % name)
|
2003-10-04 00:28:05 +02:00
|
|
|
else:
|
2004-02-04 19:01:00 +01:00
|
|
|
irc.error(str(e))
|
2003-08-28 18:33:45 +02:00
|
|
|
return
|
2004-04-30 20:24:35 +02:00
|
|
|
cb = loadPluginClass(irc, module)
|
|
|
|
name = cb.name() # Let's normalize this.
|
2004-01-19 23:38:09 +01:00
|
|
|
conf.registerPlugin(name, True)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-08-28 18:33:45 +02:00
|
|
|
|
|
|
|
def reload(self, irc, msg, args):
|
2003-09-05 20:47:58 +02:00
|
|
|
"""<plugin>
|
2003-08-28 18:33:45 +02:00
|
|
|
|
2003-12-16 20:57:18 +01:00
|
|
|
Unloads and subsequently reloads the plugin by name; use the 'list'
|
|
|
|
command to see a list of the currently loaded plugins.
|
2003-08-28 18:33:45 +02:00
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
|
|
|
callbacks = irc.removeCallback(name)
|
|
|
|
if callbacks:
|
2003-10-22 00:24:13 +02:00
|
|
|
module = sys.modules[callbacks[0].__module__]
|
|
|
|
if hasattr(module, 'reload'):
|
|
|
|
x = module.reload()
|
2003-08-28 18:33:45 +02:00
|
|
|
try:
|
2003-09-18 07:52:55 +02:00
|
|
|
module = loadPluginModule(name)
|
2003-10-22 00:24:13 +02:00
|
|
|
if hasattr(module, 'reload'):
|
|
|
|
module.reload(x)
|
2003-09-06 03:13:43 +02:00
|
|
|
for callback in callbacks:
|
|
|
|
callback.die()
|
|
|
|
del callback
|
|
|
|
gc.collect()
|
2003-09-18 07:52:55 +02:00
|
|
|
callback = loadPluginClass(irc, module)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-08-28 18:33:45 +02:00
|
|
|
except ImportError:
|
|
|
|
for callback in callbacks:
|
|
|
|
irc.addCallback(callback)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('No plugin %s exists.' % name)
|
2003-08-28 18:33:45 +02:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There was no callback %s.' % name)
|
2003-08-28 18:33:45 +02:00
|
|
|
|
|
|
|
def unload(self, irc, msg, args):
|
2003-09-05 20:47:58 +02:00
|
|
|
"""<plugin>
|
2003-08-28 18:33:45 +02:00
|
|
|
|
|
|
|
Unloads the callback by name; use the 'list' command to see a list
|
|
|
|
of the currently loaded callbacks.
|
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
|
|
|
callbacks = irc.removeCallback(name)
|
|
|
|
if callbacks:
|
|
|
|
for callback in callbacks:
|
|
|
|
callback.die()
|
|
|
|
del callback
|
|
|
|
gc.collect()
|
2004-01-19 23:38:09 +01:00
|
|
|
conf.registerPlugin(name, False)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-08-28 18:33:45 +02:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There was no callback %s' % name)
|
2003-08-28 18:33:45 +02:00
|
|
|
|
|
|
|
|
2003-10-21 08:03:57 +02:00
|
|
|
Class = Owner
|
2003-08-28 18:33:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
|
|
|
|