index-mail-headers.c revision 87b426af6a2365c6840b14281a98c23e903bf28e
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
2ebeb22b9a8a8bb7fbe2f2e2908478a220792b87Timo Sirainen/* Copyright (C) 2003-2006 Timo Sirainen */
9d3ccd79130199ffdb19a688027d49bf20a4aaaaTimo Sirainen unsigned int count;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unsigned int *idx;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const char **name;
043c8a96a035379bcba04f487d58457beefdfcaaTimo Sirainenstatic const enum message_header_parser_flags hdr_parser_flags =
043c8a96a035379bcba04f487d58457beefdfcaaTimo Sirainen MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
043c8a96a035379bcba04f487d58457beefdfcaaTimo Sirainenstatic const enum message_parser_flags msg_parser_flags =
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstatic int header_line_cmp(const void *p1, const void *p2)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const struct index_mail_line *l1 = p1, *l2 = p2;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen diff = (int)l1->field_idx - (int)l2->field_idx;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstatic void index_mail_parse_header_finish(struct index_mail *mail)
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen unsigned int i, j, count, match_idx, match_count;
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen lines = array_get_modifiable(&mail->header_lines, &count);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* sort it first so fields are grouped together and ordered by
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen line number */
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen qsort(lines, count, sizeof(*lines), header_line_cmp);
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen match = array_get(&mail->header_match, &match_count);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen header = buffer_get_data(mail->header_data, NULL);
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen buf = buffer_create_dynamic(pool_datastack_create(), 256);
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen /* go through all the header lines we found */
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen /* matches and header lines are both sorted, all matches
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen until lines[i] weren't found */
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen /* if match[] doesn't have header_match_value,
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen it belongs to some older header parsing and we
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen just want to ignore it. */
872b8fd8a8db97dc54067b7ab25bda96ec0aac0dTimo Sirainen if (match[match_idx] == mail->header_match_value &&
872b8fd8a8db97dc54067b7ab25bda96ec0aac0dTimo Sirainen mail_cache_field_exists(mail->trans->cache_view,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* this header doesn't exist. remember that. */
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen /* save index to first header line */
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen array_idx_set(&mail->header_match_lines, match_idx, &j);
872b8fd8a8db97dc54067b7ab25bda96ec0aac0dTimo Sirainen /* header is already cached */
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen /* buffer contains: { uint32_t line_num[], 0, header texts }
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen noncontiguous is just a small optimization.. */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (; i < j; i++) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen buffer_append(buf, header + lines[i].start_pos,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen buffer_append(buf, header + lines[i].start_pos,
5cda0bfea032000c4a51134c748d9efe6614870bTimo Sirainen index_mail_cache_add_idx(mail, lines[i].field_idx,
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen for (; match_idx < match_count; match_idx++) {
872b8fd8a8db97dc54067b7ab25bda96ec0aac0dTimo Sirainen if (match[match_idx] == mail->header_match_value &&
872b8fd8a8db97dc54067b7ab25bda96ec0aac0dTimo Sirainen mail_cache_field_exists(mail->trans->cache_view,
2aecf7be5834e7f6520f8deaad683a6fa1de4d61Timo Sirainen /* this header doesn't exist. remember that. */
5cda0bfea032000c4a51134c748d9efe6614870bTimo Sirainen index_mail_cache_add_idx(mail, match_idx, NULL, 0);
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainenstatic unsigned int
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainenget_header_field_idx(struct index_mailbox *ibox, const char *field)
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen header_field.name = t_strconcat("hdr.", field, NULL);
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen mail_cache_register_fields(ibox->cache, &header_field, 1);
77af0bd168cf3e3ddc3ae68abc82bfad7e9b5ff4Timo Sirainenbool index_mail_want_parse_headers(struct index_mail *mail)
77af0bd168cf3e3ddc3ae68abc82bfad7e9b5ff4Timo Sirainen return mail->wanted_headers != NULL || mail->data.save_sent_date ||
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid index_mail_parse_header_init(struct index_mail *mail,
45b2a27617d8475f71fdfc870690e46cd63849f2Timo Sirainen const struct mail_cache_field *all_cache_fields;
45b2a27617d8475f71fdfc870690e46cd63849f2Timo Sirainen unsigned int i, count;
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen mail->header_data = buffer_create_dynamic(default_pool, 4096);
872b8fd8a8db97dc54067b7ab25bda96ec0aac0dTimo Sirainen /* wrapped, we'll have to clear the buffer */
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen array_idx_set(&mail->header_match, headers->idx[i],
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (mail->wanted_headers != NULL && mail->wanted_headers != headers) {
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen array_idx_set(&mail->header_match, headers->idx[i],
45b2a27617d8475f71fdfc870690e46cd63849f2Timo Sirainen /* register also all the other headers that exist in cache file */
3af0a1a2c2ef22a14c6b3c5cc4f780a2ea4df438Timo Sirainen mail_cache_register_get_list(mail->ibox->cache,
45b2a27617d8475f71fdfc870690e46cd63849f2Timo Sirainen for (i = 0; i < count; i++) {
45b2a27617d8475f71fdfc870690e46cd63849f2Timo Sirainen if (strncasecmp(all_cache_fields[i].name, "hdr.", 4) == 0) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstatic void index_mail_parse_finish_imap_envelope(struct index_mail *mail)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen imap_envelope_write_part_data(mail->data.envelope_data, str);
97511ac4d7607e1ba64ce151eda3d9b5f9775519Timo Sirainen index_mail_cache_add(mail, MAIL_CACHE_IMAP_ENVELOPE,
d2b94d25f842cd1b7acaf4dd7de858f7c6a821c9Timo Sirainenvoid index_mail_parse_header(struct message_part *part,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen imap_bodystructure_parse_header(mail->data_pool, part, hdr);
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen /* end of headers */
97511ac4d7607e1ba64ce151eda3d9b5f9775519Timo Sirainen index_mail_cache_add(mail, MAIL_CACHE_SENT_DATE,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen cache_field_name = t_strconcat("hdr.", hdr->name, NULL);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* we don't want this field */
2ebeb22b9a8a8bb7fbe2f2e2908478a220792b87Timo Sirainen mail_cache_field_want_add(mail->trans->cache_trans,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen match = array_get_modifiable(&mail->header_match, &count);
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen if (field_idx < count && match[field_idx] == mail->header_match_value) {
38d7db318188c4ac9cdc8c6cdb936b36a5258e19Timo Sirainen /* first header */
38d7db318188c4ac9cdc8c6cdb936b36a5258e19Timo Sirainen (match[field_idx] & ~1) != mail->header_match_value)) {
38d7db318188c4ac9cdc8c6cdb936b36a5258e19Timo Sirainen /* we don't need to do anything with this header */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen data->parse_line.start_pos = str_len(mail->header_data);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen data->parse_line.line_num = data->parse_line_num;
60576cd64e6a537413cd90104f7e862f71d48c81Timo Sirainen str_append_n(mail->header_data, hdr->middle, hdr->middle_len);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen str_append_n(mail->header_data, hdr->value, hdr->value_len);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen data->parse_line.end_pos = str_len(mail->header_data);
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen array_append(&mail->header_lines, &data->parse_line, 1);
d96f86fb881c5b106649e8994ead1052acf24030Timo Sirainenindex_mail_parse_part_header_cb(struct message_part *part,
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainenindex_mail_parse_header_cb(struct message_header_line *hdr,
d96f86fb881c5b106649e8994ead1052acf24030Timo Sirainen index_mail_parse_header(mail->data.parts, hdr, mail);
7a54d58280aad8a64f266c61273ea1e8dff511a3Timo Sirainenindex_mail_cache_parse_init(struct mail *_mail, struct istream *input)
043c8a96a035379bcba04f487d58457beefdfcaaTimo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
dd0dea1fdd913a04bae16e82dd66d67571a5f6c2Timo Sirainenstatic void index_mail_init_parser(struct index_mail *mail)
dd0dea1fdd913a04bae16e82dd66d67571a5f6c2Timo Sirainen (void)message_parser_deinit(&data->parser_ctx);
043c8a96a035379bcba04f487d58457beefdfcaaTimo Sirainen data->parser_ctx = message_parser_init(mail->data_pool,
2aecf7be5834e7f6520f8deaad683a6fa1de4d61Timo Sirainenint index_mail_parse_headers(struct index_mail *mail,
20195ef995a4eb63a282283db63f1dc0605323e0Timo Sirainen old_offset = data->stream == NULL ? 0 : data->stream->v_offset;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (mail_get_stream(&mail->mail.mail, NULL, NULL, &input) < 0)
1045a1d4c191a14867cde0d5cea9e4ac5e36f85fTimo Sirainen if (data->parts == NULL || data->save_bodystructure_header) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* initialize bodystructure parsing in case we read the whole
96541d31299bb40b5a6efdbf9b4cb3d4f4b4a069Timo Sirainen message_parser_parse_header(data->parser_ctx, &data->hdr_size,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* just read the header */
d96f86fb881c5b106649e8994ead1052acf24030Timo Sirainen message_parse_header(data->stream, &data->hdr_size,
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainenimap_envelope_parse_callback(struct message_header_line *hdr,
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenint index_mail_headers_get_envelope(struct index_mail *mail)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen header_ctx = mailbox_header_lookup_init(&mail->ibox->box,
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (mail_get_header_stream(&mail->mail.mail, header_ctx, &stream) < 0)
9db263f2b9ab771fbf9a2bff44a245c45eaef218Timo Sirainen if (mail->data.envelope == NULL && stream != NULL) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* we got the headers from cache - parse them to get the
043c8a96a035379bcba04f487d58457beefdfcaaTimo Sirainen message_parse_header(stream, NULL, hdr_parser_flags,
98dd8e6e81f11f1e6040ca72f4916242d246c863Timo Sirainenstatic size_t get_header_size(buffer_t *buffer, size_t pos)
98dd8e6e81f11f1e6040ca72f4916242d246c863Timo Sirainen const unsigned char *data;
98dd8e6e81f11f1e6040ca72f4916242d246c863Timo Sirainenstatic int index_mail_header_is_parsed(struct index_mail *mail,
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen unsigned int count;
cf52b37d807553e91a2d6fb7cb2c8b4c34589e1dTimo Sirainen match = array_get(&mail->header_match, &count);
98dd8e6e81f11f1e6040ca72f4916242d246c863Timo Sirainen if (match[field_idx] == mail->header_match_value)
98dd8e6e81f11f1e6040ca72f4916242d246c863Timo Sirainen else if (match[field_idx] == mail->header_match_value + 1)
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenstatic bool skip_header(const unsigned char **data, size_t len)
1e242794e7a4f653f18fbb8edfe9ccec489a3a08Timo Sirainen const unsigned char *p = *data;
1e242794e7a4f653f18fbb8edfe9ccec489a3a08Timo Sirainen for (i = 0; i < len; i++) {
1e242794e7a4f653f18fbb8edfe9ccec489a3a08Timo Sirainen if (p[i] == ':')
1e242794e7a4f653f18fbb8edfe9ccec489a3a08Timo Sirainen for (i++; i < len; i++) {
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainenstatic const char *const *
98dd8e6e81f11f1e6040ca72f4916242d246c863Timo Sirainenindex_mail_get_parsed_header(struct index_mail *mail, unsigned int field_idx)
1e242794e7a4f653f18fbb8edfe9ccec489a3a08Timo Sirainen const unsigned char *header, *value_start, *value_end;
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen const unsigned int *line_idx;
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen line_idx = array_idx(&mail->header_match_lines, field_idx);
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen p_array_init(&header_values, mail->data_pool, 4);
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen header = buffer_get_data(mail->header_data, NULL);
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen lines = array_get(&mail->header_lines, &lines_count);
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen for (i = first_line_idx; i < lines_count; i++) {
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen if (lines[i].field_idx != lines[first_line_idx].field_idx)
1e242794e7a4f653f18fbb8edfe9ccec489a3a08Timo Sirainen /* skip header: and drop ending LF */
1e242794e7a4f653f18fbb8edfe9ccec489a3a08Timo Sirainen if (skip_header(&value_start, value_end - value_start)) {
1e242794e7a4f653f18fbb8edfe9ccec489a3a08Timo Sirainen if (value_start != value_end && value_end[-1] == '\n')
1e242794e7a4f653f18fbb8edfe9ccec489a3a08Timo Sirainen value = p_strndup(mail->data_pool, value_start,
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen array_append(&header_values, &value, sizeof(value));
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenindex_mail_get_raw_headers(struct index_mail *mail, const char *field,
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen const char *const **value_r)
2aecf7be5834e7f6520f8deaad683a6fa1de4d61Timo Sirainen struct mailbox_header_lookup_ctx *headers_ctx;
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen unsigned char *data;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen field_idx = get_header_field_idx(mail->ibox, field);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (mail_cache_lookup_headers(mail->trans->cache_view, dest,
98dd8e6e81f11f1e6040ca72f4916242d246c863Timo Sirainen /* not in cache / error - first see if it's already parsed */
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen ret = index_mail_parse_headers(mail, headers_ctx);
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if ((ret = index_mail_header_is_parsed(mail, field_idx)) <= 0) {
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen /* not found */
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen *value_r = p_new(mail->data_pool, const char *, 1);
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen *value_r = index_mail_get_parsed_header(mail, field_idx);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen data = buffer_get_modifiable_data(dest, &len);
38d7db318188c4ac9cdc8c6cdb936b36a5258e19Timo Sirainen /* cached as non-existing. */
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen *value_r = p_new(mail->data_pool, const char *, 1);
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen p_array_init(&header_values, mail->data_pool, 4);
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen /* cached. skip "header name: " parts in dest. */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < len; i++) {
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen /* @UNSAFE */
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen array_append(&header_values, &value, sizeof(value));
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen array_append(&header_values, &value, sizeof(value));
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainenstatic const char *const *
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainenindex_mail_headers_decode(struct index_mail *mail, const char *const *list,
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen unsigned int i, count;
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen decoded_list = p_new(mail->data_pool, const char *, count + 1);
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen buf = buffer_create_dynamic(pool_datastack_create(), 512);
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen for (i = 0; i < count; i++) {
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen if (!message_header_decode_utf8((const unsigned char *)list[i],
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenint index_mail_get_headers(struct mail *_mail, const char *field,
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen bool decode_to_utf8, const char *const **value_r)
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (index_mail_get_raw_headers(mail, field, value_r) < 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen *value_r = index_mail_headers_decode(mail, *value_r, (unsigned int)-1);
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenint index_mail_get_first_header(struct mail *_mail, const char *field,
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen const char *const *list;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (index_mail_get_raw_headers(mail, field, &list) < 0)
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen list = index_mail_headers_decode(mail, list, 1);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstatic void header_cache_callback(struct message_header_line *hdr,
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenint index_mail_get_header_stream(struct mail *_mail,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
a24b0595f0f7d3925d4c9ac26fa503ff87c43e43Timo Sirainen /* we have to parse the header. */
a24b0595f0f7d3925d4c9ac26fa503ff87c43e43Timo Sirainen if (index_mail_parse_headers(mail, _headers) < 0)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (mail_cache_lookup_headers(mail->trans->cache_view, dest,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* not in cache / error */
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (mail_get_stream(&mail->mail.mail, NULL, NULL, &input) < 0)
185ed0142fbbfb86e7a98519e7c6f11ec00723cdTimo Sirainen i_stream_create_header_filter(mail->data.stream,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenindex_header_lookup_init(struct mailbox *box, const char *const headers[])
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)box;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct mail_cache_field *fields, header_field = {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const char *const *name;
9d3ccd79130199ffdb19a688027d49bf20a4aaaaTimo Sirainen unsigned int i, count;
9d3ccd79130199ffdb19a688027d49bf20a4aaaaTimo Sirainen for (count = 0, name = headers; *name != NULL; name++)
042e2b8447b34051f0380baafcf0073704430239Timo Sirainen /* @UNSAFE: headers need to be sorted for filter stream. */
042e2b8447b34051f0380baafcf0073704430239Timo Sirainen memcpy(sorted_headers, headers, count * sizeof(*sorted_headers));
87b426af6a2365c6840b14281a98c23e903bf28eTimo Sirainen qsort(sorted_headers, count, sizeof(*sorted_headers), i_strcasecmp_p);
9d3ccd79130199ffdb19a688027d49bf20a4aaaaTimo Sirainen /* @UNSAFE */
9d3ccd79130199ffdb19a688027d49bf20a4aaaaTimo Sirainen fields = t_new(struct mail_cache_field, count);
9d3ccd79130199ffdb19a688027d49bf20a4aaaaTimo Sirainen for (i = 0; i < count; i++) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen header_field.name = t_strconcat("hdr.", headers[i], NULL);
9d3ccd79130199ffdb19a688027d49bf20a4aaaaTimo Sirainen mail_cache_register_fields(ibox->cache, fields, count);
0a9ad944630d84066c5c445eb4b657206ad88e80Timo Sirainen pool = pool_alloconly_create("index_header_lookup_ctx", 1024);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen ctx = p_new(pool, struct index_header_lookup_ctx, 1);
042e2b8447b34051f0380baafcf0073704430239Timo Sirainen /* @UNSAFE */
042e2b8447b34051f0380baafcf0073704430239Timo Sirainen for (i = 0; i < count; i++) {