]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/mailer.py
f1459faf5c50f4e3cf377deda135e9e08d9d8f85
[tld-builder.git] / TLD_Builder / mailer.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import time
4 import os
5 import sys
6 import StringIO
7
8 from config import config
9 import util
10 import log
11
12 def recode(s):
13     if s.__class__ == ''.__class__:
14         return s.decode('iso-8859-1', 'replace').encode('us-ascii', 'replace')
15     else:
16         return s.encode('us-ascii', 'replace')
17
18 class Message:
19     def __init__(self):
20         self.headers = {}
21         self.body = StringIO.StringIO()
22         self.set_std_headers()
23
24     def set_header(self, n, v):
25         self.headers[n] = v
26
27     def set_headers(self, to = None, cc = None, subject = None):
28         if to != None:
29             self.set_header("To", to)
30         if cc != None:
31             self.set_header("Cc", cc)
32         if subject != None:
33             self.set_header("Subject", subject)
34
35     def remove_header(self, n):
36         if n in self.headers:
37             del self.headers[n]
38
39     def write_line(self, l):
40         self.body.write(recode("%s\n" % l))
41
42     def write(self, s):
43         self.body.write(recode(s))
44
45     def append_log(self, log):
46         s = os.stat(log)
47         if s.st_size > 50000:
48             # just head and tail
49             f = open(log)
50             line_cnt = 0
51             for l in f.xreadlines():
52                 line_cnt += 1
53             f.seek(0)
54             line = 0
55             for l in f.xreadlines():
56                 if line < 100 or line > line_cnt - 100:
57                     self.body.write(recode(l))
58                 if line == line_cnt - 100:
59                     self.body.write("\n\n[...]\n\n")
60                 line += 1
61         else:
62             util.sendfile(open(log), self.body)
63
64     def set_std_headers(self):
65         self.headers["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
66         self.headers["Message-ID"] = "<tld-builder.%f.%d@%s>" \
67                 % (time.time(), os.getpid(), os.uname()[1])
68         self.headers["From"] = "TLD %s builder <%s>" \
69                 % (config.builder, config.email)
70         self.headers["X-TLD-Builder"] = config.builder
71
72     def write_to(self, f):
73         for k, v in self.headers.items():
74             f.write("%s: %s\n" % (k, v))
75         f.write("\n")
76         self.body.seek(0)
77         util.sendfile(self.body, f)
78
79     def send(self):
80         if not os.path.exists("/usr/lib/sendmail"):
81             # TODO: dump to file?
82             log.alert("/usr/lib/sendmail doesn't exist: Can't send email")
83             return False
84         send_sendmail = "/usr/lib/sendmail -i -t -f %s" % config.admin_email
85         f = os.popen(send_sendmail, "w")
86         try:
87             self.write_to(f)
88         except IOError, e:
89             log.alert("sending email message failed: %s" % e)
90             f.close()
91             return False
92         return f.close()