mailbox-header.c revision 02c335c23bf5fa225a467c19f2c063fb0dc7b8c3
1042N/A/* Copyright (c) 2003-2016 Dovecot authors, see the included COPYING file */
2362N/A
1042N/A#include "lib.h"
1042N/A#include "mail-cache.h"
1042N/A#include "mail-storage-private.h"
1042N/A
1042N/A
1042N/Astatic struct mailbox_header_lookup_ctx *
1042N/Amailbox_header_lookup_init_real(struct mailbox *box,
1042N/A const char *const headers[])
1042N/A{
1042N/A struct mail_cache_field *fields, header_field = {
1042N/A .type = MAIL_CACHE_FIELD_HEADER,
1042N/A .decision = MAIL_CACHE_DECISION_TEMP
1042N/A };
1042N/A struct mailbox_header_lookup_ctx *ctx;
1042N/A const char *const *name;
1042N/A const char **sorted_headers, **dest_name;
2362N/A pool_t pool;
2362N/A unsigned int i, count;
2362N/A
1042N/A i_assert(*headers != NULL);
1042N/A
1042N/A for (count = 0, name = headers; *name != NULL; name++)
1042N/A count++;
1042N/A
1042N/A /* @UNSAFE: headers need to be sorted for filter stream. */
1042N/A sorted_headers = t_new(const char *, count);
1042N/A memcpy(sorted_headers, headers, count * sizeof(*sorted_headers));
i_qsort(sorted_headers, count, sizeof(*sorted_headers), i_strcasecmp_p);
headers = sorted_headers;
/* @UNSAFE */
fields = t_new(struct mail_cache_field, count);
for (i = 0; i < count; i++) {
header_field.name = t_strconcat("hdr.", headers[i], NULL);
fields[i] = header_field;
}
mail_cache_register_fields(box->cache, fields, count);
pool = pool_alloconly_create("mailbox_header_lookup_ctx", 1024);
ctx = p_new(pool, struct mailbox_header_lookup_ctx, 1);
ctx->box = box;
ctx->refcount = 1;
ctx->pool = pool;
ctx->count = count;
ctx->idx = p_new(pool, unsigned int, count);
/* @UNSAFE */
dest_name = p_new(pool, const char *, count + 1);
for (i = 0; i < count; i++) {
ctx->idx[i] = fields[i].idx;
dest_name[i] = p_strdup(pool, headers[i]);
}
ctx->name = dest_name;
return ctx;
}
struct mailbox_header_lookup_ctx *
mailbox_header_lookup_init(struct mailbox *box, const char *const headers[])
{
struct mailbox_header_lookup_ctx *ctx;
T_BEGIN {
ctx = mailbox_header_lookup_init_real(box, headers);
} T_END;
return ctx;
}
void mailbox_header_lookup_ref(struct mailbox_header_lookup_ctx *ctx)
{
i_assert(ctx->refcount > 0);
ctx->refcount++;
}
void mailbox_header_lookup_unref(struct mailbox_header_lookup_ctx **_ctx)
{
struct mailbox_header_lookup_ctx *ctx = *_ctx;
*_ctx = NULL;
i_assert(ctx->refcount > 0);
if (--ctx->refcount > 0)
return;
pool_unref(&ctx->pool);
}