plugins/basecommands.sp
branchrefac-jit
changeset 2168 c24a6fa957db
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/basecommands.sp	Sun Jul 06 04:49:55 2008 +0000
     1.3 @@ -0,0 +1,399 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Basic Commands Plugin
     1.8 + * Implements basic admin commands.
     1.9 + *
    1.10 + * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
    1.11 + * =============================================================================
    1.12 + *
    1.13 + * This program is free software; you can redistribute it and/or modify it under
    1.14 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.15 + * Free Software Foundation.
    1.16 + * 
    1.17 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.18 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.19 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.20 + * details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU General Public License along with
    1.23 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.24 + *
    1.25 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.26 + * code of this program (as well as its derivative 1works) to "Half-Life 2," the
    1.27 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.28 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.29 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.30 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.31 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.32 + * or <http://www.sourcemod.net/license.php>.
    1.33 + *
    1.34 + * Version: $Id$
    1.35 + */
    1.36 +
    1.37 +#pragma semicolon 1
    1.38 +
    1.39 +#include <sourcemod>
    1.40 +#undef REQUIRE_PLUGIN
    1.41 +#include <adminmenu>
    1.42 +
    1.43 +public Plugin:myinfo =
    1.44 +{
    1.45 +	name = "Basic Commands",
    1.46 +	author = "AlliedModders LLC",
    1.47 +	description = "Basic Admin Commands",
    1.48 +	version = SOURCEMOD_VERSION,
    1.49 +	url = "http://www.sourcemod.net/"
    1.50 +};
    1.51 +
    1.52 +new Handle:hTopMenu = INVALID_HANDLE;
    1.53 +
    1.54 +new Handle:g_MapList;
    1.55 +new Handle:g_ProtectedVars;
    1.56 +
    1.57 +#include "basecommands/kick.sp"
    1.58 +#include "basecommands/reloadadmins.sp"
    1.59 +#include "basecommands/cancelvote.sp"
    1.60 +#include "basecommands/who.sp"
    1.61 +#include "basecommands/map.sp"
    1.62 +#include "basecommands/execcfg.sp"
    1.63 +
    1.64 +public OnPluginStart()
    1.65 +{
    1.66 +	LoadTranslations("common.phrases");
    1.67 +	LoadTranslations("plugin.basecommands");
    1.68 +
    1.69 +	RegAdminCmd("sm_kick", Command_Kick, ADMFLAG_KICK, "sm_kick <#userid|name> [reason]");
    1.70 +	RegAdminCmd("sm_map", Command_Map, ADMFLAG_CHANGEMAP, "sm_map <map>");
    1.71 +	RegAdminCmd("sm_rcon", Command_Rcon, ADMFLAG_RCON, "sm_rcon <args>");
    1.72 +	RegAdminCmd("sm_cvar", Command_Cvar, ADMFLAG_CONVARS, "sm_cvar <cvar> [value]");
    1.73 +	RegAdminCmd("sm_execcfg", Command_ExecCfg, ADMFLAG_CONFIG, "sm_execcfg <filename>");
    1.74 +	RegAdminCmd("sm_who", Command_Who, ADMFLAG_GENERIC, "sm_who [#userid|name]");
    1.75 +	RegAdminCmd("sm_reloadadmins", Command_ReloadAdmins, ADMFLAG_BAN, "sm_reloadadmins");
    1.76 +	RegAdminCmd("sm_cancelvote", Command_CancelVote, ADMFLAG_VOTE, "sm_cancelvote");
    1.77 +	RegConsoleCmd("sm_revote", Command_ReVote);
    1.78 +	
    1.79 +	/* Account for late loading */
    1.80 +	new Handle:topmenu;
    1.81 +	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
    1.82 +	{
    1.83 +		OnAdminMenuReady(topmenu);
    1.84 +	}
    1.85 +	
    1.86 +	g_MapList = CreateMenu(MenuHandler_ChangeMap);
    1.87 +	SetMenuTitle(g_MapList, "Please select a map");
    1.88 +	SetMenuExitBackButton(g_MapList, true);
    1.89 +	
    1.90 +	decl String:mapListPath[PLATFORM_MAX_PATH];
    1.91 +	BuildPath(Path_SM, mapListPath, sizeof(mapListPath), "configs/adminmenu_maplist.ini");
    1.92 +	SetMapListCompatBind("sm_map menu", mapListPath);
    1.93 +	
    1.94 +	g_ProtectedVars = CreateTrie();
    1.95 +	ProtectVar("rcon_password");
    1.96 +	ProtectVar("sm_show_activity");
    1.97 +	ProtectVar("sm_immunity_mode");
    1.98 +}
    1.99 +
   1.100 +public OnMapStart()
   1.101 +{
   1.102 +	ParseConfigs();
   1.103 +}
   1.104 +
   1.105 +public OnConfigsExecuted()
   1.106 +{
   1.107 +	LoadMapList(g_MapList);
   1.108 +}
   1.109 +
   1.110 +ProtectVar(const String:cvar[])
   1.111 +{
   1.112 +	SetTrieValue(g_ProtectedVars, cvar, 1);
   1.113 +}
   1.114 +
   1.115 +bool:IsVarProtected(const String:cvar[])
   1.116 +{
   1.117 +	decl dummy_value;
   1.118 +	return GetTrieValue(g_ProtectedVars, cvar, dummy_value);
   1.119 +}
   1.120 +
   1.121 +public OnAdminMenuReady(Handle:topmenu)
   1.122 +{
   1.123 +	/* Block us from being called twice */
   1.124 +	if (topmenu == hTopMenu)
   1.125 +	{
   1.126 +		return;
   1.127 +	}
   1.128 +	
   1.129 +	/* Save the Handle */
   1.130 +	hTopMenu = topmenu;
   1.131 +	
   1.132 +	/* Build the "Player Commands" category */
   1.133 +	new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
   1.134 +	
   1.135 +	if (player_commands != INVALID_TOPMENUOBJECT)
   1.136 +	{
   1.137 +		AddToTopMenu(hTopMenu, 
   1.138 +			"sm_kick",
   1.139 +			TopMenuObject_Item,
   1.140 +			AdminMenu_Kick,
   1.141 +			player_commands,
   1.142 +			"sm_kick",
   1.143 +			ADMFLAG_KICK);
   1.144 +			
   1.145 +		AddToTopMenu(hTopMenu,
   1.146 +			"sm_who",
   1.147 +			TopMenuObject_Item,
   1.148 +			AdminMenu_Who,
   1.149 +			player_commands,
   1.150 +			"sm_who",
   1.151 +			ADMFLAG_GENERIC);
   1.152 +	}
   1.153 +
   1.154 +	new TopMenuObject:server_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_SERVERCOMMANDS);
   1.155 +
   1.156 +	if (server_commands != INVALID_TOPMENUOBJECT)
   1.157 +	{
   1.158 +		AddToTopMenu(hTopMenu,
   1.159 +			"sm_reloadadmins",
   1.160 +			TopMenuObject_Item,
   1.161 +			AdminMenu_ReloadAdmins,
   1.162 +			server_commands,
   1.163 +			"sm_reloadadmins",
   1.164 +			ADMFLAG_BAN);
   1.165 +			
   1.166 +		AddToTopMenu(hTopMenu,
   1.167 +			"sm_map",
   1.168 +			TopMenuObject_Item,
   1.169 +			AdminMenu_Map,
   1.170 +			server_commands,
   1.171 +			"sm_map",
   1.172 +			ADMFLAG_CHANGEMAP);
   1.173 +			
   1.174 +		AddToTopMenu(hTopMenu,
   1.175 +			"sm_execcfg",
   1.176 +			TopMenuObject_Item,
   1.177 +			AdminMenu_ExecCFG,
   1.178 +			server_commands,
   1.179 +			"sm_execcfg",
   1.180 +			ADMFLAG_CONFIG);		
   1.181 +	}
   1.182 +
   1.183 +	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
   1.184 +
   1.185 +	if (voting_commands != INVALID_TOPMENUOBJECT)
   1.186 +	{
   1.187 +		AddToTopMenu(hTopMenu,
   1.188 +			"sm_cancelvote",
   1.189 +			TopMenuObject_Item,
   1.190 +			AdminMenu_CancelVote,
   1.191 +			voting_commands,
   1.192 +			"sm_cancelvote",
   1.193 +			ADMFLAG_VOTE);
   1.194 +	}
   1.195 +}
   1.196 +
   1.197 +public OnLibraryRemoved(const String:name[])
   1.198 +{
   1.199 +	if (strcmp(name, "adminmenu") == 0)
   1.200 +	{
   1.201 +		hTopMenu = INVALID_HANDLE;
   1.202 +	}
   1.203 +}
   1.204 +
   1.205 +#define FLAG_STRINGS		14
   1.206 +new String:g_FlagNames[FLAG_STRINGS][20] =
   1.207 +{
   1.208 +	"res",
   1.209 +	"admin",
   1.210 +	"kick",
   1.211 +	"ban",
   1.212 +	"unban",
   1.213 +	"slay",
   1.214 +	"map",
   1.215 +	"cvars",
   1.216 +	"cfg",
   1.217 +	"chat",
   1.218 +	"vote",
   1.219 +	"pass",
   1.220 +	"rcon",
   1.221 +	"cheat"
   1.222 +};
   1.223 +
   1.224 +CustomFlagsToString(String:buffer[], maxlength, flags)
   1.225 +{
   1.226 +	decl String:joins[6][6];
   1.227 +	new total;
   1.228 +	
   1.229 +	for (new i=_:Admin_Custom1; i<=_:Admin_Custom6; i++)
   1.230 +	{
   1.231 +		if (flags & (1<<i))
   1.232 +		{
   1.233 +			IntToString(i - _:Admin_Custom1 + 1, joins[total++], 6);
   1.234 +		}
   1.235 +	}
   1.236 +	
   1.237 +	ImplodeStrings(joins, total, ",", buffer, maxlength);
   1.238 +	
   1.239 +	return total;
   1.240 +}
   1.241 +
   1.242 +FlagsToString(String:buffer[], maxlength, flags)
   1.243 +{
   1.244 +	decl String:joins[FLAG_STRINGS+1][32];
   1.245 +	new total;
   1.246 +
   1.247 +	for (new i=0; i<FLAG_STRINGS; i++)
   1.248 +	{
   1.249 +		if (flags & (1<<i))
   1.250 +		{
   1.251 +			strcopy(joins[total++], 32, g_FlagNames[i]);
   1.252 +		}
   1.253 +	}
   1.254 +	
   1.255 +	decl String:custom_flags[32];
   1.256 +	if (CustomFlagsToString(custom_flags, sizeof(custom_flags), flags))
   1.257 +	{
   1.258 +		Format(joins[total++], 32, "custom(%s)", custom_flags);
   1.259 +	}
   1.260 +
   1.261 +	ImplodeStrings(joins, total, ", ", buffer, maxlength);
   1.262 +}
   1.263 +
   1.264 +public Action:Command_Cvar(client, args)
   1.265 +{
   1.266 +	if (args < 1)
   1.267 +	{
   1.268 +		if (client == 0)
   1.269 +		{
   1.270 +			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar|protect> [value]");
   1.271 +		}
   1.272 +		else
   1.273 +		{
   1.274 +			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
   1.275 +		}
   1.276 +		return Plugin_Handled;
   1.277 +	}
   1.278 +
   1.279 +	decl String:cvarname[64];
   1.280 +	GetCmdArg(1, cvarname, sizeof(cvarname));
   1.281 +	
   1.282 +	if (client == 0 && StrEqual(cvarname, "protect"))
   1.283 +	{
   1.284 +		if (args < 2)
   1.285 +		{
   1.286 +			ReplyToCommand(client, "[SM] Usage: sm_cvar <protect> <cvar>");
   1.287 +			return Plugin_Handled;
   1.288 +		}
   1.289 +		GetCmdArg(2, cvarname, sizeof(cvarname));
   1.290 +		ProtectVar(cvarname);
   1.291 +		ReplyToCommand(client, "[SM] %t", "Cvar is now protected", cvarname);
   1.292 +		return Plugin_Handled;
   1.293 +	}
   1.294 +
   1.295 +	new Handle:hndl = FindConVar(cvarname);
   1.296 +	if (hndl == INVALID_HANDLE)
   1.297 +	{
   1.298 +		ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
   1.299 +		return Plugin_Handled;
   1.300 +	}
   1.301 +	
   1.302 +	new bool:allowed = false;
   1.303 +	new client_flags = client == 0 ? ADMFLAG_ROOT : GetUserFlagBits(client);
   1.304 +	
   1.305 +	if (client_flags & ADMFLAG_ROOT)
   1.306 +	{
   1.307 +		allowed = true;
   1.308 +	}
   1.309 +	else
   1.310 +	{
   1.311 +		if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
   1.312 +		{
   1.313 +			allowed = ((client_flags & ADMFLAG_PASSWORD) == ADMFLAG_PASSWORD);
   1.314 +		}
   1.315 +		else if (StrEqual(cvarname, "sv_cheats"))
   1.316 +		{
   1.317 +			allowed = ((client_flags & ADMFLAG_CHEATS) == ADMFLAG_CHEATS);
   1.318 +		}
   1.319 +		else if (!IsVarProtected(cvarname))
   1.320 +		{
   1.321 +			allowed = true;
   1.322 +		}
   1.323 +	}
   1.324 +	
   1.325 +	if (!allowed)
   1.326 +	{
   1.327 +		ReplyToCommand(client, "[SM] %t", "No access to cvar");
   1.328 +		return Plugin_Handled;
   1.329 +	}
   1.330 +
   1.331 +	decl String:value[255];
   1.332 +	if (args < 2)
   1.333 +	{
   1.334 +		GetConVarString(hndl, value, sizeof(value));
   1.335 +
   1.336 +		ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
   1.337 +		return Plugin_Handled;
   1.338 +	}
   1.339 +
   1.340 +	GetCmdArg(2, value, sizeof(value));
   1.341 +
   1.342 +	if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
   1.343 +	{
   1.344 +		ShowActivity2(client, "[SM] ", "%t", "Cvar changed", cvarname, value);
   1.345 +	}
   1.346 +	else
   1.347 +	{
   1.348 +		ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
   1.349 +	}
   1.350 +
   1.351 +	LogAction(client, -1, "\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
   1.352 +
   1.353 +	SetConVarString(hndl, value, true);
   1.354 +
   1.355 +	return Plugin_Handled;
   1.356 +}
   1.357 +
   1.358 +public Action:Command_Rcon(client, args)
   1.359 +{
   1.360 +	if (args < 1)
   1.361 +	{
   1.362 +		ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
   1.363 +		return Plugin_Handled;
   1.364 +	}
   1.365 +
   1.366 +	decl String:argstring[255];
   1.367 +	GetCmdArgString(argstring, sizeof(argstring));
   1.368 +
   1.369 +	LogAction(client, -1, "\"%L\" console command (cmdline \"%s\")", client, argstring);
   1.370 +
   1.371 +	ServerCommand("%s", argstring);
   1.372 +
   1.373 +	return Plugin_Handled;
   1.374 +}
   1.375 +
   1.376 +public Action:Command_ReVote(client, args)
   1.377 +{
   1.378 +	if (client == 0)
   1.379 +	{
   1.380 +		ReplyToCommand(client, "[SM] %t", "Command is in-game only");
   1.381 +		return Plugin_Handled;
   1.382 +	}
   1.383 +	
   1.384 +	if (!IsVoteInProgress())
   1.385 +	{
   1.386 +		ReplyToCommand(client, "[SM] %t", "Vote Not In Progress");
   1.387 +		return Plugin_Handled;
   1.388 +	}
   1.389 +	
   1.390 +	if (!IsClientInVotePool(client))
   1.391 +	{
   1.392 +		ReplyToCommand(client, "[SM] %t", "Cannot participate in vote");
   1.393 +		return Plugin_Handled;
   1.394 +	}
   1.395 +	
   1.396 +	if (!RedrawClientVoteMenu(client))
   1.397 +	{
   1.398 +		ReplyToCommand(client, "[SM] %t", "Cannot change vote");
   1.399 +	}
   1.400 +	
   1.401 +	return Plugin_Handled;
   1.402 +}