
    ,h                        d Z ddlmZ ddlZddl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 ej&                  j(                  Zd
dgZd Z ej.                  d      Zd Zd ZddZd ZddZddZd Zd Z d Z!y)z
Parsing for IMAP command responses with focus on FETCH responses as
returned by imaplib.

Initially inspired by http://effbot.org/zone/simple-iterator-parser.htm
    )unicode_literalsN)defaultdict   )parse_to_datetime)TokenSource)BodyDataEnvelopeAddress	SearchIds)ProtocolErrorparse_responseparse_message_listc                 :    | dgk(  rg S t        t        |             S )zbPull apart IMAP command responses.

    Returns nested tuples of appropriately typed objects.
    N)tuplegen_parsed_response)datas    \/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/imapclient/response_parser.pyr   r       s#    
 v~	$T*++    z(\d+(?: +\d+)*)c                    t        |       dk7  rt        d      | d   } | s
t               S t        j                  r+t        | t        j                        r| j                  d      } t        j                  |       }|st        d      t        d |j                  d      j                         D              }| |j                  d      d }|rt        |j                  d      g      D ]c  }t        |t              r/t        |      dk(  r!|d   j!                         d	k(  r|d   |_        Bt        |t$              sS|j'                  |       e |S )
ap  Parse a list of message ids and return them as a list.

    parse_response is also capable of doing this but this is
    faster. This also has special handling of the optional MODSEQ part
    of a SEARCH response.

    The returned list is a SearchIds instance which has a *modseq*
    attribute which contains the MODSEQ response (if returned by the
    server).
    r   zunexpected message list datar   asciizunexpected message list formatc              3   2   K   | ]  }t        |        y wN)int).0ns     r   	<genexpr>z%parse_message_list.<locals>.<genexpr>F   s     7qCF7   N   s   modseq)len
ValueErrorr   sixPY3
isinstancebinary_typedecode_msg_id_patternmatchgroupsplitendr   encoder   lowermodseqr   append)r   midsextraitems        r   r   r   -   s    4yA~7887D{
ww:dCOO4{{7#d#A9::
7AGGAJ$4$4$67
7C qOE"ELL$9#:; 	!D$&3t9>d1gmmoQZ>Z!!W
D#&

4 		!
 Jr   c              #      K   | sy t        |       }d }	 |D ]  }t        ||        y # t        $ r  t        $ r2 t	        j
                         \  }}}t        t        |      d|      w xY wwNz: )r   atomr   r    sysexc_infostr)textsrctoken_errs        r   r   r   T   sz     
d
CE: 	#EsE""	#  :LLN	3C%899:s   A1* A1AA..A1c                    | dgk(  ri S t        |       }t        t              }	 	 t        t	        j
                  |      d      x}}	 t	        j
                  |      }t        |t              st        dt        |      z        t        |      dz  rt        dt        |      z        d|i}t        dt        |      d      D ]  }	||	   j                         }
||	d	z      }|
d
k(  rt        |d      }|r|}4|||
<   :|
dk(  rt        ||      ||
<   O|
dk(  rt        ||      ||
<   d|
dv rt!        j"                  |      ||
<   |||
<    ||   j%                  |       <# t        $ r Y |S w xY w# t        $ r t        d      w xY w)zPull apart IMAP FETCH responses as returned by imaplib.

    Returns a dictionary, keyed by message ID. Each value a dictionary
    keyed by FETCH field type (eg."RFC822").
    Nzinvalid message IDzunexpected EOFzbad response type: %sr   z#uneven number of response items: %ss   SEQr   r   s   UIDzinvalid UIDs   INTERNALDATEs   ENVELOPE)s   BODYs   BODYSTRUCTURE)r   r   dict_int_or_errorr!   nextStopIterationr   r#   r   reprr   xrangeupper_convert_INTERNALDATE_convert_ENVELOPEr   createupdate)r9   normalise_times
uid_is_keyresponseparsed_responsemsg_idseqmsg_responsemsg_dataiwordvalueuids                r   parse_fetch_responserV   d   s    v~	"4(H!$'O
	(();)=? ?FS
	288H-L ,. 7$|:L LMM|q  E\HZ Z[[ C=3|,a0 	'A?((*D Q'Ev~#E=9 F%(HTN(!6uo!N$!25/!J44!)!7!&#	'& 	&&x0O   	H K	
  	2 011	2s   !E  E0  	E-,E-0Fc                 r    	 t        |       S # t        t        f$ r t        |dt	        |             w xY wr4   )r   	TypeErrorr    r   rC   )rT   
error_texts     r   r@   r@      s<    B5zz" B
DK@AABs   
 )6c                 B    | y 	 t        | |      S # t        $ r Y y w xY w)N	normalise)r   r    )date_stringrJ   s     r   rF   rF      s/     HH s    	c                 B   d }| d   r	 t        | d   |      }| d   }g }| dd D ]S  }g }|r<|D ]  }|s|j                  t        |         |j                  t	        |             C|j                  d        U t        ||g|| d   | d   dS # t        $ r Y w xY w)Nr   r[   r   r      	   )in_reply_to
message_id)r   r    r.   r
   r   r	   )envelope_responserJ   dtsubject	addresses	addr_listaddrs
addr_tuples           r   rG   rG      s    	B	"#4Q#7?SB  "G I&q+ #	' 7
LL*!567 U5\*T"# 
G%a($Q' %  		s   B 	BBc                 l   |dk(  rt        |       S |dk(  ry |d d dk(  rSt        |dd       }| j                  }|t        d|z        t	        |      |k7  rt        d|t	        |      fz        |S t	        |      dk\  r|d d |dd  cxk(  rd	k(  rn n|dd S |j                         rt        |      S |S )
N   (s   NILr      {zNo literal corresponds to %rz$Expecting literal of size %d, got %dr      ")parse_tupler   current_literalr   r   isdigit)r:   r;   literal_lenliteral_texts       r   r5   r5      s    }3	&	rd	%"+&** > FGG|+ FS.J0 !0 1 1	UqeBQi5:==Qr{	5zr   c                     g }| D ]/  }|dk(  rt        |      c S |j                  t        | |             1 t        dt	        |      z        )N   )zTuple incomplete before "(%s")r   r.   r5   r   
_fmt_tuple)r:   outr;   s      r   ro   ro      sQ    
C %D=:

4U#$%
 7*S/I
JJr   c                 2    dj                  d | D              S )N c              3   2   K   | ]  }t        |        y wr   )r8   )r   r2   s     r   r   z_fmt_tuple.<locals>.<genexpr>   s     ,$CI,r   )join)ts    r   rv   rv      s    88,!,,,r   )TT)T)"__doc__
__future__r   rer6   collectionsr   r!   datetime_utilr   response_lexerr   response_typesr   r	   r
   r   
exceptionsr   movesrD   __all__r   compiler&   r   r   rV   r@   rF   rG   r5   ro   rv    r   r   <module>r      s   
 ( 	 
 # 
 , ' B B %			1
2, "**./$N: 4nB<,K-r   