Limnoria/src/Owner.py

859 lines
34 KiB
Python
Raw Normal View History

2003-08-28 18:33:45 +02:00
#!/usr/bin/env python
###
# Copyright (c) 2002-2004, Jeremiah Fincher
2003-08-28 18:33:45 +02: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-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
2004-07-24 07:18:26 +02:00
import supybot.fix as fix
2003-09-12 20:07:04 +02:00
2003-08-28 18:33:45 +02:00
import gc
import os
2003-08-28 18:33:45 +02:00
import imp
import sre
2003-08-28 18:33:45 +02:00
import sys
2004-01-15 15:08:14 +01:00
import getopt
import socket
import logging
2003-08-28 18:33:45 +02:00
import linecache
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.drivers as drivers
2004-07-24 07:18:26 +02:00
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
2004-07-24 07:18:26 +02:00
import supybot.privmsgs as privmsgs
import supybot.registry as registry
import supybot.callbacks as callbacks
2003-08-28 18:33:45 +02:00
2004-01-15 15:08:14 +01:00
class Deprecated(ImportError):
pass
def loadPluginModule(name, ignoreDeprecation=False):
"""Loads (and returns) the module for the plugin with the given name."""
files = []
2004-01-18 08:58:26 +01:00
pluginDirs = conf.supybot.directories.plugins()
for dir in pluginDirs:
try:
files.extend(os.listdir(dir))
2004-01-15 15:08:14 +01:00
except EnvironmentError: # OSError, IOError superclass.
2004-07-28 04:58:10 +02:00
log.warning('Invalid plugin directory: %r; removing.', dir)
conf.supybot.directories.plugins().remove(dir)
loweredFiles = map(str.lower, files)
try:
index = loweredFiles.index(name.lower()+'.py')
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.'
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)
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]
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
if module.__name__ in sys.modules:
sys.modules[module.__name__] = module
linecache.checkcache()
return module
2004-04-28 10:42:01 +02:00
def loadPluginClass(irc, module, register=None):
"""Loads the plugin Class from the given module into the given Irc."""
2004-08-02 01:19:59 +02:00
try:
cb = module.Class()
except AttributeError:
raise callbacks.Error, 'This plugin module doesn\'t have a "Class" ' \
'attribute to specify which plugin should be ' \
'instantiated. If you didn\'t write this ' \
'plugin, but received it with Supybot, file ' \
'a bug with us about this error.'
2004-04-28 10:42:01 +02:00
name = cb.name()
public = True
if hasattr(cb, 'public'):
public = cb.public
conf.registerPlugin(name, register, public)
2004-04-28 10:42:01 +02:00
assert not irc.getCallback(name)
irc.addCallback(cb)
return cb
conf.registerPlugin('Owner', True)
conf.supybot.plugins.Owner.register('public', registry.Boolean(True,
2004-08-19 01:15:27 +02:00
"""Determines whether this plugin is publicly visible."""))
###
# supybot.commands.
###
2004-08-11 14:57:52 +02: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())
def registerDefaultPlugin(command, plugin):
command = callbacks.canonicalName(command)
conf.registerGlobalValue(conf.supybot.commands.defaultPlugins,
command, registry.String(plugin, ''))
# This must be set, or the quotes won't be removed.
conf.supybot.commands.defaultPlugins.get(command).set(plugin)
registerDefaultPlugin('ignore', 'Admin')
2004-02-20 07:39:35 +01:00
registerDefaultPlugin('unignore', 'Admin')
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
# 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
2004-02-07 12:48:03 +01:00
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)
2003-10-21 08:03:57 +02:00
class Owner(privmsgs.CapabilityCheckingPrivmsg):
# 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'
_srcPlugins = ircutils.IrcSet(('Admin', 'Channel', 'Config',
'Misc', 'Owner', 'User'))
2003-08-28 18:33:45 +02:00
def __init__(self):
callbacks.Privmsg.__init__(self)
# Setup log object/command.
2004-02-07 12:48:03 +01:00
self.log = LogProxy(self.log)
# Setup exec command.
setattr(self.__class__, 'exec', self.__class__._exec)
# Setup Irc objects, connected to networks. If world.ircs is already
# populated, chances are that we're being reloaded, so don't do this.
if not world.ircs:
for network in conf.supybot.networks():
try:
self._connect(network)
except socket.error, e:
self.log.error('Could not connect to %s: %s.', network, e)
except Exception, e:
self.log.exception('Exception connecting to %s:', network)
self.log.error('Could not connect to %s: %s.', network, e)
# Setup plugins and default plugins for commands.
for (name, s) in registry._cache.iteritems():
2004-01-18 08:58:26 +01:00
if name.startswith('supybot.plugins'):
try:
2004-07-24 23:40:47 +02:00
(_, _, name) = registry.split(name)
except ValueError: # unpack list of wrong size.
2004-01-18 08:58:26 +01:00
continue
# This is just for the prettiness of the configuration file.
# There are no plugins that are all-lowercase, so we'll at
# least attempt to capitalize them.
if name == name.lower():
name = name.capitalize()
conf.registerPlugin(name)
if name.startswith('supybot.commands.defaultPlugins'):
try:
2004-07-24 23:40:47 +02:00
(_, _, _, name) = registry.split(name)
except ValueError: # unpack list of wrong size.
continue
registerDefaultPlugin(name, s)
2004-01-18 08:58:26 +01:00
def _getIrc(self, network):
network = network.lower()
for irc in world.ircs:
if irc.network.lower() == network:
return irc
return None
def outFilter(self, irc, msg):
if msg.command == 'PRIVMSG' and not world.testing:
if ircutils.strEqual(msg.args[0], irc.nick):
self.log.warning('Tried to send a message to myself: %r.', msg)
return None
return msg
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):
self.log.info('Loading plugins.')
alwaysLoadSrcPlugins = conf.supybot.plugins.alwaysLoadDefault()
for (name, value) in conf.supybot.plugins.getValues(fullNames=False):
if name.lower() in ('owner', 'alwaysloaddefault'):
continue
if irc.getCallback(name) is None:
load = value()
if not load and name in self._srcPlugins:
if alwaysLoadSrcPlugins:
s = '%s is configured not to be loaded, but is being '\
'loaded anyway because ' \
'supybot.plugins.alwaysLoadDefault is True.'
self.log.warning(s, name)
load = True
if load:
if not irc.getCallback(name):
2004-08-17 17:36:42 +02:00
# This is debug because each log logs its beginning.
self.log.debug('Loading %s.' % name)
try:
m = loadPluginModule(name)
loadPluginClass(irc, m)
except callbacks.Error, e:
# This is just an error message.
log.warning(str(e))
except ImportError, e:
log.warning('Failed to load %s: %s', name, e)
if name in self._srcPlugins:
self.log.exception('Error loading %s:', name)
self.log.error('Error loading src/ plugin %s. '
'This is usually rather '
'serious; these plugins are '
2004-08-11 15:51:15 +02:00
'almost always be loaded.',name)
except Exception, e:
log.exception('Failed to load %s:', name)
else:
# Let's import the module so configuration is preserved.
try:
_ = loadPluginModule(name)
except Exception, e:
log.info('Attempted to load %s to preserve its '
'configuration, but load failed: %s',
name, e)
world.starting = False
2003-10-30 04:08:52 +01:00
def disambiguate(self, irc, tokens, ambiguousCommands=None):
"""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."""
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:
cbs = callbacks.findCallbackForCommand(irc, command)
if len(cbs) > 1:
names = [cb.name() for cb in cbs]
srcs = [name for name in names if name in self._srcPlugins]
if len(srcs) == 1:
2004-08-16 15:22:08 +02:00
if callbacks.canonicalName(name) != command:
# We don't insert the dispatcher name here because
# it's handled later. Man, this stuff is a mess.
tokens.insert(0, srcs[0])
elif command not in map(callbacks.canonicalName, names):
ambiguousCommands[command] = names
2003-10-30 04:08:52 +01:00
for elt in tokens:
if isinstance(elt, list):
self.disambiguate(irc, elt, ambiguousCommands)
return ambiguousCommands
2003-08-28 18:33:45 +02:00
2004-08-31 18:35:42 +02:00
def ambiguousError(self, irc, msg, 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))
def processTokens(self, irc, msg, tokens):
ambiguousCommands = self.disambiguate(irc, tokens)
if ambiguousCommands:
2004-08-31 18:35:42 +02:00
self.ambiguousError(irc, msg, ambiguousCommands)
else:
callbacks.IrcObjectProxy(irc, msg, tokens)
def do376(self, irc, msg):
channels = ircutils.IrcSet(conf.supybot.channels())
channels |= conf.supybot.networks.get(irc.network).channels()
channels = list(channels)
if not channels:
return
utils.sortBy(lambda s: ',' not in s, channels)
keys = []
chans = []
for channel in channels:
if ',' in channel:
(channel, key) = channel.split(',', 1)
chans.append(channel)
keys.append(key)
else:
chans.append(channel)
irc.queueMsg(ircmsgs.joins(chans, keys))
do422 = do377 = do376
def doPrivmsg(self, irc, msg):
callbacks.Privmsg.handled = False
callbacks.Privmsg.errored = False
if ircdb.checkIgnored(msg.prefix):
return
s = callbacks.addressed(irc.nick, msg)
if s:
brackets = conf.supybot.reply.brackets.get(msg.args[0])()
try:
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
self.processTokens(irc, msg, tokens)
except SyntaxError, e:
callbacks.Privmsg.errored = True
irc.queueMsg(callbacks.error(msg, str(e)))
return
2003-10-30 04:08:52 +01:00
if conf.allowEval:
2004-08-20 07:30:37 +02:00
_evalEnv = {'_': None,
'__': None,
'___': None,
}
_evalEnv.update(globals())
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.
"""
if conf.allowEval:
s = privmsgs.getArgs(args)
try:
2004-08-20 07:30:37 +02:00
self._evalEnv.update(locals())
x = eval(s, self._evalEnv, self._evalEnv)
self._evalEnv['___'] = self._evalEnv['__']
self._evalEnv['__'] = self._evalEnv['_']
self._evalEnv['_'] = x
irc.reply(repr(x))
except SyntaxError, e:
irc.reply('%s: %r' % (utils.exnToString(e), s))
except Exception, e:
irc.reply(utils.exnToString(e))
else:
# 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 '
2004-02-05 08:53:00 +01:00
'option for this command to be enabled.')
2003-08-28 18:33:45 +02:00
def _exec(self, irc, msg, args):
"""<statement>
2003-09-02 21:55:53 +02: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.
irc.error('You must run Supybot with the --allow-eval '
2004-02-05 08:53:00 +01:00
'option for this command to be enabled.')
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 '
'this command to be enabled.')
_exec = eval
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))
def defaultplugin(self, irc, msg, args):
"""[--remove] <command> [<plugin>]
2003-08-28 18:33:45 +02: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)
if remove:
try:
conf.supybot.commands.defaultPlugins.unregister(command)
irc.replySuccess()
except registry.NonExistentRegistryEntry:
2004-07-31 08:15:19 +02:00
s = 'I don\'t have a default plugin set for that command.'
irc.error(s)
elif not cbs:
irc.error('That\'s not a valid command.')
return
elif plugin:
2004-07-31 08:15:19 +02:00
cb = irc.getCallback(plugin)
if cb is None:
irc.error('That\'s not a valid plugin.')
return
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)
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)
except Exception, e:
irc.error(utils.exnToString(e))
else:
2003-08-28 18:33:45 +02:00
irc.queueMsg(m)
def quit(self, irc, msg, args):
2004-08-19 22:32:07 +02:00
"""[<text>]
2003-08-28 18:33:45 +02:00
2004-08-19 22:32:07 +02:00
Exits the bot with the QUIT message <text>. If <text> is not given,
your nick will be substituted.
2003-08-28 18:33:45 +02:00
"""
2004-08-19 22:32:07 +02:00
text = privmsgs.getArgs(args, required=0, optional=1)
if not text:
text = msg.nick
m = ircmsgs.quit(text)
world.upkeep()
for irc in world.ircs[:]:
2004-08-19 22:32:07 +02:00
irc.queueMsg(m)
irc.die()
2003-08-28 18:33:45 +02:00
def flush(self, irc, msg, args):
"""takes no arguments
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()
irc.replySuccess()
2003-08-28 18:33:45 +02:00
def upkeep(self, irc, msg, args):
"""[<level>]
2003-08-28 18:33:45 +02:00
Runs the standard upkeep stuff (flushes and gc.collects()). If given
a level, runs that level of upkeep (currently, the only supported
level is "high", which causes the bot to flush a lot of caches as well
as do normal upkeep stuff.
2003-08-28 18:33:45 +02:00
"""
level = privmsgs.getArgs(args, required=0, optional=1)
L = []
if level == 'high':
L.append('Regexp cache flushed: %s cleared.' %
utils.nItems('regexp', len(sre._cache)))
sre.purge()
L.append('Pattern cache flushed: %s cleared.' %
utils.nItems('compiled pattern',
len(ircutils._patternCache)))
ircutils._patternCache.clear()
L.append('hostmaskPatternEqual cache flushed: %s cleared.' %
2004-08-21 10:53:29 +02:00
utils.nItems('result',
len(ircutils._hostmaskPatternEqualCache)))
ircutils._hostmaskPatternEqualCache.clear()
L.append('ircdb username cache flushed: %s cleared.' %
utils.nItems('username to id mapping',
len(ircdb.users._nameCache)))
ircdb.users._nameCache.clear()
L.append('ircdb hostmask cache flushed: %s cleared.' %
utils.nItems('hostmask to id mapping',
len(ircdb.users._hostmaskCache)))
ircdb.users._hostmaskCache.clear()
L.append('linecache line cache flushed: %s cleared.' %
utils.nItems('line', len(linecache.cache)))
linecache.clearcache()
sys.exc_clear()
collected = world.upkeep(scheduleNext=False)
2003-08-28 18:33:45 +02:00
if gc.garbage:
L.append('Garbage! %r.' % gc.garbage)
L.append('%s collected.' % utils.nItems('object', collected))
irc.reply(' '.join(L))
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
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-08-03 07:40:45 +02:00
irc.error('That plugin 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:
2004-08-03 07:40:45 +02:00
irc.error('That plugin is deprecated. '
'Use --deprecated to force it to load.')
2004-01-15 15:08:14 +01:00
return
except ImportError, e:
if name in str(e):
irc.error('No plugin %s exists.' % name)
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.
conf.registerPlugin(name, True)
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:
module = sys.modules[callbacks[0].__module__]
if hasattr(module, 'reload'):
x = module.reload()
2003-08-28 18:33:45 +02:00
try:
module = loadPluginModule(name)
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() # This makes sure the callback is collected.
callback = loadPluginClass(irc, module)
irc.replySuccess()
2003-08-28 18:33:45 +02:00
except ImportError:
for callback in callbacks:
irc.addCallback(callback)
irc.error('No plugin %s exists.' % name)
2003-08-28 18:33:45 +02:00
else:
2004-08-03 05:27:49 +02:00
irc.error('There was no plugin %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
2004-08-01 20:46:56 +02:00
of the currently loaded callbacks. Obviously, the Owner plugin can't
be unloaded.
2003-08-28 18:33:45 +02:00
"""
name = privmsgs.getArgs(args)
2004-08-01 20:46:56 +02:00
if ircutils.strEqual(name, self.name()):
irc.error('You can\'t unload the %s plugin.' % name)
2004-08-01 20:46:56 +02:00
return
# Let's do this so even if the plugin isn't currently loaded, it doesn't
# stay attempting to load.
conf.registerPlugin(name, False)
2003-08-28 18:33:45 +02:00
callbacks = irc.removeCallback(name)
if callbacks:
for callback in callbacks:
callback.die()
del callback
gc.collect()
irc.replySuccess()
2003-08-28 18:33:45 +02:00
else:
2004-08-03 05:27:49 +02:00
irc.error('There was no plugin %s.' % name)
2003-08-28 18:33:45 +02:00
2004-07-20 19:46:36 +02:00
def reconnect(self, irc, msg, args):
"""[<network>]
2004-07-20 19:46:36 +02:00
Disconnects and then reconnects to <network>. If no network is given,
disconnects and then reconnects to the network the command was given
on.
2004-07-20 19:46:36 +02:00
"""
network = privmsgs.getArgs(args, required=0, optional=1)
if network:
badIrc = self._getIrc(network)
if badIrc is None:
irc.error('I\'m not currently connected on %s.' % network)
return
else:
badIrc = irc
2004-07-20 19:46:36 +02:00
try:
badIrc.driver.reconnect()
if badIrc != irc:
irc.replySuccess()
2004-07-20 19:46:36 +02:00
except AttributeError: # There's a cleaner way to do this, but I'm lazy.
irc.error('I couldn\'t reconnect. You should restart me instead.')
2004-07-21 21:36:35 +02:00
2004-08-27 07:00:40 +02:00
def defaultcapability(self, irc, msg, args):
"""<add|remove> <capability>
Adds or removes (according to the first argument) <capability> from the
default capabilities given to users (the configuration variable
supybot.capabilities stores these).
"""
(action, capability) = privmsgs.getArgs(args, required=2)
if action == 'add':
conf.supybot.capabilities().add(capability)
irc.replySuccess()
elif action == 'remove':
try:
conf.supybot.capabilities().remove(capability)
irc.replySuccess()
except KeyError:
if ircdb.isAntiCapability(capability):
irc.error('That capability wasn\'t in '
'supybot.capabilities.')
else:
anticap = ircdb.makeAntiCapability(capability)
conf.supybot.capabilities().add(anticap)
irc.replySuccess()
else:
irc.error('That\'s not a valid action to take. Valid actions '
'are "add" and "remove"')
def disable(self, irc, msg, args):
"""[<plugin>] <command>
2004-08-27 06:31:53 +02:00
Disables the command <command> for all users (including the owners).
If <plugin> is given, only disables the <command> from <plugin>. If
you want to disable a command for most users but not for yourself, set
a default capability of -plugin.command or -command (if you want to
disable the command in all plugins).
"""
(plugin, command) = privmsgs.getArgs(args, optional=1)
if not command:
(plugin, command) = (None, plugin)
conf.supybot.commands.disabled().add(command)
else:
conf.supybot.commands.disabled().add('%s.%s' % (plugin, command))
if command in ('enable', 'identify'):
irc.error('You can\'t disable %s.' % command)
else:
self._disabled.add(command, plugin)
irc.replySuccess()
def enable(self, irc, msg, args):
"""[<plugin>] <command>
2004-08-27 06:31:53 +02:00
Enables the command <command> for all users. If <plugin>
if given, only enables the <command> from <plugin>. This command is
the inverse of disable.
"""
(plugin, command) = privmsgs.getArgs(args, optional=1)
try:
if not command:
(plugin, command) = (None, plugin)
conf.supybot.commands.disabled().remove(command)
else:
name = '%s.%s' % (plugin, command)
conf.supybot.commands.disabled().remove(name)
self._disabled.remove(command, plugin)
irc.replySuccess()
except KeyError:
raise
irc.error('That command wasn\'t disabled.')
def rename(self, irc, msg, args):
"""<plugin> <command> <new name>
Renames <command> in <plugin> to the <new name>.
"""
(plugin, command, newName) = privmsgs.getArgs(args, required=3)
name = callbacks.canonicalName(newName)
if name != newName:
irc.error('%s is a not a valid new command name. '
'Try making it lowercase and removing - and _.' %newName)
return
cb = irc.getCallback(plugin)
if cb is None:
irc.error('%s is not a valid plugin.' % plugin)
return
if not cb.isCommand(command):
s = '%s is not a valid command in the %s plugin.' % (name, plugin)
irc.error(s)
return
if hasattr(cb, name):
irc.error('The %s plugin already has an attribute named %s.' %
(plugin, name))
return
method = getattr(cb.__class__, command)
setattr(cb.__class__, name, method)
delattr(cb.__class__, command)
irc.replySuccess()
2004-08-11 14:57:52 +02:00
def _connect(self, network, serverPort=None):
try:
group = conf.supybot.networks.get(network)
(server, port) = group.servers()[0]
except (registry.NonExistentRegistryEntry, IndexError):
if serverPort is None:
raise ValueError, 'connect requires a (server, port) ' \
'if the network is not registered.'
conf.registerNetwork(network)
serverS = '%s:%s' % serverPort
conf.supybot.networks.get(network).servers.append(serverS)
assert conf.supybot.networks.get(network).servers()
self.log.info('Creating new Irc for %s.', network)
newIrc = irclib.Irc(network)
for irc in world.ircs:
if irc != newIrc:
newIrc.state.history = irc.state.history
driver = drivers.newDriver(newIrc)
return newIrc
def connect(self, irc, msg, args):
"""<network> [<host[:port]>]
Connects to another network at <host:port>. If port is not provided, it
defaults to 6667, the default port for IRC.
"""
(network, server) = privmsgs.getArgs(args, optional=1)
otherIrc = self._getIrc(network)
if otherIrc is not None:
irc.error('I\'m already connected to %s.' % network)
return
if server:
if ':' in server:
(server, port) = server.split(':')
port = int(port)
else:
port = 6667
serverPort = (server, port)
else:
try:
serverPort = conf.supybot.networks.get(network).servers()[0]
except (registry.NonExistentRegistryEntry, IndexError):
irc.error('A server must be provided if the network is not '
'already registered.')
return
newIrc = self._connect(network, serverPort=serverPort)
conf.supybot.networks().add(network)
assert newIrc.callbacks is irc.callbacks, 'callbacks list is different'
irc.replySuccess('Connection to %s initiated.' % network)
def disconnect(self, irc, msg, args):
"""<network> [<quit message>]
Disconnects and ceases to relay to and from the network represented by
the network <network>. If <quit message> is given, quits the network
with the given quit message.
"""
(network, quitMsg) = privmsgs.getArgs(args, optional=1)
if not quitMsg:
quitMsg = msg.nick
otherIrc = self._getIrc(network)
if otherIrc is not None:
# replySuccess here, rather than lower, in case we're being
# told to disconnect from the network we received the command on.
irc.replySuccess()
otherIrc.queueMsg(ircmsgs.quit(quitMsg))
otherIrc.die()
else:
irc.error('I\'m not connected to %s.' % network, Raise=True)
conf.supybot.networks().discard(network)
2004-07-20 19:46:36 +02:00
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: