o b&0@sdZddlmZddlmZddlmZmZddlm Z ddl m Z m Z ddl mZmZmZddlmZeejGd d d ZGd d d eZGd ddeZGdddejZGdddeZGdddejZGddde jZdS)z Mail protocol support. ) implementer) longversion)CramMD5CredentialsUsernamePassword)UnauthorizedLogin)deferprotocol)pop3relaysmtp)logc@sHeZdZUdZdZdZeed<ej fddZ ddZ d d Z d d Z dS) DomainDeliveryBaseaN A base class for message delivery using the domains of a mail service. @ivar service: See L{__init__} @ivar user: See L{__init__} @ivar host: See L{__init__} @type protocolName: L{bytes} @ivar protocolName: The protocol being used to deliver the mail. Sub-classes should set this appropriately. Nsnot-implemented-protocol protocolNamecCs||_||_||_dS)z @type service: L{MailService} @param service: A mail service. @type user: L{bytes} or L{None} @param user: The authenticated SMTP user. @type host: L{bytes} @param host: The hostname. N)serviceuserhost)selfrrrr8/usr/lib/python3/dist-packages/twisted/mail/protocols.py__init__%s  zDomainDeliveryBase.__init__c Csd}}|jrd|jd}|drd|d}d|dd|dd ||}d |jd |jd td d}ddtt|dt }d|d|d|S)a Generate a received header string for a message. @type helo: 2-L{tuple} of (L{bytes}, L{bytes}) @param helo: The client's identity as sent in the HELO command and its IP address. @type origin: L{Address} @param origin: The origination address of the message. @type recipients: L{list} of L{User} @param recipients: The destination addresses for the message. @rtype: L{bytes} @return: A received header string. s auth=xtextrs helo=sfrom s ([]sby s with s (ascii)sfor < s> s Received: s ) rencoderrrjoinmapbytesr rfc822date) rheloorigin recipientsauthStrheloStrfromUserbyforUserrrrreceivedHeader4s. $ z!DomainDeliveryBase.receivedHeadercCsX|jr|jjr|jj|jjd}|durt|jd}n|jj|jj}t |j |S)a9 Validate the address for which a message is destined. @type user: L{User} @param user: The destination address. @rtype: L{Deferred } which successfully fires with no-argument callable which returns L{IMessage } provider. @return: A deferred which successfully fires with a no-argument callable which returns a message receiver for the destination. @raise SMTPBadRcpt: When messages cannot be accepted for the destination address. NT) rrqueuedomainsgetdestdomainr DomainQueuerr maybeDeferredexists)rrdrrr validateToYszDomainDeliveryBase.validateTocCs8|s t|dd|jdkr|jdkrt|dd|S)a Validate the address from which a message originates. @type helo: 2-L{tuple} of (L{bytes}, L{bytes}) @param helo: The client's identity as sent in the HELO command and its IP address. @type origin: L{Address} @param origin: The origination address of the message. @rtype: L{Address} @return: The origination address. @raise SMTPBadSender: When messages cannot be accepted from the origination address. izWho are you? Say HELO first.riz#Sender address must contain domain.)r SMTPBadSenderlocalr/)rr"r#rrr validateFromrs zDomainDeliveryBase.validateFrom)__name__ __module__ __qualname____doc__rrr __annotations__r DNSNAMErr*r4r7rrrrr s  % r c@eZdZdZdZdS)SMTPDomainDeliveryzA A domain delivery base class for use in an SMTP server. ssmtpNr8r9r:r;rrrrrr?r?c@r>)ESMTPDomainDeliveryzB A domain delivery base class for use in an ESMTP server. sesmtpNr@rrrrrBrArBc@s,eZdZdZejZdZdddZddZ dS) SMTPFactorya; An SMTP server protocol factory. @ivar service: See L{__init__} @ivar portal: See L{__init__} @type protocol: no-argument callable which returns a L{Protocol } subclass @ivar protocol: A callable which creates a protocol. The default value is L{SMTP}. NcCstj|||_||_dS)z @type service: L{MailService} @param service: An email service. @type portal: L{Portal } or L{None} @param portal: A portal to use for authentication. N)r rCrrportal)rrrDrrrrs  zSMTPFactory.__init__cCs2td|tj||}|j|_|j|_|S)a Create an instance of an SMTP server protocol. @type addr: L{IAddress } provider @param addr: The address of the SMTP client. @rtype: L{SMTP} @return: An SMTP protocol. zConnection from )r msgr rC buildProtocolrrDraddrprrrrFs  zSMTPFactory.buildProtocol)N) r8r9r:r;r SMTPrrDrrFrrrrrCs    rCc@s*eZdZdZejZdZddZddZ dS) ESMTPFactorya An ESMTP server protocol factory. @type protocol: no-argument callable which returns a L{Protocol } subclass @ivar protocol: A callable which creates a protocol. The default value is L{ESMTP}. @type context: L{IOpenSSLContextFactory } or L{None} @ivar context: A factory to generate contexts to be used in negotiating encrypted communication. @type challengers: L{dict} mapping L{bytes} to no-argument callable which returns L{ICredentials } subclass provider. @ivar challengers: A mapping of acceptable authorization mechanism to callable which creates credentials to use for authentication. NcGs tj|g|Rdti|_dS)zk @param args: Arguments for L{SMTPFactory.__init__} @see: L{SMTPFactory.__init__} sCRAM-MD5N)rCrr challengers)rargsrrrrszESMTPFactory.__init__cCs t||}|j|_|j|_|S)a Create an instance of an ESMTP server protocol. @type addr: L{IAddress } provider @param addr: The address of the ESMTP client. @rtype: L{ESMTP} @return: An ESMTP protocol. )rCrFrLcontextctxrGrrrrFs zESMTPFactory.buildProtocol) r8r9r:r;r ESMTPrrNrrFrrrrrKs  rKc@s0eZdZdZdZdZddZddZdd ZdS) VirtualPOP3a[ A virtual hosting POP3 server. @type service: L{MailService} @ivar service: The email service that created this server. This must be set by the service. @type domainSpecifier: L{bytes} @ivar domainSpecifier: The character to use to split an email address into local-part and domain. The default is '@'. N@cCsZ||\}}z|j|}WntyttYSw|t |j ||dtj S)a1 Perform APOP authentication. Override the default lookup scheme to allow virtual domains. @type user: L{bytes} @param user: The name of the user attempting to log in. @type digest: L{bytes} @param digest: The challenge response. @rtype: L{Deferred} which successfully results in 3-L{tuple} of (L{IMailbox }, L{IMailbox } provider, no-argument callable) @return: A deferred which fires when authentication is complete. If successful, it returns an L{IMailbox } interface, a mailbox and a logout function. If authentication fails, the deferred fails with an L{UnauthorizedLogin } error. N) lookupDomainr lookupPortalKeyErrorrfailrloginr APOPCredentialsmagicIMailbox)rrdigestr/rDrrrauthenticateUserAPOP s z VirtualPOP3.authenticateUserAPOPcCsT||\}}z|j|}WntyttYSw|t||dt j S)aY Perform authentication for a username/password login. Override the default lookup scheme to allow virtual domains. @type user: L{bytes} @param user: The name of the user attempting to log in. @type password: L{bytes} @param password: The password to authenticate with. @rtype: L{Deferred} which successfully results in 3-L{tuple} of (L{IMailbox }, L{IMailbox } provider, no-argument callable) @return: A deferred which fires when authentication is complete. If successful, it returns an L{IMailbox } interface, a mailbox and a logout function. If authentication fails, the deferred fails with an L{UnauthorizedLogin } error. N) rSrrTrUrrVrrWrr rZ)rrpasswordr/rDrrrauthenticateUserPASS+s z VirtualPOP3.authenticateUserPASScCsXz ||jd\}}Wn tyd}Ynw||jjvr(td|d||fS)a Check whether a domain is among the virtual domains supported by the mail service. @type user: L{bytes} @param user: An email address. @rtype: 2-L{tuple} of (L{bytes}, L{bytes}) @return: The local part and the domain part of the email address if the domain is supported. @raise POP3Error: When the domain is not supported by the mail service. rrzno such domain {}zutf-8) splitdomainSpecifier ValueErrorrr,r POP3Errorformatdecode)rrr/rrrrSHs  zVirtualPOP3.lookupDomain) r8r9r:r;rr`r\r^rSrrrrrQs  rQc@s(eZdZdZeZdZddZddZdS) POP3Factorya A POP3 server protocol factory. @ivar service: See L{__init__} @type protocol: no-argument callable which returns a L{Protocol } subclass @ivar protocol: A callable which creates a protocol. The default value is L{VirtualPOP3}. NcCs ||_dS)zY @type service: L{MailService} @param service: An email service. N)r)rrrrrrns zPOP3Factory.__init__cCstj||}|j|_|S)a Create an instance of a POP3 server protocol. @type addr: L{IAddress } provider @param addr: The address of the POP3 client. @rtype: L{POP3} @return: A POP3 protocol. )r ServerFactoryrFrrGrrrrFus zPOP3Factory.buildProtocol) r8r9r:r;rQrrrrFrrrrre_s   reN)r;zope.interfacertwisted.copyrightrtwisted.cred.credentialsrrtwisted.cred.errorrtwisted.internetrr twisted.mailr r r twisted.pythonr IMessageDeliveryr r?rBrCrKPOP3rQrfrerrrrs     u.1f