o
    �žŽjž  ã                   @   s  d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZm	Z	m
Z
mZmZmZmZmZ d dlmZ d dlZi Zi Zi Zdd„ Zd,dd	„Zd-d
d„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"d d!„ Z#d"d#„ Z$d$d%„ Z%d&d'„ Z&d(d)„ Z'd-d*d+„Z(dS ).é    N)ÚClientÚfilters)ÚAuthKeyUnregisteredÚ	FloodWaitÚ	PeerFloodÚPeerIdInvalidÚUserDeactivatedÚUserIsBlockedÚUsernameInvalidÚUsernameNotOccupied)ÚMessageHandlerc                 C   s$   t | ƒ} | tvrt ¡ t| < t|  S ©N)ÚstrÚ_locksÚasyncioÚLock)Úkey© r   ú!/var/www/kodo/Anonymous/client.pyÚ_lock   s   r   Úkodoc                 C   s   | › dt t ¡ d ƒ› �S )NÚ_iè  )ÚintÚtime)Úprefixr   r   r   Ú	_tmp_name!   s   r   c                 C   sP   | rt tƒ tjtj| dd�S |rt |tjtj|ptjd�S t tƒ tjtjdd�S )NT)ÚnameÚapi_idÚapi_hashÚsession_stringÚ	in_memory)r   r   r   Úworkdir)r   r   r   r    )r   r   ÚconfigÚAPI_IDÚAPI_HASHÚSESSIONS_DIR)r   r   r!   r   r   r   Ú_new_client%   s*   ûüür&   c                 Ã   sT   �t ƒ }| ¡ I d H  z
| | ¡I d H }W n ty$   | ¡ I d H  ‚ w ||jfS r   )r&   ÚconnectÚ	send_codeÚ	ExceptionÚ
disconnectÚphone_code_hash)ÚphoneÚappÚsentr   r   r   Ústart_login=   s   €þ
r/   c                 Ã   s   �|   |||¡I d H S r   )Úsign_in)r-   r,   r+   Úcoder   r   r   Úconfirm_codeH   s   €r2   c                 Ã   s   �|   |¡I d H S r   )Úcheck_password)r-   Úpasswordr   r   r   Úconfirm_passwordL   s   €r5   c                 Ã   s4   �|   ¡ I d H }|  ¡ I d H }|  ¡ I d H  ||fS r   )Úexport_session_stringÚget_mer*   )r-   r   Úmer   r   r   Úfinish_loginP   s
   €r9   c                 Ã   s,   �z
|   ¡ I d H  W d S  ty   Y d S w r   )r*   r)   )r-   r   r   r   Úcancel_loginW   s   €ÿr:   c                 C   st   zt  | ¡}| d¡}dd„ | ¡ D ƒ}| ¡  W n
 ty#   Y dS w d|v s0d|v s0d|v r2dS d	|v r8d
S dS )Nz1SELECT name FROM sqlite_master WHERE type='table'c                 S   s   h | ]}|d  ’qS )r   r   )Ú.0Úrowr   r   r   Ú	<setcomp>b   s    z!detect_library.<locals>.<setcomp>ÚunknownÚentitiesÚ
sent_filesÚupdate_stateÚTelethonÚsessionsÚpyrogram)Úsqlite3r'   ÚexecuteÚfetchallÚcloser)   )ÚpathÚconnÚcurÚtablesr   r   r   Údetect_library^   s   

ÿrM   c                 Ã   sÖ   �t | ƒ}|dkrd d |fS tj | ¡}| d¡r|d d… }t|tj | ¡d�}z)| ¡ I d H  | ¡ I d H }| 	¡ I d H }W z	| 
¡ I d H  W n tyR   Y nw z	| 
¡ I d H  W w  tye   Y w w |||fS )NrD   z.sessioniøÿÿÿ)r   r!   )rM   ÚosrI   ÚbasenameÚendswithr&   Údirnamer'   r6   r7   r*   r)   )rI   Úlibr   r-   r   r8   r   r   r   Úimport_session_filem   s.   €

ÿþÿ
rS   c                 Ã   s’   �t | d�}| ¡ I d H  z(| ¡ I d H }| ¡ I d H }W z| ¡ I d H  W ||fS  ty5   Y ||fS w z	| ¡ I d H  W w  tyH   Y w w )N©r   )r&   r'   r7   r6   r*   r)   )r   r-   r8   Úexportedr   r   r   Úimport_session_string�   s$   €
þþþÿrV   c                 Ã   s<   �t  | ¡}|d ur|dfS t|d�}| ¡ I d H  |dfS )NFrT   T)Ú_persistentÚgetr&   Ústart)Ú
account_idr   r-   r   r   r   Ú_acquire�   s   €

r[   c                 Ã   s4   �|rz
|   ¡ I d H  W d S  ty   Y d S w d S r   )Ústopr)   )r-   Ú	temporaryr   r   r   Ú_release˜   s   €ÿýr^   c                 Ã   s†  �t | ƒ4 I d H š­ t| |ƒI d H \}}z˜z | |¡I d H }|dfW W t||ƒI d H  W  d   ƒI d H  S  tttttfyV   Y W t||ƒI d H  W d   ƒI d H  dS  t	y‚ } z!d d|j
› �fW  Y d }~W t||ƒI d H  W  d   ƒI d H  S d }~w ty¬ } zd t|ƒfW  Y d }~W t||ƒI d H  W  d   ƒI d H  S d }~ww t||ƒI d H  w 1 I d H s¼w   Y  d S )NÚ )NÚnotfoundú
FloodWait )r   r[   Ú	get_usersr^   r   r
   r   Ú
IndexErrorÚKeyErrorr   Úvaluer)   r   )rZ   r   Úusernamer-   ÚtempÚuserÚer   r   r   Úresolve_user    s2   €
ôôô€	ô€	ôrj   c                 Ã   sÈ  �t | ƒ4 I d H �šL t ¡ }zt| |ƒI d H \}}W n7 ttfy0   Y W d   ƒI d H  dS  tyR } zddt|ƒfW  Y d }~W  d   ƒI d H  S d }~ww zùz&| ||¡I d H  dt ¡ | dfW W t	||ƒI d H  W  d   ƒI d H  S  t
y¬ } z&dt ¡ | d|j› �fW  Y d }~W t	||ƒI d H  W  d   ƒI d H  S d }~w tttfyÓ   dt ¡ | df Y W t	||ƒI d H  W  d   ƒI d H  S  ty÷   dt ¡ | df Y W t	||ƒI d H  W  d   ƒI d H  S  t�y   dt ¡ | d	f Y W t	||ƒI d H  W  d   ƒI d H  S  t�yL } z$dt ¡ | t|ƒfW  Y d }~W t	||ƒI d H  W  d   ƒI d H  S d }~ww t	||ƒI d H  w 1 I d H �s]w   Y  d S )
N)Fç        ÚdeadFrk   Tr_   ra   r`   ÚblockedÚ	peerflood)r   r   Ú	monotonicr[   r   r   r)   r   Úsend_messager^   r   re   r   r   r
   r	   r   )rZ   r   ÚtargetÚtextÚstartedr-   rg   ri   r   r   r   Ú	send_text°   sV   €ûù€ê&
ê€êêê"ê€êrt   c              	   Ã   sº   �t | ƒ4 I d H šG t | ¡}|d u r't|d�}| ¡ I d H  |t| < dt| < t|t |¡tj	@ tj
@ ƒ}| |¡ t | d¡d t| < |W  d   ƒI d H  S 1 I d H sVw   Y  d S )NrT   r   é   )r   rW   rX   r&   rY   Ú_watch_countr   r   rh   ÚprivateÚincomingÚadd_handler)rZ   r   Ú	target_idÚcallbackr-   Úhandlerr   r   r   Ústart_watcherÊ   s   €

ÿ
0ôr}   c              	   Ã   s   �t | ƒ4 I d H šz t | ¡}|d u r	 W d   ƒI d H  d S z| |¡ W n	 ty/   Y nw tdt | d¡d ƒt| < t|  dkrbt | d ¡ t | d ¡ z	| ¡ I d H  W n tya   Y nw W d   ƒI d H  d S W d   ƒI d H  d S W d   ƒI d H  d S 1 I d H s‰w   Y  d S )Nr   ru   )	r   rW   rX   Úremove_handlerr)   Úmaxrv   Úpopr\   )rZ   r|   r-   r   r   r   Ústop_watcherÚ   s4   €
ýÿÿû÷ó.ñr�   c              
   Ã   sŠ   �t | ƒ4 I d H š/ t| |ƒI d H \}}z| ¡ I d H W t||ƒI d H  W  d   ƒI d H  S t||ƒI d H  w 1 I d H s>w   Y  d S r   )r   r[   r7   r^   )rZ   r   r-   rg   r   r   r   r7   í   s   €ûûr7   c                 Ã   sÜ  �t | ƒ4 I d H šØ zt| |ƒI d H \}}W n6 ttfy+   Y W d   ƒI d H  dS  tyL } zdt|ƒfW  Y d }~W  d   ƒI d H  S d }~ww zŠz2|d usW|d ura|j||d�I d H  |rl|j|d�I d H  W W t||ƒI d H  W d   ƒI d H  dS  t	y­ } z!dd|j
› �fW  Y d }~W t||ƒI d H  W  d   ƒI d H  S d }~w ty× } zdt|ƒfW  Y d }~W t||ƒI d H  W  d   ƒI d H  S d }~ww t||ƒI d H  w 1 I d H sçw   Y  d S )N)Frl   F)Ú
first_nameÚbio)Úphoto)Tr_   ra   )r   r[   r   r   r)   r   Úupdate_profileÚset_profile_photor^   r   re   )rZ   r   r‚   rƒ   Ú
photo_pathr-   rg   ri   r   r   r   r…   ö   sB   €üú€îî€î€îr…   )r   )NNN))r   rN   rE   r   rD   r   r   Úpyrogram.errorsr   r   r   r   r   r	   r
   r   Úpyrogram.handlersr   r"   r   rW   rv   r   r   r&   r/   r2   r5   r9   r:   rM   rS   rV   r[   r^   rj   rt   r}   r�   r7   r…   r   r   r   r   Ú<module>   s<    (


		