plugins/basebans.sp
author Peace-Maker <januk@freenet.de>
Sat May 10 05:42:33 2014 -0700 (2014-05-10)
changeset 4354 7011587e0021
parent 4262 c2836eb6c34a
permissions -rw-r--r--
Remove LogMessageEx definition from logging.inc (bug 5897, r=kyles).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basic Commands Plugin
     5  * Implements basic admin commands.
     6  *
     7  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    10  * This program is free software; you can redistribute it and/or modify it under
    11  * the terms of the GNU General Public License, version 3.0, as published by the
    12  * Free Software Foundation.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT
    15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    17  * details.
    18  *
    19  * You should have received a copy of the GNU General Public License along with
    20  * this program.  If not, see <http://www.gnu.org/licenses/>.
    21  *
    22  * As a special exception, AlliedModders LLC gives you permission to link the
    23  * code of this program (as well as its derivative works) to "Half-Life 2," the
    24  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    25  * by the Valve Corporation.  You must obey the GNU General Public License in
    26  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    27  * this exception to all derivative works.  AlliedModders LLC defines further
    28  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    29  * or <http://www.sourcemod.net/license.php>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37 #undef REQUIRE_PLUGIN
    38 #include <adminmenu>
    39 
    40 public Plugin:myinfo =
    41 {
    42 	name = "Basic Ban Commands",
    43 	author = "AlliedModders LLC",
    44 	description = "Basic Banning Commands",
    45 	version = SOURCEMOD_VERSION,
    46 	url = "http://www.sourcemod.net/"
    47 };
    48 
    49 new Handle:hTopMenu = INVALID_HANDLE;
    50 
    51 new g_BanTarget[MAXPLAYERS+1];
    52 new g_BanTargetUserId[MAXPLAYERS+1];
    53 new g_BanTime[MAXPLAYERS+1];
    54 
    55 new g_IsWaitingForChatReason[MAXPLAYERS+1];
    56 new Handle:g_hKvBanReasons;
    57 new String:g_BanReasonsPath[PLATFORM_MAX_PATH];
    58 
    59 #include "basebans/ban.sp"
    60 
    61 public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
    62 {
    63 	BuildPath(Path_SM, g_BanReasonsPath, sizeof(g_BanReasonsPath), "configs/banreasons.txt");
    64 	if(LoadBanReasons())
    65 	{
    66 		return APLRes_Success;
    67 	}
    68 	else
    69 	{
    70 		Format(error, err_max, "Couldn't load file %s: See log for details.", g_BanReasonsPath);
    71 		return APLRes_Failure;
    72 	}
    73 }
    74 
    75 public OnPluginStart()
    76 {
    77 	LoadTranslations("common.phrases");
    78 	LoadTranslations("basebans.phrases");
    79 	LoadTranslations("core.phrases");
    80 
    81 	RegAdminCmd("sm_ban", Command_Ban, ADMFLAG_BAN, "sm_ban <#userid|name> <minutes|0> [reason]");
    82 	RegAdminCmd("sm_unban", Command_Unban, ADMFLAG_UNBAN, "sm_unban <steamid|ip>");
    83 	RegAdminCmd("sm_addban", Command_AddBan, ADMFLAG_RCON, "sm_addban <time> <steamid> [reason]");
    84 	RegAdminCmd("sm_banip", Command_BanIp, ADMFLAG_BAN, "sm_banip <ip|#userid|name> <time> [reason]");
    85 	
    86 	//This to manage custom ban reason messages
    87 	RegConsoleCmd("sm_abortban", Command_AbortBan, "sm_abortban");
    88 	
    89 	/* Account for late loading */
    90 	new Handle:topmenu;
    91 	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
    92 	{
    93 		OnAdminMenuReady(topmenu);
    94 	}
    95 }
    96 
    97 public OnPluginEnd()
    98 {
    99 	//Close kv-handle
   100 	if(g_hKvBanReasons != INVALID_HANDLE)
   101 	{
   102 		CloseHandle(g_hKvBanReasons);
   103 	}
   104 }
   105 
   106 public OnMapStart()
   107 {
   108 	//(Re-)Load BanReasons
   109 	if(g_hKvBanReasons != INVALID_HANDLE)
   110 	{
   111 		CloseHandle(g_hKvBanReasons);
   112 	}
   113 	if(!LoadBanReasons())
   114 	{
   115 		SetFailState("Error trying to load or parse %s: See logfile for details", g_BanReasonsPath);
   116 	}
   117 }
   118 
   119 public OnClientDisconnect(client)
   120 {
   121 	g_IsWaitingForChatReason[client] = false;
   122 }
   123 
   124 public bool:LoadBanReasons()
   125 {
   126 	g_hKvBanReasons = CreateKeyValues("banreasons");
   127 	if(FileToKeyValues(g_hKvBanReasons, g_BanReasonsPath))
   128 	{
   129 		decl String:sectionName[255];
   130 		if(!KvGetSectionName(g_hKvBanReasons, sectionName, sizeof(sectionName)))
   131 		{
   132 			LogMessage("Error in %s: File corrupt or in the wrong format", g_BanReasonsPath);
   133 			return false;
   134 		}
   135 		if(strcmp(sectionName, "banreasons") != 0)
   136 		{
   137 			LogMessage("Error in %s: Couldn't find 'banreasons'", g_BanReasonsPath);
   138 			return false;
   139 		}
   140 		
   141 		//Reset kvHandle
   142 		KvRewind(g_hKvBanReasons);
   143 		
   144 		return true;
   145 	}
   146 	else
   147 	{
   148 		LogMessage("Error in %s: File not found, corrupt or in the wrong format", g_BanReasonsPath);
   149 		return false;
   150 	}
   151 }
   152 
   153 public OnAdminMenuReady(Handle:topmenu)
   154 {
   155 	/* Block us from being called twice */
   156 	if (topmenu == hTopMenu)
   157 	{
   158 		return;
   159 	}
   160 	
   161 	/* Save the Handle */
   162 	hTopMenu = topmenu;
   163 	
   164 	/* Find the "Player Commands" category */
   165 	new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
   166 
   167 	if (player_commands != INVALID_TOPMENUOBJECT)
   168 	{
   169 		AddToTopMenu(hTopMenu,
   170 			"sm_ban",
   171 			TopMenuObject_Item,
   172 			AdminMenu_Ban,
   173 			player_commands,
   174 			"sm_ban",
   175 			ADMFLAG_BAN);
   176 			
   177 	}
   178 }
   179 
   180 public Action:Command_BanIp(client, args)
   181 {
   182 	if (args < 2)
   183 	{
   184 		ReplyToCommand(client, "[SM] Usage: sm_banip <ip|#userid|name> <time> [reason]");
   185 		return Plugin_Handled;
   186 	}
   187 
   188 	decl len, next_len;
   189 	decl String:Arguments[256];
   190 	decl String:arg[50], String:time[20];
   191 	
   192 	GetCmdArgString(Arguments, sizeof(Arguments));
   193 	len = BreakString(Arguments, arg, sizeof(arg));
   194 	
   195 	if ((next_len = BreakString(Arguments[len], time, sizeof(time))) != -1)
   196 	{
   197 		len += next_len;
   198 	}
   199 	else
   200 	{
   201 		len = 0;
   202 		Arguments[0] = '\0';
   203 	}
   204 
   205 	if (StrEqual(arg, "0"))
   206 	{
   207 		ReplyToCommand(client, "[SM] %t", "Cannot ban that IP");
   208 		return Plugin_Handled;
   209 	}
   210 	
   211 	decl String:target_name[MAX_TARGET_LENGTH];
   212 	decl target_list[1], bool:tn_is_ml;
   213 	new found_client = -1;
   214 	
   215 	if (ProcessTargetString(
   216 			arg,
   217 			client, 
   218 			target_list, 
   219 			1, 
   220 			COMMAND_FILTER_CONNECTED|COMMAND_FILTER_NO_MULTI,
   221 			target_name,
   222 			sizeof(target_name),
   223 			tn_is_ml) > 0)
   224 	{
   225 		found_client = target_list[0];
   226 	}
   227 	
   228 	new bool:has_rcon;
   229 	
   230 	if (client == 0 || (client == 1 && !IsDedicatedServer()))
   231 	{
   232 		has_rcon = true;
   233 	}
   234 	else
   235 	{
   236 		new AdminId:id = GetUserAdmin(client);
   237 		has_rcon = (id == INVALID_ADMIN_ID) ? false : GetAdminFlag(id, Admin_RCON);
   238 	}
   239 	
   240 	new hit_client = -1;
   241 	if (found_client != -1
   242 		&& !IsFakeClient(found_client)
   243 		&& (has_rcon || CanUserTarget(client, found_client)))
   244 	{
   245 		GetClientIP(found_client, arg, sizeof(arg));
   246 		hit_client = found_client;
   247 	}
   248 	
   249 	if (hit_client == -1 && !has_rcon)
   250 	{
   251 		ReplyToCommand(client, "[SM] %t", "No Access");
   252 		return Plugin_Handled;
   253 	}
   254 
   255 	new minutes = StringToInt(time);
   256 
   257 	LogAction(client, 
   258 			  hit_client, 
   259 			  "\"%L\" added ban (minutes \"%d\") (ip \"%s\") (reason \"%s\")", 
   260 			  client, 
   261 			  minutes, 
   262 			  arg, 
   263 			  Arguments[len]);
   264 				
   265 	ReplyToCommand(client, "[SM] %t", "Ban added");
   266 	
   267 	BanIdentity(arg, 
   268 				minutes, 
   269 				BANFLAG_IP, 
   270 				Arguments[len], 
   271 				"sm_banip", 
   272 				client);
   273 				
   274 	if (hit_client != -1)
   275 	{
   276 		KickClient(hit_client, "Banned: %s", Arguments[len]);
   277 	}
   278 
   279 	return Plugin_Handled;
   280 }
   281 
   282 public Action:Command_AddBan(client, args)
   283 {
   284 	if (args < 2)
   285 	{
   286 		ReplyToCommand(client, "[SM] Usage: sm_addban <time> <steamid> [reason]");
   287 		return Plugin_Handled;
   288 	}
   289 
   290 	decl String:arg_string[256];
   291 	new String:time[50];
   292 	new String:authid[50];
   293 
   294 	GetCmdArgString(arg_string, sizeof(arg_string));
   295 
   296 	new len, total_len;
   297 
   298 	/* Get time */
   299 	if ((len = BreakString(arg_string, time, sizeof(time))) == -1)
   300 	{
   301 		ReplyToCommand(client, "[SM] Usage: sm_addban <time> <steamid> [reason]");
   302 		return Plugin_Handled;
   303 	}	
   304 	total_len += len;
   305 
   306 	/* Get steamid */
   307 	if ((len = BreakString(arg_string[total_len], authid, sizeof(authid))) != -1)
   308 	{
   309 		total_len += len;
   310 	}
   311 	else
   312 	{
   313 		total_len = 0;
   314 		arg_string[0] = '\0';
   315 	}
   316 
   317 	/* Verify steamid */
   318 	if (strncmp(authid, "STEAM_", 6) != 0 || authid[7] != ':')
   319 	{
   320 		ReplyToCommand(client, "[SM] %t", "Invalid SteamID specified");
   321 		return Plugin_Handled;
   322 	}
   323 
   324 	new minutes = StringToInt(time);
   325 
   326 	LogAction(client, 
   327 			  -1, 
   328 			  "\"%L\" added ban (minutes \"%d\") (id \"%s\") (reason \"%s\")", 
   329 			  client, 
   330 			  minutes, 
   331 			  authid, 
   332 			  arg_string[total_len]);
   333 	BanIdentity(authid, 
   334 				minutes, 
   335 				BANFLAG_AUTHID, 
   336 				arg_string[total_len], 
   337 				"sm_addban", 
   338 				client);
   339 
   340 	ReplyToCommand(client, "[SM] %t", "Ban added");
   341 
   342 	return Plugin_Handled;
   343 }
   344 
   345 public Action:Command_Unban(client, args)
   346 {
   347 	if (args < 1)
   348 	{
   349 		ReplyToCommand(client, "[SM] Usage: sm_unban <steamid|ip>");
   350 		return Plugin_Handled;
   351 	}
   352 
   353 	decl String:arg[50];
   354 	GetCmdArgString(arg, sizeof(arg));
   355 
   356 	ReplaceString(arg, sizeof(arg), "\"", "");	
   357 
   358 	new ban_flags;
   359 	if (strncmp(arg, "STEAM_", 6) == 0 && arg[7] == ':')
   360 	{
   361 		ban_flags |= BANFLAG_AUTHID;
   362 	}
   363 	else
   364 	{
   365 		ban_flags |= BANFLAG_IP;
   366 	}
   367 
   368 	LogAction(client, -1, "\"%L\" removed ban (filter \"%s\")", client, arg);
   369 	RemoveBan(arg, ban_flags, "sm_unban", client);
   370 
   371 	ReplyToCommand(client, "[SM] %t", "Removed bans matching", arg);
   372 
   373 	return Plugin_Handled;
   374 }
   375 
   376 public Action:Command_AbortBan(client, args)
   377 {
   378 	if(!CheckCommandAccess(client, "sm_ban", ADMFLAG_BAN))
   379 	{
   380 		ReplyToCommand(client, "[SM] %t", "No Access");
   381 		return Plugin_Handled;
   382 	}
   383 	if(g_IsWaitingForChatReason[client])
   384 	{
   385 		g_IsWaitingForChatReason[client] = false;
   386 		ReplyToCommand(client, "[SM] %t", "AbortBan applied successfully");
   387 	}
   388 	else
   389 	{
   390 		ReplyToCommand(client, "[SM] %t", "AbortBan not waiting for custom reason");
   391 	}
   392 	
   393 	return Plugin_Handled;
   394 }
   395 
   396 public Action:OnClientSayCommand(client, const String:command[], const String:sArgs[])
   397 {
   398 	if(g_IsWaitingForChatReason[client])
   399 	{
   400 		g_IsWaitingForChatReason[client] = false;
   401 		PrepareBan(client, g_BanTarget[client], g_BanTime[client], sArgs);
   402 		return Plugin_Stop;
   403 	}
   404 
   405 	return Plugin_Continue;
   406 }