Add support for customizing ban reasons in basebans.sp (bug 5762, r=psychonic).
authorMatthias Kollek <CPUCalc@yahoo.de>
Wed Aug 28 13:17:17 2013 -0400 (2013-08-28)
changeset 4089a4e4207da173
parent 4088 40e6b95468ef
child 4090 06731c8a772e
Add support for customizing ban reasons in basebans.sp (bug 5762, r=psychonic).
plugins/basebans.sp
plugins/basebans/ban.sp
translations/basebans.phrases.txt
     1.1 --- a/plugins/basebans.sp	Tue Aug 27 23:32:04 2013 -0400
     1.2 +++ b/plugins/basebans.sp	Wed Aug 28 13:17:17 2013 -0400
     1.3 @@ -52,18 +52,40 @@
     1.4  new g_BanTargetUserId[MAXPLAYERS+1];
     1.5  new g_BanTime[MAXPLAYERS+1];
     1.6  
     1.7 +new g_IsWaitingForChatReason[MAXPLAYERS+1];
     1.8 +new Handle:g_hKvBanReasons;
     1.9 +new String:g_BanReasonsPath[PLATFORM_MAX_PATH];
    1.10 +
    1.11  #include "basebans/ban.sp"
    1.12  
    1.13 +public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
    1.14 +{
    1.15 +	BuildPath(Path_SM, g_BanReasonsPath, sizeof(g_BanReasonsPath), "configs/banreasons.txt");
    1.16 +	if(LoadBanReasons())
    1.17 +	{
    1.18 +		return APLRes_Success;
    1.19 +	}
    1.20 +	else
    1.21 +	{
    1.22 +		Format(error, err_max, "Couldn't load file %s: See log for details.", g_BanReasonsPath);
    1.23 +		return APLRes_Failure;
    1.24 +	}
    1.25 +}
    1.26 +
    1.27  public OnPluginStart()
    1.28  {
    1.29  	LoadTranslations("common.phrases");
    1.30  	LoadTranslations("basebans.phrases");
    1.31 +	LoadTranslations("core.phrases");
    1.32  
    1.33  	RegAdminCmd("sm_ban", Command_Ban, ADMFLAG_BAN, "sm_ban <#userid|name> <minutes|0> [reason]");
    1.34  	RegAdminCmd("sm_unban", Command_Unban, ADMFLAG_UNBAN, "sm_unban <steamid|ip>");
    1.35  	RegAdminCmd("sm_addban", Command_AddBan, ADMFLAG_RCON, "sm_addban <time> <steamid> [reason]");
    1.36  	RegAdminCmd("sm_banip", Command_BanIp, ADMFLAG_BAN, "sm_banip <ip|#userid|name> <time> [reason]");
    1.37  	
    1.38 +	//This to manage custom ban reason messages
    1.39 +	RegConsoleCmd("sm_abortban", Command_AbortBan, "sm_abortban");
    1.40 +	
    1.41  	/* Account for late loading */
    1.42  	new Handle:topmenu;
    1.43  	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
    1.44 @@ -72,6 +94,62 @@
    1.45  	}
    1.46  }
    1.47  
    1.48 +public OnPluginEnd()
    1.49 +{
    1.50 +	//Close kv-handle
    1.51 +	if(g_hKvBanReasons != INVALID_HANDLE)
    1.52 +	{
    1.53 +		CloseHandle(g_hKvBanReasons);
    1.54 +	}
    1.55 +}
    1.56 +
    1.57 +public OnMapStart()
    1.58 +{
    1.59 +	//(Re-)Load BanReasons
    1.60 +	if(g_hKvBanReasons != INVALID_HANDLE)
    1.61 +	{
    1.62 +		CloseHandle(g_hKvBanReasons);
    1.63 +	}
    1.64 +	if(!LoadBanReasons())
    1.65 +	{
    1.66 +		SetFailState("Error trying to load or parse %s: See logfile for details", g_BanReasonsPath);
    1.67 +	}
    1.68 +}
    1.69 +
    1.70 +public OnClientDisconnect(client)
    1.71 +{
    1.72 +	g_IsWaitingForChatReason[client] = false;
    1.73 +}
    1.74 +
    1.75 +public bool:LoadBanReasons()
    1.76 +{
    1.77 +	g_hKvBanReasons = CreateKeyValues("banreasons");
    1.78 +	if(FileToKeyValues(g_hKvBanReasons, g_BanReasonsPath))
    1.79 +	{
    1.80 +		decl String:sectionName[255];
    1.81 +		if(!KvGetSectionName(g_hKvBanReasons, sectionName, sizeof(sectionName)))
    1.82 +		{
    1.83 +			LogMessage("Error in %s: File corrupt or in the wrong format", g_BanReasonsPath);
    1.84 +			return false;
    1.85 +		}
    1.86 +		if(strcmp(sectionName, "banreasons") != 0)
    1.87 +		{
    1.88 +			LogMessage("Error in %s: Couldn't find 'banreasons'", g_BanReasonsPath);
    1.89 +			return false;
    1.90 +		}
    1.91 +		
    1.92 +		//Reset kvHandle
    1.93 +		KvRewind(g_hKvBanReasons);
    1.94 +		
    1.95 +		return true;
    1.96 +	}
    1.97 +	else
    1.98 +	{
    1.99 +		LogMessage("Error in %s: File not found, corrupt or in the wrong format", g_BanReasonsPath);
   1.100 +		return false;
   1.101 +	}
   1.102 +}
   1.103 +
   1.104  public OnAdminMenuReady(Handle:topmenu)
   1.105  {
   1.106  	/* Block us from being called twice */
   1.107 @@ -295,3 +373,39 @@
   1.108  	return Plugin_Handled;
   1.109  }
   1.110  
   1.111 +public Action:Command_AbortBan(client, args)
   1.112 +{
   1.113 +	if(!CheckCommandAccess(client, "sm_ban", ADMFLAG_BAN))
   1.114 +	{
   1.115 +		ReplyToCommand(client, "[SM] %t", "No Access");
   1.116 +		return Plugin_Handled;
   1.117 +	}
   1.118 +	if(g_IsWaitingForChatReason[client])
   1.119 +	{
   1.120 +		g_IsWaitingForChatReason[client] = false;
   1.121 +		ReplyToCommand(client, "[SM] %t", "AbortBan applied successfully");
   1.122 +	}
   1.123 +	else
   1.124 +	{
   1.125 +		ReplyToCommand(client, "[SM] %t", "AbortBan not waiting for custom reason");
   1.126 +	}
   1.127 +	
   1.128 +	return Plugin_Handled;
   1.129 +}
   1.130 +
   1.131 +public Action:OnClientSayCommand(client, const String:command[], const String:sArgs[])
   1.132 +{
   1.133 +	if(g_IsWaitingForChatReason[client])
   1.134 +	{
   1.135 +		g_IsWaitingForChatReason[client] = false;
   1.136 +		
   1.137 +		decl String:message[192];		
   1.138 +		GetCmdArgString(message, sizeof(message));
   1.139 +		StripQuotes(message);
   1.140 +		
   1.141 +		PrepareBan(client, g_BanTarget[client], g_BanTime[client], message);
   1.142 +		
   1.143 +		return Plugin_Handled;
   1.144 +	}
   1.145 +	return Plugin_Continue;
   1.146 +}
   1.147 \ No newline at end of file
     2.1 --- a/plugins/basebans/ban.sp	Tue Aug 27 23:32:04 2013 -0400
     2.2 +++ b/plugins/basebans/ban.sp	Wed Aug 28 13:17:17 2013 -0400
     2.3 @@ -123,22 +123,28 @@
     2.4  	Format(title, sizeof(title), "%T: %N", "Ban reason", client, g_BanTarget[client]);
     2.5  	SetMenuTitle(menu, title);
     2.6  	SetMenuExitBackButton(menu, true);
     2.7 -
     2.8 -	/* :TODO: we should either remove this or make it configurable */
     2.9 -
    2.10 -	AddMenuItem(menu, "Abusive", "Abusive");
    2.11 -	AddMenuItem(menu, "Racism", "Racism");
    2.12 -	AddMenuItem(menu, "General cheating/exploits", "General cheating/exploits");
    2.13 -	AddMenuItem(menu, "Wallhack", "Wallhack");
    2.14 -	AddMenuItem(menu, "Aimbot", "Aimbot");
    2.15 -	AddMenuItem(menu, "Speedhacking", "Speedhacking");
    2.16 -	AddMenuItem(menu, "Mic spamming", "Mic spamming");
    2.17 -	AddMenuItem(menu, "Admin disrespect", "Admin disrespect");
    2.18 -	AddMenuItem(menu, "Camping", "Camping");
    2.19 -	AddMenuItem(menu, "Team killing", "Team killing");
    2.20 -	AddMenuItem(menu, "Unacceptable Spray", "Unacceptable Spray");
    2.21 -	AddMenuItem(menu, "Breaking Server Rules", "Breaking Server Rules");
    2.22 -	AddMenuItem(menu, "Other", "Other");
    2.23 +	
    2.24 +	//Add custom chat reason entry first
    2.25 +	AddMenuItem(menu, "", "Custom reason (type in chat)");
    2.26 +	
    2.27 +	//Loading configurable entries from the kv-file
    2.28 +	decl String:reasonName[100];
    2.29 +	decl String:reasonFull[255];
    2.30 +	
    2.31 +	//Iterate through the kv-file
    2.32 +	KvGotoFirstSubKey(g_hKvBanReasons, false);
    2.33 +	do
    2.34 +	{
    2.35 +		KvGetSectionName(g_hKvBanReasons, reasonName, sizeof(reasonName));
    2.36 +		KvGetString(g_hKvBanReasons, NULL_STRING, reasonFull, sizeof(reasonFull));
    2.37 +		
    2.38 +		//Add entry
    2.39 +		AddMenuItem(menu, reasonFull, reasonName);
    2.40 +		
    2.41 +	} while (KvGotoNextKey(g_hKvBanReasons, false));
    2.42 +	
    2.43 +	//Reset kvHandle
    2.44 +	KvRewind(g_hKvBanReasons);
    2.45  
    2.46  	DisplayMenu(menu, client, MENU_TIME_FOREVER);
    2.47  }
    2.48 @@ -150,6 +156,9 @@
    2.49  							  String:buffer[],
    2.50  							  maxlength)
    2.51  {
    2.52 +	//Reset chat reason first
    2.53 +	g_IsWaitingForChatReason[param] = false;
    2.54 +	
    2.55  	if (action == TopMenuAction_DisplayOption)
    2.56  	{
    2.57  		Format(buffer, maxlength, "%T", "Ban player", param);
    2.58 @@ -175,11 +184,20 @@
    2.59  	}
    2.60  	else if (action == MenuAction_Select)
    2.61  	{
    2.62 -		decl String:info[64];
    2.63 -
    2.64 -		GetMenuItem(menu, param2, info, sizeof(info));
    2.65 -
    2.66 -		PrepareBan(param1, g_BanTarget[param1], g_BanTime[param1], info);
    2.67 +		if(param2 == 0)
    2.68 +		{
    2.69 +			//Chat reason
    2.70 +			g_IsWaitingForChatReason[param1] = true;
    2.71 +			PrintToChat(param1, "[SM] %t", "Custom ban reason explanation", "sm_abortban");
    2.72 +		}
    2.73 +		else
    2.74 +		{
    2.75 +			decl String:info[64];
    2.76 +			
    2.77 +			GetMenuItem(menu, param2, info, sizeof(info));
    2.78 +			
    2.79 +			PrepareBan(param1, g_BanTarget[param1], g_BanTime[param1], info);
    2.80 +		}
    2.81  	}
    2.82  }
    2.83  
     3.1 --- a/translations/basebans.phrases.txt	Tue Aug 27 23:32:04 2013 -0400
     3.2 +++ b/translations/basebans.phrases.txt	Wed Aug 28 13:17:17 2013 -0400
     3.3 @@ -49,4 +49,20 @@
     3.4  	{
     3.5  		"en"			"You cannot ban that IP address."
     3.6  	}
     3.7 +	
     3.8 +	"Custom ban reason explanation"
     3.9 +	{
    3.10 +		"#format"		"{1:s}"
    3.11 +		"en"			"Enter the reason as a chat message. Use {1} to abort this."
    3.12 +	}
    3.13 +	
    3.14 +	"AbortBan applied successfully"
    3.15 +	{
    3.16 +		"en"			"Ban aborted."
    3.17 +	}
    3.18 +	
    3.19 +	"AbortBan not waiting for custom reason"
    3.20 +	{
    3.21 +		"en"			"Not waiting for a ban reason to be typed in, no ban to abort."
    3.22 +	}
    3.23  }