virtual-config.c revision eca30f1fe8556c46abc75c94d03f59b2e89d4162
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2008-2010 Dovecot authors, see the included COPYING file */
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "lib.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "array.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "crc32.h"
3343a61404603b21c246783a7963b77833095f31Timo Sirainen#include "istream.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "str.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "imap-parser.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "imap-match.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "mail-search-build.h"
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen#include "virtual-storage.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "virtual-plugin.h"
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainen
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainen#include <unistd.h>
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen#include <fcntl.h>
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainenstruct virtual_parse_context {
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen struct virtual_mailbox *mbox;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen struct istream *input;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen pool_t pool;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen string_t *rule;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen unsigned int rule_idx;
42456ec33fe65feb411890f99d436071e0185ee3Timo Sirainen
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen char sep;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen bool have_wildcards;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen};
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainenstatic struct mail_search_args *
42456ec33fe65feb411890f99d436071e0185ee3Timo Sirainenvirtual_search_args_parse(const string_t *rule, const char **error_r)
42456ec33fe65feb411890f99d436071e0185ee3Timo Sirainen{
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen struct istream *input;
3f190f4cbb9233a3a6830956cb5c7ae56a577b79Timo Sirainen struct imap_parser *parser;
3f190f4cbb9233a3a6830956cb5c7ae56a577b79Timo Sirainen const struct imap_arg *args;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen struct mail_search_args *sargs;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen bool fatal;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen int ret;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen if (str_len(rule) == 0) {
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen sargs = mail_search_build_init();
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen mail_search_build_add_all(sargs);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen return sargs;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen }
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen input = i_stream_create_from_data(str_data(rule), str_len(rule));
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen (void)i_stream_read(input);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen parser = imap_parser_create(input, NULL, (size_t)-1);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen ret = imap_parser_finish_line(parser, 0, 0, &args);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen if (ret < 0) {
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen sargs = NULL;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen *error_r = t_strdup(imap_parser_get_error(parser, &fatal));
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen } else if (mail_search_build_from_imap_args(args, "UTF-8", &sargs,
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen error_r) < 0)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen sargs = NULL;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen imap_parser_destroy(&parser);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen i_stream_destroy(&input);
0d16525a729011f4fced989a3da74d755ea49e6dTimo Sirainen return sargs;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen}
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainenstatic int
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainenvirtual_config_add_rule(struct virtual_parse_context *ctx, const char **error_r)
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen{
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen struct virtual_backend_box *const *bboxes;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen struct mail_search_args *search_args;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen unsigned int i, count;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen i_assert(str_len(ctx->rule) == 0);
42456ec33fe65feb411890f99d436071e0185ee3Timo Sirainen return 0;
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen }
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen ctx->mbox->search_args_crc32 =
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen crc32_str_more(ctx->mbox->search_args_crc32, str_c(ctx->rule));
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen search_args = virtual_search_args_parse(ctx->rule, error_r);
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen str_truncate(ctx->rule, 0);
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen if (search_args == NULL) {
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen *error_r = t_strconcat("Previous search rule is invalid: ",
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen *error_r, NULL);
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen return -1;
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen }
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainen /* update at all the mailboxes that were introduced since the previous
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainen rule. */
d42eb03b3a4e79a2da22a1be2de59b95660af2beTimo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainen i_assert(ctx->rule_idx < count);
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen for (i = ctx->rule_idx; i < count; i++) {
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen i_assert(bboxes[i]->search_args == NULL);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen mail_search_args_ref(search_args);
ed63764502561bbeb12fe03878fb33a82b89bf27Timo Sirainen bboxes[i]->search_args = search_args;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen }
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen mail_search_args_unref(&search_args);
8ed8c821ba8aab0b4ed0375f87d48737ef0e0d8eTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen ctx->rule_idx = array_count(&ctx->mbox->backend_boxes);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen return 0;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen}
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainenstatic int
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainenvirtual_config_parse_line(struct virtual_parse_context *ctx, const char *line,
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen const char **error_r)
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen{
ced118ac5caf6fe83d34339c2c65c63b2aa768acTimo Sirainen struct mail_user *user = ctx->mbox->storage->storage.user;
ced118ac5caf6fe83d34339c2c65c63b2aa768acTimo Sirainen struct virtual_backend_box *bbox;
42456ec33fe65feb411890f99d436071e0185ee3Timo Sirainen const char *name;
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen if (*line == ' ' || *line == '\t') {
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen /* continues the previous search rule */
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen *error_r = "Search rule without a mailbox";
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen return -1;
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen }
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen str_append(ctx->rule, line);
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen return 0;
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen }
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen /* if there is no rule yet, it means we want the previous mailboxes
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen to use the rule that comes later */
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen if (str_len(ctx->rule) > 0) {
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen if (virtual_config_add_rule(ctx, error_r) < 0)
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen return -1;
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen }
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen /* new mailbox. the search args are added to it later. */
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen if (strcasecmp(line, "INBOX") == 0)
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen line = "INBOX";
0a601ada15c7fe82f0db895fc2068b71b3a5243cTimo Sirainen bbox->name = p_strdup(ctx->pool, line);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen if (*line == '-') line++;
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen bbox->ns = strcasecmp(line, "!INBOX") != 0 ?
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen mail_namespace_find(user->namespaces, &line) :
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen mail_namespace_find_inbox(user->namespaces);
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen if (bbox->ns == NULL) {
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen *error_r = t_strdup_printf("Namespace not found for %s",
42456ec33fe65feb411890f99d436071e0185ee3Timo Sirainen bbox->name);
424236b2b88a5a7bbde5cf6a6b32189ca3437629Timo Sirainen return -1;
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen }
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen if (strchr(bbox->name, '*') != NULL ||
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen strchr(bbox->name, '%') != NULL) {
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainen name = bbox->name[0] == '-' ? bbox->name + 1 : bbox->name;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen bbox->glob = imap_match_init(ctx->pool, name, TRUE, ctx->sep);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen ctx->have_wildcards = TRUE;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen } else if (bbox->name[0] == '!') {
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen /* save messages here */
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen if (ctx->mbox->save_bbox != NULL) {
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen *error_r = "Multiple save mailboxes defined";
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen return -1;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen }
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen bbox->name++;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen ctx->mbox->save_bbox = bbox;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen }
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen array_append(&ctx->mbox->backend_boxes, &bbox, 1);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen return 0;
7501b9f694460101b41d1d708ebc3ec2b0400b1cTimo Sirainen}
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainen
7501b9f694460101b41d1d708ebc3ec2b0400b1cTimo Sirainenstatic void
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainenvirtual_mailbox_get_list_patterns(struct virtual_parse_context *ctx)
690bafa70767e3f6e98bbfd62ad4a26be2387ea9Timo Sirainen{
690bafa70767e3f6e98bbfd62ad4a26be2387ea9Timo Sirainen struct virtual_mailbox *mbox = ctx->mbox;
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen ARRAY_TYPE(mailbox_virtual_patterns) *dest;
7501b9f694460101b41d1d708ebc3ec2b0400b1cTimo Sirainen struct mailbox_virtual_pattern pattern;
7501b9f694460101b41d1d708ebc3ec2b0400b1cTimo Sirainen struct virtual_backend_box *const *bboxes;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen unsigned int i, count;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen memset(&pattern, 0, sizeof(pattern));
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen p_array_init(&mbox->list_include_patterns, ctx->pool, count);
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen p_array_init(&mbox->list_exclude_patterns, ctx->pool, count);
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen for (i = 0; i < count; i++) {
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen pattern.ns = bboxes[i]->ns;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen pattern.pattern = bboxes[i]->name;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen if (*pattern.pattern != '-')
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen dest = &mbox->list_include_patterns;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen else {
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen dest = &mbox->list_exclude_patterns;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen pattern.pattern++;
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen }
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen array_append(dest, &pattern, 1);
1b5366b2234892f8930a29351da06b193e385150Timo Sirainen }
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen}
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
e5afebd2df1d4990f7bec2a839260ff2e6d78168Timo Sirainenstatic void
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainenseparate_wildcard_mailboxes(struct virtual_mailbox *mbox,
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen ARRAY_TYPE(virtual_backend_box) *wildcard_boxes,
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen ARRAY_TYPE(virtual_backend_box) *neg_boxes)
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen{
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen struct virtual_backend_box *const *bboxes;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen ARRAY_TYPE(virtual_backend_box) *dest;
690bafa70767e3f6e98bbfd62ad4a26be2387ea9Timo Sirainen unsigned int i, count;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainen t_array_init(wildcard_boxes, I_MIN(16, count));
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen t_array_init(neg_boxes, 4);
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen for (i = 0; i < count;) {
690bafa70767e3f6e98bbfd62ad4a26be2387ea9Timo Sirainen if (*bboxes[i]->name == '-')
690bafa70767e3f6e98bbfd62ad4a26be2387ea9Timo Sirainen dest = neg_boxes;
6060b7c8edf8fce73470d0df6a2479b69b01c537Timo Sirainen else if (bboxes[i]->glob != NULL)
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen dest = wildcard_boxes;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen else {
690bafa70767e3f6e98bbfd62ad4a26be2387ea9Timo Sirainen dest = NULL;
3343a61404603b21c246783a7963b77833095f31Timo Sirainen i++;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen }
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
3343a61404603b21c246783a7963b77833095f31Timo Sirainen if (dest != NULL) {
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen array_append(dest, &bboxes[i], 1);
3343a61404603b21c246783a7963b77833095f31Timo Sirainen array_delete(&mbox->backend_boxes, i, 1);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes,
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen &count);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen }
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen }
}
static void virtual_config_copy_expanded(struct virtual_parse_context *ctx,
struct virtual_backend_box *wbox,
const char *name)
{
struct virtual_backend_box *bbox;
bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
*bbox = *wbox;
bbox->name = p_strdup(ctx->pool, name);
bbox->glob = NULL;
bbox->wildcard = TRUE;
mail_search_args_ref(bbox->search_args);
array_append(&ctx->mbox->backend_boxes, &bbox, 1);
}
static bool virtual_config_match(const struct mailbox_info *info,
ARRAY_TYPE(virtual_backend_box) *boxes_arr,
unsigned int *idx_r)
{
struct virtual_backend_box *const *boxes;
unsigned int i, count;
boxes = array_get_modifiable(boxes_arr, &count);
for (i = 0; i < count; i++) {
if (boxes[i]->glob != NULL) {
/* we match only one namespace for each pattern. */
if (boxes[i]->ns == info->ns &&
imap_match(boxes[i]->glob,
info->name) == IMAP_MATCH_YES) {
*idx_r = i;
return TRUE;
}
} else {
i_assert(boxes[i]->name[0] == '-');
if (strcmp(boxes[i]->name + 1, info->name) == 0) {
*idx_r = i;
return TRUE;
}
}
}
return FALSE;
}
static int virtual_config_expand_wildcards(struct virtual_parse_context *ctx)
{
struct mail_user *user = ctx->mbox->storage->storage.user;
ARRAY_TYPE(virtual_backend_box) wildcard_boxes, neg_boxes;
struct mailbox_list_iterate_context *iter;
struct virtual_backend_box *const *wboxes;
const char **patterns;
const struct mailbox_info *info;
unsigned int i, j, count;
separate_wildcard_mailboxes(ctx->mbox, &wildcard_boxes, &neg_boxes);
/* get patterns we want to list */
wboxes = array_get_modifiable(&wildcard_boxes, &count);
if (count == 0) {
/* only negative wildcards - doesn't really make sense.
just ignore. */
return 0;
}
patterns = t_new(const char *, count + 1);
for (i = 0; i < count; i++)
patterns[i] = wboxes[i]->name;
/* match listed mailboxes to wildcards */
iter = mailbox_list_iter_init_namespaces(user->namespaces, patterns,
MAILBOX_LIST_ITER_VIRTUAL_NAMES |
MAILBOX_LIST_ITER_RETURN_NO_FLAGS);
while ((info = mailbox_list_iter_next(iter)) != NULL) {
/* skip non-selectable mailboxes (especially mbox
directories) */
if ((info->flags & MAILBOX_NOSELECT) != 0)
continue;
if (virtual_config_match(info, &wildcard_boxes, &i) &&
!virtual_config_match(info, &neg_boxes, &j) &&
virtual_backend_box_lookup_name(ctx->mbox,
info->name) == NULL) {
virtual_config_copy_expanded(ctx, wboxes[i],
info->name);
}
}
for (i = 0; i < count; i++)
mail_search_args_unref(&wboxes[i]->search_args);
return mailbox_list_iter_deinit(&iter);
}
static void virtual_config_search_args_dup(struct virtual_mailbox *mbox)
{
struct virtual_backend_box *const *bboxes;
struct mail_search_args *old_args;
unsigned int i, count;
bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
for (i = 0; i < count; i++) {
old_args = bboxes[i]->search_args;
bboxes[i]->search_args = mail_search_args_dup(old_args);
mail_search_args_unref(&old_args);
}
}
int virtual_config_read(struct virtual_mailbox *mbox)
{
struct mail_user *user = mbox->storage->storage.user;
struct virtual_parse_context ctx;
const char *path, *line, *error;
unsigned int linenum = 0;
int fd, ret = 0;
i_array_init(&mbox->backend_boxes, 8);
mbox->search_args_crc32 = (uint32_t)-1;
path = t_strconcat(mbox->box.path, "/"VIRTUAL_CONFIG_FNAME, NULL);
fd = open(path, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
mailbox_list_set_error(mbox->box.list,
MAIL_ERROR_NOTPOSSIBLE,
"Virtual mailbox missing configuration file");
return -1;
}
mailbox_list_set_critical(mbox->box.list,
"open(%s) failed: %m", path);
return -1;
}
memset(&ctx, 0, sizeof(ctx));
ctx.sep = mail_namespaces_get_root_sep(user->namespaces);
ctx.mbox = mbox;
ctx.pool = mbox->box.pool;
ctx.rule = t_str_new(256);
ctx.input = i_stream_create_fd(fd, (size_t)-1, FALSE);
i_stream_set_return_partial_line(ctx.input, TRUE);
while ((line = i_stream_read_next_line(ctx.input)) != NULL) {
linenum++;
if (*line == '#')
continue;
if (*line == '\0')
ret = virtual_config_add_rule(&ctx, &error);
else
ret = virtual_config_parse_line(&ctx, line, &error);
if (ret < 0) {
mailbox_list_set_critical(mbox->box.list,
"%s: Error at line %u: %s",
path, linenum, error);
break;
}
}
if (ret == 0) {
ret = virtual_config_add_rule(&ctx, &error);
if (ret < 0) {
mailbox_list_set_critical(mbox->box.list,
"%s: Error at line %u: %s",
path, linenum, error);
}
}
virtual_mailbox_get_list_patterns(&ctx);
if (ret == 0 && ctx.have_wildcards)
ret = virtual_config_expand_wildcards(&ctx);
if (ret == 0 && array_count(&mbox->backend_boxes) == 0) {
mailbox_list_set_critical(mbox->box.list,
"%s: No mailboxes defined", path);
ret = -1;
}
if (ret == 0)
virtual_config_search_args_dup(mbox);
i_stream_unref(&ctx.input);
(void)close(fd);
return ret;
}
void virtual_config_free(struct virtual_mailbox *mbox)
{
struct virtual_backend_box *const *bboxes;
unsigned int i, count;
bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
for (i = 0; i < count; i++) {
if (bboxes[i]->search_args != NULL)
mail_search_args_unref(&bboxes[i]->search_args);
}
array_free(&mbox->backend_boxes);
}