Skip to content

Commit

Permalink
doveadm-mailbox: Add update subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
cmouse authored and sirainen committed May 19, 2016
1 parent b6d4252 commit 9b496d9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/doveadm/doveadm-mail-mailbox.c
Expand Up @@ -47,6 +47,12 @@ struct list_cmd_context {
bool mutf7;
};

struct update_cmd_context {
struct doveadm_mailbox_cmd_context ctx;
const char *mailbox;
struct mailbox_update update;
};

void doveadm_mailbox_args_check(const char *const args[])
{
unsigned int i;
Expand Down Expand Up @@ -556,6 +562,97 @@ static struct doveadm_mail_cmd_context *cmd_mailbox_unsubscribe_alloc(void)
return cmd_mailbox_subscriptions_alloc(FALSE);
}

static
void cmd_mailbox_update_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 update");

doveadm_mailbox_args_check(args);

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");
}
}

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;
}

static
int cmd_mailbox_update_run(struct doveadm_mail_cmd_context *_ctx,
struct mail_user *user)
{
struct update_cmd_context *ctx = (struct update_cmd_context *)_ctx;
struct mail_namespace *ns;
struct mailbox *box;
int ret = 0;

ns = mail_namespace_find(user->namespaces, ctx->mailbox);
box = mailbox_alloc(ns->list, ctx->mailbox, 0);

if ((ret = mailbox_update(box, &(ctx->update))) != 0) {
i_error("Cannot update %s: %s",
ctx->mailbox,
mailbox_get_last_error(box, NULL));
}

mailbox_free(&box);

return ret;
}

static
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;
}

struct doveadm_cmd_ver2 doveadm_cmd_mailbox_list_ver2 = {
.name = "mailbox list",
.mail_cmd = cmd_mailbox_list_alloc,
Expand Down Expand Up @@ -623,3 +720,19 @@ DOVEADM_CMD_MAIL_COMMON
DOVEADM_CMD_PARAM('\0', "mailbox", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
};

struct doveadm_cmd_ver2 doveadm_cmd_mailbox_update_ver2 = {
.name = "mailbox update",
.mail_cmd = cmd_mailbox_update_alloc,
.usage = DOVEADM_CMD_MAIL_USAGE_PREFIX"[--mailbox-guid guid] [--uid-validity uid] [--min-next-uid uid] [--min-first-recent-uid uid] [--min-highest-modseq seq] [--min-highest-pvt-modseq seq] <mailbox>",
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_INT64, 0)
DOVEADM_CMD_PARAM('N', "min-next-uid", CMD_PARAM_INT64, 0)
DOVEADM_CMD_PARAM('R', "min-first-recent-uid", CMD_PARAM_INT64, 0)
DOVEADM_CMD_PARAM('H', "min-highest-modseq", CMD_PARAM_INT64, 0)
DOVEADM_CMD_PARAM('P', "min-highest-pvt-modseq", CMD_PARAM_INT64, 0)
DOVEADM_CMD_PARAM('\0', "mailbox", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
};
1 change: 1 addition & 0 deletions src/doveadm/doveadm-mail.c
Expand Up @@ -870,6 +870,7 @@ static struct doveadm_cmd_ver2 *mail_commands_ver2[] = {
&doveadm_cmd_mailbox_rename_ver2,
&doveadm_cmd_mailbox_subscribe_ver2,
&doveadm_cmd_mailbox_unsubscribe_ver2,
&doveadm_cmd_mailbox_update_ver2,
&doveadm_cmd_fetch_ver2,
&doveadm_cmd_save_ver2,
&doveadm_cmd_index_ver2,
Expand Down
1 change: 1 addition & 0 deletions src/doveadm/doveadm-mail.h
Expand Up @@ -197,6 +197,7 @@ extern struct doveadm_cmd_ver2 doveadm_cmd_import_ver2;
extern struct doveadm_cmd_ver2 doveadm_cmd_search_ver2;
extern struct doveadm_cmd_ver2 doveadm_cmd_copy_ver2;
extern struct doveadm_cmd_ver2 doveadm_cmd_move_ver2;
extern struct doveadm_cmd_ver2 doveadm_cmd_mailbox_update_ver2;

#define DOVEADM_CMD_MAIL_COMMON \
DOVEADM_CMD_PARAM('A', "all-users", CMD_PARAM_BOOL, 0) \
Expand Down

0 comments on commit 9b496d9

Please sign in to comment.