doveadm-mail-mailbox.c revision 8c072aac2949d0c840162d1a1d334e8367fd2993
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2010-2013 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid doveadm_mailbox_args_check(const char *const args[])
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic bool cmd_mailbox_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen (type *)doveadm_mailbox_cmd_alloc_size(sizeof(type))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainencmd_mailbox_list_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct list_cmd_context *ctx = (struct list_cmd_context *)_ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainencmd_mailbox_list_run(struct doveadm_mail_cmd_context *_ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct list_cmd_context *ctx = (struct list_cmd_context *)_ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen iter_flags |= MAILBOX_LIST_ITER_SELECT_SUBSCRIBED;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen iter = doveadm_mailbox_list_iter_full_init(_ctx, user, ctx->search_args,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainendoveadm_mail_mailbox_search_args_build(const char *const args[])
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen unsigned int i;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen arg = mail_search_build_add(search_args, type);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen arg->value.str = p_strdup(search_args->pool, args[i]);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_search_arg *subargs = search_args->args;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen arg = mail_search_build_add(search_args, SEARCH_OR);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic void cmd_mailbox_list_init(struct doveadm_mail_cmd_context *_ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const args[])
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct list_cmd_context *ctx = (struct list_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->search_args = doveadm_mail_mailbox_search_args_build(args);
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_list_alloc(void)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx = doveadm_mailbox_cmd_alloc(struct list_cmd_context);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ctx.ctx.v.parse_arg = cmd_mailbox_list_parse_arg;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainencmd_mailbox_create_run(struct doveadm_mail_cmd_context *_ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *namep;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int len;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ns = mail_namespace_find(user->namespaces, name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (len > 0 && name[len-1] == mail_namespace_get_sep(ns)) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mailbox_create(box, NULL, directory) < 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen i_error("Can't subscribe to mailbox %s: %s", name,
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenstatic void cmd_mailbox_create_init(struct doveadm_mail_cmd_context *_ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const args[])
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int i;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_create_alloc(void)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx = doveadm_mailbox_cmd_alloc(struct mailbox_cmd_context);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->ctx.ctx.v.init = cmd_mailbox_create_init;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen p_array_init(&ctx->mailboxes, ctx->ctx.ctx.pool, 16);
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainenstatic int i_strcmp_reverse_p(const char *const *s1, const char *const *s2)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenget_child_mailboxes(struct mail_user *user, ARRAY_TYPE(const_string) *mailboxes,
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 while ((info = mailbox_list_iter_next(iter)) != NULL) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainencmd_mailbox_delete_run(struct doveadm_mail_cmd_context *_ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct delete_cmd_context *ctx = (struct delete_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *namep;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const ARRAY_TYPE(const_string) *mailboxes = &ctx->mailboxes;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (get_child_mailboxes(user, &recursive_mailboxes,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen doveadm_mail_failed_error(_ctx, MAIL_ERROR_TEMP);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen array_sort(&recursive_mailboxes, i_strcmp_reverse_p);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ns = mail_namespace_find(user->namespaces, name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen i_error("Can't unsubscribe mailbox %s: %s", name,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void cmd_mailbox_delete_init(struct doveadm_mail_cmd_context *_ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *const args[])
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct delete_cmd_context *ctx = (struct delete_cmd_context *)_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen array_sort(&ctx->mailboxes, i_strcmp_reverse_p);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainencmd_mailbox_delete_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct delete_cmd_context *ctx = (struct delete_cmd_context *)_ctx;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_delete_alloc(void)
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.parse_arg = cmd_mailbox_delete_parse_arg;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen p_array_init(&ctx->mailboxes, ctx->ctx.ctx.pool, 16);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainencmd_mailbox_rename_run(struct doveadm_mail_cmd_context *_ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct rename_cmd_context *ctx = (struct rename_cmd_context *)_ctx;
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 i_error("Can't rename mailbox %s to %s: %s", oldname, newname,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mailbox_set_subscribed(oldbox, FALSE) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_error("Can't unsubscribe mailbox %s: %s", ctx->oldname,
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen if (mailbox_set_subscribed(newbox, TRUE) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_error("Can't subscribe to mailbox %s: %s", ctx->newname,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void cmd_mailbox_rename_init(struct doveadm_mail_cmd_context *_ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *const args[])
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct rename_cmd_context *ctx = (struct rename_cmd_context *)_ctx;
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 Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_rename_alloc(void)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx = doveadm_mailbox_cmd_alloc(struct rename_cmd_context);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->ctx.ctx.v.init = cmd_mailbox_rename_init;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainencmd_mailbox_subscribe_run(struct doveadm_mail_cmd_context *_ctx,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *namep;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ns = mail_namespace_find(user->namespaces, name);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mailbox_set_subscribed(box, ctx->ctx.subscriptions) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "unsubscribe",
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic void cmd_mailbox_subscribe_init(struct doveadm_mail_cmd_context *_ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *const args[])
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int i;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen doveadm_mail_help_name(ctx->ctx.subscriptions ?
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "mailbox subscribe" :
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "mailbox unsubscribe");
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainencmd_mailbox_subscriptions_alloc(bool subscriptions)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx = doveadm_mail_cmd_alloc(struct mailbox_cmd_context);
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 Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_subscribe_alloc(void)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_mailbox_unsubscribe_alloc(void)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return cmd_mailbox_subscriptions_alloc(FALSE);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "[-7|-8] [-s] [<mailbox mask> [...]]"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "[-s] <mailbox> [...]"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "[-s] <mailbox> [...]"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "[-s] <old name> <new name>"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstruct doveadm_mail_cmd cmd_mailbox_subscribe = {
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen cmd_mailbox_subscribe_alloc, "mailbox subscribe",
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen "<mailbox> [...]"
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainenstruct doveadm_mail_cmd cmd_mailbox_unsubscribe = {
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen cmd_mailbox_unsubscribe_alloc, "mailbox unsubscribe",
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen "<mailbox> [...]"