commit 3ae9de3621fb2cc158482dcf0c3d5a4d37073a86 Author: Isis Lovecruft isis@torproject.org Date: Mon Jun 2 21:05:29 2014 +0000
Refactor b.e.server.MailFactory.
* CHANGE b.e.server.MailFactory into two separate classes, b.e.server.SMTPIncomingDeliveryFactory and b.e.server.SMTPIncomingServerFactory. This will enable us to be able to keep state for multiple emails coming in over a single connection, if necessary. * CHANGE b.e.server.addServer to use the new classes. --- lib/bridgedb/email/server.py | 81 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 14 deletions(-)
diff --git a/lib/bridgedb/email/server.py b/lib/bridgedb/email/server.py index e575eab..6bc7300 100644 --- a/lib/bridgedb/email/server.py +++ b/lib/bridgedb/email/server.py @@ -333,23 +333,75 @@ class SMTPIncomingDelivery(smtp.SMTP): return lambda: SMTPMessage(self.context, self.fromCanonicalSMTP)
-class MailFactory(smtp.SMTPFactory): - """Plugs into Twisted Mail; creates a new MailDelivery whenever we get - a connection on the SMTP port.""" +class SMTPIncomingDeliveryFactory(object): + """Factory for :class:`SMTPIncomingDelivery`s. + + This class is used to distinguish between different messages delivered + over the same connection. This can be used to optimize delivery of a + single message to multiple recipients, something which cannot be done by + :api:`IMessageDelivery <twisted.mail.smtp.IMessageDelivery>` implementors + due to their lack of information. + + :ivar context: A :class:`MailServerContext` for storing configuration settings. + :ivar delivery: A :class:`SMTPIncomingDelivery` to deliver incoming + SMTP messages to. + """ + implements(smtp.IMessageDeliveryFactory) + + context = None + delivery = SMTPIncomingDelivery
- def __init__(self, context=None, **kw): - smtp.SMTPFactory.__init__(self, **kw) - self.delivery = MailDelivery() - if context: - self.setBridgeDBContext(context) + def __init__(self): + logging.debug("%s created." % self.__class__.__name__)
- def setBridgeDBContext(self, context): - self.context = context - self.delivery.setBridgeDBContext(context) + @classmethod + def setContext(cls, context): + """Set our :ivar:`context` and the context for our :ivar:`delivery`.""" + cls.context = context + cls.delivery.setContext(cls.context) + + def getMessageDelivery(self): + """Get a new :class:`SMTPIncomingDelivery` instance.""" + return self.delivery() + + +class SMTPIncomingServerFactory(smtp.SMTPFactory): + """Plugs into :api:`twisted.mail.smtp.SMTPFactory`; creates a new + :class:`SMTPMessageDelivery`, which handles response email automation, + whenever we get a incoming connection on the SMTP port. + + .. warning:: My :ivar:`context` isn't an OpenSSL context, as is used for + the :api:`twisted.mail.smtp.ESMTPSender` + + :ivar context: A :class:`MailServerContext` for storing configuration settings. + :ivar deliveryFactory: A :class:`SMTPIncomingDeliveryFactory` for + producing :class:`SMTPIncomingDelivery`s. + :ivar domain: :api:`Our FQDN <twisted.mail.smtp.DNSNAME>`. + :ivar int timeout: The number of seconds to wait, after the last chunk of + data was received, before raising a + :api:`SMTPTimeoutError <twisted.mail.smtp.SMTPTimeoutError>` for an + incoming connection. + :ivar protocol: :api:`SMTP <twisted.mail.smtp.SMTP>` + """ + + context = None + deliveryFactory = SMTPIncomingDeliveryFactory + + def __init__(self, **kwargs): + smtp.SMTPFactory.__init__(self, **kwargs) + self.deliveryFactory = self.deliveryFactory() + + @classmethod + def setContext(cls, context): + """Set :ivar:`context` and :ivar:`deliveryFactory`.context.""" + cls.context = context + cls.deliveryFactory.setContext(cls.context)
def buildProtocol(self, addr): p = smtp.SMTPFactory.buildProtocol(self, addr) - p.delivery = self.delivery + self.deliveryFactory.transport = p.transport # XXX is this set yet? + p.factory = self + p.deliveryFactory = self.deliveryFactory return p
@@ -364,8 +416,9 @@ def addServer(config, distributor, schedule): :type schedule: :class:`bridgedb.schedule.ScheduledInterval` :param schedule: The schedule. XXX: Is this even used? """ - context = MailContext(config, distributor, schedule) - factory = MailFactory(context) + context = MailServerContext(config, distributor, schedule) + factory = SMTPIncomingServerFactory() + factory.setContext(context)
addr = config.EMAIL_BIND_IP or "" port = config.EMAIL_PORT
tor-commits@lists.torproject.org