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

« back to all changes in this revision

Viewing changes to gozerplugs/plugs/nickserv.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
 
# gozerplugs/plugs/nickserv.py
2
 
#
3
 
# let gozerbot authenticate to NickServ
4
 
 
5
 
__author__ = "Wijnand 'tehmaze' Modderman - http://tehmaze.com"
6
 
__license__ ='BSD'
7
 
__gendoclast__ = ['ns-del', ]
8
 
 
9
 
from gozerbot.examples import examples
10
 
from gozerbot.callbacks import callbacks
11
 
from gozerbot.commands import cmnds
12
 
from gozerbot.datadir import datadir
13
 
from gozerbot.fleet import fleet
14
 
from gozerbot.pdod import Pdod
15
 
from gozerbot.plughelp import plughelp
16
 
from gozerbot.generic import rlog
17
 
from gozerbot.config import config
18
 
import os, time
19
 
 
20
 
plughelp.add('nickserv', 'authenticate the bot through (nick)services')
21
 
 
22
 
class NSAuth(Pdod):
23
 
    def __init__(self):
24
 
        self.registered = False
25
 
        Pdod.__init__(self, os.path.join(datadir, 'nickserv'))
26
 
 
27
 
    def add(self, bot, **kwargs):
28
 
        options = {
29
 
            'nickserv': 'NickServ',
30
 
            'identify': 'IDENTIFY',
31
 
        }
32
 
        options.update(kwargs)
33
 
        assert options.has_key('password'), 'A password must be set'
34
 
        for key in options.keys():
35
 
            Pdod.set(self, bot.name, key, options[key])
36
 
        self.save()
37
 
 
38
 
    def remove(self, bot):
39
 
        if self.has_key(bot.name):
40
 
            del self[bot.name]
41
 
            self.save()
42
 
 
43
 
    def has(self, bot):
44
 
        return self.has_key(bot.name)
45
 
 
46
 
    def register(self, bot, passwd):
47
 
        if self.has_key(bot.name) and self.has_key2(bot.name, 'nickserv'):
48
 
            bot.sendraw('PRIVMSG %s :%s %s' % (self.get(bot.name, \
49
 
'nickserv'),  'REGISTER', passwd))
50
 
            rlog(10, 'nickserv', 'register sent on %s' % bot.server)
51
 
 
52
 
    def identify(self, bot):
53
 
        if self.has_key(bot.name) and self.has_key2(bot.name, 'nickserv'):
54
 
            bot.sendraw('PRIVMSG %s :%s %s' % (self.get(bot.name, \
55
 
'nickserv'),  self.get(bot.name, 'identify'), self.get(bot.name, 'password')))
56
 
            rlog(10, 'nickserv', 'identify sent on %s' % bot.server)
57
 
 
58
 
    def listbots(self): 
59
 
        all = []
60
 
        for bot in self.data.keys():
61
 
            all.append((bot, self.data[bot]['nickserv']))
62
 
        return all
63
 
 
64
 
    def sendstring(self, bot, txt):
65
 
        nickservnick = self.get(bot.name, 'nickserv')
66
 
        bot.say(nickservnick, txt)
67
 
 
68
 
    def handle_001(self, bot, ievent):
69
 
        self.identify(bot)
70
 
        try:
71
 
            for i in self.data[bot.name]['nickservtxt']:
72
 
                self.sendstring(bot, i)
73
 
                rlog(10, 'nickserv', 'sent %s' % i)
74
 
        except:
75
 
            pass
76
 
 
77
 
nsauth = NSAuth()
78
 
 
79
 
callbacks.add('001', nsauth.handle_001, threaded=True)
80
 
 
81
 
def init():
82
 
    bot = fleet.byname('main')
83
 
    if bot.jabber:
84
 
        return
85
 
    passwd = config['nickservpass']
86
 
    if passwd:
87
 
        nsauth.add(bot, **{'password': passwd, 'nickservtxt': \
88
 
config['nickservtxt']})
89
 
    return 1
90
 
 
91
 
def handle_nsadd(bot, ievent):
92
 
    if len(ievent.args) < 1:
93
 
        ievent.missing('<password> [<nickserv nick>] [<identify command>]')
94
 
        return
95
 
    if nsauth.has(bot):
96
 
        ievent.reply('replacing previous configuration')
97
 
    options = {}
98
 
    if len(ievent.args) >= 1:
99
 
        options.update({'password': ievent.args[0]})
100
 
    if len(ievent.args) >= 2:
101
 
        options.update({'nickserv': ievent.args[1]})
102
 
    if len(ievent.args) >= 3:
103
 
        options.update({'identify': ' '.join(ievent.args[2:])})
104
 
    nsauth.add(bot, **options)
105
 
    ievent.reply('ok')
106
 
 
107
 
cmnds.add('ns-add', handle_nsadd, 'OPER')
108
 
examples.add('ns-add', 'ns-add <password> [<nickserv nick>] [<identify \
109
 
command>] .. add nickserv', 'ns-add mekker')
110
 
 
111
 
def handle_nsdel(bot, ievent):
112
 
    if len(ievent.args) != 1:
113
 
        ievent.missing('<fleetbot name>')
114
 
        return
115
 
    fbot = fleet.byname(ievent.args[0])
116
 
    if not fbot:
117
 
        ievent.reply('fleet not found')
118
 
        return
119
 
    if not nsauth.has(fbot):
120
 
        ievent.reply('nickserv not configured on %s' % fbot.name)
121
 
        return
122
 
    nsauth.remove(fbot)
123
 
    ievent.reply('ok')
124
 
 
125
 
cmnds.add('ns-del', handle_nsdel, 'OPER')
126
 
examples.add('ns-del', 'ns-del <fleetbot name>', 'ns-del test')
127
 
 
128
 
def handle_nssend(bot, ievent):
129
 
    if not ievent.rest:
130
 
        ievent.missing('<txt>')
131
 
        return
132
 
    nsauth.sendstring(bot, ievent.rest)    
133
 
    ievent.reply('send')
134
 
 
135
 
cmnds.add('ns-send', handle_nssend, 'OPER')
136
 
examples.add('ns-send', 'ns-send <txt> .. send txt to nickserv', 'ns-send \
137
 
identify bla')
138
 
 
139
 
def handle_nsauth(bot, ievent):
140
 
    if len(ievent.args) != 1:
141
 
        name = bot.name
142
 
    else:
143
 
        name = ievent.args[0]
144
 
    fbot = fleet.byname(name)
145
 
    if not fbot:
146
 
        ievent.reply('fleet not found')
147
 
        return
148
 
    if not nsauth.has(fbot):
149
 
        ievent.reply('nickserv not configured on %s' % fbot.name)
150
 
        return
151
 
    nsauth.identify(fbot)
152
 
    ievent.reply('ok')
153
 
 
154
 
cmnds.add('ns-auth', handle_nsauth, 'OPER')
155
 
examples.add('ns-auth','ns-auth [<botname>]', '1) ns-auth 2) ns-auth test')
156
 
 
157
 
def handle_nslist(bot, ievent):
158
 
    all = dict(nsauth.listbots())
159
 
    rpl = []
160
 
    for bot in all.keys():
161
 
        rpl.append('%s: authenticating through %s' % (bot, all[bot]))
162
 
    rpl.sort()
163
 
    ievent.reply(' .. '.join(rpl))
164
 
 
165
 
cmnds.add('ns-list', handle_nslist, 'OPER')
166
 
examples.add('ns-list', 'list all nickserv entries', 'ns-list')