dovecot-2.0: doveadm: Mail commands now prefix each line with us...

dovecot at dovecot.org dovecot at dovecot.org
Wed Jun 9 20:53:45 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/3dfe1690b568
changeset: 11512:3dfe1690b568
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jun 09 18:53:40 2010 +0100
description:
doveadm: Mail commands now prefix each line with username if -A parameter is given.

diffstat:

 src/doveadm/doveadm-mail-mailbox.c |   4 ++--
 src/doveadm/doveadm-mail-search.c  |  10 +++++-----
 src/doveadm/doveadm-mail.c         |  36 +++++++++++++++++++++++++++++-------
 src/doveadm/doveadm-mail.h         |   9 +++++++++
 src/plugins/quota/doveadm-quota.c  |  34 +++++++++++++++++-----------------
 5 files changed, 62 insertions(+), 31 deletions(-)

diffs (252 lines):

diff -r 9f9f9d9e4a79 -r 3dfe1690b568 src/doveadm/doveadm-mail-mailbox.c
--- a/src/doveadm/doveadm-mail-mailbox.c	Wed Jun 09 18:52:42 2010 +0100
+++ b/src/doveadm/doveadm-mail-mailbox.c	Wed Jun 09 18:53:40 2010 +0100
@@ -124,9 +124,9 @@
 	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);
 }
diff -r 9f9f9d9e4a79 -r 3dfe1690b568 src/doveadm/doveadm-mail-search.c
--- a/src/doveadm/doveadm-mail-search.c	Wed Jun 09 18:52:42 2010 +0100
+++ b/src/doveadm/doveadm-mail-search.c	Wed Jun 09 18:53:40 2010 +0100
@@ -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 @@
 	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 @@
 	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 @@
 
 	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);
 }
diff -r 9f9f9d9e4a79 -r 3dfe1690b568 src/doveadm/doveadm-mail.c
--- a/src/doveadm/doveadm-mail.c	Wed Jun 09 18:52:42 2010 +0100
+++ b/src/doveadm/doveadm-mail.c	Wed Jun 09 18:53:40 2010 +0100
@@ -41,6 +41,7 @@
 	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 @@
 		       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 @@
 	}
 
 	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 @@
 		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 @@
 	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 @@
 
 	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 @@
 	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[])
diff -r 9f9f9d9e4a79 -r 3dfe1690b568 src/doveadm/doveadm-mail.h
--- a/src/doveadm/doveadm-mail.h	Wed Jun 09 18:52:42 2010 +0100
+++ b/src/doveadm/doveadm-mail.h	Wed Jun 09 18:53:40 2010 +0100
@@ -38,9 +38,13 @@
 	/* 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 @@
 #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;
diff -r 9f9f9d9e4a79 -r 3dfe1690b568 src/plugins/quota/doveadm-quota.c
--- a/src/plugins/quota/doveadm-quota.c	Wed Jun 09 18:52:42 2010 +0100
+++ b/src/plugins/quota/doveadm-quota.c	Wed Jun 09 18:53:40 2010 +0100
@@ -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 *


More information about the dovecot-cvs mailing list