1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
6 if sys.version_info[0] == 2:
9 from io import StringIO
11 from config import config
16 if s.__class__ == ''.__class__:
17 return s.decode('iso-8859-1', 'replace').encode('us-ascii', 'replace')
19 return s.encode('us-ascii', 'replace')
24 self.body = StringIO.StringIO()
25 self.set_std_headers()
27 def set_header(self, n, v):
30 def set_headers(self, to = None, cc = None, subject = None):
32 self.set_header("To", to)
34 self.set_header("Cc", cc)
36 self.set_header("Subject", subject)
38 def remove_header(self, n):
42 def write_line(self, l):
43 self.body.write(recode("%s\n" % l))
46 self.body.write(recode(s))
48 def append_log(self, log):
59 if line < 100 or line > line_cnt - 100:
60 self.body.write(recode(l))
61 if line == line_cnt - 100:
62 self.body.write("\n\n[...]\n\n")
65 util.sendfile(open(log), self.body)
67 def set_std_headers(self):
68 self.headers["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
69 self.headers["Message-ID"] = "<tld-builder.%f.%d@%s>" \
70 % (time.time(), os.getpid(), os.uname()[1])
71 self.headers["From"] = "TLD %s builder <%s>" \
72 % (config.builder, config.email)
73 self.headers["X-TLD-Builder"] = config.builder
75 def write_to(self, f):
76 for k, v in self.headers.items():
77 f.write("%s: %s\n" % (k, v))
80 util.sendfile(self.body, f)
83 if not os.path.exists("/usr/lib/sendmail"):
85 log.alert("/usr/lib/sendmail doesn't exist: Can't send email")
87 send_sendmail = "/usr/lib/sendmail -i -t -f %s" % config.admin_email
88 f = os.popen(send_sendmail, "w")
92 log.alert("sending email message failed: %s" % e)