
    3fį                     N   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 ddlmZ ddlmZ dd	lmZmZmZmZmZmZmZmZmZmZ  ej6                  e      Z G d
 de      Z G d de      Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d deee      Z$y)    N)Enum)Path)BeautifulSoup)parse   )Event)Category)
ApiComponentAttachableMixinBaseAttachmentBaseAttachmentsCaseEnumHandleRecipientsMixinImportanceLevelOutlookWellKnowFolderNames	Recipient
TrackerSetc                       e Zd ZdZdZdZy)RecipientTypetoccbccN)__name__
__module____qualname__TOCCBCC     J/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/O365/message.pyr   r      s    	B	B
Cr    r   c                        e Zd ZdZdZdZdZdZy)MeetingMessageTypemeetingRequestmeetingCancelledmeetingAcceptedmeetingTentativelyAcceptedmeetingDeclinedN)r   r   r   MeetingRequestMeetingCancelledMeetingAcceptedMeetingTentativelyAcceptedMeetingDeclinedr   r    r!   r#   r#   "   s    %N)'O!='Or    r#   c                       e Zd ZdZdZdZy)Flag
notFlaggedcompleteflaggedN)r   r   r   
NotFlaggedCompleteFlaggedr   r    r!   r/   r/   *   s    JHGr    r/   c                       e Zd ZdddZy)MessageAttachment/messages/{id}/attachments /messages/{id}/attachments/{ida})attach
attachmentN)r   r   r   
_endpointsr   r    r!   r7   r7   0   s    .8Jr    r7   c                   N    e Zd ZdddddZeZddZded	efd
ZdefdZ	defdZ
y)MessageAttachmentsr8   r9   z'/messages/{id}/attachments/{ida}/$valuez./messages/{id}/attachments/createUploadSession)attachmentsr;   get_mimecreate_upload_sessionNc                 (   | j                  |      }|sy|t        d      }nt        |t              st        |      }|j                  s|j	                  d      }|j                  d      5 }|j                  |       	 ddd       y# 1 sw Y   yxY w)zSaves this message as and EML to the file system
        :param MessageAttachment attachment: the MessageAttachment to store as eml.
        :param Path or str to_path: the path where to store this file
        FNmessage_eml.eml.emlwbT)get_mime_contentr   
isinstancesuffixwith_suffixopenwrite)selfr;   to_pathmime_contentfile_objs        r!   save_as_emlzMessageAttachments.save_as_eml@   s    
 ,,Z8?,-Ggt,w-~~))&1G\\$ 	8NN<(	 	 	s   ,BBr;   returnc                 F   |r+t        |t              r|j                  |j                  dk7  rt	        d      | j
                  j                  }|t        d      | j                  | j                  j                  d      j                  ||j                              }|S )zA Returns the url used to get the MIME contents of this attachmentitemz@Must provide a saved "item" attachment of type MessageAttachment9Attempting to get the mime contents of an unsaved messager@   )idida)rG   r7   attachment_idattachment_type
ValueError_parent	object_idRuntimeError	build_urlr<   getformat)rL   r;   msg_idurls       r!   _get_mime_urlz MessageAttachments._get_mime_urlV   s     j*;<''/))V3R  ''>K  nnOO
+22z77 3 

 
r    c                     | j                  |      }| j                  j                  j                  |      }|sy|j                  S )z,Returns the MIME contents of this attachmentN)rb   rZ   conr^   content)rL   r;   ra   responses       r!   rF   z#MessageAttachments.get_mime_contento   s@       ,<<##'',r    c                    | j                  |      }| j                  j                  j                  d      j	                  d      }|j                  d       d| d}| j                  j                  j                  |      }|sy|j                         j                  di       }|r4 | j                  j                  d	d| j                  i| j                  |iS y)
z6 Returns a Message object out an eml attached message item_attachment_type#z$valuez	?$expand=z/itemNrS   parentr   )rb   protocolkeyword_data_storer^   removeprefixremovesuffixrZ   rd   json	__class___cloud_data_key)rL   r;   ra   item_attachment_keywordrf   content_items         r!   get_eml_as_objectz$MessageAttachments.get_eml_as_object{   s       , #'--"B"B"F"FG]"^"k"klo"p!!(+,I6M5NeT<<##'',}}**626)4<<))ff$BVBVXdAeffr    N)r   r   r   r<   r7   _attachment_constructorrP   strrb   rF   rt   r   r    r!   r>   r>   7   sP    38=!Q	J 0,(9 c 2
 +< 
 ,= r    r>   c                        e Zd ZdZ fdZd Zd Zd Zd Ze	d        Z
ddd	d
ZdddZd Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Zd Z xZS )MessageFlagzA flag on a messagec                    t         |   |j                  |j                         || _        t
        j                  |j                  | j                  d      d            | _	        |j                  | j                  d      i       }| j                  |      | _        |j                  | j                  d      i       }| j                  |      | _        |j                  | j                  d      i       }| j                  |      | _        y)zAn flag on a message
        Not available on Outlook Rest Api v2 (only in beta)

        :param parent: parent of this
        :type parent: Message
        :param dict flag_data: flag data from cloud
        )rk   main_resource
flagStatusr0   startDateTimedueDateTimecompletedDateTimeN)super__init__rk   r{   _MessageFlag__messager/   
from_valuer^   _cc_MessageFlag__status_parse_date_time_time_zone_MessageFlag__start_MessageFlag__due_date_MessageFlag__completed)rL   rj   	flag_data	start_objdue_date_objcompleted_date_objrp   s         r!   r   zMessageFlag.__init__   s     	&//AUAUVMM$((<0,?
 MM$((?";R@	66yA }}TXXm%<bA99,G&]]4884G+H"M::;MNr    c                 ,    t        | j                        S ru   )rw   r   rL   s    r!   __repr__zMessageFlag.__repr__   s    4==!!r    c                 "    | j                         S ru   r   r   s    r!   __str__zMessageFlag.__str__       }}r    c                     | j                   S ru   )
is_flaggedr   s    r!   __bool__zMessageFlag.__bool__   s    r    c                 N    | j                   j                  j                  d       y)zXUpdate the track_changes on the message to reflect a
        needed update on this fieldflagN)r   _track_changesaddr   s    r!   r   zMessageFlag._track_changes   s     	%%))&1r    c                     | j                   S ru   )r   r   s    r!   statuszMessageFlag.status   s    }}r    N)
start_datedue_datec                   t         j                  | _        |xs t        j                  j                         }|xs t        j                  j                         }|j                  &|j                  | j                  j                        }|j                  &|j                  | j                  j                        }|| _
        || _        | j                          y)zSets this message as flagged
        :param start_date: the start datetime of the followUp
        :param due_date: the due datetime of the followUp
        Ntzinfo)r/   r5   r   dtdatetimenowr   replacerk   timezoner   r   r   )rL   r   r   s      r!   set_flaggedzMessageFlag.set_flagged   s    
 42;;??#4
0r{{0$#++4==3I3I+JJ??"''t}}/E/E'FH!"r    )completition_datec                   t         j                  | _        |xs t        j                  j                         }|j                  &|j                  | j                  j                        }|| _
        | j                          y)zwSets this message flag as completed
        :param completition_date: the datetime this followUp was completed
        Nr   )r/   r4   r   r   r   r   r   r   rk   r   r   r   )rL   r   s     r!   set_completedzMessageFlag.set_completed   sc     -B1B##+ 1 9 9AWAW 9 X,r    c                 x    t         j                  | _        d| _        d| _        d| _        | j                          y)zSets this message as un flaggedN)r/   r3   r   r   r   r   r   r   s    r!   delete_flagzMessageFlag.delete_flag   s/    r    c                     | j                   S )ziThe start date of the message flag.

        :getter: get the start_date
        :type: datetime
        )r   r   s    r!   r   zMessageFlag.start_date   s     ||r    c                     | j                   S )zeThe due date of the message flag.

        :getter: get the due_date
        :type: datetime
        )r   r   s    r!   r   zMessageFlag.due_date   s     r    c                     | j                   S )zsThe completion date of the message flag.

        :getter: get the completion_date
        :type: datetime
        )r   r   s    r!   r   zMessageFlag.completition_date   s     r    c                 :    | j                   t        j                  u S )zaIs the flag completed.

        :getter: get the is_completed status
        :type: bool
        )r   r/   r4   r   s    r!   is_completedzMessageFlag.is_completed   s     }}--r    c                 v    | j                   t        j                  u xs | j                   t        j                  u S )zYIs item flagged.

        :getter: get the is_flagged status
        :type: bool
        )r   r/   r5   r4   r   s    r!   r   zMessageFlag.is_flagged  s)     }},N0NNr    c                 (   | j                  d      | j                  | j                  j                        i}| j                  t        j                  u rv| j
                  | j                  | j
                        nd|| j                  d      <   | j                  | j                  | j                        nd|| j                  d      <   | j                  t        j                  u r-| j                  | j                        || j                  d      <   |S )z4Returns this data as a dict to be sent to the serverr|   Nr}   r~   r   )
r   r   valuer/   r5   r   _build_date_time_time_zoner   r4   r   )rL   datas     r!   to_api_datazMessageFlag.to_api_data  s    &1D1D(EF==DLL( <<+ //= /*+ ??. //@ -() ==DMM)262Q2Q  3D-./ r    )r   r   r   __doc__r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   r   __classcell__rp   s   @r!   ry   ry      s    O2"2
   )-t   26 	         . . O Or    ry   c                       e Zd ZdZdddddddd	d
dddZddd fd
Zd Zd Zd Ze	d        Z
e
j                  d        Z
e	d        Ze	d        Ze	d        Zej                  d        Ze	d        Ze	d        Ze	d        Zej                  d        Ze	d        Ze	d        Ze	d         Ze	d!        Ze	d"        Ze	d#        Ze	d$        Zej                  d%        Ze	d&        Ze	d'        Ze	d(        Ze	d)        Ze	d*        Zej                  d+        Zd, Ze	d-        Zej                  d.        Ze	d/        Z e j                  d0        Z e	d1        Z!e!j                  d2        Z!e	d3        Z"e	d4        Z#e	d5        Z$e	d6        Z%e	d7        Z&e&j                  d8        Z&d9 Z'dJd:Z(dKd;Z)dKd<Z*d= Z+d> Z,d? Z-d@ Z.dA Z/dB Z0dC Z1e2jf                  fdDZ4dE Z5dF Z6dG Z7dH Z8dJdIZ9 xZ:S )LMessagezQManagement of the process of sending, receiving, reading, and
    editing emails.z	/messagesz/mailFolders/{id}/messagesz	/sendMailz/messages/{id}/sendz/messages/{id}z/messages/{id}/movez/messages/{id}/copyz/messages/{id}/createReplyz/messages/{id}/createReplyAllz/messages/{id}/createForwardz/messages/{id}/$value)create_draftcreate_draft_folder	send_mail
send_draftget_messagemove_messagecopy_messagecreate_replycreate_reply_allforward_messager@   N)rj   rd   c                   |r|rt        d      |r|j                  n|| _        |j                  dd      xs |rt        |dd      nd}t        |   |r|j                  n|j                  d      |dd       |j                  d      }|j                  | j                  i       }| j                  }t        |	      | _        |j                   |d
      |j                  dd            | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        | j                  j$                  }| j                  r$t'        | j                        j)                  |      nd| _        | j                  r$t'        | j                        j)                  |      nd| _        | j                   r$t'        | j                         j)                  |      nd| _        | j"                  r$t'        | j"                        j)                  |      nd| _        t+        | g       | _        | j,                  j/                  | j                  |j                   |d      g       i       |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      i       }	|	j                   |d      d      | _        |	j                   |d      d      | _        |j                   |d      i       }
|
j                   |d      d      | _        |
j                   |d      d      | _        |r&| j>                  r| j@                  jC                          | jE                  |j                   |d      d       |d            | _#        | jI                  |j                   |d      g        |d            | _%        | jI                  |j                   |d      g        |d            | _&        | jI                  |j                   |d       g        |d             | _'        | jI                  |j                   |d!      g        |d!            | _(        |j                   |d"      g       | _)        tU        jV                  |j                   |d#      d$      xs d$      | _,        |j                   |d%      d      | _-        |j                   |d&      d      | _.        |j                   |d'      d      | _/        |j                   |d(      g       | _0        |j                   |d)      d*      }|jc                  d+d,      }|d*k7  rtd        jW                  |      nd| _3        |j                   |d-      |j                  d.d/            | _4        |j                   |d0      d      | _5        |j                   |d1      d      | _6        |j                   |d2      d      | _7        |j                   |d3      i       }tq        | |4      | _9        |j                   |d5      d      | _:        |j                   |d6      d      | _;        |j                   |d7      g       | _<        y)8a&  Makes a new message wrapper for sending and receiving messages.

        :param parent: parent folder/account to create the message in
        :type parent: mailbox.Folder or Account
        :param Connection con: connection to use if no parent specified
        :param Protocol protocol: protocol to use if no parent specified
         (kwargs)
        :param str main_resource: use this resource instead of parent resource
         (kwargs)
        :param bool download_attachments: whether or not to
         download attachments (kwargs)
        z*Need a parent or a connection but not bothr{   Nrk   subjectmessage_type)rk   r{   attachment_name_propertyrX   download_attachments)casingrU   r[   inferenceClassificationcreatedDateTimelastModifiedDateTimereceivedDateTimesentDateTime)rj   r?   r?   hasAttachmentsF bodyPreviewbodyre   contentTypeHTML
uniqueBodyfrom)fieldtoRecipientsccRecipientsbccRecipientsreplyTo
categories
importancenormalisReadisReadReceiptRequestedisDeliveryReceiptRequestedsingleValueExtendedPropertiesmeetingMessageTypenone
TenativelyTentativelyisDraftis_draftTconversationIdconversationIndexparentFolderIdr   )rj   r   internetMessageIdwebLinkinternetMessageHeaders)=rY   rd   popgetattrr   r   rk   r^   rq   r   r   r   r[   "_Message__inference_classification_Message__created_Message__modified_Message__received_Message__sentr   r   
astimezoner>   _Message__attachmentsr   _Message__has_attachments_Message__subject_Message__body_preview_Message__body	body_type_Message__unique_bodyunique_body_typehas_attachmentsr?   r   _recipient_from_cloud_Message__sender_recipients_from_cloud_Message__to_Message__cc_Message__bcc_Message__reply_to_Message__categoriesr   r   _Message__importance_Message__is_read#_Message__is_read_receipt_requested'_Message__is_delivery_receipt_requested*_Message__single_value_extended_propertiesr   r#   _Message__meeting_message_type_Message__is_draftconversation_idconversation_index	folder_idry   _Message__flaginternet_message_idweb_link_Message__message_headers)rL   rj   rd   kwargsr{   r   
cloud_datar   local_tzr   unique_body
meeting_mtr   rp   s                r!   r   zMessage.__init__5  s    cIJJ!'6::S 

?D9 
6<GFOT2$ 	 	(.V__FJJz4J'%.*	 	 	
  &zz*@AZZ 4 4b9
XX )3#4&**[$2OP*4..()4+
' $+<(=tD$..,B)CTJ$..,>)?F nnR%7>==)):>..E$..!,,X6d 	 <@??E$//"--h7PT 	 <@??E$//"--h7PT 	 BFeDKK(33H=RV/tL!!:>>"]2CR#HI	
 ",3C0De!L#9r:(nnR->C~~bj"-hhr)}b1}v
 !nnR%5r:(__R	]B? +}v!
  D$8$811322NN2f:t,BvJ 3 
 //NN2n-r2"^:L 0 
	 //NN2n-r2"^:L 0 
	 00NN2o.32o;N 1 

 55NN2i="-R	] 6 
 'NN2l+;R@+66NN2l+X6B(
 $8d;+5>>'(%,
( 0:~~+,e0
, 3=.../3
/
  ^^B';$<fE
  ''mD
 :Dv9M))*5SW 	#
 %..I

:t8TU)~~b1A.BDI",..4G1H$"O#+;(<dCNN2f:r2	!C $.>>"5H2I2#N "r)}b9 ",3K0Lb!Qr    c                 "    | j                         S ru   r   r   s    r!   r   zMessage.__str__  r   r    c                 8    dj                  | j                        S )NzSubject: {})r_   r   r   s    r!   r   zMessage.__repr__  s    ##DLL11r    c                 4    | j                   |j                   k(  S ru   )r[   )rL   others     r!   __eq__zMessage.__eq__  s    ~~00r    c                     | j                   S )zCheck if the message is read or not

        :getter: Get the status of message read
        :setter: Mark the message as read
        :type: bool
        )r  r   s    r!   is_readzMessage.is_read       ~~r    c                 H    || _         | j                  j                  d       y )Nr   )r  r   r   rL   r   s     r!   r&  zMessage.is_read  s    )r    c                     | j                   du rS| j                  j                         dk(  r6t        d | j	                         j                  d      D              rd| _         | j                   S )zGCheck if the message contains attachments

        :type: bool
        Fr   c              3   ^   K   | ]%  }|j                  d d      j                  d       ' yw)srcr   zcid:N)r^   
startswith).0imgs     r!   	<genexpr>z*Message.has_attachments.<locals>.<genexpr>  s.       r"--f5s   +-r/  T)r   r  upperanyget_body_soupfind_allr   s    r!   r  zMessage.has_attachments  se     !!U*t~~/C/C/E/O --/88?  *.&%%%r    c                     | j                   S )zECheck if the message is marked as draft

        :type: bool
        )r  r   s    r!   r   zMessage.is_draft  s     r    c                     | j                   S )zSubject of the email message

        :getter: Get the current subject
        :setter: Assign a new subject
        :type: str
        )r   r   s    r!   r   zMessage.subject  r'  r    c                 H    || _         | j                  j                  d       y )Nr   )r   r   r   r)  s     r!   r   zMessage.subject   s    	*r    c                     | j                   S )zReturns the body preview)r   r   s    r!   body_previewzMessage.body_preview  s     """r    c                     | j                   S )zBody of the email message

        :getter: Get body text of current message
        :setter: set html body of the message
        :type: str
        )r   r   s    r!   r   zMessage.body
  s     {{r    c                     | j                   S )zMessage is focused or not)r   r   s    r!   inference_classificationz Message.inference_classification  s     ...r    c                 t   | j                   r|sd| _         n| j                  dk(  rMt        | j                   d      }|j                  j	                  dt        |d             t        |      | _         n+dj                  |d| j                   f      | _         n|| _         | j                  j                  d       y )Nr   htmlhtml.parserr   
r   )	r   r  bsr   insertrw   joinr   r   )rL   r   soups      r!   r   zMessage.body  s    ;; 6)$++}5		  Bum$<=!$i ggudDKK&@ADK'r    c                     | j                   S )zlThe unique body of this message

            Requires a select to retrieve it.

        :rtype: str
        )r  r   s    r!   r  zMessage.unique_body(  s     !!!r    c                     | j                   S )zCreated time of the message)r   r   s    r!   createdzMessage.created2  s     ~~r    c                     | j                   S )zMessage last modified time)r   r   s    r!   modifiedzMessage.modified7       r    c                     | j                   S )zMessage received time)r   r   s    r!   receivedzMessage.received<  rJ  r    c                     | j                   S )zMessage sent time)r   r   s    r!   sentzMessage.sentA       {{r    c                     | j                   S )zList of attachments)r   r   s    r!   r?   zMessage.attachmentsF  s     !!!r    c                     | j                   S )zSender of the message

        :getter: Get the current sender
        :setter: Update the from address with new value
        :type: str or Recipient
        )r  r   s    r!   senderzMessage.senderK  s     }}r    c                    t        |t              r"|j                  | |_        d|_        || _        n>t        |t
              r#|| j                  _        d| j                  _        nt        d      | j                  j                  d       y)z<sender is a property to force to be always a Recipient classNr   r   z6sender must be an address string or a Recipient object)rG   r   rZ   _fieldr  rw   addressnamerY   r   r   r)  s     r!   rR  zMessage.senderU  so     eY'}}$ $%!DMs#$)DMM!!#DMMUVV'r    c                     | j                   S )z'TO' list of recipients)r  r   s    r!   r   z
Message.tod       yyr    c                     | j                   S )z'CC' list of recipients)r	  r   s    r!   r   z
Message.cci  rX  r    c                     | j                   S )z'BCC' list of recipients)r
  r   s    r!   r   zMessage.bccn  s     zzr    c                     | j                   S )zReply to address)r  r   s    r!   reply_tozMessage.reply_tos  rJ  r    c                     | j                   S )zCategories of this message

        :getter: Current list of categories
        :setter: Set new categories for the message
        :type: list[str] or str
        )r  r   s    r!   r   zMessage.categoriesx          r    c                    t        |t              r`g | _        |D ]S  }t        |t              r&| j                  j	                  |j
                         9| j                  j	                  |       U nGt        |t              r	|g| _        n.t        |t              r|j
                  g| _        nt        d      | j                  j                  d       y )Nzcategories must be a listr   )
rG   listr  r	   appendrV  rw   rY   r   r   )rL   r   vals      r!   r   zMessage.categories  s    eT" "D 2c8,%%,,SXX6%%,,S1	2
 s#!&Dx(!&D899-r    c                     t        |t              r&| j                  j                  |j                         n| j                  j                  |       | j
                  j                  d       y)z7Adds a category to this message current categories listr   N)rG   r	   r  ra  rV  r   r   )rL   categorys     r!   add_categoryzMessage.add_category  sN     h)$$X]]3$$X.-r    c                     | j                   S )zImportance of the message

        :getter: Get the current priority of the message
        :setter: Set a different importance level
        :type: str or ImportanceLevel
        )r  r   s    r!   r   zMessage.importance  r^  r    c                     t        |t              r|nt        j                  |      | _        | j                  j                  d       y )Nr   )rG   r   r   r  r   r   r)  s     r!   r   zMessage.importance  s?     %1  ++E2 	
 	-r    c                     | j                   S )zif the read receipt is requested for this message

        :getter: Current state of isReadReceiptRequested
        :setter: Set isReadReceiptRequested for the message
        :type: bool
        )r  r   s    r!   is_read_receipt_requestedz!Message.is_read_receipt_requested  s     ///r    c                 Z    t        |      | _        | j                  j                  d       y )Nr   )boolr  r   r   r)  s     r!   ri  z!Message.is_read_receipt_requested  s#    +/;( 89r    c                     | j                   S )zif the delivery receipt is requested for this message

        :getter: Current state of isDeliveryReceiptRequested
        :setter: Set isDeliveryReceiptRequested for the message
        :type: bool
        )r  r   s    r!   is_delivery_receipt_requestedz%Message.is_delivery_receipt_requested  s     333r    c                 Z    t        |      | _        | j                  j                  d       y )Nr   )rk  r  r   r   r)  s     r!   rm  z%Message.is_delivery_receipt_requested  s#    /3E{, <=r    c                     | j                   S )zIf this message is a EventMessage, returns the
        meeting type: meetingRequest, meetingCancelled, meetingAccepted,
        meetingTentativelyAccepted, meetingDeclined
        r  r   s    r!   meeting_message_typezMessage.meeting_message_type  s     ***r    c                     | j                   duS )zlReturns if this message is of type EventMessage
        and therefore can return the related event.
        Nrp  r   s    r!   is_event_messagezMessage.is_event_message  s    
 **$66r    c                     | j                   S )zThe Message Flag instance)r  r   s    r!   r   zMessage.flag  rO  r    c                     | j                   S )r   )r  r   s    r!    single_value_extended_propertiesz(Message.single_value_extended_properties  s     666r    c                     | j                   S )zCustom message headers

            List of internetMessageHeaders, see definition: https://learn.microsoft.com/en-us/graph/api/resources/internetmessageheader?view=graph-rest-1.0

        :type: list[dict[str, str]]
        )r  r   s    r!   message_headerszMessage.message_headers  s     %%%r    c                 ~    t        |t              st        d      || _        | j                  j                  d       y )Nz"message_header" must be a listrx  )rG   r`  rY   r  r   r   r)  s     r!   rx  zMessage.message_headers  s5    %&>??!& 12r    c                     | j                   D ]  }|d   |k(  s||d<    y  | j                   j                  ||d       y )NrV  r   )rV  r   )r  ra  )rL   rV  r   headers       r!   add_message_headerzMessage.add_message_header  sJ    ,, 	Ff~%"'w	 	%%te&DEr    c                    | j                   } |d      | j                   |d       |d      | j                   |d      | j                  i |d       || j                  j
                         |d      | j                  j                          |d      | j                   |d      | j                  i}| j                  r3| j                  D cg c]  }| j                  |       c}| |d	      <   ng | |d	      <   | j                  r3| j                  D cg c]  }| j                  |       c}| |d
      <   ng | |d
      <   | j                  r3| j                  D cg c]  }| j                  |       c}| |d      <   ng | |d      <   | j                  r3| j                  D cg c]  }| j                  |       c}| |d      <   ng | |d      <   | j                  r#| j                  j                         | |d      <   | j                   r:| j                   j"                  r$| j                  | j                         | |d      <   | j$                  sd|xs i v r| j$                  | |d      <   | j&                  r| j(                  s| j&                  | |d      <   | j*                  rJ| j*                  j-                  t.        j0                  j2                        j5                         | |d      <   | j6                  rJ| j6                  j-                  t.        j0                  j2                        j5                         | |d      <   | j8                  rJ| j8                  j-                  t.        j0                  j2                        j5                         | |d      <   t;        | j                        | |d      <   | j<                  | |d      <   | j(                  | |d      <   | j>                  | |d      <   | j@                  | |d      <   | jB                  r| jB                  | |d      <   |r&tE        |jG                               D ]
  }||vs||=  |S c c}w c c}w c c}w c c}w )a$   Returns a dict representation of this message prepared to be sent
        to the cloud

        :param restrict_keys: a set of keys to restrict the returned
         data to
        :type restrict_keys: dict or set
        :return: converted to cloud based keys
        :rtype: dict
        r   r   r   re   r   r   r   r   r   r   r   r   r?   r   r   rU   r   r   r   r   r   r   r   r   r   )$r   r   r  r   r   r   r   r   ri  rm  r   _recipient_to_cloudr   r   r\  r?   rR  rU  r   r[   r  rG  r   r   r   utc	isoformatrL  rN  rk  r&  r  r  rx  r`  keys)rL   restrict_keysr   message	recipientkeys         r!   r   zMessage.to_api_data  s    XX yM4<<vJ=!4>>9tyy* |b!6!67vJ		--/'($*H*H+,d.P.P	
 77<@GG+E/8 ,0+C+CI+N +EGB~&' +-GB~&'77<@GG+E/8 ,0+C+CI+N +EGB~&' +-GB~&'88=AXX,G09 -1,D,DY,O ,GGB'( ,.GB'(==37==&B&/ '+&>&>y&I &BGByM" &(GByM")-)9)9)E)E)GGB}%&;;4;;.."&":":4;;"GGBvJ??l}/BC(,GB|$%>>$// !%GBtH||151H1HKKOO2%%.Y[ ,-.}}26--2J2JKKOO3%%.Y[ -./yy.2ii.B.BKKOO/%%.Y[ >*+,01A1A,BGB'()$(LLGBxL!%)__GByM",0,@,@GB'(),0NNGB'()484H4HGB/01GLLN+ %m+% o+E
+E
,G
&Bs   Q'Q,!Q1+Q6c                    | j                   r| j                  st        d      S | j                  ro| j                   rc| j                  | j                  j                  d      j                  | j                               }| j                  r| j                          d}nc| j                  | j                  j                  d            }| j                  d      | j                         i}|du rd|| j                  d      <   | j                  j                  ||	      }|sy| j                   sd
n| j                   | _         d| _        y)z Sends this message

        :param bool save_to_sent_folder: whether or not to save it to
         sent folder
        :return: Success / Failure
        :rtype: bool
        zXNot possible to send a message that is not new or a draft. Use Reply or Forward instead.r   rU   Nr   r  FsaveToSentItemsr   sent_messageT)r[   r  r\   r]   r<   r^   r_   r   
save_draftr   r   rd   post)rL   save_to_sent_folderra   r   rf   s        r!   sendzMessage.sendS  s    >>$// !P Q Q ??t~~..##L188DNN8KMC""!D ..!4!4[!ABCHHY')9)9);<D"e+49TXX/0188==4=0/3~~4>>r    c                 8   | j                   r| j                  rt        d      |rE| j                  | j                  j                  d      j                  | j                               }nD| j                  | j                  j                  d      j                  | j                               }d| j                  j                  d      i}| j                  j                  ||      }|sy|j                         } | j                  d
d	| i| j                  |iS )z  Creates a new message that is a reply to this message

        :param bool to_all: whether or not to replies to all the recipients
         instead to just the sender
        :return: new message
        :rtype: Message
        zCan't reply to this messager   r  r   Preferprefer_timezone_header)headersNrj   r   )r[   r  r\   r]   r<   r^   r_   rk   get_service_keywordrd   r  ro   rp   rq   )rL   to_allra   r  rf   r  s         r!   replyzMessage.replyx  s     ~~<==..!4!45G!H!O!O>> "P "# $C ..##N3::dnn:MOC T]]>>?WXY88==g=6--/ t~~MTMd.B.BG-LMMr    c                 l   | j                   r| j                  rt        d      | j                  | j                  j                  d      j                  | j                               }| j                  j                  |      }|sy|j                         } | j                  dd| i| j                  |iS )zu  Creates a new message that is a forward this message

        :return: new message
        :rtype: Message
        zCan't forward this messager   r  Nrj   r   )r[   r  r\   r]   r<   r^   r_   rd   r  ro   rp   rq   )rL   ra   rf   r  s       r!   forwardzMessage.forward  s     ~~;<<nnOO 1299T^^9LN 88==%--/ t~~MTMd.B.BG-LMMr    c                    | j                   t        d      | j                  | j                  j	                  d      j                  | j                               }| j                  j                  |      }t        |      S )z[ Deletes a stored message

        :return: Success / Failure
        :rtype: bool
        z'Attempting to delete an unsaved Messager   r  )	r[   r\   r]   r<   r^   r_   rd   deleterk  rL   ra   rf   s      r!   r  zMessage.delete  sk     >>!HIInnOO.555HJ 88??3'H~r    c                 F   | j                   | j                  rt        d      | j                  d      di}| j	                  | j
                  j                  d      j                  | j                               }| j                  j                  ||      }|syd| _
        y)zj Marks this message as read in the cloud

        :return: Success / Failure
        :rtype: bool
        z-Attempting to mark as read an unsaved Messager   Tr   r  r  Fr[   r  r\   r   r]   r<   r^   r_   rd   patchr  rL   r   ra   rf   s       r!   mark_as_readzMessage.mark_as_read  s     >>!T__NOO"D)nnOO.555HJ 88>>#D>1r    c                 F   | j                   | j                  rt        d      | j                  d      di}| j	                  | j
                  j                  d      j                  | j                               }| j                  j                  ||      }|syd| _
        y)zl Marks this message as unread in the cloud

        :return: Success / Failure
        :rtype: bool
        z/Attempting to mark as unread an unsaved Messager   Fr   r  r  Tr  r  s       r!   mark_as_unreadzMessage.mark_as_unread  s     >>!T__PQQ"E*nnOO.555HJ 88>>#D>1r    c                    | j                   t        d      | j                  | j                  j	                  d      j                  | j                               }t        |t              r|}nt        |dd      }|st        d      | j                  d      |i}| j                  j                  ||      }|sy	|j                         }|| _        |j	                  d
      | _         y)z Move the message to a given folder

        :param folder: Folder object or Folder id or Well-known name to
         move this message to
        :type folder: str or mailbox.Folder
        :return: Success / Failure
        :rtype: bool
        N%Attempting to move an unsaved Messager   r  r  Must Provide a valid folder_iddestinationIdr  FrU   T)r[   r\   r]   r<   r^   r_   rG   rw   r   r   rd   r  ro   r  rL   folderra   r  r   rf   r  s          r!   movezMessage.move  s     >>!FGGnnOO/66$..6IK fc"IT:I?@@)9588==4=0--/" T*r    c                    | j                   t        d      | j                  | j                  j	                  d      j                  | j                               }t        |t              r|}nt        |dd      }|st        d      | j                  d      |i}| j                  j                  ||      }|sy|j                         } | j                  d
d	| i| j                  |iS )z Copy the message to a given folder

        :param folder: Folder object or Folder id or Well-known name to
         copy this message to
        :type folder: str or mailbox.Folder
        :returns: the copied message
        :rtype: Message
        Nr  r   r  r  r  r  r  rj   r   )r[   r\   r]   r<   r^   r_   rG   rw   r   r   rd   r  ro   rp   rq   r  s          r!   copyzMessage.copy  s     >>!FGGnnOO/66$..6IK fc"IT:I?@@)9588==4=0--/ t~~MTMd.B.BG-LMMr    c                    | j                   rH| j                  s;| j                  d      | j                  d      | j                  d      | j                  d      h}| j                  D ch c]	  }||v s| }}|sy| j	                  | j
                  j                  d      j                  | j                               }| j                  |      }| j                  j                  ||	      }|sy
| j                  j                          t        j                  j                         j                  | j                   j"                        | _        y| j'                         S c c}w )aI  Saves changes to a message.
        If the message is a new or saved draft it will call 'save_draft' otherwise
        this will save only properties of a message that are draft-independent such as:

            - is_read
            - category
            - flag

        :return: Success / Failure
        :rtype: bool
        r   r   r   r   Tr   r  r  r  Fr   )r[   r  r   r   r]   r<   r^   r_   r   rd   r  clearr   r   r   r   rk   r   r   r  )rL   allowed_changestcchangesra   r   rf   s          r!   save_messagezMessage.save_message-  s    >>$//#xx1488L3I#xx/)1DFO$($7$7Qb2;PrQGQ..!4!4]!C!J!Jdnn!J!]^C##'#:Dxx~~c~5H%%' kkoo/77t}}?U?U7VDO ??$$) Rs   +	E&5E&c                    | j                   r| j                  sy| j                  | j                  j	                  d      j                  | j                               }| j                  j                  }| j                  | j                        }|j                  | j                  d      d       | j                  j                          n| j                  st        d      |xs t        j                   }t#        |t              r|j$                  }n5t#        |t&              s%t)        |dt        j                   j$                        }| j                  | j                  j	                  d	      j                  |            }| j                  j*                  }| j                         }|sy |||
      }|sy| j                  j-                          | j                   se|j/                         }|j	                  | j                  d      d      | _         |j	                  | j                  d      d      | _        |j	                  | j                  d      d      | _        |j	                  | j                  d      d      | _        | j2                  r8t7        | j2                        j9                  | j:                  j<                        nd| _        | j4                  r8t7        | j4                        j9                  | j:                  j<                        nd| _        |j	                  | j                  d      d      | _        yt@        jB                  jE                         jG                  | j:                  j<                        | _        y)z Save this message as a draft on the cloud

        :param target_folder: name of the drafts folder
        :return: Success / Failure
        :rtype: bool
        Tr   r  r  r?   Nz*Only draft messages can be saved as draftsr  r   r  FrU   r   r   r   r   r   r   )$r[   r   r]   r<   r^   r_   rd   r  r   r   r   r?   _update_attachments_to_cloudr  r\   r   DRAFTSrG   r   rw   r   r  r  ro   r  r   r   r   r   rk   r   r  r   r   r   r   )rL   target_folderra   methodr   rf   r  s          r!   r  zMessage.save_draftS  s    >>&&..##M299T^^9LNCXX^^F##$2E2E#FDHHTXXm, 99; ??"#OPP)N-G-N-NM-)CD - 3 3s3 '{(B(I(I(O(O!Q ..##$9:AA$ B &'C XX]]F##%D#D)!!#~~mmoG$[[$>DN$[[2B)CTJDN$[[2C)DTJDN%kk$((3I*J4PDO ,0>> #4>>2==&&(?C N ,0?? $DOO4??&&(@D O $KK(;R@DM  !kkoo/77t}}?U?U7VDOr    c                     | j                   j                         dk7  r| j                  S 	 t        | j                  d      }|j                  j                  S # t
        $ r | j                  cY S w xY w)zt Parse the body html and returns the body text using bs4

        :return: body as text
        :rtype: str
        r   r?  )r  r1  r   rA  textr\   )rL   rD  s     r!   get_body_textzMessage.get_body_text  sc     >>!V+99	"dii/D 99>>!  	99	s   A A/.A/c                 j    | j                   j                         dk7  ryt        | j                  d      S )z Returns the beautifulsoup4 of the html body

        :return: BeautifulSoup object of body
        :rtype: BeautifulSoup
        r   Nr?  )r  r1  rA  r   r   s    r!   r3  zMessage.get_body_soup  s-     >>!V+dii//r    c                    | j                   sy| j                         j                  d      }| j                  | j                  j                  d      j                  | j                              }| j                  j                  ||j                               }|sy|j                         }|j                  | j                  d            }t        dd| i| j                  |iS )z= If this is a EventMessage it should return the related EventNeventr   r  )paramsrj   r   )rs  qexpandr]   r<   r^   r_   r[   rd   	as_paramsro   r   r   rq   )rL   queryra   rf   r   
event_datas         r!   	get_eventzMessage.get_event  s     $$ (nnT__00?FF$..FYZ88<<EOO,=<>}}XXdhhw/0
GDGT%9%9:$FGGr    c                    | j                   t        d      | j                  | j                  j	                  d      j                  | j                               }| j                  j	                  |      }|sy|j                  S )z+ Returns the MIME contents of this message NrT   r@   r  )r[   r\   r]   r<   r^   r_   rd   re   r  s      r!   rF   zMessage.get_mime_content  sk    >>!Z[[nnT__00<CCt~~CVW88<<$r    c                 $   |t        d      }nt        |t               st        |      }|j                  s|j                  d      }| j	                         }|r,|j                  d      5 }|j                  |       ddd       yy# 1 sw Y   yxY w)z Saves this message as and EML to the file system
        :param Path or str to_path: the path where to store this file
        NrC   rD   rE   TF)r   rG   rH   rI   rF   rJ   rK   )rL   rM   rN   rO   s       r!   rP   zMessage.save_as_eml  s    
 ?,-Ggt,w-~~))&1G,,.d# -x|,--s   *BBru   )T);r   r   r   r   r<   r   r   r   r$  r   r&  setterr  r   r   r9  r   r<  r  rG  rI  rL  rN  r?   rR  r   r   r   r\  r   re  r   ri  rm  rq  rs  r   rv  rx  r|  r   r  r  r  r  r  r  r  r  r  r   r  r  r  r3  r  rF   rP   r   r   s   @r!   r   r   #  s    $; +'--4;9+J "&4 PRd21   ^^* * & &     ^^+ + # #   / / 
[[( ( " "         " "   ]]( (         ! ! . . . ! ! . . 0 0 %%: &: 4 4 #))> *> + + 7 7   7 7 & & 3 3FPd#JN<N* ,,"H ND$%L (B'H'H DL" 	0H* r    r   )%r   r   loggingenumr   pathlibr   bs4r   rA  dateutil.parserr   calendarr   rd  r	   utilsr
   r   r   r   r   r   r   r   r   r   	getLoggerr   logr   r#   r/   r7   r>   ry   r   r   r    r!   <module>r     s        $ !     g!D ( (8  U UpR, RhHlO-B Hr    