From: Timo Sirainen Date: Thu, 13 May 2010 10:05:24 +0000 (+0200) Subject: doveadm: Added subscribe/unsubscribe commands. X-Git-Tag: 2.0.beta6~230 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ecbbdf594f9329fc15a182bd6c7c4a7fb144ed74;p=thirdparty%2Fdovecot%2Fcore.git doveadm: Added subscribe/unsubscribe commands. --HG-- branch : HEAD --- diff --git a/src/doveadm/doveadm-mail-mailbox.c b/src/doveadm/doveadm-mail-mailbox.c index 23c10b86b3..378213c0c8 100644 --- a/src/doveadm/doveadm-mail-mailbox.c +++ b/src/doveadm/doveadm-mail-mailbox.c @@ -366,6 +366,80 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_rename_alloc(void) return &ctx->ctx.ctx; } +static void +cmd_mailbox_subscribe_run(struct doveadm_mail_cmd_context *_ctx, + struct mail_user *user) +{ + struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx; + struct mail_namespace *ns; + struct mailbox *box; + const char *const *namep; + + array_foreach(&ctx->mailboxes, namep) { + const char *storage_name = *namep; + + ns = mail_namespace_find(user->namespaces, &storage_name); + if (ns == NULL) + i_fatal("Can't find namespace for: %s", *namep); + + box = mailbox_alloc(ns->list, storage_name, 0); + if (mailbox_list_set_subscribed(ns->list, storage_name, + ctx->ctx.subscriptions) < 0) { + i_error("Can't %s mailbox %s: %s", *namep, + ctx->ctx.subscriptions ? "subscribe to" : + "unsubscribe", + mailbox_list_get_last_error(ns->list, NULL)); + } + mailbox_free(&box); + } +} + +static void cmd_mailbox_subscribe_init(struct doveadm_mail_cmd_context *_ctx, + const char *const args[]) +{ + struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx; + const char *name; + unsigned int i; + + if (args[0] == NULL) { + doveadm_mail_help_name(ctx->ctx.subscriptions ? + "mailbox subscribe" : + "mailbox unsubscribe"); + } + doveadm_mailbox_translate_args(&ctx->ctx, &args); + + for (i = 0; args[i] != NULL; i++) { + name = p_strdup(ctx->ctx.ctx.pool, args[i]); + array_append(&ctx->mailboxes, &name, 1); + } +} + +static struct doveadm_mail_cmd_context * +cmd_mailbox_subscriptions_alloc(bool subscriptions) +{ + struct mailbox_cmd_context *ctx; + + ctx = doveadm_mail_cmd_alloc(struct mailbox_cmd_context); + ctx->ctx.subscriptions = subscriptions; + + ctx->ctx.ctx.getopt_args = "78"; + ctx->ctx.ctx.parse_arg = cmd_mailbox_parse_arg; + ctx->ctx.ctx.init = cmd_mailbox_subscribe_init; + ctx->ctx.ctx.run = cmd_mailbox_subscribe_run; + p_array_init(&ctx->mailboxes, ctx->ctx.ctx.pool, 16); + return &ctx->ctx.ctx; +} + +static struct doveadm_mail_cmd_context *cmd_mailbox_subscribe_alloc(void) +{ + return cmd_mailbox_subscriptions_alloc(TRUE); +} + +static struct doveadm_mail_cmd_context *cmd_mailbox_unsubscribe_alloc(void) +{ + return cmd_mailbox_subscriptions_alloc(FALSE); +} + static void cmd_mailbox_convert(int argc, char *argv[]) { string_t *str; @@ -425,6 +499,14 @@ struct doveadm_mail_cmd cmd_mailbox_rename = { cmd_mailbox_rename_alloc, "mailbox rename", "[-7|-8] [-s] " }; +struct doveadm_mail_cmd cmd_mailbox_subscribe = { + cmd_mailbox_subscribe_alloc, "mailbox subscribe", + "[-7|-8] [...]" +}; +struct doveadm_mail_cmd cmd_mailbox_unsubscribe = { + cmd_mailbox_unsubscribe_alloc, "mailbox unsubscribe", + "[-7|-8] [...]" +}; struct doveadm_cmd doveadm_cmd_mailbox_convert = { cmd_mailbox_convert, "mailbox convert", "[-7|-8] [...]", NULL diff --git a/src/doveadm/doveadm-mail.c b/src/doveadm/doveadm-mail.c index c041182337..e53622e489 100644 --- a/src/doveadm/doveadm-mail.c +++ b/src/doveadm/doveadm-mail.c @@ -450,7 +450,9 @@ static struct doveadm_mail_cmd *mail_commands[] = { &cmd_mailbox_list, &cmd_mailbox_create, &cmd_mailbox_delete, - &cmd_mailbox_rename + &cmd_mailbox_rename, + &cmd_mailbox_subscribe, + &cmd_mailbox_unsubscribe }; void doveadm_mail_init(void) diff --git a/src/doveadm/doveadm-mail.h b/src/doveadm/doveadm-mail.h index 29893d555e..234ddcab8b 100644 --- a/src/doveadm/doveadm-mail.h +++ b/src/doveadm/doveadm-mail.h @@ -56,5 +56,7 @@ struct doveadm_mail_cmd cmd_mailbox_list; struct doveadm_mail_cmd cmd_mailbox_create; struct doveadm_mail_cmd cmd_mailbox_delete; struct doveadm_mail_cmd cmd_mailbox_rename; +struct doveadm_mail_cmd cmd_mailbox_subscribe; +struct doveadm_mail_cmd cmd_mailbox_unsubscribe; #endif