7
from urlparse import parse_qs
9
from grackle.error import (
13
UnsupportedDisplayType,
18
SUPPORTED_DISPLAY_TYPES = set(['all', 'text-only', 'headers-only'])
21
SUPPORTED_ORDERS = set(
22
['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
26
def threaded_messages(messages):
30
for message in messages:
31
if message.get('replies') is None:
32
threads[message['message_id']] = [message]
35
pending.append(message)
36
for message in pending:
37
threads[message['replies']].append(message)
38
return threads.values()
41
def get_body_text(message):
42
"""Return the first plain/text messaage part."""
43
if not message.is_multipart():
44
return message.get_payload()
45
for part in email.iterators.typed_subpart_iterator(message, 'multipart'):
46
subparts = part.get_payload()
47
for subpart in subparts:
48
if subpart.get_content_type() == 'text/plain':
49
return subpart.get_payload().strip()
53
def make_json_message(message_id, raw_message, hidden=False):
54
message = email.message_from_string(raw_message)
55
headers = dict(message.items())
57
'message_id': message_id,
59
# This is broken because the in-reply-to must be encoded.
60
# X-Message-ID-Hash is calculated from the Base 32.
61
'thread_id': headers.get('in-reply-to', message_id),
62
'date': headers.get('date'),
63
'subject': headers.get('subject'),
64
'author': headers.get('from'),
67
'replies': headers.get('in-reply-to'),
68
'body': get_body_text(message),
74
"""A memory-backed message store."""
76
def __init__(self, message_archives):
78
self.message_archives = message_archives
81
def is_multipart(message):
82
return isinstance(message['body'], list)
84
def put_message(self, archive_id, message_id, raw_message):
85
# XXX sinzui 2012-02-29: this needs to raise an error
86
# if the th archive_id is invalid, message_id is not base32
87
# or the raw message is not an email.
88
if archive_id not in self.message_archives:
89
raise ArchiveIdNotFound()
91
raise ValueError('raw_message is not a message.')
92
json_message = make_json_message(message_id, raw_message)
93
messages = self.message_archives[archive_id]
94
messages.append(json_message)
96
def get_messages(self, archive_id, query_string):
97
"""Return matching messages.
99
:param archive_id: The archive to retrieve from.
100
:param query_string: Contains 'parameters', which is a JSON-format
101
string describing parameters.
103
query = parse_qs(query_string)
104
parameters = simplejson.loads(query['parameters'][0])
105
order = parameters.get('order')
106
messages = self.message_archives[archive_id]
107
if order is not None:
108
if order not in SUPPORTED_ORDERS:
109
raise UnsupportedOrder
110
elif order.startswith('thread_'):
111
threaded = threaded_messages(messages)
113
if order == 'thread_subject':
114
threaded.sort(key=lambda t: t[0]['subject'])
115
if order == 'thread_oldest':
116
threaded.sort(key=lambda t: min(m['date'] for m in t))
117
if order == 'thread_newest':
118
threaded.sort(key=lambda t: max(m['date'] for m in t))
119
for thread in threaded:
120
messages.extend(thread)
122
messages.sort(key=lambda m: m[order])
123
display_type = parameters.get('display_type', 'all')
124
if display_type not in SUPPORTED_DISPLAY_TYPES:
125
raise UnsupportedDisplayType
126
if 'date_range' in parameters:
128
start_date, end_date = parameters['date_range'].split('..')
129
if not start_date or not end_date:
130
raise UnparsableDateRange
132
raise UnparsableDateRange
134
for message in messages:
135
if (not parameters['include_hidden'] and message['hidden']):
137
if ('message_ids' in parameters
138
and message['message_id'] not in parameters['message_ids']):
140
if ('date_range' in parameters
141
and (message['date'] < start_date
142
or message['date'] > end_date)):
144
message = dict(message)
145
if 'headers' in parameters:
147
(k, v) for k, v in message['headers'].iteritems()
148
if k in parameters['headers'])
149
message['headers'] = headers
150
if display_type == 'headers-only':
152
elif display_type == 'text-only' and self.is_multipart(message):
154
part.get_payload() for part in message['body']
155
if part.get_content_type() == 'text/plain']
156
message['body'] = '\n\n'.join(text_parts)
157
elif display_type == 'all' and self.is_multipart(message):
158
parts = [str(part.get_payload()) for part in message['body']]
159
message['body'] = '\n\n'.join(parts)
160
max_body = parameters.get('max_body_length')
161
if max_body is not None and display_type != 'headers-only':
162
message['body'] = message['body'][:max_body]
163
new_messages.append(message)
164
messages = new_messages
165
limit = parameters.get('limit', 100)
166
memo = parameters.get('memo')
167
message_id_indices = dict(
168
(m['message_id'], idx) for idx, m in enumerate(messages))
172
start = message_id_indices[memo.encode('rot13')]
174
previous_memo = messages[start - 1]['message_id'].encode('rot13')
177
end = min(start + limit, len(messages))
178
if end < len(messages):
179
next_memo = messages[end]['message_id'].encode('rot13')
182
messages = messages[start:end]
185
'messages': messages,
186
'next_memo': next_memo,
187
'previous_memo': previous_memo
191
def hide_message(self, archive_id, message_id, query_string):
192
"""Change the visbility of a message in an archive.
194
:param archive_id: The archive to retrieve from.
195
:param query_string: Contains 'parameters', which is a JSON-format
196
string describing parameters.
198
query = parse_qs(query_string)
199
parameters = simplejson.loads(query['parameters'][0])
200
hidden = parameters['hidden']
201
messages = self.message_archives[archive_id]
202
for message in messages:
203
if message['message_id'] == message_id:
204
message['hidden'] = hidden
206
'message_id': message_id,
210
raise MessageIdNotFound