changeset 19900:6f760a1def9f

doveadm kick: Don't show who command's help on duplicate network/username masks
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Sun, 28 Feb 2016 18:59:35 +0200
parents ef466c52207b
children 579e186b9623
files src/doveadm/doveadm-kick.c src/doveadm/doveadm-who.c src/doveadm/doveadm-who.h
diffstat 3 files changed, 21 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/doveadm/doveadm-kick.c	Sun Feb 28 18:48:15 2016 +0200
+++ b/src/doveadm/doveadm-kick.c	Sun Feb 28 18:59:35 2016 +0200
@@ -196,7 +196,8 @@
 		i_fatal_status(EX_USAGE,
 			       "user and/or ip[/bits] must be specified.");
 	}
-	who_parse_args(&ctx.who, argv);
+	if (who_parse_args(&ctx.who, (const char *const *)argv + 1) < 0)
+		help(&doveadm_cmd_kick);
 
 	who_lookup(&ctx.who, kick_aggregate_line);
 	kick_users(&ctx);
--- a/src/doveadm/doveadm-who.c	Sun Feb 28 18:48:15 2016 +0200
+++ b/src/doveadm/doveadm-who.c	Sun Feb 28 18:59:35 2016 +0200
@@ -112,24 +112,30 @@
 		array_append(&user->pids, &line->pid, 1);
 }
 
-void who_parse_args(struct who_context *ctx, char **args)
+int who_parse_args(struct who_context *ctx, const char *const *masks)
 {
 	struct ip_addr net_ip;
-	unsigned int net_bits;
+	unsigned int i, net_bits;
 
-	while (args[1] != NULL) {
-		if (net_parse_range(args[1], &net_ip, &net_bits) == 0) {
-			if (ctx->filter.net_bits != 0)
-				help(&doveadm_cmd_who);
+	for (i = 0; masks[i] != NULL; i++) {
+		if (net_parse_range(masks[i], &net_ip, &net_bits) == 0) {
+			if (ctx->filter.net_bits != 0) {
+				i_error("Multiple network masks not supported");
+				doveadm_exit_code = EX_USAGE;
+				return -1;
+			}
 			ctx->filter.net_ip = net_ip;
 			ctx->filter.net_bits = net_bits;
 		} else {
-			if (ctx->filter.username != NULL)
-				help(&doveadm_cmd_who);
-			ctx->filter.username = args[1];
+			if (ctx->filter.username != NULL) {
+				i_error("Multiple username masks not supported");
+				doveadm_exit_code = EX_USAGE;
+				return -1;
+			}
+			ctx->filter.username = masks[i];
 		}
-		args++;
 	}
+	return 0;
 }
 
 void who_lookup(struct who_context *ctx, who_callback_t *callback)
@@ -293,7 +299,8 @@
 	}
 
 	argv += optind - 1;
-	who_parse_args(&ctx, argv);
+	if (who_parse_args(&ctx, (const char *const *)argv + 1) < 0)
+		help(&doveadm_cmd_who);
 
 	doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);
 	if (!separate_connections) {
--- a/src/doveadm/doveadm-who.h	Sun Feb 28 18:48:15 2016 +0200
+++ b/src/doveadm/doveadm-who.h	Sun Feb 28 18:59:35 2016 +0200
@@ -27,7 +27,7 @@
 typedef void who_callback_t(struct who_context *ctx,
 			    const struct who_line *line);
 
-void who_parse_args(struct who_context *ctx, char **args);
+int who_parse_args(struct who_context *ctx, const char *const *masks);
 
 void who_lookup(struct who_context *ctx, who_callback_t *callback);