7
__copyright__ = 'this file is in the public domain'
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
18
plughelp.add('mail', 'mail related commands')
21
if not 'log' in plugins.plugdeny.data:
22
from gozerplugs.plugs.log import logs
26
class Mailservernotset(Exception):
28
""" exception to raise if mail server is not set """
31
return "config['mailserver'] is not set"
33
def domail(mailto, txt, fromm=None, mailserver=None, subject=None):
36
rlog(10, 'mail', 'no text to send')
41
fromaddr = config['mailfrom']
43
fromaddr = 'gozerbot@gozerbot.org'
45
mailserver = config['mailserver']
47
raise Mailservernotset
48
if type(txt) != types.ListType:
50
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
51
(fromaddr, mailto, subject))
54
server = smtplib.SMTP(mailserver)
55
server.sendmail(fromaddr, mailto, msg)
57
def handle_mailset(bot, ievent):
58
""" set email by sending a email with confirmation code """
62
ievent.missing('<emailadres>')
64
username = users.getname(ievent.userhost)
65
rendezvous[username] = (str(random.randint(0, 10000)), addr)
67
domail(addr, 'the code is %s' % str(rendezvous[username][0]), \
68
subject='gozerbot mail verification')
70
ievent.reply("can't send test email: %s" % str(ex))
72
ievent.reply('test email is sent .. what is de code ? .. use the \
76
del rendezvous[username]
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')
84
def handle_mailcode(bot, ievent):
89
ievent.missing('<nr>')
91
username = users.getname(ievent.userhost)
93
code = rendezvous[username][0]
95
ievent.reply('the code is not longer valid .. try the mail-set \
98
if nr == rendezvous[username][0]:
99
if users.setemail(username, rendezvous[username][1]):
100
ievent.reply('email set')
102
del rendezvous[username]
106
ievent.reply('failed to set email')
108
ievent.reply('reply is not ok .. try again')
110
cmnds.add('mail-code', handle_mailcode, 'MAIL')
111
examples.add('mail-code', 'check mail confirmation code', 'mail-code 333')
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')
118
result = waitforqueue(ievent.inqueue, 60)
120
ievent.reply('no data to mail')
122
username = users.getname(ievent.userhost)
123
email = users.getemail(username)
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))
131
ievent.reply('%s lines sent' % len(result))
133
ievent.reply("can't get email of %s" % username)
135
cmnds.add('mail', handle_mail, 'MAIL')
136
examples.add('mail', 'mail pipelined data to user giving the command', \
139
def handle_re(bot, ievent):
140
""" mail log since last spoken """
142
ievent.reply('log plugin is not enabled')
144
if ievent.channel not in logs.loglist:
145
ievent.reply('logging is not enabled in %s' % ievent.channel)
147
lastlist = logs.lastspokelist(ievent.channel, ievent.userhost, 100)
148
lasttime = time.time()
150
for i in lastlist[::-1]:
158
result = logs.fromtimewithbot(ievent.channel, gotcha)
160
username = users.getname(ievent.userhost)
161
email = users.getemail(username)
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))
177
ievent.reply('%s lines sent' % len(result))
180
ievent.reply("can't get email of %s" % username)
182
ievent.reply('no data found')
184
cmnds.add('re', handle_re, 'MAIL')
185
examples.add('re', 'mail the log since last spoken word', 're')
187
def handle_mailtime(bot, ievent):
188
""" mail log since a given time """
190
ievent.reply('log plugin is not enabled')
192
if ievent.channel not in logs.loglist:
193
ievent.reply('logging is not enabled in %s' % ievent.channel)
195
fromtime = strtotime(ievent.rest)
197
ievent.reply("can't detect time")
199
result = logs.fromtimewithbot(ievent.channel, fromtime)
201
username = users.getname(ievent.userhost)
202
email = users.getemail(username)
211
txt = i[4][nr:].strip()
212
res.append("[%s] <%s> %s" % (hourmin(float(i[1])), i[2], \
214
domail(email, res, subject="log of %s" % ievent.channel)
215
except Exception, ex:
216
ievent.reply("can't send email: %s" % str(ex))
218
ievent.reply('%s lines sent' % len(result))
221
ievent.reply("can't get email of %s" % username)
223
ievent.reply('no data found')
225
cmnds.add('mail-time', handle_mailtime, 'MAIL')
226
examples.add('mail-time', 'mail the log since given time', 'mail-time')