2004-01-20 08:15:37 +01:00
|
|
|
#!/usr/bin/env python
|
2004-01-18 06:39:03 +01:00
|
|
|
|
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2004-01-18 06:39:03 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Handles configuration of the bot while it's running.
|
|
|
|
"""
|
|
|
|
|
2004-09-01 08:06:54 +02:00
|
|
|
import supybot
|
|
|
|
|
2004-02-04 19:01:55 +01:00
|
|
|
__revision__ = "$Id$"
|
2004-09-01 08:06:54 +02:00
|
|
|
__author__ = supybot.authors.jemfinch
|
2004-02-04 19:01:55 +01:00
|
|
|
|
2004-08-29 20:11:43 +02:00
|
|
|
import os
|
2004-01-26 18:54:07 +01:00
|
|
|
import getopt
|
2004-08-23 22:08:49 +02:00
|
|
|
import signal
|
2004-01-18 06:39:03 +01:00
|
|
|
|
2004-08-23 22:08:49 +02:00
|
|
|
import supybot.log as log
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.world as world
|
|
|
|
import supybot.ircdb as ircdb
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.registry as registry
|
|
|
|
import supybot.callbacks as callbacks
|
2004-01-18 06:39:03 +01:00
|
|
|
|
|
|
|
###
|
|
|
|
# Now, to setup the registry.
|
|
|
|
###
|
|
|
|
|
|
|
|
class InvalidRegistryName(callbacks.Error):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def getWrapper(name):
|
2004-07-24 23:40:47 +02:00
|
|
|
parts = registry.split(name)
|
2004-07-24 04:28:43 +02:00
|
|
|
if not parts or parts[0] not in ('supybot', 'users'):
|
2004-01-18 06:39:03 +01:00
|
|
|
raise InvalidRegistryName, name
|
2004-07-24 04:28:43 +02:00
|
|
|
group = getattr(conf, parts.pop(0))
|
2004-01-18 06:39:03 +01:00
|
|
|
while parts:
|
|
|
|
try:
|
2004-01-25 09:22:50 +01:00
|
|
|
group = group.get(parts.pop(0))
|
2004-01-18 06:39:03 +01:00
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
raise InvalidRegistryName, name
|
|
|
|
return group
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
def getCapability(name):
|
|
|
|
capability = 'owner' # Default to requiring the owner capability.
|
2004-07-24 23:40:47 +02:00
|
|
|
parts = registry.split(name)
|
2004-01-18 08:58:26 +01:00
|
|
|
while parts:
|
|
|
|
part = parts.pop()
|
|
|
|
if ircutils.isChannel(part):
|
|
|
|
# If a registry value has a channel in it, it requires a channel.op
|
|
|
|
# capability, or so we assume. We'll see if we're proven wrong.
|
|
|
|
capability = ircdb.makeChannelCapability(part, 'op')
|
|
|
|
### Do more later, for specific capabilities/sections.
|
|
|
|
return capability
|
|
|
|
|
2004-08-23 22:08:49 +02:00
|
|
|
def _reload():
|
|
|
|
ircdb.users.reload()
|
|
|
|
ircdb.channels.reload()
|
|
|
|
registry.open(world.registryFilename)
|
|
|
|
|
|
|
|
def _hupHandler(sig, frame):
|
|
|
|
log.info('Received SIGHUP, reloading configuration.')
|
|
|
|
_reload()
|
|
|
|
|
2004-08-29 20:09:20 +02:00
|
|
|
if os.name == 'posix':
|
|
|
|
signal.signal(signal.SIGHUP, _hupHandler)
|
2004-08-23 22:08:49 +02:00
|
|
|
|
2004-01-18 06:39:03 +01:00
|
|
|
|
|
|
|
class Config(callbacks.Privmsg):
|
2004-09-06 10:04:03 +02:00
|
|
|
def callCommand(self, name, irc, msg, *L, **kwargs):
|
2004-09-09 18:25:53 +02:00
|
|
|
#XXX For some reason, that confuses jamessan, InvalidRegistryName
|
|
|
|
# is not really being caught here, but it is caught if we
|
|
|
|
# attempt to catch it in the individual command methods.
|
2004-01-18 06:39:03 +01:00
|
|
|
try:
|
2004-09-06 10:04:03 +02:00
|
|
|
super(Config, self).callCommand(name, irc, msg, *L, **kwargs)
|
2004-01-18 06:39:03 +01:00
|
|
|
except InvalidRegistryName, e:
|
2004-09-09 01:34:48 +02:00
|
|
|
irc.errorInvalid('configuration variable', e.args[0])
|
2004-01-18 06:39:03 +01:00
|
|
|
except registry.InvalidRegistryValue, e:
|
|
|
|
irc.error(str(e))
|
|
|
|
|
2004-02-02 09:33:03 +01:00
|
|
|
def _canonicalizeName(self, name):
|
2004-09-03 15:52:10 +02:00
|
|
|
if name.startswith('conf.'):
|
|
|
|
name = name[5:]
|
2004-07-24 04:28:43 +02:00
|
|
|
if not name.startswith('supybot') and not name.startswith('users'):
|
2004-02-02 09:33:03 +01:00
|
|
|
name = 'supybot.' + name
|
|
|
|
return name
|
|
|
|
|
2004-08-31 17:40:23 +02:00
|
|
|
def _list(self, name):
|
2004-07-24 04:28:43 +02:00
|
|
|
name = self._canonicalizeName(name)
|
|
|
|
group = getWrapper(name)
|
2004-08-31 17:40:23 +02:00
|
|
|
L = []
|
|
|
|
for (vname, v) in group._children.iteritems():
|
2004-09-10 08:59:05 +02:00
|
|
|
if hasattr(v, 'channelValue') and v.channelValue:
|
|
|
|
vname = '#' + vname
|
|
|
|
if v._added and not all(ircutils.isChannel, v._added):
|
2004-08-31 17:40:23 +02:00
|
|
|
vname = '@' + vname
|
|
|
|
L.append(vname)
|
|
|
|
utils.sortBy(str.lower, L)
|
|
|
|
return L
|
2004-09-09 18:25:53 +02:00
|
|
|
|
2004-01-18 06:39:03 +01:00
|
|
|
def list(self, irc, msg, args):
|
2004-08-31 17:40:23 +02:00
|
|
|
"""<group>
|
2004-01-18 06:39:03 +01:00
|
|
|
|
|
|
|
Returns the configuration variables available under the given
|
2004-08-31 17:40:23 +02:00
|
|
|
configuration <group>. Subgroups are indicated by a preceding @.
|
2004-01-18 06:39:03 +01:00
|
|
|
"""
|
2004-08-31 17:40:23 +02:00
|
|
|
name = privmsgs.getArgs(args)
|
2004-09-09 18:25:53 +02:00
|
|
|
try:
|
|
|
|
L = self._list(name)
|
|
|
|
except InvalidRegistryName, e:
|
|
|
|
irc.errorInvalid('configuration variable', e.args[0])
|
2004-07-24 04:28:43 +02:00
|
|
|
if L:
|
|
|
|
irc.reply(utils.commaAndify(L))
|
2004-01-18 06:39:03 +01:00
|
|
|
else:
|
2004-09-01 20:27:10 +02:00
|
|
|
irc.error('There don\'t seem to be any values in %s.' % name)
|
2004-01-18 06:39:03 +01:00
|
|
|
|
2004-01-31 02:37:01 +01:00
|
|
|
def search(self, irc, msg, args):
|
|
|
|
"""<word>
|
|
|
|
|
|
|
|
Searches for <word> in the current configuration variables.
|
|
|
|
"""
|
|
|
|
word = privmsgs.getArgs(args)
|
|
|
|
word = word.lower()
|
|
|
|
L = []
|
|
|
|
for (name, _) in conf.supybot.getValues(getChildren=True):
|
|
|
|
if word in name.lower():
|
2004-08-16 20:44:38 +02:00
|
|
|
possibleChannel = registry.split(name)[-1]
|
|
|
|
if not ircutils.isChannel(possibleChannel):
|
|
|
|
L.append(name)
|
2004-01-31 02:37:01 +01:00
|
|
|
if L:
|
|
|
|
irc.reply(utils.commaAndify(L))
|
|
|
|
else:
|
|
|
|
irc.reply('There were no matching configuration variables.')
|
|
|
|
|
2004-01-30 06:51:54 +01:00
|
|
|
def config(self, irc, msg, args):
|
|
|
|
"""<name> [<value>]
|
|
|
|
|
|
|
|
If <value> is given, sets the value of <name> to <value>. Otherwise,
|
2004-02-16 05:07:45 +01:00
|
|
|
returns the current value of <name>. You may omit the leading
|
|
|
|
"supybot." in the name if you so choose.
|
2004-01-30 06:51:54 +01:00
|
|
|
"""
|
2004-09-09 18:25:53 +02:00
|
|
|
try:
|
|
|
|
if len(args) >= 2:
|
|
|
|
self._set(irc, msg, args)
|
|
|
|
else:
|
|
|
|
self._get(irc, msg, args)
|
|
|
|
except InvalidRegistryName, e:
|
|
|
|
irc.errorInvalid('configuration variable', e.args[0])
|
2004-01-30 06:51:54 +01:00
|
|
|
|
2004-08-02 12:47:40 +02:00
|
|
|
def channel(self, irc, msg, args):
|
|
|
|
"""[<channel>] <name> [<value>]
|
|
|
|
|
|
|
|
If <value> is given, sets the channel configuration variable for <name>
|
|
|
|
to <value> for <channel>. Otherwise, returns the current channel
|
|
|
|
configuration value of <name>. <channel> is only necessary if the
|
|
|
|
message isn't sent in the channel itself."""
|
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2004-08-08 19:36:08 +02:00
|
|
|
if not args:
|
|
|
|
raise callbacks.ArgumentError
|
2004-08-11 00:08:54 +02:00
|
|
|
args[0] = self._canonicalizeName(args[0])
|
2004-09-09 18:25:53 +02:00
|
|
|
try:
|
|
|
|
wrapper = getWrapper(args[0])
|
|
|
|
except InvalidRegistryName, e:
|
|
|
|
irc.errorInvalid('configuration variable', e.args[0])
|
2004-08-08 19:36:08 +02:00
|
|
|
if not wrapper.channelValue:
|
|
|
|
irc.error('That configuration variable is not a channel-specific '
|
|
|
|
'configuration variable.')
|
|
|
|
return
|
2004-08-02 12:47:40 +02:00
|
|
|
components = registry.split(args[0])
|
|
|
|
components.append(channel)
|
|
|
|
args[0] = registry.join(components)
|
|
|
|
self.config(irc, msg, args)
|
|
|
|
|
2004-01-30 06:51:54 +01:00
|
|
|
def _get(self, irc, msg, args):
|
2004-01-18 06:39:03 +01:00
|
|
|
"""<name>
|
|
|
|
|
|
|
|
Shows the current value of the configuration variable <name>.
|
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
2004-02-02 09:33:03 +01:00
|
|
|
name = self._canonicalizeName(name)
|
2004-01-18 06:39:03 +01:00
|
|
|
wrapper = getWrapper(name)
|
2004-02-06 06:53:16 +01:00
|
|
|
if hasattr(wrapper, 'value'):
|
2004-07-31 07:00:43 +02:00
|
|
|
if not wrapper._private:
|
2004-08-17 10:32:00 +02:00
|
|
|
irc.reply(str(wrapper) or ' ')
|
2004-02-14 01:47:21 +01:00
|
|
|
else:
|
|
|
|
capability = getCapability(name)
|
|
|
|
if ircdb.checkCapability(msg.prefix, capability):
|
2004-09-02 00:21:37 +02:00
|
|
|
irc.reply(str(wrapper), private=True)
|
2004-02-14 01:47:21 +01:00
|
|
|
else:
|
|
|
|
irc.errorNoCapability(capability)
|
2004-02-06 06:53:16 +01:00
|
|
|
else:
|
|
|
|
irc.error('That registry variable has no value. Use the list '
|
|
|
|
'command in this plugin to see what values are '
|
|
|
|
'available in this group.')
|
2004-01-18 06:39:03 +01:00
|
|
|
|
2004-01-30 06:51:54 +01:00
|
|
|
def _set(self, irc, msg, args):
|
2004-01-18 06:39:03 +01:00
|
|
|
"""<name> <value>
|
|
|
|
|
|
|
|
Sets the current value of the configuration variable <name> to <value>.
|
|
|
|
"""
|
|
|
|
(name, value) = privmsgs.getArgs(args, required=2)
|
2004-02-02 09:33:03 +01:00
|
|
|
name = self._canonicalizeName(name)
|
2004-01-18 08:58:26 +01:00
|
|
|
capability = getCapability(name)
|
|
|
|
if ircdb.checkCapability(msg.prefix, capability):
|
|
|
|
wrapper = getWrapper(name)
|
|
|
|
wrapper.set(value)
|
|
|
|
irc.replySuccess()
|
|
|
|
else:
|
|
|
|
irc.errorNoCapability(capability)
|
2004-01-18 06:39:03 +01:00
|
|
|
|
|
|
|
def help(self, irc, msg, args):
|
|
|
|
"""<name>
|
|
|
|
|
|
|
|
Returns the description of the configuration variable <name>.
|
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
2004-02-02 09:33:03 +01:00
|
|
|
name = self._canonicalizeName(name)
|
2004-09-09 18:25:53 +02:00
|
|
|
try:
|
|
|
|
wrapper = getWrapper(name)
|
|
|
|
except InvalidRegistryName, e:
|
|
|
|
irc.errorInvalid('configuration variable', e.args[0])
|
2004-04-18 04:40:18 +02:00
|
|
|
if hasattr(wrapper, 'help'):
|
2004-08-16 23:43:59 +02:00
|
|
|
s = wrapper.help
|
|
|
|
if not wrapper._private:
|
2004-08-26 16:25:04 +02:00
|
|
|
s += ' (Current value: %s)' % wrapper
|
2004-08-16 23:43:59 +02:00
|
|
|
irc.reply(s)
|
2004-01-27 01:42:17 +01:00
|
|
|
else:
|
|
|
|
irc.error('%s has no help.' % name)
|
2004-01-18 06:39:03 +01:00
|
|
|
|
|
|
|
def default(self, irc, msg, args):
|
|
|
|
"""<name>
|
|
|
|
|
|
|
|
Returns the default value of the configuration variable <name>.
|
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
2004-02-02 09:33:03 +01:00
|
|
|
name = self._canonicalizeName(name)
|
2004-09-09 18:25:53 +02:00
|
|
|
try:
|
|
|
|
wrapper = getWrapper(name)
|
|
|
|
except InvalidRegistryName, e:
|
|
|
|
irc.errorInvalid('configuration variable', e.args[0])
|
2004-08-27 07:29:44 +02:00
|
|
|
v = wrapper.__class__(wrapper._default, '')
|
2004-02-18 13:15:04 +01:00
|
|
|
irc.reply(str(v))
|
2004-01-18 06:39:03 +01:00
|
|
|
|
2004-01-25 09:22:50 +01:00
|
|
|
def reload(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
2004-01-18 06:39:03 +01:00
|
|
|
|
2004-01-25 09:22:50 +01:00
|
|
|
Reloads the various configuration files (user database, channel
|
|
|
|
database, registry, etc.).
|
2004-01-18 06:39:03 +01:00
|
|
|
"""
|
2004-08-23 22:08:49 +02:00
|
|
|
_reload() # This was factored out for SIGHUP handling.
|
2004-01-18 06:39:03 +01:00
|
|
|
irc.replySuccess()
|
2004-01-25 09:22:50 +01:00
|
|
|
reload = privmsgs.checkCapability(reload, 'owner')
|
2004-09-09 18:25:53 +02:00
|
|
|
|
2004-01-18 06:39:03 +01:00
|
|
|
|
|
|
|
Class = Config
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|