-
Notifications
You must be signed in to change notification settings - Fork 8
/
interfaces.py
346 lines (250 loc) · 10.7 KB
/
interfaces.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright (c) 2014 Ian C. Good
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
"""Module defining the interfaces available to pymap backends.
"""
import asyncio
__all__ = ['SessionInterface', 'MailboxInterface', 'MessageInterface']
class SessionInterface(object):
"""Corresponds to a single, authenticated IMAP session."""
def __init__(self, user):
super().__init__()
#: The identity of the authenticated user.
self.user = user
@asyncio.coroutine
def list_mailboxes(self, subscribed=False):
"""List the mailboxes owned by the user.
:param bool subscribed: If true, only list the subscribed mailboxes.
:returns: list of :class:`Mailbox` objects.
"""
raise NotImplementedError
@asyncio.coroutine
def get_mailbox(self, name):
"""Retrieves a :class:`Mailbox` object corresponding to an existing
mailbox owned by the user. Raises an exception if the mailbox does not
yet exist.
:param str name: The name of the mailbox.
:rtype: :class:`Mailbox`
:raises: :class:`~pymap.exceptions.MailboxNotFound`
"""
raise NotImplementedError
@asyncio.coroutine
def create_mailbox(self, name):
"""Creates a new mailbox owned by the user.
.. seealso: `RFC 3501 6.3.3
<https://tools.ietf.org/html/rfc3501#section-6.3.3>`_
:param str name: The name of the mailbox.
:raises: :class:`~pymap.exceptions.MailboxConflict`
"""
raise NotImplementedError
@asyncio.coroutine
def delete_mailbox(self, name):
"""Deletes the mailbox owned by the user.
.. seealso: `RFC 3501 6.3.4
<https://tools.ietf.org/html/rfc3501#section-6.3.4>`_
:param str name: The name of the mailbox.
:raises: :class:`~pymap.exceptions.MailboxNotFound`,
:class:`~pymap.exceptions.MailboxHasChildren`
"""
raise NotImplementedError
@asyncio.coroutine
def rename_mailbox(self, before_name, after_name):
"""Renames the mailbox owned by the user.
.. seealso: `RFC 3501 6.3.5
<https://tools.ietf.org/html/rfc3501#section-6.3.5>`_
:param str before_name: The name of the mailbox before the rename.
:param str after_name: The name of the mailbox after the rename.
:raises: :class:`~pymap.exceptions.MailboxNotFound`,
:class:`~pymap.exceptions.MailboxConflict`
"""
raise NotImplementedError
@asyncio.coroutine
def subscribe(self, name):
"""Mark the given folder name as subscribed, whether or not the given
folder name currently exists.
.. seealso: `RFC 3501 6.3.6
<https://tools.ietf.org/html/rfc3501#section-6.3.6>`_
:param str name: The name of the folder.
"""
raise NotImplementedError
@asyncio.coroutine
def unsubscribe(self, name):
"""Remove the given folder name from the subscription list, whether or
not the given folder name currently exists.
.. seealso: `RFC 3501 6.3.6
<https://tools.ietf.org/html/rfc3501#section-6.3.6>`_
:param str name: The name of the folder.
"""
raise NotImplementedError
class MailboxInterface(object):
"""Corresponds to a mailbox available to the IMAP session."""
#: The flags defined in the mailbox.
flags = None
#: The subset of :attr:`.flags` that may be changed permanently.
permanent_flags = None
#: If ``True``, the mailbox is read-only.
readonly = False
#: Number of total messages in the mailbox.
exists = None
#: Number of recent messages in the mailbox.
recent = None
#: Number of unseen messages in the mailbox.
unseen = None
#: The sequence number of the first unseen message.
first_unseen = None
#: The predicted next message UID.
next_uid = None
#: The UID validity value.
uid_validity = None
def __init__(self, name):
super().__init__()
#: The name of the mailbox.
self.name = name
@asyncio.coroutine
def get_messages_by_seq(self, seqs):
"""Get a list of :class:`Message` objects corresponding to given
sequence set.
:param seqs: List of items, as described in
:class:`~pymap.parsing.specials.SequenceSet`.
:returns: List of :class:`Message` objects.
"""
raise NotImplementedError
@asyncio.coroutine
def get_messages_by_uid(self, uids):
"""Get a list of :class:`Message` objects corresponding to the given
UIDs.
:param seqs: List of items, as described in
:class:`~pymap.parsing.specials.SequenceSet`.
:returns: List of :class:`Message` objects.
"""
raise NotImplementedError
@asyncio.coroutine
def append_message(self, message, flag_list=None, when=None):
"""Appends a message to the end of the mailbox.
.. seealso: `RFC 3501 6.3.11
<https://tools.ietf.org/html/rfc3501#section-6.3.11>`_
:param bytes message: The contents of the message.
:param flag_list: List of flag bytestrings.
:param when: The internal time associated with the message.
:type when: :py:class:`~datetime.datetime`
:raises: :class:`~pymap.exceptions.MailboxNotFound`,
:class:`~pymap.exceptions.AppendFailure`
"""
raise NotImplementedError
@asyncio.coroutine
def expunge(self):
"""All messages that are marked as deleted are immediately expunged
from the mailbox.
.. seealso: `RFC 3501 6.4.3
<https://tools.ietf.org/html/rfc3501#section-6.4.3>`_
:raises: :class:`~pymap.exceptions.MailboxReadOnly`
"""
raise NotImplementedError
@asyncio.coroutine
def copy(self, messages, mailbox):
"""Copy a set of messages into the given mailbox.
.. seealso: `RFC 3501 6.4.7
<https://tools.ietf.org/html/rfc3501#section-6.4.7>`_
:param messages: List of :class:`Message` objects.
:param str name: Name of the mailbox to copy messages into.
:raises: :class:`~pymap.exceptions.MailboxNotFound`
"""
raise NotImplementedError
@asyncio.coroutine
def search(self, keys):
"""Get a list of :class:`Message` objects in the current mailbox that
meet the given search criteria.
.. seealso: `RFC 3501 7.2.5
<https://tools.ietf.org/html/rfc3501#section-7.2.5>`_
:param list keys: List of :class:`~pymap.parsing.specials.SearchKey`
objects specifying the search criteria.
"""
raise NotImplementedError
@asyncio.coroutine
def update_flags(self, messages, flag_list, mode='replace'):
"""Update the flags for the given set of messages.
.. seealso: `RFC 3501 6.4.6
<https://tools.ietf.org/html/rfc3501#section-6.4.6>`_
:param messages: List of :class:`Message` objects.
:param flag_list: List of flag bytestrings.
:param str mode: Update mode, can be ``replace``, ``add`` or
``delete``.
"""
raise NotImplementedError
@asyncio.coroutine
def poll(self):
"""Checks the mailbox for any changes. This first time this is called
on the object, the ``exists`` and ``recent`` keys are always returned.
:returns: Dictionary with anything that has changed since the last
:meth:`.poll`.
"""
raise NotImplementedError
class MessageInterface(object):
"""Corresponds to a single message, as it exists in a single mailbox."""
def __init__(self, seq, uid):
super().__init__()
#: The message's sequence number in relation to other messages in the
#: mailbox.
self.seq = seq
#: The message's unique identifier in the mailbox.
self.uid = uid
@asyncio.coroutine
def fetch_size(self):
"""Returns the total size of the RFC-2822 representation of the
message.
:rtype: int
"""
raise NotImplementedError
@asyncio.coroutine
def fetch_header(self, header, section=None):
"""Returns the list of values associated with the given header, like
you would get from the :meth:`~email.message.Message.get_all` method.
:param str header: The name of the header to return values for,
case-insensitive.
:param section: Optional nested index of the MIME part. For example,
``[2, 3]`` would index the 2nd MIME part and then the
3rd MIME sub-part within it. The default ``None``
refers to the top-level MIME part.
:returns: List of header values, as *str* objects.
"""
raise NotImplementedError
@asyncio.coroutine
def fetch_part(self, headers_only=False, section=None):
"""Returns the bytestring of the given MIME part.
:param bool headers_only: Only include the headers in the result.
:param section: Optional nested index of the MIME part. For example,
``[2, 3]`` would index the 2nd MIME part and then the
3rd MIME sub-part within it. The default ``None``
refers to the top-level MIME part.
:rtype: bytes
"""
raise NotImplementedError
@asyncio.coroutine
def fetch_internal_date(self):
"""Returns the internal time associated with the message.
:rtype: datetime.datetime
"""
raise NotImplementedError
@asyncio.coroutine
def fetch_flags(self):
"""Returns the flags associated with the message.
:returns: List of bytestrings.
"""
raise NotImplementedError