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