a
    ¡˜ëj  ã                   @  s¾   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„ 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                   @  sX  e Zd ZdZddœdd„Zedd dœdd	„ƒZed
dœdd„ƒZeddœdd„ƒZ	eddœdd„ƒZ
eddœdd„ƒZeddœdd„ƒZeddœdd„ƒZeddœdd„ƒZeddœd d!„ƒZeddœd"d#„ƒZed$dœd%d&„ƒZeddœd'd(„ƒZed)dœd*d+„ƒZd,dœd-d.„Zd/dœd0d1„Zed2dœd3d4„ƒZeddœd5d6„ƒZd7dœd8d9„Zd:S );ÚMessagezAAn Outlook email message loaded from an OXMSG format (.msg) file.z
m.StorageT)Ústoragec                 C  s
   || _ d S ©N)Ú_storage)Úselfr   © r   úL/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/oxmsg/message.pyÚ__init__%   s    zMessage.__init__zstr | IO[bytes] | bytes)Úmsg_fileÚreturnc                 C  sŽ   t |tƒrt|d�nt|ƒ}|sHt |tƒr6tj |¡nd}t|› d�ƒ‚t|ƒ�}t	 
|¡}W d  ƒ n1 sp0    Y  | |ƒ}| ¡  |S )z„Load 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   Zfrom_oleÚ	_validate)Úclsr   Z
is_olefileÚfilenameZoleZroot_storager   r   r   r   Úload(   s    ÿ
(zMessage.loadÚint)r   c                 C  s
   | j d S )z&Number of attachments on this message.é   )Ú_header_prop_values©r   r   r   r   Úattachment_count=   s    zMessage.attachment_countztuple[Attachment, ...]c                 C  s   t |  ¡ ƒS )z6Attachment object for each attachment in this message.)ÚtupleÚ_iter_attachmentsr)   r   r   r   ÚattachmentsB   s    zMessage.attachmentsz
str | Nonec                 C  s   | j  tj¡S )z;The plain-text body of this message; `None` if not present.)Ú
propertiesÚstr_prop_valueÚcZPID_BODYr)   r   r   r   ÚbodyG   s    zMessage.bodyc                 C  s:   | j  tj¡ }r | | j j¡S | j  tj¡ }r6|S dS )z;The HTML body of this message if present, `None` otherwise.N)r.   Zbinary_prop_valuer0   ZPID_HTMLÚdecodeÚbody_encodingr/   ZPID_BODY_HTML)r   Z
html_bytesZhtml_strr   r   r   Ú	html_bodyL   s
    zMessage.html_bodyzdict[str, str]c                 C  s
   t | jƒS )zOFrom, To, Content-Type, etc. headers for this message as {name: value} mapping.)ÚdictÚ_message_headersr)   r   r   r   Úmessage_headersW   s    zMessage.message_headersr   c                 C  s   | j  tj¡pdS )zBOutlook Message Class identifier like "IPM.Note" for this message.Ú )r.   r/   r0   ZPID_MESSAGE_CLASSr)   r   r   r   Úmessage_class\   s    zMessage.message_classr   c                 C  s   | j S )z7Provides access to the properties of this OXMSG object.)Ú_propertiesr)   r   r   r   r.   a   s    zMessage.propertiesztuple[Recipient, ...]c                 C  s   t |  ¡ ƒS )z6`Recipient` object for each recipient of this message.)r+   Ú_iter_recipientsr)   r   r   r   Ú
recipientsf   s    zMessage.recipientsc                 C  sx   | j d  }r|S | j}| tj¡p&d ¡ }|r<d|› d�nd}| tj¡pV| tj¡}|rl|› d|› d�S | 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.
        Úfromr8   ú"z" ú<ú>)	r6   r.   r/   r0   ZPID_SENDER_NAMEÚstripZPID_SENDER_EMAIL_ADDRESSZPID_SENDER_SMTP_ADDRESSZPID_SENT_REPRESENTING_NAME)r   Zfrom_header_valueÚpropsZraw_nameÚnameÚemailr   r   r   Úsenderk   s    ÿzMessage.senderzdt.datetime | Nonec                 C  s   | j  tj¡S )z˜When 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.   Zdate_prop_valuer0   ZPID_CLIENT_SUBMIT_TIMEr)   r   r   r   Ú	sent_dateƒ   s    zMessage.sent_datec                 C  s   | j  tj¡pdS )zSubject line of this message.r8   )r.   r/   r0   ZPID_SUBJECTr)   r   r   r   Úsubject‹   s    zMessage.subjectztuple[int, int, int, int]c                 C  s   t  d| jjdd… ¡S )zàThe 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   Zproperties_stream_bytesr)   r   r   r   r(   �   s    
zMessage._header_prop_valueszIterator[Attachment]c                 C  s   dd„ | j  ¡ D ƒS )zzGenerate `Attachment` object for each attachment in this message.

        Should need to be called at most once.
        c                 s  s   | ]}t |ƒV  qd S r   r   ©Ú.0r   r   r   r   Ú	<genexpr>¡   ó    z,Message._iter_attachments.<locals>.<genexpr>)r   Ziter_attachment_storagesr)   r   r   r   r,   œ   s    zMessage._iter_attachmentszIterator[Recipient]c                 C  s   dd„ | j  ¡ D ƒS )zxGenerate `Recipient` object for each recipient in this message.

        Should need to be called at most once.
        c                 s  s   | ]}t |ƒV  qd S r   r   rK   r   r   r   rM   ¨   rN   z+Message._iter_recipients.<locals>.<genexpr>)r   Ziter_recipient_storagesr)   r   r   r   r;   £   s    zMessage._iter_recipientszemail.message.Messagec                 C  s"   | j  tj¡pd}tj ¡  |¡S )zÐFrom, 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`.
        r8   )r.   r/   r0   ZPID_TRANSPORT_MESSAGE_HEADERSrD   ÚparserÚHeaderParserÚparsestr)r   Úheadersr   r   r   r6   ª   s    zMessage._message_headersc                 C  s   t | jtjd�S )N)Zproperties_header_offset)r   r   ÚmZMSG_HDR_OFFSETr)   r   r   r   r:   ´   s    zMessage._propertiesÚNonec                 C  s   dS )zJRaise if this message is invalid for one of a variety of possible reasons.Nr   r)   r   r   r   r"   ¸   s    zMessage._validateN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr%   Úpropertyr*   r-   r1   r4   r7   r9   r.   r<   rE   rF   rG   r   r(   r,   r;   r6   r:   r"   r   r   r   r   r   "   sF   
	r   ) rX   Ú
__future__r   ÚdatetimeÚdtÚemail.messagerD   Úemail.parserr   rI   Útypingr   r   Zolefiler   r   Zoxmsg.attachmentr   Zoxmsg.domainr	   r0   r
   rS   Zoxmsg.propertiesr   Zoxmsg.recipientr   Zoxmsg.storager   Z
oxmsg.utilr   r   r   r   r   r   Ú<module>   s    