bodystruct.c
8.33 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012, 2014-2017 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/types.h>
#include <mailutils/assoc.h>
#include <mailutils/list.h>
#include <mailutils/message.h>
#include <mailutils/mime.h>
#include <mailutils/header.h>
#include <mailutils/sys/message.h>
#include <mailutils/errno.h>
#include <mailutils/debug.h>
#include <mailutils/nls.h>
#include <mailutils/cstr.h>
#include <mailutils/body.h>
void
mu_list_free_bodystructure (void *item)
{
mu_bodystructure_free (item);
}
void
mu_bodystructure_free (struct mu_bodystructure *bs)
{
if (!bs)
return;
free (bs->body_type);
free (bs->body_subtype);
mu_assoc_destroy (&bs->body_param);
free (bs->body_id);
free (bs->body_descr);
free (bs->body_encoding);
free (bs->body_md5);
free (bs->body_disposition);
mu_assoc_destroy (&bs->body_disp_param);
free (bs->body_language);
free (bs->body_location);
switch (bs->body_message_type)
{
case mu_message_other:
case mu_message_text:
break;
case mu_message_rfc822:
mu_message_imapenvelope_free (bs->v.rfc822.body_env);
mu_bodystructure_free (bs->v.rfc822.body_struct);
break;
case mu_message_multipart:
mu_list_destroy (&bs->v.multipart.body_parts);
}
free (bs);
}
static int bodystructure_fill (mu_message_t msg,
struct mu_bodystructure *bs);
static int
bodystructure_init (mu_message_t msg, struct mu_bodystructure **pbs)
{
int rc;
struct mu_bodystructure *bs = calloc (1, sizeof (*bs));
if (!bs)
return ENOMEM;
rc = bodystructure_fill (msg, bs);
if (rc)
mu_bodystructure_free (bs);
else
*pbs = bs;
return rc;
}
static int
bodystructure_fill (mu_message_t msg, struct mu_bodystructure *bs)
{
mu_header_t header = NULL;
const char *buffer = NULL;
mu_body_t body = NULL;
int rc;
int is_multipart = 0;
rc = mu_message_get_header (msg, &header);
if (rc)
return rc;
if (mu_header_sget_value (header, MU_HEADER_CONTENT_TYPE, &buffer) == 0)
{
char *value;
char *p;
size_t len;
rc = mu_mime_header_parse (buffer, "UTF-8", &value, &bs->body_param);
if (rc)
return rc;
len = strcspn (value, "/");
if (mu_c_strcasecmp (value, "MESSAGE/RFC822") == 0)
bs->body_message_type = mu_message_rfc822;
else if (mu_c_strncasecmp (value, "TEXT", len) == 0)
bs->body_message_type = mu_message_text;
p = malloc (len + 1);
if (!p)
return ENOMEM;
memcpy (p, value, len);
p[len] = 0;
bs->body_type = p;
mu_strupper (bs->body_type);
if (value[len])
{
bs->body_subtype = strdup (value + len + 1);
if (!bs->body_subtype)
return ENOMEM;
mu_strupper (bs->body_subtype);
}
/* body parameter parenthesized list: Content-type attributes */
rc = mu_message_is_multipart (msg, &is_multipart);
if (rc)
return rc;
if (is_multipart)
bs->body_message_type = mu_message_multipart;
}
else
{
struct mu_mime_param param;
/* Default? If Content-Type is not present consider as text/plain. */
bs->body_type = strdup ("TEXT");
if (!bs->body_type)
return ENOMEM;
bs->body_subtype = strdup ("PLAIN");
if (!bs->body_subtype)
{
free (bs->body_type);
return ENOMEM;
}
rc = mu_mime_param_assoc_create (&bs->body_param);
if (rc)
return rc;
memset (¶m, 0, sizeof (param));
param.value = strdup ("US-ASCII");
if (!param.value)
{
free (bs->body_type);
free (bs->body_subtype);
return ENOMEM;
}
rc = mu_assoc_install (bs->body_param, "CHARSET", ¶m);
if (rc)
{
free (param.value);
return rc;
}
bs->body_message_type = mu_message_text;
}
if (is_multipart)
{
size_t i, nparts;
rc = mu_message_get_num_parts (msg, &nparts);
if (rc)
return rc;
rc = mu_list_create (&bs->v.multipart.body_parts);
if (rc)
return rc;
mu_list_set_destroy_item (bs->v.multipart.body_parts,
mu_list_free_bodystructure);
for (i = 1; i <= nparts; i++)
{
mu_message_t partmsg;
struct mu_bodystructure *partbs;
rc = mu_message_get_part (msg, i, &partmsg);
if (rc)
return rc;
rc = bodystructure_init (partmsg, &partbs);
if (rc)
return rc;
rc = mu_list_append (bs->v.multipart.body_parts, partbs);
if (rc)
{
mu_bodystructure_free (partbs);
return rc;
}
}
}
else
{
/* body id: Content-ID. */
rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_ID,
&bs->body_id);
if (rc && rc != MU_ERR_NOENT)
return rc;
/* body description: Content-Description. */
rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_DESCRIPTION,
&bs->body_descr);
if (rc && rc != MU_ERR_NOENT)
return rc;
/* body encoding: Content-Transfer-Encoding. */
rc = mu_header_aget_value_unfold (header,
MU_HEADER_CONTENT_TRANSFER_ENCODING,
&bs->body_encoding);
if (rc == MU_ERR_NOENT)
{
bs->body_encoding = strdup ("7BIT");
if (!bs->body_encoding)
return ENOMEM;
}
else if (rc)
return rc;
/* body size RFC822 format. */
rc = mu_message_get_body (msg, &body);
if (rc)
return rc;
rc = mu_body_size (body, &bs->body_size);
if (rc)
return rc;
/* If the mime type was text. */
if (bs->body_message_type == mu_message_text)
{
rc = mu_body_lines (body, &bs->v.text.body_lines);
if (rc)
return rc;
}
else if (bs->body_message_type == mu_message_rfc822)
{
mu_message_t emsg = NULL;
/* Add envelope structure of the encapsulated message. */
rc = mu_message_unencapsulate (msg, &emsg, NULL);
if (rc)
return rc;
rc = mu_message_get_imapenvelope (emsg, &bs->v.rfc822.body_env);
if (rc)
return rc;
/* Add body structure of the encapsulated message. */
rc = bodystructure_init (emsg, &bs->v.rfc822.body_struct);
if (rc)
return rc;
/* Size in text lines of the encapsulated message. */
rc = mu_message_lines (emsg, &bs->v.rfc822.body_lines);
mu_message_destroy (&emsg, NULL);
}
}
/* body MD5: Content-MD5. */
rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_MD5,
&bs->body_md5);
if (rc && rc != MU_ERR_NOENT)
return rc;
/* body disposition: Content-Disposition. */
rc = mu_header_sget_value (header, MU_HEADER_CONTENT_DISPOSITION,
&buffer);
if (rc == 0)
{
rc = mu_mime_header_parse (buffer, "UTF-8", &bs->body_disposition,
&bs->body_disp_param);
if (rc)
return rc;
}
else if (rc != MU_ERR_NOENT)
return rc;
/* body language: Content-Language. */
rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_LANGUAGE,
&bs->body_language);
if (rc && rc != MU_ERR_NOENT)
return rc;
rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_LOCATION,
&bs->body_location);
if (rc && rc != MU_ERR_NOENT)
return rc;
return 0;
}
int
mu_message_get_bodystructure (mu_message_t msg,
struct mu_bodystructure **pbs)
{
if (msg == NULL)
return EINVAL;
if (pbs == NULL)
return MU_ERR_OUT_PTR_NULL;
if (msg->_bodystructure)
return msg->_bodystructure (msg, pbs);
return bodystructure_init (msg, pbs);
}
int
mu_message_set_bodystructure (mu_message_t msg,
int (*_bodystructure) (mu_message_t, struct mu_bodystructure **),
void *owner)
{
if (msg == NULL)
return EINVAL;
if (msg->owner != owner)
return EACCES;
msg->_bodystructure = _bodystructure;
return 0;
}