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