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