doveadm-mail-mailbox.c revision 8c072aac2949d0c840162d1a1d334e8367fd2993
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2010-2013 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "lib.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "array.h"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#include "str.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "unichar.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "imap-utf7.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "mail-namespace.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "mail-storage.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "mail-search-build.h"
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen#include "doveadm-print.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "doveadm-mailbox-list-iter.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "doveadm-mail.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <stdio.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstruct doveadm_mailbox_cmd_context {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct doveadm_mail_cmd_context ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bool subscriptions;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen};
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mailbox_cmd_context {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct doveadm_mailbox_cmd_context ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ARRAY_TYPE(const_string) mailboxes;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen};
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct delete_cmd_context {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct doveadm_mailbox_cmd_context ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ARRAY_TYPE(const_string) mailboxes;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen bool recursive;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen};
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct rename_cmd_context {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct doveadm_mailbox_cmd_context ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *oldname, *newname;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen};
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstruct list_cmd_context {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct doveadm_mailbox_cmd_context ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_search_args *search_args;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bool mutf7;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen};
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid doveadm_mailbox_args_check(const char *const args[])
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (i = 0; args[i] != NULL; i++) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (!uni_utf8_str_is_valid(args[i])) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_fatal_status(EX_DATAERR,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "Mailbox name not valid UTF-8: %s", args[i]);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic bool cmd_mailbox_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct doveadm_mailbox_cmd_context *ctx =
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen (struct doveadm_mailbox_cmd_context *)_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen switch (c) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case 's':
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->subscriptions = TRUE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen default:
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen return FALSE;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen }
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen return TRUE;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#define doveadm_mailbox_cmd_alloc(type) \
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen (type *)doveadm_mailbox_cmd_alloc_size(sizeof(type))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic struct doveadm_mail_cmd_context *
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainendoveadm_mailbox_cmd_alloc_size(size_t size)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct doveadm_mail_cmd_context *ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx = doveadm_mail_cmd_alloc_size(size);
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen ctx->getopt_args = "s";
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->v.parse_arg = cmd_mailbox_parse_arg;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic bool
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainencmd_mailbox_list_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct list_cmd_context *ctx = (struct list_cmd_context *)_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen switch (c) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case '7':
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->mutf7 = TRUE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case '8':
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->mutf7 = FALSE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case 's':
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.subscriptions = TRUE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen default:
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return FALSE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return TRUE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic int
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainencmd_mailbox_list_run(struct doveadm_mail_cmd_context *_ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_user *user)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct list_cmd_context *ctx = (struct list_cmd_context *)_ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen enum mailbox_list_iter_flags iter_flags =
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen MAILBOX_LIST_ITER_RETURN_NO_FLAGS;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct doveadm_mailbox_list_iter *iter;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const struct mailbox_info *info;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen string_t *str = t_str_new(256);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (ctx->ctx.subscriptions)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen iter_flags |= MAILBOX_LIST_ITER_SELECT_SUBSCRIBED;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen iter = doveadm_mailbox_list_iter_full_init(_ctx, user, ctx->search_args,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen iter_flags);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) {
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen if (!ctx->mutf7)
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen doveadm_print(info->vname);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen else {
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainen str_truncate(str, 0);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (imap_utf8_to_utf7(info->vname, str) < 0)
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainen i_unreached();
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_print(str_c(str));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen return -1;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen return 0;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen}
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenstruct mail_search_args *
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainendoveadm_mail_mailbox_search_args_build(const char *const args[])
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen{
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct mail_search_args *search_args;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct mail_search_arg *arg;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen enum mail_search_arg_type type;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen unsigned int i;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen doveadm_mailbox_args_check(args);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen search_args = mail_search_build_init();
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen for (i = 0; args[i] != NULL; i++) {
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen if (strchr(args[i], '*') != NULL ||
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen strchr(args[i], '%') != NULL)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen type = SEARCH_MAILBOX_GLOB;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen else
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen type = SEARCH_MAILBOX;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen arg = mail_search_build_add(search_args, type);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen arg->value.str = p_strdup(search_args->pool, args[i]);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (i > 1) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_search_arg *subargs = search_args->args;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen search_args->args = NULL;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen arg = mail_search_build_add(search_args, SEARCH_OR);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen arg->value.subargs = subargs;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return search_args;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic void cmd_mailbox_list_init(struct doveadm_mail_cmd_context *_ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const args[])
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct list_cmd_context *ctx = (struct list_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_print_header("mailbox", "mailbox",
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->search_args = doveadm_mail_mailbox_search_args_build(args);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainen
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_list_alloc(void)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct list_cmd_context *ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx = doveadm_mailbox_cmd_alloc(struct list_cmd_context);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ctx.ctx.v.init = cmd_mailbox_list_init;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ctx.ctx.v.run = cmd_mailbox_list_run;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ctx.ctx.v.parse_arg = cmd_mailbox_list_parse_arg;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ctx.ctx.getopt_args = "78s";
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_print_init(DOVEADM_PRINT_TYPE_FLOW);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return &ctx->ctx.ctx;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen}
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainenstatic int
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainencmd_mailbox_create_run(struct doveadm_mail_cmd_context *_ctx,
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct mail_user *user)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_namespace *ns;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox *box;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *namep;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen int ret = 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen array_foreach(&ctx->mailboxes, namep) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *name = *namep;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int len;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen bool directory = FALSE;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ns = mail_namespace_find(user->namespaces, name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen len = strlen(name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (len > 0 && name[len-1] == mail_namespace_get_sep(ns)) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen name = t_strndup(name, len-1);
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen directory = TRUE;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen box = mailbox_alloc(ns->list, name, 0);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mailbox_create(box, NULL, directory) < 0) {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen i_error("Can't create mailbox %s: %s", name,
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen mailbox_get_last_error(box, NULL));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_mail_failed_mailbox(_ctx, box);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = -1;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (ctx->ctx.subscriptions) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mailbox_set_subscribed(box, TRUE) < 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen i_error("Can't subscribe to mailbox %s: %s", name,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox_get_last_error(box, NULL));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_mail_failed_mailbox(_ctx, box);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen ret = -1;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen mailbox_free(&box);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen return ret;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen}
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenstatic void cmd_mailbox_create_init(struct doveadm_mail_cmd_context *_ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const args[])
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *name;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int i;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (args[0] == NULL)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_mail_help_name("mailbox create");
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_mailbox_args_check(args);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen for (i = 0; args[i] != NULL; i++) {
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen name = p_strdup(ctx->ctx.ctx.pool, args[i]);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen array_append(&ctx->mailboxes, &name, 1);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen}
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_create_alloc(void)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_cmd_context *ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx = doveadm_mailbox_cmd_alloc(struct mailbox_cmd_context);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ctx.ctx.v.init = cmd_mailbox_create_init;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ctx.ctx.v.run = cmd_mailbox_create_run;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen p_array_init(&ctx->mailboxes, ctx->ctx.ctx.pool, 16);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return &ctx->ctx.ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainenstatic int i_strcmp_reverse_p(const char *const *s1, const char *const *s2)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return -strcmp(*s1, *s2);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic int
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenget_child_mailboxes(struct mail_user *user, ARRAY_TYPE(const_string) *mailboxes,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *name)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_list_iterate_context *iter;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_namespace *ns;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const struct mailbox_info *info;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *pattern, *child_name;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ns = mail_namespace_find(user->namespaces, name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen pattern = t_strdup_printf("%s%c*", name, mail_namespace_get_sep(ns));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen iter = mailbox_list_iter_init(ns->list, pattern,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen MAILBOX_LIST_ITER_RETURN_NO_FLAGS);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen while ((info = mailbox_list_iter_next(iter)) != NULL) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen child_name = t_strdup(info->vname);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen array_append(mailboxes, &child_name, 1);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return mailbox_list_iter_deinit(&iter);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic int
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainencmd_mailbox_delete_run(struct doveadm_mail_cmd_context *_ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_user *user)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct delete_cmd_context *ctx = (struct delete_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_namespace *ns;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox *box;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_storage *storage;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *namep;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ARRAY_TYPE(const_string) recursive_mailboxes;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const ARRAY_TYPE(const_string) *mailboxes = &ctx->mailboxes;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen int ret = 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (ctx->recursive) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen t_array_init(&recursive_mailboxes, 32);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen array_foreach(&ctx->mailboxes, namep) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (get_child_mailboxes(user, &recursive_mailboxes,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen *namep) < 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_mail_failed_error(_ctx, MAIL_ERROR_TEMP);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = -1;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen array_append(&recursive_mailboxes, namep, 1);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen array_sort(&recursive_mailboxes, i_strcmp_reverse_p);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailboxes = &recursive_mailboxes;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen array_foreach(mailboxes, namep) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *name = *namep;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ns = mail_namespace_find(user->namespaces, name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen box = mailbox_alloc(ns->list, name, 0);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen storage = mailbox_get_storage(box);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mailbox_delete(box) < 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen i_error("Can't delete mailbox %s: %s", name,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox_get_last_error(box, NULL));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_mail_failed_mailbox(_ctx, box);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = -1;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (ctx->ctx.subscriptions) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mailbox_set_subscribed(box, FALSE) < 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen i_error("Can't unsubscribe mailbox %s: %s", name,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mail_storage_get_last_error(storage, NULL));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_mail_failed_mailbox(_ctx, box);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = -1;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox_free(&box);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return ret;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void cmd_mailbox_delete_init(struct doveadm_mail_cmd_context *_ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *const args[])
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct delete_cmd_context *ctx = (struct delete_cmd_context *)_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *name;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (args[0] == NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen doveadm_mail_help_name("mailbox delete");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen doveadm_mailbox_args_check(args);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (i = 0; args[i] != NULL; i++) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen name = p_strdup(ctx->ctx.ctx.pool, args[i]);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen array_append(&ctx->mailboxes, &name, 1);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen array_sort(&ctx->mailboxes, i_strcmp_reverse_p);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic bool
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainencmd_mailbox_delete_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct delete_cmd_context *ctx = (struct delete_cmd_context *)_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen switch (c) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case 'r':
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->recursive = TRUE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen case 's':
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.subscriptions = TRUE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen default:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return FALSE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return TRUE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_delete_alloc(void)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct delete_cmd_context *ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx = doveadm_mailbox_cmd_alloc(struct delete_cmd_context);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.ctx.v.init = cmd_mailbox_delete_init;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.ctx.v.run = cmd_mailbox_delete_run;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.ctx.v.parse_arg = cmd_mailbox_delete_parse_arg;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.ctx.getopt_args = "rs";
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen p_array_init(&ctx->mailboxes, ctx->ctx.ctx.pool, 16);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return &ctx->ctx.ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic int
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainencmd_mailbox_rename_run(struct doveadm_mail_cmd_context *_ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct rename_cmd_context *ctx = (struct rename_cmd_context *)_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_namespace *oldns, *newns;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mailbox *oldbox, *newbox;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *oldname = ctx->oldname;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *newname = ctx->newname;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen int ret = 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen oldns = mail_namespace_find(user->namespaces, oldname);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen newns = mail_namespace_find(user->namespaces, newname);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen oldbox = mailbox_alloc(oldns->list, oldname, 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen newbox = mailbox_alloc(newns->list, newname, 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mailbox_rename(oldbox, newbox) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_error("Can't rename mailbox %s to %s: %s", oldname, newname,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_get_last_error(oldbox, NULL));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen doveadm_mail_failed_mailbox(_ctx, oldbox);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = -1;
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (ctx->ctx.subscriptions) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mailbox_set_subscribed(oldbox, FALSE) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_error("Can't unsubscribe mailbox %s: %s", ctx->oldname,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_get_last_error(oldbox, NULL));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen doveadm_mail_failed_mailbox(_ctx, oldbox);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen if (mailbox_set_subscribed(newbox, TRUE) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_error("Can't subscribe to mailbox %s: %s", ctx->newname,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_get_last_error(newbox, NULL));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen doveadm_mail_failed_mailbox(_ctx, newbox);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mailbox_free(&oldbox);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mailbox_free(&newbox);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return ret;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void cmd_mailbox_rename_init(struct doveadm_mail_cmd_context *_ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *const args[])
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct rename_cmd_context *ctx = (struct rename_cmd_context *)_ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (str_array_length(args) != 2)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen doveadm_mail_help_name("mailbox rename");
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_mailbox_args_check(args);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->oldname = p_strdup(ctx->ctx.ctx.pool, args[0]);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->newname = p_strdup(ctx->ctx.ctx.pool, args[1]);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_rename_alloc(void)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct rename_cmd_context *ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx = doveadm_mailbox_cmd_alloc(struct rename_cmd_context);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.ctx.v.init = cmd_mailbox_rename_init;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.ctx.v.run = cmd_mailbox_rename_run;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return &ctx->ctx.ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic int
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainencmd_mailbox_subscribe_run(struct doveadm_mail_cmd_context *_ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_user *user)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_namespace *ns;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox *box;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *namep;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen int ret = 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen array_foreach(&ctx->mailboxes, namep) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *name = *namep;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ns = mail_namespace_find(user->namespaces, name);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen box = mailbox_alloc(ns->list, name, 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mailbox_set_subscribed(box, ctx->ctx.subscriptions) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_error("Can't %s mailbox %s: %s", name,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.subscriptions ? "subscribe to" :
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "unsubscribe",
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mailbox_get_last_error(box, NULL));
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen doveadm_mail_failed_mailbox(_ctx, box);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_free(&box);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return ret;
db0735f9b388c5bcfb781b1b25015e898d63d953Timo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void cmd_mailbox_subscribe_init(struct doveadm_mail_cmd_context *_ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *const args[])
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *name;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int i;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (args[0] == NULL) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen doveadm_mail_help_name(ctx->ctx.subscriptions ?
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "mailbox subscribe" :
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "mailbox unsubscribe");
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen doveadm_mailbox_args_check(args);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (i = 0; args[i] != NULL; i++) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen name = p_strdup(ctx->ctx.ctx.pool, args[i]);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen array_append(&ctx->mailboxes, &name, 1);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic struct doveadm_mail_cmd_context *
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainencmd_mailbox_subscriptions_alloc(bool subscriptions)
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainen{
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainen struct mailbox_cmd_context *ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx = doveadm_mail_cmd_alloc(struct mailbox_cmd_context);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->ctx.subscriptions = subscriptions;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->ctx.ctx.v.parse_arg = cmd_mailbox_parse_arg;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->ctx.ctx.v.init = cmd_mailbox_subscribe_init;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->ctx.ctx.v.run = cmd_mailbox_subscribe_run;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen p_array_init(&ctx->mailboxes, ctx->ctx.ctx.pool, 16);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return &ctx->ctx.ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_subscribe_alloc(void)
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return cmd_mailbox_subscriptions_alloc(TRUE);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_unsubscribe_alloc(void)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return cmd_mailbox_subscriptions_alloc(FALSE);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstruct doveadm_mail_cmd cmd_mailbox_list = {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen cmd_mailbox_list_alloc, "mailbox list",
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "[-7|-8] [-s] [<mailbox mask> [...]]"
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainen};
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainenstruct doveadm_mail_cmd cmd_mailbox_create = {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen cmd_mailbox_create_alloc, "mailbox create",
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "[-s] <mailbox> [...]"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen};
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainenstruct doveadm_mail_cmd cmd_mailbox_delete = {
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen cmd_mailbox_delete_alloc, "mailbox delete",
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "[-s] <mailbox> [...]"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen};
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstruct doveadm_mail_cmd cmd_mailbox_rename = {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen cmd_mailbox_rename_alloc, "mailbox rename",
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "[-s] <old name> <new name>"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen};
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstruct doveadm_mail_cmd cmd_mailbox_subscribe = {
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen cmd_mailbox_subscribe_alloc, "mailbox subscribe",
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen "<mailbox> [...]"
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen};
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenstruct doveadm_mail_cmd cmd_mailbox_unsubscribe = {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen cmd_mailbox_unsubscribe_alloc, "mailbox unsubscribe",
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen "<mailbox> [...]"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen};
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen