Add plugins.getChannel

This commit is contained in:
James Vega 2004-12-17 05:18:21 +00:00
parent 75eb335e39
commit 3b2ef3d88f
5 changed files with 46 additions and 52 deletions

View File

@ -320,15 +320,6 @@ class SqliteMoobotDB(object):
MoobotDB = plugins.DB('MoobotFactoids', {'sqlite': SqliteMoobotDB}) MoobotDB = plugins.DB('MoobotFactoids', {'sqlite': SqliteMoobotDB})
## We define our own getChannel so we can take advantage of the channeldb
# wrapper from non-command methods. This means that the bot will use
# supybot.databases.plugins.channelSpecific.channel as the default channel
# when the user is talking to the bot privately.
def getChannel(irc, msg, args=()):
spec = Spec(['channeldb'])
state = spec(irc, msg, args)
return state.channel
class MoobotFactoids(callbacks.Privmsg): class MoobotFactoids(callbacks.Privmsg):
callBefore = ['Dunno'] callBefore = ['Dunno']
def __init__(self): def __init__(self):
@ -369,7 +360,7 @@ class MoobotFactoids(callbacks.Privmsg):
else: else:
key = ' '.join(tokens) key = ' '.join(tokens)
key = self._sanitizeKey(key) key = self._sanitizeKey(key)
channel = getChannel(irc, msg) channel = plugins.getChannel(msg.args[0])
fact = self.db.getFactoid(channel, key) fact = self.db.getFactoid(channel, key)
if fact: if fact:
self.db.updateRequest(channel, key, msg.prefix) self.db.updateRequest(channel, key, msg.prefix)
@ -427,7 +418,7 @@ class MoobotFactoids(callbacks.Privmsg):
def addFactoid(self, irc, msg, tokens): def addFactoid(self, irc, msg, tokens):
# First, check and see if the entire message matches a factoid key # First, check and see if the entire message matches a factoid key
channel = getChannel(irc, msg) channel = plugins.getChannel(msg.args[0])
id = self._getUserId(irc, msg.prefix) id = self._getUserId(irc, msg.prefix)
(key, fact) = self._getKeyAndFactoid(tokens) (key, fact) = self._getKeyAndFactoid(tokens)
# Check and make sure it's not in the DB already # Check and make sure it's not in the DB already
@ -440,7 +431,7 @@ class MoobotFactoids(callbacks.Privmsg):
id = self._getUserId(irc, msg.prefix) id = self._getUserId(irc, msg.prefix)
(key, regexp) = map(' '.join, (key, regexp) = map(' '.join,
utils.itersplit('=~'.__eq__, tokens, maxsplit=1)) utils.itersplit('=~'.__eq__, tokens, maxsplit=1))
channel = getChannel(irc, msg) channel = plugins.getChannel(msg.args[0])
# Check and make sure it's in the DB # Check and make sure it's in the DB
fact = self._getFactoid(irc, channel, key) fact = self._getFactoid(irc, channel, key)
self._checkNotLocked(irc, channel, key) self._checkNotLocked(irc, channel, key)
@ -461,7 +452,7 @@ class MoobotFactoids(callbacks.Privmsg):
isAlso = pairs.index(['is', 'also']) isAlso = pairs.index(['is', 'also'])
key = ' '.join(tokens[:isAlso]) key = ' '.join(tokens[:isAlso])
new_text = ' '.join(tokens[isAlso+2:]) new_text = ' '.join(tokens[isAlso+2:])
channel = getChannel(irc, msg) channel = plugins.getChannel(msg.args[0])
fact = self._getFactoid(irc, channel, key) fact = self._getFactoid(irc, channel, key)
self._checkNotLocked(irc, channel, key) self._checkNotLocked(irc, channel, key)
# It's fair game if we get to here # It's fair game if we get to here
@ -472,7 +463,7 @@ class MoobotFactoids(callbacks.Privmsg):
def replaceFactoid(self, irc, msg, tokens): def replaceFactoid(self, irc, msg, tokens):
# Must be registered! # Must be registered!
channel = getChannel(irc, msg) channel = plugins.getChannel(msg.args[0])
id = self._getUserId(irc, msg.prefix) id = self._getUserId(irc, msg.prefix)
del tokens[0] # remove the "no," del tokens[0] # remove the "no,"
(key, fact) = self._getKeyAndFactoid(tokens) (key, fact) = self._getKeyAndFactoid(tokens)

View File

@ -125,10 +125,7 @@ class Seen(callbacks.Privmsg):
def doPrivmsg(self, irc, msg): def doPrivmsg(self, irc, msg):
if irc.isChannel(msg.args[0]): if irc.isChannel(msg.args[0]):
said = ircmsgs.prettyPrint(msg) said = ircmsgs.prettyPrint(msg)
channel = msg.args[0] channel = plugins.getChannel(msg.args[0])
channelSpecific = conf.supybot.databases.plugins.channelSpecific
if not conf.get(channelSpecific, channel):
channel = conf.get(channelSpecific.channel, channel)
self.db.update(channel, msg.nick, said) self.db.update(channel, msg.nick, said)
try: try:
id = ircdb.users.getUserId(msg.prefix) id = ircdb.users.getUserId(msg.prefix)

View File

@ -150,6 +150,7 @@ class WordStatsDB(plugins.ChannelUserDB):
(channel, text) = msg.args (channel, text) = msg.args
if not ircutils.isChannel(channel): if not ircutils.isChannel(channel):
return return
channel = plugins.getChannel(channel)
text = text.strip().lower() text = text.strip().lower()
if not text: if not text:
return return
@ -217,7 +218,7 @@ class WordStats(callbacks.Privmsg):
return return
self.db.addWord(channel, word) self.db.addWord(channel, word)
irc.replySuccess() irc.replySuccess()
add = wrap(add, ['channel', 'somethingWithoutSpaces']) add = wrap(add, ['channeldb', 'somethingWithoutSpaces'])
def remove(self, irc, msg, args, channel, word): def remove(self, irc, msg, args, channel, word):
"""[<channel>] <word> """[<channel>] <word>
@ -235,7 +236,7 @@ class WordStats(callbacks.Privmsg):
'on.' % utils.quoted(word)) 'on.' % utils.quoted(word))
else: else:
irc.error('I am not currently keeping any word stats.') irc.error('I am not currently keeping any word stats.')
remove = wrap(remove, ['channel', 'somethingWithoutSpaces']) remove = wrap(remove, ['channeldb', 'somethingWithoutSpaces'])
def wordstats(self, irc, msg, args, channel, user, word): def wordstats(self, irc, msg, args, channel, user, word):
"""[<channel>] [<user>] [<word>] """[<channel>] [<user>] [<word>]
@ -319,7 +320,7 @@ class WordStats(callbacks.Privmsg):
except KeyError: except KeyError:
irc.error('I have no wordstats for %s.' % user.name) irc.error('I have no wordstats for %s.' % user.name)
wordstats = wrap(wordstats, wordstats = wrap(wordstats,
['channel', ['channeldb',
optional('otherUser'), optional('otherUser'),
additional('somethingWithoutSpaces')]) additional('somethingWithoutSpaces')])

View File

@ -101,7 +101,7 @@ class NoSuitableDatabase(Exception):
def __init__(self, suitable): def __init__(self, suitable):
self.suitable = suitable self.suitable = suitable
self.suitable.sort() self.suitable.sort()
def __str__(self): def __str__(self):
return 'No suitable databases were found. Suitable databases ' \ return 'No suitable databases were found. Suitable databases ' \
'include %s. If you have one of these databases installed, ' \ 'include %s. If you have one of these databases installed, ' \
@ -169,6 +169,11 @@ def makeChannelFilename(filename, channel=None, dirname=None):
os.makedirs(dirname) os.makedirs(dirname)
return os.path.join(dirname, filename) return os.path.join(dirname, filename)
def getChannel(channel):
channelSpecific = conf.supybot.databases.plugins.channelSpecific
if not conf.get(channelSpecific, channel):
channel = conf.get(channelSpecific.channel, channel)
return channel
# XXX This shouldn't be a mixin. This should be contained by classes that # XXX This shouldn't be a mixin. This should be contained by classes that
# want such behavior. But at this point, it wouldn't gain much for us # want such behavior. But at this point, it wouldn't gain much for us
@ -251,7 +256,7 @@ class DbiChannelDB(object):
db = self._getDb(channel) db = self._getDb(channel)
return getattr(db, attr)(*args, **kwargs) return getattr(db, attr)(*args, **kwargs)
return _getDbAndDispatcher return _getDbAndDispatcher
# XXX This should eventually be gotten rid of in favor of some dbi thing. At # XXX This should eventually be gotten rid of in favor of some dbi thing. At
# the very least, it ought to get an interface much closer to dbi.DB. # the very least, it ought to get an interface much closer to dbi.DB.
@ -402,7 +407,7 @@ class ChannelIdDatabasePlugin(callbacks.Privmsg):
if ircdb.checkCapability(msg.prefix, cap): if ircdb.checkCapability(msg.prefix, cap):
return True return True
irc.errorNoCapability(cap) irc.errorNoCapability(cap)
def addValidator(self, irc, text): def addValidator(self, irc, text):
"""This should irc.error or raise an exception if text is invalid.""" """This should irc.error or raise an exception if text is invalid."""
pass pass

View File

@ -54,11 +54,11 @@ class WordStatsTestCase(ChannelPluginTestCase):
self.assertNotError('add lol') self.assertNotError('add lol')
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol',
prefix=self.prefix)) prefix=self.prefix))
self.assertResponse('wordstats foo', '\'lol\': 2') self.assertRegexp('wordstats foo', r'[\'"]lol[\'"]: 2')
self.assertNotError('add moo') self.assertNotError('add moo')
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'moo', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'moo',
prefix=self.prefix)) prefix=self.prefix))
self.assertResponse('wordstats foo', '\'lol\': 2 and \'moo\': 2') self.assertRegexp('wordstats foo', r'lol[\'"]: 2 and [\'"]moo[\'"]: 2')
def testWordStatsWord(self): def testWordStatsWord(self):
userPrefix1 = 'moo!bar@baz'; userNick1 = 'moo' userPrefix1 = 'moo!bar@baz'; userNick1 = 'moo'
@ -77,12 +77,12 @@ class WordStatsTestCase(ChannelPluginTestCase):
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol',
prefix=userPrefix1)) prefix=userPrefix1))
self.assertRegexp('wordstats lol', self.assertRegexp('wordstats lol',
'2.*%s: 5.*foo: 2' % userNick1) r'2.*%s: 5.*foo: 2' % userNick1)
for i in range(10): for i in range(10):
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol',
prefix=userPrefix2)) prefix=userPrefix2))
self.assertRegexp('wordstats lol', self.assertRegexp('wordstats lol',
'3.*%s: 10.*%s: 5.*foo: 3' % r'3.*%s: 10.*%s: 5.*foo: 3' %
(userNick2, userNick1)) (userNick2, userNick1))
# Check for the extra-swanky stuff too # Check for the extra-swanky stuff too
# (note: to do so we must make sure they don't appear in the list, # (note: to do so we must make sure they don't appear in the list,
@ -91,50 +91,50 @@ class WordStatsTestCase(ChannelPluginTestCase):
orig = conf.supybot.plugins.WordStats.rankingDisplay() orig = conf.supybot.plugins.WordStats.rankingDisplay()
conf.supybot.plugins.WordStats.rankingDisplay.setValue(2) conf.supybot.plugins.WordStats.rankingDisplay.setValue(2)
self.assertRegexp('wordstats lol', self.assertRegexp('wordstats lol',
'total.*19 \'lol\'s.*%s: 10.*%s: 5.*' r'total.*19 [\'"]lol[\'"]s.*%s: 10.*%s: 5.*'
'ranked 3 out of 3 \'lol\'ers' % \ r'ranked 3 out of 3 [\'"]lol[\'"]ers' % \
(userNick2, userNick1)) (userNick2, userNick1))
finally: finally:
conf.supybot.plugins.WordStats.rankingDisplay.setValue(orig) conf.supybot.plugins.WordStats.rankingDisplay.setValue(orig)
def testWordStatsUserWord(self): def testWordStatsUserWord(self):
self.assertNotError('add lol') self.assertNotError('add lol')
self.assertResponse('wordstats foo lol', self.assertRegexp('wordstats foo lol',
'foo has said \'lol\' 1 time.') r'foo has said [\'"]lol[\'"] 1 time.')
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol',
prefix=self.prefix)) prefix=self.prefix))
self.assertResponse('wordstats foo lol', self.assertRegexp('wordstats foo lol',
'foo has said \'lol\' 3 times.') r'foo has said [\'"]lol[\'"] 3 times.')
# Now check for case-insensitivity # Now check for case-insensitivity
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'LOL', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'LOL',
prefix=self.prefix)) prefix=self.prefix))
self.assertResponse('wordstats foo lol', self.assertRegexp('wordstats foo lol',
'foo has said \'lol\' 5 times.') r'foo has said [\'"]lol[\'"] 5 times.')
# Check and make sure actions get nabbed too # Check and make sure actions get nabbed too
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol',
prefix=self.prefix)) prefix=self.prefix))
self.assertResponse('wordstats foo lol', self.assertRegexp('wordstats foo lol',
'foo has said \'lol\' 7 times.') r'foo has said [\'"]lol[\'"] 7 times.')
# Check and make sure it handles two words in one message # Check and make sure it handles two words in one message
self.assertNotError('add heh') self.assertNotError('add heh')
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol heh', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol heh',
prefix=self.prefix)) prefix=self.prefix))
self.assertResponse('wordstats foo lol', self.assertRegexp('wordstats foo lol',
'foo has said \'lol\' 9 times.') r'foo has said [\'"]lol[\'"] 9 times.')
self.assertResponse('wordstats foo heh', self.assertRegexp('wordstats foo heh',
'foo has said \'heh\' 2 times.') r'foo has said [\'"]heh[\'"] 2 times.')
# It should ignore punctuation around words # It should ignore punctuation around words
self.irc.feedMsg(ircmsgs.privmsg(self.channel,'lol, I said "heh"', self.irc.feedMsg(ircmsgs.privmsg(self.channel,'lol, I said "heh"',
prefix=self.prefix)) prefix=self.prefix))
self.assertResponse('wordstats foo lol', self.assertRegexp('wordstats foo lol',
'foo has said \'lol\' 11 times.') r'foo has said [\'"]lol[\'"] 11 times.')
self.assertResponse('wordstats foo heh', self.assertRegexp('wordstats foo heh',
'foo has said \'heh\' 4 times.') r'foo has said [\'"]heh[\'"] 4 times.')
def testAddword(self): def testAddword(self):
self.assertError('add lol!') self.assertError('add lol!')
self.assertNotError('add lolz0r') self.assertNotError('add lolz0r')
self.assertRegexp('wordstats lolz0r', r'1 \'lolz0r\' seen') self.assertRegexp('wordstats lolz0r', r'1 [\'"]lolz0r[\'"] seen')
def testRemoveword(self): def testRemoveword(self):
# Using a word that's also the name of a user isn't smart since that # Using a word that's also the name of a user isn't smart since that
@ -142,13 +142,13 @@ class WordStatsTestCase(ChannelPluginTestCase):
#self.assertError('wordstats remove foo') #self.assertError('wordstats remove foo')
self.assertError('wordstats remove blarg') self.assertError('wordstats remove blarg')
self.assertNotError('wordstats add blarg') self.assertNotError('wordstats add blarg')
self.assertRegexp('wordstats blarg', r'1 \'blarg\' seen') self.assertRegexp('wordstats blarg', r'1 [\'"]blarg[\'"] seen')
self.assertRegexp('wordstats blarg', r'2 \'blarg\'s seen') self.assertRegexp('wordstats blarg', r'2 [\'"]blarg[\'"]s seen')
self.assertNotError('wordstats remove blarg') self.assertNotError('wordstats remove blarg')
self.assertRegexp('wordstats blarg', r'doesn\'t look like a word I') self.assertRegexp('wordstats blarg', r'doesn\'t look like a word I')
# Verify that we aren't keeping results from before # Verify that we aren't keeping results from before
self.assertNotError('add blarg') self.assertNotError('add blarg')
self.assertRegexp('wordstats blarg', r'1 \'blarg\' seen') self.assertRegexp('wordstats blarg', r'1 [\'"]blarg[\'"] seen')
def testWordStatsRankingDisplay(self): def testWordStatsRankingDisplay(self):
self.assertNotError('add lol') self.assertNotError('add lol')
@ -171,8 +171,8 @@ class WordStatsTestCase(ChannelPluginTestCase):
prefix=users[i][0])) prefix=users[i][0]))
# Make sure it shows the top 5 # Make sure it shows the top 5
self.assertRegexp('wordstats lol', self.assertRegexp('wordstats lol',
'Top 5 \'lol\'ers.*foo9: 9.*foo8: 8.*' r'Top 5 [\'"]lol[\'"]ers.*foo9: 9.*foo8: 8.*'
'foo7: 7.*foo6: 6.*foo5: 5') r'foo7: 7.*foo6: 6.*foo5: 5')
finally: finally:
conf.supybot.plugins.WordStats.rankingDisplay.setValue(orig) conf.supybot.plugins.WordStats.rankingDisplay.setValue(orig)