~launchpad-pqm/launchpad/devel

8687.15.34 by Karl Fogel
Add license header blocks to .py, .zcml, and .pt files that don't have it
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
8697.22.1 by Barry Warsaw
Convert to using lazr.smtptest. Move and re-enable a few mailman tests we'd
3
4
"""SMTP test helper."""
5
6
7
__metaclass__ = type
8
__all__ = [
9
    'SMTPController',
10
    ]
11
12
9480.2.1 by Bjorn Tillenius
Set FD_CLOEXEC on the port the SMTP server uses, so that libuuid won't end up listening to the port.
13
import fcntl
14
8697.22.1 by Barry Warsaw
Convert to using lazr.smtptest. Move and re-enable a few mailman tests we'd
15
import logging
16
import Queue as queue
17
8697.22.4 by Barry Warsaw
Use lazr.smtptest 1.1
18
from lazr.smtptest.controller import QueueController
8697.22.1 by Barry Warsaw
Convert to using lazr.smtptest. Move and re-enable a few mailman tests we'd
19
from lazr.smtptest.server import QueueServer
20
21
22
log = logging.getLogger('lazr.smtptest')
23
24
25
class SMTPServer(QueueServer):
26
    """SMTP server which knows about Launchpad test specifics."""
27
28
    def handle_message(self, message):
29
        """See `QueueServer.handle_message()`."""
30
        message_id = message.get('message-id', 'n/a')
31
        log.debug('msgid: %s, to: %s, beenthere: %s, from: %s, rcpt: %s',
32
                  message_id, message['to'],
33
                  message['x-beenthere'],
34
                  message['x-mailfrom'], message['x-rcptto'])
8697.22.3 by Barry Warsaw
Fix email message sending by fixing a typo, and placing the log file in the
35
        from Mailman.Utils import list_names
36
        listnames = list_names()
8697.22.1 by Barry Warsaw
Convert to using lazr.smtptest. Move and re-enable a few mailman tests we'd
37
        try:
8697.22.3 by Barry Warsaw
Fix email message sending by fixing a typo, and placing the log file in the
38
            local, hostname = message['to'].split('@', 1)
39
            log.debug('local: %s, hostname: %s, listnames: %s',
40
                      local, hostname, listnames)
8697.22.1 by Barry Warsaw
Convert to using lazr.smtptest. Move and re-enable a few mailman tests we'd
41
        except ValueError:
42
            # There was no '@' sign in the email message, so ignore it.
43
            log.debug('Bad To header: %s', message.get('to', 'n/a'))
44
            return
45
        # If the message came from Mailman, place it onto the queue.  If the
46
        # local part indicates that the message is destined for a Mailman
47
        # mailing list, deliver it to Mailman's incoming queue.
48
        # pylint: disable-msg=F0401
8697.22.13 by Barry Warsaw
Respond to reviewer comments.
49
        if local in listnames and 'x-beenthere' not in message:
8697.22.1 by Barry Warsaw
Convert to using lazr.smtptest. Move and re-enable a few mailman tests we'd
50
            # It's destined for a mailing list.
51
            log.debug('delivered to Mailman: %s', message_id)
52
            from Mailman.Post import inject
53
            inject(local, message)
54
        else:
8697.22.13 by Barry Warsaw
Respond to reviewer comments.
55
            # It came from Mailman and goes in the queue, or it's destined for
56
            # a 'normal' user.  Either way, it goes in the queue.
8697.22.6 by Barry Warsaw
Make postings.txt work by changing slightly the smptd output. Also the
57
            log.debug('delivered to upstream: %s', message_id)
8697.22.1 by Barry Warsaw
Convert to using lazr.smtptest. Move and re-enable a few mailman tests we'd
58
            self.queue.put(message)
59
60
    def reset(self):
61
        # Base class is old-style.
62
        QueueServer.reset(self)
63
        # Consume everything out of the queue.
64
        while True:
65
            try:
66
                self.queue.get_nowait()
67
            except queue.Empty:
68
                break
69
70
8697.22.4 by Barry Warsaw
Use lazr.smtptest 1.1
71
class SMTPController(QueueController):
8697.22.1 by Barry Warsaw
Convert to using lazr.smtptest. Move and re-enable a few mailman tests we'd
72
    """A controller for the `SMTPServer`."""
73
    
8697.22.4 by Barry Warsaw
Use lazr.smtptest 1.1
74
    def _make_server(self, host, port):
75
        """See `QueueController`."""
8697.22.1 by Barry Warsaw
Convert to using lazr.smtptest. Move and re-enable a few mailman tests we'd
76
        self.server = SMTPServer(host, port, self.queue)
9480.2.1 by Bjorn Tillenius
Set FD_CLOEXEC on the port the SMTP server uses, so that libuuid won't end up listening to the port.
77
        # Set FD_CLOEXEC on the port's file descriptor, so that forked
78
        # processes like uuidd won't steal the port.
79
        flags = fcntl.fcntl(self.server._fileno, fcntl.F_GETFD)
80
        flags |= fcntl.FD_CLOEXEC
81
        fcntl.fcntl(self.server._fileno, fcntl.F_SETFD, flags)