-
Notifications
You must be signed in to change notification settings - Fork 289
/
mime-parse.c
372 lines (316 loc) · 8.13 KB
/
mime-parse.c
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <libetpan/libetpan.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
static void display_mime_content(struct mailmime_content * content_type);
static void display_mime_data(struct mailmime_data * data)
{
switch (data->dt_type) {
case MAILMIME_DATA_TEXT:
printf("data : %u bytes\n", (unsigned int) data->dt_data.dt_text.dt_length);
break;
case MAILMIME_DATA_FILE:
printf("data (file) : %s\n", data->dt_data.dt_filename);
break;
}
}
static void display_mime_dsp_parm(struct mailmime_disposition_parm * param)
{
switch (param->pa_type) {
case MAILMIME_DISPOSITION_PARM_FILENAME:
printf("filename: %s\n", param->pa_data.pa_filename);
break;
}
}
static void display_mime_disposition(struct mailmime_disposition * disposition)
{
clistiter * cur;
for(cur = clist_begin(disposition->dsp_parms) ;
cur != NULL ; cur = clist_next(cur)) {
struct mailmime_disposition_parm * param;
param = clist_content(cur);
display_mime_dsp_parm(param);
}
}
static void display_mime_field(struct mailmime_field * field)
{
switch (field->fld_type) {
case MAILMIME_FIELD_TYPE:
printf("content-type: ");
display_mime_content(field->fld_data.fld_content);
printf("\n");
break;
case MAILMIME_FIELD_DISPOSITION:
display_mime_disposition(field->fld_data.fld_disposition);
break;
}
}
static void display_mime_fields(struct mailmime_fields * fields)
{
clistiter * cur;
for(cur = clist_begin(fields->fld_list) ; cur != NULL ; cur = clist_next(cur)) {
struct mailmime_field * field;
field = clist_content(cur);
display_mime_field(field);
}
}
static void display_date_time(struct mailimf_date_time * d)
{
printf("%02i/%02i/%i %02i:%02i:%02i %+04i",
d->dt_day, d->dt_month, d->dt_year,
d->dt_hour, d->dt_min, d->dt_sec, d->dt_zone);
}
static void display_orig_date(struct mailimf_orig_date * orig_date)
{
display_date_time(orig_date->dt_date_time);
}
static void display_mailbox(struct mailimf_mailbox * mb)
{
if (mb->mb_display_name != NULL)
printf("%s ", mb->mb_display_name);
printf("<%s>", mb->mb_addr_spec);
}
static void display_mailbox_list(struct mailimf_mailbox_list * mb_list)
{
clistiter * cur;
for(cur = clist_begin(mb_list->mb_list) ; cur != NULL ;
cur = clist_next(cur)) {
struct mailimf_mailbox * mb;
mb = clist_content(cur);
display_mailbox(mb);
if (clist_next(cur) != NULL) {
printf(", ");
}
}
}
static void display_group(struct mailimf_group * group)
{
clistiter * cur;
printf("%s: ", group->grp_display_name);
for(cur = clist_begin(group->grp_mb_list->mb_list) ; cur != NULL ; cur = clist_next(cur)) {
struct mailimf_mailbox * mb;
mb = clist_content(cur);
display_mailbox(mb);
}
printf("; ");
}
static void display_address(struct mailimf_address * a)
{
switch (a->ad_type) {
case MAILIMF_ADDRESS_GROUP:
display_group(a->ad_data.ad_group);
break;
case MAILIMF_ADDRESS_MAILBOX:
display_mailbox(a->ad_data.ad_mailbox);
break;
}
}
static void display_address_list(struct mailimf_address_list * addr_list)
{
clistiter * cur;
for(cur = clist_begin(addr_list->ad_list) ; cur != NULL ;
cur = clist_next(cur)) {
struct mailimf_address * addr;
addr = clist_content(cur);
display_address(addr);
if (clist_next(cur) != NULL) {
printf(", ");
}
}
}
static void display_from(struct mailimf_from * from)
{
display_mailbox_list(from->frm_mb_list);
}
static void display_to(struct mailimf_to * to)
{
display_address_list(to->to_addr_list);
}
static void display_cc(struct mailimf_cc * cc)
{
display_address_list(cc->cc_addr_list);
}
static void display_subject(struct mailimf_subject * subject)
{
printf("%s", subject->sbj_value);
}
static void display_field(struct mailimf_field * field)
{
switch (field->fld_type) {
case MAILIMF_FIELD_ORIG_DATE:
printf("Date: ");
display_orig_date(field->fld_data.fld_orig_date);
printf("\n");
break;
case MAILIMF_FIELD_FROM:
printf("From: ");
display_from(field->fld_data.fld_from);
printf("\n");
break;
case MAILIMF_FIELD_TO:
printf("To: ");
display_to(field->fld_data.fld_to);
printf("\n");
break;
case MAILIMF_FIELD_CC:
printf("Cc: ");
display_cc(field->fld_data.fld_cc);
printf("\n");
break;
case MAILIMF_FIELD_SUBJECT:
printf("Subject: ");
display_subject(field->fld_data.fld_subject);
printf("\n");
break;
case MAILIMF_FIELD_MESSAGE_ID:
printf("Message-ID: %s\n", field->fld_data.fld_message_id->mid_value);
break;
}
}
static void display_fields(struct mailimf_fields * fields)
{
clistiter * cur;
for(cur = clist_begin(fields->fld_list) ; cur != NULL ;
cur = clist_next(cur)) {
struct mailimf_field * f;
f = clist_content(cur);
display_field(f);
}
}
static void display_mime_discrete_type(struct mailmime_discrete_type * discrete_type)
{
switch (discrete_type->dt_type) {
case MAILMIME_DISCRETE_TYPE_TEXT:
printf("text");
break;
case MAILMIME_DISCRETE_TYPE_IMAGE:
printf("image");
break;
case MAILMIME_DISCRETE_TYPE_AUDIO:
printf("audio");
break;
case MAILMIME_DISCRETE_TYPE_VIDEO:
printf("video");
break;
case MAILMIME_DISCRETE_TYPE_APPLICATION:
printf("application");
break;
case MAILMIME_DISCRETE_TYPE_EXTENSION:
printf("%s", discrete_type->dt_extension);
break;
}
}
static void display_mime_composite_type(struct mailmime_composite_type * ct)
{
switch (ct->ct_type) {
case MAILMIME_COMPOSITE_TYPE_MESSAGE:
printf("message");
break;
case MAILMIME_COMPOSITE_TYPE_MULTIPART:
printf("multipart");
break;
case MAILMIME_COMPOSITE_TYPE_EXTENSION:
printf("%s", ct->ct_token);
break;
}
}
static void display_mime_type(struct mailmime_type * type)
{
switch (type->tp_type) {
case MAILMIME_TYPE_DISCRETE_TYPE:
display_mime_discrete_type(type->tp_data.tp_discrete_type);
break;
case MAILMIME_TYPE_COMPOSITE_TYPE:
display_mime_composite_type(type->tp_data.tp_composite_type);
break;
}
}
static void display_mime_content(struct mailmime_content * content_type)
{
printf("type: ");
display_mime_type(content_type->ct_type);
printf("/%s\n", content_type->ct_subtype);
}
static void display_mime(struct mailmime * mime)
{
clistiter * cur;
switch (mime->mm_type) {
case MAILMIME_SINGLE:
printf("single part\n");
break;
case MAILMIME_MULTIPLE:
printf("multipart\n");
break;
case MAILMIME_MESSAGE:
printf("message\n");
break;
}
if (mime->mm_mime_fields != NULL) {
if (clist_begin(mime->mm_mime_fields->fld_list) != NULL) {
printf("MIME headers begin\n");
display_mime_fields(mime->mm_mime_fields);
printf("MIME headers end\n");
}
}
display_mime_content(mime->mm_content_type);
switch (mime->mm_type) {
case MAILMIME_SINGLE:
display_mime_data(mime->mm_data.mm_single);
break;
case MAILMIME_MULTIPLE:
for(cur = clist_begin(mime->mm_data.mm_multipart.mm_mp_list) ; cur != NULL ; cur = clist_next(cur)) {
display_mime(clist_content(cur));
}
break;
case MAILMIME_MESSAGE:
if (mime->mm_data.mm_message.mm_fields) {
if (clist_begin(mime->mm_data.mm_message.mm_fields->fld_list) != NULL) {
printf("headers begin\n");
display_fields(mime->mm_data.mm_message.mm_fields);
printf("headers end\n");
}
if (mime->mm_data.mm_message.mm_msg_mime != NULL) {
display_mime(mime->mm_data.mm_message.mm_msg_mime);
}
break;
}
}
}
int main(int argc, char ** argv)
{
FILE * f;
int r;
struct mailmime * mime;
struct stat stat_info;
char * data;
size_t current_index;
char * filename;
if (argc < 2) {
fprintf(stderr, "syntax: mime-parse [filename]\n");
}
filename = argv[1];
f = fopen(filename, "r");
if (f == NULL) {
exit(EXIT_FAILURE);
}
r = stat(filename, &stat_info);
if (r != 0) {
fclose(f);
exit(EXIT_FAILURE);
}
data = malloc(stat_info.st_size);
fread(data, 1, stat_info.st_size, f);
fclose(f);
current_index = 0;
r = mailmime_parse(data, stat_info.st_size,
¤t_index, &mime);
if (r != MAILIMF_NO_ERROR) {
free(data);
exit(EXIT_FAILURE);
return EXIT_FAILURE;
}
display_mime(mime);
mailmime_free(mime);
free(data);
exit(EXIT_SUCCESS);
}