Merge pull request #1131 from GLolol/allow-unregistered-db-add

Allow unregistered users to use 'add', 'remove', etc. in ChannelIdDatabasePlugin plugins
This commit is contained in:
Valentin Lorentz 2015-06-28 23:54:22 +02:00
commit 451ec28163
4 changed files with 36 additions and 9 deletions

View File

@ -29,7 +29,7 @@
from supybot.test import * from supybot.test import *
class PraiseTestCase(PluginTestCase): class PraiseTestCase(ChannelPluginTestCase):
plugins = ('Praise',) plugins = ('Praise',)
def testAdd(self): def testAdd(self):

View File

@ -40,4 +40,12 @@ class QuoteTestCase(ChannelPluginTestCase):
self.assertRegexp("quote get 1", "goodbye") self.assertRegexp("quote get 1", "goodbye")
self.assertError("quote replace 5 afsdafas") # non-existant self.assertError("quote replace 5 afsdafas") # non-existant
def testUnauthenticatedAdd(self):
# This should fail because the user isn't registered
self.assertError('quote add hello world!')
with conf.supybot.databases.plugins.requireRegistration.context(False):
self.assertNotError('quote add hello world!')
self.assertRegexp('quote get 1', 'hello')
self.assertNotError('quote remove 1')
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -330,7 +330,9 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
(self.name(), id, channel)) (self.name(), id, channel))
def checkChangeAllowed(self, irc, msg, channel, user, record): def checkChangeAllowed(self, irc, msg, channel, user, record):
if user.id == record.by: # Checks and returns True if either the user ID (integer)
# or the hostmask of the caller match.
if (hasattr(user, 'id') and user.id == record.by) or user == record.by:
return True return True
cap = ircdb.makeChannelCapability(channel, 'op') cap = ircdb.makeChannelCapability(channel, 'op')
if ircdb.checkCapability(msg.prefix, cap): if ircdb.checkCapability(msg.prefix, cap):
@ -341,27 +343,38 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
"""This should irc.error or raise an exception if text is invalid.""" """This should irc.error or raise an exception if text is invalid."""
pass pass
def add(self, irc, msg, args, user, channel, text): def getUserId(self, irc, prefix, channel=None):
try:
user = ircdb.users.getUser(prefix)
return user.id
except KeyError:
if conf.get(conf.supybot.databases.plugins.requireRegistration, channel):
irc.errorNotRegistered(Raise=True)
return
def add(self, irc, msg, args, channel, text):
"""[<channel>] <text> """[<channel>] <text>
Adds <text> to the $type database for <channel>. Adds <text> to the $type database for <channel>.
<channel> is only necessary if the message isn't sent in the channel <channel> is only necessary if the message isn't sent in the channel
itself. itself.
""" """
user = self.getUserId(irc, msg.prefix, channel) or msg.prefix
at = time.time() at = time.time()
self.addValidator(irc, text) self.addValidator(irc, text)
if text is not None: if text is not None:
id = self.db.add(channel, at, user.id, text) id = self.db.add(channel, at, user, text)
irc.replySuccess('%s #%s added.' % (self.name(), id)) irc.replySuccess('%s #%s added.' % (self.name(), id))
add = wrap(add, ['user', 'channeldb', 'text']) add = wrap(add, ['channeldb', 'text'])
def remove(self, irc, msg, args, user, channel, id): def remove(self, irc, msg, args, channel, id):
"""[<channel>] <id> """[<channel>] <id>
Removes the $type with id <id> from the $type database for <channel>. Removes the $type with id <id> from the $type database for <channel>.
<channel> is only necessary if the message isn't sent in the channel <channel> is only necessary if the message isn't sent in the channel
itself. itself.
""" """
user = self.getUserId(irc, msg.prefix, channel) or msg.prefix
try: try:
record = self.db.get(channel, id) record = self.db.get(channel, id)
self.checkChangeAllowed(irc, msg, channel, user, record) self.checkChangeAllowed(irc, msg, channel, user, record)
@ -369,7 +382,7 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
irc.replySuccess() irc.replySuccess()
except KeyError: except KeyError:
self.noSuchRecord(irc, channel, id) self.noSuchRecord(irc, channel, id)
remove = wrap(remove, ['user', 'channeldb', 'id']) remove = wrap(remove, ['channeldb', 'id'])
def searchSerializeRecord(self, record): def searchSerializeRecord(self, record):
text = utils.str.ellipsisify(record.text, 50) text = utils.str.ellipsisify(record.text, 50)
@ -430,13 +443,14 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
self.noSuchRecord(irc, channel, id) self.noSuchRecord(irc, channel, id)
get = wrap(get, ['channeldb', 'id']) get = wrap(get, ['channeldb', 'id'])
def change(self, irc, msg, args, user, channel, id, replacer): def change(self, irc, msg, args, channel, id, replacer):
"""[<channel>] <id> <regexp> """[<channel>] <id> <regexp>
Changes the $type with id <id> according to the regular expression Changes the $type with id <id> according to the regular expression
<regexp>. <channel> is only necessary if the message isn't sent in the <regexp>. <channel> is only necessary if the message isn't sent in the
channel itself. channel itself.
""" """
user = self.getUserId(irc, msg.prefix, channel) or msg.prefix
try: try:
record = self.db.get(channel, id) record = self.db.get(channel, id)
self.checkChangeAllowed(irc, msg, channel, user, record) self.checkChangeAllowed(irc, msg, channel, user, record)
@ -445,7 +459,7 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
irc.replySuccess() irc.replySuccess()
except KeyError: except KeyError:
self.noSuchRecord(irc, channel, id) self.noSuchRecord(irc, channel, id)
change = wrap(change, ['user', 'channeldb', 'id', 'regexpReplacer']) change = wrap(change, ['channeldb', 'id', 'regexpReplacer'])
def stats(self, irc, msg, args, channel): def stats(self, irc, msg, args, channel):
"""[<channel>] """[<channel>]

View File

@ -931,6 +931,11 @@ class ChannelSpecific(registry.Boolean):
return lchannel return lchannel
registerGroup(supybot.databases, 'plugins') registerGroup(supybot.databases, 'plugins')
registerChannelValue(supybot.databases.plugins, 'requireRegistration',
registry.Boolean(True, _("""Determines whether the bot will require user
registration to use 'add' commands in database-based Supybot
plugins.""")))
registerChannelValue(supybot.databases.plugins, 'channelSpecific', registerChannelValue(supybot.databases.plugins, 'channelSpecific',
ChannelSpecific(True, _("""Determines whether database-based plugins that ChannelSpecific(True, _("""Determines whether database-based plugins that
can be channel-specific will be so. This can be overridden by individual can be channel-specific will be so. This can be overridden by individual