Infobot is looking pretty complete!

This commit is contained in:
James Vega 2004-08-06 02:42:20 +00:00
parent 2952ed4d07
commit 04d0e5f667

View File

@ -111,6 +111,13 @@ class InfobotDB(object):
def close(self): def close(self):
self.flush() self.flush()
def changeIs(self, factoid, replacer):
old = self._is[factoid]
if replacer is not None:
self._is[factoid] = replacer(old)
self._changes += 1
self.flush()
def getIs(self, factoid): def getIs(self, factoid):
ret = self._is[factoid] ret = self._is[factoid]
self._responses += 1 self._responses += 1
@ -129,6 +136,13 @@ class InfobotDB(object):
def hasIs(self, factoid): def hasIs(self, factoid):
return factoid in self._is return factoid in self._is
def changeAre(self, factoid, replacer):
old = self._are[factoid]
if replacer is not None:
self._are[factoid] = replacer(old)
self._changes += 1
self.flush()
def getAre(self, factoid): def getAre(self, factoid):
ret = self._are[factoid] ret = self._are[factoid]
self._responses += 1 self._responses += 1
@ -163,7 +177,7 @@ class Dunno(Exception):
pass pass
class Infobot(callbacks.PrivmsgCommandAndRegexp): class Infobot(callbacks.PrivmsgCommandAndRegexp):
regexps = ['doForget', 'doFactoid', 'doUnknown'] regexps = ['doForget', 'doChange', 'doFactoid', 'doUnknown']
def __init__(self): def __init__(self):
callbacks.PrivmsgCommandAndRegexp.__init__(self) callbacks.PrivmsgCommandAndRegexp.__init__(self)
try: try:
@ -291,8 +305,10 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
tokens = callbacks.tokenize(payload) tokens = callbacks.tokenize(payload)
if callbacks.findCallbackForCommand(irc, tokens[0]): if callbacks.findCallbackForCommand(irc, tokens[0]):
return return
else: elif '=~' not in payload:
payload += '?' payload += '?'
else: # Looks like we have a doChange expression
pass
except SyntaxError: except SyntaxError:
pass pass
if payload.endswith(irc.nick): if payload.endswith(irc.nick):
@ -334,6 +350,31 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
# XXX: Should this be genericified? # XXX: Should this be genericified?
irc.reply('I\'ve never heard of %s, %s!' % (fact, msg.nick)) irc.reply('I\'ve never heard of %s, %s!' % (fact, msg.nick))
def doChange(self, irc, msg, match):
r"^(.+)\s+=~\s+(.+)$"
(fact, regexp) = match.groups()
changed = False
try:
r = utils.perlReToReplacer(regexp)
except ValueError, e:
if self.addressed:
irc.error('Invalid regexp: %s' % regexp)
return
else:
self.log.info('Invalid regexp: %s' % regexp)
return
for method in [self.db.changeIs, self.db.changeAre]:
try:
method(fact, r)
changed = True
except KeyError:
pass
if changed:
self.confirm()
else:
# XXX: Should this be genericified?
irc.reply('I\'ve never heard of %s, %s!' % (fact, msg.nick))
def doUnknown(self, irc, msg, match): def doUnknown(self, irc, msg, match):
r"^(.+?)\?[?!. ]*$" r"^(.+?)\?[?!. ]*$"
key = match.group(1) key = match.group(1)
@ -341,7 +382,7 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
self.factoid(key) # Does the dunno'ing for us itself. self.factoid(key) # Does the dunno'ing for us itself.
def invalidCommand(self, irc, msg, tokens): def invalidCommand(self, irc, msg, tokens):
self.replied = True irc.finished = True
def doFactoid(self, irc, msg, match): def doFactoid(self, irc, msg, match):
r"^(.+)\s+(?<!\\)(was|is|am|were|are)\s+(also\s+)?(.+?)[?!. ]*$" r"^(.+)\s+(?<!\\)(was|is|am|were|are)\s+(also\s+)?(.+?)[?!. ]*$"