5
__copyright__ = 'this file is in the public domain'
6
__gendocfirst__ = ['mail-set', ]
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
17
plughelp.add('mail', 'mail related commands')
20
if not 'log' in plugins.plugdeny.data:
21
from gozerplugs.plugs.log import logs
25
class Mailservernotset(Exception):
27
""" exception to raise if mail server is not set """
30
return "config['mailserver'] is not set"
32
def domail(mailto, txt, fromm=None, mailserver=None, subject=None):
35
rlog(10, 'mail', 'no text to send')
40
fromaddr = config['mailfrom']
42
fromaddr = 'gozerbot@gozerbot.org'
44
mailserver = config['mailserver']
46
raise Mailservernotset
47
if type(txt) != types.ListType:
49
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
50
(fromaddr, mailto, subject))
53
server = smtplib.SMTP(mailserver)
54
server.sendmail(fromaddr, mailto, msg)
56
def handle_mailset(bot, ievent):
57
""" set email by sending a email with confirmation code """
61
ievent.missing('<emailadres>')
63
username = users.getname(ievent.userhost)
64
rendezvous[username] = (str(random.randint(0, 10000)), addr)
66
domail(addr, 'the code is %s' % str(rendezvous[username][0]), \
67
subject='gozerbot mail verification')
69
ievent.reply("can't send test email: %s" % str(ex))
71
ievent.reply('test email is sent .. what is de code ? .. use the \
75
del rendezvous[username]
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')
83
def handle_mailcode(bot, ievent):
88
ievent.missing('<nr>')
90
username = users.getname(ievent.userhost)
92
code = rendezvous[username][0]
94
ievent.reply('the code is not longer valid .. try the mail-set \
97
if nr == rendezvous[username][0]:
98
if users.setemail(username, rendezvous[username][1]):
99
ievent.reply('email set')
101
del rendezvous[username]
105
ievent.reply('failed to set email')
107
ievent.reply('reply is not ok .. try again')
109
cmnds.add('mail-code', handle_mailcode, ['MAIL', 'OPER'])
110
examples.add('mail-code', 'check mail confirmation code', 'mail-code 333')
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')
117
result = waitforqueue(ievent.inqueue, 60)
119
ievent.reply('no data to mail')
121
username = users.getname(ievent.userhost)
122
email = users.getemail(username)
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))
130
ievent.reply('%s lines sent' % len(result))
132
ievent.reply("can't get email of %s" % username)
134
cmnds.add('mail', handle_mail, ['MAIL', 'OPER'])
135
examples.add('mail', 'mail pipelined data to user giving the command', \
138
def handle_re(bot, ievent):
139
""" mail log since last spoken """
141
ievent.reply('log plugin is not enabled')
143
if ievent.channel not in logs.loglist:
144
ievent.reply('logging is not enabled in %s' % ievent.channel)
146
lastlist = logs.lastspokelist(ievent.channel, ievent.userhost, 100)
147
lasttime = time.time()
149
for i in lastlist[::-1]:
157
result = logs.fromtimewithbot(ievent.channel, gotcha)
159
username = users.getname(ievent.userhost)
160
email = users.getemail(username)
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))
176
ievent.reply('%s lines sent' % len(result))
179
ievent.reply("can't get email of %s" % username)
181
ievent.reply('no data found')
183
cmnds.add('re', handle_re, ['MAIL', 'OPER'])
184
examples.add('re', 'mail the log since last spoken word', 're')
186
def handle_mailtime(bot, ievent):
187
""" mail log since a given time """
189
ievent.reply('log plugin is not enabled')
191
if ievent.channel not in logs.loglist:
192
ievent.reply('logging is not enabled in %s' % ievent.channel)
194
fromtime = strtotime(ievent.rest)
196
ievent.reply("can't detect time")
198
result = logs.fromtimewithbot(ievent.channel, fromtime)
200
username = users.getname(ievent.userhost)
201
email = users.getemail(username)
210
txt = i[4][nr:].strip()
211
res.append("[%s] <%s> %s" % (hourmin(float(i[1])), i[2], \
213
domail(email, res, subject="log of %s" % ievent.channel)
214
except Exception, ex:
215
ievent.reply("can't send email: %s" % str(ex))
217
ievent.reply('%s lines sent' % len(result))
220
ievent.reply("can't get email of %s" % username)
222
ievent.reply('no data found')
224
cmnds.add('mail-time', handle_mailtime, ['MAIL', 'OPER'])
225
examples.add('mail-time', 'mail the log since given time', 'mail-time 12:00')