
    :Qg                        d Z ddlmZ 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mZ ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ  G d d      Zy)a  The `Message` object is the primary interface object in `python-oxmsg`.

This object is returned by the `Message.load()` classmethod:

```python
>>> from oxmsg import Message

>>> msg = Message.load("message.msg")
>>> msg.subject
'Agenda for the Acme meeting'
```
    )annotationsN)IOIterator)	OleFileIO	isOleFile
Attachment)	constants)model)
Properties	Recipient)Storage)lazypropertyc                  >   e Zd ZdZddZedd       Zedd       Zedd       Z	edd       Z
edd       Zedd       Zedd	       Zedd
       Zedd       Zedd       Zedd       Zedd       Zed d       Zd!dZd"dZed#d       Zedd       Zd$dZy)%MessagezAAn Outlook email message loaded from an OXMSG format (.msg) file.c                    || _         y N)_storage)selfstorages     J/var/www/html/answerous/venv/lib/python3.12/site-packages/oxmsg/message.py__init__zMessage.__init__%   s	        c                p   t        |t              rt        |      n
t        |      }|s?t        |t              rt        j
                  j                  |      nd}t        | d      t        |      5 }t        j                  |      }ddd        |       }|j                          |S # 1 sw Y   #xY w)zLoad an instance from `msg_file`.

        `msg_file` can be a file-path, a file-like object, or the bytes of the MSG file.
        )datamsg_filez is not an Outlook MSG fileN)
isinstancebytesr   strospathbasename
ValueErrorr   r   from_ole	_validate)clsr   
is_olefilefilenameoleroot_storager   s          r   loadzMessage.load(   s     )38U(CI8$S[I\ 	 5?#5Nrww''1T^Hz)DEFFx  	1C"++C0L	1 < 	1 	1s   4B,,B5c                     | j                   d   S )z&Number of attachments on this message.   )_header_prop_valuesr   s    r   attachment_countzMessage.attachment_count=   s     ''**r   c                4    t        | j                               S )z6Attachment object for each attachment in this message.)tuple_iter_attachmentsr0   s    r   attachmentszMessage.attachmentsB   s     T++-..r   c                T    | j                   j                  t        j                        S )z;The plain-text body of this message; `None` if not present.)
propertiesstr_prop_valuecPID_BODYr0   s    r   bodyzMessage.bodyG   s     --ajj99r   c                    | j                   j                  t        j                        x}r%|j	                  | j                   j
                        S | j                   j                  t        j                        x}r|S y)z;The HTML body of this message if present, `None` otherwise.N)r7   binary_prop_valuer9   PID_HTMLdecodebody_encodingr8   PID_BODY_HTML)r   
html_byteshtml_strs      r   	html_bodyzMessage.html_bodyL   sc     ::1::FF:F$$T__%B%BCC55aooFF8FOr   c                ,    t        | j                        S )zOFrom, To, Content-Type, etc. headers for this message as {name: value} mapping.)dict_message_headersr0   s    r   message_headerszMessage.message_headersW   s     D))**r   c                \    | j                   j                  t        j                        xs dS )zBOutlook Message Class identifier like "IPM.Note" for this message. )r7   r8   r9   PID_MESSAGE_CLASSr0   s    r   message_classzMessage.message_class\   s$     --a.A.ABHbHr   c                    | j                   S )z7Provides access to the properties of this OXMSG object.)_propertiesr0   s    r   r7   zMessage.propertiesa   s     r   c                4    t        | j                               S )z6`Recipient` object for each recipient of this message.)r3   _iter_recipientsr0   s    r   
recipientszMessage.recipientsf   s     T**,--r   c                   | j                   d   x}r|S | j                  }|j                  t        j                        xs dj                         }|rd| dnd}|j                  t        j                        xs |j                  t        j                        }|r| d| dS |j                  t        j                        S )a  Name and email address of the message sender.

        Like '"John Doe" <john@bigisp.com>'.

        None if it is not recorded in the message. This may occur when the message is a draft or
        system-generated or perhaps in other cases.

        Note that the value of the From: message header is returned when present (usually I expect)
        and may contain multiple addresses separated by commas.
        fromrJ   "z" <>)	rG   r7   r8   r9   PID_SENDER_NAMEstripPID_SENDER_EMAIL_ADDRESSPID_SENDER_SMTP_ADDRESSPID_SENT_REPRESENTING_NAME)r   from_header_valuepropsraw_namenameemails         r   senderzMessage.senderk   s     !% 5 5f ===$$(():):;ArHHJ#+8*B$$Q%?%?@ 
EDXDX%%E
 &+$qq!b0D0DQEaEa0bbr   c                T    | j                   j                  t        j                        S )zWhen this message was submitted by the sender.

        This value will be `None` if the message has not been sent and possibly in other cases.
        )r7   date_prop_valuer9   PID_CLIENT_SUBMIT_TIMEr0   s    r   	sent_datezMessage.sent_date   s     ..q/G/GHHr   c                \    | j                   j                  t        j                        xs dS )zSubject line of this message.rJ   )r7   r8   r9   PID_SUBJECTr0   s    r   subjectzMessage.subject   s"     --amm<BBr   c                \    t        j                  d| j                  j                  dd       S )zThe property values in the MSG-root properties header.

        It is a tuple of the four int values:
         - next_recipient_id
         - next_attachment_id
         - recipient_count
         - attachment_count
        z<8x4IN   )structunpackr   properties_stream_bytesr0   s    r   r/   zMessage._header_prop_values   s&     }}Wdmm&K&KCR&PQQr   c                D    d | j                   j                         D        S )zzGenerate `Attachment` object for each attachment in this message.

        Should need to be called at most once.
        c              3  2   K   | ]  }t        |        y wr   r   .0r   s     r   	<genexpr>z,Message._iter_attachments.<locals>.<genexpr>   s     \
7#\   )r   iter_attachment_storagesr0   s    r   r4   zMessage._iter_attachments   s    
 ]4==3Y3Y3[\\r   c                D    d | j                   j                         D        S )zxGenerate `Recipient` object for each recipient in this message.

        Should need to be called at most once.
        c              3  2   K   | ]  }t        |        y wr   r   rp   s     r   rr   z+Message._iter_recipients.<locals>.<genexpr>   s     Zw	'"Zrs   )r   iter_recipient_storagesr0   s    r   rP   zMessage._iter_recipients   s    
 [$--2W2W2YZZr   c                    | j                   j                  t        j                        xs d}t        j
                  j                         j                  |      S )zFrom, To, Content-Type, etc. headers for this message as Message object.

        This provides case-insensitive access along with some other convenient behaviors not
        available from a `dict`.
        rJ   )r7   r8   r9   PID_TRANSPORT_MESSAGE_HEADERSr`   parserHeaderParserparsestr)r   headerss     r   rG   zMessage._message_headers   sB     //001P1PQWUW||((*33G<<r   c                L    t        | j                  t        j                        S )N)properties_header_offset)r   r   mMSG_HDR_OFFSETr0   s    r   rN   zMessage._properties   s    $--!BRBRSSr   c                     y)zJRaise if this message is invalid for one of a variety of possible reasons.N r0   s    r   r&   zMessage._validate   s    r   N)r   z
m.StorageT)r   zstr | IO[bytes] | bytesreturnr   )r   int)r   ztuple[Attachment, ...])r   z
str | None)r   zdict[str, str])r   r    )r   r   )r   ztuple[Recipient, ...])r   zdt.datetime | None)r   ztuple[int, int, int, int])r   zIterator[Attachment])r   zIterator[Recipient])r   zemail.message.Message)r   None)__name__
__module____qualname____doc__r   classmethodr,   propertyr1   r5   r;   rD   rH   rL   r7   rQ   ra   re   rh   r   r/   r4   rP   rG   rN   r&   r   r   r   r   r   "   sT   K   ( + + / / : :   + + I I     . . c c. I I C C 	R 	R][ = = T TYr   r   ) r   
__future__r   datetimedtemail.messager`   email.parserr!   rk   typingr   r   olefiler   r   oxmsg.attachmentr	   oxmsg.domainr
   r9   r   r   oxmsg.propertiesr   oxmsg.recipientr   oxmsg.storager   
oxmsg.utilr   r   r   r   r   <module>r      sG    #    	   ( ' ' # ' % ! #WY WYr   