U
    h;                     @   s  d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZ ddlZddlZddlZddlZddlmZmZmZmZ eeee eeef ddd	Zeeed
ddZeedddZ G dd deZ!d-eeeeee!dddZ"d.eeddddZ#eeeeddddZ$eedddZ%G d d! d!eZ&d/eeee&d"d#d$Z'edd%d&d'Z(G d(d) d)eZ)ee)d*d+d,Z*dS )0   )EmailSyntaxError)ValidatedEmail)EMAIL_MAX_LENGTHLOCAL_PART_MAX_LENGTHDOMAIN_MAX_LENGTHDOT_ATOM_TEXTDOT_ATOM_TEXT_INTLATEXT_REATEXT_INTL_DOT_REATEXT_HOSTNAME_INTL
QTEXT_INTLDNS_LABEL_LENGTH_LIMITDOT_ATOM_TEXT_HOSTNAMEDOMAIN_NAME_REGEXDOMAIN_LITERAL_CHARS    N)OptionalTuple	TypedDictUnion)emailreturnc                 C   s0  t tt df tt t f ddd}t tt tf ddd}|| d\}}|d	r| }||\}}|sd
d |D }|rtddt| d t|dd d|krtd|d}|d dkrtd|dd  d}||d\}	}
nd }|| }	}
|
dr|
dd  }
||	\}	}||	|
|fS )N.)textspecialsr   c                 S   s   d}d}d}t | D ]\}}td| |d  d |krB||7 }q|r|||7 }|dkr`|s`d}q|dkrv|svd}d}qd}q|dkr||7 }d}q||kr qq||7 }qt|t| krd| krtd	d
| krtdtd| t|d  }||fS )NF ZNFCr   \T"u   ＠zZThe email address has the "full-width" at-sign (@) character instead of a regular at-sign.u   ﹫zWThe email address has the "small commercial at" character instead of a regular at-sign.z%An email address must have an @-sign.)	enumerateunicodedata	normalizelenr   )r   r   Zinside_quoteescaped	left_partic
right_part r&   <./venv/lib/python3.8/site-packages/email_validator/syntax.py split_string_at_unquoted_special2   s8    

	z5split_email.<locals>.split_string_at_unquoted_special)r   r   c              	   S   s   d}d}d}t | D ]\}}|r|r2||7 }d}q|dkr@d}q|dkr|t| d krtddd	d
 | |d d  D   qq||7 }q|dkr|dkrd}q||7 }q||fS )NFr   r   Tr   r   z,Extra character(s) found after close quote: , c                 s   s   | ]}t |V  qd S Nsafe_character_display.0r$   r&   r&   r'   	<genexpr>   s     z=split_email.<locals>.unquote_quoted_string.<locals>.<genexpr>r   )r   r    r   join)r   quotedr!   valuer#   r$   r&   r&   r'   unquote_quoted_strings   s*    

z*split_email.<locals>.unquote_quoted_string)@<r5   c                 S   s.   h | ]&}t |s|d ks"|dkrt|qS ) .r	   matchr,   r-   r&   r&   r'   	<setcomp>   s
   
  zsplit_email.<locals>.<setcomp>z>The display name contains invalid characters when not quoted: r)   r7   Tallow_space>zoAn open angle bracket at the start of the email address has to be followed by a close angle bracket at the end.r6   z0There can't be anything after the email address.r   )r4   r4   )	strr   bool
startswithrstripr   r0   sortedcheck_unsafe_chars)r   r(   r3   r"   r%   Zdisplay_nameZdisplay_name_quoted	bad_charsZ	addr_spec
local_partZdomain_partZis_quoted_local_partr&   r&   r'   split_email   s4    $"A


rG   )addrlimitr   c                 C   s.   t | | }|dkrdnd}d| d| dS )zEHelper function to return an error message related to invalid length.r   sr   (
 character
 too many))r    )rH   rI   diffsuffixr&   r&   r'   get_length_reason   s    rP   )r$   r   c                 C   sp   | dkrd|  dS t | d dkr.t| S t| dk rPdt| d }ndt| d }t | |S )	Nr   r   r   LNPSi  zU+Z04xZ08x)r   categoryreprorduppername)r$   hr&   r&   r'   r,      s    r,   c                   @   s*   e Zd ZU eed< ee ed< eed< dS )LocalPartValidationResultrF   ascii_local_partsmtputf8N)__name__
__module____qualname__r?   __annotations__r   r@   r&   r&   r&   r'   r\      s   
r\   TF)localallow_smtputf8allow_empty_localquoted_local_partstrictr   c           
   
   C   s  t | dkr$|std| | ddS |rPt | tkrPt| td}td| dt| rf| | ddS d}d}t| r|sd	d
 | D }|rtddt| d tdd}d}nn|r(dd
 | D }|rtddt| d dd
 | D }|r$d}|s$tddt| d d}|rt	| |dkd z| 
d W n. tk
rz }	 ztd|	W 5 d}	~	X Y nX |dkrdtdd|  d } | |s| nd|dS dd
 | D }|rtddt| d t| dddd tddS ) z;Validates the syntax of the local part of an email address.r   z*There must be something before the @-sign.F)rF   r]   r^   rI   z0The email address is too long before the @-sign r7   Nc                 S   s   h | ]}t |st|qS r&   r8   r-   r&   r&   r'   r:   "  s   
z,validate_email_local_part.<locals>.<setcomp>zBInternationalized characters before the @-sign are not supported: r)   zAInternationalized characters before the @-sign are not supported.zdot-atomTc                 S   s   h | ]}t |st|qS r&   )r   r9   r,   r-   r&   r&   r'   r:   :  s   
zKThe email address contains invalid characters in quotes before the @-sign: c                 S   s.   h | ]&}d t |  kr dksn t|qS )    ~   )rX   r,   r-   r&   r&   r'   r:   C  s
     
r1   r;   utf8z0The email address contains an invalid character.r   z(["\\])z\\\1c                 S   s   h | ]}t |st|qS r&   )r
   r9   r,   r-   r&   r&   r'   r:   w  s   
zAThe email address contains invalid characters before the @-sign: z(An email address cannot start with a {}.z@An email address cannot have a {} immediately before the @-sign.is_hostnamez@The email address contains invalid characters before the @-sign.)r    r   r   rP   r   r9   r   r0   rC   rD   encode
ValueErrorresubcheck_dot_atom)
rc   rd   re   rf   rg   reasonZvalidZrequires_smtputf8rE   er&   r&   r'   validate_email_local_part   sx    	



ru   )rJ   r<   r   c                 C   s   t  }t| D ]\}}t|}|d dkr.q|d dkrN|dkr|| q|dkrf|s|| q|d dkr~|| q|d dkr|| q|| q|rtddd	d
 t|D  d d S )Nr   rQ   MZsZCz.The email address contains unsafe characters: r)   c                 s   s   | ]}t |V  qd S r*   r+   r-   r&   r&   r'   r/     s     z%check_unsafe_chars.<locals>.<genexpr>r7   )setr   r   rV   addr   r0   rC   )rJ   r<   rE   r#   r$   rV   r&   r&   r'   rD     s,    

rD   )labelstart_descr	end_descrrm   r   c                 C   s   |  drt|d| dr0t|dd| kr@td|r|  dr\t|d| drtt|dd| ksd| krtd	d S )
Nr7   Zperiodz..z2An email address cannot have two periods in a row.-Zhyphenz.-z-.zFAn email address cannot have a period and a hyphen next to each other.)endswithr   formatrA   )r|   r}   r~   rm   r&   r&   r'   rr     s    



rr   )charr   c                 C   s   t | }d|  krdkr$n ndS d|  kr8dkrjn n.d|  krPdksjn d|  krfd	krn n6|d
ksd|  krdksn d|  krdkrn ndS tt|dkrdS dtt|dkrdS dS )N      Fi   i`   i$   i&   i(   i.   )   id   i  i   i   i i T)ZCfZCnZCoZCsrw   ZZlZZpZ002Er6   )rX   r   rV   chrZdecompositionsplit)r   r$   r&   r&   r'   uts46_valid_char  s,    F  
r   c                   @   s   e Zd ZU eed< eed< dS )DomainNameValidationResultascii_domaindomainN)r_   r`   ra   r?   rb   r&   r&   r&   r'   r     s   
r   )r   test_environmentglobally_deliverabler   c              
   C   s  dd | D }|r,t ddt| d t|  dd | D }|r`t ddt| d | }ztj| ddd} W n6 tjk
r } zt d	| d
|W 5 d}~X Y nX dd | D }|rt ddt| d t| dddd | dD ]}t	
d|t	jrt dqt
| r&| }nlzddd | dD }W n8 tjk
r| } zt d| d
|W 5 d}~X Y nX t
|st dt|tkr||krt|td}t d| dn4t|t }	|	dkrdnd}
t d|	 d|
 d|dD ]0}t|tkrt|td}t d | dq|rtd|kr`|d!krX|s`t d"t|stt d#dd$lm} |D ]<}|d!kr|rq||ks|d| rt d%qzt|d&}W n8 tjk
r } zt d'| d
|W 5 d}~X Y nX d(d |D }|r>t ddt| d t| zt| W n8 tjk
r } zt d)| d
|W 5 d}~X Y nX ||d*S )+z<Validates the syntax of the domain part of an email address.c                 S   s   h | ]}t |st|qS r&   r   r9   r,   r-   r&   r&   r'   r:     s   
z-validate_email_domain_name.<locals>.<setcomp>z7The part after the @-sign contains invalid characters: r)   r7   c                 S   s   h | ]}t |st|qS r&   )r   r,   r-   r&   r&   r'   r:     s   F)Z
std3_rulesZtransitionalz7The part after the @-sign contains invalid characters ().Nc                 S   s   h | ]}t |st|qS r&   r   r-   r&   r&   r'   r:     s   
zSThe part after the @-sign contains invalid characters after Unicode normalization: z?An email address cannot have a {} immediately after the @-sign.z&An email address cannot end with a {}.Trl   z
(?!xn)..--zAn email address cannot have two letters followed by two dashes immediately after the @-sign or after a period, except Punycode.c                 s   s   | ]}t |d V  qdS )asciiN)idnaZalabeldecode)r.   r|   r&   r&   r'   r/   G  s   z-validate_email_domain_name.<locals>.<genexpr>z&The part after the @-sign is invalid (zSThe email address contains invalid characters after the @-sign after IDNA encoding.rh   z/The email address is too long after the @-sign r   r   rJ   z0The email address is too long after the @-sign ( bytez too many after IDNA encoding).zDAfter the @-sign, periods cannot be separated by so many characters Ztestz@The part after the @-sign is not valid. It should have a period.zRThe part after the @-sign is not valid. It is not within a valid top-level domain.)SPECIAL_USE_DOMAIN_NAMESz[The part after the @-sign is a special-use or reserved name that cannot be used with email.r   z-The part after the @-sign is not valid IDNA (c                 S   s   h | ]}t |st|qS r&   r   r-   r&   r&   r'   r:     s   
zXThe part after the @-sign became invalid after normalizing to international characters ()r   r   )r   r0   rC   rD   r   Zuts46_remapZ	IDNAErrorrr   r   rp   r9   Ir   r    r   rP   r   r   searchr   r   r   r   rn   )r   r   r   rE   Zoriginal_domainrt   r|   r   rs   rN   rJ   r   dZdomain_i18nr&   r&   r'   validate_email_domain_name  s    
$	

$	

$$	r   )addrinfor   c           
      C   s*  | j d f| jdf| jp| jpdd | j dfg}|D ]\}}t|}t|d}|t }|dkr4|d kr||krt|td}n|d krt	dd	 |D }t	d
|| }|d
krdnd}	||krd| d|	 d}nd| d| d|	 d}n&|d
krdnd}	|d| d|	 d7 }t
d| dq4d S )Nzafter normalizationr   r4   z9when the part after the @-sign is converted to IDNA ASCIIrk   r   rh   c                 s   s   | ]}t |d V  qdS )rk   N)r    rn   r-   r&   r&   r'   r/     s     z(validate_email_length.<locals>.<genexpr>r   rJ   rK   rL   rM   r   z (r   zThe email address is too long r7   )ZoriginalZ
normalizedr]   rF   r   r    rn   r   rP   maxr   )
r   Zaddresses_to_checkrH   rs   Zaddr_lenZaddr_utf8_lenrN   ZmbpcZmcharsrO   r&   r&   r'   validate_email_length  s*    r   c                   @   s*   e Zd ZU eejejf ed< eed< dS )DomainLiteralValidationResultdomain_addressr   N)	r_   r`   ra   r   	ipaddressIPv4AddressIPv6Addressrb   r?   r&   r&   r&   r'   r     s   
r   )domain_literalr   c              
   C   s  t d| rbzt| }W n4 tk
rN } ztd| d|W 5 d }~X Y nX |d| ddS | drzt| dd  }W n4 tk
r } ztd	| d
|W 5 d }~X Y nX |d|j ddS d| krtddd | D }|r
tdd	t
| d tdd S )Nz
^[0-9\.]+$zRThe address in brackets after the @-sign is not valid: It is not an IPv4 address (z') or is missing an address literal tag.[])r   r   zIPv6:   z<The IPv6 address in brackets after the @-sign is not valid (r   z[IPv6::z\The part after the @-sign in brackets is not an IPv4 address and has no address literal tag.c                 S   s   h | ]}t |st|qS r&   )r   r9   r,   r-   r&   r&   r'   r:   ,  s   
z0validate_email_domain_literal.<locals>.<setcomp>zCThe part after the @-sign contains invalid characters in brackets: r)   r7   zNThe part after the @-sign contains an invalid address literal tag in brackets.)rp   r9   r   r   ro   r   rA   r   
compressedr0   rC   )r   rH   rt   rE   r&   r&   r'   validate_email_domain_literal  s0    
$

$r   )TFFF)F)FT)+
exceptionsr   typesr   Zrfc_constantsr   r   r   r   r   r	   r
   r   r   r   r   r   r   rp   r   r   r   typingr   r   r   r   r?   r@   rG   intrP   r,   r\   ru   rD   rr   r   r   r   r   r   r   r&   r&   r&   r'   <module>   s<   <  <       "4 H?