]> git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/messages.py
Drop dependency on textile
[people/jschlag/pbs.git] / src / buildservice / messages.py
1 #!/usr/bin/python
2
3 import logging
4 import smtplib
5 import subprocess
6 import tornado.locale
7
8 from email.mime.text import MIMEText
9
10 import base
11
12 class Messages(base.Object):
13 def add(self, to, subject, text, frm=None):
14 subject = "%s %s" % (self.pakfire.settings.get("email_subject_prefix"), subject)
15
16 # Get default sender from the settings.
17 if not frm:
18 frm = self.pakfire.settings.get("email_from")
19
20 self.db.execute("INSERT INTO user_messages(frm, `to`, subject, text)"
21 " VALUES(%s, %s, %s, %s)", frm, to, subject, text)
22
23 def get_all(self, limit=None):
24 query = "SELECT * FROM user_messages ORDER BY time_added ASC"
25 if limit:
26 query += " LIMIT %d" % limit
27
28 return self.db.query(query)
29
30 @property
31 def count(self):
32 ret = self.db.get("SELECT COUNT(*) as count FROM user_messages")
33
34 return ret.count
35
36 def delete(self, id):
37 self.db.execute("DELETE FROM user_messages WHERE id = %s", id)
38
39 def send_to_all(self, recipients, subject, body, format=None):
40 """
41 Sends an email to all recipients and does the translation.
42 """
43 if not format:
44 format = {}
45
46 for recipient in recipients:
47 if not recipient:
48 logging.warning("Ignoring empty recipient.")
49 continue
50
51 # We try to get more information about the user from the database
52 # like the locale.
53 user = self.pakfire.users.get_by_email(recipient)
54 if user:
55 # Get locale that the user prefers.
56 locale = tornado.locale.get(user.locale)
57 else:
58 # Get the default locale.
59 locale = tornado.locale.get()
60
61 # Translate the message.
62 _subject = locale.translate(subject) % format
63 _body = locale.translate(body) % format
64
65 # If we know the real name of the user we add the realname to
66 # the recipient field.
67 if user:
68 recipient = "%s <%s>" % (user.realname, user.email)
69
70 # Add the message to the queue that it is sent.
71 self.add(recipient, _subject, _body)
72
73 @staticmethod
74 def send_msg(msg):
75 if not msg.to:
76 logging.warning("Dropping message with empty recipient.")
77 return
78
79 logging.debug("Sending mail to %s: %s" % (msg.to, msg.subject))
80
81 # Preparing mail content.
82 mail = MIMEText(msg.text.encode("latin-1"))
83 mail["From"] = msg.frm.encode("latin-1")
84 mail["To"] = msg.to.encode("latin-1")
85 mail["Subject"] = msg.subject.encode("latin-1")
86 #mail["Content-type"] = "text/plain; charset=utf-8"
87
88 #smtp = smtplib.SMTP("localhost")
89 #smtp.sendmail(msg.frm, msg.to.split(", "), mail.as_string())
90 #smtp.quit()
91
92 # We use sendmail here to workaround problems with the mailserver
93 # communication.
94 # So, just call /usr/lib/sendmail, pipe the message in and see
95 # what sendmail tells us in return.
96 sendmail = ["/usr/lib/sendmail", "-t"]
97 p = subprocess.Popen(sendmail, bufsize=0, close_fds=True,
98 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
99
100 stdout, stderr = p.communicate(mail.as_string())
101
102 # Wait until sendmail has finished.
103 p.wait()
104
105 if p.returncode:
106 raise Exception, "Could not send mail: %s" % stderr