give messageparser ability to use arbitrary commands as trigger responses.

This commit is contained in:
Daniel Folkinshteyn 2010-03-16 16:49:55 -04:00
parent a88353dd0d
commit d72649c5c5
2 changed files with 19 additions and 3 deletions

View File

@ -54,6 +54,10 @@ import sqlite3
import threading import threading
import supybot.world as world import supybot.world as world
import supybot.log as log
class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler): class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
"""This plugin can set regexp triggers to activate the bot. """This plugin can set regexp triggers to activate the bot.
Use 'add' command to add regexp trigger, 'remove' to remove.""" Use 'add' command to add regexp trigger, 'remove' to remove."""
@ -105,6 +109,15 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
cursor.execute("UPDATE triggers SET usage_count=? WHERE regexp=?", (old_count + 1, regexp,)) cursor.execute("UPDATE triggers SET usage_count=? WHERE regexp=?", (old_count + 1, regexp,))
db.commit() db.commit()
def _runCommandFunction(self, irc, msg, command):
"""Run a command from message, as if command was sent over IRC."""
# need to encode it from unicode, since sqlite stores text as unicode.
tokens = callbacks.tokenize(unicode.encode(command, 'utf8'))
try:
self.Proxy(irc.irc, msg, tokens)
except Exception, e:
log.exception('Uncaught exception in scheduled function:')
def doPrivmsg(self, irc, msg): def doPrivmsg(self, irc, msg):
channel = msg.args[0] channel = msg.args[0]
if not irc.isChannel(channel): if not irc.isChannel(channel):
@ -125,8 +138,10 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
action = re.sub(r'\$' + str(i+1), match.group(i+1), action) action = re.sub(r'\$' + str(i+1), match.group(i+1), action)
actions.append(action) actions.append(action)
if len(actions) > 0: #if len(actions) > 0:
irc.replies(actions, prefixNick=False) # irc.replies(actions, prefixNick=False)
for action in actions:
self._runCommandFunction(irc, msg, action)
def add(self, irc, msg, args, channel, regexp, action): def add(self, irc, msg, args, channel, regexp, action):
"""[<channel>] <regexp> <action> """[<channel>] <regexp> <action>
@ -143,7 +158,6 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
(id, locked) = map(int, results[0]) (id, locked) = map(int, results[0])
else: else:
locked = False locked = False
#capability = ircdb.makeChannelCapability(channel, 'factoids')
if not locked: if not locked:
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

View File

@ -44,6 +44,8 @@ class Scheduler(callbacks.Plugin):
def _makeCommandFunction(self, irc, msg, command, remove=True): def _makeCommandFunction(self, irc, msg, command, remove=True):
"""Makes a function suitable for scheduling from command.""" """Makes a function suitable for scheduling from command."""
tokens = callbacks.tokenize(command) tokens = callbacks.tokenize(command)
print command
print tokens
def f(): def f():
if remove: if remove:
del self.events[str(f.eventId)] del self.events[str(f.eventId)]