Config: Support arbitrarily many channels for '@config channel'.

This commit is contained in:
Valentin Lorentz 2018-05-29 22:17:19 +02:00
parent 43ee68ee3d
commit c2b6305c46
3 changed files with 54 additions and 12 deletions

View File

@ -205,13 +205,13 @@ class Config(callbacks.Plugin):
value = _('Global: %s; %s: %s') % (value, msg.args[0], s) value = _('Global: %s; %s: %s') % (value, msg.args[0], s)
if hasattr(group, 'value'): if hasattr(group, 'value'):
if not group._private: if not group._private:
irc.reply(value) return (value, None)
else: else:
capability = getCapability(group._name) capability = getCapability(group._name)
if ircdb.checkCapability(msg.prefix, capability): if ircdb.checkCapability(msg.prefix, capability):
irc.reply(value, private=True) return (value, True)
else: else:
irc.errorNoCapability(capability) irc.errorNoCapability(capability, Raise=True)
else: else:
irc.error(_('That registry variable has no value. Use the list ' irc.error(_('That registry variable has no value. Use the list '
'command in this plugin to see what variables are ' 'command in this plugin to see what variables are '
@ -226,28 +226,42 @@ class Config(callbacks.Plugin):
if ircdb.checkCapability(msg.prefix, capability): if ircdb.checkCapability(msg.prefix, capability):
# I think callCommand catches exceptions here. Should it? # I think callCommand catches exceptions here. Should it?
group.set(value) group.set(value)
irc.replySuccess()
else: else:
irc.errorNoCapability(capability) irc.errorNoCapability(capability, Raise=True)
@internationalizeDocstring @internationalizeDocstring
def channel(self, irc, msg, args, channel, group, value): def channel(self, irc, msg, args, channels, group, value):
"""[<channel>] <name> [<value>] """[<channel>] <name> [<value>]
If <value> is given, sets the channel configuration variable for <name> If <value> is given, sets the channel configuration variable for <name>
to <value> for <channel>. Otherwise, returns the current channel to <value> for <channel>. Otherwise, returns the current channel
configuration value of <name>. <channel> is only necessary if the configuration value of <name>. <channel> is only necessary if the
message isn't sent in the channel itself.""" message isn't sent in the channel itself. More than one channel may
be given at once by separating them with commas."""
if not group.channelValue: if not group.channelValue:
irc.error(_('That configuration variable is not a channel-specific ' irc.error(_('That configuration variable is not a channel-specific '
'configuration variable.')) 'configuration variable.'))
return return
group = group.get(channel)
if value is not None: if value is not None:
self._setValue(irc, msg, group, value) for channel in channels:
assert irc.isChannel(channel)
self._setValue(irc, msg, group.get(channel), value)
irc.replySuccess()
else: else:
self._getValue(irc, msg, group) values = []
channel = wrap(channel, ['channel', 'settableConfigVar', private = None
for channel in channels:
(value, private_value) = self._getValue(irc, msg, group.get(channel))
values.append(value)
if private_value:
private = True
if len(channels) > 1:
irc.reply('; '.join([
'%s: %s' % (channel, value)
for value in values]))
else:
irc.reply(values[0])
channel = wrap(channel, ['channels', 'settableConfigVar',
additional('text')]) additional('text')])
@internationalizeDocstring @internationalizeDocstring
@ -260,8 +274,10 @@ class Config(callbacks.Plugin):
""" """
if value is not None: if value is not None:
self._setValue(irc, msg, group, value) self._setValue(irc, msg, group, value)
irc.replySuccess()
else: else:
self._getValue(irc, msg, group, addChannel=group.channelValue) (value, private) = self._getValue(irc, msg, group, addChannel=group.channelValue)
irc.reply(value, private=private)
config = wrap(config, ['settableConfigVar', additional('text')]) config = wrap(config, ['settableConfigVar', additional('text')])
@internationalizeDocstring @internationalizeDocstring

View File

@ -167,5 +167,20 @@ class ConfigTestCase(ChannelPluginTestCase):
self.assertResponse('config plugins.Config.%s' % var_name, self.assertResponse('config plugins.Config.%s' % var_name,
'Global: 0; #test: 1') 'Global: 0; #test: 1')
def testChannel(self):
self.assertResponse('config reply.whenAddressedBy.strings ^',
'The operation succeeded.')
self.assertResponse('config channel reply.whenAddressedBy.strings @',
'The operation succeeded.')
self.assertResponse('config channel reply.whenAddressedBy.strings', '@')
self.assertNotError('config channel reply.whenAddressedBy.strings $')
self.assertResponse('config channel #testchan1 reply.whenAddressedBy.strings', '^')
self.assertResponse('config channel #testchan2 reply.whenAddressedBy.strings', '^')
self.assertNotError('config channel #testchan1,#testchan2 reply.whenAddressedBy.strings .')
self.assertResponse('config channel reply.whenAddressedBy.strings', '$')
self.assertResponse('config channel #testchan1 reply.whenAddressedBy.strings', '.')
self.assertResponse('config channel #testchan2 reply.whenAddressedBy.strings', '.')
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -488,6 +488,16 @@ def getChannel(irc, msg, args, state):
state.channel = channel state.channel = channel
state.args.append(channel) state.args.append(channel)
def getChannels(irc, msg, args, state):
if args and all(map(irc.isChannel, args[0].split(','))):
channels = args.pop(0).split(',')
elif irc.isChannel(msg.args[0]):
channels = [msg.args[0]]
else:
state.log.debug('Raising ArgumentError because there is no channel.')
raise callbacks.ArgumentError
state.args.append(channels)
def getChannelDb(irc, msg, args, state, **kwargs): def getChannelDb(irc, msg, args, state, **kwargs):
channelSpecific = conf.supybot.databases.plugins.channelSpecific channelSpecific = conf.supybot.databases.plugins.channelSpecific
try: try:
@ -730,6 +740,7 @@ wrappers = ircutils.IrcDict({
'isGranted': getHaveHalfopPlus, # Backward compatibility 'isGranted': getHaveHalfopPlus, # Backward compatibility
'capability': getSomethingNoSpaces, 'capability': getSomethingNoSpaces,
'channel': getChannel, 'channel': getChannel,
'channels': getChannels,
'channelOrGlobal': getChannelOrGlobal, 'channelOrGlobal': getChannelOrGlobal,
'channelDb': getChannelDb, 'channelDb': getChannelDb,
'checkCapability': checkCapability, 'checkCapability': checkCapability,