plugins/basebans.sp
author Michael McKoy <ferret99gt@gmail.com>
Sun Jan 27 16:04:36 2008 +0000 (2008-01-27)
changeset 1862 2220462e2237
parent 1736 c40db9557030
child 1947 e73a1e51833b
permissions -rw-r--r--
File saved wrong? Fixed...
     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:time[50];
   213 	new String:authid[50];
   214 
   215 	GetCmdArgString(arg_string, sizeof(arg_string));
   216 
   217 	new len, total_len;
   218 
   219 	/* Get time */
   220 	if ((len = BreakString(arg_string, time, sizeof(time))) == -1)
   221 	{
   222 		ReplyToCommand(client, "t[SM] Usage: sm_addban <time> <steamid> [reason]");
   223 		return Plugin_Handled;
   224 	}	
   225 	total_len += len;
   226 
   227 	/* Get steamid */
   228 	if ((len = BreakString(arg_string[total_len], authid, sizeof(authid))) != -1)
   229 	{
   230 		total_len += len;
   231 	}
   232 	else
   233 	{
   234 		total_len = 0;
   235 		arg_string[0] = '\0';
   236 	}
   237 
   238 	/* Verify steamid */
   239 	if (strncmp(authid, "STEAM_0:", 8) != 0)
   240 	{
   241 		ReplyToCommand(client, "[SM] %t", "Invalid SteamID specified");
   242 		return Plugin_Handled;
   243 	}
   244 
   245 	new minutes = StringToInt(time);
   246 
   247 	LogAction(client, 
   248 			  -1, 
   249 			  "\"%L\" added ban (minutes \"%d\") (id \"%s\") (reason \"%s\")", 
   250 			  client, 
   251 			  minutes, 
   252 			  authid, 
   253 			  arg_string[total_len]);
   254 	BanIdentity(authid, 
   255 				minutes, 
   256 				BANFLAG_AUTHID, 
   257 				arg_string[total_len], 
   258 				"sm_addban", 
   259 				client);
   260 
   261 	ReplyToCommand(client, "[SM] %t", "Ban added");
   262 
   263 	return Plugin_Handled;
   264 }
   265 
   266 public Action:Command_Unban(client, args)
   267 {
   268 	if (args < 1)
   269 	{
   270 		ReplyToCommand(client, "[SM] Usage: sm_unban <steamid|ip>");
   271 		return Plugin_Handled;
   272 	}
   273 
   274 	decl String:arg[50];
   275 	GetCmdArgString(arg, sizeof(arg));
   276 
   277 	ReplaceString(arg, sizeof(arg), "\"", "");	
   278 
   279 	new ban_flags;
   280 	if (strncmp(arg, "STEAM_0:", 8) == 0)
   281 	{
   282 		ban_flags |= BANFLAG_AUTHID;
   283 	}
   284 	else
   285 	{
   286 		ban_flags |= BANFLAG_IP;
   287 	}
   288 
   289 	LogAction(client, -1, "\"%L\" removed ban (filter \"%s\")", client, arg);
   290 	RemoveBan(arg, ban_flags, "sm_unban", client);
   291 
   292 	ReplyToCommand(client, "[SM] %t", "Removed bans matching", arg);
   293 
   294 	return Plugin_Handled;
   295 }
   296