virtual-config.c revision ebc7480f2d7f235f2fd58f91b52f8a19f53ca26a
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen/* Copyright (c) 2008-2010 Dovecot authors, see the included COPYING file */
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainenvirtual_search_args_parse(const string_t *rule, const char **error_r)
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen input = i_stream_create_from_data(str_data(rule), str_len(rule));
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen imap_parser = imap_parser_create(input, NULL, (size_t)-1);
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen ret = imap_parser_finish_line(imap_parser, 0, 0, &args);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = t_strdup(imap_parser_get_error(imap_parser, &fatal));
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (mail_search_build(mail_search_register_get_imap(),
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenvirtual_config_add_rule(struct virtual_parse_context *ctx, const char **error_r)
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen unsigned int i, count;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen crc32_str_more(ctx->mbox->search_args_crc32, str_c(ctx->rule));
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen search_args = virtual_search_args_parse(ctx->rule, error_r);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = t_strconcat("Previous search rule is invalid: ",
4c61d6b6ad6173f06563c2cee9bd813c59277dd2Timo Sirainen /* update at all the mailboxes that were introduced since the previous
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen ctx->rule_idx = array_count(&ctx->mbox->backend_boxes);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenvirtual_config_parse_line(struct virtual_parse_context *ctx, const char *line,
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen const char **error_r)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct mail_user *user = ctx->mbox->storage->storage.user;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* continues the previous search rule */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
4c61d6b6ad6173f06563c2cee9bd813c59277dd2Timo Sirainen /* if there is no rule yet, it means we want the previous mailboxes
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen to use the rule that comes later */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (virtual_config_add_rule(ctx, error_r) < 0)
fac865bad1ba10e85d80b63dedfd3493a65510d4Timo Sirainen /* new mailbox. the search args are added to it later. */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = t_strdup_printf("Namespace not found for %s",
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen name = bbox->name[0] == '-' ? bbox->name + 1 : bbox->name;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila bbox->glob = imap_match_init(ctx->pool, name, TRUE, ctx->sep);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* save messages here */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila array_append(&ctx->mbox->backend_boxes, &bbox, 1);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilavirtual_mailbox_get_list_patterns(struct virtual_parse_context *ctx)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int i, count;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila p_array_init(&mbox->list_include_patterns, ctx->pool, count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila p_array_init(&mbox->list_exclude_patterns, ctx->pool, count);
957b0b4c9aeff7153bb9ebf91d8aea550bd07865Teemu Huovila for (i = 0; i < count; i++) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilaseparate_wildcard_mailboxes(struct virtual_mailbox *mbox,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila ARRAY_TYPE(virtual_backend_box) *wildcard_boxes,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int i, count;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila t_array_init(wildcard_boxes, I_MIN(16, count));
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila for (i = 0; i < count;) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila bboxes = array_get_modifiable(&mbox->backend_boxes,
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovilastatic void virtual_config_copy_expanded(struct virtual_parse_context *ctx,
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen array_append(&ctx->mbox->backend_boxes, &bbox, 1);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilastatic bool virtual_config_match(const struct mailbox_info *info,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int *idx_r)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int i, count;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila boxes = array_get_modifiable(boxes_arr, &count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila for (i = 0; i < count; i++) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* we match only one namespace for each pattern. */
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila if (strcmp(boxes[i]->name + 1, info->name) == 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstatic int virtual_config_expand_wildcards(struct virtual_parse_context *ctx)
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila NAMESPACE_PRIVATE | NAMESPACE_SHARED | NAMESPACE_PUBLIC;
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila const enum mailbox_list_iter_flags iter_flags =
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct mail_user *user = ctx->mbox->storage->storage.user;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen ARRAY_TYPE(virtual_backend_box) wildcard_boxes, neg_boxes;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen unsigned int i, j, count;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen separate_wildcard_mailboxes(ctx->mbox, &wildcard_boxes, &neg_boxes);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* get patterns we want to list */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen wboxes = array_get_modifiable(&wildcard_boxes, &count);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* only negative wildcards - doesn't really make sense.
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen just ignore. */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen for (i = 0; i < count; i++)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* match listed mailboxes to wildcards */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen iter = mailbox_list_iter_init_namespaces(user->namespaces, patterns,
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen while ((info = mailbox_list_iter_next(iter)) != NULL) {
60ba197d17023594231d9805d889817782e41859Timo Sirainen /* skip non-selectable mailboxes (especially mbox
60ba197d17023594231d9805d889817782e41859Timo Sirainen directories) */
60ba197d17023594231d9805d889817782e41859Timo Sirainen if (virtual_config_match(info, &wildcard_boxes, &i) &&
60ba197d17023594231d9805d889817782e41859Timo Sirainen !virtual_config_match(info, &neg_boxes, &j) &&
60ba197d17023594231d9805d889817782e41859Timo Sirainen for (i = 0; i < count; i++)
60ba197d17023594231d9805d889817782e41859Timo Sirainen mail_search_args_unref(&wboxes[i]->search_args);
60ba197d17023594231d9805d889817782e41859Timo Sirainenstatic void virtual_config_search_args_dup(struct virtual_mailbox *mbox)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen unsigned int i, count;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen for (i = 0; i < count; i++) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bboxes[i]->search_args = mail_search_args_dup(old_args);
60ba197d17023594231d9805d889817782e41859Timo Sirainenint virtual_config_read(struct virtual_mailbox *mbox)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct mail_storage *storage = mbox->box.storage;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen unsigned int linenum = 0;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen path = t_strconcat(mbox->box.path, "/"VIRTUAL_CONFIG_FNAME, NULL);
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen mail_error_eacces_msg("stat", mbox->box.path));
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila mail_storage_set_error(storage, MAIL_ERROR_NOTPOSSIBLE,
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila "Virtual mailbox missing configuration file");
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_storage_set_error(storage, MAIL_ERROR_NOTFOUND,
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen T_MAIL_ERR_MAILBOX_NOT_FOUND(mbox->box.name));
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen ctx.sep = mail_namespaces_get_root_sep(storage->user->namespaces);
linenum++;
if (ret < 0) {
if (ret == 0) {
if (ret < 0) {
if (ret == 0)
return ret;
unsigned int i, count;
for (i = 0; i < count; i++) {