
    Vh@1                       d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZ ddlmZmZ dd	lmZmZmZ dd
lmZ erddlmZ  eej4                         G d d             Z G d de      Z G d de      Z G d dej<                        Z G d de      Z G d dej@                        Z! G d dejD                        Z#y)z
Mail protocol support.
    )annotations)TYPE_CHECKINGCallable)implementer)longversion)CramMD5CredentialsUsernamePassword)UnauthorizedLogin)deferprotocol)pop3relaysmtp)log)MailServicec                  T    e Zd ZU dZdZdZded<   ej                  fdZ	d Z
d Zd	 Zy)
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.
    Ns   not-implemented-protocolbytesprotocolNamec                .    || _         || _        || _        y)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)serviceuserhost)selfr   r   r   s       F/home/dcms/DCMS/lib/python3.12/site-packages/twisted/mail/protocols.py__init__zDomainDeliveryBase.__init__*   s     		    c                   dx}}| j                   rd| j                   j                  d      z   }|d   rd|d   z   }d|d   z   dz   |d   z   d	z   |z   |z   }d
| j                  z   dz   | j                  z   dz   t	        j                  d      z   dz   }ddj                  t        t        |            z   dz   t        j                         z   }d|z   dz   |z   dz   |z   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.
        r   s    auth=xtextr   s    helo=s   from s    ([      ]s   by s    with s    (ascii   )s   for <    s   > s
   Received: s   
	)
r   encoder   r   r   joinmapr   r   
rfc822date)	r   heloorigin
recipientsauthStrheloStrfromUserbyforUsers	            r   receivedHeaderz!DomainDeliveryBase.receivedHeader9   s   "  '99$))"2"27";;G7$q')Gd1g%.a84?'IGSii   	
   )*  	 tyyUJ!7885@4??CTT 	 x''1B6@7JJr   c                   | j                   rs| j                  j                  r]| j                  j                  j	                  |j
                  j                  d      }|Nt        j                  | j                  d      }n-| j                  j                  |j
                  j                     }t        j                  |j                  |      S )a9  
        Validate the address for which a message is destined.

        @type user: L{User}
        @param user: The destination address.

        @rtype: L{Deferred <defer.Deferred>} which successfully fires with
            no-argument callable which returns L{IMessage <smtp.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)r   r   queuedomainsgetdestdomainr   DomainQueuerr   maybeDeferredexists)r   r   ds      r   
validateTozDomainDeliveryBase.validateTo^   s    " 99++$$(()9)94@Ay&&t||T:$$TYY%5%56A""188T22r   c                    |st        j                  |dd      |j                  dk7  r&|j                  dk(  rt        j                  |dd      |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.
        i  zWho are you?  Say HELO first.r   i  z#Sender address must contain domain.)r   SMTPBadSenderlocalr7   )r   r)   r*   s      r   validateFromzDomainDeliveryBase.validateFromw   sO    " $$VS2QRR<<36==C#7$$VS2WXXr   )__name__
__module____qualname____doc__r   r   __annotations__r   DNSNAMEr   r1   r<   r@    r   r   r   r      s5    
 G5L%5+/<< #KJ32r   r   c                      e Zd ZdZdZy)SMTPDomainDeliveryzA
    A domain delivery base class for use in an SMTP server.
    s   smtpNrA   rB   rC   rD   r   rG   r   r   rI   rI      s     Lr   rI   c                      e Zd ZdZdZy)ESMTPDomainDeliveryzB
    A domain delivery base class for use in an ESMTP server.
    s   esmtpNrJ   rG   r   r   rL   rL      s     Lr   rL   c                  :    e Zd ZdZej
                  ZdZddZd Z	y)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
        <protocol.Protocol>} subclass
    @ivar protocol: A callable which creates a protocol.  The default value is
        L{SMTP}.
    Nc                ^    t         j                  j                  |        || _        || _        y)z
        @type service: L{MailService}
        @param service: An email service.

        @type portal: L{Portal <twisted.cred.portal.Portal>} or
            L{None}
        @param portal: A portal to use for authentication.
        N)r   rN   r   r   portal)r   r   rP   s      r   r   zSMTPFactory.__init__   s&     	!!$'r   c                    t        j                  d|        t        j                  j	                  | |      }| j
                  |_        | j                  |_        |S )a  
        Create an instance of an SMTP server protocol.

        @type addr: L{IAddress <twisted.internet.interfaces.IAddress>} provider
        @param addr: The address of the SMTP client.

        @rtype: L{SMTP}
        @return: An SMTP protocol.
        zConnection from )r   msgr   rN   buildProtocolr   rP   r   addrps      r   rS   zSMTPFactory.buildProtocol   sK     	"4&)***46LL	;;r   )N)
rA   rB   rC   rD   r   SMTPr   rP   r   rS   rG   r   r   rN   rN      s!    
 yyHFr   rN   c                  8    e Zd ZdZej
                  ZdZd Zd Z	y)ESMTPFactorya  
    An ESMTP server protocol factory.

    @type protocol: no-argument callable which returns a L{Protocol
        <protocol.Protocol>} subclass
    @ivar protocol: A callable which creates a protocol.  The default value is
        L{ESMTP}.

    @type context: L{IOpenSSLContextFactory
        <twisted.internet.interfaces.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 <twisted.cred.credentials.ICredentials>}
        subclass provider.
    @ivar challengers: A mapping of acceptable authorization mechanism to
        callable which creates credentials to use for authentication.
    Nc                J    t        j                  | g|  dt        i| _        y)zk
        @param args: Arguments for L{SMTPFactory.__init__}

        @see: L{SMTPFactory.__init__}
        s   CRAM-MD5N)rN   r   r   challengers)r   argss     r   r   zESMTPFactory.__init__   s%     	T)D)');<r   c                v    t         j                  | |      }| j                  |_        | j                  |_        |S )a  
        Create an instance of an ESMTP server protocol.

        @type addr: L{IAddress <twisted.internet.interfaces.IAddress>} provider
        @param addr: The address of the ESMTP client.

        @rtype: L{ESMTP}
        @return: An ESMTP protocol.
        )rN   rS   r[   contextctxrT   s      r   rS   zESMTPFactory.buildProtocol   s3     %%dD1((r   )
rA   rB   rC   rD   r   ESMTPr   r^   r   rS   rG   r   r   rY   rY      s!    ( zzHG=r   rY   c                  P    e Zd ZU dZdZded<   dZded<   d Z	 	 	 	 	 	 dd	Zdd
Z	y)VirtualPOP3a  
    A virtual hosting POP3 server.

    @ivar service: The email service that created this server.  This must be
        set by the service.

    @ivar domainSpecifier: The character to use to split an email address into
        local-part and domain. The default is '@'.
    NzMailService | Noner      @r   domainSpecifierc                :   | j                  |      \  }}	 | j                  j                  |      }|j                  t	        j
                  | j                  ||      dt        j                        S # t        $ r  t        j                  t                     cY S w xY w)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 <pop3.IMailbox>}, L{IMailbox <pop3.IMailbox>}
            provider, no-argument callable)
        @return: A deferred which fires when authentication is complete.
            If successful, it returns an L{IMailbox <pop3.IMailbox>} interface,
            a mailbox and a logout function. If authentication fails, the
            deferred fails with an L{UnauthorizedLogin
            <twisted.cred.error.UnauthorizedLogin>} error.
        N)lookupDomainr   lookupPortalloginr   APOPCredentialsmagicIMailboxKeyErrorr   failr
   )r   r   digestr7   rP   s        r   authenticateUserAPOPz VirtualPOP3.authenticateUserAPOP  s    * ((.f	\\..v6F <<$$TZZv>dmm   	3::/122	3s   A1 1&BBc                :   | j                  |      \  }}| j                  J d       	 | j                  j                  |      }|j                  t	        ||      dt
        j                        }|S # t        $ r  t        j                  t                     cY S w xY w)aa  
        Perform authentication for a username/password login.

        Override the default lookup scheme to allow virtual domains.

        @param user: The name of the user attempting to log in.

        @param password: The password to authenticate with.

        @return: A deferred which fires when authentication is complete.
            If successful, it returns an L{IMailbox <pop3.IMailbox>} interface,
            a mailbox and a logout function. If authentication fails, the
            deferred fails with an L{UnauthorizedLogin
            <twisted.cred.error.UnauthorizedLogin>} error.
        Nz.must have a service to be able to authenticate)rf   r   rg   rh   r	   r   rk   rl   r   rm   r
   )r   r   passwordr7   rP   results         r   authenticateUserPASSz VirtualPOP3.authenticateUserPASS.  s    $ ((.fLL$	<;	<$	\\..v6F -dH=tT]]S  M  	3::/122	3s   A1 1&BBc                (   	 |j                  | j                  d      \  }}| j                  J d       || j                  j                  vr3t        j                  dj                  |j                  d                  ||fS # t        $ r d}Y ow xY w)a{  
        Check whether a domain part of the given email address is among the
        virtual domains supported by the mail service.

        @param user: An email address.

        @return: a 2-tuple of (local part, domain part) of the email address if
            the domain is supported.

        @raise POP3Error: When the domain is not supported by the mail service.
        r    r   z(cannot look up domain if service not setzno such domain {}zutf-8)	splitrd   
ValueErrorr   r4   r   	POP3Errorformatdecode)r   r   r7   s      r   rf   zVirtualPOP3.lookupDomainN  s    	::d&:&:A>LD& ||'S)SS'---..!4!;!;FMM'<R!STTV|  	F	s   B BB)r   r   rq   r   returnzMdefer.Deferred[tuple[type[pop3.IMailbox], pop3.IMailbox, Callable[[], None]]])r   r   rz   ztuple[bytes, bytes])
rA   rB   rC   rD   r   rE   rd   ro   rs   rf   rG   r   r   rb   rb      sF     #'G&!OU!>%*	V@r   rb   c                  $    e Zd ZdZeZdZd Zd Zy)POP3Factorya  
    A POP3 server protocol factory.

    @ivar service: See L{__init__}

    @type protocol: no-argument callable which returns a L{Protocol
        <protocol.Protocol>} subclass
    @ivar protocol: A callable which creates a protocol.  The default value is
        L{VirtualPOP3}.
    Nc                    || _         y)zY
        @type service: L{MailService}
        @param service: An email service.
        N)r   )r   r   s     r   r   zPOP3Factory.__init__s  s    
 r   c                h    t         j                  j                  | |      }| j                  |_        |S )a   
        Create an instance of a POP3 server protocol.

        @type addr: L{IAddress <twisted.internet.interfaces.IAddress>} provider
        @param addr: The address of the POP3 client.

        @rtype: L{POP3}
        @return: A POP3 protocol.
        )r   ServerFactoryrS   r   rT   s      r   rS   zPOP3Factory.buildProtocolz  s,     ""00t<LL	r   )	rA   rB   rC   rD   rb   r   r   r   rS   rG   r   r   r|   r|   d  s    	 HGr   r|   N)$rD   
__future__r   typingr   r   zope.interfacer   twisted.copyrightr   twisted.cred.credentialsr   r	   twisted.cred.errorr
   twisted.internetr   r   twisted.mailr   r   r   twisted.pythonr   twisted.mail.mailr   IMessageDeliveryr   rI   rL   rN   rY   POP3rb   r   r|   rG   r   r   <module>r      s   
 # * & ) I 0 , * * - T""#r r $rj+ , +$"" +\.; .bc$)) cL"((( "r   