1 /* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
2
3 #include "lib.h"
4 #include "sort.h"
5 #include "mail-cache.h"
6 #include "mail-storage-private.h"
7
8
9 static struct mailbox_header_lookup_ctx *
mailbox_header_lookup_init_real(struct mailbox * box,const char * const headers[])10 mailbox_header_lookup_init_real(struct mailbox *box,
11 const char *const headers[])
12 {
13 struct mail_cache_field *fields, header_field = {
14 .type = MAIL_CACHE_FIELD_HEADER,
15 .decision = MAIL_CACHE_DECISION_NO,
16 };
17 struct mailbox_header_lookup_ctx *ctx;
18 const char *const *name;
19 const char **sorted_headers, **dest_name;
20 pool_t pool;
21 unsigned int i, j, count;
22
23 i_assert(*headers != NULL);
24
25 for (count = 0, name = headers; *name != NULL; name++)
26 count++;
27
28 /* @UNSAFE: headers need to be sorted for filter stream. */
29 sorted_headers = t_new(const char *, count);
30 memcpy(sorted_headers, headers, count * sizeof(*sorted_headers));
31 i_qsort(sorted_headers, count, sizeof(*sorted_headers), i_strcasecmp_p);
32 headers = sorted_headers;
33
34 /* @UNSAFE */
35 fields = t_new(struct mail_cache_field, count);
36 for (i = j = 0; i < count; i++) {
37 if (i > 0 && strcasecmp(headers[i-1], headers[i]) == 0)
38 continue;
39 header_field.name = t_strconcat("hdr.", headers[i], NULL);
40 fields[j++] = header_field;
41 }
42 count = j;
43 mail_cache_register_fields(box->cache, fields, count);
44
45 pool = pool_alloconly_create("mailbox_header_lookup_ctx", 1024);
46 ctx = p_new(pool, struct mailbox_header_lookup_ctx, 1);
47 ctx->box = box;
48 ctx->refcount = 1;
49 ctx->pool = pool;
50 ctx->count = count;
51
52 ctx->idx = p_new(pool, unsigned int, count);
53
54 /* @UNSAFE */
55 dest_name = p_new(pool, const char *, count + 1);
56 for (i = 0; i < count; i++) {
57 ctx->idx[i] = fields[i].idx;
58 dest_name[i] = p_strdup(pool, fields[i].name + strlen("hdr."));
59 }
60 ctx->name = dest_name;
61 return ctx;
62 }
63
64 struct mailbox_header_lookup_ctx *
mailbox_header_lookup_init(struct mailbox * box,const char * const headers[])65 mailbox_header_lookup_init(struct mailbox *box, const char *const headers[])
66 {
67 struct mailbox_header_lookup_ctx *ctx;
68
69 T_BEGIN {
70 ctx = mailbox_header_lookup_init_real(box, headers);
71 } T_END;
72 return ctx;
73 }
74
mailbox_header_lookup_ref(struct mailbox_header_lookup_ctx * ctx)75 void mailbox_header_lookup_ref(struct mailbox_header_lookup_ctx *ctx)
76 {
77 i_assert(ctx->refcount > 0);
78 ctx->refcount++;
79 }
80
mailbox_header_lookup_unref(struct mailbox_header_lookup_ctx ** _ctx)81 void mailbox_header_lookup_unref(struct mailbox_header_lookup_ctx **_ctx)
82 {
83 struct mailbox_header_lookup_ctx *ctx = *_ctx;
84
85 if (ctx == NULL)
86 return;
87
88 *_ctx = NULL;
89
90 i_assert(ctx->refcount > 0);
91 if (--ctx->refcount > 0)
92 return;
93
94 pool_unref(&ctx->pool);
95 }
96
97 struct mailbox_header_lookup_ctx *
mailbox_header_lookup_merge(const struct mailbox_header_lookup_ctx * hdr1,const struct mailbox_header_lookup_ctx * hdr2)98 mailbox_header_lookup_merge(const struct mailbox_header_lookup_ctx *hdr1,
99 const struct mailbox_header_lookup_ctx *hdr2)
100 {
101 ARRAY_TYPE(const_string) names;
102 unsigned int i;
103
104 i_assert(hdr1->box == hdr2->box);
105
106 t_array_init(&names, 32);
107 for (i = 0; i < hdr1->count; i++)
108 array_push_back(&names, &hdr1->name[i]);
109 for (i = 0; i < hdr2->count; i++)
110 array_push_back(&names, &hdr2->name[i]);
111 array_append_zero(&names);
112 return mailbox_header_lookup_init(hdr1->box, array_front(&names));
113 }
114