Added several new types for better error checking, converted supybot.channels to be space-separated, and made Admin.{join,nick} Do The Right Thing.

This commit is contained in:
Jeremy Fincher 2004-01-21 17:13:04 +00:00
parent bd47403bf0
commit 58332ad382
3 changed files with 65 additions and 23 deletions

View File

@ -62,7 +62,18 @@ class Admin(privmsgs.CapabilityCheckingPrivmsg):
self.joins = {} self.joins = {}
def do376(self, irc, msg): def do376(self, irc, msg):
irc.queueMsg(ircmsgs.joins(conf.supybot.channels())) channels = conf.supybot.channels()
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(channels, keys))
do422 = do377 = do376 do422 = do377 = do376
def do471(self, irc, msg): def do471(self, irc, msg):
@ -128,6 +139,7 @@ class Admin(privmsgs.CapabilityCheckingPrivmsg):
keys = [] keys = []
channels = [] channels = []
for channel in args: for channel in args:
original = channel
if ',' in channel: if ',' in channel:
(channel, key) = channel.split(',', 1) (channel, key) = channel.split(',', 1)
channels.insert(0, channel) channels.insert(0, channel)
@ -137,6 +149,7 @@ class Admin(privmsgs.CapabilityCheckingPrivmsg):
if not ircutils.isChannel(channel): if not ircutils.isChannel(channel):
irc.error('%r is not a valid channel.' % channel) irc.error('%r is not a valid channel.' % channel)
return return
conf.supybot.channels().append(original)
irc.queueMsg(ircmsgs.joins(channels, keys)) irc.queueMsg(ircmsgs.joins(channels, keys))
for channel in channels: for channel in channels:
self.joins[channel] = (irc, msg) self.joins[channel] = (irc, msg)
@ -164,7 +177,11 @@ class Admin(privmsgs.CapabilityCheckingPrivmsg):
Changes the bot's nick to <nick>.""" Changes the bot's nick to <nick>."""
nick = privmsgs.getArgs(args) nick = privmsgs.getArgs(args)
irc.queueMsg(ircmsgs.nick(nick)) if ircutils.isNick(nick):
conf.supybot.nick.setValue(nick)
irc.queueMsg(ircmsgs.nick(nick))
else:
irc.error('That\'s not a valid nick.')
def part(self, irc, msg, args): def part(self, irc, msg, args):
"""<channel> [<channel> ...] """<channel> [<channel> ...]
@ -178,6 +195,9 @@ class Admin(privmsgs.CapabilityCheckingPrivmsg):
if arg not in irc.state.channels: if arg not in irc.state.channels:
irc.error('I\'m not currently in %s' % arg) irc.error('I\'m not currently in %s' % arg)
return return
for arg in args:
if arg in conf.supybot.channels():
conf.supybot.channels().remove(arg)
irc.queueMsg(ircmsgs.parts(args, msg.nick)) irc.queueMsg(ircmsgs.parts(args, msg.nick))
def disable(self, irc, msg, args): def disable(self, irc, msg, args):

View File

@ -72,12 +72,20 @@ def registerGlobalValue(group, name, value):
group.register(name, value) group.register(name, value)
class ValidNick(registry.String): class ValidNick(registry.String):
def set(self, s): def setValue(self, v):
original = getattr(self, 'value', self.default) if not ircutils.isNick(v):
registry.String.set(self, s) raise registry.InvalidRegistryValue, \
if not ircutils.isNick(self.value): 'Value must be a valid IRC nick.'
self.value = original else:
raise registry.InvalidRegistryValue, 'Value must be a valid nick.' registry.String.setValue(self, v)
class ValidChannel(registry.String):
def setValue(self, v):
if not ircutils.isChannel(v):
raise registry.InvalidRegistryValue, \
'Value must be a valid IRC channel name.'
else:
registry.String.setValue(self, v)
supybot.register('nick', ValidNick('supybot', supybot.register('nick', ValidNick('supybot',
"""Determines the bot's nick.""")) """Determines the bot's nick."""))
@ -95,9 +103,14 @@ be sent to the server if it requires one."""))
supybot.register('server', registry.String('irc.freenode.net', """Determines supybot.register('server', registry.String('irc.freenode.net', """Determines
what server the bot connects to.""")) what server the bot connects to."""))
supybot.register('channels', registry.CommaSeparatedListOfStrings(['#supybot'], class SpaceSeparatedListOfChannels(registry.SeparatedListOf):
"""Determines what channels the bot will join when it connects to the server. Value = ValidChannel
""")) def splitter(self, s):
return s.split()
joiner = ' '.join
supybot.register('channels', SpaceSeparatedListOfChannels(['#supybot'], """
Determines what channels the bot will join when it connects to the server."""))
supybot.registerGroup('databases') supybot.registerGroup('databases')
supybot.databases.registerGroup('users') supybot.databases.registerGroup('users')
@ -126,13 +139,11 @@ Refer to the Python documentation for the time module to see valid formatting
characteres for time formats.""")) characteres for time formats."""))
class IP(registry.String): class IP(registry.String):
def set(self, s): def setValue(self, v):
original = getattr(self, 'value', self.default) if v and not (utils.isIP(v) or utils.isIPV6(v)):
registry.String.set(self, s) raise registry.InvalidRegistryValue, 'Value must be a valid IP.'
if self.value: # Empty string is alright. else:
if not (utils.isIP(self.value) or utils.isIPV6(self.value)): registry.String.setValue(self, v)
raise registry.InvalidRegistryValue, \
'Value must be a valid IP.'
supybot.register('externalIP', IP('', """A string that is the external IP of supybot.register('externalIP', IP('', """A string that is the external IP of
the bot. If this is the empty string, the bot will attempt to find out its IP the bot. If this is the empty string, the bot will attempt to find out its IP

View File

@ -37,6 +37,7 @@ import sets
import types import types
import textwrap import textwrap
import fix
import utils import utils
class RegistryException(Exception): class RegistryException(Exception):
@ -65,14 +66,20 @@ def open(filename):
_cache[key.lower()] = value _cache[key.lower()] = value
def close(registry, filename, annotated=True): def close(registry, filename, annotated=True):
first = True
helpCache = sets.Set()
fd = file(filename, 'w') fd = file(filename, 'w')
for (name, value) in registry.getValues(getChildren=True): for (name, value) in registry.getValues(getChildren=True):
if annotated and hasattr(value, 'help'): if annotated and hasattr(value,'help') and value.help not in helpCache:
helpCache.add(value.help)
lines = textwrap.wrap(value.help) lines = textwrap.wrap(value.help)
for (i, line) in enumerate(lines): for (i, line) in enumerate(lines):
lines[i] = '# %s\n' % line lines[i] = '# %s\n' % line
lines.insert(0, '###\n') lines.insert(0, '###\n')
lines.insert(0, '\n') if first:
first = False
else:
lines.insert(0, '\n')
lines.append('###\n') lines.append('###\n')
fd.writelines(lines) fd.writelines(lines)
fd.write('%s: %s\n' % (name, value)) fd.write('%s: %s\n' % (name, value))
@ -210,13 +217,17 @@ class SeparatedListOf(Value):
def __str__(self): def __str__(self):
return self.joiner(self.value) return self.joiner(self.value)
class SpaceSeparatedListOfStrings(SeparatedListOf):
Value = String
def splitter(self, s):
return s.split(s)
joiner = ' '.join
class CommaSeparatedListOfStrings(SeparatedListOf): class CommaSeparatedListOfStrings(SeparatedListOf):
Value = String Value = String
def splitter(self, s): def splitter(self, s):
return re.split(r'\s*,\s*', s) return re.split(r'\s*,\s*', s)
def joiner(self, L): joiner = ', '.join
return ','.join(L)
class CommaSeparatedSetOfStrings(CommaSeparatedListOfStrings): class CommaSeparatedSetOfStrings(CommaSeparatedListOfStrings):
def setValue(self, v): def setValue(self, v):