o
    b1                     @   s   d Z ddlZddlmZ ddlmZmZ ddl	m
Z
mZ ddlmZmZ ddlmZ dd	 Zd
d ZG dd dejejZG dd deejZG dd dejZdS )z1
Postfix mail transport agent related protocols.
    N)UserDict)quoteunquote)deferprotocol)basicpolicies)logc                 C   s    t | }t|tr|d}|S Nascii)_quote
isinstancestrencodesquoted r   ;/usr/lib/python3/dist-packages/twisted/protocols/postfix.pyr      s   

r   c                 C   s&   t | tr
| d} t| }|dS r
   )r   bytesdecode_unquoter   r   r   r   r   r      s   


r   c                   @   sR   e Zd ZdZdZdZdd ZdddZd	d
 Zdd Z	dd Z
dd Zdd ZdS )PostfixTCPMapServera  
    Postfix mail transport agent TCP map protocol implementation.

    Receive requests for data matching given key via lineReceived,
    asks it's factory for the data with self.factory.get(key), and
    returns the data to the requester. None means no entry found.

    You can use postfix's postmap to test the map service::

    /usr/sbin/postmap -q KEY tcp:localhost:4242

    iX     
c                 C   s   |  | j d S N)
setTimeouttimeout)selfr   r   r   connectionMade3   s   z"PostfixTCPMapServer.connectionMade    c                 C   s    |  t|dd |  dS )z8
        Send an SMTP-like code with a message.
        r       N)sendLiner   r   )r   codemessager   r   r   sendCode6   s    zPostfixTCPMapServer.sendCodec                 C   s   |    z
|d d\}}W n ty   |}d }Y nw zt| d|d }W n ty8   | dd Y d S w z|| W d S  tya   tt	
 d d}| dd| d |  Y d S w )N   do_r     s   unknown commands   Command s	    failed: )resetTimeoutsplit
ValueErrorgetattrr   AttributeErrorr$   BaseExceptionr   sysexc_infor   )r   linerequestparamsfexcInfor   r   r   lineReceived<   s$   z PostfixTCPMapServer.lineReceivedc                 C   sH   |d u r|  dd d S t| jj|}|| j| j |t	j
 d S )Nr'   s!   Command 'get' takes 1 parameters.)r$   r   maybeDeferredfactorygetaddCallbacks_cbGot_cbNot
addErrbackr	   err)r   keydr   r   r   do_getN   s
   zPostfixTCPMapServer.do_getc                 C   s   |  d}| d| d S )Nr   r'   )getErrorMessager   r$   )r   failmsgr   r   r   r;   V   s   zPostfixTCPMapServer._cbNotc                 C   s*   |d u r|  d d S |  dt| d S )N     )r$   r   )r   valuer   r   r   r:   Z   s   zPostfixTCPMapServer._cbGotc                 C   s^   |d u r|  dd d S z
|d d\}}W n ty&   |  dd Y d S w |  dd d S )Nr'   s!   Command 'put' takes 2 parameters.r%   rD   s   put is not implemented yet.)r$   r)   r*   )r   keyAndValuer>   rF   r   r   r   do_put`   s   zPostfixTCPMapServer.do_putN)r   )__name__
__module____qualname____doc__r   	delimiterr   r$   r5   r@   r;   r:   rH   r   r   r   r   r   "   s    
r   c                   @   s   e Zd ZdZeZdS )PostfixTCPMapDictServerFactoryB
    An in-memory dictionary factory for PostfixTCPMapServer.
    N)rI   rJ   rK   rL   r   r   r   r   r   r   rN   l   s    rN   c                   @   s&   e Zd ZdZeZdddZdd ZdS )'PostfixTCPMapDeferringDictServerFactoryrO   Nc                 C   s"   i | _ |d ur| j | d S d S r   )dataupdate)r   rQ   r   r   r   __init__{   s   z0PostfixTCPMapDeferringDictServerFactory.__init__c                 C   s   t | j|S r   )r   succeedrQ   r8   )r   r>   r   r   r   r8      s   z+PostfixTCPMapDeferringDictServerFactory.getr   )rI   rJ   rK   rL   r   r   rS   r8   r   r   r   r   rP   t   s
    
rP   )rL   r.   collectionsr   urllib.parser   r   r   r   twisted.internetr   r   twisted.protocolsr   r   twisted.pythonr	   LineReceiverTimeoutMixinr   ServerFactoryrN   rP   r   r   r   r   <module>   s   J