plugins/basecommands.sp
author David Anderson <dvander@alliedmods.net>
Fri Feb 22 21:01:08 2008 +0000 (2008-02-22)
changeset 1889 5b4d6acc4d82
parent 1844 eff718469d84
child 1947 e73a1e51833b
permissions -rw-r--r--
deprecated LoadMaps() and its usage in three plugins
renamed basefun* to fun*
removed map file list cvars
     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 1works) 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 Commands",
    43 	author = "AlliedModders LLC",
    44 	description = "Basic Admin Commands",
    45 	version = SOURCEMOD_VERSION,
    46 	url = "http://www.sourcemod.net/"
    47 };
    48 
    49 new Handle:hTopMenu = INVALID_HANDLE;
    50 
    51 new Handle:g_MapList;
    52 new Handle:g_ProtectedVars;
    53 
    54 #include "basecommands/kick.sp"
    55 #include "basecommands/reloadadmins.sp"
    56 #include "basecommands/cancelvote.sp"
    57 #include "basecommands/who.sp"
    58 #include "basecommands/map.sp"
    59 #include "basecommands/execcfg.sp"
    60 
    61 public OnPluginStart()
    62 {
    63 	LoadTranslations("common.phrases");
    64 	LoadTranslations("plugin.basecommands");
    65 
    66 	RegAdminCmd("sm_kick", Command_Kick, ADMFLAG_KICK, "sm_kick <#userid|name> [reason]");
    67 	RegAdminCmd("sm_map", Command_Map, ADMFLAG_CHANGEMAP, "sm_map <map>");
    68 	RegAdminCmd("sm_rcon", Command_Rcon, ADMFLAG_RCON, "sm_rcon <args>");
    69 	RegAdminCmd("sm_cvar", Command_Cvar, ADMFLAG_CONVARS, "sm_cvar <cvar> [value]");
    70 	RegAdminCmd("sm_execcfg", Command_ExecCfg, ADMFLAG_CONFIG, "sm_execcfg <filename>");
    71 	RegAdminCmd("sm_who", Command_Who, ADMFLAG_GENERIC, "sm_who [#userid|name]");
    72 	RegAdminCmd("sm_reloadadmins", Command_ReloadAdmins, ADMFLAG_BAN, "sm_reloadadmins");
    73 	RegAdminCmd("sm_cancelvote", Command_CancelVote, ADMFLAG_VOTE, "sm_cancelvote");
    74 	
    75 	/* Account for late loading */
    76 	new Handle:topmenu;
    77 	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
    78 	{
    79 		OnAdminMenuReady(topmenu);
    80 	}
    81 	
    82 	g_MapList = CreateMenu(MenuHandler_ChangeMap);
    83 	SetMenuTitle(g_MapList, "Please select a map");
    84 	SetMenuExitBackButton(g_MapList, true);
    85 	
    86 	decl String:mapListPath[PLATFORM_MAX_PATH];
    87 	BuildPath(Path_SM, mapListPath, sizeof(mapListPath), "configs/adminmenu_maplist.ini");
    88 	SetMapListCompatBind("sm_map menu", mapListPath);
    89 	
    90 	g_ProtectedVars = CreateTrie();
    91 	ProtectVar("rcon_password");
    92 	ProtectVar("sm_show_activity");
    93 	ProtectVar("sm_immunity_mode");
    94 }
    95 
    96 public OnMapStart()
    97 {
    98 	ParseConfigs();
    99 }
   100 
   101 public OnConfigsExecuted()
   102 {
   103 	LoadMapList(g_MapList);
   104 }
   105 
   106 ProtectVar(const String:cvar[])
   107 {
   108 	SetTrieValue(g_ProtectedVars, cvar, 1);
   109 }
   110 
   111 bool:IsVarProtected(const String:cvar[])
   112 {
   113 	decl dummy_value;
   114 	return GetTrieValue(g_ProtectedVars, cvar, dummy_value);
   115 }
   116 
   117 public OnAdminMenuReady(Handle:topmenu)
   118 {
   119 	/* Block us from being called twice */
   120 	if (topmenu == hTopMenu)
   121 	{
   122 		return;
   123 	}
   124 	
   125 	/* Save the Handle */
   126 	hTopMenu = topmenu;
   127 	
   128 	/* Build the "Player Commands" category */
   129 	new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
   130 	
   131 	if (player_commands != INVALID_TOPMENUOBJECT)
   132 	{
   133 		AddToTopMenu(hTopMenu, 
   134 			"sm_kick",
   135 			TopMenuObject_Item,
   136 			AdminMenu_Kick,
   137 			player_commands,
   138 			"sm_kick",
   139 			ADMFLAG_KICK);
   140 			
   141 		AddToTopMenu(hTopMenu,
   142 			"sm_who",
   143 			TopMenuObject_Item,
   144 			AdminMenu_Who,
   145 			player_commands,
   146 			"sm_who",
   147 			ADMFLAG_GENERIC);
   148 	}
   149 
   150 	new TopMenuObject:server_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_SERVERCOMMANDS);
   151 
   152 	if (server_commands != INVALID_TOPMENUOBJECT)
   153 	{
   154 		AddToTopMenu(hTopMenu,
   155 			"sm_reloadadmins",
   156 			TopMenuObject_Item,
   157 			AdminMenu_ReloadAdmins,
   158 			server_commands,
   159 			"sm_reloadadmins",
   160 			ADMFLAG_BAN);
   161 			
   162 		AddToTopMenu(hTopMenu,
   163 			"sm_map",
   164 			TopMenuObject_Item,
   165 			AdminMenu_Map,
   166 			server_commands,
   167 			"sm_map",
   168 			ADMFLAG_CHANGEMAP);
   169 			
   170 		AddToTopMenu(hTopMenu,
   171 			"sm_execcfg",
   172 			TopMenuObject_Item,
   173 			AdminMenu_ExecCFG,
   174 			server_commands,
   175 			"sm_execcfg",
   176 			ADMFLAG_CONFIG);		
   177 	}
   178 
   179 	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
   180 
   181 	if (voting_commands != INVALID_TOPMENUOBJECT)
   182 	{
   183 		AddToTopMenu(hTopMenu,
   184 			"sm_cancelvote",
   185 			TopMenuObject_Item,
   186 			AdminMenu_CancelVote,
   187 			voting_commands,
   188 			"sm_cancelvote",
   189 			ADMFLAG_VOTE);
   190 	}
   191 }
   192 
   193 public OnLibraryRemoved(const String:name[])
   194 {
   195 	if (strcmp(name, "adminmenu") == 0)
   196 	{
   197 		hTopMenu = INVALID_HANDLE;
   198 	}
   199 }
   200 
   201 #define FLAG_STRINGS		14
   202 new String:g_FlagNames[FLAG_STRINGS][20] =
   203 {
   204 	"res",
   205 	"admin",
   206 	"kick",
   207 	"ban",
   208 	"unban",
   209 	"slay",
   210 	"map",
   211 	"cvars",
   212 	"cfg",
   213 	"chat",
   214 	"vote",
   215 	"pass",
   216 	"rcon",
   217 	"cheat"
   218 };
   219 
   220 CustomFlagsToString(String:buffer[], maxlength, flags)
   221 {
   222 	decl String:joins[6][6];
   223 	new total;
   224 	
   225 	for (new i=_:Admin_Custom1; i<=_:Admin_Custom6; i++)
   226 	{
   227 		if (flags & (1<<i))
   228 		{
   229 			IntToString(i - _:Admin_Custom1 + 1, joins[total++], 6);
   230 		}
   231 	}
   232 	
   233 	ImplodeStrings(joins, total, ",", buffer, maxlength);
   234 	
   235 	return total;
   236 }
   237 
   238 FlagsToString(String:buffer[], maxlength, flags)
   239 {
   240 	decl String:joins[FLAG_STRINGS+1][32];
   241 	new total;
   242 
   243 	for (new i=0; i<FLAG_STRINGS; i++)
   244 	{
   245 		if (flags & (1<<i))
   246 		{
   247 			strcopy(joins[total++], 32, g_FlagNames[i]);
   248 		}
   249 	}
   250 	
   251 	decl String:custom_flags[32];
   252 	if (CustomFlagsToString(custom_flags, sizeof(custom_flags), flags))
   253 	{
   254 		Format(joins[total++], 32, "custom(%s)", custom_flags);
   255 	}
   256 
   257 	ImplodeStrings(joins, total, ", ", buffer, maxlength);
   258 }
   259 
   260 public Action:Command_Cvar(client, args)
   261 {
   262 	if (args < 1)
   263 	{
   264 		if (client == 0)
   265 		{
   266 			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar|protect> [value]");
   267 		}
   268 		else
   269 		{
   270 			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
   271 		}
   272 		return Plugin_Handled;
   273 	}
   274 
   275 	decl String:cvarname[64];
   276 	GetCmdArg(1, cvarname, sizeof(cvarname));
   277 	
   278 	if (client == 0 && StrEqual(cvarname, "protect"))
   279 	{
   280 		if (args < 2)
   281 		{
   282 			ReplyToCommand(client, "[SM] Usage: sm_cvar <protect> <cvar>");
   283 			return Plugin_Handled;
   284 		}
   285 		GetCmdArg(2, cvarname, sizeof(cvarname));
   286 		ProtectVar(cvarname);
   287 		ReplyToCommand(client, "[SM] %t", "Cvar is now protected", cvarname);
   288 		return Plugin_Handled;
   289 	}
   290 
   291 	new Handle:hndl = FindConVar(cvarname);
   292 	if (hndl == INVALID_HANDLE)
   293 	{
   294 		ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
   295 		return Plugin_Handled;
   296 	}
   297 	
   298 	new bool:allowed = false;
   299 	new client_flags = client == 0 ? ADMFLAG_ROOT : GetUserFlagBits(client);
   300 	
   301 	if (client_flags & ADMFLAG_ROOT)
   302 	{
   303 		allowed = true;
   304 	}
   305 	else
   306 	{
   307 		if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
   308 		{
   309 			allowed = ((client_flags & ADMFLAG_PASSWORD) == ADMFLAG_PASSWORD);
   310 		}
   311 		else if (StrEqual(cvarname, "sv_cheats"))
   312 		{
   313 			allowed = ((client_flags & ADMFLAG_CHEATS) == ADMFLAG_CHEATS);
   314 		}
   315 		else if (!IsVarProtected(cvarname))
   316 		{
   317 			allowed = true;
   318 		}
   319 	}
   320 	
   321 	if (!allowed)
   322 	{
   323 		ReplyToCommand(client, "[SM] %t", "No access to cvar");
   324 		return Plugin_Handled;
   325 	}
   326 
   327 	decl String:value[255];
   328 	if (args < 2)
   329 	{
   330 		GetConVarString(hndl, value, sizeof(value));
   331 
   332 		ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
   333 		return Plugin_Handled;
   334 	}
   335 
   336 	GetCmdArg(2, value, sizeof(value));
   337 
   338 	if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
   339 	{
   340 		ShowActivity2(client, "[SM] ", "%t", "Cvar changed", cvarname, value);
   341 	}
   342 	else
   343 	{
   344 		ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
   345 	}
   346 
   347 	LogAction(client, -1, "\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
   348 
   349 	SetConVarString(hndl, value, true);
   350 
   351 	return Plugin_Handled;
   352 }
   353 
   354 public Action:Command_Rcon(client, args)
   355 {
   356 	if (args < 1)
   357 	{
   358 		ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
   359 		return Plugin_Handled;
   360 	}
   361 
   362 	decl String:argstring[255];
   363 	GetCmdArgString(argstring, sizeof(argstring));
   364 
   365 	LogAction(client, -1, "\"%L\" console command (cmdline \"%s\")", client, argstring);
   366 
   367 	ServerCommand("%s", argstring);
   368 
   369 	return Plugin_Handled;
   370 }