From 7c40b0d10cd2090bbf342b2434e62e4ec83d75c0 Mon Sep 17 00:00:00 2001
From: HarpyWar <harpywar@gmail.com>
Date: Sun, 15 Jun 2014 22:04:14 +0400
Subject: [PATCH] Fix automatic unban when user login with Warcraft 3
 https://github.com/HarpyWar/pvpgn/issues/27

Extract duplicated code for output author, time, reason into a function
---
 src/bnetd/account_wrap.cpp | 38 +++++++++++++++++++++++++++++
 src/bnetd/account_wrap.h   |  1 +
 src/bnetd/channel.cpp      | 28 ++-------------------
 src/bnetd/command.cpp      | 50 +++++++++++---------------------------
 src/bnetd/handle_bnet.cpp  | 34 ++++----------------------
 5 files changed, 60 insertions(+), 91 deletions(-)

diff --git a/src/bnetd/account_wrap.cpp b/src/bnetd/account_wrap.cpp
index 46bb0fa..60f4dc2 100644
--- a/src/bnetd/account_wrap.cpp
+++ b/src/bnetd/account_wrap.cpp
@@ -39,6 +39,8 @@
 #include "clan.h"
 #include "anongame_infos.h"
 #include "team.h"
+#include "server.h"
+#include "compat/snprintf.h"
 #include "common/setup_after.h"
 
 namespace pvpgn
@@ -450,6 +452,42 @@ namespace pvpgn
 			return account_set_strattr(account, "BNET\\auth\\muteby", val);
 		}
 
+
+		/* Return text with account lock */
+		extern char * account_get_locktext(t_account * account, bool with_author)
+		{
+			char msgtemp[MAX_MESSAGE_LEN], msgtemp2[MAX_MESSAGE_LEN];
+			snprintf(msgtemp, sizeof(msgtemp), "");
+
+			// append author of ban
+			if (with_author)
+			{
+				if (char const * author = account_get_auth_lockby(account))
+				if (author && author[0] != '\0')
+				{
+					snprintf(msgtemp2, sizeof(msgtemp2), " by %s", author);
+					std::strcat(msgtemp, msgtemp2);
+				}
+			}
+
+			// append remaining time
+			if (unsigned int locktime = account_get_auth_locktime(account))
+				snprintf(msgtemp2, sizeof(msgtemp2), " for %.48s", seconds_to_timestr(locktime - now));
+			else
+				snprintf(msgtemp2, sizeof(msgtemp2), " permanently");
+			std::strcat(msgtemp, msgtemp2);
+
+			// append reason
+			char const * reason = account_get_auth_lockreason(account);
+			if (reason && reason[0] != '\0')
+			{
+				snprintf(msgtemp2, sizeof(msgtemp2), " with a reason \"%s\"", reason);
+				std::strcat(msgtemp, msgtemp2);
+			}
+			return msgtemp;
+		}
+
+
 		/****************************************************************/
 
 
diff --git a/src/bnetd/account_wrap.h b/src/bnetd/account_wrap.h
index 63d7671..4d12245 100644
--- a/src/bnetd/account_wrap.h
+++ b/src/bnetd/account_wrap.h
@@ -93,6 +93,7 @@ namespace pvpgn
 		extern int account_set_auth_mutetime(t_account * account, unsigned int val);
 		extern int account_set_auth_mutereason(t_account * account, char const * val);
 		extern int account_set_auth_muteby(t_account * account, char const * val);
+		extern char * account_get_locktext(t_account * account, bool with_author = true);
 
 		/* profile */
 		extern char const * account_get_sex(t_account * account); /* the profile attributes are updated directly in bnetd.c */
diff --git a/src/bnetd/channel.cpp b/src/bnetd/channel.cpp
index 39cc8ba..46690a1 100644
--- a/src/bnetd/channel.cpp
+++ b/src/bnetd/channel.cpp
@@ -681,36 +681,12 @@ namespace pvpgn
 			if (type != message_type_join && type != message_type_part)
 				return;
 
-			// if user muted
+			// if user is muted
 			if (account_get_auth_mute(acc) == 1)
 			{
 				char msgtemp[MAX_MESSAGE_LEN], msgtemp2[MAX_MESSAGE_LEN];
 
-				snprintf(msgtemp, sizeof(msgtemp), "You can't talk on the channel. Your account has been muted");
-
-				// append author of ban
-				char const * author = account_get_auth_muteby(acc);
-				if (author && author[0] != '\0')
-				{
-					snprintf(msgtemp2, sizeof(msgtemp2), " by %s", author);
-					std::strcat(msgtemp, msgtemp2);
-				}
-
-				// append remaining time
-				if (unsigned int locktime = account_get_auth_mutetime(acc))
-					snprintf(msgtemp2, sizeof(msgtemp2), " for %.48s", seconds_to_timestr(locktime - std::time(NULL)));
-				else
-					snprintf(msgtemp2, sizeof(msgtemp2), " permanently");
-				std::strcat(msgtemp, msgtemp2);
-
-				// append reason
-				char const * reason = account_get_auth_mutereason(acc);
-				if (reason && reason[0] != '\0')
-				{
-					snprintf(msgtemp2, sizeof(msgtemp2), " with a reason \"%s\"", reason);
-					std::strcat(msgtemp, msgtemp2);
-				}
-
+				snprintf(msgtemp, sizeof(msgtemp), "You can't talk on the channel. Your account has been muted%s", account_get_locktext(acc, true));
 				message_send_text(me, message_type_error, me, msgtemp);
 				return;
 			}
diff --git a/src/bnetd/command.cpp b/src/bnetd/command.cpp
index 3bffe54..e3d3b61 100644
--- a/src/bnetd/command.cpp
+++ b/src/bnetd/command.cpp
@@ -4298,7 +4298,6 @@ namespace pvpgn
 			t_account *    account;
 			char const * username, *reason = "", *hours = "24"; // default time 24 hours
 			unsigned int sectime;
-			char msgtemp3[MAX_MESSAGE_LEN];
 
 			std::vector<std::string> args = split_command(text, 3);
 			if (args[1].empty())
@@ -4325,28 +4324,15 @@ namespace pvpgn
 			account_set_auth_lockby(account, conn_get_username(c));
 
 
-			// append remaining time
-			if (sectime == 0)
-				snprintf(msgtemp3, sizeof(msgtemp3), " permanently");
-			else
-				snprintf(msgtemp3, sizeof(msgtemp3), " for %.48s", seconds_to_timestr(sectime - now));
-
-			// append reason
-			if (reason[0] != '\0')
-			{
-				snprintf(msgtemp2, sizeof(msgtemp2), " with a reason \"%s\"", reason);
-				std::strcat(msgtemp3, msgtemp2);
-			}
-
 			// send message to author
-			snprintf(msgtemp, sizeof(msgtemp), "Account %s is now locked%s", account_get_name(account), msgtemp3);
+			snprintf(msgtemp, sizeof(msgtemp), "Account %s is now locked%s", account_get_name(account), account_get_locktext(account, false));
 			message_send_text(c, message_type_error, c, msgtemp);
 
 			// send message to locked user
 			if ((user = connlist_find_connection_by_accountname(username)))
 			{
-				snprintf(msgtemp, sizeof(msgtemp), "Your account has just been locked by %s%s", conn_get_username(c), msgtemp3);
-				message_send_text(user, message_type_info, user, msgtemp);
+				snprintf(msgtemp, sizeof(msgtemp), "Your account has just been locked%s", account_get_locktext(account, true));
+				message_send_text(user, message_type_error, user, msgtemp);
 			}
 
 			return 0;
@@ -4372,7 +4358,10 @@ namespace pvpgn
 			}
 
 			if ((user = connlist_find_connection_by_accountname(text)))
-				message_send_text(user, message_type_info, user, "Your account has just been unlocked by an admin.");
+			{
+				snprintf(msgtemp, sizeof(msgtemp), "Your account has just been unlocked by %s", conn_get_username(c));
+				message_send_text(user, message_type_info, user, msgtemp);
+			}
 
 			account_set_auth_lock(account, 0);
 			message_send_text(c, message_type_error, c, "That user's account is now unlocked.");
@@ -4386,7 +4375,6 @@ namespace pvpgn
 			t_account *    account;
 			char const * username, *reason = "", *hours = "1"; // default time 1 hour
 			unsigned int sectime;
-			char msgtemp3[MAX_MESSAGE_LEN];
 
 			std::vector<std::string> args = split_command(text, 3);
 			if (args[1].empty())
@@ -4413,28 +4401,15 @@ namespace pvpgn
 			account_set_auth_mutereason(account, reason);
 			account_set_auth_muteby(account, conn_get_username(c));
 
-			// append remaining time
-			if (sectime == 0)
-				snprintf(msgtemp3, sizeof(msgtemp3), " permanently");
-			else
-				snprintf(msgtemp3, sizeof(msgtemp3), " for %.48s", seconds_to_timestr(sectime - now));
-
-			// append reason
-			if (reason[0] != '\0')
-			{
-				snprintf(msgtemp2, sizeof(msgtemp2), " with a reason \"%s\"", reason);
-				std::strcat(msgtemp3, msgtemp2);
-			}
-
 			// send message to author
-			snprintf(msgtemp, sizeof(msgtemp), "Account %s is now muted%s", account_get_name(account), msgtemp3);
+			snprintf(msgtemp, sizeof(msgtemp), "Account %s is now muted%s", account_get_name(account), account_get_locktext(account, false));
 			message_send_text(c, message_type_error, c, msgtemp);
 
 			// send message to muted user
 			if ((user = connlist_find_connection_by_accountname(username)))
 			{
-				snprintf(msgtemp, sizeof(msgtemp), "Your account has just been muted by %s%s", conn_get_username(c), msgtemp3);
-				message_send_text(user, message_type_info, user, msgtemp);
+				snprintf(msgtemp, sizeof(msgtemp), "Your account has just been muted%s", account_get_locktext(account, true));
+				message_send_text(user, message_type_error, user, msgtemp);
 			}
 
 			return 0;
@@ -4460,7 +4435,10 @@ namespace pvpgn
 			}
 
 			if ((user = connlist_find_connection_by_accountname(text)))
-				message_send_text(user, message_type_info, user, "Your account has just been unmuted by an admin.");
+			{
+				snprintf(msgtemp, sizeof(msgtemp), "Your account has just been unmuted by %s", conn_get_username(c));
+				message_send_text(user, message_type_info, user, msgtemp);
+			}
 
 			account_set_auth_mute(account, 0);
 			message_send_text(c, message_type_error, c, "That user's account is now unmuted.");
diff --git a/src/bnetd/handle_bnet.cpp b/src/bnetd/handle_bnet.cpp
index 1609416..1b712c5 100644
--- a/src/bnetd/handle_bnet.cpp
+++ b/src/bnetd/handle_bnet.cpp
@@ -1687,34 +1687,9 @@ namespace pvpgn
 					eventlog(eventlog_level_info, __FUNCTION__, "[%d] login for \"%s\" refused (this account is locked)", conn_get_socket(c), username);
 					if (supports_locked_reply)
 					{
-						char msgtemp[MAX_MESSAGE_LEN], msgtemp2[MAX_MESSAGE_LEN];
-						snprintf(msgtemp, sizeof(msgtemp), "This account has been locked");
-
-						// append author of ban
-						if (char const * author = account_get_auth_lockby(account))
-						if (author && author[0] != '\0')
-						{
-							snprintf(msgtemp2, sizeof(msgtemp2), " by %s", author);
-							std::strcat(msgtemp, msgtemp2);
-						}
-
-						// append remaining time
-						if (unsigned int locktime = account_get_auth_locktime(account))
-							snprintf(msgtemp2, sizeof(msgtemp2), " for %.48s", seconds_to_timestr(locktime - now));
-						else
-							snprintf(msgtemp2, sizeof(msgtemp2), " permanently");
-						std::strcat(msgtemp, msgtemp2);
-
-						// append reason
-						char const * reason = account_get_auth_lockreason(account);
-						if (reason && reason[0] != '\0')
-						{
-							snprintf(msgtemp2, sizeof(msgtemp2), " with a reason \"%s\"", reason);
-							std::strcat(msgtemp, msgtemp2);
-						}
-
-
 						bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY2_MESSAGE_LOCKED);
+						char msgtemp[MAX_MESSAGE_LEN];
+						snprintf(msgtemp, sizeof(msgtemp), "This account has been locked%s", account_get_locktext(account, true));
 						packet_append_string(rpacket, msgtemp);
 					}
 					else {
@@ -2137,7 +2112,9 @@ namespace pvpgn
 				else if (account_get_auth_lock(account) == 1) {	/* default to false */
 					eventlog(eventlog_level_info, __FUNCTION__, "[%d] login for \"%s\" refused (this account is locked)", conn_get_socket(c), username);
 					bn_int_set(&rpacket->u.server_logonproofreply.response, SERVER_LOGONPROOFREPLY_RESPONSE_CUSTOM);
-					packet_append_string(rpacket, "This account has been locked.");
+					char msgtemp[MAX_MESSAGE_LEN];
+					snprintf(msgtemp, sizeof(msgtemp), "This account has been locked%s", account_get_locktext(account, true));
+					packet_append_string(rpacket, msgtemp);
 				}
 				else {
 					t_hash serverhash;
@@ -5329,7 +5306,6 @@ namespace pvpgn
 			eventlog(eventlog_level_info, __FUNCTION__, "[%d] get password for account \"%s\" to email \"%s\"", conn_get_socket(c), account_get_name(account), email);
 			return 0;
 		}
-
 	}
 
 }