MessageParser: Add support for global triggers.

This commit is contained in:
Valentin Lorentz 2012-10-18 19:31:08 +02:00
parent d3da0c2efc
commit a565b7f7d7

View File

@ -115,7 +115,8 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
return db return db
def _updateRank(self, channel, regexp): def _updateRank(self, channel, regexp):
if self.registryValue('keepRankInfo', channel): subfolder = None if channel == 'global' else channel
if self.registryValue('keepRankInfo', subfolder):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT usage_count cursor.execute("""SELECT usage_count
@ -156,13 +157,17 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
if callbacks.addressed(irc.nick, msg): #message is direct command if callbacks.addressed(irc.nick, msg): #message is direct command
return return
actions = [] actions = []
results = []
for channel in (channel, 'global'):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("SELECT regexp, action FROM triggers") cursor.execute("SELECT regexp, action FROM triggers")
results = cursor.fetchall() # Fetch results and prepend channel name or 'global'. This
# prevents duplicating the following lines.
results.extend(map(lambda x: (channel,)+x, cursor.fetchall()))
if len(results) == 0: if len(results) == 0:
return return
for (regexp, action) in results: for (channel, regexp, action) in results:
for match in re.finditer(regexp, msg.args[1]): for match in re.finditer(regexp, msg.args[1]):
if match is not None: if match is not None:
thisaction = action thisaction = action
@ -176,7 +181,7 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
@internationalizeDocstring @internationalizeDocstring
def add(self, irc, msg, args, channel, regexp, action): def add(self, irc, msg, args, channel, regexp, action):
"""[<channel>] <regexp> <action> """[<channel>|global] <regexp> <action>
Associates <regexp> with <action>. <channel> is only Associates <regexp> with <action>. <channel> is only
necessary if the message isn't sent on the channel necessary if the message isn't sent on the channel
@ -212,11 +217,11 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
else: else:
irc.error(_('That trigger is locked.')) irc.error(_('That trigger is locked.'))
return return
add = wrap(add, ['channel', 'something', 'something']) add = wrap(add, ['channelOrGlobal', 'something', 'something'])
@internationalizeDocstring @internationalizeDocstring
def remove(self, irc, msg, args, channel, optlist, regexp): def remove(self, irc, msg, args, channel, optlist, regexp):
"""[<channel>] [--id] <regexp>] """[<channel>|global] [--id] <regexp>]
Removes the trigger for <regexp> from the triggers database. Removes the trigger for <regexp> from the triggers database.
<channel> is only necessary if <channel> is only necessary if
@ -248,13 +253,13 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
cursor.execute("""DELETE FROM triggers WHERE id=?""", (id,)) cursor.execute("""DELETE FROM triggers WHERE id=?""", (id,))
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
remove = wrap(remove, ['channel', remove = wrap(remove, ['channelOrGlobal',
getopts({'id': '',}), getopts({'id': '',}),
'something']) 'something'])
@internationalizeDocstring @internationalizeDocstring
def lock(self, irc, msg, args, channel, regexp): def lock(self, irc, msg, args, channel, regexp):
"""[<channel>] <regexp> """[<channel>|global] <regexp>
Locks the <regexp> so that it cannot be Locks the <regexp> so that it cannot be
removed or overwritten to. <channel> is only necessary if the message isn't removed or overwritten to. <channel> is only necessary if the message isn't
@ -273,11 +278,11 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
cursor.execute("UPDATE triggers SET locked=1 WHERE regexp=?", (regexp,)) cursor.execute("UPDATE triggers SET locked=1 WHERE regexp=?", (regexp,))
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
lock = wrap(lock, ['channel', 'text']) lock = wrap(lock, ['channelOrGlobal', 'text'])
@internationalizeDocstring @internationalizeDocstring
def unlock(self, irc, msg, args, channel, regexp): def unlock(self, irc, msg, args, channel, regexp):
"""[<channel>] <regexp> """[<channel>|global] <regexp>
Unlocks the entry associated with <regexp> so that it can be Unlocks the entry associated with <regexp> so that it can be
removed or overwritten. <channel> is only necessary if the message isn't removed or overwritten. <channel> is only necessary if the message isn't
@ -296,11 +301,11 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
cursor.execute("UPDATE triggers SET locked=0 WHERE regexp=?", (regexp,)) cursor.execute("UPDATE triggers SET locked=0 WHERE regexp=?", (regexp,))
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
unlock = wrap(unlock, ['channel', 'text']) unlock = wrap(unlock, ['channelOrGlobal', 'text'])
@internationalizeDocstring @internationalizeDocstring
def show(self, irc, msg, args, channel, optlist, regexp): def show(self, irc, msg, args, channel, optlist, regexp):
"""[<channel>] [--id] <regexp> """[<channel>|global] [--id] <regexp>
Looks up the value of <regexp> in the triggers database. Looks up the value of <regexp> in the triggers database.
<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
@ -323,13 +328,13 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
return return
irc.reply("The action for regexp trigger \"%s\" is \"%s\"" % (regexp, action)) irc.reply("The action for regexp trigger \"%s\" is \"%s\"" % (regexp, action))
show = wrap(show, ['channel', show = wrap(show, ['channelOrGlobal',
getopts({'id': '',}), getopts({'id': '',}),
'something']) 'something'])
@internationalizeDocstring @internationalizeDocstring
def info(self, irc, msg, args, channel, optlist, regexp): def info(self, irc, msg, args, channel, optlist, regexp):
"""[<channel>] [--id] <regexp> """[<channel>|global] [--id] <regexp>
Display information about <regexp> in the triggers database. Display information about <regexp> in the triggers database.
<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
@ -362,13 +367,13 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
time.localtime(int(added_at))), time.localtime(int(added_at))),
usage_count, usage_count,
locked and _("locked") or _("not locked"),)) locked and _("locked") or _("not locked"),))
info = wrap(info, ['channel', info = wrap(info, ['channelOrGlobal',
getopts({'id': '',}), getopts({'id': '',}),
'something']) 'something'])
@internationalizeDocstring @internationalizeDocstring
def list(self, irc, msg, args, channel): def list(self, irc, msg, args, channel):
"""[<channel>] """[<channel>|global]
Lists regexps present in the triggers database. Lists regexps present in the triggers database.
<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
@ -387,11 +392,11 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
s = [ "\"%s\" (%d)" % (regexp[0], regexp[1]) for regexp in regexps ] s = [ "\"%s\" (%d)" % (regexp[0], regexp[1]) for regexp in regexps ]
separator = self.registryValue('listSeparator', channel) separator = self.registryValue('listSeparator', channel)
irc.reply(separator.join(s)) irc.reply(separator.join(s))
list = wrap(list, ['channel']) list = wrap(list, ['channelOrGlobal'])
@internationalizeDocstring @internationalizeDocstring
def rank(self, irc, msg, args, channel): def rank(self, irc, msg, args, channel):
"""[<channel>] """[<channel>|global]
Returns a list of top-ranked regexps, sorted by usage count Returns a list of top-ranked regexps, sorted by usage count
(rank). The number of regexps returned is set by the (rank). The number of regexps returned is set by the
@ -411,11 +416,11 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
return return
s = [ "#%d \"%s\" (%d)" % (i+1, regexp[0], regexp[1]) for i, regexp in enumerate(regexps) ] s = [ "#%d \"%s\" (%d)" % (i+1, regexp[0], regexp[1]) for i, regexp in enumerate(regexps) ]
irc.reply(", ".join(s)) irc.reply(", ".join(s))
rank = wrap(rank, ['channel']) rank = wrap(rank, ['channelOrGlobal'])
@internationalizeDocstring @internationalizeDocstring
def vacuum(self, irc, msg, args, channel): def vacuum(self, irc, msg, args, channel):
"""[<channel>] """[<channel>|global]
Vacuums the database for <channel>. Vacuums the database for <channel>.
See SQLite vacuum doc here: http://www.sqlite.org/lang_vacuum.html See SQLite vacuum doc here: http://www.sqlite.org/lang_vacuum.html
@ -433,7 +438,7 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
cursor.execute("""VACUUM""") cursor.execute("""VACUUM""")
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
vacuum = wrap(vacuum, ['channel']) vacuum = wrap(vacuum, ['channelOrGlobal'])
MessageParser = internationalizeDocstring(MessageParser) MessageParser = internationalizeDocstring(MessageParser)
Class = MessageParser Class = MessageParser