plugins/basecommands.sp
changeset 1952 b7aa2b626807
child 2068 ec32dd185d80
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/basecommands.sp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,370 @@
     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 +	
    1.78 +	/* Account for late loading */
    1.79 +	new Handle:topmenu;
    1.80 +	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
    1.81 +	{
    1.82 +		OnAdminMenuReady(topmenu);
    1.83 +	}
    1.84 +	
    1.85 +	g_MapList = CreateMenu(MenuHandler_ChangeMap);
    1.86 +	SetMenuTitle(g_MapList, "Please select a map");
    1.87 +	SetMenuExitBackButton(g_MapList, true);
    1.88 +	
    1.89 +	decl String:mapListPath[PLATFORM_MAX_PATH];
    1.90 +	BuildPath(Path_SM, mapListPath, sizeof(mapListPath), "configs/adminmenu_maplist.ini");
    1.91 +	SetMapListCompatBind("sm_map menu", mapListPath);
    1.92 +	
    1.93 +	g_ProtectedVars = CreateTrie();
    1.94 +	ProtectVar("rcon_password");
    1.95 +	ProtectVar("sm_show_activity");
    1.96 +	ProtectVar("sm_immunity_mode");
    1.97 +}
    1.98 +
    1.99 +public OnMapStart()
   1.100 +{
   1.101 +	ParseConfigs();
   1.102 +}
   1.103 +
   1.104 +public OnConfigsExecuted()
   1.105 +{
   1.106 +	LoadMapList(g_MapList);
   1.107 +}
   1.108 +
   1.109 +ProtectVar(const String:cvar[])
   1.110 +{
   1.111 +	SetTrieValue(g_ProtectedVars, cvar, 1);
   1.112 +}
   1.113 +
   1.114 +bool:IsVarProtected(const String:cvar[])
   1.115 +{
   1.116 +	decl dummy_value;
   1.117 +	return GetTrieValue(g_ProtectedVars, cvar, dummy_value);
   1.118 +}
   1.119 +
   1.120 +public OnAdminMenuReady(Handle:topmenu)
   1.121 +{
   1.122 +	/* Block us from being called twice */
   1.123 +	if (topmenu == hTopMenu)
   1.124 +	{
   1.125 +		return;
   1.126 +	}
   1.127 +	
   1.128 +	/* Save the Handle */
   1.129 +	hTopMenu = topmenu;
   1.130 +	
   1.131 +	/* Build the "Player Commands" category */
   1.132 +	new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
   1.133 +	
   1.134 +	if (player_commands != INVALID_TOPMENUOBJECT)
   1.135 +	{
   1.136 +		AddToTopMenu(hTopMenu, 
   1.137 +			"sm_kick",
   1.138 +			TopMenuObject_Item,
   1.139 +			AdminMenu_Kick,
   1.140 +			player_commands,
   1.141 +			"sm_kick",
   1.142 +			ADMFLAG_KICK);
   1.143 +			
   1.144 +		AddToTopMenu(hTopMenu,
   1.145 +			"sm_who",
   1.146 +			TopMenuObject_Item,
   1.147 +			AdminMenu_Who,
   1.148 +			player_commands,
   1.149 +			"sm_who",
   1.150 +			ADMFLAG_GENERIC);
   1.151 +	}
   1.152 +
   1.153 +	new TopMenuObject:server_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_SERVERCOMMANDS);
   1.154 +
   1.155 +	if (server_commands != INVALID_TOPMENUOBJECT)
   1.156 +	{
   1.157 +		AddToTopMenu(hTopMenu,
   1.158 +			"sm_reloadadmins",
   1.159 +			TopMenuObject_Item,
   1.160 +			AdminMenu_ReloadAdmins,
   1.161 +			server_commands,
   1.162 +			"sm_reloadadmins",
   1.163 +			ADMFLAG_BAN);
   1.164 +			
   1.165 +		AddToTopMenu(hTopMenu,
   1.166 +			"sm_map",
   1.167 +			TopMenuObject_Item,
   1.168 +			AdminMenu_Map,
   1.169 +			server_commands,
   1.170 +			"sm_map",
   1.171 +			ADMFLAG_CHANGEMAP);
   1.172 +			
   1.173 +		AddToTopMenu(hTopMenu,
   1.174 +			"sm_execcfg",
   1.175 +			TopMenuObject_Item,
   1.176 +			AdminMenu_ExecCFG,
   1.177 +			server_commands,
   1.178 +			"sm_execcfg",
   1.179 +			ADMFLAG_CONFIG);		
   1.180 +	}
   1.181 +
   1.182 +	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
   1.183 +
   1.184 +	if (voting_commands != INVALID_TOPMENUOBJECT)
   1.185 +	{
   1.186 +		AddToTopMenu(hTopMenu,
   1.187 +			"sm_cancelvote",
   1.188 +			TopMenuObject_Item,
   1.189 +			AdminMenu_CancelVote,
   1.190 +			voting_commands,
   1.191 +			"sm_cancelvote",
   1.192 +			ADMFLAG_VOTE);
   1.193 +	}
   1.194 +}
   1.195 +
   1.196 +public OnLibraryRemoved(const String:name[])
   1.197 +{
   1.198 +	if (strcmp(name, "adminmenu") == 0)
   1.199 +	{
   1.200 +		hTopMenu = INVALID_HANDLE;
   1.201 +	}
   1.202 +}
   1.203 +
   1.204 +#define FLAG_STRINGS		14
   1.205 +new String:g_FlagNames[FLAG_STRINGS][20] =
   1.206 +{
   1.207 +	"res",
   1.208 +	"admin",
   1.209 +	"kick",
   1.210 +	"ban",
   1.211 +	"unban",
   1.212 +	"slay",
   1.213 +	"map",
   1.214 +	"cvars",
   1.215 +	"cfg",
   1.216 +	"chat",
   1.217 +	"vote",
   1.218 +	"pass",
   1.219 +	"rcon",
   1.220 +	"cheat"
   1.221 +};
   1.222 +
   1.223 +CustomFlagsToString(String:buffer[], maxlength, flags)
   1.224 +{
   1.225 +	decl String:joins[6][6];
   1.226 +	new total;
   1.227 +	
   1.228 +	for (new i=_:Admin_Custom1; i<=_:Admin_Custom6; i++)
   1.229 +	{
   1.230 +		if (flags & (1<<i))
   1.231 +		{
   1.232 +			IntToString(i - _:Admin_Custom1 + 1, joins[total++], 6);
   1.233 +		}
   1.234 +	}
   1.235 +	
   1.236 +	ImplodeStrings(joins, total, ",", buffer, maxlength);
   1.237 +	
   1.238 +	return total;
   1.239 +}
   1.240 +
   1.241 +FlagsToString(String:buffer[], maxlength, flags)
   1.242 +{
   1.243 +	decl String:joins[FLAG_STRINGS+1][32];
   1.244 +	new total;
   1.245 +
   1.246 +	for (new i=0; i<FLAG_STRINGS; i++)
   1.247 +	{
   1.248 +		if (flags & (1<<i))
   1.249 +		{
   1.250 +			strcopy(joins[total++], 32, g_FlagNames[i]);
   1.251 +		}
   1.252 +	}
   1.253 +	
   1.254 +	decl String:custom_flags[32];
   1.255 +	if (CustomFlagsToString(custom_flags, sizeof(custom_flags), flags))
   1.256 +	{
   1.257 +		Format(joins[total++], 32, "custom(%s)", custom_flags);
   1.258 +	}
   1.259 +
   1.260 +	ImplodeStrings(joins, total, ", ", buffer, maxlength);
   1.261 +}
   1.262 +
   1.263 +public Action:Command_Cvar(client, args)
   1.264 +{
   1.265 +	if (args < 1)
   1.266 +	{
   1.267 +		if (client == 0)
   1.268 +		{
   1.269 +			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar|protect> [value]");
   1.270 +		}
   1.271 +		else
   1.272 +		{
   1.273 +			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
   1.274 +		}
   1.275 +		return Plugin_Handled;
   1.276 +	}
   1.277 +
   1.278 +	decl String:cvarname[64];
   1.279 +	GetCmdArg(1, cvarname, sizeof(cvarname));
   1.280 +	
   1.281 +	if (client == 0 && StrEqual(cvarname, "protect"))
   1.282 +	{
   1.283 +		if (args < 2)
   1.284 +		{
   1.285 +			ReplyToCommand(client, "[SM] Usage: sm_cvar <protect> <cvar>");
   1.286 +			return Plugin_Handled;
   1.287 +		}
   1.288 +		GetCmdArg(2, cvarname, sizeof(cvarname));
   1.289 +		ProtectVar(cvarname);
   1.290 +		ReplyToCommand(client, "[SM] %t", "Cvar is now protected", cvarname);
   1.291 +		return Plugin_Handled;
   1.292 +	}
   1.293 +
   1.294 +	new Handle:hndl = FindConVar(cvarname);
   1.295 +	if (hndl == INVALID_HANDLE)
   1.296 +	{
   1.297 +		ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
   1.298 +		return Plugin_Handled;
   1.299 +	}
   1.300 +	
   1.301 +	new bool:allowed = false;
   1.302 +	new client_flags = client == 0 ? ADMFLAG_ROOT : GetUserFlagBits(client);
   1.303 +	
   1.304 +	if (client_flags & ADMFLAG_ROOT)
   1.305 +	{
   1.306 +		allowed = true;
   1.307 +	}
   1.308 +	else
   1.309 +	{
   1.310 +		if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
   1.311 +		{
   1.312 +			allowed = ((client_flags & ADMFLAG_PASSWORD) == ADMFLAG_PASSWORD);
   1.313 +		}
   1.314 +		else if (StrEqual(cvarname, "sv_cheats"))
   1.315 +		{
   1.316 +			allowed = ((client_flags & ADMFLAG_CHEATS) == ADMFLAG_CHEATS);
   1.317 +		}
   1.318 +		else if (!IsVarProtected(cvarname))
   1.319 +		{
   1.320 +			allowed = true;
   1.321 +		}
   1.322 +	}
   1.323 +	
   1.324 +	if (!allowed)
   1.325 +	{
   1.326 +		ReplyToCommand(client, "[SM] %t", "No access to cvar");
   1.327 +		return Plugin_Handled;
   1.328 +	}
   1.329 +
   1.330 +	decl String:value[255];
   1.331 +	if (args < 2)
   1.332 +	{
   1.333 +		GetConVarString(hndl, value, sizeof(value));
   1.334 +
   1.335 +		ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
   1.336 +		return Plugin_Handled;
   1.337 +	}
   1.338 +
   1.339 +	GetCmdArg(2, value, sizeof(value));
   1.340 +
   1.341 +	if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
   1.342 +	{
   1.343 +		ShowActivity2(client, "[SM] ", "%t", "Cvar changed", cvarname, value);
   1.344 +	}
   1.345 +	else
   1.346 +	{
   1.347 +		ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
   1.348 +	}
   1.349 +
   1.350 +	LogAction(client, -1, "\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
   1.351 +
   1.352 +	SetConVarString(hndl, value, true);
   1.353 +
   1.354 +	return Plugin_Handled;
   1.355 +}
   1.356 +
   1.357 +public Action:Command_Rcon(client, args)
   1.358 +{
   1.359 +	if (args < 1)
   1.360 +	{
   1.361 +		ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
   1.362 +		return Plugin_Handled;
   1.363 +	}
   1.364 +
   1.365 +	decl String:argstring[255];
   1.366 +	GetCmdArgString(argstring, sizeof(argstring));
   1.367 +
   1.368 +	LogAction(client, -1, "\"%L\" console command (cmdline \"%s\")", client, argstring);
   1.369 +
   1.370 +	ServerCommand("%s", argstring);
   1.371 +
   1.372 +	return Plugin_Handled;
   1.373 +}