~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
12
from canonical.config import config
14027.3.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
13
from lp.services.scripts.base import LaunchpadScript
13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
14
from lp.services.mail.incoming import (
15
    handle_one_mail)
16
from lp.services.mail.helpers import (
17
    save_mail_to_librarian,
18
    )
14027.3.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
19
from lp.services.mail.signedmessage import signed_message_from_string
20
13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
21
22
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.
23
    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).
24
13932.1.4 by mbp at canonical
process-one-mail can optionally take a filename argument, so you can use stdin for pdb.
25
    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).
26
13932.1.6 by mbp at canonical
process-one-mail arranges for sent mail to be printed to stdout.
27
    Any mail generated in response is printed to stdout.
28
13932.1.1 by Martin Pool
Accept as trusted mail signed by the Sender domain not the From address (bug 643223).
29
    """ + __doc__
30
31
    def main(self):
32
        self.txn.begin()
33
        # NB: This somewhat duplicates handleMail, but there it's mixed in
34
        # 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.
35
        if len(self.args) >= 1:
36
            from_file = file(self.args[0], 'rb')
37
        else:
38
            from_file = sys.stdin
39
        self.logger.debug("reading message from %r" % (from_file,))
40
        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).
41
        self.logger.debug("got %d bytes" % len(raw_mail))
42
        file_alias = save_mail_to_librarian(raw_mail)
43
        self.logger.debug("saved to librarian as %r" % (file_alias,))
44
        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.
45
        # Kinda kludgey way to cause sendmail to just print it.
46
        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).
47
        handle_one_mail(
48
            self.logger, parsed_mail,
49
            file_alias, file_alias.http_url,
50
            signature_timestamp_checker=None)
51
        self.logger.debug("mail handling complete")
52
        self.txn.commit()
53
54
55
if __name__ == '__main__':
56
    script = ProcessMail('process-one-mail', dbuser=config.processmail.dbuser)
57
    # No need to lock; you can run as many as you want as they use no global
58
    # resources (like a mailbox).
59
    script.run(use_web_security=True)