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