plugins/basecommands.sp
author David Anderson <dvander@alliedmods.net>
Fri Oct 19 08:51:05 2007 +0000 (2007-10-19)
changeset 1611 1bd18b7530eb
parent 1606 4fb3959c2c96
child 1623 382af42756e3
permissions -rw-r--r--
- added GetClientAimTarget()
- fixed a bug where GetClientEyeAngles() was basically bogus
     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 #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 
    87 public OnMapStart()
    88 {
    89 	LoadMapList(g_MapList);
    90 	
    91 	ParseConfigs();
    92 }
    93 
    94 public OnAdminMenuReady(Handle:topmenu)
    95 {
    96 	/* Block us from being called twice */
    97 	if (topmenu == hTopMenu)
    98 	{
    99 		return;
   100 	}
   101 	
   102 	/* Save the Handle */
   103 	hTopMenu = topmenu;
   104 	
   105 	/* Build the "Player Commands" category */
   106 	new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
   107 	
   108 	if (player_commands != INVALID_TOPMENUOBJECT)
   109 	{
   110 		AddToTopMenu(hTopMenu, 
   111 			"sm_kick",
   112 			TopMenuObject_Item,
   113 			AdminMenu_Kick,
   114 			player_commands,
   115 			"sm_kick",
   116 			ADMFLAG_KICK);
   117 			
   118 		AddToTopMenu(hTopMenu,
   119 			"sm_who",
   120 			TopMenuObject_Item,
   121 			AdminMenu_Who,
   122 			player_commands,
   123 			"sm_who",
   124 			ADMFLAG_GENERIC);
   125 	}
   126 
   127 	new TopMenuObject:server_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_SERVERCOMMANDS);
   128 
   129 	if (server_commands != INVALID_TOPMENUOBJECT)
   130 	{
   131 		AddToTopMenu(hTopMenu,
   132 			"sm_reloadadmins",
   133 			TopMenuObject_Item,
   134 			AdminMenu_ReloadAdmins,
   135 			server_commands,
   136 			"sm_reloadadmins",
   137 			ADMFLAG_BAN);
   138 			
   139 		AddToTopMenu(hTopMenu,
   140 			"sm_map",
   141 			TopMenuObject_Item,
   142 			AdminMenu_Map,
   143 			server_commands,
   144 			"sm_map",
   145 			ADMFLAG_CHANGEMAP);
   146 			
   147 		AddToTopMenu(hTopMenu,
   148 			"sm_execcfg",
   149 			TopMenuObject_Item,
   150 			AdminMenu_ExecCFG,
   151 			server_commands,
   152 			"sm_execcfg",
   153 			ADMFLAG_CONFIG);		
   154 	}
   155 
   156 	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
   157 
   158 	if (voting_commands != INVALID_TOPMENUOBJECT)
   159 	{
   160 		AddToTopMenu(hTopMenu,
   161 			"sm_cancelvote",
   162 			TopMenuObject_Item,
   163 			AdminMenu_CancelVote,
   164 			voting_commands,
   165 			"sm_cancelvote",
   166 			ADMFLAG_VOTE);
   167 	}
   168 }
   169 
   170 public OnLibraryRemoved(const String:name[])
   171 {
   172 	if (strcmp(name, "adminmenu") == 0)
   173 	{
   174 		hTopMenu = INVALID_HANDLE;
   175 	}
   176 }
   177 
   178 #define FLAG_STRINGS		14
   179 new String:g_FlagNames[FLAG_STRINGS][20] =
   180 {
   181 	"res",
   182 	"admin",
   183 	"kick",
   184 	"ban",
   185 	"unban",
   186 	"slay",
   187 	"map",
   188 	"cvars",
   189 	"cfg",
   190 	"chat",
   191 	"vote",
   192 	"pass",
   193 	"rcon",
   194 	"cheat"
   195 };
   196 
   197 CustomFlagsToString(String:buffer[], maxlength, flags)
   198 {
   199 	decl String:joins[6][6];
   200 	new total;
   201 	
   202 	for (new i=_:Admin_Custom1; i<=_:Admin_Custom6; i++)
   203 	{
   204 		if (flags & (1<<i))
   205 		{
   206 			IntToString(i - _:Admin_Custom1 + 1, joins[total++], 6);
   207 		}
   208 	}
   209 	
   210 	ImplodeStrings(joins, total, ",", buffer, maxlength);
   211 	
   212 	return total;
   213 }
   214 
   215 FlagsToString(String:buffer[], maxlength, flags)
   216 {
   217 	decl String:joins[FLAG_STRINGS+1][32];
   218 	new total;
   219 
   220 	for (new i=0; i<FLAG_STRINGS; i++)
   221 	{
   222 		if (flags & (1<<i))
   223 		{
   224 			strcopy(joins[total++], 32, g_FlagNames[i]);
   225 		}
   226 	}
   227 	
   228 	decl String:custom_flags[32];
   229 	if (CustomFlagsToString(custom_flags, sizeof(custom_flags), flags))
   230 	{
   231 		Format(joins[total++], 32, "custom(%s)", custom_flags);
   232 	}
   233 
   234 	ImplodeStrings(joins, total, ", ", buffer, maxlength);
   235 }
   236 
   237 public Action:Command_Cvar(client, args)
   238 {
   239 	if (args < 1)
   240 	{
   241 		ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
   242 		return Plugin_Handled;
   243 	}
   244 
   245 	decl String:cvarname[33];
   246 	GetCmdArg(1, cvarname, sizeof(cvarname));
   247 
   248 	new Handle:hndl = FindConVar(cvarname);
   249 	if (hndl == INVALID_HANDLE)
   250 	{
   251 		ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
   252 		return Plugin_Handled;
   253 	}
   254 
   255 	new bool:allowed = false;
   256 	if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
   257 	{
   258 		/* If they're root, allow anything */
   259 		if ((GetUserFlagBits(client) & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   260 		{
   261 			allowed = true;
   262 		}
   263 		/* If they're not root, and getting sv_password, see if they have ADMFLAG_PASSWORD */
   264 		else if (StrEqual(cvarname, "sv_password") && (GetUserFlagBits(client) & ADMFLAG_PASSWORD))
   265 		{
   266 			allowed = true;
   267 		}
   268 	}
   269 	/* Do a check for the cheat cvar */
   270 	else if (StrEqual(cvarname, "sv_cheats"))
   271 	{
   272 		if (GetUserFlagBits(client) & ADMFLAG_CHEATS
   273 			|| GetUserFlagBits(client) & ADMFLAG_ROOT)
   274 		{
   275 			allowed = true;
   276 		}
   277 	}
   278 	/* If we drop down to here, it was a normal cvar. */
   279 	else
   280 	{
   281 		allowed = true;
   282 	}
   283 
   284 	if (!allowed)
   285 	{
   286 		ReplyToCommand(client, "[SM] %t", "No access to cvar");
   287 		return Plugin_Handled;
   288 	}
   289 
   290 	decl String:value[255];
   291 	if (args < 2)
   292 	{
   293 		GetConVarString(hndl, value, sizeof(value));
   294 
   295 		ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
   296 		return Plugin_Handled;
   297 	}
   298 
   299 	GetCmdArg(2, value, sizeof(value));
   300 
   301 	if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
   302 	{
   303 		ShowActivity(client, "%t", "Cvar changed", cvarname, value);
   304 	} else {
   305 		ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
   306 	}
   307 
   308 	LogAction(client, -1, "\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
   309 
   310 	SetConVarString(hndl, value, true);
   311 
   312 	return Plugin_Handled;
   313 }
   314 
   315 public Action:Command_Rcon(client, args)
   316 {
   317 	if (args < 1)
   318 	{
   319 		ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
   320 		return Plugin_Handled;
   321 	}
   322 
   323 	decl String:argstring[255];
   324 	GetCmdArgString(argstring, sizeof(argstring));
   325 
   326 	LogAction(client, -1, "\"%L\" console command (cmdline \"%s\")", client, argstring);
   327 
   328 	ServerCommand("%s", argstring);
   329 
   330 	return Plugin_Handled;
   331 }