Better emulation of Infobot wrt overwriting factoids and missing factoids.

This commit is contained in:
James Vega 2004-12-29 16:28:46 +00:00
parent 82a055c466
commit cb42006490
2 changed files with 83 additions and 45 deletions

View File

@ -503,16 +503,25 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
def confirm(self, irc=None, msg=None):
if self.registryValue('personality'):
self.reply(self.db.getConfirm(), irc=irc, msg=msg)
s = self.db.getConfirm()
else:
assert self.irc is not None
self.reply(conf.supybot.replies.success())
s = conf.supybot.replies.success()
self.reply(s, irc=irc, msg=msg)
def missing(self, fact, irc=None, msg=None):
if msg is None:
assert self.msg is not None
msg = self.msg
self.reply('I didn\'t have anything matching %s, %s.' %
(utils.quoted(fact), msg.nick),
irc=irc, msg=msg)
def dunno(self, irc=None, msg=None):
if self.registryValue('personality'):
self.reply(self.db.getDunno(), irc=irc, msg=msg)
s = self.db.getDunno()
else:
self.reply(self.registryValue('boringDunno'), irc=irc, msg=msg)
s = self.registryValue('boringDunno')
self.reply(s, irc=irc, msg=msg)
def factoid(self, key, irc=None, msg=None, dunno=True, prepend='',
isAre=None):
@ -568,10 +577,16 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
s = prepend + s
self.reply(s, irc=irc, msg=msg)
def normalize(self, s):
_iAm = (re.compile(r'^i am ', re.I), '%s is ')
_my = (re.compile(r'^my ', re.I), '%s\'s ')
_your = (re.compile(r'^your ', re.I), '%s\'s ')
def normalize(self, s, bot, nick):
s = ircutils.stripFormatting(s)
s = s.strip() # After stripFormatting for formatted spaces.
s = utils.normalizeWhitespace(s)
s = self._iAm[0].sub(self._iAm[1] % nick, s)
s = self._my[0].sub(self._my[1] % nick, s)
s = self._your[0].sub(self._your[1] % bot, s)
contractions = [('what\'s', 'what is'), ('where\'s', 'where is'),
('who\'s', 'who is'), ('wtf\'s', 'wtf is'),]
for (contraction, replacement) in contractions:
@ -593,7 +608,10 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
return
# For later dynamic scoping
channel = plugins.getChannel(msg.args[0])
payload = self.normalize(msg.args[1])
s = callbacks.addressed(irc.nick, msg)
payload = self.normalize(s or msg.args[1], irc.nick, msg.nick)
if s:
msg.tag('addressed', payload)
msg = ircmsgs.IrcMsg(args=(msg.args[0], payload), msg=msg)
self.__parent.doPrivmsg(irc, msg)
finally:
@ -622,9 +640,8 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
pass
if deleted:
self.confirm()
else:
# XXX: Should this be genericified?
self.reply('I\'ve never heard of %s, %s!' % (fact, msg.nick))
elif msg.addressed:
self.missing(fact, irc=irc, msg=msg)
def doForce(self, irc, msg, match):
r"^no,\s+(\w+,\s+)?(.+?)\s+(?<!\\)(was|is|am|were|are)\s+(.+?)[?!. ]*$"
@ -647,18 +664,27 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
isAre = isAre.lower()
if self.added:
return
channel = dynamic.channel
if isAre in ('was', 'is', 'am'):
if self.db.hasIs(dynamic.channel, key):
if self.db.hasIs(channel, key):
oldValue = self.db.getIs(channel, key)
if oldValue.lower() == value.lower():
self.reply('I already had it that way, %s.' % msg.nick)
return
self.log.debug('Forcing %s to %s.',
utils.quoted(key), utils.quoted(value))
self.added = True
self.db.setIs(dynamic.channel, key, value)
self.db.setIs(channel, key, value)
else:
if self.db.hasAre(dynamic.channel, key):
if self.db.hasAre(channel, key):
oldValue = self.db.getAre(channel, key)
if oldValue.lower() == value.lower():
self.reply('I already had it that way, %s.' % msg.nick)
return
self.log.debug('Forcing %s to %s.',
utils.quoted(key), utils.quoted(value))
self.added = True
self.db.setAre(dynamic.channel, key, value)
self.db.setAre(channel, key, value)
self.confirm()
def doChange(self, irc, msg, match):
@ -684,8 +710,7 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
if changed:
self.confirm()
else:
# XXX: Should this be genericified?
self.reply('I\'ve never heard of %s, %s!' % (fact, msg.nick))
self.missing(fact, irc=irc, msg=msg)
def doUnknown(self, irc, msg, match):
r"^(.+?)\s*(\?[?!. ]*)?$"
@ -718,22 +743,18 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
return
if isAre in ('was', 'is', 'am'):
if self.db.hasIs(dynamic.channel, key):
oldValue = self.db.getIs(dynamic.channel, key)
if oldValue.lower() == value.lower():
self.reply('I already had it that way, %s.' % msg.nick)
return
if also:
self.log.debug('Adding %s to %s.',
utils.quoted(key), utils.quoted(value))
value = '%s or %s' % (self.db.getIs(dynamic.channel, key),
value)
elif not msg.addressed:
value = self.db.getIs(dynamic.channel, key)
if initialIs.get(key) != value:
self.reply('... but %s is %s, %s ...' %
(key, value, msg.nick), substitute=False)
return
value = '%s or %s' % (oldValue, value)
elif msg.addressed:
value = self.db.getIs(dynamic.channel, key)
if initialIs.get(key) != value:
self.reply('But %s is %s, %s.' %
(key, value, msg.nick), substitute=False)
self.reply('... but %s is %s ...' %
(key, oldValue), substitute=False)
return
else:
self.log.debug('Already have a %s key.',
@ -743,22 +764,18 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
self.db.setIs(dynamic.channel, key, value)
else:
if self.db.hasAre(dynamic.channel, key):
oldValue = self.db.getAre(dynamic.channel, key)
if oldValue.lower() == value.lower():
self.reply('I already had it that way, %s.' % msg.nick)
return
if also:
self.log.debug('Adding %s to %s.',
utils.quoted(key), utils.quoted(value))
value = '%s or %s' % (self.db.getAre(dynamic.channel, key),
value)
elif not msg.addressed:
value = self.db.getAre(dynamic.channel, key)
if initialAre.get(key) != value:
self.reply('... but %s are %s, %s ...' %
(key, value, msg.nick), substitute=False)
return
value = '%s or %s' % (oldValue, value)
elif msg.addressed:
value = self.db.getAre(dynamic.channel, key)
if initialAre.get(key) != value:
self.reply('But %s are %s, %s.' %
(key, value, msg.nick), substitute=False)
self.reply('... but %s are %s ...' %
(key, oldValue), substitute=False)
return
else:
self.log.debug('Already have a %s key.',

View File

@ -46,7 +46,7 @@ class InfobotTestCase(ChannelPluginTestCase):
self.assertSnarfNoResponse('foo is at http://bar.com/', 2)
self.assertRegexp('infobot stats', '1 modification')
self.assertRegexp('infobot status', '1 modification')
self.assertSnarfRegexp('foo?', r'http://bar.com/')
self.assertSnarfRegexp('foo?', r'foo.*is.*http://bar.com/')
self.assertSnarfNoResponse('foo is at http://baz.com/', 2)
self.assertSnarfNotRegexp('foo?', 'baz')
m = self.getMsg('bar is at http://foo.com/')
@ -56,6 +56,27 @@ class InfobotTestCase(ChannelPluginTestCase):
ibot.unaddressed.snarfDefinitions.setValue(learn)
ibot.unaddressed.answerQuestions.setValue(answer)
def testNormalize(self):
learn = ibot.unaddressed.snarfDefinitions()
answer = ibot.unaddressed.answerQuestions()
try:
ibot.unaddressed.snarfDefinitions.setValue(True)
ibot.unaddressed.answerQuestions.setValue(True)
self.assertSnarfNoResponse('foo is at http://bar.com/', 2)
self.assertSnarfRegexp('what\'s foo?', r'foo.*is.*http://bar.com/')
self.assertSnarfRegexp('wtf\'s foo?', r'foo.*is.*http://bar.com/')
self.assertSnarfRegexp('who\'s foo?', r'foo.*is.*http://bar.com/')
self.assertSnarfRegexp('where\'s foo?',r'foo.*is.*http://bar.com/')
self.assertSnarfNoResponse('i am a tool', 2)
self.assertRegexp('%s?' % self.nick, r'is.*tool')
self.assertSnarfNoResponse('my bot is a tool', 2)
self.assertRegexp('%s\'s bot?' % self.nick, r'is.*tool')
self.assertSnarfNoResponse('your name is weird', 2)
self.assertRegexp('%s\'s name?' % self.irc.nick, r'is.*weird')
finally:
ibot.unaddressed.snarfDefinitions.setValue(learn)
ibot.unaddressed.answerQuestions.setValue(answer)
def testAreSnarf(self):
learn = ibot.unaddressed.snarfDefinitions()
answer = ibot.unaddressed.answerQuestions()
@ -91,7 +112,7 @@ class InfobotTestCase(ChannelPluginTestCase):
try:
ibot.unaddressed.answerQuestions.setValue(True)
self.assertSnarfNoResponse('foo is bar')
self.assertSnarfRegexp('foo?', 'bar')
self.assertSnarfRegexp('foo?', 'foo.*is.*bar')
ibot.unaddressed.answerQuestions.setValue(False)
self.assertSnarfNoResponse('foo?', 2)
finally:
@ -104,9 +125,9 @@ class InfobotTestCase(ChannelPluginTestCase):
ibot.unaddressed.answerQuestions.setValue(True)
ibot.unaddressed.snarfDefinitions.setValue(True)
self.assertSnarfNoResponse('forums are good')
self.assertSnarfRegexp('forums?', 'good')
self.assertSnarfRegexp('forums?', r'forums.*are.*good')
self.assertNotError('no, forums are evil')
self.assertSnarfRegexp('forums?', 'evil')
self.assertSnarfRegexp('forums?', r'forums.*.are.*evil')
finally:
ibot.unaddressed.answerQuestions.setValue(answer)
ibot.unaddressed.snarfDefinitions.setValue(learn)
@ -129,15 +150,15 @@ class InfobotTestCase(ChannelPluginTestCase):
ibot.unaddressed.answerQuestions.setValue(answer)
ibot.unaddressed.snarfDefinitions.setValue(learn)
# def testNoKarmaDunno(self):
# self.assertNoResponse('foo++')
def testNoKarmaDunno(self):
self.assertNoResponse('foo++')
def testPredefinedFactoids(self):
self.assertSnarfNoResponse('what?', 3)
self.assertRegexp('roses?', 'roses are red')
def testAddressedQuestions(self):
self.assertNotError('hi is <reply>Hello, $who.')
self.assertSnarfNoResponse('hi is <reply>Hello, $who.')
self.assertSnarfNoResponse('hi')
self.assertRegexp('hi', 'Hello')