/
message.py
178 lines (155 loc) · 8.55 KB
/
message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import logging
from ..fields import BooleanField, Base64Field, TextField, MailboxField, MailboxListField, CharField
from ..properties import ReferenceItemId
from ..version import EXCHANGE_2010
from .base import BaseReplyItem
from .item import Item, AUTO_RESOLVE, SEND_TO_NONE, SEND_ONLY, SEND_AND_SAVE_COPY
log = logging.getLogger(__name__)
class Message(Item):
"""
MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/message-ex15websvcsotherref
"""
ELEMENT_NAME = 'Message'
LOCAL_FIELDS = [
MailboxField('sender', field_uri='message:Sender', is_read_only=True, is_read_only_after_send=True),
MailboxListField('to_recipients', field_uri='message:ToRecipients', is_read_only_after_send=True,
is_searchable=False),
MailboxListField('cc_recipients', field_uri='message:CcRecipients', is_read_only_after_send=True,
is_searchable=False),
MailboxListField('bcc_recipients', field_uri='message:BccRecipients', is_read_only_after_send=True,
is_searchable=False),
BooleanField('is_read_receipt_requested', field_uri='message:IsReadReceiptRequested',
is_required=True, default=False, is_read_only_after_send=True),
BooleanField('is_delivery_receipt_requested', field_uri='message:IsDeliveryReceiptRequested',
is_required=True, default=False, is_read_only_after_send=True),
Base64Field('conversation_index', field_uri='message:ConversationIndex', is_read_only=True),
CharField('conversation_topic', field_uri='message:ConversationTopic', is_read_only=True),
# Rename 'From' to 'author'. We can't use fieldname 'from' since it's a Python keyword.
MailboxField('author', field_uri='message:From', is_read_only_after_send=True),
CharField('message_id', field_uri='message:InternetMessageId', is_read_only_after_send=True),
BooleanField('is_read', field_uri='message:IsRead', is_required=True, default=False),
BooleanField('is_response_requested', field_uri='message:IsResponseRequested', default=False, is_required=True),
TextField('references', field_uri='message:References'),
MailboxListField('reply_to', field_uri='message:ReplyTo', is_read_only_after_send=True, is_searchable=False),
MailboxField('received_by', field_uri='message:ReceivedBy', is_read_only=True),
MailboxField('received_representing', field_uri='message:ReceivedRepresenting', is_read_only=True),
# Placeholder for ReminderMessageData
]
FIELDS = Item.FIELDS + LOCAL_FIELDS
__slots__ = tuple(f.name for f in LOCAL_FIELDS)
def send(self, save_copy=True, copy_to_folder=None, conflict_resolution=AUTO_RESOLVE,
send_meeting_invitations=SEND_TO_NONE):
# Only sends a message. The message can either be an existing draft stored in EWS or a new message that does
# not yet exist in EWS.
if not self.account:
raise ValueError('%s must have an account' % self.__class__.__name__)
if self.id:
res = self.account.bulk_send(ids=[self], save_copy=save_copy, copy_to_folder=copy_to_folder)
if len(res) != 1:
raise ValueError('Expected result length 1, but got %s' % res)
if isinstance(res[0], Exception):
raise res[0]
# The item will be deleted from the original folder
self.id, self.changekey = None, None
self.folder = copy_to_folder
return None
# New message
if copy_to_folder:
if not save_copy:
raise AttributeError("'save_copy' must be True when 'copy_to_folder' is set")
# This would better be done via send_and_save() but lets just support it here
self.folder = copy_to_folder
return self.send_and_save(conflict_resolution=conflict_resolution,
send_meeting_invitations=send_meeting_invitations)
if self.account.version.build < EXCHANGE_2010 and self.attachments:
# Exchange 2007 can't send attachments immediately. You need to first save, then attach, then send.
# This is done in send_and_save(). send() will delete the item again.
self.send_and_save(conflict_resolution=conflict_resolution,
send_meeting_invitations=send_meeting_invitations)
return None
res = self._create(message_disposition=SEND_ONLY, send_meeting_invitations=send_meeting_invitations)
if res:
raise ValueError('Unexpected response in send-only mode')
return None
def send_and_save(self, update_fields=None, conflict_resolution=AUTO_RESOLVE,
send_meeting_invitations=SEND_TO_NONE):
# Sends Message and saves a copy in the parent folder. Does not return an ItemId.
if self.id:
self._update(
update_fieldnames=update_fields,
message_disposition=SEND_AND_SAVE_COPY,
conflict_resolution=conflict_resolution,
send_meeting_invitations=send_meeting_invitations
)
else:
if self.account.version.build < EXCHANGE_2010 and self.attachments:
# Exchange 2007 can't send-and-save attachments immediately. You need to first save, then attach, then
# send. This is done in save().
self.save(update_fields=update_fields, conflict_resolution=conflict_resolution,
send_meeting_invitations=send_meeting_invitations)
self.send(save_copy=False, conflict_resolution=conflict_resolution,
send_meeting_invitations=send_meeting_invitations)
else:
res = self._create(
message_disposition=SEND_AND_SAVE_COPY,
send_meeting_invitations=send_meeting_invitations
)
if res:
raise ValueError('Unexpected response in send-only mode')
def create_reply(self, subject, body, to_recipients=None, cc_recipients=None, bcc_recipients=None):
if not self.account:
raise ValueError('%s must have an account' % self.__class__.__name__)
if not self.id:
raise ValueError('%s must have an ID' % self.__class__.__name__)
if to_recipients is None:
if not self.author:
raise ValueError("'to_recipients' must be set when message has no 'author'")
to_recipients = [self.author]
return ReplyToItem(
account=self.account,
reference_item_id=ReferenceItemId(id=self.id, changekey=self.changekey),
subject=subject,
new_body=body,
to_recipients=to_recipients,
cc_recipients=cc_recipients,
bcc_recipients=bcc_recipients,
)
def reply(self, subject, body, to_recipients=None, cc_recipients=None, bcc_recipients=None):
self.create_reply(
subject,
body,
to_recipients,
cc_recipients,
bcc_recipients
).send()
def create_reply_all(self, subject, body):
if not self.account:
raise ValueError('%s must have an account' % self.__class__.__name__)
if not self.id:
raise ValueError('%s must have an ID' % self.__class__.__name__)
to_recipients = list(self.to_recipients) if self.to_recipients else []
if self.author:
to_recipients.append(self.author)
return ReplyAllToItem(
account=self.account,
reference_item_id=ReferenceItemId(id=self.id, changekey=self.changekey),
subject=subject,
new_body=body,
to_recipients=to_recipients,
cc_recipients=self.cc_recipients,
bcc_recipients=self.bcc_recipients,
)
def reply_all(self, subject, body):
self.create_reply_all(subject, body).send()
class ReplyToItem(BaseReplyItem):
"""MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/replytoitem"""
ELEMENT_NAME = 'ReplyToItem'
__slots__ = tuple()
class ReplyAllToItem(BaseReplyItem):
"""MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/replyalltoitem"""
ELEMENT_NAME = 'ReplyAllToItem'
__slots__ = tuple()
class ForwardItem(BaseReplyItem):
"""MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/forwarditem"""
ELEMENT_NAME = 'ForwardItem'
__slots__ = tuple()