| 1 |
tbm |
133 |
#!/usr/bin/env python |
| 2 |
tbm |
5 |
|
| 3 |
tbm |
161 |
# Store an e-mail or a summary in the database; called by exim |
| 4 |
tbm |
1116 |
# Copyright (C) 2001, 2002, 2003, 2004, 2005 Martin Michlmayr <tbm@cyrius.com> |
| 5 |
myon |
1439 |
# Copyright (C) 2006 Christoph Berg <myon@debian.org> |
| 6 |
tbm |
21 |
# $Id$ |
| 7 |
tbm |
5 |
|
| 8 |
|
|
# This program is free software; you can redistribute it and/or modify |
| 9 |
|
|
# it under the terms of the GNU General Public License as published by |
| 10 |
|
|
# the Free Software Foundation; either version 2 of the License, or |
| 11 |
|
|
# (at your option) any later version. |
| 12 |
|
|
|
| 13 |
|
|
# This program is distributed in the hope that it will be useful, |
| 14 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
|
|
# GNU General Public License for more details. |
| 17 |
|
|
|
| 18 |
|
|
# You should have received a copy of the GNU General Public License |
| 19 |
|
|
# along with this program; if not, write to the Free Software |
| 20 |
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
|
|
|
| 22 |
|
|
|
| 23 |
tbm |
1117 |
import email, os, re, sys, time |
| 24 |
tbm |
673 |
import apt_pkg, utils, status |
| 25 |
tbm |
5 |
|
| 26 |
tbm |
675 |
re_summary = re.compile(r"MIA-Summary:\s*(.+)") |
| 27 |
|
|
re_id = re.compile(r"MIA-ID:\s*(\d+)") |
| 28 |
|
|
|
| 29 |
tbm |
230 |
apt_pkg.init() |
| 30 |
tbm |
246 |
Cnf = apt_pkg.newConfiguration() |
| 31 |
|
|
apt_pkg.ReadConfigFileISC(Cnf, status.config) |
| 32 |
tbm |
230 |
|
| 33 |
tbm |
1117 |
msg = email.message_from_string(sys.stdin.read()) |
| 34 |
tbm |
5 |
|
| 35 |
tbm |
845 |
# Check where the mail is being delivered to because we only want to accept |
| 36 |
|
|
# mail for mia-*@qa users. |
| 37 |
tbm |
1117 |
maint = msg.get("Delivered-To", "").split("@")[0] |
| 38 |
tbm |
845 |
if not maint: |
| 39 |
jeroen |
979 |
print >> sys.stderr, "No maintainer given, please use mia-<maint>@qa" |
| 40 |
tbm |
845 |
sys.exit(1) |
| 41 |
|
|
# mail has to be sent to mia-*, otherwise mia-record will not accept it |
| 42 |
|
|
if maint[:4] != "mia-": |
| 43 |
jeroen |
979 |
print >> sys.stderr, "No such user" |
| 44 |
tbm |
845 |
sys.exit(1) |
| 45 |
|
|
maint = maint[4:] |
| 46 |
jeroen |
979 |
# make sure to not allow shell stuff, /'s, etc! |
| 47 |
jeroen |
1078 |
if re.compile(r"[^a-z0-9=+._-]").search(maint, re.I): |
| 48 |
jeroen |
979 |
print >> sys.stderr, "Invalid characters found in maintainer, aborting" |
| 49 |
|
|
sys.exit(1) |
| 50 |
tbm |
845 |
|
| 51 |
tbm |
673 |
Subst = {} |
| 52 |
tbm |
674 |
Subst["__FROM__"] = "%s <%s@%s>" % (Cnf["MyAdminName"], Cnf["MyUser"], Cnf["MyHost"]) |
| 53 |
tbm |
673 |
Subst["__SUBJECT__"] = msg.get("Subject", "") |
| 54 |
|
|
Subst["__MESSAGE_ID__"] = msg.get("Message-ID", "") |
| 55 |
tbm |
1117 |
Subst["__REFERENCES__"] = "%s %s" % (msg.get("References", ""), msg.get("Message-ID", "")) |
| 56 |
tbm |
674 |
Subst["__MIA_USER__"] = Cnf["MyUser"] |
| 57 |
|
|
Subst["__MIA_DOMAIN__"] = Cnf["MyHost"] |
| 58 |
tbm |
673 |
Subst["__LOGIN__"] = maint |
| 59 |
tbm |
844 |
Subst["__EMAIL__"] = maint.replace("=", "@") |
| 60 |
tbm |
673 |
Subst["__REVISION__"] = "$Revision$" |
| 61 |
|
|
|
| 62 |
tbm |
1117 |
if "MIA-SUMMARY" in msg.get("Subject", ""): |
| 63 |
tbm |
675 |
summary = id = None |
| 64 |
tbm |
1123 |
for line in msg.as_string().split("\n"): |
| 65 |
tbm |
675 |
if not summary: |
| 66 |
|
|
summary = re_summary.search(line, re.I) |
| 67 |
|
|
if not id: |
| 68 |
|
|
id = re_id.search(line, re.I) |
| 69 |
tbm |
231 |
|
| 70 |
tbm |
675 |
if summary and id: |
| 71 |
myon |
1439 |
status.write_status(maint, id.group(1), summary.group(1), sender=msg.get("From", "")) |
| 72 |
tbm |
673 |
Subst["__SUMMARY__"] = summary.group(1) |
| 73 |
|
|
Subst["__ID__"] = str(id.group(1)) |
| 74 |
|
|
mail_message = utils.TemplateSubst(Subst, Cnf["Dir::Templates"]+"/mia-record.thanks") |
| 75 |
|
|
utils.send_mail(mail_message) |
| 76 |
tbm |
5 |
else: |
| 77 |
jeroen |
979 |
print >> sys.stderr, "not ok: no summary or ID" |
| 78 |
tbm |
5 |
sys.exit(1) |
| 79 |
|
|
else: |
| 80 |
|
|
try: |
| 81 |
tbm |
515 |
mbox = open(Cnf["Dir::Database"] + "/" + maint, "a") |
| 82 |
tbm |
412 |
except IOError, e: |
| 83 |
jeroen |
979 |
print >> sys.stderr, "Cannot open mbox file for %s to append: %s" % (maint, e) |
| 84 |
tbm |
5 |
sys.exit(1) |
| 85 |
tbm |
1124 |
mbox.write(msg.as_string(unixfrom=1)) |
| 86 |
tbm |
5 |
mbox.close() |
| 87 |
|
|
|
| 88 |
tbm |
1117 |
date_tz = email.Utils.parsedate_tz(msg["Date"]) |
| 89 |
jeroen |
1281 |
when = email.Utils.mktime_tz(date_tz) |
| 90 |
tbm |
5 |
|
| 91 |
jeroen |
1281 |
|
| 92 |
tbm |
673 |
summary = msg.get("X-MIA-Summary") |
| 93 |
|
|
if summary: |
| 94 |
myon |
1439 |
status.write_status(maint, when, summary, sender=msg.get("From", "")) |
| 95 |
tbm |
673 |
Subst["__SUMMARY__"] = summary |
| 96 |
tbm |
1121 |
mail_msg = utils.TemplateSubstMIMEMultipart(Subst, Cnf["Dir::Templates"]+"/mia-record.added") |
| 97 |
tbm |
673 |
else: |
| 98 |
|
|
Subst["__ID__"] = str(when) |
| 99 |
tbm |
677 |
Subst["__SUMMARIES__"] = "" |
| 100 |
|
|
for i in Cnf.SubTree("Summaries").List(): |
| 101 |
tbm |
1116 |
Subst["__SUMMARIES__"] += " %s: %s\n" % (i, ", ".join(Cnf.ValueList("Summaries::%s" % i))) |
| 102 |
tbm |
1121 |
mail_msg = utils.TemplateSubstMIMEMultipart(Subst, Cnf["Dir::Templates"]+"/mia-record.respond") |
| 103 |
|
|
# Attach the original message |
| 104 |
|
|
a = email.Message.Message() |
| 105 |
|
|
a["Content-Type"] = "message/rfc822" |
| 106 |
|
|
a.set_payload([msg]) |
| 107 |
|
|
mail_msg.attach(a) |
| 108 |
|
|
utils.send_mail(mail_msg.as_string()) |
| 109 |
tbm |
673 |
|
| 110 |
tbm |
516 |
# debian.org's exim has dumb default file permissions. Give group write perms |
| 111 |
tbm |
515 |
for file in os.listdir(Cnf["Dir::Database"]): |
| 112 |
tbm |
202 |
try: |
| 113 |
tbm |
515 |
os.chmod(Cnf["Dir::Database"] + "/" + file, 0664) |
| 114 |
tbm |
202 |
except OSError: |
| 115 |
|
|
pass |
| 116 |
tbm |
24 |
|
| 117 |
tbm |
454 |
|
| 118 |
tbm |
672 |
# vim: ts=4:expandtab:shiftwidth=4: |