5
'SUPPORTED_DISPLAY_TYPES',
11
from urlparse import parse_qs
13
from grackle.error import (
18
UnsupportedDisplayType,
23
SUPPORTED_DISPLAY_TYPES = set(['all', 'text-only', 'headers-only'])
26
SUPPORTED_ORDERS = set(
27
['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
31
def threaded_messages(messages):
35
for message in messages:
36
if message.get('replies') is None:
37
threads[message['message_id']] = [message]
40
pending.append(message)
41
for message in pending:
42
threads[message['replies']].append(message)
43
return threads.values()
46
def get_body_text(message):
47
"""Return the first plain/text messaage part."""
48
if not message.is_multipart():
49
return message.get_payload()
50
for part in email.iterators.typed_subpart_iterator(message, 'multipart'):
51
subparts = part.get_payload()
52
for subpart in subparts:
53
if subpart.get_content_type() == 'text/plain':
54
return subpart.get_payload().strip()
58
def make_json_message(message_id, raw_message, hidden=False):
59
message = email.message_from_string(raw_message)
60
headers = dict(message.items())
62
'message_id': message_id,
64
# This is broken because the in-reply-to must be encoded.
65
# X-Message-ID-Hash is calculated from the Base 32.
66
'thread_id': headers.get('in-reply-to', message_id),
67
'date': headers.get('date'),
68
'subject': headers.get('subject'),
69
'author': headers.get('from'),
72
'replies': headers.get('in-reply-to'),
73
'body': get_body_text(message),
79
"""A memory-backed message store."""
81
def __init__(self, message_archives):
83
self.message_archives = message_archives
86
def is_multipart(message):
87
return isinstance(message['body'], list)
89
def put_archive(self, archive_id, raw_archive=None):
90
# XXX sinzui 2012-02-29: this needs to raise an error
91
# if the th archive_id is invalid, or the raw archive is not mbox.
92
if archive_id in self.message_archives:
93
raise ArchiveIdExists()
94
self.message_archives[archive_id] = []
96
def put_message(self, archive_id, message_id, raw_message):
97
# XXX sinzui 2012-02-29: this needs to raise an error
98
# if the th archive_id is invalid, message_id is not base32
99
# or the raw message is not an email.
100
if archive_id not in self.message_archives:
101
raise ArchiveIdNotFound()
103
raise ValueError('raw_message is not a message.')
104
json_message = make_json_message(message_id, raw_message)
105
messages = self.message_archives[archive_id]
106
messages.append(json_message)
108
def get_messages(self, archive_id, query_string):
109
"""Return matching messages.
111
:param archive_id: The archive to retrieve from.
112
:param query_string: Contains 'parameters', which is a JSON-format
113
string describing parameters.
115
query = parse_qs(query_string)
116
parameters = simplejson.loads(query['parameters'][0])
117
order = parameters.get('order')
118
messages = self.message_archives[archive_id]
119
if order is not None:
120
if order not in SUPPORTED_ORDERS:
121
raise UnsupportedOrder
122
elif order.startswith('thread_'):
123
threaded = threaded_messages(messages)
125
if order == 'thread_subject':
126
threaded.sort(key=lambda t: t[0]['subject'])
127
if order == 'thread_oldest':
128
threaded.sort(key=lambda t: min(m['date'] for m in t))
129
if order == 'thread_newest':
130
threaded.sort(key=lambda t: max(m['date'] for m in t))
131
for thread in threaded:
132
messages.extend(thread)
134
messages.sort(key=lambda m: m[order])
135
display_type = parameters.get('display_type', 'all')
136
if display_type not in SUPPORTED_DISPLAY_TYPES:
137
raise UnsupportedDisplayType
138
if 'date_range' in parameters:
140
start_date, end_date = parameters['date_range'].split('..')
141
if not start_date or not end_date:
142
raise UnparsableDateRange
144
raise UnparsableDateRange
146
for message in messages:
147
if (not parameters['include_hidden'] and message['hidden']):
149
if ('message_ids' in parameters
150
and message['message_id'] not in parameters['message_ids']):
152
if ('date_range' in parameters
153
and (message['date'] < start_date
154
or message['date'] > end_date)):
156
message = dict(message)
157
if 'headers' in parameters:
159
(k, v) for k, v in message['headers'].iteritems()
160
if k in parameters['headers'])
161
message['headers'] = headers
162
if display_type == 'headers-only':
164
elif display_type == 'text-only' and self.is_multipart(message):
166
part.get_payload() for part in message['body']
167
if part.get_content_type() == 'text/plain']
168
message['body'] = '\n\n'.join(text_parts)
169
elif display_type == 'all' and self.is_multipart(message):
170
parts = [str(part.get_payload()) for part in message['body']]
171
message['body'] = '\n\n'.join(parts)
172
max_body = parameters.get('max_body_length')
173
if max_body is not None and display_type != 'headers-only':
174
message['body'] = message['body'][:max_body]
175
new_messages.append(message)
176
messages = new_messages
177
limit = parameters.get('limit', 100)
178
memo = parameters.get('memo')
179
message_id_indices = dict(
180
(m['message_id'], idx) for idx, m in enumerate(messages))
184
start = message_id_indices[memo.encode('rot13')]
186
previous_memo = messages[start - 1]['message_id'].encode('rot13')
189
end = min(start + limit, len(messages))
190
if end < len(messages):
191
next_memo = messages[end]['message_id'].encode('rot13')
194
messages = messages[start:end]
197
'messages': messages,
198
'next_memo': next_memo,
199
'previous_memo': previous_memo
203
def hide_message(self, archive_id, message_id, query_string):
204
"""Change the visbility of a message in an archive.
206
:param archive_id: The archive to retrieve from.
207
:param query_string: Contains 'parameters', which is a JSON-format
208
string describing parameters.
210
query = parse_qs(query_string)
211
parameters = simplejson.loads(query['parameters'][0])
212
hidden = parameters['hidden']
213
messages = self.message_archives[archive_id]
214
for message in messages:
215
if message['message_id'] == message_id:
216
message['hidden'] = hidden
218
'message_id': message_id,
222
raise MessageIdNotFound