
    >OOfS                         d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZmZmZmZ dd	lmZmZ dd
lmZ ddlmZ  G d de      Zy)
    )StringIO)linesepN   LDAPLDIFError)prepare_for_stream)LDAPMessage	MessageID
ProtocolOpLDAP_MAX_INT)operation_to_ldifadd_ldif_header)build_controls_list   )BaseStrategyc                   b    e Zd ZdZd ZddZd Zd Zd Zd Z	dd	Z
d
 Zd Zd Zd Zd Zd Zy)LdifProducerStrategya  
    This strategy is used to create the LDIF stream for the Add, Delete, Modify, ModifyDn operations.
    You send the request and get the request in the ldif-change representation of the operation.
    NO OPERATION IS SENT TO THE LDAP SERVER!
    Connection.request will contain the result LDAP message in a dict form
    Connection.response will contain the ldif-change format of the requested operation if available
    You don't need a real server to connect to for this strategy
    c                     t        j                  | |       d| _        d| _        d| _        d| _        t        | _        d| _        d | _	        t               | _        d| _        t        j                          y NTF)r   __init__syncno_real_dsapooled
can_streamr   line_separator
all_base64streamdictorder_header_addedrandomseed)selfldap_connections     \/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/ldap3/strategy/ldifProducer.pyr   zLdifProducerStrategy.__init__0   s_    dO4	%V
"    c                     t         | j                  _        | j                  j                  r)| j                  j                  xj
                  dz  c_        d| j                  _        y )Nr   F)NotImplemented
connectionsocketusage_usageopen_socketsclosed)r$   addressuse_sslunix_sockets       r&   _open_socketz!LdifProducerStrategy._open_socket=   sB    !/??  OO""//14/!&r'   c                     | j                   j                  r)| j                   j                  xj                  dz  c_        d | j                   _        d| j                   _        y )Nr   T)r*   r,   r-   closed_socketsr+   r/   r$   s    r&   _close_socketz"LdifProducerStrategy._close_socketD   sB    ??  OO""11Q61!%!%r'   c                 &   d| j                   _        d| j                   _        d| j                   _        d| _        | j
                  r0t        | j
                  t              r1| j
                  j                  r| j                  t                      y y y r   )	r*   	listeningr/   boundr!   r   
isinstancer   
set_streamr6   s    r&   _start_listenz"LdifProducerStrategy._start_listenK   sf    $(!!& $"{{z$++x@T[[EWEWOOHJ' FX@r'   c                     | j                   j                          d| j                  _        d| j                  _        d| j                  _        y )NFT)r   closer*   r9   r:   r/   r6   s    r&   _stop_listenz!LdifProducerStrategy._stop_listenS   s6    $)! %!%r'   c                      y N r6   s    r&   	receivingzLdifProducerStrategy.receivingY   s    r'   Nc                    t        j                  dt              }t               }t	        |      |d<   t               j                  ||      |d<   t        |      }|||d<   t        j                  |||      | j                  _        || j                  j                  d<   | j                  t               | _        | j                  j                  | j                  |<   |S )zA
        Build the LDAPMessage without sending to server
        r   	messageID
protocolOpcontrols)r"   randintr   r
   r   r   setComponentByNamer   r   decode_requestr*   request_outstandingr   )r$   message_typerL   rH   
message_idldap_messagemessage_controlss          r&   sendzLdifProducerStrategy.send\   s     ^^A|4
"}$-j$9[!%/\%D%D\SZ%[\".x8''7L$"."="=lGU]"^.6
+$ $D(,(?(?*%r'   c           	         d | j                   _        d | j                   _        | j                  r"|| j                  v r| j                  j	                  |      }t        | j                   j                  d   || j                  | j                  j                  | j                   j                  d               }| j                  rB|r@| j                   j                  s*| j                  | j                  j                  |             t        |      }| j                  j                  |      | j                   _        | j                   j                  S y )Ntype)r*   responseresultrM   popr   rL   r   r    getr   r/   accumulate_streamr   joinr   )r$   rO   rL   
ldif_liness       r&   post_send_single_responsez.LdifProducerStrategy.post_send_single_responseo   s   #' !%t/@/@!@''++J7G*4??+B+B6+JGUYUdUdfjfpfpftftuy  vE  vE  vM  vM  NT  vU  gV  WJ{{z$//2H2H&&t':':'?'?
'KL(4J'+':':'?'?
'KDOO$??+++r'   c                     t        d      )Nz5LDIF-CONTENT cannot be produced for Search operationsr   )r$   rO   s     r&   post_send_searchz%LdifProducerStrategy.post_send_search}   s    STTr'   c                      y rB   rC   )r$   rO   timeouts      r&   _get_responsez"LdifProducerStrategy._get_response   s    r'   c                 z   | j                   sq| j                  j                         dk(  rTd| _         t        dg      d   }| j                  j	                  t        || j                  z   | j                  z                | j                  j	                  t        || j                  z   | j                  z                y )Nr   T-)r!   r   tellr   writer	   r   )r$   fragmentheaders      r&   rY   z&LdifProducerStrategy.accumulate_stream   s    !!dkk&6&6&8A&=!%D$cU+A.FKK0$:M:M1MPTPcPc1cde,X8K8K-KdNaNa-abcr'   c                     | j                   S rB   )r   r6   s    r&   
get_streamzLdifProducerStrategy.get_stream   s    {{r'   c                     d}	 |j                         sd}|rt        d      || _        y # t        t        f$ r d}Y (w xY w)NFTzstream must be writable)writable
ValueErrorAttributeErrorr   r   )r$   valueerrors      r&   r<   zLdifProducerStrategy.set_stream   sN    	>>#  9:: N+ 	E	s   + ??)FFrB   )__name__
__module____qualname____doc__r   r3   r7   r=   r@   rD   rR   r\   r^   ra   rY   ri   r<   rC   r'   r&   r   r   &   sJ    '&(&&Udr'   r   )rs   ior   osr   r"   core.exceptionsr   
utils.convr	   protocol.rfc4511r
   r   r   r   protocol.rfc2849r   r   protocol.convertr   baser   r   rC   r'   r&   <module>r|      s8   2    + + O O A 2 r< rr'   