From: Marco Bettini Date: Tue, 19 Apr 2022 16:21:51 +0000 (+0000) Subject: doveadm: Remove ::parse_arg() and ::args from most doveadm-mail-*.c files X-Git-Tag: 2.4.0~4007 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a17562ecf7f2eaf7d5a1e07fe91710bba3fcd13c;p=thirdparty%2Fdovecot%2Fcore.git doveadm: Remove ::parse_arg() and ::args from most doveadm-mail-*.c files --- diff --git a/src/doveadm/doveadm-mail-altmove.c b/src/doveadm/doveadm-mail-altmove.c index 73b2432b12..b5c6d038dc 100644 --- a/src/doveadm/doveadm-mail-altmove.c +++ b/src/doveadm/doveadm-mail-altmove.c @@ -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; diff --git a/src/doveadm/doveadm-mail-copymove.c b/src/doveadm/doveadm-mail-copymove.c index 055a72be48..930289c6a6 100644 --- a/src/doveadm/doveadm-mail-copymove.c +++ b/src/doveadm/doveadm-mail-copymove.c @@ -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 }; diff --git a/src/doveadm/doveadm-mail-deduplicate.c b/src/doveadm/doveadm-mail-deduplicate.c index ea9097697d..83aa88cde5 100644 --- a/src/doveadm/doveadm-mail-deduplicate.c +++ b/src/doveadm/doveadm-mail-deduplicate.c @@ -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; diff --git a/src/doveadm/doveadm-mail-expunge.c b/src/doveadm/doveadm-mail-expunge.c index 61681433d8..5fd0052acb 100644 --- a/src/doveadm/doveadm-mail-expunge.c +++ b/src/doveadm/doveadm-mail-expunge.c @@ -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; diff --git a/src/doveadm/doveadm-mail-fetch.c b/src/doveadm/doveadm-mail-fetch.c index bf1e31aa79..f0e7bfb26d 100644 --- a/src/doveadm/doveadm-mail-fetch.c +++ b/src/doveadm/doveadm-mail-fetch.c @@ -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) diff --git a/src/doveadm/doveadm-mail-flags.c b/src/doveadm/doveadm-mail-flags.c index f100ac18d2..498a2b3aac 100644 --- a/src/doveadm/doveadm-mail-flags.c +++ b/src/doveadm/doveadm-mail-flags.c @@ -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 * diff --git a/src/doveadm/doveadm-mail-import.c b/src/doveadm/doveadm-mail-import.c index 310269d36f..86d5213281 100644 --- a/src/doveadm/doveadm-mail-import.c +++ b/src/doveadm/doveadm-mail-import.c @@ -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; diff --git a/src/doveadm/doveadm-mail-index.c b/src/doveadm/doveadm-mail-index.c index 69fcb5c1d0..602e61b6f0 100644 --- a/src/doveadm/doveadm-mail-index.c +++ b/src/doveadm/doveadm-mail-index.c @@ -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 }; diff --git a/src/doveadm/doveadm-mail-mailbox-cache.c b/src/doveadm/doveadm-mail-mailbox-cache.c index 3d5749101f..1a6fa4d5be 100644 --- a/src/doveadm/doveadm-mail-mailbox-cache.c +++ b/src/doveadm/doveadm-mail-mailbox-cache.c @@ -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 diff --git a/src/doveadm/doveadm-mail-mailbox-metadata.c b/src/doveadm/doveadm-mail-mailbox-metadata.c index e5271d1505..da657a2c3a 100644 --- a/src/doveadm/doveadm-mail-mailbox-metadata.c +++ b/src/doveadm/doveadm-mail-mailbox-metadata.c @@ -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; diff --git a/src/doveadm/doveadm-mail-mailbox-status.c b/src/doveadm/doveadm-mail-mailbox-status.c index 9f470952fc..324d4c9df0 100644 --- a/src/doveadm/doveadm-mail-mailbox-status.c +++ b/src/doveadm/doveadm-mail-mailbox-status.c @@ -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; diff --git a/src/doveadm/doveadm-mail-mailbox.c b/src/doveadm/doveadm-mail-mailbox.c index 2b2c453c19..060e99981d 100644 --- a/src/doveadm/doveadm-mail-mailbox.c +++ b/src/doveadm/doveadm-mail-mailbox.c @@ -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 ] ", 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 diff --git a/src/doveadm/doveadm-mail-rebuild.c b/src/doveadm/doveadm-mail-rebuild.c index 757d02ad7b..9b858ee86f 100644 --- a/src/doveadm/doveadm-mail-rebuild.c +++ b/src/doveadm/doveadm-mail-rebuild.c @@ -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); } diff --git a/src/doveadm/doveadm-mail-save.c b/src/doveadm/doveadm-mail-save.c index d8312c8227..4bcadbf530 100644 --- a/src/doveadm/doveadm-mail-save.c +++ b/src/doveadm/doveadm-mail-save.c @@ -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"; diff --git a/src/doveadm/doveadm-mail-search.c b/src/doveadm/doveadm-mail-search.c index c417ec5f86..7489129438 100644 --- a/src/doveadm/doveadm-mail-search.c +++ b/src/doveadm/doveadm-mail-search.c @@ -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) diff --git a/src/doveadm/doveadm-mail.h b/src/doveadm/doveadm-mail.h index c143aec5ac..fe4c4d7231 100644 --- a/src/doveadm/doveadm-mail.h +++ b/src/doveadm/doveadm-mail.h @@ -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[]);