]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Mail commands now prefix each line with username if -A parameter is given.
authorTimo Sirainen <tss@iki.fi>
Wed, 9 Jun 2010 17:53:40 +0000 (18:53 +0100)
committerTimo Sirainen <tss@iki.fi>
Wed, 9 Jun 2010 17:53:40 +0000 (18:53 +0100)
--HG--
branch : HEAD

src/doveadm/doveadm-mail-mailbox.c
src/doveadm/doveadm-mail-search.c
src/doveadm/doveadm-mail.c
src/doveadm/doveadm-mail.h
src/plugins/quota/doveadm-quota.c

index 4b0e17c59b2120820bc5bc7190521c7a8b92531a..6ea01d670c6723cd76ef9798ad2d7602f3b8220a 100644 (file)
@@ -124,9 +124,9 @@ cmd_mailbox_list_run(struct doveadm_mail_cmd_context *_ctx,
        while ((info = doveadm_mail_list_iter_next(iter)) != NULL) {
                str_truncate(str, 0);
                if (ctx->mutf7 || imap_utf7_to_utf8(info->name, str) < 0)
-                       printf("%s\n", info->name);
+                       dm_printf(_ctx, "%s\n", info->name);
                else
-                       printf("%s\n", str_c(str));
+                       dm_printf(_ctx, "%s\n", str_c(str));
        }
        doveadm_mail_list_iter_deinit(&iter);
 }
index 02174acfd56376982cc2e631e3c673816eac0515..82e0fb2ba5f7c730eee0e9fe5c7c0e2a837d98fd 100644 (file)
@@ -9,8 +9,8 @@
 #include <stdio.h>
 
 static int
-cmd_search_box(const struct mailbox_info *info,
-              struct mail_search_args *search_args)
+cmd_search_box(struct doveadm_mail_cmd_context *ctx,
+              const struct mailbox_info *info)
 {
        struct doveadm_mail_iter *iter;
        struct mailbox_transaction_context *trans;
@@ -19,7 +19,7 @@ cmd_search_box(const struct mailbox_info *info,
        const char *guid_str;
        int ret = 0;
 
-       if (doveadm_mail_iter_init(info, search_args, &trans, &iter) < 0)
+       if (doveadm_mail_iter_init(info, ctx->search_args, &trans, &iter) < 0)
                return -1;
 
        mail = mail_alloc(trans, 0, NULL);
@@ -28,7 +28,7 @@ cmd_search_box(const struct mailbox_info *info,
        else {
                guid_str = mail_guid_128_to_string(guid);
                while (doveadm_mail_iter_next(iter, mail))
-                       printf("%s %u\n", guid_str, mail->uid);
+                       dm_printf(ctx, "%s %u\n", guid_str, mail->uid);
        }
        mail_free(&mail);
        if (doveadm_mail_iter_deinit(&iter) < 0)
@@ -49,7 +49,7 @@ cmd_search_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user)
 
        iter = doveadm_mail_list_iter_init(user, ctx->search_args, iter_flags);
        while ((info = doveadm_mail_list_iter_next(iter)) != NULL) T_BEGIN {
-               (void)cmd_search_box(info, ctx->search_args);
+               (void)cmd_search_box(ctx, info);
        } T_END;
        doveadm_mail_list_iter_deinit(&iter);
 }
index 575053ada0c40268afa6bb564ff8f2ce2caf0604..c25139319412ac8638e609145be12e6cf25f0bc1 100644 (file)
@@ -41,6 +41,7 @@ doveadm_mail_cmd_alloc_size(size_t size)
        pool = pool_alloconly_create("doveadm mail cmd", 1024);
        ctx = p_malloc(pool, size);
        ctx->pool = pool;
+       ctx->dm_printf_last_lf = TRUE;
        return ctx;
 }
 
@@ -179,7 +180,6 @@ doveadm_mail_next_user(struct doveadm_mail_cmd_context *ctx,
                       const char **error_r)
 {
        struct mail_storage_service_user *service_user;
-       struct mail_user *mail_user;
        const char *error;
        int ret;
 
@@ -196,15 +196,15 @@ doveadm_mail_next_user(struct doveadm_mail_cmd_context *ctx,
        }
 
        ret = mail_storage_service_next(ctx->storage_service, service_user,
-                                       &mail_user);
+                                       &ctx->cur_mail_user);
        if (ret < 0) {
                *error_r = "User init failed";
                mail_storage_service_user_free(&service_user);
                return ret;
        }
 
-       ctx->v.run(ctx, mail_user);
-       mail_user_unref(&mail_user);
+       ctx->v.run(ctx, ctx->cur_mail_user);
+       mail_user_unref(&ctx->cur_mail_user);
        mail_storage_service_user_free(&service_user);
        return 1;
 }
@@ -331,7 +331,6 @@ doveadm_mail_cmd(const struct doveadm_mail_cmd *cmd, int argc, char *argv[])
                MAIL_STORAGE_SERVICE_FLAG_NO_LOG_INIT;
        struct doveadm_mail_cmd_context *ctx;
        const char *getopt_args, *username, *wildcard_user;
-       bool all_users = FALSE;
        int c;
 
        if (doveadm_debug)
@@ -353,7 +352,7 @@ doveadm_mail_cmd(const struct doveadm_mail_cmd *cmd, int argc, char *argv[])
        while ((c = getopt(argc, argv, getopt_args)) > 0) {
                switch (c) {
                case 'A':
-                       all_users = TRUE;
+                       ctx->iterate_all_users = TRUE;
                        break;
                case 'u':
                        service_flags |=
@@ -378,7 +377,7 @@ doveadm_mail_cmd(const struct doveadm_mail_cmd *cmd, int argc, char *argv[])
 
        ctx->v.init(ctx, (const void *)argv);
 
-       if (!all_users && wildcard_user == NULL) {
+       if (!ctx->iterate_all_users && wildcard_user == NULL) {
                doveadm_mail_single_user(ctx, username, service_flags);
        } else {
                service_flags |= MAIL_STORAGE_SERVICE_FLAG_TEMP_PRIV_DROP;
@@ -387,6 +386,29 @@ doveadm_mail_cmd(const struct doveadm_mail_cmd *cmd, int argc, char *argv[])
        ctx->v.deinit(ctx);
 }
 
+void dm_printf(struct doveadm_mail_cmd_context *ctx, const char *format, ...)
+{
+       va_list args;
+
+       va_start(args, format);
+       if (!ctx->iterate_all_users)
+               vprintf(format, args);
+       else T_BEGIN {
+               const char *str = t_strdup_vprintf(format, args);
+               bool prev_lf = ctx->dm_printf_last_lf;
+
+               for (; *str != '\0'; str++) {
+                       if (prev_lf)
+                               printf("%s: ", ctx->cur_mail_user->username);
+                       putchar(*str);
+                       prev_lf = *str == '\n';
+               }
+               ctx->dm_printf_last_lf = prev_lf;
+               
+       } T_END;
+       va_end(args);
+}
+
 static bool
 doveadm_mail_try_run_multi_word(const struct doveadm_mail_cmd *cmd,
                                const char *cmdname, int argc, char *argv[])
index e1450585515882bc26d85cd0b2224752980f1015..dd70b44ce94fa3c2cdb3ec0f798761090882d765 100644 (file)
@@ -38,9 +38,13 @@ struct doveadm_mail_cmd_context {
        /* search args aren't set for all mail commands */
        struct mail_search_args *search_args;
 
+       struct mail_user *cur_mail_user;
        struct doveadm_mail_cmd_vfuncs v;
 
        ARRAY_DEFINE(module_contexts, union doveadm_mail_cmd_module_context *);
+
+       unsigned int iterate_all_users:1;
+       unsigned int dm_printf_last_lf:1;
 };
 
 struct doveadm_mail_cmd {
@@ -76,6 +80,11 @@ doveadm_mail_cmd_alloc_size(size_t size);
 #define doveadm_mail_cmd_alloc(type) \
        (type *)doveadm_mail_cmd_alloc_size(sizeof(type))
 
+/* same as printf(), but when running with -A parameter,
+   prefix each line with username. */
+void dm_printf(struct doveadm_mail_cmd_context *ctx, const char *format, ...)
+       ATTR_FORMAT(2, 3);
+
 struct doveadm_mail_cmd cmd_expunge;
 struct doveadm_mail_cmd cmd_search;
 struct doveadm_mail_cmd cmd_fetch;
index fc704aadfaafa892b9c1be9eaae8f4add63b09ed..bb4f1f4301a45377d13fe232ee83967f1d90307e 100644 (file)
@@ -6,52 +6,52 @@
 #include "quota-private.h"
 #include "doveadm-mail.h"
 
-#include <stdio.h>
-
 const char *doveadm_quota_plugin_version = DOVECOT_VERSION;
 
 void doveadm_quota_plugin_init(struct module *module);
 void doveadm_quota_plugin_deinit(void);
 
-static void cmd_quota_get_root(struct mail_user *user, struct quota_root *root)
+static void
+cmd_quota_get_root(struct doveadm_mail_cmd_context *ctx,
+                  struct quota_root *root)
 {
        const char *const *res;
        uint64_t value, limit;
        int ret;
 
-       printf("%s(%s): ", user->username, root->set->name);
+       dm_printf(ctx, "%s: ", root->set->name);
        res = quota_root_get_resources(root);
        for (; *res != NULL; res++) {
                ret = quota_get_resource(root, "", *res, &value, &limit);
-               printf("%s ", *res);
+               dm_printf(ctx, "%s ", *res);
                if (ret > 0) {
-                       printf("%llu/%llu",
-                              (unsigned long long)value,
-                              (unsigned long long)limit);
+                       dm_printf(ctx, "%llu/%llu",
+                                 (unsigned long long)value,
+                                 (unsigned long long)limit);
                        if (limit >= 100) {
-                               printf(" (%u%%)",
-                                      (unsigned int)(value / (limit/100)));
+                               dm_printf(ctx, " (%u%%)",
+                                         (unsigned int)(value / (limit/100)));
                        }
                } else if (ret == 0) {
-                       printf("%llu/unlimited",
-                              (unsigned long long)value);
+                       dm_printf(ctx, "%llu/unlimited",
+                                 (unsigned long long)value);
                } else
-                       printf("error");
+                       dm_printf(ctx, "error");
                if (res[1] != NULL)
-                       printf(", ");
+                       dm_printf(ctx, ", ");
        }
-       printf("\n");
+       dm_printf(ctx, "\n");
 }
 
 static void
-cmd_quota_get_run(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
+cmd_quota_get_run(struct doveadm_mail_cmd_context *ctx,
                  struct mail_user *user)
 {
        struct quota_user *quser = QUOTA_USER_CONTEXT(user);
        struct quota_root *const *root;
 
        array_foreach(&quser->quota->roots, root)
-               cmd_quota_get_root(user, *root);
+               cmd_quota_get_root(ctx, *root);
 }
 
 static struct doveadm_mail_cmd_context *