~launchpad-pqm/launchpad/devel

13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
1
#!/usr/bin/python -S
2
#
3
# Copyright 2011 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
6
"""Process one email message, read from stdin."""
7
8
import _pythonpath
9
10
import sys
11
14605.1.1 by Curtis Hovey
Moved canonical.config to lp.services.
12
from lp.services.config import config
14612.2.7 by William Grant
scripts
13
from lp.services.mail.helpers import save_mail_to_librarian
14
from lp.services.mail.incoming import handle_one_mail
15
from lp.services.mail.signedmessage import signed_message_from_string
14027.3.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
16
from lp.services.scripts.base import LaunchpadScript
17
13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
18
19
class ProcessMail(LaunchpadScript):
13932.1.4 by mbp at canonical
process-one-mail can optionally take a filename argument, so you can use stdin for pdb.
20
    usage = """%prog [options] [MAIL_FILE]
13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
21
13932.1.4 by mbp at canonical
process-one-mail can optionally take a filename argument, so you can use stdin for pdb.
22
    Process one incoming email, read from the specified file or from stdin.
13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
23
13932.1.6 by mbp at canonical
process-one-mail arranges for sent mail to be printed to stdout.
24
    Any mail generated in response is printed to stdout.
25
13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
26
    """ + __doc__
27
28
    def main(self):
29
        self.txn.begin()
30
        # NB: This somewhat duplicates handleMail, but there it's mixed in
31
        # with handling a mailbox, which we're avoiding here.
13932.1.4 by mbp at canonical
process-one-mail can optionally take a filename argument, so you can use stdin for pdb.
32
        if len(self.args) >= 1:
33
            from_file = file(self.args[0], 'rb')
34
        else:
35
            from_file = sys.stdin
36
        self.logger.debug("reading message from %r" % (from_file,))
37
        raw_mail = from_file.read()
13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
38
        self.logger.debug("got %d bytes" % len(raw_mail))
39
        file_alias = save_mail_to_librarian(raw_mail)
40
        self.logger.debug("saved to librarian as %r" % (file_alias,))
41
        parsed_mail = signed_message_from_string(raw_mail)
13932.1.6 by mbp at canonical
process-one-mail arranges for sent mail to be printed to stdout.
42
        # Kinda kludgey way to cause sendmail to just print it.
43
        config.sendmail_to_stdout = True
13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
44
        handle_one_mail(
45
            self.logger, parsed_mail,
46
            file_alias, file_alias.http_url,
47
            signature_timestamp_checker=None)
48
        self.logger.debug("mail handling complete")
49
        self.txn.commit()
50
51
52
if __name__ == '__main__':
53
    script = ProcessMail('process-one-mail', dbuser=config.processmail.dbuser)
54
    # No need to lock; you can run as many as you want as they use no global
55
    # resources (like a mailbox).
56
    script.run(use_web_security=True)