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 )ÚPostfixTCPMapDictServerFactoryúB
    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