~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to debian/gozerbot/usr/lib/python2.5/site-packages/gozerplugs/plugs/seen.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Description: tracks when a nick is last seen
2
 
# Author: Wijnand 'tehmaze' Modderman
3
 
# Website: http://tehmaze.com
4
 
# License: BSD
5
 
 
6
 
from gozerbot.callbacks import callbacks
7
 
from gozerbot.commands import cmnds
8
 
from gozerbot.datadir import datadir
9
 
from gozerbot.pdod import Pdod
10
 
from gozerbot.persistconfig import PersistConfig
11
 
import os, datetime
12
 
 
13
 
cfg = PersistConfig()
14
 
cfg.define('tz', '+0100')
15
 
 
16
 
class Seen(Pdod):
17
 
    def __init__(self):
18
 
        Pdod.__init__(self, os.path.join(datadir, 'seen'))
19
 
 
20
 
    def handle_seen(self, bot, ievent):
21
 
        if not ievent.args:
22
 
            ievent.missing('<nick>')
23
 
            return
24
 
        nick = ievent.args[0].lower()
25
 
        if not self.data.has_key(nick):
26
 
            alts = [x for x in self.data.keys() if nick in x]
27
 
            if alts:
28
 
                alts.sort()
29
 
                if len(alts) > 10:
30
 
                    nums = len(alts) - 10
31
 
                    alts = ', '.join(alts[:10]) + ' + %d others' % nums
32
 
                else:
33
 
                    alts = ', '.join(alts)
34
 
                ievent.reply('no logs for %s, however, I remember seeing: %s' % (nick, alts))
35
 
            else:
36
 
                ievent.reply('no logs for %s' % nick)
37
 
        else:
38
 
            text = self.data[nick]['text'] and ': %s' % self.data[nick]['text'] or ''
39
 
            ievent.reply('%s was last seen on %s at %s, %s%s' % (nick, self.data[nick]['bot'], 
40
 
                self.data[nick]['time'].strftime('%a, %d %b %Y %H:%M:%S '+cfg.get('tz')), 
41
 
                self.data[nick]['what'], text))
42
 
 
43
 
    def privmsgcb(self, bot, ievent):
44
 
        self.data[ievent.nick.lower()] = {
45
 
            'time':    datetime.datetime.now(),
46
 
            'text':    ievent.origtxt,
47
 
            'bot':     bot.name,
48
 
            'channel': ievent.channel,
49
 
            'what':    'saying',
50
 
            }
51
 
 
52
 
    def joincb(self, bot, ievent):
53
 
        self.data[ievent.nick.lower()] = {
54
 
            'time':    datetime.datetime.now(),
55
 
            'text':    '',
56
 
            'bot':     bot.name,
57
 
            'channel': ievent.channel,
58
 
            'what':    'joining %s' % ievent.channel,
59
 
            }
60
 
    
61
 
    def partcb(self, bot, ievent):
62
 
        self.data[ievent.nick.lower()] = {
63
 
            'time':    datetime.datetime.now(),
64
 
            'text':    ievent.txt,
65
 
            'bot':     bot.name,
66
 
            'channel': ievent.channel,
67
 
            'what':    'parting %s' % ievent.channel,
68
 
            }
69
 
    
70
 
    def quitcb(self, bot, ievent):
71
 
        self.data[ievent.nick.lower()] = {
72
 
            'time':    datetime.datetime.now(),
73
 
            'text':    ievent.txt,
74
 
            'bot':     bot.name,
75
 
            'channel': ievent.channel,
76
 
            'what':    'quitting',
77
 
            }
78
 
    
79
 
 
80
 
    def size(self):
81
 
        return len(self.data.keys())
82
 
 
83
 
def init():
84
 
    global seen
85
 
    seen = Seen()
86
 
    callbacks.add('PRIVMSG', seen.privmsgcb)
87
 
    callbacks.add('JOIN', seen.joincb)
88
 
    callbacks.add('PART', seen.partcb)
89
 
    callbacks.add('QUIT', seen.quitcb)
90
 
    cmnds.add('seen', seen.handle_seen, ['ANON', 'USER'])
91
 
    return 1
92
 
 
93
 
def shutdown():
94
 
    global seen
95
 
    seen.save()
96
 
    del seen
97
 
 
98
 
def size():
99
 
    global seen
100
 
    return seen.size()