From d92ad7b18b50f41a67fff78fb5b9a2274eaf9566 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan.bosch@open-xchange.com>
Date: Tue, 22 Jun 2021 16:18:49 +0200
Subject: [PATCH] managesieve-login: Reformat client.c.

---
 src/managesieve-login/client.c | 204 +++++++++++++++++----------------
 1 file changed, 105 insertions(+), 99 deletions(-)

diff --git a/src/managesieve-login/client.c b/src/managesieve-login/client.c
index 5961f119c..2b4357284 100644
--- a/src/managesieve-login/client.c
+++ b/src/managesieve-login/client.c
@@ -22,14 +22,13 @@
 #include "managesieve-login-settings.h"
 #include "managesieve-proxy.h"
 
-
 /* Disconnect client when it sends too many bad commands */
 #define CLIENT_MAX_BAD_COMMANDS 3
 
 struct managesieve_command {
 	const char *name;
-	int (*func)
-		(struct managesieve_client *client, const struct managesieve_arg *args);
+	int (*func)(struct managesieve_client *client,
+		    const struct managesieve_arg *args);
 	int preparsed_args;
 };
 
@@ -55,39 +54,46 @@ bool client_skip_line(struct managesieve_client *client)
 static void client_send_capabilities(struct client *client)
 {
 	struct managesieve_client *msieve_client =
-		(struct managesieve_client *) client;
+		(struct managesieve_client *)client;
 	const char *saslcap;
 
 	T_BEGIN {
 		saslcap = client_authenticate_get_capabilities(client);
 
 		/* Default capabilities */
-		client_send_raw(client, t_strconcat("\"IMPLEMENTATION\" \"",
-			msieve_client->set->managesieve_implementation_string, "\"\r\n", NULL));
-		client_send_raw(client, t_strconcat("\"SIEVE\" \"",
-			msieve_client->set->managesieve_sieve_capability, "\"\r\n", NULL));
-		if ( msieve_client->set->managesieve_notify_capability != NULL )
-			client_send_raw(client, t_strconcat("\"NOTIFY\" \"",
-				msieve_client->set->managesieve_notify_capability, "\"\r\n", NULL));
-		client_send_raw
-			(client, t_strconcat("\"SASL\" \"", saslcap, "\"\r\n", NULL));
+		client_send_raw(client, t_strconcat(
+			"\"IMPLEMENTATION\" \"",
+			msieve_client->set->managesieve_implementation_string,
+			"\"\r\n", NULL));
+		client_send_raw(client, t_strconcat(
+			"\"SIEVE\" \"",
+			msieve_client->set->managesieve_sieve_capability,
+			"\"\r\n", NULL));
+		if (msieve_client->set->managesieve_notify_capability != NULL) {
+			client_send_raw(client, t_strconcat(
+				"\"NOTIFY\" \"",
+				msieve_client->set->managesieve_notify_capability,
+				"\"\r\n", NULL));
+		}
+		client_send_raw(client, t_strconcat("\"SASL\" \"", saslcap,
+						    "\"\r\n", NULL));
 
 		/* STARTTLS */
 		if (login_ssl_initialized && !client->tls)
-			client_send_raw(client, "\"STARTTLS\"\r\n" );
+			client_send_raw(client, "\"STARTTLS\"\r\n");
 
 		/* Protocol version */
 		client_send_raw(client, "\"VERSION\" \"1.0\"\r\n");
 
 		/* XCLIENT */
-	    if (client->trusted)
-        	client_send_raw(client, "\"XCLIENT\"\r\n");
+		if (client->trusted)
+			client_send_raw(client, "\"XCLIENT\"\r\n");
 	} T_END;
 }
 
-static int cmd_capability
-(struct managesieve_client *client,
-	const struct managesieve_arg *args ATTR_UNUSED)
+static int
+cmd_capability(struct managesieve_client *client,
+	       const struct managesieve_arg *args ATTR_UNUSED)
 {
 	o_stream_cork(client->common.output);
 
@@ -99,39 +105,37 @@ static int cmd_capability
 	return 1;
 }
 
-static int cmd_starttls
-(struct managesieve_client *client,
-	const struct managesieve_arg *args ATTR_UNUSED)
+static int
+cmd_starttls(struct managesieve_client *client,
+	     const struct managesieve_arg *args ATTR_UNUSED)
 {
 	client_cmd_starttls(&client->common);
 	return 1;
 }
 
-static void managesieve_client_notify_starttls
-(struct client *client, bool success, const char *text)
+static void
+managesieve_client_notify_starttls(struct client *client, bool success,
+				   const char *text)
 {
-	if ( success )
+	if (success)
 		client_send_ok(client, text);
 	else
 		client_send_no(client, text);
 }
 
-static int cmd_noop
-(struct managesieve_client *client,
-	const struct managesieve_arg *args)
+static int
+cmd_noop(struct managesieve_client *client, const struct managesieve_arg *args)
 {
 	const char *text;
 	string_t *resp_code;
 
-	if ( MANAGESIEVE_ARG_IS_EOL(&args[0]) ) {
+	if (MANAGESIEVE_ARG_IS_EOL(&args[0])) {
 		client_send_ok(&client->common, "NOOP Completed");
 		return 1;
 	}
-
-	if ( !MANAGESIEVE_ARG_IS_EOL(&args[1]) )
+	if (!MANAGESIEVE_ARG_IS_EOL(&args[1]))
 		return -1;
-
-	if ( !managesieve_arg_get_string(&args[0], &text) ) {
+	if (!managesieve_arg_get_string(&args[0], &text)) {
 		client_send_no(&client->common, "Invalid echo tag.");
 		return 1;
 	}
@@ -144,49 +148,49 @@ static int cmd_noop
 	return 1;
 }
 
-static int cmd_logout
-(struct managesieve_client *client,
-	const struct managesieve_arg *args ATTR_UNUSED)
+static int
+cmd_logout(struct managesieve_client *client,
+	   const struct managesieve_arg *args ATTR_UNUSED)
 {
 	client_send_ok(&client->common, "Logout completed.");
 	client_destroy(&client->common, CLIENT_UNAUTHENTICATED_LOGOUT_MSG);
 	return 1;
 }
 
-static int cmd_xclient
-(struct managesieve_client *client,
-	const struct managesieve_arg *args)
+static int
+cmd_xclient(struct managesieve_client *client,
+	    const struct managesieve_arg *args)
 {
 	const char *arg;
 	bool args_ok = TRUE;
 
-	if ( !client->common.trusted ) {
-		client_send_no(&client->common,
-			"You are not from trusted IP");
+	if (!client->common.trusted) {
+		client_send_no(&client->common, "You are not from trusted IP");
 		return 1;
 	}
 	while (!MANAGESIEVE_ARG_IS_EOL(&args[0]) &&
 		managesieve_arg_get_atom(&args[0], &arg)) {
-		if ( strncasecmp(arg, "ADDR=", 5) == 0 ) {
+		if (strncasecmp(arg, "ADDR=", 5) == 0) {
 			if (net_addr2ip(arg + 5, &client->common.ip) < 0)
 				args_ok = FALSE;
-		} else if ( strncasecmp(arg, "PORT=", 5) == 0 ) {
-			if (net_str2port(arg + 5, &client->common.remote_port) < 0)
+		} else if (strncasecmp(arg, "PORT=", 5) == 0) {
+			if (net_str2port(arg + 5,
+					 &client->common.remote_port) < 0)
 				args_ok = FALSE;
-		} else if ( strncasecmp(arg, "SESSION=", 8) == 0 ) {
+		} else if (strncasecmp(arg, "SESSION=", 8) == 0) {
 			const char *value = arg + 8;
 
 			if (strlen(value) <= LOGIN_MAX_SESSION_ID_LEN) {
 				client->common.session_id =
 					p_strdup(client->common.pool, value);
 			}
-		} else if ( strncasecmp(arg, "TTL=", 4 ) == 0) {
+		} else if (strncasecmp(arg, "TTL=", 4)  == 0) {
 			if (str_to_uint(arg + 4, &client->common.proxy_ttl) < 0)
 				args_ok = FALSE;
 		}
 		args++;
 	}
-	if ( !args_ok || !MANAGESIEVE_ARG_IS_EOL(&args[0]))
+	if (!args_ok || !MANAGESIEVE_ARG_IS_EOL(&args[0]))
 		return -1;
 
 	client_send_ok(&client->common, "Updated");
@@ -208,13 +212,13 @@ static bool client_handle_input(struct managesieve_client *client)
 	i_assert(!client->common.authenticating);
 
 	if (client->cmd_finished) {
-		/* clear the previous command from memory */
+		/* Clear the previous command from memory */
 		client->cmd_name = NULL;
 		client->cmd_parsed_args = FALSE;
 		client->cmd = NULL;
 		managesieve_parser_reset(client->parser);
 
-		/* remove \r\n */
+		/* Remove \r\n */
 		if (client->skip_line) {
 			if (!client_skip_line(client))
 				return FALSE;
@@ -230,17 +234,17 @@ static bool client_handle_input(struct managesieve_client *client)
 
 		client->cmd_name = managesieve_parser_read_word(client->parser);
 		if (client->cmd_name == NULL)
-			return FALSE; /* need more data */
+			return FALSE; /* Need more data */
 
 		cmd_name = t_str_ucase(client->cmd_name);
 		cmd = commands;
-		while ( cmd->name != NULL ) {
-			if ( strcmp(cmd->name, cmd_name) == 0 )
+		while (cmd->name != NULL) {
+			if (strcmp(cmd->name, cmd_name) == 0)
 				break;
 			cmd++;
 		}
 
-		if ( cmd->name != NULL )
+		if (cmd->name != NULL)
 			client->cmd = cmd;
 		else
 			client->skip_line = TRUE;
@@ -258,45 +262,48 @@ static bool managesieve_client_input_next_cmd(struct client *_client)
 	bool fatal;
 
 	if (client->cmd == NULL) {
-		/* unknown command */
+		/* Unknown command */
 		ret = -1;
-	} else if ( !client->cmd_parsed_args ) {
+	} else if (!client->cmd_parsed_args) {
 		unsigned int arg_count =
-			( client->cmd->preparsed_args > 0 ? client->cmd->preparsed_args : 0 );
-		switch (managesieve_parser_read_args(client->parser, arg_count, 0, &args)) {
+			(client->cmd->preparsed_args > 0 ?
+			 client->cmd->preparsed_args : 0);
+
+		switch (managesieve_parser_read_args(client->parser, arg_count,
+						     0, &args)) {
 		case -1:
-			/* error */
-			msg = managesieve_parser_get_error(client->parser, &fatal);
+			/* Error */
+			msg = managesieve_parser_get_error(client->parser,
+							   &fatal);
 			if (fatal) {
 				client_send_bye(&client->common, msg);
 				client_destroy(&client->common, msg);
 				return FALSE;
 			}
-
 			client_send_no(&client->common, msg);
 			client->cmd_finished = TRUE;
 			client->skip_line = TRUE;
 			return TRUE;
 		case -2:
-			/* not enough data */
+			/* Not enough data */
 			return FALSE;
 		}
 		i_assert(args != NULL);
 
-		if (arg_count == 0 ) {
-			/* we read the entire line - skip over the CRLF */
+		if (arg_count == 0) {
+			/* We read the entire line - skip over the CRLF */
 			if (!client_skip_line(client))
 				i_unreached();
 		} else {
-			/* get rid of it later */
+			/* Get rid of it later */
 			client->skip_line = TRUE;
 		}
 
 		client->cmd_parsed_args = TRUE;
 
 		if (client->cmd->preparsed_args == -1) {
-			/* check absence of arguments */
-			if ( args[0].type != MANAGESIEVE_ARG_EOL )
+			/* Check absence of arguments */
+			if (args[0].type != MANAGESIEVE_ARG_EOL)
 				ret = -1;
 		}
 	}
@@ -325,20 +332,20 @@ static bool managesieve_client_input_next_cmd(struct client *_client)
 static void managesieve_client_input(struct client *client)
 {
 	struct managesieve_client *managesieve_client =
-		(struct managesieve_client *) client;
+		(struct managesieve_client *)client;
 
 	if (!client_read(client))
 		return;
 
 	client_ref(client);
-
 	o_stream_cork(managesieve_client->common.output);
 	for (;;) {
 		if (!auth_client_is_connected(auth_client)) {
-			/* we're not currently connected to auth process -
+			/* We're not currently connected to auth process -
 			   don't allow any commands */
-			/* FIXME: Can't do untagged responses with managesieve. Any other ways?
-			client_send_ok(client, AUTH_SERVER_WAITING_MSG);
+			/* FIXME: Can't do untagged responses with managesieve.
+			   Any other ways?
+			   client_send_ok(client, AUTH_SERVER_WAITING_MSG);
 			*/
 			timeout_remove(&client->to_auth_waiting);
 
@@ -361,22 +368,21 @@ static struct client *managesieve_client_alloc(pool_t pool)
 	return &msieve_client->common;
 }
 
-static void managesieve_client_create
-(struct client *client, void **other_sets)
+static void managesieve_client_create(struct client *client, void **other_sets)
 {
 	struct managesieve_client *msieve_client =
-		(struct managesieve_client *) client;
+		(struct managesieve_client *)client;
 
 	msieve_client->set = other_sets[0];
-	msieve_client->parser = managesieve_parser_create
-		(msieve_client->common.input, MAX_MANAGESIEVE_LINE);
+	msieve_client->parser = managesieve_parser_create(
+		msieve_client->common.input, MAX_MANAGESIEVE_LINE);
 	client->io = io_add(client->fd, IO_READ, client_input, client);
 }
 
 static void managesieve_client_destroy(struct client *client)
 {
 	struct managesieve_client *managesieve_client =
-		(struct managesieve_client *) client;
+		(struct managesieve_client *)client;
 
 	managesieve_parser_destroy(&managesieve_client->parser);
 }
@@ -384,7 +390,7 @@ static void managesieve_client_destroy(struct client *client)
 static void managesieve_client_notify_auth_ready(struct client *client)
 {
 	/* Cork the stream to send the capability data as a single tcp frame
-	 *   Some naive clients break if we don't.
+	   Some naive clients break if we don't.
 	 */
 	o_stream_cork(client->output);
 
@@ -400,17 +406,17 @@ static void managesieve_client_notify_auth_ready(struct client *client)
 static void managesieve_client_starttls(struct client *client)
 {
 	struct managesieve_client *msieve_client =
-		(struct managesieve_client *) client;
+		(struct managesieve_client *)client;
 
 	managesieve_parser_destroy(&msieve_client->parser);
-	msieve_client->parser = managesieve_parser_create
-		(msieve_client->common.input, MAX_MANAGESIEVE_LINE);
+	msieve_client->parser = managesieve_parser_create(
+		msieve_client->common.input, MAX_MANAGESIEVE_LINE);
 
 	/* CRLF is lost from buffer when streams are reopened. */
 	msieve_client->skip_line = FALSE;
 
 	/* Cork the stream to send the capability data as a single tcp frame
-	 *   Some naive clients break if we don't.
+	   Some naive clients break if we don't.
 	 */
 	o_stream_cork(client->output);
 
@@ -421,9 +427,8 @@ static void managesieve_client_starttls(struct client *client)
 }
 
 static void
-client_send_reply_raw(struct client *client,
-				   const char *prefix, const char *resp_code,
-				   const char *text)
+client_send_reply_raw(struct client *client, const char *prefix,
+		      const char *resp_code, const char *text)
 {
 	T_BEGIN {
 		string_t *line = t_str_new(256);
@@ -436,7 +441,7 @@ client_send_reply_raw(struct client *client,
 			str_append_c(line, ')');
 		}
 
-		if ( text != NULL )	{
+		if (text != NULL) {
 			str_append_c(line, ' ');
 			managesieve_quote_append_string(line, text, TRUE);
 		}
@@ -447,9 +452,9 @@ client_send_reply_raw(struct client *client,
 	} T_END;
 }
 
-void client_send_reply_code
-(struct client *client, enum managesieve_cmd_reply reply, const char *resp_code,
-	const char *text)
+void client_send_reply_code(struct client *client,
+			    enum managesieve_cmd_reply reply,
+			    const char *resp_code, const char *text)
 {
 	const char *prefix = "NO";
 
@@ -467,22 +472,23 @@ void client_send_reply_code
 	client_send_reply_raw(client, prefix, resp_code, text);
 }
 
-void client_send_reply
-(struct client *client, enum managesieve_cmd_reply reply, const char *text)
+void client_send_reply(struct client *client, enum managesieve_cmd_reply reply,
+		       const char *text)
 {
 	client_send_reply_code(client, reply, NULL, text);
 }
 
 static void
-managesieve_client_notify_disconnect
-(struct client *client, enum client_disconnect_reason reason, const char *text)
+managesieve_client_notify_disconnect(struct client *client,
+				     enum client_disconnect_reason reason,
+				     const char *text)
 {
-	if ( reason == CLIENT_DISCONNECT_SYSTEM_SHUTDOWN ) {
-		client_send_reply_code
-			(client, MANAGESIEVE_CMD_REPLY_BYE, "TRYLATER", text);
+	if (reason == CLIENT_DISCONNECT_SYSTEM_SHUTDOWN) {
+		client_send_reply_code(client, MANAGESIEVE_CMD_REPLY_BYE,
+				       "TRYLATER", text);
 	} else {
-		client_send_reply_code
-			(client, MANAGESIEVE_CMD_REPLY_BYE, NULL, text);
+		client_send_reply_code(client, MANAGESIEVE_CMD_REPLY_BYE,
+				       NULL, text);
 	}
 }
 
-- 
GitLab