
    ,h9                        d dl Zd dlZ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 dlmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZmZ  ej8                  e      Zej?                   ej@                                 G d
 de      Z!y)    N)decode_utf7)Parser)	constants)
AttachmentBrokenAttachmentUnsupportedAttachment)os_)MSGFile)	Recipient)addNumToDirinputToBytesinputToStringc                      e Zd ZdZdedddej                  fdZd Zd dZ	d Z
d	 Zd!d
Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Z ed        Z!ed        Z"y)"MessageBasez0
    Base class for Message like msg files.
     NFc           	      X   t        j                  | ||||||       || _        d| _        | j                   | j
                   | j                   |s| j                   | j                   | j                   | j                   | j                   d| _        | j                   | j                   y)a  
        :param path: path to the msg file in the system or is the raw msg file.
        :param prefix: used for extracting embeded msg files
            inside the main one. Do not set manually unless
            you know what you are doing.
        :param attachmentClass: optional, the class the Message object
            will use for attachments. You probably should
            not change this value unless you know what you
            are doing.
        :param filename: optional, the filename to be used by default when saving.
        :param delayAttachments: optional, delays the initialization of attachments
            until the user attempts to retrieve them. Allows MSG files with bad
            attachments to be initialized so the other data can be retrieved.
        :param overrideEncoding: optional, an encoding to use instead of the one
            specified by the msg file. Do not report encoding errors caused by this.
        F
N)r
   __init__ _MessageBase__attachmentsDelayed_MessageBase__attachmentsReadymainPropertiesheader
recipientsattachmentstoccsenderdate_MessageBase__crlfbodynamed)selfpathprefixattachmentClassfilenamedelayAttachmentsoverrideEncodingattachmentErrorBehaviors           Z/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/extract_msg/message_base.pyr   zMessageBase.__init__   s    & 	tV_hHXZqr$4!"' 					

    c                 ~   d|z   }	 t        | |      S # t        $ r d}| j                         r| j                  |   }|| j                         r$t        j                  dj                  |             g }| j                  D ]0  }|j                  dz  |k(  s|j                  |j                         2 t        |      dkD  rF|d   }t        |      dkD  r1t        dt        |            D ]  }|dj                  ||         z  } |}||j                  dd	      j                  d
d	      j                  dd	      }|j                  dd	      j                  dd	      j                  dd	      }|j                  d      dk7  r'|j                  dd	      }|j                  d      dk7  r't        | ||       |cY S w xY w)z7
        Returns the specified recipient field
        _NzMHeader found, but "{}" is not included. Will be generated from other streams.   r      z, {0}z 
	 z
	 z
	
r   z  )getattrAttributeError
headerInitr   loggerinfoformatr   typeappend	formattedlenrangereplacefindsetattr)r"   recipientTyperecipientIntprivatevaluefxsts           r*   _genRecipientzMessageBase._genRecipientA   s    %	4)) 	E M2}??$KK o v v  xE  !F  G .Avv
*l:-. q6A:1B1vz!&q#a&!1 7A'..1"66B7E i5==iMUUV^`cdfc2::4EMMdTWXjj&",!MM$4E jj&",D'5)L1	s    BF<DF<*F<;F<c                     | j                   r8| j                  s,	 | j                   | j                  j	                  |||f       y | j
                  D ]  }|j                  |||        y # t        $ r
 g | _        Y Vw xY wN)attachmentsDelayedattachmentsReady_MessageBase__waitingPropertiesr5   r;   r   _registerNamedProperty)r"   entry_typename
attachments        r*   rO   z"MessageBase._registerNamedPropertyb   s    ""4+@+@.(( $$++UE4,@A".. F
11%EF	 " .+-(.s   A* *A=<A=c                     	 | j                    | j                  D ],  }|j                  dk(  s|j                  j	                          . 	 t        j                  |        y # t
        $ r Y !w xY w)Nmsg)_attachmentsr   r:   datacloser5   r
   )r"   rS   s     r*   rX   zMessageBase.closem   sc    	".. ,
??e+OO))+,
 	d  		s   *A  A   	A,+A,c                 <    	 | j                    y# t        $ r Y yw xY w)zA
        Checks whether the header has been initialized.
        TF)_headerr5   r"   s    r*   r6   zMessageBase.headerInitw   s#    	LL 		s    	c                 P    | j                   D ]  }|j                  |||||        y)z<
        Saves only attachments in the same folder.
        N)r   save)r"   	contentIdjsonuseFileNameraw
customPathrS   s          r*   save_attachmentszMessageBase.save_attachments   s/     ** 	KJOOIt[#zJ	Kr+   c           	         	 | j                   S # t        $ rH g }| j                  dd      D ]l  }|t        | j                           j                  d      s+|t        | j                           |vsF|j                  |t        | j                                  n g | _         |D ]P  }	 | j                   j                  | j                  | |             1# t        $ r}| j                  t        j                  kD  r_t        j                  dj                  |             t        j                  |       | j                   j                  t!        | |             n Y d}~d}~wt"        $ r}| j                  t        j$                  k(  r_t        j                  dj                  |             t        j                  |       | j                   j                  t'        | |             n Y d}~Kd}~ww xY w d| _        	 | j*                   | j,                  r1| j                   D ]"  }| j*                  D ]  } |j.                  |   $ n#  Y nxY w| j                   cY S w xY w)z4
        Returns a list of all attachments.
        FT__attachz!Error processing attachment at {}N)rV   r5   listDirr=   
prefixList
startswithr;   r%   NotImplementedErrorr)   r   ATTACHMENT_ERROR_THROWr7   errorr9   	exceptionr   	ExceptionATTACHMENT_ERROR_BROKENr   r   rN   r   rO   )r"   attachmentDirsdir_attachmentDirerS   props          r*   r   zMessageBase.attachments   s!   
)	%$$$ '	%NUD1 FDOO,-88DS12.H"))$s4??/C*DEF
 !#D!/ %%,,T-A-A$-VW* 33i6V6VV%H%O%OP]%^_((+))001Ft]1[\ ] ! 33y7X7XX%H%O%OP]%^_((+))001A$1VW X	$ '+D#((,,&*&7&7 E
$($<$< ED=J==tDEE$$$O'	%ss    AI I 26I )+CI 	G4A=E I  G4,A=G/)I /G44I  A	I
	I 
II I c                     | j                   S )zL
        Returns True if the attachment initialization was delayed.
        )r   r[   s    r*   rL   zMessageBase.attachmentsDelayed   s    
 (((r+   c                     | j                   S )zG
        Returns True if the attachments are ready to be used.
        )r   r[   s    r*   rM   zMessageBase.attachmentsReady   s    
 &&&r+   c                 &    | j                  dd      S )z6
        Returns the bcc field, if it exists.
        bcc   rI   r[   s    r*   rw   zMessageBase.bcc   s    
 !!%++r+   c                 ^   	 | j                   S # t        $ r | j                  d      | _         | j                   rdt        | j                   d      | _         t	        j
                  d| j                         }|'t	        j
                  d| j                         d| _        | j                   cY S w xY w)z9
        Returns the message body, if it exists.
        z__substg1.0_1000zutf-8r   r1   )_bodyr5   _getStringStreamr   researchr   )r"   as     r*   r    zMessageBase.body   s    

	:: 	../ABDJzz*4::w?
IIdDJJ/=yy4@&,::	s    BB,+B,c                 &    | j                  dd      S )z5
        Returns the cc field, if it exists.
        r      ry   r[   s    r*   r   zMessageBase.cc       
 !!$**r+   c                 (    | j                  ddd      S )zB
        Returns the compressed RTF stream, if it exists.
        _compressedRtfz__substg1.0_10090102F
_ensureSetr[   s    r*   compressedRtfzMessageBase.compressedRtf   s    
 /1GOOr+   c                 2    | j                    | j                  S )z[
        Returns the value of self.__crlf, should you need it for whatever reason.
        )r    r   r[   s    r*   crlfzMessageBase.crlf   s    
 			{{r+   c                     	 | j                   S # t        $ r* | j                  j                  | _         | j                   cY S w xY w)z6
        Returns the send date, if it exists.
        )_dater5   _propr   r[   s    r*   r   zMessageBase.date   s9    
	:: 	DJ::	s    0A Ac                    	 | j                   S # t        $ r- | j                  d      }|8t               j	                  |      | _         | j
                  | j                   d<   nt        j                  d       t               j	                  d      }|j                  d| j
                         |j                  d| j                         |j                  d| j                         |j                  d	| j                         |j                  d
| j                         |j                  dd       || _         | j                   cY S w xY w)z[
        Returns the message header, if it exists. Otherwise it will generate one.
        z__substg1.0_007DNr   zNHeader is empty or was not found. Header will be generated from other streams.r   DateFromToCcz
Message-IdzAuthentication-Results)rZ   r5   r|   EmailParserparsestrr   r7   r8   
add_headerr   r   r   	messageId)r"   
headerTextr   s      r*   r   zMessageBase.header   s   
	 << 	 ../ABJ%*}55jA'+yyV$lm$//3!!&$))4!!&$++6!!$0!!$0!!,?!!":DA%<<!	 s    D4EEc                     	 | j                   S # t        $ r_ t        | j                  j                        | _         	 | j                   j                  d       n# t        $ r Y nw xY w| j                   cY S w xY w)zC
        Returns a dictionary of the entries in the header
        Received)_headerDictr5   dictr   _headerspopKeyErrorr[   s    r*   
headerDictzMessageBase.headerDict  sp    
	$### 	$#DKK$8$89D  $$Z0 ###	$s2    -A6AA6	A$!A6#A$$A65A6c                 (    | j                  ddd      S )z6
        Returns the html body, if it exists.
        	_htmlBodyz__substg1.0_10130102Fr   r[   s    r*   htmlBodyzMessageBase.htmlBody'  s    
 {,BEJJr+   c                 &    | j                  dd      S )zJ
        Returns the message id that this message is in reply to.
        _in_reply_toz__substg1.0_1042r   r[   s    r*   	inReplyTozMessageBase.inReplyTo.  s    
 ~/ABBr+   c                 L    t        | j                  d   j                  dz        S )z@
        Returns if this email has been marked as read.
        0E070003r/   )boolr   rE   r[   s    r*   isReadzMessageBase.isRead5  s%    
 D''
399A=>>r+   c                    	 | j                   S # t        $ ru d }| j                         r| j                  d   }||| _         n;| j                         rt        j                  d       | j                  d      | _         | j                   cY S w xY w)Nz
message-idzUHeader found, but "Message-Id" is not included. Will be generated from other streams.z__substg1.0_1035)
_messageIdr5   r6   rZ   r7   r8   r|   )r"   headerResults     r*   r   zMessageBase.messageId<  s~    	#??" 
	#L #||L9'".??$KK wx"&"7"78J"K??"
	#s    A;BBc                 T    t         j                  j                  | j                        S rK   )emailutils	parsedater   r[   s    r*   
parsedDatezMessageBase.parsedDateL  s    {{$$TYY//r+   c                    	 | j                   S # t        $ r g }| j                         D ]l  }|t        | j                           j                  d      s+|t        | j                           |vsF|j                  |t        | j                                  n g | _         |D ]'  }| j                   j                  t        ||              ) | j                   cY S w xY w)z3
        Returns a list of all recipients.
        __recip)_recipientsr5   rf   r=   rg   rh   r;   r   )r"   recipientDirsrp   recipientDirs       r*   r   zMessageBase.recipientsP  s    
	$### 	$M EDOO,-88CS12-G!((c$//.B)CDE
  "D - G  ''	,(EFG ###	$s    ACC/A(CCc                 @    t        j                  | j                        S )zE
        Returns the decompressed Rtf body from the message.
        )compressed_rtf
decompressr   r[   s    r*   rtfBodyzMessageBase.rtfBodyg  s    
 ((););<<r+   c                 >   	 | j                   S # t        $ r | j                         r1| j                  d   }||| _         |cY S t        j                  d       | j                  d      }| j                  d      }d}||}n|}||d|z   dz   z  }|| _         |cY S w xY w)z;
        Returns the message sender, if it exists.
        fromNzQHeader found, but "sender" is not included. Will be generated from other streams.z__substg1.0_0C1Az__substg1.0_5D01z <>)_senderr5   r6   r   r7   r8   r|   )r"   r   textr   results        r*   r   zMessageBase.sendern  s    
	<< 	 #{{62+#/DL''op(();<D))*<=EF|$dUlS00F!DLM+	s    3BABBc                 &    | j                  dd      S )z<
        Returns the message subject, if it exists.
        _subjectz__substg1.0_0037r   r[   s    r*   subjectzMessageBase.subject  s    
 z+=>>r+   c                 &    | j                  dd      S )z5
        Returns the to field, if it exists.
        r   r/   ry   r[   s    r*   r   zMessageBase.to  r   r+   rK   )FFFFN)#__name__
__module____qualname____doc__r   r   rj   r   rI   rO   rX   r6   rc   propertyr   rL   rM   rw   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r+   r*   r   r      s    ')JSW$)d+4+K+K$LB	FK -% -%^ ) ) ' ' , ,    + + P P        0 $ $ K K C C ? ? # # 0 0 $ $, = =  : ? ? + +r+   r   )"email.utilsr   r_   loggingr}   r   imapclient.imapclientr   email.parserr   r   extract_msgr   extract_msg.attachmentr   r   r   extract_msg.compatr	   osextract_msg.msgr
   extract_msg.recipientr   extract_msg.utilsr   r   r   	getLoggerr   r7   
addHandlerNullHandlerr   r   r+   r*   <module>r      sn       	  - . ! V V ( # + F F 
		8	$   %'%%' (B+' B+r+   