-
Notifications
You must be signed in to change notification settings - Fork 271
/
index-mail.c
2217 lines (1932 loc) · 64 KB
/
index-mail.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
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2002-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "buffer.h"
#include "ioloop.h"
#include "istream.h"
#include "hex-binary.h"
#include "str.h"
#include "message-date.h"
#include "message-part-serialize.h"
#include "message-parser.h"
#include "message-snippet.h"
#include "imap-bodystructure.h"
#include "imap-envelope.h"
#include "mail-cache.h"
#include "mail-index-modseq.h"
#include "index-storage.h"
#include "istream-mail.h"
#include "index-mail.h"
#include <fcntl.h>
#define BODY_SNIPPET_ALGO_V1 "1"
#define BODY_SNIPPET_MAX_CHARS 100
struct mail_cache_field global_cache_fields[MAIL_INDEX_CACHE_FIELD_COUNT] = {
{ .name = "flags",
.type = MAIL_CACHE_FIELD_BITMASK,
.field_size = sizeof(uint32_t) },
{ .name = "date.sent",
.type = MAIL_CACHE_FIELD_FIXED_SIZE,
.field_size = sizeof(struct mail_sent_date) },
{ .name = "date.received",
.type = MAIL_CACHE_FIELD_FIXED_SIZE,
.field_size = sizeof(uint32_t) },
{ .name = "date.save",
.type = MAIL_CACHE_FIELD_FIXED_SIZE,
.field_size = sizeof(uint32_t) },
{ .name = "size.virtual",
.type = MAIL_CACHE_FIELD_FIXED_SIZE,
.field_size = sizeof(uoff_t) },
{ .name = "size.physical",
.type = MAIL_CACHE_FIELD_FIXED_SIZE,
.field_size = sizeof(uoff_t) },
{ .name = "imap.body",
.type = MAIL_CACHE_FIELD_STRING },
{ .name = "imap.bodystructure",
.type = MAIL_CACHE_FIELD_STRING },
{ .name = "imap.envelope",
.type = MAIL_CACHE_FIELD_STRING },
{ .name = "pop3.uidl",
.type = MAIL_CACHE_FIELD_STRING },
{ .name = "pop3.order",
.type = MAIL_CACHE_FIELD_FIXED_SIZE,
.field_size = sizeof(uint32_t) },
{ .name = "guid",
.type = MAIL_CACHE_FIELD_STRING },
{ .name = "mime.parts",
.type = MAIL_CACHE_FIELD_VARIABLE_SIZE },
{ .name = "binary.parts",
.type = MAIL_CACHE_FIELD_VARIABLE_SIZE },
{ .name = "body.snippet",
.type = MAIL_CACHE_FIELD_VARIABLE_SIZE }
/* FIXME: for now need to update get_metadata_precache_fields() in
index-status.c when adding more fields. those fields should probably
just be moved here to the same struct. */
};
static int index_mail_parse_body(struct index_mail *mail,
enum index_cache_field field);
int index_mail_cache_lookup_field(struct index_mail *mail, buffer_t *buf,
unsigned int field_idx)
{
int ret;
ret = mail_cache_lookup_field(mail->mail.mail.transaction->cache_view,
buf, mail->data.seq, field_idx);
if (ret > 0)
mail->mail.mail.transaction->stats.cache_hit_count++;
return ret;
}
static int get_serialized_parts(struct index_mail *mail, buffer_t **part_buf_r)
{
const unsigned int field_idx =
mail->ibox->cache_fields[MAIL_CACHE_MESSAGE_PARTS].idx;
*part_buf_r = buffer_create_dynamic(pool_datastack_create(), 128);
return index_mail_cache_lookup_field(mail, *part_buf_r, field_idx);
}
static struct message_part *get_unserialized_parts(struct index_mail *mail)
{
struct message_part *parts;
buffer_t *part_buf;
const char *error;
if (get_serialized_parts(mail, &part_buf) <= 0)
return NULL;
parts = message_part_deserialize(mail->mail.data_pool, part_buf->data,
part_buf->used, &error);
if (parts == NULL) {
mail_cache_set_corrupted(mail->mail.mail.box->cache,
"Corrupted cached mime.parts data: %s (parts=%s)",
error, binary_to_hex(part_buf->data, part_buf->used));
}
return parts;
}
static bool message_parts_have_nuls(const struct message_part *part)
{
for (; part != NULL; part = part->next) {
if ((part->flags & MESSAGE_PART_FLAG_HAS_NULS) != 0)
return TRUE;
if (part->children != NULL) {
if (message_parts_have_nuls(part->children))
return TRUE;
}
}
return FALSE;
}
static bool get_cached_parts(struct index_mail *mail)
{
struct message_part *part;
T_BEGIN {
part = get_unserialized_parts(mail);
} T_END;
if (part == NULL)
return FALSE;
/* we know the NULs now, update them */
if (message_parts_have_nuls(part)) {
mail->mail.mail.has_nuls = TRUE;
mail->mail.mail.has_no_nuls = FALSE;
} else {
mail->mail.mail.has_nuls = FALSE;
mail->mail.mail.has_no_nuls = TRUE;
}
mail->data.parts = part;
return TRUE;
}
void index_mail_set_message_parts_corrupted(struct mail *mail, const char *error)
{
buffer_t *part_buf;
const char *parts_str;
if (get_serialized_parts((struct index_mail *)mail, &part_buf) <= 0)
parts_str = "";
else
parts_str = binary_to_hex(part_buf->data, part_buf->used);
mail_set_cache_corrupted_reason(mail,
MAIL_FETCH_MESSAGE_PARTS, t_strdup_printf(
"Cached MIME parts don't match message during parsing: %s (parts=%s)",
error, parts_str));
}
static bool index_mail_get_fixed_field(struct index_mail *mail,
enum index_cache_field field,
void *data, size_t data_size)
{
const unsigned int field_idx = mail->ibox->cache_fields[field].idx;
buffer_t buf;
int ret;
buffer_create_from_data(&buf, data, data_size);
if (index_mail_cache_lookup_field(mail, &buf, field_idx) <= 0)
ret = FALSE;
else {
i_assert(buf.used == data_size);
ret = TRUE;
}
return ret;
}
bool index_mail_get_cached_uoff_t(struct index_mail *mail,
enum index_cache_field field, uoff_t *size_r)
{
return index_mail_get_fixed_field(mail, field,
size_r, sizeof(*size_r));
}
static bool index_mail_get_pvt(struct mail *_mail)
{
struct mail_private *mail = (struct mail_private *)_mail;
if (mail->seq_pvt != 0)
return TRUE;
if (_mail->box->view_pvt == NULL) {
/* no private view (set by view syncing) -> no private flags */
return FALSE;
}
if (_mail->saving) {
/* mail is still being saved, it has no private flags yet */
return FALSE;
}
i_assert(_mail->uid != 0);
index_transaction_init_pvt(_mail->transaction);
if (!mail_index_lookup_seq(_mail->transaction->view_pvt, _mail->uid,
&mail->seq_pvt))
mail->seq_pvt = 0;
return mail->seq_pvt != 0;
}
enum mail_flags index_mail_get_flags(struct mail *_mail)
{
struct mail_private *mail = (struct mail_private *)_mail;
const struct mail_index_record *rec;
enum mail_flags flags, pvt_flags_mask;
rec = mail_index_lookup(_mail->transaction->view, _mail->seq);
flags = rec->flags & (MAIL_FLAGS_NONRECENT |
MAIL_INDEX_MAIL_FLAG_BACKEND);
if (mailbox_recent_flags_have_uid(_mail->box, _mail->uid))
flags |= MAIL_RECENT;
if (index_mail_get_pvt(_mail)) {
/* mailbox has private flags */
pvt_flags_mask = mailbox_get_private_flags_mask(_mail->box);
flags &= ~pvt_flags_mask;
rec = mail_index_lookup(_mail->transaction->view_pvt,
mail->seq_pvt);
flags |= rec->flags & pvt_flags_mask;
}
return flags;
}
uint64_t index_mail_get_modseq(struct mail *_mail)
{
struct index_mail *mail = (struct index_mail *)_mail;
if (mail->data.modseq != 0)
return mail->data.modseq;
mail_index_modseq_enable(_mail->box->index);
mail->data.modseq =
mail_index_modseq_lookup(_mail->transaction->view, _mail->seq);
return mail->data.modseq;
}
uint64_t index_mail_get_pvt_modseq(struct mail *_mail)
{
struct index_mail *mail = (struct index_mail *)_mail;
if (mail->data.pvt_modseq != 0)
return mail->data.pvt_modseq;
if (mailbox_open_index_pvt(_mail->box) <= 0)
return 0;
index_transaction_init_pvt(_mail->transaction);
mail_index_modseq_enable(_mail->box->index_pvt);
mail->data.pvt_modseq =
mail_index_modseq_lookup(_mail->transaction->view_pvt,
_mail->seq);
return mail->data.pvt_modseq;
}
const char *const *index_mail_get_keywords(struct mail *_mail)
{
struct index_mail *mail = (struct index_mail *)_mail;
struct index_mail_data *data = &mail->data;
const char *const *names;
const unsigned int *keyword_indexes;
unsigned int i, count, names_count;
if (array_is_created(&data->keywords))
return array_idx(&data->keywords, 0);
(void)index_mail_get_keyword_indexes(_mail);
keyword_indexes = array_get(&data->keyword_indexes, &count);
names = array_get(mail->ibox->keyword_names, &names_count);
p_array_init(&data->keywords, mail->mail.data_pool, count + 1);
for (i = 0; i < count; i++) {
const char *name;
i_assert(keyword_indexes[i] < names_count);
name = names[keyword_indexes[i]];
array_append(&data->keywords, &name, 1);
}
/* end with NULL */
array_append_zero(&data->keywords);
return array_idx(&data->keywords, 0);
}
const ARRAY_TYPE(keyword_indexes) *
index_mail_get_keyword_indexes(struct mail *_mail)
{
struct index_mail *mail = (struct index_mail *)_mail;
struct index_mail_data *data = &mail->data;
if (!array_is_created(&data->keyword_indexes)) {
p_array_init(&data->keyword_indexes, mail->mail.data_pool, 32);
mail_index_lookup_keywords(_mail->transaction->view,
mail->data.seq,
&data->keyword_indexes);
}
return &data->keyword_indexes;
}
int index_mail_get_parts(struct mail *_mail, struct message_part **parts_r)
{
struct index_mail *mail = (struct index_mail *)_mail;
struct index_mail_data *data = &mail->data;
data->cache_fetch_fields |= MAIL_FETCH_MESSAGE_PARTS;
if (data->parts != NULL || get_cached_parts(mail)) {
*parts_r = data->parts;
return 0;
}
if (data->parser_ctx == NULL) {
if (index_mail_parse_headers(mail, NULL) < 0)
return -1;
}
data->save_message_parts = TRUE;
if (index_mail_parse_body(mail, 0) < 0)
return -1;
*parts_r = data->parts;
return 0;
}
int index_mail_get_received_date(struct mail *_mail, time_t *date_r)
{
struct index_mail *mail = (struct index_mail *)_mail;
struct index_mail_data *data = &mail->data;
data->cache_fetch_fields |= MAIL_FETCH_RECEIVED_DATE;
if (data->received_date == (time_t)-1) {
uint32_t t;
if (index_mail_get_fixed_field(mail, MAIL_CACHE_RECEIVED_DATE,
&t, sizeof(t)))
data->received_date = t;
}
*date_r = data->received_date;
return *date_r == (time_t)-1 ? -1 : 0;
}
int index_mail_get_save_date(struct mail *_mail, time_t *date_r)
{
struct index_mail *mail = (struct index_mail *)_mail;
struct index_mail_data *data = &mail->data;
data->cache_fetch_fields |= MAIL_FETCH_SAVE_DATE;
if (data->save_date == (time_t)-1) {
uint32_t t;
if (index_mail_get_fixed_field(mail, MAIL_CACHE_SAVE_DATE,
&t, sizeof(t)))
data->save_date = t;
}
*date_r = data->save_date;
return *date_r == (time_t)-1 ? -1 : 0;
}
static int index_mail_cache_sent_date(struct index_mail *mail)
{
struct index_mail_data *data = &mail->data;
const char *str;
time_t t;
int ret, tz;
if (data->sent_date.time != (uint32_t)-1)
return 0;
if ((ret = mail_get_first_header(&mail->mail.mail, "Date", &str)) < 0)
return ret;
if (ret == 0 ||
!message_date_parse((const unsigned char *)str,
strlen(str), &t, &tz)) {
/* 0 = not found / invalid */
t = 0;
tz = 0;
}
data->sent_date.time = t;
data->sent_date.timezone = tz;
index_mail_cache_add(mail, MAIL_CACHE_SENT_DATE,
&data->sent_date, sizeof(data->sent_date));
return 0;
}
int index_mail_get_date(struct mail *_mail, time_t *date_r, int *timezone_r)
{
struct index_mail *mail = (struct index_mail *)_mail;
struct index_mail_data *data = &mail->data;
struct mail_sent_date sentdate;
data->cache_fetch_fields |= MAIL_FETCH_DATE;
if (data->sent_date.time != (uint32_t)-1) {
*timezone_r = data->sent_date.timezone;
*date_r = data->sent_date.time;
return 0;
}
if (index_mail_get_fixed_field(mail, MAIL_CACHE_SENT_DATE,
&sentdate, sizeof(sentdate)))
data->sent_date = sentdate;
if (index_mail_cache_sent_date(mail) < 0)
return -1;
*timezone_r = data->sent_date.timezone;
*date_r = data->sent_date.time;
return 0;
}
static bool get_cached_msgpart_sizes(struct index_mail *mail)
{
struct index_mail_data *data = &mail->data;
if (data->parts == NULL)
(void)get_cached_parts(mail);
if (data->parts != NULL) {
data->hdr_size_set = TRUE;
data->hdr_size = data->parts->header_size;
data->body_size = data->parts->body_size;
data->body_size_set = TRUE;
data->virtual_size = data->parts->header_size.virtual_size +
data->body_size.virtual_size;
data->physical_size = data->parts->header_size.physical_size +
data->body_size.physical_size;
}
return data->parts != NULL;
}
bool index_mail_get_cached_virtual_size(struct index_mail *mail, uoff_t *size_r)
{
struct index_mail_data *data = &mail->data;
uoff_t size;
data->cache_fetch_fields |= MAIL_FETCH_VIRTUAL_SIZE;
if (data->virtual_size == (uoff_t)-1) {
if (index_mail_get_cached_uoff_t(mail,
MAIL_CACHE_VIRTUAL_FULL_SIZE,
&size))
data->virtual_size = size;
else {
if (!get_cached_msgpart_sizes(mail))
return FALSE;
}
}
if (data->hdr_size_set && data->physical_size != (uoff_t)-1) {
data->body_size.physical_size = data->physical_size -
data->hdr_size.physical_size;
data->body_size.virtual_size = data->virtual_size -
data->hdr_size.virtual_size;
data->body_size_set = TRUE;
}
*size_r = data->virtual_size;
return TRUE;
}
static void index_mail_get_cached_body_size(struct index_mail *mail)
{
struct index_mail_data *data = &mail->data;
uoff_t tmp;
if (!data->hdr_size_set)
return;
/* we've already called get_cached_msgpart_sizes() and it didn't work.
try to do this by using cached virtual size and a quick physical
size lookup. */
if (!index_mail_get_cached_virtual_size(mail, &tmp))
return;
if (!data->body_size_set) {
enum mail_lookup_abort old_abort = mail->mail.mail.lookup_abort;
/* get the physical size, but not if it requires reading
through the whole message */
if (mail->mail.mail.lookup_abort < MAIL_LOOKUP_ABORT_READ_MAIL)
mail->mail.mail.lookup_abort = MAIL_LOOKUP_ABORT_READ_MAIL;
if (mail_get_physical_size(&mail->mail.mail, &tmp) == 0) {
/* we should have everything now. try again. */
(void)index_mail_get_cached_virtual_size(mail, &tmp);
}
mail->mail.mail.lookup_abort = old_abort;
}
}
int index_mail_get_virtual_size(struct mail *_mail, uoff_t *size_r)
{
struct index_mail *mail = (struct index_mail *)_mail;
struct index_mail_data *data = &mail->data;
struct message_size hdr_size, body_size;
struct istream *input;
uoff_t old_offset;
if (index_mail_get_cached_virtual_size(mail, size_r))
return 0;
old_offset = data->stream == NULL ? 0 : data->stream->v_offset;
if (mail_get_stream(_mail, &hdr_size, &body_size, &input) < 0)
return -1;
i_stream_seek(data->stream, old_offset);
i_assert(data->virtual_size != (uoff_t)-1);
*size_r = data->virtual_size;
return 0;
}
int index_mail_get_physical_size(struct mail *_mail, uoff_t *size_r)
{
struct index_mail *mail = (struct index_mail *)_mail;
struct index_mail_data *data = &mail->data;
uoff_t size;
data->cache_fetch_fields |= MAIL_FETCH_PHYSICAL_SIZE;
if (data->physical_size == (uoff_t)-1) {
if (index_mail_get_cached_uoff_t(mail,
MAIL_CACHE_PHYSICAL_FULL_SIZE,
&size))
data->physical_size = size;
else
(void)get_cached_msgpart_sizes(mail);
}
*size_r = data->physical_size;
return *size_r == (uoff_t)-1 ? -1 : 0;
}
void index_mail_cache_add(struct index_mail *mail, enum index_cache_field field,
const void *data, size_t data_size)
{
index_mail_cache_add_idx(mail, mail->ibox->cache_fields[field].idx,
data, data_size);
}
void index_mail_cache_add_idx(struct index_mail *mail, unsigned int field_idx,
const void *data, size_t data_size)
{
struct mail *_mail = &mail->mail.mail;
const struct mail_storage_settings *set = _mail->box->storage->set;
const struct mail_index_header *hdr;
if (set->mail_cache_min_mail_count > 0) {
/* First check if we've configured caching not to be used with
low enough message count. */
hdr = mail_index_get_header(_mail->transaction->view);
if (hdr->messages_count < set->mail_cache_min_mail_count)
return;
}
if (!mail->data.no_caching &&
mail->data.dont_cache_field_idx != field_idx &&
!_mail->box->mail_cache_disabled) {
mail_cache_add(_mail->transaction->cache_trans, _mail->seq,
field_idx, data, data_size);
}
}
static void parse_bodystructure_part_header(struct message_part *part,
struct message_header_line *hdr,
pool_t pool)
{
imap_bodystructure_parse_header(pool, part, hdr);
}
static bool want_plain_bodystructure_cached(struct index_mail *mail)
{
const unsigned int cache_field_body =
mail->ibox->cache_fields[MAIL_CACHE_IMAP_BODY].idx;
const unsigned int cache_field_bodystructure =
mail->ibox->cache_fields[MAIL_CACHE_IMAP_BODYSTRUCTURE].idx;
struct mail *_mail = &mail->mail.mail;
if ((mail->data.wanted_fields & (MAIL_FETCH_IMAP_BODY |
MAIL_FETCH_IMAP_BODYSTRUCTURE)) != 0)
return TRUE;
if (mail_cache_field_want_add(_mail->transaction->cache_trans,
_mail->seq, cache_field_body))
return TRUE;
if (mail_cache_field_want_add(_mail->transaction->cache_trans,
_mail->seq, cache_field_bodystructure))
return TRUE;
return FALSE;
}
static void index_mail_body_parsed_cache_flags(struct index_mail *mail)
{
struct mail *_mail = &mail->mail.mail;
struct index_mail_data *data = &mail->data;
unsigned int cache_flags_idx;
uint32_t cache_flags = data->cache_flags;
bool want_cached;
cache_flags_idx = mail->ibox->cache_fields[MAIL_CACHE_FLAGS].idx;
want_cached = mail_cache_field_want_add(_mail->transaction->cache_trans,
_mail->seq, cache_flags_idx);
if (data->parsed_bodystructure &&
imap_bodystructure_is_plain_7bit(data->parts) &&
(want_cached || want_plain_bodystructure_cached(mail))) {
cache_flags |= MAIL_CACHE_FLAG_TEXT_PLAIN_7BIT_ASCII;
/* we need message_parts cached to be able to
actually use it in BODY/BODYSTRUCTURE reply */
want_cached = TRUE;
data->save_message_parts = TRUE;
}
/* cache flags should never get unset as long as the message doesn't
change, but try to handle it anyway */
cache_flags &= ~(MAIL_CACHE_FLAG_BINARY_HEADER |
MAIL_CACHE_FLAG_BINARY_BODY |
MAIL_CACHE_FLAG_HAS_NULS |
MAIL_CACHE_FLAG_HAS_NO_NULS);
if (message_parts_have_nuls(data->parts)) {
_mail->has_nuls = TRUE;
_mail->has_no_nuls = FALSE;
cache_flags |= MAIL_CACHE_FLAG_HAS_NULS;
} else {
_mail->has_nuls = FALSE;
_mail->has_no_nuls = TRUE;
cache_flags |= MAIL_CACHE_FLAG_HAS_NO_NULS;
}
if (data->hdr_size.virtual_size == data->hdr_size.physical_size)
cache_flags |= MAIL_CACHE_FLAG_BINARY_HEADER;
if (data->body_size.virtual_size == data->body_size.physical_size)
cache_flags |= MAIL_CACHE_FLAG_BINARY_BODY;
if (cache_flags != data->cache_flags && want_cached) {
index_mail_cache_add_idx(mail, cache_flags_idx,
&cache_flags, sizeof(cache_flags));
}
data->cache_flags = cache_flags;
}
static void index_mail_body_parsed_cache_message_parts(struct index_mail *mail)
{
struct mail *_mail = &mail->mail.mail;
struct index_mail_data *data = &mail->data;
const unsigned int cache_field =
mail->ibox->cache_fields[MAIL_CACHE_MESSAGE_PARTS].idx;
enum mail_cache_decision_type decision;
buffer_t *buffer;
if (data->messageparts_saved_to_cache ||
mail_cache_field_exists(_mail->transaction->cache_view, _mail->seq,
cache_field) != 0) {
/* already cached */
return;
}
decision = mail_cache_field_get_decision(_mail->box->cache,
cache_field);
if (decision == (MAIL_CACHE_DECISION_NO | MAIL_CACHE_DECISION_FORCED)) {
/* we never want it cached */
return;
}
if (decision == MAIL_CACHE_DECISION_NO &&
!data->save_message_parts &&
(data->wanted_fields & MAIL_FETCH_MESSAGE_PARTS) == 0) {
/* we didn't really care about the message parts themselves,
just wanted to use something that depended on it */
return;
}
T_BEGIN {
buffer = buffer_create_dynamic(pool_datastack_create(), 1024);
message_part_serialize(mail->data.parts, buffer);
index_mail_cache_add_idx(mail, cache_field,
buffer->data, buffer->used);
} T_END;
data->messageparts_saved_to_cache = TRUE;
}
static void
index_mail_body_parsed_cache_bodystructure(struct index_mail *mail,
enum index_cache_field field)
{
struct mail *_mail = &mail->mail.mail;
struct index_mail_data *data = &mail->data;
const unsigned int cache_field_parts =
mail->ibox->cache_fields[MAIL_CACHE_MESSAGE_PARTS].idx;
const unsigned int cache_field_body =
mail->ibox->cache_fields[MAIL_CACHE_IMAP_BODY].idx;
const unsigned int cache_field_bodystructure =
mail->ibox->cache_fields[MAIL_CACHE_IMAP_BODYSTRUCTURE].idx;
enum mail_cache_decision_type dec;
string_t *str;
bool bodystructure_cached = FALSE;
bool plain_bodystructure = FALSE;
bool cache_bodystructure, cache_body;
if ((data->cache_flags & MAIL_CACHE_FLAG_TEXT_PLAIN_7BIT_ASCII) != 0) {
if (data->messageparts_saved_to_cache ||
mail_cache_field_exists(_mail->transaction->cache_view,
_mail->seq, cache_field_parts) > 0) {
/* cached it as flag + message_parts */
plain_bodystructure = TRUE;
}
}
if (!data->parsed_bodystructure)
return;
i_assert(data->parts != NULL);
/* If BODY is fetched first but BODYSTRUCTURE is also wanted, we don't
normally want to first cache BODY and then BODYSTRUCTURE. So check
the wanted_fields also in here. */
if (plain_bodystructure)
cache_bodystructure = FALSE;
else if (field == MAIL_CACHE_IMAP_BODYSTRUCTURE ||
(data->wanted_fields & MAIL_FETCH_IMAP_BODYSTRUCTURE) != 0) {
cache_bodystructure =
mail_cache_field_can_add(_mail->transaction->cache_trans,
_mail->seq, cache_field_bodystructure);
} else {
cache_bodystructure =
mail_cache_field_want_add(_mail->transaction->cache_trans,
_mail->seq, cache_field_bodystructure);
}
if (cache_bodystructure) {
str = str_new(mail->mail.data_pool, 128);
imap_bodystructure_write(data->parts, str, TRUE);
data->bodystructure = str_c(str);
index_mail_cache_add(mail, MAIL_CACHE_IMAP_BODYSTRUCTURE,
str_c(str), str_len(str)+1);
bodystructure_cached = TRUE;
} else {
bodystructure_cached =
mail_cache_field_exists(_mail->transaction->cache_view,
_mail->seq, cache_field_bodystructure) > 0;
}
/* normally don't cache both BODY and BODYSTRUCTURE, but do it
if BODY is forced to be cached */
dec = mail_cache_field_get_decision(_mail->box->cache,
cache_field_body);
if (plain_bodystructure ||
(bodystructure_cached &&
(dec != (MAIL_CACHE_DECISION_FORCED | MAIL_CACHE_DECISION_YES))))
cache_body = FALSE;
else if (field == MAIL_CACHE_IMAP_BODY) {
cache_body =
mail_cache_field_can_add(_mail->transaction->cache_trans,
_mail->seq, cache_field_body);
} else {
cache_body =
mail_cache_field_want_add(_mail->transaction->cache_trans,
_mail->seq, cache_field_body);
}
if (cache_body) {
str = str_new(mail->mail.data_pool, 128);
imap_bodystructure_write(data->parts, str, FALSE);
data->body = str_c(str);
index_mail_cache_add(mail, MAIL_CACHE_IMAP_BODY,
str_c(str), str_len(str)+1);
}
}
static bool
index_mail_want_cache(struct index_mail *mail, enum index_cache_field field)
{
struct mail *_mail = &mail->mail.mail;
enum mail_fetch_field fetch_field;
unsigned int cache_field;
switch (field) {
case MAIL_CACHE_SENT_DATE:
fetch_field = MAIL_FETCH_DATE;
break;
case MAIL_CACHE_RECEIVED_DATE:
fetch_field = MAIL_FETCH_RECEIVED_DATE;
break;
case MAIL_CACHE_SAVE_DATE:
fetch_field = MAIL_FETCH_SAVE_DATE;
break;
case MAIL_CACHE_VIRTUAL_FULL_SIZE:
fetch_field = MAIL_FETCH_VIRTUAL_SIZE;
break;
case MAIL_CACHE_PHYSICAL_FULL_SIZE:
fetch_field = MAIL_FETCH_PHYSICAL_SIZE;
break;
case MAIL_CACHE_BODY_SNIPPET:
fetch_field = MAIL_FETCH_BODY_SNIPPET;
break;
default:
i_unreached();
}
if ((mail->data.dont_cache_fetch_fields & fetch_field) != 0)
return FALSE;
cache_field = mail->ibox->cache_fields[field].idx;
if ((mail->data.cache_fetch_fields & fetch_field) != 0) {
return mail_cache_field_can_add(_mail->transaction->cache_trans,
_mail->seq, cache_field);
} else {
return mail_cache_field_want_add(_mail->transaction->cache_trans,
_mail->seq, cache_field);
}
}
static void index_mail_body_parsed_cache_body_snippet(struct index_mail *mail)
{
if (mail->data.body_snippet != NULL &&
index_mail_want_cache(mail, MAIL_CACHE_BODY_SNIPPET)) {
index_mail_cache_add(mail, MAIL_CACHE_BODY_SNIPPET,
mail->data.body_snippet,
strlen(mail->data.body_snippet)+1);
}
}
static void index_mail_cache_sizes(struct index_mail *mail)
{
static enum index_cache_field size_fields[] = {
MAIL_CACHE_VIRTUAL_FULL_SIZE,
MAIL_CACHE_PHYSICAL_FULL_SIZE
};
uoff_t sizes[N_ELEMENTS(size_fields)];
unsigned int i;
sizes[0] = mail->data.virtual_size;
sizes[1] = mail->data.physical_size;
for (i = 0; i < N_ELEMENTS(size_fields); i++) {
if (sizes[i] != (uoff_t)-1 &&
index_mail_want_cache(mail, size_fields[i])) {
index_mail_cache_add(mail, size_fields[i],
&sizes[i], sizeof(sizes[i]));
}
}
}
static void index_mail_cache_dates(struct index_mail *mail)
{
static enum index_cache_field date_fields[] = {
MAIL_CACHE_RECEIVED_DATE,
MAIL_CACHE_SAVE_DATE
};
time_t dates[N_ELEMENTS(date_fields)];
unsigned int i;
uint32_t t;
dates[0] = mail->data.received_date;
dates[1] = mail->mail.mail.saving ? ioloop_time :
mail->data.save_date;
for (i = 0; i < N_ELEMENTS(date_fields); i++) {
if (dates[i] != (time_t)-1 &&
index_mail_want_cache(mail, date_fields[i])) {
t = dates[i];
index_mail_cache_add(mail, date_fields[i],
&t, sizeof(t));
}
}
if (mail->data.sent_date_parsed &&
index_mail_want_cache(mail, MAIL_CACHE_SENT_DATE))
(void)index_mail_cache_sent_date(mail);
}
static struct message_part *
index_mail_find_first_text_mime_part(struct message_part *parts)
{
struct message_part_body_data *body_data = parts->context;
struct message_part *part;
i_assert(body_data != NULL);
if (body_data->content_type == NULL ||
strcasecmp(body_data->content_type, "\"text\"") == 0) {
/* use any text/ part, even if we don't know what exactly
it is. */
return parts;
}
if (strcasecmp(body_data->content_type, "\"multipart\"") != 0) {
/* for now we support only text Content-Types */
return NULL;
}
if (strcasecmp(body_data->content_subtype, "\"alternative\"") == 0) {
/* text/plain > text/html > text/ */
struct message_part *html_part = NULL, *text_part = NULL;
for (part = parts->children; part != NULL; part = part->next) {
struct message_part_body_data *sub_body_data =
part->context;
i_assert(sub_body_data != NULL);
if (strcasecmp(sub_body_data->content_type, "\"text\"") == 0) {
if (strcasecmp(sub_body_data->content_subtype, "\"plain\"") == 0)
return part;
if (strcasecmp(sub_body_data->content_subtype, "\"html\"") == 0)
html_part = part;
else
text_part = part;
}
}
return html_part != NULL ? html_part : text_part;
}
/* find the first usable MIME part */
for (part = parts->children; part != NULL; part = part->next) {
struct message_part *subpart =
index_mail_find_first_text_mime_part(part);
if (subpart != NULL)
return subpart;
}
return NULL;
}
static int index_mail_write_body_snippet(struct index_mail *mail)
{
struct message_part *part;
struct istream *input;
uoff_t old_offset;
string_t *str;
int ret;
i_assert(mail->data.parsed_bodystructure);
part = index_mail_find_first_text_mime_part(mail->data.parts);
if (part == NULL) {
mail->data.body_snippet = BODY_SNIPPET_ALGO_V1;
return 0;
}
old_offset = mail->data.stream == NULL ? 0 : mail->data.stream->v_offset;
if (mail_get_stream(&mail->mail.mail, NULL, NULL, &input) < 0)
return -1;
i_assert(mail->data.stream != NULL);
i_stream_seek(input, part->physical_pos);
input = i_stream_create_limit(input, part->header_size.physical_size +
part->body_size.physical_size);
str = str_new(mail->mail.data_pool, 128);
str_append(str, BODY_SNIPPET_ALGO_V1);
ret = message_snippet_generate(input, BODY_SNIPPET_MAX_CHARS, str);
if (ret == 0)
mail->data.body_snippet = str_c(str);
i_stream_destroy(&input);
i_stream_seek(mail->data.stream, old_offset);
return ret;
}
static int
index_mail_parse_body_finish(struct index_mail *mail,
enum index_cache_field field, bool success)
{
struct istream *parser_input = mail->data.parser_input;
const char *error = NULL;
int ret;
if (parser_input == NULL) {
ret = message_parser_deinit_from_parts(&mail->data.parser_ctx,
&mail->data.parts, &error) < 0 ? 0 : 1;
} else {
mail->data.parser_input = NULL;
i_stream_ref(parser_input);
ret = message_parser_deinit_from_parts(&mail->data.parser_ctx,
&mail->data.parts, &error) < 0 ? 0 : 1;
if (success && (parser_input->stream_errno == 0 ||
parser_input->stream_errno == EPIPE)) {
/* do one final read, which verifies that the message
size is correct. */
if (i_stream_read(parser_input) != -1 ||
i_stream_have_bytes_left(parser_input))
i_unreached();
}
/* EPIPE = input already closed. allow the caller to
decide if that is an error or not. (for example we
could be coming here from IMAP APPEND when IMAP
client has closed the connection too early. we
don't want to log an error in that case.) */
if (parser_input->stream_errno != 0 &&
parser_input->stream_errno != EPIPE) {
index_mail_stream_log_failure_for(mail, parser_input);
ret = -1;
}
i_stream_unref(&parser_input);
}