doveadm-mail-mailbox-status.c revision a18da4410dcd47ab8d9b40c09a76a54fa55b9c86
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (c) 2010-2016 Dovecot authors, see the included COPYING file */
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "lib.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "str.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "mail-namespace.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "mail-storage.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "mail-search.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "doveadm-print.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "doveadm-mail.h"
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen#include "doveadm-mailbox-list-iter.h"
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#define ALL_STATUS_ITEMS \
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen (STATUS_MESSAGES | STATUS_RECENT | \
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen STATUS_UIDNEXT | STATUS_UIDVALIDITY | \
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen STATUS_UNSEEN | STATUS_HIGHESTMODSEQ)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#define ALL_METADATA_ITEMS \
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen (MAILBOX_METADATA_VIRTUAL_SIZE | MAILBOX_METADATA_GUID)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#define TOTAL_STATUS_ITEMS \
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen (STATUS_MESSAGES | STATUS_RECENT | STATUS_UNSEEN)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#define TOTAL_METADATA_ITEMS \
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen (MAILBOX_METADATA_VIRTUAL_SIZE)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstruct status_cmd_context {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct doveadm_mail_cmd_context ctx;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct mail_search_args *search_args;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen enum mailbox_status_items status_items;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen enum mailbox_metadata_items metadata_items;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen struct mailbox_status total_status;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen struct mailbox_metadata total_metadata;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen unsigned int total_sum:1;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen};
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic void status_parse_fields(struct status_cmd_context *ctx,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const char *const *fields)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (*fields == NULL)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen i_fatal_status(EX_USAGE, "No status fields");
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen for (; *fields != NULL; fields++) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const char *field = *fields;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (strcmp(field, "all") == 0) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (ctx->total_sum) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->status_items |= TOTAL_STATUS_ITEMS;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->metadata_items |= TOTAL_METADATA_ITEMS;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen } else {
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen ctx->status_items |= ALL_STATUS_ITEMS;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen ctx->metadata_items |= ALL_METADATA_ITEMS;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen } else if (strcmp(field, "messages") == 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->status_items |= STATUS_MESSAGES;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen else if (strcmp(field, "recent") == 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->status_items |= STATUS_RECENT;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen else if (strcmp(field, "uidnext") == 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->status_items |= STATUS_UIDNEXT;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen else if (strcmp(field, "uidvalidity") == 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->status_items |= STATUS_UIDVALIDITY;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen else if (strcmp(field, "unseen") == 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->status_items |= STATUS_UNSEEN;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen else if (strcmp(field, "highestmodseq") == 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->status_items |= STATUS_HIGHESTMODSEQ;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen else if (strcmp(field, "vsize") == 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->metadata_items |= MAILBOX_METADATA_VIRTUAL_SIZE;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen else if (strcmp(field, "guid") == 0)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen ctx->metadata_items |= MAILBOX_METADATA_GUID;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen else {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen i_fatal_status(EX_USAGE,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "Unknown status field: %s", field);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen if (ctx->total_sum &&
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen ((ctx->status_items & ~TOTAL_STATUS_ITEMS) != 0 ||
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen (ctx->metadata_items & ~TOTAL_METADATA_ITEMS) != 0)) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen i_fatal_status(EX_USAGE,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "Status field %s can't be used with -t", field);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic void ATTR_NULL(2)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatus_output(struct status_cmd_context *ctx, struct mailbox *box,
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen const struct mailbox_status *status,
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen const struct mailbox_metadata *metadata)
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen{
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen if (box != NULL)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen doveadm_print(mailbox_get_vname(box));
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen if ((ctx->status_items & STATUS_MESSAGES) != 0)
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen doveadm_print_num(status->messages);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if ((ctx->status_items & STATUS_RECENT) != 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen doveadm_print_num(status->recent);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if ((ctx->status_items & STATUS_UIDNEXT) != 0)
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen doveadm_print_num(status->uidnext);
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen if ((ctx->status_items & STATUS_UIDVALIDITY) != 0)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen doveadm_print_num(status->uidvalidity);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if ((ctx->status_items & STATUS_UNSEEN) != 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen doveadm_print_num(status->unseen);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if ((ctx->status_items & STATUS_HIGHESTMODSEQ) != 0)
597dba3488c648ffb375ee4a552bd52ac4346979Timo Sirainen doveadm_print_num(status->highest_modseq);
74f810327aca91b3375d3fc963bce8076785b1cbTimo Sirainen if ((ctx->metadata_items & MAILBOX_METADATA_VIRTUAL_SIZE) != 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen doveadm_print_num(metadata->virtual_size);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if ((ctx->metadata_items & MAILBOX_METADATA_GUID) != 0)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen doveadm_print(guid_128_to_string(metadata->guid));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic void
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatus_sum(struct status_cmd_context *ctx,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const struct mailbox_status *status,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const struct mailbox_metadata *metadata)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct mailbox_status *dest = &ctx->total_status;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen dest->messages += status->messages;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen dest->recent += status->recent;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen dest->unseen += status->unseen;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->total_metadata.virtual_size += metadata->virtual_size;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic int
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatus_mailbox(struct status_cmd_context *ctx, const struct mailbox_info *info)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct mailbox *box;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct mailbox_status status;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct mailbox_metadata metadata;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen box = doveadm_mailbox_find(ctx->ctx.cur_mail_user, info->vname);
0a70a2e71658d168f75d1f075661d7936701343cTimo Sirainen if (mailbox_get_status(box, ctx->status_items, &status) < 0 ||
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen mailbox_get_metadata(box, ctx->metadata_items, &metadata) < 0) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen i_error("Mailbox %s: Failed to lookup mailbox status: %s",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen mailbox_get_vname(box), mailbox_get_last_error(box, NULL));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen doveadm_mail_failed_mailbox(&ctx->ctx, box);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen mailbox_free(&box);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return -1;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen if (!ctx->total_sum)
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen status_output(ctx, box, &status, &metadata);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen else
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen status_sum(ctx, &status, &metadata);
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen mailbox_free(&box);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return 0;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic int
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainencmd_mailbox_status_run(struct doveadm_mail_cmd_context *_ctx,
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen struct mail_user *user)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen{
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen struct status_cmd_context *ctx = (struct status_cmd_context *)_ctx;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen enum mailbox_list_iter_flags iter_flags =
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen MAILBOX_LIST_ITER_NO_AUTO_BOXES |
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen MAILBOX_LIST_ITER_RETURN_NO_FLAGS;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct doveadm_mailbox_list_iter *iter;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen const struct mailbox_info *info;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen int ret = 0;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen memset(&ctx->total_status, 0, sizeof(ctx->total_status));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen memset(&ctx->total_metadata, 0, sizeof(ctx->total_metadata));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen iter = doveadm_mailbox_list_iter_init(_ctx, user, ctx->search_args,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen iter_flags);
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen T_BEGIN {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (status_mailbox(ctx, info) < 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ret = -1;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen } T_END;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ret = -1;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (ctx->total_sum) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen status_output(ctx, NULL, &ctx->total_status,
74f810327aca91b3375d3fc963bce8076785b1cbTimo Sirainen &ctx->total_metadata);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return ret;
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic void cmd_mailbox_status_init(struct doveadm_mail_cmd_context *_ctx,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const char *const args[])
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct status_cmd_context *ctx = (struct status_cmd_context *)_ctx;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const char *fields = args[0];
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (fields == NULL || args[1] == NULL)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen doveadm_mail_help_name("mailbox status");
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen status_parse_fields(ctx, t_strsplit_spaces(fields, " "));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ctx->search_args = doveadm_mail_mailbox_search_args_build(args+1);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (!ctx->total_sum) {
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen doveadm_print_header("mailbox", "mailbox",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
597dba3488c648ffb375ee4a552bd52ac4346979Timo Sirainen if ((ctx->status_items & STATUS_MESSAGES) != 0)
597dba3488c648ffb375ee4a552bd52ac4346979Timo Sirainen doveadm_print_header_simple("messages");
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen if ((ctx->status_items & STATUS_RECENT) != 0)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen doveadm_print_header_simple("recent");
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen if ((ctx->status_items & STATUS_UIDNEXT) != 0)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen doveadm_print_header_simple("uidnext");
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if ((ctx->status_items & STATUS_UIDVALIDITY) != 0)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen doveadm_print_header_simple("uidvalidity");
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen if ((ctx->status_items & STATUS_UNSEEN) != 0)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen doveadm_print_header_simple("unseen");
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen if ((ctx->status_items & STATUS_HIGHESTMODSEQ) != 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen doveadm_print_header_simple("highestmodseq");
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if ((ctx->metadata_items & MAILBOX_METADATA_VIRTUAL_SIZE) != 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen doveadm_print_header_simple("vsize");
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if ((ctx->metadata_items & MAILBOX_METADATA_GUID) != 0)
9511a40d933181045343110c8101b75887062aaeTimo Sirainen doveadm_print_header_simple("guid");
9511a40d933181045343110c8101b75887062aaeTimo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
static void cmd_mailbox_status_deinit(struct doveadm_mail_cmd_context *_ctx)
{
struct status_cmd_context *ctx = (struct status_cmd_context *)_ctx;
if (ctx->search_args != NULL)
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;
doveadm_print_init(DOVEADM_PRINT_TYPE_FLOW);
return &ctx->ctx;
}
struct doveadm_cmd_ver2 doveadm_cmd_mailbox_status_ver2 = {
.name = "mailbox status",
.mail_cmd = cmd_mailbox_status_alloc,
.usage = DOVEADM_CMD_MAIL_USAGE_PREFIX"<fields> <mailbox> [...]",
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_MAIL_COMMON
DOVEADM_CMD_PARAM('t', "total-sum", CMD_PARAM_BOOL, 0)
DOVEADM_CMD_PARAM('f', "field", CMD_PARAM_ARRAY, 0)
DOVEADM_CMD_PARAM('\0', "fieldstr", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL | CMD_PARAM_FLAG_DO_NOT_EXPOSE) /* FIXME: horrible hack, remove me when possible */
DOVEADM_CMD_PARAM('\0', "mailbox-mask", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
};