plugins/basebans.sp
author David Anderson <dvander@alliedmods.net>
Thu May 29 04:47:42 2008 +0000 (2008-05-29)
changeset 2092 204db878bcfa
parent 2030 e6281a0ff855
child 2221 98a80ac8217e
permissions -rw-r--r--
(merging from branch) fixed amb1583 - MySQL string fetch from prepared queries returned corrupted data.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Basic Commands Plugin
ds@1952
     5
 * Implements basic admin commands.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#pragma semicolon 1
ds@1952
    35
ds@1952
    36
#include <sourcemod>
ds@1952
    37
#undef REQUIRE_PLUGIN
ds@1952
    38
#include <adminmenu>
ds@1952
    39
ds@1952
    40
public Plugin:myinfo =
ds@1952
    41
{
ds@1952
    42
	name = "Basic Ban Commands",
ds@1952
    43
	author = "AlliedModders LLC",
ds@1952
    44
	description = "Basic Banning Commands",
ds@1952
    45
	version = SOURCEMOD_VERSION,
ds@1952
    46
	url = "http://www.sourcemod.net/"
ds@1952
    47
};
ds@1952
    48
ds@1952
    49
new Handle:hTopMenu = INVALID_HANDLE;
ds@1952
    50
ds@1952
    51
new g_BanTarget[MAXPLAYERS+1];
ds@1952
    52
new g_BanTime[MAXPLAYERS+1];
ds@1952
    53
ds@1952
    54
#include "basebans/ban.sp"
ds@1952
    55
ds@1952
    56
public OnPluginStart()
ds@1952
    57
{
ds@1952
    58
	LoadTranslations("common.phrases");
ds@1952
    59
	LoadTranslations("basebans.phrases");
ds@1952
    60
ds@1952
    61
	RegAdminCmd("sm_ban", Command_Ban, ADMFLAG_BAN, "sm_ban <#userid|name> <minutes|0> [reason]");
ds@1952
    62
	RegAdminCmd("sm_unban", Command_Unban, ADMFLAG_UNBAN, "sm_unban <steamid>");
ds@1952
    63
	RegAdminCmd("sm_addban", Command_AddBan, ADMFLAG_RCON, "sm_addban <time> <steamid> [reason]");
dvander@2030
    64
	RegAdminCmd("sm_banip", Command_BanIp, ADMFLAG_BAN, "sm_banip <ip|#userid|name> <time> [reason]");
ds@1952
    65
	
ds@1952
    66
	/* Account for late loading */
ds@1952
    67
	new Handle:topmenu;
ds@1952
    68
	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
ds@1952
    69
	{
ds@1952
    70
		OnAdminMenuReady(topmenu);
ds@1952
    71
	}
ds@1952
    72
}
ds@1952
    73
ds@1952
    74
public OnAdminMenuReady(Handle:topmenu)
ds@1952
    75
{
ds@1952
    76
	/* Block us from being called twice */
ds@1952
    77
	if (topmenu == hTopMenu)
ds@1952
    78
	{
ds@1952
    79
		return;
ds@1952
    80
	}
ds@1952
    81
	
ds@1952
    82
	/* Save the Handle */
ds@1952
    83
	hTopMenu = topmenu;
ds@1952
    84
	
ds@1952
    85
	/* Find the "Player Commands" category */
ds@1952
    86
	new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
ds@1952
    87
ds@1952
    88
	if (player_commands != INVALID_TOPMENUOBJECT)
ds@1952
    89
	{
ds@1952
    90
		AddToTopMenu(hTopMenu,
ds@1952
    91
			"sm_ban",
ds@1952
    92
			TopMenuObject_Item,
ds@1952
    93
			AdminMenu_Ban,
ds@1952
    94
			player_commands,
ds@1952
    95
			"sm_ban",
ds@1952
    96
			ADMFLAG_BAN);
ds@1952
    97
			
ds@1952
    98
	}
ds@1952
    99
}
ds@1952
   100
ds@1952
   101
public Action:Command_BanIp(client, args)
ds@1952
   102
{
ds@1952
   103
	if (args < 2)
ds@1952
   104
	{
dvander@2030
   105
		ReplyToCommand(client, "[SM] Usage: sm_banip <ip|#userid|name> <time> [reason]");
ds@1952
   106
		return Plugin_Handled;
ds@1952
   107
	}
ds@1952
   108
ds@1952
   109
	decl len, next_len;
ds@1952
   110
	decl String:Arguments[256];
ds@1952
   111
	decl String:arg[50], String:time[20];
ds@1952
   112
	
ds@1952
   113
	GetCmdArgString(Arguments, sizeof(Arguments));
ds@1952
   114
	len = BreakString(Arguments, arg, sizeof(arg));
ds@1952
   115
	
ds@1952
   116
	if ((next_len = BreakString(Arguments[len], time, sizeof(time))) != -1)
ds@1952
   117
	{
ds@1952
   118
		len += next_len;
ds@1952
   119
	}
ds@1952
   120
	else
ds@1952
   121
	{
ds@1952
   122
		len = 0;
ds@1952
   123
		Arguments[0] = '\0';
ds@1952
   124
	}
ds@1952
   125
ds@1952
   126
	if (StrEqual(arg, "0"))
ds@1952
   127
	{
ds@1952
   128
		ReplyToCommand(client, "[SM] %t", "Cannot ban that IP");
ds@1952
   129
		return Plugin_Handled;
ds@1952
   130
	}
ds@1952
   131
	
ds@1952
   132
	decl String:target_name[MAX_TARGET_LENGTH];
ds@1952
   133
	decl target_list[1], bool:tn_is_ml;
ds@1952
   134
	new found_client = -1;
ds@1952
   135
	
ds@1952
   136
	if (ProcessTargetString(
ds@1952
   137
			arg,
ds@1952
   138
			client, 
ds@1952
   139
			target_list, 
ds@1952
   140
			1, 
ds@1952
   141
			COMMAND_FILTER_CONNECTED|COMMAND_FILTER_NO_MULTI,
ds@1952
   142
			target_name,
ds@1952
   143
			sizeof(target_name),
ds@1952
   144
			tn_is_ml) > 0)
ds@1952
   145
	{
ds@1952
   146
		found_client = target_list[0];
ds@1952
   147
	}
ds@1952
   148
	
ds@1952
   149
	new bool:has_rcon;
ds@1952
   150
	
ds@1952
   151
	if (client == 0 || (client == 1 && !IsDedicatedServer()))
ds@1952
   152
	{
ds@1952
   153
		has_rcon = true;
ds@1952
   154
	}
ds@1952
   155
	else
ds@1952
   156
	{
ds@1952
   157
		new AdminId:id = GetUserAdmin(client);
ds@1952
   158
		has_rcon = (id == INVALID_ADMIN_ID) ? false : GetAdminFlag(id, Admin_RCON);
ds@1952
   159
	}
ds@1952
   160
	
ds@1952
   161
	new hit_client = -1;
ds@1952
   162
	if (found_client != -1
ds@1952
   163
		&& !IsFakeClient(found_client)
ds@1952
   164
		&& (has_rcon || CanUserTarget(client, found_client)))
ds@1952
   165
	{
ds@1952
   166
		GetClientIP(found_client, arg, sizeof(arg));
ds@1952
   167
		hit_client = found_client;
ds@1952
   168
	}
ds@1952
   169
	
ds@1952
   170
	if (hit_client == -1 && !has_rcon)
ds@1952
   171
	{
ds@1952
   172
		ReplyToCommand(client, "[SM] %t", "No Access");
ds@1952
   173
		return Plugin_Handled;
ds@1952
   174
	}
ds@1952
   175
ds@1952
   176
	new minutes = StringToInt(time);
ds@1952
   177
ds@1952
   178
	LogAction(client, 
ds@1952
   179
			  hit_client, 
ds@1952
   180
			  "\"%L\" added ban (minutes \"%d\") (ip \"%s\") (reason \"%s\")", 
ds@1952
   181
			  client, 
ds@1952
   182
			  minutes, 
ds@1952
   183
			  arg, 
ds@1952
   184
			  Arguments[len]);
ds@1952
   185
				
ds@1952
   186
	ReplyToCommand(client, "[SM] %t", "Ban added");
ds@1952
   187
	
ds@1952
   188
	BanIdentity(arg, 
ds@1952
   189
				minutes, 
ds@1952
   190
				BANFLAG_IP, 
ds@1952
   191
				Arguments[len], 
ds@1952
   192
				"sm_banip", 
ds@1952
   193
				client);
ds@1952
   194
				
ds@1952
   195
	if (hit_client != -1)
ds@1952
   196
	{
ds@1952
   197
		KickClient(hit_client, "Banned: %s", Arguments[len]);
ds@1952
   198
	}
ds@1952
   199
ds@1952
   200
	return Plugin_Handled;
ds@1952
   201
}
ds@1952
   202
ds@1952
   203
public Action:Command_AddBan(client, args)
ds@1952
   204
{
ds@1952
   205
	if (args < 2)
ds@1952
   206
	{
ds@1952
   207
		ReplyToCommand(client, "[SM] Usage: sm_addban <time> <steamid> [reason]");
ds@1952
   208
		return Plugin_Handled;
ds@1952
   209
	}
ds@1952
   210
ds@1952
   211
	decl String:arg_string[256];
ds@1952
   212
	new String:time[50];
ds@1952
   213
	new String:authid[50];
ds@1952
   214
ds@1952
   215
	GetCmdArgString(arg_string, sizeof(arg_string));
ds@1952
   216
ds@1952
   217
	new len, total_len;
ds@1952
   218
ds@1952
   219
	/* Get time */
ds@1952
   220
	if ((len = BreakString(arg_string, time, sizeof(time))) == -1)
ds@1952
   221
	{
ds@1952
   222
		ReplyToCommand(client, "t[SM] Usage: sm_addban <time> <steamid> [reason]");
ds@1952
   223
		return Plugin_Handled;
ds@1952
   224
	}	
ds@1952
   225
	total_len += len;
ds@1952
   226
ds@1952
   227
	/* Get steamid */
ds@1952
   228
	if ((len = BreakString(arg_string[total_len], authid, sizeof(authid))) != -1)
ds@1952
   229
	{
ds@1952
   230
		total_len += len;
ds@1952
   231
	}
ds@1952
   232
	else
ds@1952
   233
	{
ds@1952
   234
		total_len = 0;
ds@1952
   235
		arg_string[0] = '\0';
ds@1952
   236
	}
ds@1952
   237
ds@1952
   238
	/* Verify steamid */
ds@1952
   239
	if (strncmp(authid, "STEAM_0:", 8) != 0)
ds@1952
   240
	{
ds@1952
   241
		ReplyToCommand(client, "[SM] %t", "Invalid SteamID specified");
ds@1952
   242
		return Plugin_Handled;
ds@1952
   243
	}
ds@1952
   244
ds@1952
   245
	new minutes = StringToInt(time);
ds@1952
   246
ds@1952
   247
	LogAction(client, 
ds@1952
   248
			  -1, 
ds@1952
   249
			  "\"%L\" added ban (minutes \"%d\") (id \"%s\") (reason \"%s\")", 
ds@1952
   250
			  client, 
ds@1952
   251
			  minutes, 
ds@1952
   252
			  authid, 
ds@1952
   253
			  arg_string[total_len]);
ds@1952
   254
	BanIdentity(authid, 
ds@1952
   255
				minutes, 
ds@1952
   256
				BANFLAG_AUTHID, 
ds@1952
   257
				arg_string[total_len], 
ds@1952
   258
				"sm_addban", 
ds@1952
   259
				client);
ds@1952
   260
ds@1952
   261
	ReplyToCommand(client, "[SM] %t", "Ban added");
ds@1952
   262
ds@1952
   263
	return Plugin_Handled;
ds@1952
   264
}
ds@1952
   265
ds@1952
   266
public Action:Command_Unban(client, args)
ds@1952
   267
{
ds@1952
   268
	if (args < 1)
ds@1952
   269
	{
ds@1952
   270
		ReplyToCommand(client, "[SM] Usage: sm_unban <steamid|ip>");
ds@1952
   271
		return Plugin_Handled;
ds@1952
   272
	}
ds@1952
   273
ds@1952
   274
	decl String:arg[50];
ds@1952
   275
	GetCmdArgString(arg, sizeof(arg));
ds@1952
   276
ds@1952
   277
	ReplaceString(arg, sizeof(arg), "\"", "");	
ds@1952
   278
ds@1952
   279
	new ban_flags;
ds@1952
   280
	if (strncmp(arg, "STEAM_0:", 8) == 0)
ds@1952
   281
	{
ds@1952
   282
		ban_flags |= BANFLAG_AUTHID;
ds@1952
   283
	}
ds@1952
   284
	else
ds@1952
   285
	{
ds@1952
   286
		ban_flags |= BANFLAG_IP;
ds@1952
   287
	}
ds@1952
   288
ds@1952
   289
	LogAction(client, -1, "\"%L\" removed ban (filter \"%s\")", client, arg);
ds@1952
   290
	RemoveBan(arg, ban_flags, "sm_unban", client);
ds@1952
   291
ds@1952
   292
	ReplyToCommand(client, "[SM] %t", "Removed bans matching", arg);
ds@1952
   293
ds@1952
   294
	return Plugin_Handled;
ds@1952
   295
}
ds@1952
   296