f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen#include "lib.h"
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen#include "mail-storage.h"
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen#include "doveadm-print.h"
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen#include "doveadm-mailbox-list-iter.h"
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen#include "doveadm-mail-iter.h"
146240408e677e99e579d1feed92689585cc25d4Timo Sirainen#include "doveadm-mail.h"
b1dd6be436e887774b94965ebe9af6d04179c227Timo Sirainen
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen#include <stdio.h>
4073f0dbf3277f981a8fcee3b89ea15aaf380a7fTimo Sirainen
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomistatic int
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomicmd_search_box(struct doveadm_mail_cmd_context *ctx,
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen const struct mailbox_info *info)
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomi{
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomi struct doveadm_mail_iter *iter;
0aac625db5e6e179c8ee7420a12ab300d6b178edTimo Sirainen struct mailbox *box;
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen struct mail *mail;
e5224c0589916fb22f95f959326cf4b6221715b0Timo Sirainen struct mailbox_metadata metadata;
ca44a6ba994aaa3231a20ef6e046dfd97a8dcd2dTimo Sirainen const char *guid_str;
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen int ret = 0;
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen if (doveadm_mail_iter_init(ctx, info, ctx->search_args, 0, NULL, FALSE,
ca44a6ba994aaa3231a20ef6e046dfd97a8dcd2dTimo Sirainen &iter) < 0)
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen return -1;
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen box = doveadm_mail_iter_get_mailbox(iter);
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomi
ca44a6ba994aaa3231a20ef6e046dfd97a8dcd2dTimo Sirainen if (mailbox_get_metadata(box, MAILBOX_METADATA_GUID, &metadata) < 0) {
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen i_error("Couldn't get mailbox '%s' GUID: %s",
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen mailbox_get_vname(box),
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen mailbox_get_last_internal_error(box, NULL));
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen ret = -1;
146240408e677e99e579d1feed92689585cc25d4Timo Sirainen doveadm_mail_failed_mailbox(ctx, box);
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen } else {
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen guid_str = guid_128_to_string(metadata.guid);
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen while (doveadm_mail_iter_next(iter, &mail)) {
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen doveadm_print(guid_str);
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen T_BEGIN {
cbe49ba128638e63395aedaa2144087c89835633Timo Sirainen doveadm_print(dec2str(mail->uid));
cbe49ba128638e63395aedaa2144087c89835633Timo Sirainen } T_END;
}
}
if (doveadm_mail_iter_deinit(&iter) < 0)
ret = -1;
return ret;
}
static int
cmd_search_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user)
{
const enum mailbox_list_iter_flags iter_flags =
MAILBOX_LIST_ITER_NO_AUTO_BOXES |
MAILBOX_LIST_ITER_RETURN_NO_FLAGS;
struct doveadm_mailbox_list_iter *iter;
const struct mailbox_info *info;
int ret = 0;
iter = doveadm_mailbox_list_iter_init(ctx, user, ctx->search_args,
iter_flags);
while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) T_BEGIN {
if (cmd_search_box(ctx, info) < 0)
ret = -1;
} T_END;
if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
ret = -1;
return ret;
}
static void cmd_search_init(struct doveadm_mail_cmd_context *ctx,
const char *const args[])
{
if (args[0] == NULL)
doveadm_mail_help_name("search");
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)
{
struct doveadm_mail_cmd_context *ctx;
ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
ctx->v.init = cmd_search_init;
ctx->v.run = cmd_search_run;
doveadm_print_init(DOVEADM_PRINT_TYPE_FLOW);
return ctx;
}
struct doveadm_cmd_ver2 doveadm_cmd_search_ver2 = {
.name = "search",
.mail_cmd = cmd_search_alloc,
.usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "<search query>",
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_MAIL_COMMON
DOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
};