~ubuntu-branches/ubuntu/precise/gozerbot/precise

« back to all changes in this revision

Viewing changes to build/lib/gozerplugs/plugs/mail.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2008-06-02 19:26:39 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080602192639-3rn65nx4q1sgd6sy
Tags: 0.8.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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