
    Ng                        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dS )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d0d            Zed,d            Zed1d            Zed.d            Zed2d            Zd3dZd4d!Zed5d#            Zed/d$            Zd6d&Zd'S )7MessagezAAn Outlook email message loaded from an OXMSG format (.msg) file.storage
m.StorageTc                    || _         d S N)_storage)selfr   s     I/var/www/html/ai-engine/env/lib/python3.11/site-packages/oxmsg/message.py__init__zMessage.__init__%   s        msg_filestr | IO[bytes] | bytesreturnc                   t          |t                    rt          |          nt          |          }|sHt          |t                    rt          j                            |          nd}t          | d          t          |          5 }t          j
        |          }ddd           n# 1 swxY w Y    | |          }|                                 |S )zLoad an instance from `msg_file`.

        `msg_file` can be a file-path, a file-like object, or the bytes of the MSG file.
        )datar   z 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(C(C\I8$$$$S[I\I\ 	  	G5?#5N5N^rw''111T^HEEEFFFx   	1C"+C00L	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 s<  s   B//B36B3intc                    | j         d         S )z&Number of attachments on this message.   )_header_prop_valuesr   s    r   attachment_countzMessage.attachment_count=   s     '**r   tuple[Attachment, ...]c                D    t          |                                           S )z6Attachment object for each attachment in this message.)tuple_iter_attachmentsr4   s    r   attachmentszMessage.attachmentsB   s     T++--...r   
str | Nonec                J    | j                             t          j                  S )z;The plain-text body of this message; `None` if not present.)
propertiesstr_prop_valuecPID_BODYr4   s    r   bodyzMessage.bodyG   s     --aj999r   c                    | j                             t          j                  x}r|                    | j         j                  S | j                             t          j                  x}r|S dS )z;The HTML body of this message if present, `None` otherwise.N)r=   binary_prop_valuer?   PID_HTMLdecodebody_encodingr>   PID_BODY_HTML)r   
html_byteshtml_strs      r   	html_bodyzMessage.html_bodyL   sf     ::1:FFF: 	D$$T_%BCCC55aoFFF8 	Otr   dict[str, str]c                *    t          | j                  S )zOFrom, To, Content-Type, etc. headers for this message as {name: value} mapping.)dict_message_headersr4   s    r   message_headerszMessage.message_headersW   s     D)***r   r#   c                N    | j                             t          j                  pdS )zBOutlook Message Class identifier like "IPM.Note" for this message. )r=   r>   r?   PID_MESSAGE_CLASSr4   s    r   message_classzMessage.message_class\   s"     --a.ABBHbHr   r   c                    | j         S )z7Provides access to the properties of this OXMSG object.)_propertiesr4   s    r   r=   zMessage.propertiesa   s     r   tuple[Recipient, ...]c                D    t          |                                           S )z6`Recipient` object for each recipient of this message.)r8   _iter_recipientsr4   s    r   
recipientszMessage.recipientsf   s     T**,,---r   c                z   | j         d         x}r|S | j        }|                    t          j                  pd                                }|rd| dnd}|                    t          j                  p|                    t          j                  }|r| d| dn|                    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.
        fromrQ   "z" <>)	rN   r=   r>   r?   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     !% 5f == 	%$$(():;;ArHHJJ#+38$$Q%?@@ 
EDXDX%E
 E
 &+b$!!!!!!0D0DQEa0b0bbr   dt.datetime | Nonec                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.
        )r=   date_prop_valuer?   PID_CLIENT_SUBMIT_TIMEr4   s    r   	sent_datezMessage.sent_date   s     ..q/GHHHr   c                N    | j                             t          j                  pdS )zSubject line of this message.rQ   )r=   r>   r?   PID_SUBJECTr4   s    r   subjectzMessage.subject   s!     --am<<BBr   tuple[int, int, int, int]c                P    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_bytesr4   s    r   r3   zMessage._header_prop_values   s$     }Wdm&KCRC&PQQQr   Iterator[Attachment]c                H    d | j                                         D             S )zzGenerate `Attachment` object for each attachment in this message.

        Should need to be called at most once.
        c              3  4   K   | ]}t          |          V  d S r   r   .0r   s     r   	<genexpr>z,Message._iter_attachments.<locals>.<genexpr>   s*      \\
7##\\\\\\r   )r   iter_attachment_storagesr4   s    r   r9   zMessage._iter_attachments   s'    
 ]\4=3Y3Y3[3[\\\\r   Iterator[Recipient]c                H    d | j                                         D             S )zxGenerate `Recipient` object for each recipient in this message.

        Should need to be called at most once.
        c              3  4   K   | ]}t          |          V  d S r   r   r{   s     r   r}   z+Message._iter_recipients.<locals>.<genexpr>   s*      ZZw	'""ZZZZZZr   )r   iter_recipient_storagesr4   s    r   rX   zMessage._iter_recipients   s'    
 [Z$-2W2W2Y2YZZZZr   email.message.Messagec                    | j                             t          j                  pd}t          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`.
        rQ   )r=   r>   r?   PID_TRANSPORT_MESSAGE_HEADERSrh   parserHeaderParserparsestr)r   headerss     r   rN   zMessage._message_headers   sD     /001PQQWUW|((**33G<<<r   c                B    t          | j        t          j                  S )N)properties_header_offset)r   r   mMSG_HDR_OFFSETr4   s    r   rU   zMessage._properties   s    $-!BRSSSSr   Nonec                    dS )zJRaise if this message is invalid for one of a variety of possible reasons.N r4   s    r   r)   zMessage._validate   s      r   N)r   r   )r   r   r   r   )r   r0   )r   r6   )r   r;   )r   rK   )r   r#   )r   r   )r   rV   )r   rj   )r   rr   )r   rx   )r   r   )r   r   )r   r   )__name__
__module____qualname____doc__r   classmethodr/   propertyr5   r:   rA   rJ   rO   rS   r=   rY   ri   rn   rq   r   r3   r9   rX   rN   rU   r)   r   r   r   r   r   "   sp       KK           [( + + + X+ / / / X/ : : : X:    X + + + X+ I I I XI       X  . . . X. c c c Xc. I I I XI C C C XC 	R 	R 	R \	R] ] ] ][ [ [ [ = = = \= T T T \TY Y Y Y Y Yr   r   ) r   
__future__r   datetimedtemail.messagerh   email.parserr$   ru   typingr   r   olefiler   r   oxmsg.attachmentr	   oxmsg.domainr
   r?   r   r   oxmsg.propertiesr   oxmsg.recipientr   oxmsg.storager   
oxmsg.utilr   r   r   r   r   <module>r      sM    # " " " " "             				          ( ( ( ( ( ( ( ( ' ' ' ' ' ' ' ' ' ' ' ' # # # # # # ' ' ' ' ' ' % % % % % % ! ! ! ! ! ! # # # # # #WY WY WY WY WY WY WY WY WY WYr   