virtual-config.c revision 075a53973bbdf15cc3bd2ba4872f96f3f2f00574
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomivirtual_search_args_parse(const string_t *rule, const char **error_r)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen input = i_stream_create_from_data(str_data(rule), str_len(rule));
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen parser = imap_parser_create(input, NULL, (size_t)-1);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen ret = imap_parser_finish_line(parser, 0, 0, &args);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen *error_r = t_strdup(imap_parser_get_error(parser, &fatal));
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen } else if (mail_search_build_from_imap_args(args, "UTF-8", &sargs,
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenvirtual_config_add_rule(struct virtual_parse_context *ctx, const char **error_r)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen unsigned int i, count;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen crc32_str_more(ctx->mbox->search_args_crc32, str_c(ctx->rule));
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen search_args = virtual_search_args_parse(ctx->rule, error_r);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen *error_r = t_strconcat("Previous search rule is invalid: ",
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen /* update at all the mailboxes that were introduced since the previous
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen ctx->rule_idx = array_count(&ctx->mbox->backend_boxes);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenvirtual_config_parse_line(struct virtual_parse_context *ctx, const char *line,
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen const char **error_r)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct mail_user *user = ctx->mbox->storage->storage.ns->user;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen /* continues the previous search rule */
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen /* if there is no rule yet, it means we want the previous mailboxes
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen to use the rule that comes later */
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen if (virtual_config_add_rule(ctx, error_r) < 0)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen /* new mailbox. the search args are added to it later. */
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen mail_namespace_find(user->namespaces, &line) :
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen *error_r = t_strdup_printf("Namespace not found for %s",
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen name = bbox->name[0] == '-' ? bbox->name + 1 : bbox->name;
791fb70b3255a11a91ce0c2dc3ae1460d4cf8459Timo Sirainen bbox->glob = imap_match_init(ctx->pool, name, TRUE, ctx->sep);
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen /* save messages here */
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen array_append(&ctx->mbox->backend_boxes, &bbox, 1);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainenvirtual_mailbox_get_list_patterns(struct virtual_parse_context *ctx)
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi unsigned int i, count;
95f5b08fa73ddd9a9de40a97aa141e9c74e0645eAki Tuomi bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen p_array_init(&mbox->list_include_patterns, ctx->pool, count);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen p_array_init(&mbox->list_exclude_patterns, ctx->pool, count);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen for (i = 0; i < count; i++) {
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainenseparate_wildcard_mailboxes(struct virtual_mailbox *mbox,
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen ARRAY_TYPE(virtual_backend_box) *wildcard_boxes,
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen unsigned int i, count;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen t_array_init(wildcard_boxes, I_MIN(16, count));
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen for (i = 0; i < count;) {
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes,
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainenstatic void virtual_config_copy_expanded(struct virtual_parse_context *ctx,
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen array_append(&ctx->mbox->backend_boxes, &bbox, 1);
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainenstatic bool virtual_config_match(const struct mailbox_info *info,
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen unsigned int *idx_r)
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen unsigned int i, count;
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen boxes = array_get_modifiable(boxes_arr, &count);
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen for (i = 0; i < count; i++) {
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen /* we match only one namespace for each pattern. */
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen if (strcmp(boxes[i]->name + 1, info->name) == 0) {
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainenstatic int virtual_config_expand_wildcards(struct virtual_parse_context *ctx)
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen struct mail_user *user = ctx->mbox->storage->storage.ns->user;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen ARRAY_TYPE(virtual_backend_box) wildcard_boxes, neg_boxes;
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi const char **patterns;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen unsigned int i, j, count;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen separate_wildcard_mailboxes(ctx->mbox, &wildcard_boxes, &neg_boxes);
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen /* get patterns we want to list */
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen wboxes = array_get_modifiable(&wildcard_boxes, &count);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen /* only negative wildcards - doesn't really make sense.
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen just ignore. */
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen for (i = 0; i < count; i++)
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen /* match listed mailboxes to wildcards */
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen iter = mailbox_list_iter_init_namespaces(user->namespaces, patterns,
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen while ((info = mailbox_list_iter_next(iter)) != NULL) {
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen /* skip non-selectable mailboxes (especially mbox
32bd32dcc845cd0c00d5617aea1ffbe45522b413Aki Tuomi directories) */
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen if (virtual_config_match(info, &wildcard_boxes, &i) &&
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen for (i = 0; i < count; i++)
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen mail_search_args_unref(&wboxes[i]->search_args);
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainenint virtual_config_read(struct virtual_mailbox *mbox)
unsigned int linenum = 0;
linenum++;
if (ret < 0) {
if (ret == 0) {
if (ret < 0) {
return ret;
unsigned int i, count;
for (i = 0; i < count; i++) {