]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blame - modules/mailer.py
- initial revision taken from pld-builder.new
[projects/pld-ftp-admin.git] / modules / mailer.py
CommitLineData
972c18d7
MM
1# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3import time
4import os
5import sys
6import StringIO
7
8from config import config
9import util
10
11class Message:
12 def __init__(self):
13 self.headers = {}
14 self.body = StringIO.StringIO()
15 self.set_std_headers()
16
17 def set_header(self, n, v):
18 self.headers[n] = v
19
20 def set_headers(self, to = None, cc = None, subject = None):
21 if to != None:
22 self.set_header("To", to)
23 if cc != None:
24 self.set_header("Cc", cc)
25 if subject != None:
26 self.set_header("Subject", subject)
27
28 def write_line(self, l):
29 self.body.write("%s\n" % l)
30
31 def write(self, s):
32 self.body.write(s)
33
34 def append_log(self, log):
35 s = os.stat(log)
36 if s.st_size > 50000:
37 # just head and tail
38 f = open(log)
39 line_cnt = 0
40 for l in f.xreadlines():
41 line_cnt += 1
42 f.seek(0)
43 line = 0
44 for l in f.xreadlines():
45 if line < 100 or line > line_cnt - 100:
46 self.body.write(l)
47 if line == line_cnt - 100:
48 self.body.write("\n\n[...]\n\n")
49 line += 1
50 else:
51 util.sendfile(open(log), self.body)
52
53 def set_std_headers(self):
54 self.headers["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
55 self.headers["Message-ID"] = "<pld-builder.%f.%d@%s>" \
56 % (time.time(), os.getpid(), os.uname()[1])
57 self.headers["From"] = "PLD %s builder <%s>" \
58 % (config.builder, config.email)
59 self.headers["X-PLD-Builder"] = config.builder
60
61 def write_to(self, f):
62 for k, v in self.headers.items():
63 f.write("%s: %s\n" % (k, v))
64 f.write("\n")
65 self.body.seek(0)
66 util.sendfile(self.body, f)
67
68 def send(self):
69 send_sendmail = "/usr/sbin/sendmail -t -f %s" % config.admin_email
70 f = os.popen(send_sendmail, "w")
71 self.write_to(f)
72 f.close()
This page took 0.0422 seconds and 4 git commands to generate.