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

« back to all changes in this revision

Viewing changes to build/lib/gplugs/away.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# plugs/away.py
 
2
#
 
3
 
4
 
 
5
""" keep away messages """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
from gozerbot.generic import rlog
 
10
from gozerbot.commands import cmnds
 
11
from gozerbot.examples import examples
 
12
from gozerbot.callbacks import callbacks
 
13
from gozerbot.users import users
 
14
from gozerbot.plugins import plugins
 
15
from gozerbot.datadir import datadir
 
16
from gozerbot.persist.persist import PlugPersist
 
17
from gozerbot.plughelp import plughelp
 
18
from gozerbot.tests import tests
 
19
import time, os
 
20
 
 
21
plughelp.add('away', "keep notice of who's away")
 
22
 
 
23
lasttime = []
 
24
 
 
25
# use datadir/away as pickle file
 
26
awaydict = PlugPersist('away.data')
 
27
 
 
28
# see if data attribute is set otherwise init it
 
29
if not awaydict.data:
 
30
    awaydict.data = {}
 
31
if not awaydict.data.has_key('enable'):
 
32
    awaydict.data['enable'] = 0
 
33
    awaydict.save()
 
34
 
 
35
def init():
 
36
    """ init plugin """
 
37
    if not awaydict.data['enable']:
 
38
        rlog(0, 'away', 'away is disabled')
 
39
        return 1
 
40
    else:
 
41
        rlog(0, 'away', 'away is enabled')
 
42
    callbacks.add('PRIVMSG', doaway, awaytest)
 
43
    callbacks.add('PRIVMSG', doback, backtest)
 
44
    return 1
 
45
 
 
46
def awaytest(bot, ievent):
 
47
    """ test if away callback should be called """
 
48
    if (len(ievent.txt) > 1 and ievent.txt[-1] == '\001' and \
 
49
ievent.txt[-2] == '&' ) or ievent.txt.strip() == '&':
 
50
        return 1
 
51
        
 
52
def doaway(bot, ievent):
 
53
    """ away callback """
 
54
    if not users.allowed(ievent.userhost, 'USER'):
 
55
        return
 
56
    # use username of user giving the command
 
57
    name = users.getname(ievent.userhost)
 
58
    if not name:
 
59
        return
 
60
    if awaydict.data.has_key((name, bot.name, ievent.channel)):
 
61
        return
 
62
    ievent.reply("ltrs %s" % ievent.nick)
 
63
    # add away data to entry indexed by username, botname and channel
 
64
    awaydict.data[(name, bot.name, ievent.channel)] = time.time()
 
65
    awaydict.save()
 
66
 
 
67
def backtest(bot, ievent):
 
68
    """ test if we should say hello """
 
69
    if 'back' == ievent.txt.strip() or 're' == ievent.txt.strip():
 
70
        return 1
 
71
    
 
72
def doback(bot, ievent):
 
73
    """ say hello """
 
74
    if not users.allowed(ievent.userhost, 'USER'):
 
75
        return
 
76
    # reset away entry 
 
77
    name = users.getname(ievent.userhost)
 
78
    if not name:
 
79
        return
 
80
    if not awaydict.data.has_key((name, bot.name, ievent.channel)):
 
81
        return
 
82
    ievent.reply("welcome back %s" % ievent.nick)
 
83
    try:
 
84
        del awaydict.data[(name, bot.name, ievent.channel)]
 
85
        awaydict.save()
 
86
    except KeyError:
 
87
        pass
 
88
 
 
89
def handle_awayenable(bot, ievent):
 
90
    """ away-enable .. enable away """
 
91
    awaydict.data['enable'] = 1
 
92
    awaydict.save()
 
93
    plugins.reload('gplugs', 'away')
 
94
    ievent.reply('away enabled')
 
95
 
 
96
cmnds.add('away-enable', handle_awayenable, 'OPER', threaded=True)
 
97
examples.add('away-enable' , 'enable the away plugin', 'away-enable')
 
98
 
 
99
def handle_awaydisable(bot, ievent):
 
100
    """ away-disable .. disable away """
 
101
    awaydict.data['enable'] = 0
 
102
    awaydict.save()
 
103
    plugins.reload('gplugs', 'away')
 
104
    ievent.reply('away disabled')
 
105
 
 
106
cmnds.add('away-disable', handle_awaydisable, 'OPER')
 
107
examples.add('away-disable', 'disable the away plugin', 'away-disable')
 
108
tests.add('away-enable --chan #dunkbots', 'enabled').fakein(':dunker!mekker@127.0.0.1 PRIVMSG #dunkbots :&').fakein(':dunker!mekker@127.0.0.1 PRIVMSG #dunkbots :re').add('away-disable --chan #dunkbots', 'disabled')