0
|
1 /* Copyright (C) 2002 Timo Sirainen */
|
|
2
|
|
3 #include "lib.h"
|
|
4 #include "temp-string.h"
|
|
5 #include "rfc822-tokenize.h"
|
|
6 #include "message-parser.h"
|
|
7 #include "message-content-parser.h"
|
|
8 #include "imap-envelope.h"
|
|
9 #include "imap-bodystructure.h"
|
|
10
|
|
11 #define EMPTY_BODYSTRUCTURE \
|
|
12 "(\"text\" \"plain\" (\"charset\" \"us-ascii\") NIL NIL \"7bit\" 0 0)"
|
|
13
|
|
14 typedef struct {
|
|
15 Pool pool;
|
|
16 TempString *str;
|
|
17 char *content_type, *content_subtype;
|
|
18 char *content_type_params;
|
|
19 char *content_transfer_encoding;
|
|
20 char *content_id;
|
|
21 char *content_description;
|
|
22 char *content_disposition;
|
|
23 char *content_disposition_params;
|
|
24 char *content_md5;
|
|
25 char *content_language;
|
|
26
|
|
27 MessagePartEnvelopeData *envelope;
|
|
28 } MessagePartBodyData;
|
|
29
|
|
30 static void part_write_bodystructure(MessagePart *part, TempString *str,
|
|
31 int extended);
|
|
32
|
|
33 static void parse_content_type(const Rfc822Token *tokens,
|
|
34 int count, void *user_data)
|
|
35 {
|
|
36 MessagePartBodyData *data = user_data;
|
|
37 const char *value;
|
|
38 int i;
|
|
39
|
|
40 /* find the content type separator */
|
|
41 for (i = 0; i < count; i++) {
|
|
42 if (tokens[i].token == '/')
|
|
43 break;
|
|
44 }
|
|
45
|
|
46 value = rfc822_tokens_get_value_quoted(tokens, i, FALSE);
|
|
47 data->content_type = p_strdup(data->pool, value);
|
|
48
|
|
49 value = rfc822_tokens_get_value_quoted(tokens+i+1, count-i-1, FALSE);
|
|
50 data->content_subtype = p_strdup(data->pool, value);
|
|
51 }
|
|
52
|
|
53 static void parse_save_params_list(const Rfc822Token *name,
|
|
54 const Rfc822Token *value, int value_count,
|
|
55 void *user_data)
|
|
56 {
|
|
57 MessagePartBodyData *data = user_data;
|
|
58 const char *str;
|
|
59
|
|
60 if (data->str->len != 0)
|
|
61 t_string_append_c(data->str, ' ');
|
|
62
|
|
63 t_string_append_c(data->str, '"');
|
|
64 t_string_append_n(data->str, name->ptr, name->len);
|
|
65 t_string_append(data->str, "\" ");
|
|
66
|
|
67 str = rfc822_tokens_get_value_quoted(value, value_count, FALSE);
|
|
68 t_string_append(data->str, str);
|
|
69 }
|
|
70
|
|
71 static void parse_content_transfer_encoding(const Rfc822Token *tokens,
|
|
72 int count, void *user_data)
|
|
73 {
|
|
74 MessagePartBodyData *data = user_data;
|
|
75 const char *value;
|
|
76
|
|
77 value = rfc822_tokens_get_value_quoted(tokens, count, FALSE);
|
|
78 data->content_transfer_encoding = p_strdup(data->pool, value);
|
|
79 }
|
|
80
|
|
81 static void parse_content_disposition(const Rfc822Token *tokens,
|
|
82 int count, void *user_data)
|
|
83 {
|
|
84 MessagePartBodyData *data = user_data;
|
|
85 const char *value;
|
|
86
|
|
87 value = rfc822_tokens_get_value_quoted(tokens, count, FALSE);
|
|
88 data->content_disposition = p_strdup(data->pool, value);
|
|
89 }
|
|
90
|
|
91 static void parse_content_language(const Rfc822Token *tokens,
|
|
92 int count, void *user_data)
|
|
93 {
|
|
94 MessagePartBodyData *data = user_data;
|
|
95 const char *value;
|
|
96
|
|
97 if (count <= 0)
|
|
98 return;
|
|
99
|
|
100 value = rfc822_tokens_get_value_quoted(tokens, count, FALSE);
|
|
101 data->content_language = p_strdup(data->pool, value);
|
|
102
|
|
103 /* FIXME: a,b,c -> "a" "b" "c" */
|
|
104 }
|
|
105
|
|
106 static void parse_header(MessagePart *part,
|
|
107 const char *name, unsigned int name_len,
|
|
108 const char *value,
|
|
109 unsigned int value_len, void *user_data)
|
|
110 {
|
|
111 Pool pool = user_data;
|
|
112 MessagePartBodyData *part_data;
|
|
113 int parent_rfc822;
|
|
114
|
|
115 parent_rfc822 = part->parent != NULL && part->parent->message_rfc822;
|
|
116 if (!parent_rfc822 && (name_len <= 8 ||
|
|
117 strncasecmp(name, "Content-", 8) != 0))
|
|
118 return;
|
|
119
|
|
120 if (part->user_data == NULL) {
|
|
121 /* initialize message part data */
|
|
122 part->user_data = part_data =
|
|
123 p_new(pool, MessagePartBodyData, 1);
|
|
124 part_data->pool = pool;
|
|
125 }
|
|
126 part_data = part->user_data;
|
|
127
|
|
128 t_push();
|
|
129
|
|
130 /* fix the name to be \0-terminated */
|
|
131 name = t_strndup(name, name_len);
|
|
132
|
|
133 if (strcasecmp(name, "Content-Type") == 0) {
|
|
134 part_data->str = t_string_new(256);
|
|
135 (void)message_content_parse_header(t_strndup(value, value_len),
|
|
136 parse_content_type,
|
|
137 parse_save_params_list,
|
|
138 part_data);
|
|
139 part_data->content_type_params =
|
|
140 p_strdup(pool, part_data->str->str);
|
|
141 } else if (strcasecmp(name, "Content-Transfer-Encoding") == 0) {
|
|
142 (void)message_content_parse_header(t_strndup(value, value_len),
|
|
143 parse_content_transfer_encoding,
|
|
144 NULL, part_data);
|
|
145 } else if (strcasecmp(name, "Content-ID") == 0) {
|
|
146 part_data->content_id = p_strndup(pool, value, value_len);
|
|
147 } else if (strcasecmp(name, "Content-Description") == 0) {
|
|
148 part_data->content_description =
|
|
149 p_strndup(pool, value, value_len);
|
|
150 } else if (strcasecmp(name, "Content-Disposition") == 0) {
|
|
151 part_data->str = t_string_new(256);
|
|
152 (void)message_content_parse_header(t_strndup(value, value_len),
|
|
153 parse_content_disposition,
|
|
154 parse_save_params_list,
|
|
155 part_data);
|
|
156 part_data->content_disposition_params =
|
|
157 p_strdup(pool, part_data->str->str);
|
|
158 } else if (strcasecmp(name, "Content-Language") == 0) {
|
|
159 (void)message_content_parse_header(t_strndup(value, value_len),
|
|
160 parse_content_language, NULL,
|
|
161 part_data);
|
|
162 } else if (strcasecmp(name, "Content-MD5") == 0) {
|
|
163 part_data->content_md5 = p_strndup(pool, value, value_len);
|
|
164 } else if (parent_rfc822) {
|
|
165 /* message/rfc822, we need the envelope */
|
|
166 imap_envelope_parse_header(pool, &part_data->envelope,
|
|
167 name, value, value_len);
|
|
168 }
|
|
169 t_pop();
|
|
170 }
|
|
171
|
|
172 static void part_parse_headers(MessagePart *part, const char *msg,
|
|
173 size_t size, Pool pool)
|
|
174 {
|
|
175 while (part != NULL) {
|
|
176 /* note that we want to parse the header of all
|
|
177 the message parts, multiparts too. */
|
|
178 message_parse_header(part, msg + part->pos.physical_pos,
|
|
179 part->header_size.physical_size,
|
|
180 NULL, parse_header, pool);
|
|
181
|
|
182 if (part->children != NULL)
|
|
183 part_parse_headers(part->children, msg, size, pool);
|
|
184
|
|
185 part = part->next;
|
|
186 }
|
|
187 }
|
|
188
|
|
189 static void part_write_body_multipart(MessagePart *part, TempString *str,
|
|
190 int extended)
|
|
191 {
|
|
192 MessagePartBodyData *data = part->user_data;
|
|
193
|
|
194 if (part->children != NULL)
|
|
195 part_write_bodystructure(part->children, str, extended);
|
|
196 else {
|
|
197 /* no parts in multipart message,
|
|
198 that's not allowed. write a single
|
|
199 0-length text/plain structure */
|
|
200 t_string_append(str, EMPTY_BODYSTRUCTURE);
|
|
201 }
|
|
202
|
|
203 t_string_append_c(str, ' ');
|
|
204 t_string_append(str, data->content_subtype);
|
|
205
|
|
206 if (!extended)
|
|
207 return;
|
|
208
|
|
209 /* BODYSTRUCTURE data */
|
|
210 t_string_append_c(str, ' ');
|
|
211 if (data->content_type_params == NULL)
|
|
212 t_string_append(str, "NIL");
|
|
213 else {
|
|
214 t_string_append_c(str, '(');
|
|
215 t_string_append(str, data->content_type_params);
|
|
216 t_string_append_c(str, ')');
|
|
217 }
|
|
218
|
|
219 t_string_append_c(str, ' ');
|
|
220 if (data->content_disposition == NULL)
|
|
221 t_string_append(str, "NIL");
|
|
222 else {
|
|
223 t_string_append_c(str, '(');
|
|
224 t_string_append(str, data->content_disposition);
|
|
225 if (data->content_disposition_params != NULL) {
|
|
226 t_string_append(str, " (");
|
|
227 t_string_append(str, data->content_disposition_params);
|
|
228 t_string_append_c(str, ')');
|
|
229 }
|
|
230 t_string_append_c(str, ')');
|
|
231 }
|
|
232
|
|
233 t_string_append_c(str, ' ');
|
|
234 if (data->content_language == NULL)
|
|
235 t_string_append(str, "NIL");
|
|
236 else {
|
|
237 t_string_append_c(str, '(');
|
|
238 t_string_append(str, data->content_language);
|
|
239 t_string_append_c(str, ')');
|
|
240 }
|
|
241 }
|
|
242
|
|
243 static void part_write_body(MessagePart *part, TempString *str, int extended)
|
|
244 {
|
|
245 MessagePartBodyData *data = part->user_data;
|
|
246
|
|
247 if (data == NULL) {
|
|
248 /* there was no content headers, use an empty structure */
|
|
249 data = t_new(MessagePartBodyData, 1);
|
|
250 }
|
|
251
|
|
252 /* "content type" "subtype" */
|
|
253 t_string_append(str, NVL(data->content_type, "\"text\""));
|
|
254 t_string_append_c(str, ' ');
|
|
255 t_string_append(str, NVL(data->content_subtype, "\"plain\""));
|
|
256
|
|
257 /* ("content type param key" "value" ...) */
|
|
258 t_string_append_c(str, ' ');
|
|
259 if (data->content_type_params == NULL)
|
|
260 t_string_append(str, "NIL");
|
|
261 else {
|
|
262 t_string_append_c(str, '(');
|
|
263 t_string_append(str, data->content_type_params);
|
|
264 t_string_append_c(str, ')');
|
|
265 }
|
|
266
|
|
267 t_string_printfa(str, " %s %s %s %lu",
|
|
268 NVL(data->content_id, "NIL"),
|
|
269 NVL(data->content_description, "NIL"),
|
|
270 NVL(data->content_transfer_encoding, "\"8bit\""),
|
|
271 (unsigned long) part->body_size.virtual_size);
|
|
272
|
|
273 if (part->text) {
|
|
274 /* text/.. contains line count */
|
|
275 t_string_printfa(str, " %u", part->body_size.lines);
|
|
276 } else if (part->message_rfc822) {
|
|
277 /* message/rfc822 contains envelope + body + line count */
|
|
278 MessagePartBodyData *child_data;
|
|
279
|
|
280 i_assert(part->children != NULL);
|
|
281 i_assert(part->children->next == NULL);
|
|
282
|
|
283 child_data = part->children->user_data;
|
|
284
|
|
285 t_string_append_c(str, ' ');
|
|
286 if (child_data != NULL && child_data->envelope != NULL) {
|
|
287 imap_envelope_write_part_data(child_data->envelope,
|
|
288 str);
|
|
289 } else {
|
|
290 /* buggy message */
|
|
291 t_string_append(str, "NIL");
|
|
292 }
|
|
293 t_string_append_c(str, ' ');
|
|
294 part_write_bodystructure(part->children, str, extended);
|
|
295 t_string_printfa(str, " %u", part->body_size.lines);
|
|
296 }
|
|
297
|
|
298 if (!extended)
|
|
299 return;
|
|
300
|
|
301 /* BODYSTRUCTURE data */
|
|
302
|
|
303 /* "md5" ("content disposition" ("disposition" "params"))
|
|
304 ("body" "language" "params") */
|
|
305 t_string_append_c(str, ' ');
|
|
306 t_string_append(str, NVL(data->content_md5, "NIL"));
|
|
307
|
|
308 t_string_append_c(str, ' ');
|
|
309 if (data->content_disposition == NULL)
|
|
310 t_string_append(str, "NIL");
|
|
311 else {
|
|
312 t_string_append_c(str, '(');
|
|
313 t_string_append(str, data->content_disposition);
|
|
314 t_string_append_c(str, ')');
|
|
315
|
|
316 if (data->content_disposition_params != NULL) {
|
|
317 t_string_append(str, " (");
|
|
318 t_string_append(str, data->content_disposition_params);
|
|
319 t_string_append_c(str, ')');
|
|
320 }
|
|
321 }
|
|
322
|
|
323 t_string_append_c(str, ' ');
|
|
324 if (data->content_language == NULL)
|
|
325 t_string_append(str, "NIL");
|
|
326 else {
|
|
327 t_string_append_c(str, '(');
|
|
328 t_string_append(str, data->content_language);
|
|
329 t_string_append_c(str, ')');
|
|
330 }
|
|
331 }
|
|
332
|
|
333 static void part_write_bodystructure(MessagePart *part, TempString *str,
|
|
334 int extended)
|
|
335 {
|
|
336 while (part != NULL) {
|
|
337 t_string_append_c(str, '(');
|
|
338 if (part->multipart)
|
|
339 part_write_body_multipart(part, str, extended);
|
|
340 else
|
|
341 part_write_body(part, str, extended);
|
|
342
|
|
343 part = part->next;
|
|
344 t_string_append_c(str, ')');
|
|
345 }
|
|
346 }
|
|
347
|
|
348 static const char *part_get_bodystructure(MessagePart *part, int extended)
|
|
349 {
|
|
350 TempString *str;
|
|
351
|
|
352 str = t_string_new(2048);
|
|
353 part_write_bodystructure(part, str, extended);
|
|
354 return str->str;
|
|
355 }
|
|
356
|
|
357 const char *imap_part_get_bodystructure(Pool pool, MessagePart **part,
|
|
358 const char *msg, size_t size,
|
|
359 int extended)
|
|
360 {
|
|
361 if (*part == NULL)
|
|
362 *part = message_parse(pool, msg, size, parse_header, pool);
|
|
363 else
|
|
364 part_parse_headers(*part, msg, size, pool);
|
|
365
|
|
366 return part_get_bodystructure(*part, extended);
|
|
367 }
|