plugins/basebans.sp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Feb 11 09:39:23 2013 -0500 (2013-02-11)
changeset 3783 1a12199cb085
parent 3108 9fc8e7ac629a
child 4089 a4e4207da173
permissions -rw-r--r--
Removed intentional invalid steamid crash in SDK Hooks.
     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 #include "basebans/ban.sp"
    56 
    57 public OnPluginStart()
    58 {
    59 	LoadTranslations("common.phrases");
    60 	LoadTranslations("basebans.phrases");
    61 
    62 	RegAdminCmd("sm_ban", Command_Ban, ADMFLAG_BAN, "sm_ban <#userid|name> <minutes|0> [reason]");
    63 	RegAdminCmd("sm_unban", Command_Unban, ADMFLAG_UNBAN, "sm_unban <steamid|ip>");
    64 	RegAdminCmd("sm_addban", Command_AddBan, ADMFLAG_RCON, "sm_addban <time> <steamid> [reason]");
    65 	RegAdminCmd("sm_banip", Command_BanIp, ADMFLAG_BAN, "sm_banip <ip|#userid|name> <time> [reason]");
    66 	
    67 	/* Account for late loading */
    68 	new Handle:topmenu;
    69 	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
    70 	{
    71 		OnAdminMenuReady(topmenu);
    72 	}
    73 }
    74 
    75 public OnAdminMenuReady(Handle:topmenu)
    76 {
    77 	/* Block us from being called twice */
    78 	if (topmenu == hTopMenu)
    79 	{
    80 		return;
    81 	}
    82 	
    83 	/* Save the Handle */
    84 	hTopMenu = topmenu;
    85 	
    86 	/* Find the "Player Commands" category */
    87 	new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
    88 
    89 	if (player_commands != INVALID_TOPMENUOBJECT)
    90 	{
    91 		AddToTopMenu(hTopMenu,
    92 			"sm_ban",
    93 			TopMenuObject_Item,
    94 			AdminMenu_Ban,
    95 			player_commands,
    96 			"sm_ban",
    97 			ADMFLAG_BAN);
    98 			
    99 	}
   100 }
   101 
   102 public Action:Command_BanIp(client, args)
   103 {
   104 	if (args < 2)
   105 	{
   106 		ReplyToCommand(client, "[SM] Usage: sm_banip <ip|#userid|name> <time> [reason]");
   107 		return Plugin_Handled;
   108 	}
   109 
   110 	decl len, next_len;
   111 	decl String:Arguments[256];
   112 	decl String:arg[50], String:time[20];
   113 	
   114 	GetCmdArgString(Arguments, sizeof(Arguments));
   115 	len = BreakString(Arguments, arg, sizeof(arg));
   116 	
   117 	if ((next_len = BreakString(Arguments[len], time, sizeof(time))) != -1)
   118 	{
   119 		len += next_len;
   120 	}
   121 	else
   122 	{
   123 		len = 0;
   124 		Arguments[0] = '\0';
   125 	}
   126 
   127 	if (StrEqual(arg, "0"))
   128 	{
   129 		ReplyToCommand(client, "[SM] %t", "Cannot ban that IP");
   130 		return Plugin_Handled;
   131 	}
   132 	
   133 	decl String:target_name[MAX_TARGET_LENGTH];
   134 	decl target_list[1], bool:tn_is_ml;
   135 	new found_client = -1;
   136 	
   137 	if (ProcessTargetString(
   138 			arg,
   139 			client, 
   140 			target_list, 
   141 			1, 
   142 			COMMAND_FILTER_CONNECTED|COMMAND_FILTER_NO_MULTI,
   143 			target_name,
   144 			sizeof(target_name),
   145 			tn_is_ml) > 0)
   146 	{
   147 		found_client = target_list[0];
   148 	}
   149 	
   150 	new bool:has_rcon;
   151 	
   152 	if (client == 0 || (client == 1 && !IsDedicatedServer()))
   153 	{
   154 		has_rcon = true;
   155 	}
   156 	else
   157 	{
   158 		new AdminId:id = GetUserAdmin(client);
   159 		has_rcon = (id == INVALID_ADMIN_ID) ? false : GetAdminFlag(id, Admin_RCON);
   160 	}
   161 	
   162 	new hit_client = -1;
   163 	if (found_client != -1
   164 		&& !IsFakeClient(found_client)
   165 		&& (has_rcon || CanUserTarget(client, found_client)))
   166 	{
   167 		GetClientIP(found_client, arg, sizeof(arg));
   168 		hit_client = found_client;
   169 	}
   170 	
   171 	if (hit_client == -1 && !has_rcon)
   172 	{
   173 		ReplyToCommand(client, "[SM] %t", "No Access");
   174 		return Plugin_Handled;
   175 	}
   176 
   177 	new minutes = StringToInt(time);
   178 
   179 	LogAction(client, 
   180 			  hit_client, 
   181 			  "\"%L\" added ban (minutes \"%d\") (ip \"%s\") (reason \"%s\")", 
   182 			  client, 
   183 			  minutes, 
   184 			  arg, 
   185 			  Arguments[len]);
   186 				
   187 	ReplyToCommand(client, "[SM] %t", "Ban added");
   188 	
   189 	BanIdentity(arg, 
   190 				minutes, 
   191 				BANFLAG_IP, 
   192 				Arguments[len], 
   193 				"sm_banip", 
   194 				client);
   195 				
   196 	if (hit_client != -1)
   197 	{
   198 		KickClient(hit_client, "Banned: %s", Arguments[len]);
   199 	}
   200 
   201 	return Plugin_Handled;
   202 }
   203 
   204 public Action:Command_AddBan(client, args)
   205 {
   206 	if (args < 2)
   207 	{
   208 		ReplyToCommand(client, "[SM] Usage: sm_addban <time> <steamid> [reason]");
   209 		return Plugin_Handled;
   210 	}
   211 
   212 	decl String:arg_string[256];
   213 	new String:time[50];
   214 	new String:authid[50];
   215 
   216 	GetCmdArgString(arg_string, sizeof(arg_string));
   217 
   218 	new len, total_len;
   219 
   220 	/* Get time */
   221 	if ((len = BreakString(arg_string, time, sizeof(time))) == -1)
   222 	{
   223 		ReplyToCommand(client, "[SM] Usage: sm_addban <time> <steamid> [reason]");
   224 		return Plugin_Handled;
   225 	}	
   226 	total_len += len;
   227 
   228 	/* Get steamid */
   229 	if ((len = BreakString(arg_string[total_len], authid, sizeof(authid))) != -1)
   230 	{
   231 		total_len += len;
   232 	}
   233 	else
   234 	{
   235 		total_len = 0;
   236 		arg_string[0] = '\0';
   237 	}
   238 
   239 	/* Verify steamid */
   240 	if (strncmp(authid, "STEAM_", 6) != 0 || authid[7] != ':')
   241 	{
   242 		ReplyToCommand(client, "[SM] %t", "Invalid SteamID specified");
   243 		return Plugin_Handled;
   244 	}
   245 
   246 	new minutes = StringToInt(time);
   247 
   248 	LogAction(client, 
   249 			  -1, 
   250 			  "\"%L\" added ban (minutes \"%d\") (id \"%s\") (reason \"%s\")", 
   251 			  client, 
   252 			  minutes, 
   253 			  authid, 
   254 			  arg_string[total_len]);
   255 	BanIdentity(authid, 
   256 				minutes, 
   257 				BANFLAG_AUTHID, 
   258 				arg_string[total_len], 
   259 				"sm_addban", 
   260 				client);
   261 
   262 	ReplyToCommand(client, "[SM] %t", "Ban added");
   263 
   264 	return Plugin_Handled;
   265 }
   266 
   267 public Action:Command_Unban(client, args)
   268 {
   269 	if (args < 1)
   270 	{
   271 		ReplyToCommand(client, "[SM] Usage: sm_unban <steamid|ip>");
   272 		return Plugin_Handled;
   273 	}
   274 
   275 	decl String:arg[50];
   276 	GetCmdArgString(arg, sizeof(arg));
   277 
   278 	ReplaceString(arg, sizeof(arg), "\"", "");	
   279 
   280 	new ban_flags;
   281 	if (strncmp(arg, "STEAM_", 6) == 0 && arg[7] == ':')
   282 	{
   283 		ban_flags |= BANFLAG_AUTHID;
   284 	}
   285 	else
   286 	{
   287 		ban_flags |= BANFLAG_IP;
   288 	}
   289 
   290 	LogAction(client, -1, "\"%L\" removed ban (filter \"%s\")", client, arg);
   291 	RemoveBan(arg, ban_flags, "sm_unban", client);
   292 
   293 	ReplyToCommand(client, "[SM] %t", "Removed bans matching", arg);
   294 
   295 	return Plugin_Handled;
   296 }
   297