doveadm-mail-list-iter.c revision d28f3e9322095648773e6158503e1d6505f00880
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2010 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
18398a5d21c88cbb34c601c6b6c1f9dea502e1caTimo Sirainen#include "array.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "str.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-search.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-namespace.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mailbox-list.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "doveadm-mail-list-iter.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct doveadm_mail_list_iter {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_search_args *search_args;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mailbox_list_iter_flags iter_flags;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mailbox_list_iterate_context *iter;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int
55a210942dc7da58b2fd0b11bed8da6b030af5c1Timo Sirainensearch_args_get_mailbox_patterns(const struct mail_search_arg *args,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ARRAY_TYPE(const_string) *patterns)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mail_search_arg *subargs;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (; args != NULL; args = args->next) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen switch (args->type) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case SEARCH_OR:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case SEARCH_SUB:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case SEARCH_INTHREAD:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen subargs = args->value.subargs;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (; subargs != NULL; subargs = subargs->next) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!search_args_get_mailbox_patterns(subargs,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen patterns))
d5960ce1c0adda5c9e259bc429123ebc29c60baeTimo Sirainen return 0;
d5960ce1c0adda5c9e259bc429123ebc29c60baeTimo Sirainen }
d5960ce1c0adda5c9e259bc429123ebc29c60baeTimo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case SEARCH_MAILBOX:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case SEARCH_MAILBOX_GLOB:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (args->not) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen array_clear(patterns);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen array_append(patterns, &args->value.str, 1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen default:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct doveadm_mail_list_iter *
doveadm_mail_list_iter_init(struct mail_user *user,
struct mail_search_args *search_args,
enum mailbox_list_iter_flags iter_flags)
{
static const char *all_pattern = "*";
struct doveadm_mail_list_iter *iter;
ARRAY_TYPE(const_string) patterns;
i_assert((iter_flags & MAILBOX_LIST_ITER_VIRTUAL_NAMES) != 0);
iter = i_new(struct doveadm_mail_list_iter, 1);
iter->search_args = search_args;
t_array_init(&patterns, 16);
search_args_get_mailbox_patterns(search_args->args, &patterns);
if (array_count(&patterns) == 0) {
iter_flags |= MAILBOX_LIST_ITER_SKIP_ALIASES;
array_append(&patterns, &all_pattern, 1);
} else {
iter_flags |= MAILBOX_LIST_ITER_STAR_WITHIN_NS;
}
(void)array_append_space(&patterns);
iter->iter_flags = iter_flags;
iter->iter = mailbox_list_iter_init_namespaces(user->namespaces,
array_idx(&patterns, 0),
NAMESPACE_PRIVATE,
iter_flags);
return iter;
}
void doveadm_mail_list_iter_deinit(struct doveadm_mail_list_iter **_iter)
{
struct doveadm_mail_list_iter *iter = *_iter;
*_iter = NULL;
if (mailbox_list_iter_deinit(&iter->iter) < 0)
i_error("Listing mailboxes failed");
i_free(iter);
}
const struct mailbox_info *
doveadm_mail_list_iter_next(struct doveadm_mail_list_iter *iter)
{
const struct mailbox_info *info;
while ((info = mailbox_list_iter_next(iter->iter)) != NULL) {
if (mail_search_args_match_mailbox(iter->search_args,
info->name, info->ns->sep))
break;
}
return info;
}