Updated some capabilities stuff.

This commit is contained in:
Jeremy Fincher 2004-01-08 15:24:56 +00:00
parent 25918481ad
commit 099b8bc14b
6 changed files with 64 additions and 89 deletions

View File

@ -107,13 +107,14 @@ class Factoids(plugins.ChannelDBHandler,
db.commit() db.commit()
return db return db
def learn(self, irc, msg, args): def learn(self, irc, msg, args, channel):
"""[<channel>] <key> as <value> """[<channel>] <key> as <value>
Associates <key> with <value>. <channel> is only necessary if the Associates <key> with <value>. <channel> is only necessary if the
message isn't sent on the channel itself. message isn't sent on the channel itself. The word "as" is necessary
to separate the key from the value. It can be changed to another
word via the learn-separator configurable.
""" """
channel = privmsgs.getChannel(msg, args)
try: try:
separator = self.configurables.get('learn-separator', channel) separator = self.configurables.get('learn-separator', channel)
i = args.index(separator) i = args.index(separator)
@ -132,9 +133,6 @@ class Factoids(plugins.ChannelDBHandler,
(id, locked) = imap(int, cursor.fetchone()) (id, locked) = imap(int, cursor.fetchone())
capability = ircdb.makeChannelCapability(channel, 'factoids') capability = ircdb.makeChannelCapability(channel, 'factoids')
if not locked: if not locked:
if not ircdb.checkCapability(msg.prefix, capability):
irc.error(conf.replyNoCapability % capability)
return
if ircdb.users.hasUser(msg.prefix): if ircdb.users.hasUser(msg.prefix):
name = ircdb.users.getUser(msg.prefix).name name = ircdb.users.getUser(msg.prefix).name
else: else:
@ -191,45 +189,35 @@ class Factoids(plugins.ChannelDBHandler,
irc.error('That\'s not a valid number for this key.') irc.error('That\'s not a valid number for this key.')
return return
def lock(self, irc, msg, args): def lock(self, irc, msg, args, channel):
"""[<channel>] <key> """[<channel>] <key>
Locks the factoid(s) associated with <key> so that they cannot be Locks the factoid(s) associated with <key> so that they cannot be
removed or added to. <channel> is only necessary if the message isn't removed or added to. <channel> is only necessary if the message isn't
sent in the channel itself. sent in the channel itself.
""" """
channel = privmsgs.getChannel(msg, args)
key = privmsgs.getArgs(args) key = privmsgs.getArgs(args)
db = self.getDb(channel) db = self.getDb(channel)
capability = ircdb.makeChannelCapability(channel, 'factoids') cursor = db.cursor()
if ircdb.checkCapability(msg.prefix, capability): cursor.execute("UPDATE keys SET locked=1 WHERE key LIKE %s", key)
cursor = db.cursor() db.commit()
cursor.execute("UPDATE keys SET locked=1 WHERE key LIKE %s", key) irc.replySuccess()
db.commit()
irc.replySuccess()
else:
irc.error(conf.replyNoCapability % capability)
def unlock(self, irc, msg, args): def unlock(self, irc, msg, args, channel):
"""[<channel>] <key> """[<channel>] <key>
Unlocks the factoid(s) associated with <key> so that they can be Unlocks the factoid(s) associated with <key> so that they can be
removed or added to. <channel> is only necessary if the message isn't removed or added to. <channel> is only necessary if the message isn't
sent in the channel itself. sent in the channel itself.
""" """
channel = privmsgs.getChannel(msg, args)
key = privmsgs.getArgs(args) key = privmsgs.getArgs(args)
db = self.getDb(channel) db = self.getDb(channel)
capability = ircdb.makeChannelCapability(channel, 'factoids') cursor = db.cursor()
if ircdb.checkCapability(msg.prefix, capability): cursor.execute("UPDATE keys SET locked=0 WHERE key LIKE %s", key)
cursor = db.cursor() db.commit()
cursor.execute("UPDATE keys SET locked=0 WHERE key LIKE %s", key) irc.replySuccess()
db.commit()
irc.replySuccess()
else:
irc.error(conf.replyNoCapability % capability)
def forget(self, irc, msg, args): def forget(self, irc, msg, args, channel):
"""[<channel>] <key> [<number>|*] """[<channel>] <key> [<number>|*]
Removes the factoid <key> from the factoids database. If there are Removes the factoid <key> from the factoids database. If there are
@ -238,7 +226,6 @@ class Factoids(plugins.ChannelDBHandler,
factoids associated with a key. <channel> is only necessary if factoids associated with a key. <channel> is only necessary if
the message isn't sent in the channel itself. the message isn't sent in the channel itself.
""" """
channel = privmsgs.getChannel(msg, args)
if args[-1].isdigit(): if args[-1].isdigit():
number = int(args.pop()) number = int(args.pop())
number -= 1 number -= 1
@ -252,39 +239,35 @@ class Factoids(plugins.ChannelDBHandler,
number = None number = None
key = privmsgs.getArgs(args) key = privmsgs.getArgs(args)
db = self.getDb(channel) db = self.getDb(channel)
capability = ircdb.makeChannelCapability(channel, 'factoids') cursor = db.cursor()
if ircdb.checkCapability(msg.prefix, capability): cursor.execute("""SELECT keys.id, factoids.id
cursor = db.cursor() FROM keys, factoids
cursor.execute("""SELECT keys.id, factoids.id WHERE key LIKE %s AND
FROM keys, factoids factoids.key_id=keys.id""", key)
WHERE key LIKE %s AND if cursor.rowcount == 0:
factoids.key_id=keys.id""", key) irc.error('There is no such factoid.')
if cursor.rowcount == 0: elif cursor.rowcount == 1 or number is True:
irc.error('There is no such factoid.') (id, _) = cursor.fetchone()
elif cursor.rowcount == 1 or number is True: cursor.execute("""DELETE FROM factoids WHERE key_id=%s""", id)
(id, _) = cursor.fetchone() cursor.execute("""DELETE FROM keys WHERE key LIKE %s""", key)
cursor.execute("""DELETE FROM factoids WHERE key_id=%s""", id) db.commit()
cursor.execute("""DELETE FROM keys WHERE key LIKE %s""", key) irc.replySuccess()
else:
if number is not None:
results = cursor.fetchall()
try:
(_, id) = results[number]
except IndexError:
irc.error('Invalid factoid number.')
return
cursor.execute("DELETE FROM factoids WHERE id=%s", id)
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
else: else:
if number is not None: irc.error('%s factoids have that key. ' \
results = cursor.fetchall() 'Please specify which one to remove, ' \
try: 'or use * to designate all of them.' % \
(_, id) = results[number] cursor.rowcount)
except IndexError:
irc.error('Invalid factoid number.')
return
cursor.execute("DELETE FROM factoids WHERE id=%s", id)
db.commit()
irc.replySuccess()
else:
irc.error('%s factoids have that key. ' \
'Please specify which one to remove, ' \
'or use * to designate all of them.' % \
cursor.rowcount)
else:
irc.error(conf.replyNoCapability % capability)
def random(self, irc, msg, args): def random(self, irc, msg, args):
"""[<channel>] """[<channel>]
@ -341,13 +324,12 @@ class Factoids(plugins.ChannelDBHandler,
utils.nItems('factoid', counter), factoids) utils.nItems('factoid', counter), factoids)
irc.reply(s) irc.reply(s)
def change(self, irc, msg, args): def change(self, irc, msg, args, channel):
"""[<channel>] <key> <number> <regexp> """[<channel>] <key> <number> <regexp>
Changes the factoid #<number> associated with <key> according to Changes the factoid #<number> associated with <key> according to
<regexp>. <regexp>.
""" """
channel = privmsgs.getChannel(msg, args)
(key, number, regexp) = privmsgs.getArgs(args, required=3) (key, number, regexp) = privmsgs.getArgs(args, required=3)
try: try:
replacer = utils.perlReToReplacer(regexp) replacer = utils.perlReToReplacer(regexp)

View File

@ -227,25 +227,20 @@ class Quotes(plugins.ChannelDBHandler, callbacks.Privmsg):
else: else:
irc.error('There isn\'t a quote with that id.') irc.error('There isn\'t a quote with that id.')
def remove(self, irc, msg, args): def remove(self, irc, msg, args, channel):
"""[<channel>] <id> """[<channel>] <id>
Removes quote <id> from the quotes database for <channel>. <channel> Removes quote <id> from the quotes database for <channel>. <channel>
is only necessary if the message isn't sent in the channel itself. is only necessary if the message isn't sent in the channel itself.
""" """
channel = privmsgs.getChannel(msg, args)
id = privmsgs.getArgs(args) id = privmsgs.getArgs(args)
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
capability = ircdb.makeChannelCapability(channel, 'op') cursor.execute("""DELETE FROM quotes WHERE id=%s""", id)
if ircdb.checkCapability(msg.prefix, capability): if cursor.rowcount == 0:
cursor.execute("""DELETE FROM quotes WHERE id=%s""", id) irc.error('There was no such quote.')
if cursor.rowcount == 0:
irc.error('There was no such quote.')
else:
irc.replySuccess()
else: else:
irc.error(conf.replyNoCapability % capability) irc.replySuccess()
Class = Quotes Class = Quotes

View File

@ -257,7 +257,7 @@ class Channel(callbacks.Privmsg):
else: else:
self.log.warning('%r attempted kban without %s', self.log.warning('%r attempted kban without %s',
msg.prefix, capability) msg.prefix, capability)
irc.error(conf.replyNoCapability % capability) irc.errorNoCapability(capability)
def unban(self, irc, msg, args, channel): def unban(self, irc, msg, args, channel):
"""[<channel>] <hostmask> """[<channel>] <hostmask>

View File

@ -299,26 +299,24 @@ class RichReplyMethods(object):
def replySuccess(self, s='', **kwargs): def replySuccess(self, s='', **kwargs):
self.reply(self._makeReply(conf.replySuccess, s), **kwargs) self.reply(self._makeReply(conf.replySuccess, s), **kwargs)
def replyNoCapability(self, capability, s='', **kwargs):
s = self._makeReply(conf.replyNoCapability % s, s)
self.reply(s, **kwargs)
def replyNotRegistered(self, s='', **kwargs):
self.reply(self._makeReply(conf.replyNotRegistered, s), **kwargs)
def replyPossibleBug(self, s='', **kwargs): def replyPossibleBug(self, s='', **kwargs):
self.reply(self._makeReply(conf.replyPossibleBug, s), **kwargs) self.reply(self._makeReply(conf.replyPossibleBug, s), **kwargs)
def replyNoUser(self, s='', **kwargs):
self.reply(self._makeReply(conf.replyNoUser, s), **kwargs)
def replyRequiresPrivacy(self, s='', **kwargs):
s = self._makeReply(conf.replyRequiresPrivacy, s)
self.reply(s, **kwargs)
def replyError(self, s='', **kwargs): def replyError(self, s='', **kwargs):
self.reply(self._makeReply(conf.replyError, s), **kwargs) self.reply(self._makeReply(conf.replyError, s), **kwargs)
def errorNoCapability(self, capability, s='', **kwargs):
self.error(self._makeReply(conf.replyNoCapability % s, s), **kwargs)
def errorNotRegistered(self, s='', **kwargs):
self.error(self._makeReply(conf.replyNotRegistered, s), **kwargs)
def errorNoUser(self, s='', **kwargs):
self.error(self._makeReply(conf.replyNoUser, s), **kwargs)
def errorRequiresPrivacy(self, s='', **kwargs):
self.error(self._makeReply(conf.replyRequiresPrivacy, s), **kwargs)
class IrcObjectProxy(RichReplyMethods): class IrcObjectProxy(RichReplyMethods):
"A proxy object to allow proper nested of commands (even threaded ones)." "A proxy object to allow proper nested of commands (even threaded ones)."

View File

@ -295,7 +295,7 @@ class Mixin(object):
except Error, e: except Error, e:
irc.error(str(e)) irc.error(str(e))
else: else:
irc.error(conf.replyNoCapability % capability) irc.errorNoCapability(capability)
else: else:
help(self.configurables) help(self.configurables)
elif hasattr(self, 'globalConfigurables') and \ elif hasattr(self, 'globalConfigurables') and \

View File

@ -98,7 +98,7 @@ def checkCapability(f, capability):
else: else:
self.log.warning('%r attempted %s without %s.', self.log.warning('%r attempted %s without %s.',
msg.prefix, f.func_name, capability) msg.prefix, f.func_name, capability)
irc.error(conf.replyNoCapability % capability) irc.errorNoCapability(capability)
newf = types.FunctionType(newf.func_code, newf.func_globals, newf = types.FunctionType(newf.func_code, newf.func_globals,
f.func_name, closure=newf.func_closure) f.func_name, closure=newf.func_closure)
newf.__doc__ = f.__doc__ newf.__doc__ = f.__doc__
@ -119,7 +119,7 @@ def checkChannelCapability(f, capability):
else: else:
self.log.warning('%r attempted %s without %s.', self.log.warning('%r attempted %s without %s.',
msg.prefix, f.func_name, capability) msg.prefix, f.func_name, capability)
irc.error(conf.replyNoCapability % chancap) irc.errorNoCapability(chancap)
newf = types.FunctionType(newf.func_code, newf.func_globals, newf = types.FunctionType(newf.func_code, newf.func_globals,
f.func_name, closure=newf.func_closure) f.func_name, closure=newf.func_closure)
newf.__doc__ = f.__doc__ newf.__doc__ = f.__doc__
@ -209,7 +209,7 @@ class CapabilityCheckingPrivmsg(callbacks.Privmsg):
else: else:
self.log.warning('%r tried to call %s without %s.', self.log.warning('%r tried to call %s without %s.',
msg.prefix, f.im_func.func_name, self.capability) msg.prefix, f.im_func.func_name, self.capability)
irc.error(conf.replyNoCapability % self.capability) irc.errorNoCapability(self.capability)
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: