]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Added subscribe/unsubscribe commands.
authorTimo Sirainen <tss@iki.fi>
Thu, 13 May 2010 10:05:24 +0000 (12:05 +0200)
committerTimo Sirainen <tss@iki.fi>
Thu, 13 May 2010 10:05:24 +0000 (12:05 +0200)
--HG--
branch : HEAD

src/doveadm/doveadm-mail-mailbox.c
src/doveadm/doveadm-mail.c
src/doveadm/doveadm-mail.h

index 23c10b86b3275101215d24789ee1928b57d2689a..378213c0c84d704c6b1b319fe9268ca7111d392c 100644 (file)
@@ -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] <old name> <new name>"
 };
+struct doveadm_mail_cmd cmd_mailbox_subscribe = {
+       cmd_mailbox_subscribe_alloc, "mailbox subscribe",
+       "[-7|-8] <mailbox> [...]"
+};
+struct doveadm_mail_cmd cmd_mailbox_unsubscribe = {
+       cmd_mailbox_unsubscribe_alloc, "mailbox unsubscribe",
+       "[-7|-8] <mailbox> [...]"
+};
 struct doveadm_cmd doveadm_cmd_mailbox_convert = {
        cmd_mailbox_convert, "mailbox convert",
        "[-7|-8] <name> [...]", NULL
index c04118233740f2d8150bccdbf5bf9a23647daa83..e53622e4891cdfc8e58a2305e32877efcae75027 100644 (file)
@@ -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)
index 29893d555e6760ba1e3d691e9c1cd6c3c888e861..234ddcab8bb018f02f6d4c2ff29f2015fe788a8b 100644 (file)
@@ -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