summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/tests/readmsg-common.c
blob: 060497dbdf36b1def70db0da702e678d602cdb7d (plain)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include "readmsg-common.h"

#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

/* returns TRUE is given MIME part is a text part */

int etpan_mime_is_text(struct mailmime * build_info)
{
  if (build_info->mm_type == MAILMIME_SINGLE) {
    if (build_info->mm_content_type != NULL) {
      if (build_info->mm_content_type->ct_type->tp_type ==
          MAILMIME_TYPE_DISCRETE_TYPE) {
        if (build_info->mm_content_type->ct_type->tp_data.tp_discrete_type->dt_type ==
            MAILMIME_DISCRETE_TYPE_TEXT)
          return 1;
      }
    }
    else
      return 1;
  }

  return 0;
}


/* display content type */

int show_part_info(FILE * f,
    struct mailmime_single_fields * mime_fields,
    struct mailmime_content * content)
{
  char * description;
  char * filename;
  int col;
  int r;

  description = mime_fields->fld_description;
  filename = mime_fields->fld_disposition_filename;

  col = 0;

  r = fprintf(f, " [ Part ");
  if (r < 0)
    goto err;

  if (content != NULL) {
    r = mailmime_content_type_write(f, &col, content);
    if (r != MAILIMF_NO_ERROR)
      goto err;
  }

  if (filename != NULL) {
    r = fprintf(f, " (%s)", filename);
    if (r < 0)
      goto err;
  }

  if (description != NULL) {
    r = fprintf(f, " : %s", description);
    if (r < 0)
      goto err;
  }

  r = fprintf(f, " ]\n\n");
  if (r < 0)
    goto err;

  return NO_ERROR;
  
 err:
  return ERROR_FILE;
}

/*
  fetch the data of the mailmime_data structure whether it is a file
  or a string.

  data must be freed with mmap_string_unref()
*/

#if 0
static int fetch_data(struct mailmime_data * data,
    char ** result, size_t * result_len)
{
  int fd;
  int r;
  char * text;
  struct stat buf;
  int res;
  MMAPString * mmapstr;

  switch (data->dt_type) {
  case MAILMIME_DATA_TEXT:
    mmapstr = mmap_string_new_len(data->dt_data.dt_text.dt_data,
        data->dt_data.dt_text.dt_length);
    if (mmapstr == NULL) {
      res = ERROR_MEMORY;
      goto err;
    }

    * result = mmapstr->str;
    * result_len = mmapstr->len;

    return NO_ERROR;

  case MAILMIME_DATA_FILE:
    fd = open(data->dt_data.dt_filename, O_RDONLY);
    if (fd < 0) {
      res = ERROR_FILE;
      goto err;
    }

    r = fstat(fd, &buf);
    if (r < 0) {
      res = ERROR_FILE;
      goto close;
    }

    if (buf.st_size != 0) {
      text = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
      if (text == MAP_FAILED) {
	res = ERROR_FILE;
	goto close;
      }

      mmapstr = mmap_string_new_len(text, buf.st_size);
      if (mmapstr == NULL) {
        res = r;
        goto unmap;
      }
      
      munmap(text, buf.st_size);
    }
    else {
      mmapstr = mmap_string_new("");
      if (mmapstr == NULL) {
        res = r;
        goto close;
      }
    }

    close(fd);

    * result = mmapstr->str;
    * result_len = mmapstr->len;

    return NO_ERROR;

  default:
    return ERROR_INVAL;
  }
  
 unmap:
  munmap(text, buf.st_size);
 close:
  close(fd);
 err:
  return res;
}
#endif

/* fetch message and decode if it is base64 or quoted-printable */

int etpan_fetch_message(mailmessage * msg_info,
    struct mailmime * mime_part,
    struct mailmime_single_fields * fields,
    char ** result, size_t * result_len)
{
  char * data;
  size_t len;
  int r;
  int encoding;
  char * decoded;
  size_t decoded_len;
  size_t cur_token;
  int res;
  int encoded;

  encoded = 0;

  r = mailmessage_fetch_section(msg_info,
      mime_part, &data, &len);
  if (r != MAIL_NO_ERROR) {
    res = ERROR_FETCH;
    goto err;
  }

  encoded = 1;

  /* decode message */

  if (encoded) {
    if (fields->fld_encoding != NULL)
      encoding = fields->fld_encoding->enc_type;
    else 
      encoding = MAILMIME_MECHANISM_8BIT;
  }
  else {
    encoding = MAILMIME_MECHANISM_8BIT;
  }

  cur_token = 0;
  r = mailmime_part_parse(data, len, &cur_token,
			  encoding, &decoded, &decoded_len);
  if (r != MAILIMF_NO_ERROR) {
    res = ERROR_FETCH;
    goto free; 
  }

  mailmessage_fetch_result_free(msg_info, data);
  
  * result = decoded;
  * result_len = decoded_len;
  
  return NO_ERROR;
  
 free:
  mailmessage_fetch_result_free(msg_info, data);
 err:
  return res;
}


/* fetch fields */

struct mailimf_fields * fetch_fields(mailmessage * msg_info,
    struct mailmime * mime)
{
  char * data;
  size_t len;
  int r;
  size_t cur_token;
  struct mailimf_fields * fields;

  r = mailmessage_fetch_section_header(msg_info, mime, &data, &len);
  if (r != MAIL_NO_ERROR)
    return NULL;

  cur_token = 0;
  r = mailimf_fields_parse(data, len, &cur_token, &fields);
  if (r != MAILIMF_NO_ERROR) {
    mailmessage_fetch_result_free(msg_info, data);
    return NULL;
  }

  mailmessage_fetch_result_free(msg_info, data);

  return fields;
}



#define MAX_MAIL_COL 72

/* write decoded mailbox */

static int
etpan_mailbox_write(FILE * f, int * col,
    struct mailimf_mailbox * mb)
{
  int r;

  if (* col > 1) {
    
    if (* col + strlen(mb->mb_addr_spec) >= MAX_MAIL_COL) {
      r = mailimf_string_write(f, col, "\r\n ", 3);
      if (r != MAILIMF_NO_ERROR)
	return ERROR_FILE;
      * col = 1;
    }
  }
  
  if (mb->mb_display_name) {
    char * decoded_from;
    size_t cur_token;

    cur_token = 0;
    r = mailmime_encoded_phrase_parse(DEST_CHARSET,
        mb->mb_display_name, strlen(mb->mb_display_name),
        &cur_token, DEST_CHARSET,
        &decoded_from);
    if (r != MAILIMF_NO_ERROR) {
      decoded_from = strdup(mb->mb_display_name);
      if (decoded_from == NULL)
        return ERROR_MEMORY;
    }

    r = mailimf_quoted_string_write(f, col, decoded_from,
        strlen(decoded_from));
    if (r != MAILIMF_NO_ERROR) {
      free(decoded_from);
      return ERROR_FILE;
    }

    if (* col > 1) {
      
      if (* col + strlen(decoded_from) + 3 >= MAX_MAIL_COL) {
	r = mailimf_string_write(f, col, "\r\n ", 3);
	if (r != MAILIMF_NO_ERROR) {
          free(decoded_from);
	  return r;
        }
	* col = 1;
      }
    }

    free(decoded_from);
    
    r = mailimf_string_write(f, col, " <", 2);
    if (r != MAILIMF_NO_ERROR)
      return ERROR_FILE;

    r = mailimf_string_write(f, col,
        mb->mb_addr_spec, strlen(mb->mb_addr_spec));
    if (r != MAILIMF_NO_ERROR)
      return ERROR_FILE;

    r = mailimf_string_write(f, col, ">", 1);
    if (r != MAILIMF_NO_ERROR)
      return ERROR_FILE;
  }
  else {
    r = mailimf_string_write(f, col,
        mb->mb_addr_spec, strlen(mb->mb_addr_spec));
    if (r != MAILIMF_NO_ERROR)
      return ERROR_FILE;
  }


  return NO_ERROR;

}

/* write decoded mailbox list */

int
etpan_mailbox_list_write(FILE * f, int * col,
    struct mailimf_mailbox_list * mb_list)
{
  clistiter * cur;
  int r;
  int first;

  first = 1;

  for(cur = clist_begin(mb_list->mb_list) ; cur != NULL ;
      cur = clist_next(cur)) {
    struct mailimf_mailbox * mb;

    mb = cur->data;

    if (!first) {
      r = mailimf_string_write(f, col, ", ", 2);
      if (r != MAILIMF_NO_ERROR)
	return ERROR_FILE;
    }
    else {
      first = 0;
    }

    r = etpan_mailbox_write(f, col, mb);
    if (r != NO_ERROR)
      return r;
  }

  return NO_ERROR;
}

/* write decoded group */

static int
etpan_group_write(FILE * f, int * col,
    struct mailimf_group * group)
{
  int r;

  r = mailimf_string_write(f, col, group->grp_display_name,
			   strlen(group->grp_display_name));
  if (r != MAILIMF_NO_ERROR)
    return ERROR_FILE;

  r = mailimf_string_write(f, col, ": ", 2);
  if (r != MAILIMF_NO_ERROR)
    return ERROR_FILE;
  
  if (group->grp_mb_list != NULL) {
    r = etpan_mailbox_list_write(f, col, group->grp_mb_list);
    if (r != NO_ERROR)
      return r;
  }

  r = mailimf_string_write(f, col, ";", 1);
  if (r != MAILIMF_NO_ERROR)
    return ERROR_FILE;

  return NO_ERROR;
}

/* write decoded address */

int
etpan_address_write(FILE * f, int * col,
    struct mailimf_address * addr)
{
  int r;

  switch(addr->ad_type) {
  case MAILIMF_ADDRESS_MAILBOX:
    r = etpan_mailbox_write(f, col, addr->ad_data.ad_mailbox);
    if (r != NO_ERROR)
      return r;

    break;

  case MAILIMF_ADDRESS_GROUP:
    r = etpan_group_write(f, col, addr->ad_data.ad_group);
    if (r != NO_ERROR)
      return r;
    
    break;
  }

  return MAILIMF_NO_ERROR;
}

/* write decoded address list */

int
etpan_address_list_write(FILE * f, int * col,
    struct mailimf_address_list * addr_list)
{
  clistiter * cur;
  int r;
  int first;

  first = 1;

  for(cur = clist_begin(addr_list->ad_list) ; cur != NULL ;
      cur = clist_next(cur)) {
    struct mailimf_address * addr;

    addr = clist_content(cur);

    if (!first) {
      r = mailimf_string_write(f, col, ", ", 2);
      if (r != MAILIMF_NO_ERROR)
	return ERROR_FILE;
    }
    else {
      first = 0;
    }

    r = etpan_address_write(f, col, addr);
    if (r != NO_ERROR)
      return r;
  }

  return NO_ERROR;
}

/* write decoded subject */

static int etpan_subject_write(FILE * f, int * col,
    char * subject)
{
  int r;
  char * decoded_subject;
  size_t cur_token;
  
  r = mailimf_string_write(f, col, "Subject: ", 9);
  if (r != MAILIMF_NO_ERROR) {
    return ERROR_FILE;
  }
  
  cur_token = 0;
  r = mailmime_encoded_phrase_parse(DEST_CHARSET,
      subject, strlen(subject),
      &cur_token, DEST_CHARSET,
      &decoded_subject);
  if (r != MAILIMF_NO_ERROR) {
    decoded_subject = strdup(subject);
    if (decoded_subject == NULL)
      return ERROR_MEMORY;
  }
  
  r = mailimf_string_write(f, col, decoded_subject, strlen(decoded_subject));
  if (r != MAILIMF_NO_ERROR) {
    free(decoded_subject);
    return ERROR_FILE;
  }

  free(decoded_subject);

  r = mailimf_string_write(f, col, "\r\n", 2);
  if (r != MAILIMF_NO_ERROR) {
    return ERROR_FILE;
  }
  * col = 0;

  return NO_ERROR;
}

/* write decoded fields */

int fields_write(FILE * f, int * col,
    struct mailimf_fields * fields)
{
  clistiter * cur;
  int r;
  
  for(cur = clist_begin(fields->fld_list) ; cur != NULL ;
      cur = clist_next(cur)) {
    struct mailimf_field * field;

    field = clist_content(cur);

    switch (field->fld_type) {
    case MAILIMF_FIELD_FROM:
      r = mailimf_string_write(f, col, "From: ", 6);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      
      r = etpan_mailbox_list_write(f, col,
          field->fld_data.fld_from->frm_mb_list);
      if (r != NO_ERROR)
        goto err;

      r = mailimf_string_write(f, col, "\r\n", 2);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      * col = 0;

      break;

    case MAILIMF_FIELD_REPLY_TO:
      r = mailimf_string_write(f, col, "Reply-To: ", 10);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      
      r = etpan_address_list_write(f, col,
          field->fld_data.fld_reply_to->rt_addr_list);
      if (r != NO_ERROR)
        goto err;

      r = mailimf_string_write(f, col, "\r\n", 2);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      * col = 0;

      break;

    case MAILIMF_FIELD_TO:
      r = mailimf_string_write(f, col, "To: ", 4);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      
      r = etpan_address_list_write(f, col,
          field->fld_data.fld_to->to_addr_list);
      if (r != NO_ERROR)
        goto err;

      r = mailimf_string_write(f, col, "\r\n", 2);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      * col = 0;

      break;

    case MAILIMF_FIELD_CC:
      r = mailimf_string_write(f, col, "Cc: ", 4);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      
      r = etpan_address_list_write(f, col,
          field->fld_data.fld_cc->cc_addr_list);
      if (r != NO_ERROR)
        goto err;

      r = mailimf_string_write(f, col, "\r\n", 2);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      * col = 0;

      break;

    case MAILIMF_FIELD_BCC:
      r = mailimf_string_write(f, col, "Bcc: ", 10);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      
      if (field->fld_data.fld_bcc->bcc_addr_list != NULL) {
        r = etpan_address_list_write(f, col,
            field->fld_data.fld_bcc->bcc_addr_list);
        if (r != NO_ERROR)
          goto err;
      }

      r = mailimf_string_write(f, col, "\r\n", 2);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      * col = 0;

      break;

    case MAILIMF_FIELD_SUBJECT:
      r = etpan_subject_write(f, col, field->fld_data.fld_subject->sbj_value);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      break;

    case MAILIMF_FIELD_RESENT_FROM:
      r = mailimf_string_write(f, col, "Resent-From: ", 13);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      
      r = etpan_mailbox_list_write(f, col,
          field->fld_data.fld_resent_from->frm_mb_list);
      if (r != NO_ERROR)
        goto err;

      r = mailimf_string_write(f, col, "\r\n", 2);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      * col = 0;
      break;

    case MAILIMF_FIELD_RESENT_TO:
      r = mailimf_string_write(f, col, "Resent-To: ", 11);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      
      r = etpan_address_list_write(f, col,
          field->fld_data.fld_resent_to->to_addr_list);
      if (r != NO_ERROR)
        goto err;

      r = mailimf_string_write(f, col, "\r\n", 2);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      * col = 0;

      break;
    case MAILIMF_FIELD_RESENT_CC:
      r = mailimf_string_write(f, col, "Resent-Cc: ", 11);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      
      r = etpan_address_list_write(f, col,
          field->fld_data.fld_resent_cc->cc_addr_list);
      if (r != NO_ERROR)
        goto err;

      r = mailimf_string_write(f, col, "\r\n", 2);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      * col = 0;

      break;
    case MAILIMF_FIELD_RESENT_BCC:
      r = mailimf_string_write(f, col, "Resent-Bcc: ", 12);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      
      if (field->fld_data.fld_resent_bcc->bcc_addr_list != NULL) {
        r = etpan_address_list_write(f, col,
            field->fld_data.fld_resent_bcc->bcc_addr_list);
        if (r != NO_ERROR)
          goto err;
      }

      r = mailimf_string_write(f, col, "\r\n", 2);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      * col = 0;

      break;

    case MAILIMF_FIELD_ORIG_DATE:
    case MAILIMF_FIELD_RESENT_DATE:
      r = mailimf_field_write(f, col, field);
      if (r != MAILIMF_NO_ERROR)
        goto err;
      break;

    case MAILIMF_FIELD_OPTIONAL_FIELD:
      if ((strcasecmp(field->fld_data.fld_optional_field->fld_name,
               "X-Mailer") == 0)
          || (strncasecmp(field->fld_data.fld_optional_field->fld_name,
                  "Resent-", 7) == 0)
          || (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                  "Newsgroups") == 0)
          || (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                  "Followup-To") == 0)
          || (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                  "User-Agent") == 0)) {
        r = mailimf_field_write(f, col, field);
        if (r != MAILIMF_NO_ERROR)
          goto err;
      }
      break;

    case MAILIMF_FIELD_MESSAGE_ID:
    case MAILIMF_FIELD_SENDER:
    case MAILIMF_FIELD_IN_REPLY_TO:
    case MAILIMF_FIELD_REFERENCES:
    default:
      break;
    }
  }

  return NO_ERROR;

 err:
  return ERROR_FILE;
}