Limnoria/plugins/Seen.py

207 lines
7.5 KiB
Python
Raw Normal View History

2004-01-31 21:26:15 +01:00
###
# Copyright (c) 2002-2004, Jeremiah Fincher
2004-01-31 21:26:15 +01:00
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
"""
Keeps track of the last time a user was seen on a channel.
"""
import supybot
2004-01-31 21:26:15 +01:00
__revision__ = "$Id$"
__contributors__ = {
supybot.authors.skorobeus: ['wildcard support'],
}
2004-01-31 21:26:15 +01:00
import os
import re
import sets
import time
import getopt
import string
from itertools import imap, ifilter
2004-07-24 07:18:26 +02:00
import supybot.log as log
import supybot.conf as conf
import supybot.utils as utils
import supybot.world as world
import supybot.ircdb as ircdb
2004-10-23 06:55:52 +02:00
from supybot.commands import *
2004-07-24 07:18:26 +02:00
import supybot.ircmsgs as ircmsgs
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.registry as registry
import supybot.callbacks as callbacks
2004-01-31 21:26:15 +01:00
2004-02-11 07:20:29 +01:00
class IrcStringAndIntDict(utils.InsensitivePreservingDict):
def key(self, x):
if isinstance(x, int):
return x
else:
return ircutils.toLower(x)
2004-07-21 21:36:35 +02:00
class SeenDB(plugins.ChannelUserDB):
2004-02-11 07:20:29 +01:00
IdDict = IrcStringAndIntDict
def serialize(self, v):
return list(v)
2004-01-31 21:26:15 +01:00
def deserialize(self, channel, id, L):
(seen, saying) = L
return (float(seen), saying)
2004-07-21 21:36:35 +02:00
def update(self, channel, nickOrId, saying):
seen = time.time()
self[channel, nickOrId] = (seen, saying)
self[channel, '<last>'] = (seen, saying)
def seenWildcard(self, channel, nick):
nicks = []
nickRe = re.compile('.*'.join(nick.split('*')), re.I)
for (searchChan, searchNick) in self.keys():
#print 'chan: %s ... nick: %s' % (searchChan, searchNick)
if isinstance(searchNick, int):
# We need to skip the reponses that are keyed by id as they
# apparently duplicate the responses for the same person that
# are keyed by nick-string
continue
if ircutils.strEqual(searchChan, channel):
try:
s = nickRe.match(searchNick).group()
except AttributeError:
continue
nicks.append(s)
L = [[nick, self.seen(channel, nick)] for nick in nicks]
def negativeTime(x):
return -x[1][0]
utils.sortBy(negativeTime, L)
return L
2004-01-31 21:26:15 +01:00
def seen(self, channel, nickOrId):
return self[channel, nickOrId]
2004-01-31 21:26:15 +01:00
filename = os.path.join(conf.supybot.directories.data(), 'Seen.db')
2004-07-21 21:36:35 +02:00
2004-01-31 21:26:15 +01:00
class Seen(callbacks.Privmsg):
noIgnore = True
def __init__(self):
self.db = SeenDB(filename)
world.flushers.append(self.db.flush)
2004-09-20 01:51:21 +02:00
self.__parent = super(Seen, self)
self.__parent.__init__()
2004-01-31 21:26:15 +01:00
def die(self):
if self.db.flush in world.flushers:
world.flushers.remove(self.db.flush)
else:
self.log.debug('Odd, no flush in flushers: %r', world.flushers)
2004-01-31 21:26:15 +01:00
self.db.close()
2004-09-20 01:51:21 +02:00
self.__parent.die()
2004-07-21 21:36:35 +02:00
2004-01-31 21:26:15 +01:00
def doPrivmsg(self, irc, msg):
if ircutils.isChannel(msg.args[0]):
said = ircmsgs.prettyPrint(msg)
channel = msg.args[0]
self.db.update(channel, msg.nick, said)
try:
id = ircdb.users.getUserId(msg.prefix)
self.db.update(channel, id, said)
except KeyError:
pass # Not in the database.
2004-07-21 21:36:35 +02:00
2004-10-23 06:55:52 +02:00
def seen(self, irc, msg, args, channel, name):
2004-08-06 19:35:00 +02:00
"""[<channel>] <nick>
Returns the last time <nick> was seen and what <nick> was last seen
saying. <channel> is only necessary if the message isn't sent on the
channel itself.
"""
try:
results = []
if '*' in name:
results = self.db.seenWildcard(channel, name)
else:
results = [[name, self.db.seen(channel, name)]]
if len(results) == 1:
(nick, info) = results[0]
(when, said) = info
irc.reply('%s was last seen in %s %s ago saying: %s' %
(nick, channel, utils.timeElapsed(time.time()-when),
said))
elif len(results) > 1:
L = []
for (nick, info) in results:
(when, said) = info
L.append('%s (%s ago)' %
(nick, utils.timeElapsed(time.time()-when)))
2004-10-23 06:55:52 +02:00
irc.reply('%s could be %s' %
(name, utils.commaAndify(L, And='or')))
else:
2004-10-23 06:55:52 +02:00
irc.reply('I haven\'t seen anyone matching %s.' % name)
2004-08-06 19:35:00 +02:00
except KeyError:
irc.reply('I have not seen %s.' % name)
2004-10-23 06:55:52 +02:00
# XXX This should be channeldb, but ChannelUserDictionary does't support it.
seen = wrap(seen, ['channel', 'nick'])
2004-08-06 19:35:00 +02:00
2004-10-23 06:55:52 +02:00
def last(self, irc, msg, args, channel):
2004-08-06 19:35:00 +02:00
"""[<channel>]
Returns the last thing said in <channel>. <channel> is only necessary
if the message isn't sent in the channel itself.
"""
try:
(when, said) = self.db.seen(channel, '<last>')
irc.reply('Someone was last seen in %s %s ago saying: %s' %
(channel, utils.timeElapsed(time.time()-when), said))
2004-08-06 19:35:00 +02:00
except KeyError:
irc.reply('I have never seen anyone.')
2004-10-23 06:55:52 +02:00
last = wrap(last, ['channel'])
2004-08-06 19:35:00 +02:00
2004-10-23 06:55:52 +02:00
def user(self, irc, msg, args, channel, user):
2004-08-06 19:35:00 +02:00
"""[<channel>] <name>
2004-01-31 21:26:15 +01:00
Returns the last time <name> was seen and what <name> was last seen
2004-08-06 19:35:00 +02:00
saying. This looks up <name> in the user seen database, which means
that it could be any nick recognized as user <name> that was seen.
<channel> is only necessary if the message isn't sent in the channel
itself.
2004-01-31 21:26:15 +01:00
"""
2004-08-06 19:35:00 +02:00
try:
2004-10-23 06:55:52 +02:00
(when, said) = self.db.seen(channel, user.id)
irc.reply('%s was last seen in %s %s ago saying: %s' %
(user.name, channel, utils.timeElapsed(time.time()-when),
said))
2004-08-06 19:35:00 +02:00
except KeyError:
irc.reply('I have not seen %s.' % name)
2004-10-23 06:55:52 +02:00
user = wrap(user, ['channel', 'otherUser'])
2004-01-31 21:26:15 +01:00
Class = Seen
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: