]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blob - modules/mailer.py
31584b055e107b640fbaaa00ead392429b0bdce8
[projects/pld-ftp-admin.git] / modules / 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
11 class 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.050391 seconds and 2 git commands to generate.