]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Remove ::parse_arg() and ::args from most doveadm-mail-*.c files
authorMarco Bettini <marco.bettini@open-xchange.com>
Tue, 19 Apr 2022 16:21:51 +0000 (16:21 +0000)
committerMarco Bettini <marco.bettini@open-xchange.com>
Wed, 1 Jun 2022 10:02:11 +0000 (10:02 +0000)
16 files changed:
src/doveadm/doveadm-mail-altmove.c
src/doveadm/doveadm-mail-copymove.c
src/doveadm/doveadm-mail-deduplicate.c
src/doveadm/doveadm-mail-expunge.c
src/doveadm/doveadm-mail-fetch.c
src/doveadm/doveadm-mail-flags.c
src/doveadm/doveadm-mail-import.c
src/doveadm/doveadm-mail-index.c
src/doveadm/doveadm-mail-mailbox-cache.c
src/doveadm/doveadm-mail-mailbox-metadata.c
src/doveadm/doveadm-mail-mailbox-status.c
src/doveadm/doveadm-mail-mailbox.c
src/doveadm/doveadm-mail-rebuild.c
src/doveadm/doveadm-mail-save.c
src/doveadm/doveadm-mail-search.c
src/doveadm/doveadm-mail.h

index 73b2432b12faa840206a1d148427dccd29236729..b5c6d038dc3e52a1dbd3daf6fbf62fdea6daf0c5 100644 (file)
@@ -11,7 +11,7 @@
 
 struct altmove_cmd_context {
        struct doveadm_mail_cmd_context ctx;
-       bool reverse;
+       bool reverse:1;
 };
 
 static int
@@ -116,27 +116,18 @@ cmd_altmove_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
        return ret;
 }
 
-static void cmd_altmove_init(struct doveadm_mail_cmd_context *ctx,
-                            const char *const args[])
-{
-       if (args[0] == NULL)
-               doveadm_mail_help_name("altmove");
-       ctx->search_args = doveadm_mail_build_search_args(args);
-}
-
-static bool
-cmd_mailbox_altmove_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
+static void cmd_altmove_init(struct doveadm_mail_cmd_context *_ctx,
+                            const char *const _args[] ATTR_UNUSED)
 {
        struct altmove_cmd_context *ctx = (struct altmove_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       switch (c) {
-       case 'r':
-               ctx->reverse = TRUE;
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
+        ctx->reverse = doveadm_cmd_param_flag(cctx, "reverse");
+
+       const char *const *query;
+       if (!doveadm_cmd_param_array(cctx, "query", &query))
+               doveadm_mail_help_name("altmove");
+       _ctx->search_args = doveadm_mail_build_search_args(query);
 }
 
 static struct doveadm_mail_cmd_context *cmd_altmove_alloc(void)
@@ -144,8 +135,6 @@ static struct doveadm_mail_cmd_context *cmd_altmove_alloc(void)
        struct altmove_cmd_context *ctx;
 
        ctx = doveadm_mail_cmd_alloc(struct altmove_cmd_context);
-       ctx->ctx.getopt_args = "r";
-       ctx->ctx.v.parse_arg = cmd_mailbox_altmove_parse_arg;
        ctx->ctx.v.init = cmd_altmove_init;
        ctx->ctx.v.run = cmd_altmove_run;
        return &ctx->ctx;
index 055a72be481bfb4fb551b1fdca028b6fda46b06c..930289c6a6352e7e077b4d9f6e048c653e32468d 100644 (file)
@@ -143,27 +143,23 @@ cmd_copy_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
 }
 
 static void cmd_copy_init(struct doveadm_mail_cmd_context *_ctx,
-                         const char *const args[])
+                         const char *const _args[] ATTR_UNUSED)
 {
        struct copy_cmd_context *ctx = (struct copy_cmd_context *)_ctx;
-       const char *destname = args[0], *cmdname = ctx->move ? "move" : "copy";
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (destname == NULL || args[1] == NULL)
-               doveadm_mail_help_name(cmdname);
-       args++;
+       const char *cmdname = ctx->move ? "move" : "copy";
+       const char *const *query;
 
-       if (args[0] != NULL && args[1] != NULL &&
-           strcasecmp(args[0], "user") == 0) {
-               if ((_ctx->service_flags &
-                    MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP) == 0)
-                       i_fatal("Use -u parameter to specify destination user");
+       if (!doveadm_cmd_param_str(cctx, "destination-mailbox", &ctx->destname) ||
+           !doveadm_cmd_param_array(cctx, "query", &query))
+               doveadm_mail_help_name(cmdname);
+       _ctx->search_args = doveadm_mail_build_search_args(query);
 
-               ctx->source_username = p_strdup(_ctx->pool, args[1]);
-               args += 2;
-       }
+       if (doveadm_cmd_param_str(cctx, "source-user", &ctx->source_username) &&
+           (_ctx->service_flags & MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP) == 0)
+               i_fatal("Use -u parameter to specify destination user");
 
-       ctx->destname = p_strdup(ctx->ctx.pool, destname);
-       _ctx->search_args = doveadm_mail_build_search_args(args);
        if (ctx->move)
                expunge_search_args_check(ctx->ctx.search_args, cmdname);
 }
@@ -206,8 +202,7 @@ struct doveadm_cmd_ver2 doveadm_cmd_copy_ver2 = {
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_MAIL_COMMON
 DOVEADM_CMD_PARAM('\0', "destination-mailbox", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
-DOVEADM_CMD_PARAM('\0', "source-type", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
-DOVEADM_CMD_PARAM('\0', "source-user", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
+DOVEADM_CMD_PARAMKV('\0', "source-user", "user", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL|CMD_PARAM_FLAG_KEY_VALUE)
 DOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAMS_END
 };
@@ -219,8 +214,7 @@ struct doveadm_cmd_ver2 doveadm_cmd_move_ver2 = {
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_MAIL_COMMON
 DOVEADM_CMD_PARAM('\0', "destination-mailbox", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
-DOVEADM_CMD_PARAM('\0', "source-type", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
-DOVEADM_CMD_PARAM('\0', "source-user", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
+DOVEADM_CMD_PARAMKV('\0', "source-user", "user", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL|CMD_PARAM_FLAG_KEY_VALUE)
 DOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAMS_END
 };
index ea9097697d57daebe5bd649b157f675d60f63f96..83aa88cde572ad55327c8829971a5d50b2986a9d 100644 (file)
@@ -96,29 +96,18 @@ cmd_deduplicate_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user
        return ret;
 }
 
-static void cmd_deduplicate_init(struct doveadm_mail_cmd_context *ctx,
-                                const char *const args[])
+static void cmd_deduplicate_init(struct doveadm_mail_cmd_context *_ctx,
+                                const char *const _args[] ATTR_UNUSED)
 {
-       if (args[0] == NULL)
-               doveadm_mail_help_name("deduplicate");
-
-       ctx->search_args = doveadm_mail_build_search_args(args);
-}
+       struct deduplicate_cmd_context *ctx = (struct deduplicate_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-static bool
-cmd_deduplicate_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
-{
-       struct deduplicate_cmd_context *ctx =
-               (struct deduplicate_cmd_context *)_ctx;
+       const char *const *query;
+       ctx->by_msgid = doveadm_cmd_param_flag(cctx, "by-msgid");
+       if (!doveadm_cmd_param_array(cctx, "query", &query))
+               doveadm_mail_help_name("deduplicate");
 
-       switch (c) {
-       case 'm':
-               ctx->by_msgid = TRUE;
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
+       _ctx->search_args = doveadm_mail_build_search_args(query);
 }
 
 static struct doveadm_mail_cmd_context *cmd_deduplicate_alloc(void)
@@ -126,8 +115,6 @@ static struct doveadm_mail_cmd_context *cmd_deduplicate_alloc(void)
        struct deduplicate_cmd_context *ctx;
 
        ctx = doveadm_mail_cmd_alloc(struct deduplicate_cmd_context);
-       ctx->ctx.getopt_args = "m";
-       ctx->ctx.v.parse_arg = cmd_deduplicate_parse_arg;
        ctx->ctx.v.init = cmd_deduplicate_init;
        ctx->ctx.v.run = cmd_deduplicate_run;
        return &ctx->ctx;
index 61681433d88706c9ebafb03faafe054a91678626..5fd0052acb21a5fe5409e87ce629cce320ca7c43 100644 (file)
@@ -239,28 +239,21 @@ void expunge_search_args_check(struct mail_search_args *args, const char *cmd)
        mail_search_args_simplify(args);
 }
 
-static void cmd_expunge_init(struct doveadm_mail_cmd_context *ctx,
-                            const char *const args[])
+static void cmd_expunge_init(struct doveadm_mail_cmd_context *_ctx,
+                            const char *const _args[] ATTR_UNUSED)
 {
-       if (args[0] == NULL)
-               doveadm_mail_help_name("expunge");
+       struct expunge_cmd_context *ctx = (struct expunge_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       ctx->search_args = doveadm_mail_build_search_args(args);
-       expunge_search_args_check(ctx->search_args, "expunge");
-}
+       ctx->delete_empty_mailbox =
+               doveadm_cmd_param_flag(cctx, "delete-empty-mailbox");
 
-static bool cmd_expunge_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
-{
-       struct expunge_cmd_context *ctx = (struct expunge_cmd_context *)_ctx;
+       const char *const *query;
+       if (!doveadm_cmd_param_array(cctx, "query", &query))
+               doveadm_mail_help_name("expunge");
 
-       switch (c) {
-       case 'd':
-               ctx->delete_empty_mailbox = TRUE;
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
+       _ctx->search_args = doveadm_mail_build_search_args(query);
+       expunge_search_args_check(_ctx->search_args, "expunge");
 }
 
 static struct doveadm_mail_cmd_context *cmd_expunge_alloc(void)
@@ -268,8 +261,6 @@ static struct doveadm_mail_cmd_context *cmd_expunge_alloc(void)
        struct expunge_cmd_context *ctx;
 
        ctx = doveadm_mail_cmd_alloc(struct expunge_cmd_context);
-       ctx->ctx.getopt_args = "d";
-       ctx->ctx.v.parse_arg = cmd_expunge_parse_arg;
        ctx->ctx.v.init = cmd_expunge_init;
        ctx->ctx.v.run = cmd_expunge_run;
        return &ctx->ctx;
index bf1e31aa79c53c250d5e25f5928111f90497c898..f0e7bfb26dc6245bb34f4ba6bf1441ae1f8d4a83 100644 (file)
@@ -531,9 +531,9 @@ static void print_fetch_fields(void)
        fprintf(stderr, "\n");
 }
 
-static void parse_fetch_fields(struct fetch_cmd_context *ctx, const char *str)
+static void parse_fetch_fields(struct fetch_cmd_context *ctx, const char *const *fields)
 {
-       const char *const *fields, *name, *section;
+       const char *name, *section;
        const struct fetch_field *field;
        struct fetch_field hdr_field, body_field;
        struct imap_msgpart *msgpart;
@@ -546,7 +546,6 @@ static void parse_fetch_fields(struct fetch_cmd_context *ctx, const char *str)
 
        t_array_init(&ctx->fields, 32);
        t_array_init(&ctx->header_fields, 32);
-       fields = t_strsplit_spaces(str, " ");
        for (; *fields != NULL; fields++) {
                name = t_str_lcase(*fields);
 
@@ -648,16 +647,25 @@ cmd_fetch_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
 }
 
 static void cmd_fetch_init(struct doveadm_mail_cmd_context *_ctx,
-                          const char *const args[])
+                          const char *const _args[] ATTR_UNUSED)
 {
        struct fetch_cmd_context *ctx = (struct fetch_cmd_context *)_ctx;
-       const char *fetch_fields = args[0];
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
+
+       const char *const *fields;
+       if (!doveadm_cmd_param_array(cctx, "field", &fields)) {
+               const char *fieldstr;
+               if (!doveadm_cmd_param_str(cctx, "fieldstr", &fieldstr))
+                       doveadm_mail_help_name("fetch");
+               fields = t_strsplit_spaces(fieldstr, " ");
+       }
 
-       if (fetch_fields == NULL || args[1] == NULL)
+       const char *const *query;
+       if (!doveadm_cmd_param_array(cctx, "query", &query))
                doveadm_mail_help_name("fetch");
 
-       parse_fetch_fields(ctx, fetch_fields);
-       _ctx->search_args = doveadm_mail_build_search_args(args + 1);
+       parse_fetch_fields(ctx, fields);
+       _ctx->search_args = doveadm_mail_build_search_args(query);
 }
 
 static struct doveadm_mail_cmd_context *cmd_fetch_alloc(void)
index f100ac18d23c6bbb1f6c96b0bd246c591b21240c..498a2b3aacbb625f48e9f7d5ec5fed57affe18f2 100644 (file)
@@ -74,14 +74,23 @@ cmd_flags_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
 }
 
 static void cmd_flags_init(struct doveadm_mail_cmd_context *_ctx,
-                          const char *const args[])
+                          const char *const _args[] ATTR_UNUSED)
 {
        struct flags_cmd_context *ctx = (struct flags_cmd_context *)_ctx;
-       const char *const *tmp;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
+
        enum mail_flags flag;
        ARRAY_TYPE(const_string) keywords;
 
-       if (args[0] == NULL || args[1] == NULL) {
+       const char *const *flags = NULL;
+       if (!doveadm_cmd_param_array(cctx, "flag", &flags)) {
+               const char *flagstr;
+               if (doveadm_cmd_param_str(cctx, "flagstr", &flagstr))
+                       flags = t_strsplit_spaces(flagstr, " ");
+       }
+
+       const char *const *query;
+       if (flags == NULL || !doveadm_cmd_param_array(cctx, "query", &query)) {
                switch (ctx->modify_type) {
                case MODIFY_ADD:
                        doveadm_mail_help_name("flags add");
@@ -94,8 +103,8 @@ static void cmd_flags_init(struct doveadm_mail_cmd_context *_ctx,
        }
 
        p_array_init(&keywords, _ctx->pool, 8);
-       for (tmp = t_strsplit(args[0], " "); *tmp != NULL; tmp++) {
-               const char *str = *tmp;
+       for (; *flags != NULL; flags++) {
+               const char *str = *flags;
 
                if (str[0] == '\\') {
                        flag = imap_parse_system_flag(str);
@@ -111,8 +120,7 @@ static void cmd_flags_init(struct doveadm_mail_cmd_context *_ctx,
                array_append_zero(&keywords);
                ctx->keywords = array_front(&keywords);
        }
-
-       _ctx->search_args = doveadm_mail_build_search_args(args+1);
+       _ctx->search_args = doveadm_mail_build_search_args(query);
 }
 
 static struct doveadm_mail_cmd_context *
index 310269d36febc4c5c7bf00889113644f70e9c871..86d5213281aef5cc453c0c2f768b02cd8205fd1b 100644 (file)
@@ -213,15 +213,21 @@ cmd_import_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
 }
 
 static void cmd_import_init(struct doveadm_mail_cmd_context *_ctx,
-                           const char *const args[])
+                           const char *const _args[] ATTR_UNUSED)
 {
        struct import_cmd_context *ctx = (struct import_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (str_array_length(args) < 3)
+       (void)doveadm_cmd_param_str(cctx, "source-user", &ctx->src_username);
+       ctx->subscribe = doveadm_cmd_param_flag(cctx, "subscribe");
+
+       const char *const *query;
+       if (!doveadm_cmd_param_str(cctx, "source-location", &ctx->src_location) ||
+           !doveadm_cmd_param_str(cctx, "dest-parent-mailbox", &ctx->dest_parent) ||
+           !doveadm_cmd_param_array(cctx, "query", &query))
                doveadm_mail_help_name("import");
-       ctx->src_location = p_strdup(_ctx->pool, args[0]);
-       ctx->dest_parent = p_strdup(_ctx->pool, args[1]);
-       ctx->ctx.search_args = doveadm_mail_build_search_args(args+2);
+
+       _ctx->search_args = doveadm_mail_build_search_args(query);
 }
 
 static void cmd_import_deinit(struct doveadm_mail_cmd_context *_ctx)
@@ -232,30 +238,11 @@ static void cmd_import_deinit(struct doveadm_mail_cmd_context *_ctx)
                mail_user_deinit(&ctx->src_user);
 }
 
-static bool cmd_import_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
-{
-       struct import_cmd_context *ctx = (struct import_cmd_context *)_ctx;
-
-       switch (c) {
-       case 'U':
-               ctx->src_username = p_strdup(_ctx->pool, optarg);
-               break;
-       case 's':
-               ctx->subscribe = TRUE;
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
-}
-
 static struct doveadm_mail_cmd_context *cmd_import_alloc(void)
 {
        struct import_cmd_context *ctx;
 
        ctx = doveadm_mail_cmd_alloc(struct import_cmd_context);
-       ctx->ctx.getopt_args = "s";
-       ctx->ctx.v.parse_arg = cmd_import_parse_arg;
        ctx->ctx.v.init = cmd_import_init;
        ctx->ctx.v.deinit = cmd_import_deinit;
        ctx->ctx.v.run = cmd_import_run;
index 69fcb5c1d0020132d2f0d16f6c0e8688b71a9399..602e61b6f0d82d9bb820ff47c46a6a0fd8b5828b 100644 (file)
@@ -20,6 +20,7 @@
 struct index_cmd_context {
        struct doveadm_mail_cmd_context ctx;
 
+       const char *const *mailboxes;
        struct istream *queue_input;
        struct ostream *queue_output;
        unsigned int max_recent_msgs;
@@ -208,18 +209,18 @@ cmd_index_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
        const enum mail_namespace_type ns_mask = MAIL_NAMESPACE_TYPE_MASK_ALL;
        struct mailbox_list_iterate_context *iter;
        const struct mailbox_info *info;
-       unsigned int i;
        int ret = 0;
 
        if (ctx->queue && !ctx->have_wildcards) {
                /* we can do this quickly without going through the mailboxes */
-               for (i = 0; _ctx->args[i] != NULL; i++)
-                       cmd_index_queue(ctx, user, _ctx->args[i]);
+               const char *const *box = ctx->mailboxes;
+               for (; *box != NULL; box++)
+                       cmd_index_queue(ctx, user, *box);
                return 0;
        }
 
-       iter = mailbox_list_iter_init_namespaces(user->namespaces, _ctx->args,
-                                                ns_mask, iter_flags);
+       iter = mailbox_list_iter_init_namespaces(
+               user->namespaces, ctx->mailboxes, ns_mask, iter_flags);
        while ((info = mailbox_list_iter_next(iter)) != NULL) {
                if ((info->flags & (MAILBOX_NOSELECT |
                                    MAILBOX_NONEXISTENT)) == 0) T_BEGIN {
@@ -241,20 +242,22 @@ cmd_index_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
 }
 
 static void cmd_index_init(struct doveadm_mail_cmd_context *_ctx,
-                          const char *const args[])
+                          const char *const _args[] ATTR_UNUSED)
 {
        struct index_cmd_context *ctx = (struct index_cmd_context *)_ctx;
-       unsigned int i;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (args[0] == NULL)
+       ctx->queue = doveadm_cmd_param_flag(cctx, "queue");
+       (void)doveadm_cmd_param_uint32(cctx, "max-recent", &ctx->max_recent_msgs);
+
+       if (!doveadm_cmd_param_array(cctx, "mailbox-mask", &ctx->mailboxes))
                doveadm_mail_help_name("index");
-       for (i = 0; args[i] != NULL; i++) {
-               if (strchr(args[i], '*') != NULL ||
-                   strchr(args[i], '%') != NULL) {
-                       ctx->have_wildcards = TRUE;
-                       break;
-               }
-       }
+
+       const char *const *box = ctx->mailboxes;
+       for (; !ctx->have_wildcards && *box != NULL; box++)
+               ctx->have_wildcards =
+                       strchr(*box, '*') != NULL ||
+                       strchr(*box, '%') != NULL;
 }
 
 static void cmd_index_deinit(struct doveadm_mail_cmd_context *_ctx)
@@ -265,34 +268,11 @@ static void cmd_index_deinit(struct doveadm_mail_cmd_context *_ctx)
        i_stream_destroy(&ctx->queue_input);
 }
 
-static bool
-cmd_index_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
-{
-       struct index_cmd_context *ctx = (struct index_cmd_context *)_ctx;
-
-       switch (c) {
-       case 'q':
-               ctx->queue = TRUE;
-               break;
-       case 'n':
-               if (str_to_uint(optarg, &ctx->max_recent_msgs) < 0) {
-                       i_fatal_status(EX_USAGE,
-                               "Invalid -n parameter number: %s", optarg);
-               }
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
-}
-
 static struct doveadm_mail_cmd_context *cmd_index_alloc(void)
 {
        struct index_cmd_context *ctx;
 
        ctx = doveadm_mail_cmd_alloc(struct index_cmd_context);
-       ctx->ctx.getopt_args = "qn:";
-       ctx->ctx.v.parse_arg = cmd_index_parse_arg;
        ctx->ctx.v.init = cmd_index_init;
        ctx->ctx.v.deinit = cmd_index_deinit;
        ctx->ctx.v.run = cmd_index_run;
@@ -306,7 +286,7 @@ struct doveadm_cmd_ver2 doveadm_cmd_index_ver2 = {
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_MAIL_COMMON
 DOVEADM_CMD_PARAM('q',"queue",CMD_PARAM_BOOL,0)
-DOVEADM_CMD_PARAM('n',"max-recent",CMD_PARAM_STR,0)
-DOVEADM_CMD_PARAM('\0',"mailbox-mask",CMD_PARAM_STR,CMD_PARAM_FLAG_POSITIONAL)
+DOVEADM_CMD_PARAM('n',"max-recent",CMD_PARAM_INT64,CMD_PARAM_FLAG_UNSIGNED)
+DOVEADM_CMD_PARAM('\0',"mailbox-mask",CMD_PARAM_ARRAY,CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAMS_END
 };
index 3d5749101f4a11b806fb7be62f1491ba4abbbecd..1a6fa4d5be9a9739019ccfae444fc7674e1ad7ef 100644 (file)
@@ -46,69 +46,55 @@ static int cmd_mailbox_cache_open_box(struct doveadm_mail_cmd_context *ctx,
        return 0;
 }
 
-static void cmd_mailbox_cache_decision_init(struct doveadm_mail_cmd_context *_ctx,
-                                           const char *const args[])
+static bool
+cmd_mailbox_cache_str_to_make_decision(const char *str,
+                                      enum mail_cache_decision_type *decision_r)
 {
-       struct mailbox_cache_cmd_context *ctx =
-               container_of(_ctx, struct mailbox_cache_cmd_context, ctx);
-       const char *fields;
-
-       doveadm_print_header("mailbox", "mailbox", DOVEADM_PRINT_HEADER_FLAG_STICKY);
-       doveadm_print_header_simple("field");
-       doveadm_print_header_simple("decision");
-       doveadm_print_header_simple("last-used");
-
-       if (!ctx->all_fields &&
-           !doveadm_cmd_param_str(_ctx->cctx, "fieldstr", &fields)) {
-               i_fatal("Missing fields parameter");
-       } else if (!ctx->all_fields) {
-               ctx->fields = t_strsplit_spaces(fields, ", ");
-       }
-
-       ctx->boxes = args;
+       if (strcmp(str, "no") == 0)
+               *decision_r = MAIL_CACHE_DECISION_NO;
+       else if (strcmp(str, "temp") == 0)
+               *decision_r = MAIL_CACHE_DECISION_TEMP;
+       else if (strcmp(str, "yes") == 0)
+               *decision_r = MAIL_CACHE_DECISION_YES;
+       else
+               return FALSE;
+       return TRUE;
 }
 
-static bool
-cmd_mailbox_cache_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
+static void cmd_mailbox_cache_decision_init(struct doveadm_mail_cmd_context *_ctx,
+                                           const char *const _args[] ATTR_UNUSED)
 {
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
        struct mailbox_cache_cmd_context *ctx =
                container_of(_ctx, struct mailbox_cache_cmd_context, ctx);
 
-       switch(c) {
-       case 'a':
-               ctx->all_fields = TRUE;
-               return TRUE;
-       /* this is handled in doveadm-mail as 'fieldstr' field */
-       case 'f':
-               return TRUE;
-       case 'l':
-               if (str_to_uint64(optarg, &ctx->last_used) < 0) {
-                       i_error("Invalid last-used '%s': not a number", optarg);
-                       return FALSE;
-               }
+       if (doveadm_cmd_param_uint64(cctx, "last-used", &ctx->last_used))
                ctx->set_last_used = TRUE;
-               return TRUE;
-       case 'd':
-               if (ctx->set_decision) {
-                       i_error("Only one decision flag allowed");
-                       return FALSE;
-               }
-               if (strcmp(optarg, "no") == 0) {
-                       ctx->decision = MAIL_CACHE_DECISION_NO;
-               } else if (strcmp(optarg, "temp") == 0) {
-                       ctx->decision = MAIL_CACHE_DECISION_TEMP;
-               } else if (strcmp(optarg, "yes") == 0) {
-                       ctx->decision = MAIL_CACHE_DECISION_YES;
-               } else {
-                       i_error("Invalid decision '%s': " \
-                               "must be one of yes, temp, no",
-                               optarg);
-                       return FALSE;
-               }
+
+       const char *value_str;
+       if (doveadm_cmd_param_str(cctx, "decision", &value_str)) {
+               if (!cmd_mailbox_cache_str_to_make_decision(value_str,
+                                                           &ctx->decision))
+                       i_fatal_status(EX_USAGE, "Invalid decision '%s': "
+                                      "must be one of yes, temp, no",
+                                      value_str);
                ctx->set_decision = TRUE;
-               return TRUE;
        }
-       return FALSE;
+
+       ctx->all_fields = doveadm_cmd_param_flag(cctx, "all");
+       if (!ctx->all_fields) {
+               if (!doveadm_cmd_param_str(cctx, "fieldstr", &value_str))
+                       i_fatal_status(EX_USAGE, "Missing fields parameter");
+               ctx->fields = t_strsplit_spaces(value_str, ", ");
+       }
+
+       if (!doveadm_cmd_param_array(cctx, "mailbox", &ctx->boxes))
+               i_fatal_status(EX_USAGE, "Missing mailbox");
+
+       doveadm_print_header("mailbox", "mailbox", DOVEADM_PRINT_HEADER_FLAG_STICKY);
+       doveadm_print_header_simple("field");
+       doveadm_print_header_simple("decision");
+       doveadm_print_header_simple("last-used");
 }
 
 static const char *
@@ -312,19 +298,21 @@ static int cmd_mailbox_cache_remove_run(struct doveadm_mail_cmd_context *_ctx,
 }
 
 static void cmd_mailbox_cache_remove_init(struct doveadm_mail_cmd_context *_ctx,
-                                         const char *const args[])
+                                         const char *const _args[] ATTR_UNUSED)
 {
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
        struct mailbox_cache_cmd_context *ctx =
                container_of(_ctx, struct mailbox_cache_cmd_context, ctx);
 
-       if (args[0] == NULL)
+       const char *const *query;
+       if (!doveadm_cmd_param_array(cctx, "query", &query))
                doveadm_mail_help_name("mailbox cache remove");
 
        doveadm_print_header_simple("mailbox");
        doveadm_print_header_simple("uid");
        doveadm_print_header_simple("result");
 
-       ctx->ctx.search_args = doveadm_mail_build_search_args(args);
+       ctx->ctx.search_args = doveadm_mail_build_search_args(query);
 }
 
 static int cmd_mailbox_cache_purge_run_box(struct mailbox_cache_cmd_context *ctx,
@@ -362,12 +350,14 @@ static int cmd_mailbox_cache_purge_run(struct doveadm_mail_cmd_context *_ctx,
 }
 
 static void cmd_mailbox_cache_purge_init(struct doveadm_mail_cmd_context *_ctx,
-                                        const char *const args[])
+                                        const char *const _args[] ATTR_UNUSED)
 {
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
        struct mailbox_cache_cmd_context *ctx =
                container_of(_ctx, struct mailbox_cache_cmd_context, ctx);
 
-       ctx->boxes = args;
+       if (!doveadm_cmd_param_array(cctx, "mailbox", &ctx->boxes))
+               i_fatal_status(EX_USAGE, "Missing mailbox");
 }
 
 static struct doveadm_mail_cmd_context *cmd_mailbox_cache_decision_alloc(void)
@@ -375,9 +365,7 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_cache_decision_alloc(void)
        struct mailbox_cache_cmd_context *ctx =
                doveadm_mail_cmd_alloc(struct mailbox_cache_cmd_context);
        ctx->ctx.v.init = cmd_mailbox_cache_decision_init;
-       ctx->ctx.v.parse_arg = cmd_mailbox_cache_parse_arg;
        ctx->ctx.v.run = cmd_mailbox_cache_decision_run;
-       ctx->ctx.getopt_args = "al:f:d:";
        doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);
        return &ctx->ctx;
 }
@@ -387,9 +375,7 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_cache_remove_alloc(void)
        struct mailbox_cache_cmd_context *ctx =
                doveadm_mail_cmd_alloc(struct mailbox_cache_cmd_context);
        ctx->ctx.v.init = cmd_mailbox_cache_remove_init;
-       ctx->ctx.v.parse_arg = cmd_mailbox_cache_parse_arg;
        ctx->ctx.v.run = cmd_mailbox_cache_remove_run;
-       ctx->ctx.getopt_args = "";
        doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);
        return &ctx->ctx;
 }
@@ -400,7 +386,6 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_cache_purge_alloc(void)
                doveadm_mail_cmd_alloc(struct mailbox_cache_cmd_context);
        ctx->ctx.v.init = cmd_mailbox_cache_purge_init;
        ctx->ctx.v.run = cmd_mailbox_cache_purge_run;
-       ctx->ctx.getopt_args = "";
        doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);
        return &ctx->ctx;
 }
@@ -415,7 +400,7 @@ DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_MAIL_COMMON
 DOVEADM_CMD_PARAM('a', "all", CMD_PARAM_BOOL, 0)
 DOVEADM_CMD_PARAM('f', "fieldstr", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('l', "last-used", CMD_PARAM_INT64, 0)
+DOVEADM_CMD_PARAM('l', "last-used", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAM('d', "decision", CMD_PARAM_STR, 0)
 DOVEADM_CMD_PARAM('\0', "mailbox", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAMS_END
index e5271d15056ba8675715b1e67db14fb1d31e1c0a..da657a2c3a99bd958da2f9c7d096598aab6e876d 100644 (file)
@@ -141,38 +141,46 @@ cmd_mailbox_metadata_parse_key(const char *arg,
 }
 
 static void
-cmd_mailbox_metadata_set_init(struct doveadm_mail_cmd_context *_ctx,
-                             const char *const args[])
+parse_args_common(struct doveadm_mail_cmd_context *_ctx)
 {
        struct metadata_cmd_context *ctx = (struct metadata_cmd_context *)_ctx;
-       const char *key;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (str_array_length(args) != 3)
-               doveadm_mail_help_name("mailbox metadata set");
-       cmd_mailbox_metadata_parse_key(args[1], &ctx->key_type, &key);
-
-       ctx->mailbox = p_strdup(_ctx->pool, args[0]);
-       ctx->key = p_strdup(_ctx->pool, key);
-       ctx->value.value = p_strdup(_ctx->pool, args[2]);
+       ctx->allow_empty_mailbox_name =
+               doveadm_cmd_param_flag(cctx, "allow-empty-mailbox-name");
+       ctx->prepend_prefix = doveadm_cmd_param_flag(cctx, "prepend-prefix");
 }
 
 static bool
-cmd_mailbox_metadata_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
+parse_args_key(struct doveadm_mail_cmd_context *_ctx,
+                                   const char *field, const char **value_r)
 {
-       struct metadata_cmd_context *ctx =
-               (struct metadata_cmd_context *)_ctx;
-
-       switch (c) {
-       case 's':
-               ctx->allow_empty_mailbox_name = TRUE;
-               break;
-       case 'p':
-               ctx->prepend_prefix = TRUE;
-               break;
-       default:
-               return FALSE;
+       struct metadata_cmd_context *ctx = (struct metadata_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
+
+       const char *value;
+       *value_r = "";
+       bool found = doveadm_cmd_param_str(cctx, field, &value);
+       if (found) {
+               const char *parsed;
+               cmd_mailbox_metadata_parse_key(value, &ctx->key_type, &parsed);
+               *value_r = p_strdup(_ctx->pool, parsed);
        }
-       return TRUE;
+       return found;
+}
+
+static void
+cmd_mailbox_metadata_set_init(struct doveadm_mail_cmd_context *_ctx,
+                             const char *const args[] ATTR_UNUSED)
+{
+       struct metadata_cmd_context *ctx = (struct metadata_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
+
+       parse_args_common(_ctx);
+       if (!doveadm_cmd_param_str(cctx, "mailbox", &ctx->mailbox) ||
+           !parse_args_key(_ctx, "key", &ctx->key) ||
+           !doveadm_cmd_param_str(cctx, "value", &ctx->value.value))
+               doveadm_mail_help_name("mailbox metadata set");
 }
 
 static struct doveadm_mail_cmd_context *cmd_mailbox_metadata_set_alloc(void)
@@ -181,24 +189,21 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_metadata_set_alloc(void)
 
        ctx = doveadm_mail_cmd_alloc(struct metadata_cmd_context);
        ctx->ctx.v.init = cmd_mailbox_metadata_set_init;
-       ctx->ctx.v.parse_arg = cmd_mailbox_metadata_parse_arg;
        ctx->ctx.v.run = cmd_mailbox_metadata_set_run;
        return &ctx->ctx;
 }
 
 static void
 cmd_mailbox_metadata_unset_init(struct doveadm_mail_cmd_context *_ctx,
-                               const char *const args[])
+                               const char *const args[] ATTR_UNUSED)
 {
        struct metadata_cmd_context *ctx = (struct metadata_cmd_context *)_ctx;
-       const char *key;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (str_array_length(args) != 2)
+       parse_args_common(_ctx);
+       if (!doveadm_cmd_param_str(cctx, "mailbox", &ctx->mailbox) ||
+           !parse_args_key(_ctx, "key", &ctx->key))
                doveadm_mail_help_name("mailbox metadata unset");
-       cmd_mailbox_metadata_parse_key(args[1], &ctx->key_type, &key);
-
-       ctx->mailbox = p_strdup(_ctx->pool, args[0]);
-       ctx->key = p_strdup(_ctx->pool, key);
 }
 
 static struct doveadm_mail_cmd_context *cmd_mailbox_metadata_unset_alloc(void)
@@ -207,7 +212,6 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_metadata_unset_alloc(void)
 
        ctx = doveadm_mail_cmd_alloc(struct metadata_cmd_context);
        ctx->ctx.v.init = cmd_mailbox_metadata_unset_init;
-       ctx->ctx.v.parse_arg = cmd_mailbox_metadata_parse_arg;
        ctx->ctx.v.run = cmd_mailbox_metadata_set_run;
        return &ctx->ctx;
 }
@@ -252,17 +256,16 @@ cmd_mailbox_metadata_get_run(struct doveadm_mail_cmd_context *_ctx,
 
 static void
 cmd_mailbox_metadata_get_init(struct doveadm_mail_cmd_context *_ctx,
-                             const char *const args[])
+                             const char *const args[] ATTR_UNUSED)
 {
        struct metadata_cmd_context *ctx = (struct metadata_cmd_context *)_ctx;
-       const char *key;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (str_array_length(args) != 2)
+       parse_args_common(_ctx);
+       if (!doveadm_cmd_param_str(cctx, "mailbox", &ctx->mailbox) ||
+           !parse_args_key(_ctx, "key", &ctx->key))
                doveadm_mail_help_name("mailbox metadata get");
-       cmd_mailbox_metadata_parse_key(args[1], &ctx->key_type, &key);
 
-       ctx->mailbox = p_strdup(_ctx->pool, args[0]);
-       ctx->key = p_strdup(_ctx->pool, key);
        doveadm_print_header("value", "value",
                             DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
 }
@@ -273,7 +276,6 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_metadata_get_alloc(void)
 
        ctx = doveadm_mail_cmd_alloc(struct metadata_cmd_context);
        ctx->ctx.v.init = cmd_mailbox_metadata_get_init;
-       ctx->ctx.v.parse_arg = cmd_mailbox_metadata_parse_arg;
        ctx->ctx.v.run = cmd_mailbox_metadata_get_run;
        doveadm_print_init(DOVEADM_PRINT_TYPE_FLOW);
        return &ctx->ctx;
@@ -345,17 +347,16 @@ cmd_mailbox_metadata_list_run(struct doveadm_mail_cmd_context *_ctx,
 
 static void
 cmd_mailbox_metadata_list_init(struct doveadm_mail_cmd_context *_ctx,
-                              const char *const args[])
+                              const char *const args[] ATTR_UNUSED)
 {
        struct metadata_cmd_context *ctx = (struct metadata_cmd_context *)_ctx;
-       const char *key = NULL;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (args[0] == NULL)
+       parse_args_common(_ctx);
+       if (!doveadm_cmd_param_str(cctx, "mailbox", &ctx->mailbox))
                doveadm_mail_help_name("mailbox metadata list");
-       if (args[1] != NULL)
-               cmd_mailbox_metadata_parse_key(args[1], &ctx->key_type, &key);
-       ctx->mailbox = p_strdup(_ctx->pool, args[0]);
-       ctx->key = key == NULL ? "" : p_strdup(_ctx->pool, key);
+       parse_args_key(_ctx, "key-prefix", &ctx->key);
+
        doveadm_print_header("key", "key",
                             DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
 }
@@ -366,7 +367,6 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_metadata_list_alloc(void)
 
        ctx = doveadm_mail_cmd_alloc(struct metadata_cmd_context);
        ctx->ctx.v.init = cmd_mailbox_metadata_list_init;
-       ctx->ctx.v.parse_arg = cmd_mailbox_metadata_parse_arg;
        ctx->ctx.v.run = cmd_mailbox_metadata_list_run;
        doveadm_print_init(DOVEADM_PRINT_TYPE_FLOW);
        return &ctx->ctx;
index 9f470952fc568a5422bf09ba54fc079211be293b..324d4c9df00699eea79661237365b0eb087980bd 100644 (file)
@@ -187,16 +187,27 @@ cmd_mailbox_status_run(struct doveadm_mail_cmd_context *_ctx,
 }
 
 static void cmd_mailbox_status_init(struct doveadm_mail_cmd_context *_ctx,
-                                   const char *const args[])
+                                   const char *const _args[] ATTR_UNUSED)
 {
        struct status_cmd_context *ctx = (struct status_cmd_context *)_ctx;
-       const char *fields = args[0];
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (fields == NULL || args[1] == NULL)
+       ctx->total_sum = doveadm_cmd_param_flag(cctx, "total-sum");
+
+       const char *const *fields;
+       if (!doveadm_cmd_param_array(cctx, "field", &fields)) {
+               const char *fieldstr;
+               if (!doveadm_cmd_param_str(cctx, "fieldstr", &fieldstr))
+                       doveadm_mail_help_name("mailbox status");
+               fields = t_strsplit_spaces(fieldstr, " ");
+       }
+
+       const char *const *args;
+       if (!doveadm_cmd_param_array(cctx, "mailbox-mask", &args))
                doveadm_mail_help_name("mailbox status");
 
-       status_parse_fields(ctx, t_strsplit_spaces(fields, " "));
-       ctx->search_args = doveadm_mail_mailbox_search_args_build(args+1);
+       status_parse_fields(ctx, fields);
+       ctx->search_args = doveadm_mail_mailbox_search_args_build(args);
 
        if (!ctx->total_sum) {
                doveadm_print_header("mailbox", "mailbox",
@@ -230,30 +241,11 @@ static void cmd_mailbox_status_deinit(struct doveadm_mail_cmd_context *_ctx)
                mail_search_args_unref(&ctx->search_args);
 }
 
-static bool
-cmd_mailbox_status_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
-{
-       struct status_cmd_context *ctx = (struct status_cmd_context *)_ctx;
-
-       switch (c) {
-       case 't':
-               ctx->total_sum = TRUE;
-               break;
-       case 'f':
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
-}
-
 static struct doveadm_mail_cmd_context *cmd_mailbox_status_alloc(void)
 {
        struct status_cmd_context *ctx;
 
        ctx = doveadm_mail_cmd_alloc(struct status_cmd_context);
-       ctx->ctx.getopt_args = "t";
-       ctx->ctx.v.parse_arg = cmd_mailbox_status_parse_arg;
        ctx->ctx.v.init = cmd_mailbox_status_init;
        ctx->ctx.v.deinit = cmd_mailbox_status_deinit;
        ctx->ctx.v.run = cmd_mailbox_status_run;
index 2b2c453c19e7d8ad87abc0e744383df6b6935eaf..060e99981d621b4a94ca9385ee3fb22e14e62589 100644 (file)
@@ -66,66 +66,29 @@ static const char *mailbox_list_path_type_names[] = {
        "control", "index", "index-private"
 };
 
-void doveadm_mailbox_args_check(const char *const args[])
+static void doveadm_mailbox_arg_check(const char *arg)
 {
-       unsigned int i;
-
-       for (i = 0; args[i] != NULL; i++) {
-               if (!uni_utf8_str_is_valid(args[i])) {
-                       i_fatal_status(EX_DATAERR,
-                               "Mailbox name not valid UTF-8: %s", args[i]);
-               }
+       if (!uni_utf8_str_is_valid(arg)) {
+               i_fatal_status(EX_DATAERR,
+                       "Mailbox name not valid UTF-8: %s", arg);
        }
 }
 
-static bool cmd_mailbox_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
+void doveadm_mailbox_args_check_array(ARRAY_TYPE(const_string) *args)
 {
-       struct doveadm_mailbox_cmd_context *ctx =
-               (struct doveadm_mailbox_cmd_context *)_ctx;
-
-       switch (c) {
-       case 's':
-               ctx->subscriptions = TRUE;
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
+       const char *arg;
+       array_foreach_elem(args, arg)
+               doveadm_mailbox_arg_check(arg);
 }
 
-#define doveadm_mailbox_cmd_alloc(type) \
-       (type *)doveadm_mailbox_cmd_alloc_size(sizeof(type))
-static struct doveadm_mail_cmd_context *
-doveadm_mailbox_cmd_alloc_size(size_t size)
+void doveadm_mailbox_args_check(const char *const args[])
 {
-       struct doveadm_mail_cmd_context *ctx;
-
-       ctx = doveadm_mail_cmd_alloc_size(size);
-       ctx->getopt_args = "s";
-       ctx->v.parse_arg = cmd_mailbox_parse_arg;
-       return ctx;
+       for (; *args != NULL; args++)
+               doveadm_mailbox_arg_check(*args);
 }
 
-static bool
-cmd_mailbox_list_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
-{
-       struct list_cmd_context *ctx = (struct list_cmd_context *)_ctx;
-
-       switch (c) {
-       case '7':
-               ctx->mutf7 = TRUE;
-               break;
-       case '8':
-               ctx->mutf7 = FALSE;
-               break;
-       case 's':
-               ctx->ctx.subscriptions = TRUE;
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
-}
+#define doveadm_mailbox_cmd_alloc(type) \
+       ((type *) doveadm_mail_cmd_alloc_size(sizeof(type)))
 
 static int
 cmd_mailbox_list_run(struct doveadm_mail_cmd_context *_ctx,
@@ -188,13 +151,20 @@ doveadm_mail_mailbox_search_args_build(const char *const args[])
 }
 
 static void cmd_mailbox_list_init(struct doveadm_mail_cmd_context *_ctx,
-                                 const char *const args[])
+                                 const char *const _args[] ATTR_UNUSED)
 {
        struct list_cmd_context *ctx = (struct list_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
+       ctx->ctx.subscriptions = doveadm_cmd_param_flag(cctx, "subscriptions");
+       ctx->mutf7 = doveadm_cmd_param_flag(cctx, "mutf7");
+       if (doveadm_cmd_param_flag(cctx, "utf8")) ctx->mutf7 = FALSE;
+
+       const char *const *args = empty_str_array;
+       (void)doveadm_cmd_param_array(cctx, "mailbox-mask", &args);
+       ctx->search_args = doveadm_mail_mailbox_search_args_build(args);
        doveadm_print_header("mailbox", "mailbox",
                             DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
-       ctx->search_args = doveadm_mail_mailbox_search_args_build(args);
 }
 
 static void cmd_mailbox_list_deinit(struct doveadm_mail_cmd_context *_ctx)
@@ -213,8 +183,6 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_list_alloc(void)
        ctx->ctx.ctx.v.init = cmd_mailbox_list_init;
        ctx->ctx.ctx.v.deinit = cmd_mailbox_list_deinit;
        ctx->ctx.ctx.v.run = cmd_mailbox_list_run;
-       ctx->ctx.ctx.v.parse_arg = cmd_mailbox_list_parse_arg;
-       ctx->ctx.ctx.getopt_args = "78s";
        doveadm_print_init(DOVEADM_PRINT_TYPE_FLOW);
        return &ctx->ctx.ctx;
 }
@@ -261,39 +229,22 @@ cmd_mailbox_create_run(struct doveadm_mail_cmd_context *_ctx,
 }
 
 static void cmd_mailbox_create_init(struct doveadm_mail_cmd_context *_ctx,
-                                   const char *const args[])
+                                   const char *const _args[] ATTR_UNUSED)
 {
-       struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
-       const char *name;
-       unsigned int i;
+       struct create_cmd_context *ctx = (struct create_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (args[0] == NULL)
-               doveadm_mail_help_name("mailbox create");
-       doveadm_mailbox_args_check(args);
+       ctx->ctx.subscriptions = doveadm_cmd_param_flag(cctx, "subscriptions");
 
-       for (i = 0; args[i] != NULL; i++) {
-               name = p_strdup(ctx->ctx.ctx.pool, args[i]);
-               array_push_back(&ctx->mailboxes, &name);
-       }
-}
+       const char *guid;
+       if (doveadm_cmd_param_str(cctx, "guid", &guid) &&
+           guid_128_from_string(guid, ctx->update.mailbox_guid) < 0)
+               doveadm_mail_help_name("mailbox create");
 
-static bool
-cmd_mailbox_create_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
-{
-       struct create_cmd_context *ctx = (struct create_cmd_context *)_ctx;
+       if (!doveadm_cmd_param_array_append(cctx, "mailbox", &ctx->mailboxes))
+               doveadm_mail_help_name("mailbox create");
 
-       switch (c) {
-       case 'g':
-               if (guid_128_from_string(optarg, ctx->update.mailbox_guid) < 0)
-                       doveadm_mail_help_name("mailbox create");
-               break;
-       case 's':
-               ctx->ctx.subscriptions = TRUE;
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
+       doveadm_mailbox_args_check_array(&ctx->mailboxes);
 }
 
 static struct doveadm_mail_cmd_context *cmd_mailbox_create_alloc(void)
@@ -303,8 +254,6 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_create_alloc(void)
        ctx = doveadm_mailbox_cmd_alloc(struct create_cmd_context);
        ctx->ctx.ctx.v.init = cmd_mailbox_create_init;
        ctx->ctx.ctx.v.run = cmd_mailbox_create_run;
-       ctx->ctx.ctx.v.parse_arg = cmd_mailbox_create_parse_arg;
-       ctx->ctx.ctx.getopt_args = "g:s";
        p_array_init(&ctx->mailboxes, ctx->ctx.ctx.pool, 16);
        return &ctx->ctx.ctx;
 }
@@ -392,47 +341,23 @@ cmd_mailbox_delete_run(struct doveadm_mail_cmd_context *_ctx,
 }
 
 static void cmd_mailbox_delete_init(struct doveadm_mail_cmd_context *_ctx,
-                                   const char *const args[])
+                                   const char *const _args[] ATTR_UNUSED)
 {
        struct delete_cmd_context *ctx = (struct delete_cmd_context *)_ctx;
-       const char *name;
-       unsigned int i;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (args[0] == NULL)
+       ctx->recursive = doveadm_cmd_param_flag(cctx, "recursive");
+       ctx->require_empty = doveadm_cmd_param_flag(cctx, "require-empty");
+       ctx->ctx.subscriptions = doveadm_cmd_param_flag(cctx, "subscriptions");
+       ctx->unsafe = doveadm_cmd_param_flag(cctx, "unsafe");
+
+       if (!doveadm_cmd_param_array_append(cctx, "mailbox", &ctx->mailboxes))
                doveadm_mail_help_name("mailbox delete");
-       doveadm_mailbox_args_check(args);
 
-       for (i = 0; args[i] != NULL; i++) {
-               name = p_strdup(ctx->ctx.ctx.pool, args[i]);
-               array_push_back(&ctx->mailboxes, &name);
-       }
+       doveadm_mailbox_args_check_array(&ctx->mailboxes);
        array_sort(&ctx->mailboxes, i_strcmp_reverse_p);
 }
 
-static bool
-cmd_mailbox_delete_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
-{
-       struct delete_cmd_context *ctx = (struct delete_cmd_context *)_ctx;
-
-       switch (c) {
-       case 'r':
-               ctx->recursive = TRUE;
-               break;
-       case 's':
-               ctx->ctx.subscriptions = TRUE;
-               break;
-       case 'e':
-               ctx->require_empty = TRUE;
-               break;
-       case 'Z':
-               ctx->unsafe = TRUE;
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
-}
-
 static struct doveadm_mail_cmd_context *cmd_mailbox_delete_alloc(void)
 {
        struct delete_cmd_context *ctx;
@@ -440,8 +365,6 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_delete_alloc(void)
        ctx = doveadm_mailbox_cmd_alloc(struct delete_cmd_context);
        ctx->ctx.ctx.v.init = cmd_mailbox_delete_init;
        ctx->ctx.ctx.v.run = cmd_mailbox_delete_run;
-       ctx->ctx.ctx.v.parse_arg = cmd_mailbox_delete_parse_arg;
-       ctx->ctx.ctx.getopt_args = "ersZ";
        p_array_init(&ctx->mailboxes, ctx->ctx.ctx.pool, 16);
        return &ctx->ctx.ctx;
 }
@@ -488,16 +411,17 @@ cmd_mailbox_rename_run(struct doveadm_mail_cmd_context *_ctx,
 }
 
 static void cmd_mailbox_rename_init(struct doveadm_mail_cmd_context *_ctx,
-                                   const char *const args[])
+                                   const char *const _args[] ATTR_UNUSED)
 {
        struct rename_cmd_context *ctx = (struct rename_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (str_array_length(args) != 2)
+       ctx->ctx.subscriptions = doveadm_cmd_param_flag(cctx, "subscriptions");
+       if (!doveadm_cmd_param_str(cctx, "mailbox", &ctx->oldname) ||
+           !doveadm_cmd_param_str(cctx, "new-name", &ctx->newname))
                doveadm_mail_help_name("mailbox rename");
-       doveadm_mailbox_args_check(args);
-
-       ctx->oldname = p_strdup(ctx->ctx.ctx.pool, args[0]);
-       ctx->newname = p_strdup(ctx->ctx.ctx.pool, args[1]);
+       doveadm_mailbox_arg_check(ctx->oldname);
+       doveadm_mailbox_arg_check(ctx->newname);
 }
 
 static struct doveadm_mail_cmd_context *cmd_mailbox_rename_alloc(void)
@@ -537,23 +461,17 @@ cmd_mailbox_subscribe_run(struct doveadm_mail_cmd_context *_ctx,
 }
 
 static void cmd_mailbox_subscribe_init(struct doveadm_mail_cmd_context *_ctx,
-                                      const char *const args[])
+                                      const char *const _args[] ATTR_UNUSED)
 {
        struct mailbox_cmd_context *ctx = (struct mailbox_cmd_context *)_ctx;
-       const char *name;
-       unsigned int i;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (args[0] == NULL) {
+       if (!doveadm_cmd_param_array_append(cctx, "mailbox", &ctx->mailboxes))
                doveadm_mail_help_name(ctx->ctx.subscriptions ?
                                       "mailbox subscribe" :
                                       "mailbox unsubscribe");
-       }
-       doveadm_mailbox_args_check(args);
 
-       for (i = 0; args[i] != NULL; i++) {
-               name = p_strdup(ctx->ctx.ctx.pool, args[i]);
-               array_push_back(&ctx->mailboxes, &name);
-       }
+       doveadm_mailbox_args_check_array(&ctx->mailboxes);
 }
 
 static struct doveadm_mail_cmd_context *
@@ -564,7 +482,6 @@ cmd_mailbox_subscriptions_alloc(bool subscriptions)
        ctx = doveadm_mail_cmd_alloc(struct mailbox_cmd_context);
        ctx->ctx.subscriptions = subscriptions;
 
-       ctx->ctx.ctx.v.parse_arg = cmd_mailbox_parse_arg;
        ctx->ctx.ctx.v.init = cmd_mailbox_subscribe_init;
        ctx->ctx.ctx.v.run = cmd_mailbox_subscribe_run;
        p_array_init(&ctx->mailboxes, ctx->ctx.ctx.pool, 16);
@@ -583,59 +500,29 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_unsubscribe_alloc(void)
 
 static
 void cmd_mailbox_update_init(struct doveadm_mail_cmd_context *_ctx,
-                            const char *const args[])
+                            const char *const _args[] ATTR_UNUSED)
 {
        struct update_cmd_context *ctx = (struct update_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       if (str_array_length(args) != 1)
+       const char *value_str;
+       if (doveadm_cmd_param_str(cctx, "mailbox-guid", &value_str) &&
+           guid_128_from_string(value_str, ctx->update.mailbox_guid) < 0)
                doveadm_mail_help_name("mailbox update");
 
-       doveadm_mailbox_args_check(args);
+       (void)doveadm_cmd_param_uint32(cctx, "uid-validity", &ctx->update.uid_validity);
+       (void)doveadm_cmd_param_uint32(cctx, "min-next-uid", &ctx->update.min_next_uid);
+       (void)doveadm_cmd_param_uint32(cctx, "min-first-recent-uid", &ctx->update.min_first_recent_uid);
+       (void)doveadm_cmd_param_uint64(cctx, "min-highest-modseq", &ctx->update.min_highest_modseq);
+       (void)doveadm_cmd_param_uint64(cctx, "min-highest-pvt-modseq", &ctx->update.min_highest_pvt_modseq);
 
-       ctx->mailbox = args[0];
-
-       if ((ctx->update.min_first_recent_uid != 0 ||
-            ctx->update.min_next_uid != 0) &&
-           ctx->update.min_first_recent_uid > ctx->update.min_next_uid) {
-               i_fatal_status(EX_DATAERR,
-                       "min_first_recent_uid > min_next_uid");
-       }
-}
+       if (ctx->update.min_first_recent_uid > ctx->update.min_next_uid &&
+           ctx->update.min_first_recent_uid + ctx->update.min_next_uid > 0)
+               i_fatal_status(EX_DATAERR, "min_first_recent_uid > min_next_uid");
 
-static
-bool cmd_mailbox_update_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
-{
-       struct update_cmd_context *ctx = (struct update_cmd_context *)_ctx;
-
-       switch (c) {
-       case 'g':
-               if (guid_128_from_string(optarg, ctx->update.mailbox_guid) < 0)
-                       doveadm_mail_help_name("mailbox update");
-               break;
-       case 'V':
-               if (str_to_uint32(optarg, &(ctx->update.uid_validity)) < 0)
-                       doveadm_mail_help_name("mailbox update");
-               break;
-       case 'N':
-               if (str_to_uint32(optarg, &(ctx->update.min_next_uid)) < 0)
-                       doveadm_mail_help_name("mailbox update");
-               break;
-       case 'R':
-               if (str_to_uint32(optarg, &(ctx->update.min_first_recent_uid)) < 0)
-                       doveadm_mail_help_name("mailbox update");
-               break;
-       case 'H':
-               if (str_to_uint64(optarg, &(ctx->update.min_highest_modseq)) < 0)
-                       doveadm_mail_help_name("mailbox update");
-               break;
-       case 'P':
-               if (str_to_uint64(optarg, &(ctx->update.min_highest_pvt_modseq)) < 0)
-                       doveadm_mail_help_name("mailbox update");
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
+       if (!doveadm_cmd_param_str(cctx, "mailbox", &ctx->mailbox))
+               doveadm_mail_help_name("mailbox update");
+       doveadm_mailbox_arg_check(ctx->mailbox);
 }
 
 static
@@ -668,27 +555,11 @@ struct doveadm_mail_cmd_context *cmd_mailbox_update_alloc(void)
 {
        struct update_cmd_context *ctx;
        ctx = doveadm_mail_cmd_alloc(struct update_cmd_context);
-       ctx->ctx.ctx.v.parse_arg = cmd_mailbox_update_parse_arg;
        ctx->ctx.ctx.v.init = cmd_mailbox_update_init;
        ctx->ctx.ctx.v.run = cmd_mailbox_update_run;
        return &ctx->ctx.ctx;
 }
 
-static void
-cmd_mailbox_path_init(struct doveadm_mail_cmd_context *_ctx,
-                     const char *const args[])
-{
-       struct update_cmd_context *ctx = (struct update_cmd_context *)_ctx;
-
-       if (str_array_length(args) != 1)
-               doveadm_mail_help_name("mailbox path");
-
-       doveadm_mailbox_args_check(args);
-
-       ctx->mailbox = args[0];
-       doveadm_print_header("path", "path", DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
-}
-
 static bool
 mailbox_list_path_type_name_parse(const char *name,
                                  enum mailbox_list_path_type *type_r)
@@ -704,20 +575,26 @@ mailbox_list_path_type_name_parse(const char *name,
        return FALSE;
 }
 
-static bool
-cmd_mailbox_path_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
+static void
+cmd_mailbox_path_init(struct doveadm_mail_cmd_context *_ctx,
+                     const char *const _args[] ATTR_UNUSED)
 {
        struct path_cmd_context *ctx = (struct path_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       switch (c) {
-       case 't':
-               if (!mailbox_list_path_type_name_parse(optarg, &ctx->path_type))
-                       doveadm_mail_help_name("mailbox path");
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
+       const char *value_str;
+       if (doveadm_cmd_param_str(cctx, "type", &value_str) &&
+           !mailbox_list_path_type_name_parse(value_str, &ctx->path_type))
+               doveadm_mail_help_name("mailbox path");
+
+       const char *const *args;
+       if (!doveadm_cmd_param_array(cctx, "mailbox", &args) ||
+           args[1] != NULL)
+               doveadm_mail_help_name("mailbox path");
+       ctx->mailbox = args[0];
+
+       doveadm_mailbox_arg_check(ctx->mailbox);
+       doveadm_print_header("path", "path", DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
 }
 
 static int
@@ -750,7 +627,6 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_path_alloc(void)
 
        ctx = doveadm_mail_cmd_alloc(struct path_cmd_context);
        ctx->path_type = MAILBOX_LIST_PATH_TYPE_INDEX;
-       ctx->ctx.ctx.v.parse_arg = cmd_mailbox_path_parse_arg;
        ctx->ctx.ctx.v.init = cmd_mailbox_path_init;
        ctx->ctx.ctx.v.run = cmd_mailbox_path_run;
        doveadm_print_init(DOVEADM_PRINT_TYPE_FLOW);
@@ -835,11 +711,11 @@ struct doveadm_cmd_ver2 doveadm_cmd_mailbox_update_ver2 = {
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_MAIL_COMMON
 DOVEADM_CMD_PARAM('g', "mailbox-guid", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('V', "uid-validity", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('N', "min-next-uid", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('R', "min-first-recent-uid", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('H', "min-highest-modseq", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('P', "min-highest-pvt-modseq", CMD_PARAM_STR, 0)
+DOVEADM_CMD_PARAM('V', "uid-validity", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
+DOVEADM_CMD_PARAM('N', "min-next-uid", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
+DOVEADM_CMD_PARAM('R', "min-first-recent-uid", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
+DOVEADM_CMD_PARAM('H', "min-highest-modseq", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
+DOVEADM_CMD_PARAM('P', "min-highest-pvt-modseq", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAM('\0', "mailbox", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAMS_END
 };
@@ -850,6 +726,7 @@ struct doveadm_cmd_ver2 doveadm_cmd_mailbox_path_ver2 = {
        .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX"[-t <type>] <mailbox>",
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_MAIL_COMMON
+/* should be CMD_PARAM_STR but it would break the http API */
 DOVEADM_CMD_PARAM('\0', "mailbox", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAM('t', "type", CMD_PARAM_STR, 0)
 DOVEADM_CMD_PARAMS_END
index 757d02ad7b140825c8543605b4daf3a14dd8772c..9b858ee86f2fa792710b492a687c3778c0e0d6e6 100644 (file)
@@ -71,12 +71,18 @@ cmd_rebuild_attachment_run(struct doveadm_mail_cmd_context *ctx,
        return ret;
 }
 
-static void cmd_rebuild_attachment_init(struct doveadm_mail_cmd_context *ctx,
-                                       const char *const args[])
+static void cmd_rebuild_attachment_init(struct doveadm_mail_cmd_context *_ctx,
+                           const char *const _args[] ATTR_UNUSED)
 {
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
+
+       const char *const *query;
+       if (!doveadm_cmd_param_array(cctx, "query", &query))
+               doveadm_mail_help_name("search");
+       _ctx->search_args = doveadm_mail_build_search_args(query);
+
        doveadm_print_header_simple("uid");
        doveadm_print_header_simple("attachment");
-       ctx->search_args = doveadm_mail_build_search_args(args);
 }
 
 
index d8312c8227bdf2fd60bdce8c9332962e6ef1d5b5..4bcadbf530fa751b7766f1c5666d6ea03422f9bb 100644 (file)
@@ -98,23 +98,12 @@ cmd_save_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
 
 static void cmd_save_init(struct doveadm_mail_cmd_context *_ctx,
                          const char *const args[] ATTR_UNUSED)
-{
-       doveadm_mail_get_input(_ctx);
-}
-
-static bool
-cmd_mailbox_save_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
 {
        struct save_cmd_context *ctx = (struct save_cmd_context *)_ctx;
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
 
-       switch (c) {
-       case 'm':
-               ctx->mailbox = optarg;
-               break;
-       default:
-               return FALSE;
-       }
-       return TRUE;
+       (void)doveadm_cmd_param_str(cctx, "mailbox", &ctx->mailbox);
+       doveadm_mail_get_input(_ctx);
 }
 
 static struct doveadm_mail_cmd_context *cmd_save_alloc(void)
@@ -122,8 +111,6 @@ static struct doveadm_mail_cmd_context *cmd_save_alloc(void)
        struct save_cmd_context *ctx;
 
        ctx = doveadm_mail_cmd_alloc(struct save_cmd_context);
-       ctx->ctx.getopt_args = "m:";
-       ctx->ctx.v.parse_arg = cmd_mailbox_save_parse_arg;
        ctx->ctx.v.init = cmd_save_init;
        ctx->ctx.v.run = cmd_save_run;
        ctx->mailbox = "INBOX";
index c417ec5f861537924f287943a4ff05b3be4dfb58..74891294382fdf310c9aaef0b3783a6a2231f4c4 100644 (file)
@@ -69,18 +69,20 @@ cmd_search_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user)
        return ret;
 }
 
-static void cmd_search_init(struct doveadm_mail_cmd_context *ctx,
-                           const char *const args[])
+static void cmd_search_init(struct doveadm_mail_cmd_context *_ctx,
+                           const char *const _args[] ATTR_UNUSED)
 {
-       if (args[0] == NULL)
+       struct doveadm_cmd_context *cctx = _ctx->cctx;
+
+       const char *const *query;
+       if (!doveadm_cmd_param_array(cctx, "query", &query))
                doveadm_mail_help_name("search");
+       _ctx->search_args = doveadm_mail_build_search_args(query);
 
        doveadm_print_header("mailbox-guid", "mailbox-guid",
                             DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
        doveadm_print_header("uid", "uid",
                             DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
-
-       ctx->search_args = doveadm_mail_build_search_args(args);
 }
 
 static struct doveadm_mail_cmd_context *cmd_search_alloc(void)
index c143aec5ac1b43531ef334b3e670c6b8403e5a48..fe4c4d7231e3dfd134147b171e5fd58110957800 100644 (file)
@@ -147,6 +147,7 @@ struct mailbox *
 doveadm_mailbox_find(struct mail_user *user, const char *mailbox);
 struct mail_search_args *
 doveadm_mail_build_search_args(const char *const args[]);
+void doveadm_mailbox_args_check_array(ARRAY_TYPE(const_string) *args);
 void doveadm_mailbox_args_check(const char *const args[]);
 struct mail_search_args *
 doveadm_mail_mailbox_search_args_build(const char *const args[]);