U
    ¯ªhÆ  ã                   @   sX   d dl Z d dlZd dlZddlmZ ddlmZ ddlmZ dZ	dZ
G d	d
„ d
eƒZdS )é    Né   )ÚSession)ÚMemorySessioné   )ÚAuthKeyz
>B{}sH256sÚ1c                       s^   e Zd ZdZdedœ‡ fdd„Zeeedœdd„ƒZeeedœd	d
„ƒZ	e
dœdd„Z‡  ZS )ÚStringSessionag  
    This session file can be easily saved and loaded as a string. According
    to the initial design, it contains only the data that is necessary for
    successful connection and authentication, so takeout ID is not stored.

    It is thought to be used where you don't want to create any on-disk
    files but would still like to be able to save and load existing sessions
    by other means.

    You can use custom `encode` and `decode` functions, if present:

    * `encode` definition must be ``def encode(value: bytes) -> str:``.
    * `decode` definition must be ``def decode(value: str) -> bytes:``.
    N)Ústringc                    sŠ   t ƒ  ¡  |r†|d tkr"tdƒ‚|dd … }t|ƒdkr>dnd}t t |¡t	 
|¡¡\| _}| _}t |¡j| _t|ƒr†t|ƒ| _d S )Nr   zNot a valid stringr   i`  é   é   )ÚsuperÚ__init__ÚCURRENT_VERSIONÚ
ValueErrorÚlenÚstructÚunpackÚ_STRUCT_PREFORMATÚformatr   ÚdecodeZ_dc_idZ_portÚ	ipaddressÚ
ip_addressÚ
compressedZ_server_addressÚanyr   Z	_auth_key)Úselfr	   Zip_lenÚipÚkey©Ú	__class__© ú</tmp/pip-unpacked-wheel-c81u5j2r/telethon/sessions/string.pyr      s    
 ÿzStringSession.__init__)ÚxÚreturnc                 C   s   t  | ¡ d¡S )NÚascii)Úbase64Úurlsafe_b64encoder   ©r!   r   r   r    Úencode,   s    zStringSession.encodec                 C   s
   t  | ¡S )N)r$   Úurlsafe_b64decoder&   r   r   r    r   0   s    zStringSession.decode)r   c              
   C   sF   | j s
dS t | j¡j}tt t 	t
 t|ƒ¡| j|| j| j j¡¡ S )NÚ )Zauth_keyr   r   Úserver_addressÚpackedr   r   r'   r   Úpackr   r   r   Zdc_idÚportr   )r   r   r   r   r    Úsave4   s    
ûzStringSession.save)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   ÚstaticmethodÚbytesr'   r   r   r.   Ú__classcell__r   r   r   r    r      s   r   )r$   r   r   Zabstractr   Zmemoryr   Zcryptor   r   r   r   r   r   r   r    Ú<module>   s   