~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/mail.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
 
# plugs/mail.py
2
 
#
3
 
#
4
 
 
5
 
__copyright__ = 'this file is in the public domain'
6
 
__gendocfirst__ = ['mail-set', ]
7
 
 
8
 
from gozerbot.commands import cmnds
9
 
from gozerbot.examples import examples
10
 
from gozerbot.generic import waitforqueue, rlog, hourmin, strtotime
11
 
from gozerbot.users import users
12
 
from gozerbot.config import config
13
 
from gozerbot.plugins import plugins
14
 
from gozerbot.plughelp import plughelp
15
 
import smtplib, random, time, types
16
 
 
17
 
plughelp.add('mail', 'mail related commands')
18
 
 
19
 
logs = None
20
 
if not 'log' in plugins.plugdeny.data:
21
 
    from gozerplugs.plugs.log import logs
22
 
 
23
 
rendezvous = {}
24
 
 
25
 
class Mailservernotset(Exception):
26
 
 
27
 
    """ exception to raise if mail server is not set """
28
 
 
29
 
    def __str__(self):
30
 
        return "config['mailserver'] is not set"
31
 
 
32
 
def domail(mailto, txt, fromm=None, mailserver=None, subject=None):
33
 
    """ sent the mail """
34
 
    if not txt:
35
 
        rlog(10, 'mail', 'no text to send')
36
 
        return
37
 
    if fromm:
38
 
        fromaddr = fromm
39
 
    else:
40
 
        fromaddr = config['mailfrom']
41
 
        if not fromaddr:
42
 
            fromaddr = 'gozerbot@gozerbot.org'
43
 
    if not mailserver:
44
 
        mailserver = config['mailserver']
45
 
    if not mailserver:
46
 
        raise Mailservernotset
47
 
    if type(txt) != types.ListType:
48
 
        txt = [txt, ]
49
 
    msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
50
 
(fromaddr, mailto, subject))
51
 
    for i in txt:
52
 
        msg += "%s\r\n" % i 
53
 
    server = smtplib.SMTP(mailserver)
54
 
    server.sendmail(fromaddr, mailto, msg)
55
 
 
56
 
def handle_mailset(bot, ievent):
57
 
    """ set email by sending a email with confirmation code """
58
 
    try:
59
 
        addr = ievent.args[0]
60
 
    except IndexError:
61
 
        ievent.missing('<emailadres>')
62
 
        return
63
 
    username = users.getname(ievent.userhost)
64
 
    rendezvous[username] = (str(random.randint(0, 10000)), addr)
65
 
    try:
66
 
        domail(addr, 'the code is %s' % str(rendezvous[username][0]), \
67
 
subject='gozerbot mail verification')
68
 
    except Exception, ex:
69
 
        ievent.reply("can't send test email: %s" % str(ex))
70
 
        return
71
 
    ievent.reply('test email is sent .. what is de code ? .. use the \
72
 
mail-code command')
73
 
    time.sleep(120)
74
 
    try:
75
 
        del rendezvous[username]
76
 
    except KeyError:
77
 
        pass
78
 
 
79
 
cmnds.add('mail-set', handle_mailset, ['MAIL', 'OPER'])
80
 
examples.add('mail-set', 'set email of user giving the command .. send \
81
 
confirm mail', 'mail-set bart@gozerbot.org')
82
 
 
83
 
def handle_mailcode(bot, ievent):
84
 
    """ confirm code """
85
 
    try:
86
 
        nr = ievent.args[0]
87
 
    except IndexError:
88
 
        ievent.missing('<nr>')
89
 
        return
90
 
    username = users.getname(ievent.userhost)
91
 
    try:
92
 
        code = rendezvous[username][0]
93
 
    except KeyError:
94
 
        ievent.reply('the code is not longer valid .. try the mail-set \
95
 
command again')
96
 
        return
97
 
    if nr == rendezvous[username][0]:
98
 
        if users.setemail(username, rendezvous[username][1]):
99
 
            ievent.reply('email set')
100
 
            try:
101
 
                del rendezvous[username]
102
 
            except KeyError:
103
 
                pass
104
 
        else:
105
 
            ievent.reply('failed to set email')
106
 
    else:
107
 
        ievent.reply('reply is not ok .. try again')
108
 
 
109
 
cmnds.add('mail-code', handle_mailcode, ['MAIL', 'OPER'])
110
 
examples.add('mail-code', 'check mail confirmation code', 'mail-code 333')
111
 
 
112
 
def handle_mail(bot, ievent):
113
 
    """ mail result from pipeline to the user giving the command """
114
 
    if not ievent.inqueue:
115
 
        ievent.reply('use mail in a pipeline')
116
 
        return
117
 
    result = waitforqueue(ievent.inqueue, 60)
118
 
    if not result:
119
 
        ievent.reply('no data to mail')
120
 
        return
121
 
    username = users.getname(ievent.userhost)
122
 
    email = users.getemail(username)
123
 
    if email:
124
 
        try:
125
 
            sub = "output of %s" % ievent.origtxt
126
 
            domail(email, result, subject=sub)
127
 
        except Exception, ex:
128
 
            ievent.reply("can't send email: %s" % str(ex))
129
 
            return
130
 
        ievent.reply('%s lines sent' % len(result))
131
 
    else:
132
 
        ievent.reply("can't get email of %s" % username)
133
 
 
134
 
cmnds.add('mail', handle_mail, ['MAIL', 'OPER'])
135
 
examples.add('mail', 'mail pipelined data to user giving the command', \
136
 
'todo | mail')
137
 
 
138
 
def handle_re(bot, ievent):
139
 
    """ mail log since last spoken """
140
 
    if not logs:
141
 
        ievent.reply('log plugin is not enabled')
142
 
        return
143
 
    if ievent.channel not in logs.loglist:
144
 
        ievent.reply('logging is not enabled in %s' % ievent.channel)
145
 
        return
146
 
    lastlist = logs.lastspokelist(ievent.channel, ievent.userhost, 100)
147
 
    lasttime = time.time()
148
 
    gotcha = None
149
 
    for i in lastlist[::-1]:
150
 
        delta = lasttime - i
151
 
        if delta > 600:
152
 
            gotcha = i
153
 
            break
154
 
        else:
155
 
            lasttime = i
156
 
    if gotcha:
157
 
        result = logs.fromtimewithbot(ievent.channel, gotcha)
158
 
        if result:
159
 
            username = users.getname(ievent.userhost)
160
 
            email = users.getemail(username)
161
 
            if email:
162
 
                try:
163
 
                    res = []
164
 
                    for i in result:
165
 
                        if i[2] == 'bot':
166
 
                            txt = i[4]
167
 
                        else:
168
 
                            nr = i[4].find(' ')
169
 
                            txt = i[4][nr:].strip()
170
 
                        res.append("[%s] <%s> %s" % \
171
 
(hourmin(float(i[1])), i[2], txt))
172
 
                    domail(email, res, subject="log of %s" % ievent.channel)
173
 
                except Exception, ex:
174
 
                    ievent.reply("can't send email: %s" % str(ex))
175
 
                    return
176
 
                ievent.reply('%s lines sent' % len(result))
177
 
                return
178
 
            else:
179
 
                ievent.reply("can't get email of %s" % username)
180
 
                return
181
 
    ievent.reply('no data found')
182
 
    
183
 
cmnds.add('re', handle_re, ['MAIL', 'OPER'])
184
 
examples.add('re', 'mail the log since last spoken word', 're')
185
 
 
186
 
def handle_mailtime(bot, ievent):
187
 
    """ mail log since a given time """
188
 
    if not logs:
189
 
        ievent.reply('log plugin is not enabled')
190
 
        return
191
 
    if ievent.channel not in logs.loglist:
192
 
        ievent.reply('logging is not enabled in %s' % ievent.channel)
193
 
        return
194
 
    fromtime = strtotime(ievent.rest)
195
 
    if not fromtime:
196
 
        ievent.reply("can't detect time")
197
 
        return
198
 
    result = logs.fromtimewithbot(ievent.channel, fromtime)
199
 
    if result:
200
 
        username = users.getname(ievent.userhost)
201
 
        email = users.getemail(username)
202
 
        if email:
203
 
            try:
204
 
                res = []
205
 
                for i in result:
206
 
                    if i[2] == 'bot':
207
 
                        txt = i[4]
208
 
                    else:
209
 
                        nr = i[4].find(' ')
210
 
                        txt = i[4][nr:].strip()
211
 
                    res.append("[%s] <%s> %s" % (hourmin(float(i[1])), i[2], \
212
 
txt))
213
 
                domail(email, res, subject="log of %s" % ievent.channel)
214
 
            except Exception, ex:
215
 
                ievent.reply("can't send email: %s" % str(ex))
216
 
                return
217
 
            ievent.reply('%s lines sent' % len(result))
218
 
            return
219
 
        else:
220
 
            ievent.reply("can't get email of %s" % username)
221
 
            return
222
 
    ievent.reply('no data found')
223
 
    
224
 
cmnds.add('mail-time', handle_mailtime, ['MAIL', 'OPER'])
225
 
examples.add('mail-time', 'mail the log since given time', 'mail-time 12:00')