plugins/basecommands.sp
author Matt Woodrow <pred@alliedmods.net>
Mon Mar 31 22:44:18 2008 +0000 (2008-03-31)
changeset 1953 e0ca4605d828
parent 1952 b7aa2b626807
child 2068 ec32dd185d80
permissions -rw-r--r--
Big changes to dynamic menu. Removed constant kv tracking. Tested everything.
Import of config files.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Basic Commands Plugin
ds@1952
     5
 * Implements basic admin commands.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative 1works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#pragma semicolon 1
ds@1952
    35
ds@1952
    36
#include <sourcemod>
ds@1952
    37
#undef REQUIRE_PLUGIN
ds@1952
    38
#include <adminmenu>
ds@1952
    39
ds@1952
    40
public Plugin:myinfo =
ds@1952
    41
{
ds@1952
    42
	name = "Basic Commands",
ds@1952
    43
	author = "AlliedModders LLC",
ds@1952
    44
	description = "Basic Admin Commands",
ds@1952
    45
	version = SOURCEMOD_VERSION,
ds@1952
    46
	url = "http://www.sourcemod.net/"
ds@1952
    47
};
ds@1952
    48
ds@1952
    49
new Handle:hTopMenu = INVALID_HANDLE;
ds@1952
    50
ds@1952
    51
new Handle:g_MapList;
ds@1952
    52
new Handle:g_ProtectedVars;
ds@1952
    53
ds@1952
    54
#include "basecommands/kick.sp"
ds@1952
    55
#include "basecommands/reloadadmins.sp"
ds@1952
    56
#include "basecommands/cancelvote.sp"
ds@1952
    57
#include "basecommands/who.sp"
ds@1952
    58
#include "basecommands/map.sp"
ds@1952
    59
#include "basecommands/execcfg.sp"
ds@1952
    60
ds@1952
    61
public OnPluginStart()
ds@1952
    62
{
ds@1952
    63
	LoadTranslations("common.phrases");
ds@1952
    64
	LoadTranslations("plugin.basecommands");
ds@1952
    65
ds@1952
    66
	RegAdminCmd("sm_kick", Command_Kick, ADMFLAG_KICK, "sm_kick <#userid|name> [reason]");
ds@1952
    67
	RegAdminCmd("sm_map", Command_Map, ADMFLAG_CHANGEMAP, "sm_map <map>");
ds@1952
    68
	RegAdminCmd("sm_rcon", Command_Rcon, ADMFLAG_RCON, "sm_rcon <args>");
ds@1952
    69
	RegAdminCmd("sm_cvar", Command_Cvar, ADMFLAG_CONVARS, "sm_cvar <cvar> [value]");
ds@1952
    70
	RegAdminCmd("sm_execcfg", Command_ExecCfg, ADMFLAG_CONFIG, "sm_execcfg <filename>");
ds@1952
    71
	RegAdminCmd("sm_who", Command_Who, ADMFLAG_GENERIC, "sm_who [#userid|name]");
ds@1952
    72
	RegAdminCmd("sm_reloadadmins", Command_ReloadAdmins, ADMFLAG_BAN, "sm_reloadadmins");
ds@1952
    73
	RegAdminCmd("sm_cancelvote", Command_CancelVote, ADMFLAG_VOTE, "sm_cancelvote");
ds@1952
    74
	
ds@1952
    75
	/* Account for late loading */
ds@1952
    76
	new Handle:topmenu;
ds@1952
    77
	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
ds@1952
    78
	{
ds@1952
    79
		OnAdminMenuReady(topmenu);
ds@1952
    80
	}
ds@1952
    81
	
ds@1952
    82
	g_MapList = CreateMenu(MenuHandler_ChangeMap);
ds@1952
    83
	SetMenuTitle(g_MapList, "Please select a map");
ds@1952
    84
	SetMenuExitBackButton(g_MapList, true);
ds@1952
    85
	
ds@1952
    86
	decl String:mapListPath[PLATFORM_MAX_PATH];
ds@1952
    87
	BuildPath(Path_SM, mapListPath, sizeof(mapListPath), "configs/adminmenu_maplist.ini");
ds@1952
    88
	SetMapListCompatBind("sm_map menu", mapListPath);
ds@1952
    89
	
ds@1952
    90
	g_ProtectedVars = CreateTrie();
ds@1952
    91
	ProtectVar("rcon_password");
ds@1952
    92
	ProtectVar("sm_show_activity");
ds@1952
    93
	ProtectVar("sm_immunity_mode");
ds@1952
    94
}
ds@1952
    95
ds@1952
    96
public OnMapStart()
ds@1952
    97
{
ds@1952
    98
	ParseConfigs();
ds@1952
    99
}
ds@1952
   100
ds@1952
   101
public OnConfigsExecuted()
ds@1952
   102
{
ds@1952
   103
	LoadMapList(g_MapList);
ds@1952
   104
}
ds@1952
   105
ds@1952
   106
ProtectVar(const String:cvar[])
ds@1952
   107
{
ds@1952
   108
	SetTrieValue(g_ProtectedVars, cvar, 1);
ds@1952
   109
}
ds@1952
   110
ds@1952
   111
bool:IsVarProtected(const String:cvar[])
ds@1952
   112
{
ds@1952
   113
	decl dummy_value;
ds@1952
   114
	return GetTrieValue(g_ProtectedVars, cvar, dummy_value);
ds@1952
   115
}
ds@1952
   116
ds@1952
   117
public OnAdminMenuReady(Handle:topmenu)
ds@1952
   118
{
ds@1952
   119
	/* Block us from being called twice */
ds@1952
   120
	if (topmenu == hTopMenu)
ds@1952
   121
	{
ds@1952
   122
		return;
ds@1952
   123
	}
ds@1952
   124
	
ds@1952
   125
	/* Save the Handle */
ds@1952
   126
	hTopMenu = topmenu;
ds@1952
   127
	
ds@1952
   128
	/* Build the "Player Commands" category */
ds@1952
   129
	new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
ds@1952
   130
	
ds@1952
   131
	if (player_commands != INVALID_TOPMENUOBJECT)
ds@1952
   132
	{
ds@1952
   133
		AddToTopMenu(hTopMenu, 
ds@1952
   134
			"sm_kick",
ds@1952
   135
			TopMenuObject_Item,
ds@1952
   136
			AdminMenu_Kick,
ds@1952
   137
			player_commands,
ds@1952
   138
			"sm_kick",
ds@1952
   139
			ADMFLAG_KICK);
ds@1952
   140
			
ds@1952
   141
		AddToTopMenu(hTopMenu,
ds@1952
   142
			"sm_who",
ds@1952
   143
			TopMenuObject_Item,
ds@1952
   144
			AdminMenu_Who,
ds@1952
   145
			player_commands,
ds@1952
   146
			"sm_who",
ds@1952
   147
			ADMFLAG_GENERIC);
ds@1952
   148
	}
ds@1952
   149
ds@1952
   150
	new TopMenuObject:server_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_SERVERCOMMANDS);
ds@1952
   151
ds@1952
   152
	if (server_commands != INVALID_TOPMENUOBJECT)
ds@1952
   153
	{
ds@1952
   154
		AddToTopMenu(hTopMenu,
ds@1952
   155
			"sm_reloadadmins",
ds@1952
   156
			TopMenuObject_Item,
ds@1952
   157
			AdminMenu_ReloadAdmins,
ds@1952
   158
			server_commands,
ds@1952
   159
			"sm_reloadadmins",
ds@1952
   160
			ADMFLAG_BAN);
ds@1952
   161
			
ds@1952
   162
		AddToTopMenu(hTopMenu,
ds@1952
   163
			"sm_map",
ds@1952
   164
			TopMenuObject_Item,
ds@1952
   165
			AdminMenu_Map,
ds@1952
   166
			server_commands,
ds@1952
   167
			"sm_map",
ds@1952
   168
			ADMFLAG_CHANGEMAP);
ds@1952
   169
			
ds@1952
   170
		AddToTopMenu(hTopMenu,
ds@1952
   171
			"sm_execcfg",
ds@1952
   172
			TopMenuObject_Item,
ds@1952
   173
			AdminMenu_ExecCFG,
ds@1952
   174
			server_commands,
ds@1952
   175
			"sm_execcfg",
ds@1952
   176
			ADMFLAG_CONFIG);		
ds@1952
   177
	}
ds@1952
   178
ds@1952
   179
	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
ds@1952
   180
ds@1952
   181
	if (voting_commands != INVALID_TOPMENUOBJECT)
ds@1952
   182
	{
ds@1952
   183
		AddToTopMenu(hTopMenu,
ds@1952
   184
			"sm_cancelvote",
ds@1952
   185
			TopMenuObject_Item,
ds@1952
   186
			AdminMenu_CancelVote,
ds@1952
   187
			voting_commands,
ds@1952
   188
			"sm_cancelvote",
ds@1952
   189
			ADMFLAG_VOTE);
ds@1952
   190
	}
ds@1952
   191
}
ds@1952
   192
ds@1952
   193
public OnLibraryRemoved(const String:name[])
ds@1952
   194
{
ds@1952
   195
	if (strcmp(name, "adminmenu") == 0)
ds@1952
   196
	{
ds@1952
   197
		hTopMenu = INVALID_HANDLE;
ds@1952
   198
	}
ds@1952
   199
}
ds@1952
   200
ds@1952
   201
#define FLAG_STRINGS		14
ds@1952
   202
new String:g_FlagNames[FLAG_STRINGS][20] =
ds@1952
   203
{
ds@1952
   204
	"res",
ds@1952
   205
	"admin",
ds@1952
   206
	"kick",
ds@1952
   207
	"ban",
ds@1952
   208
	"unban",
ds@1952
   209
	"slay",
ds@1952
   210
	"map",
ds@1952
   211
	"cvars",
ds@1952
   212
	"cfg",
ds@1952
   213
	"chat",
ds@1952
   214
	"vote",
ds@1952
   215
	"pass",
ds@1952
   216
	"rcon",
ds@1952
   217
	"cheat"
ds@1952
   218
};
ds@1952
   219
ds@1952
   220
CustomFlagsToString(String:buffer[], maxlength, flags)
ds@1952
   221
{
ds@1952
   222
	decl String:joins[6][6];
ds@1952
   223
	new total;
ds@1952
   224
	
ds@1952
   225
	for (new i=_:Admin_Custom1; i<=_:Admin_Custom6; i++)
ds@1952
   226
	{
ds@1952
   227
		if (flags & (1<<i))
ds@1952
   228
		{
ds@1952
   229
			IntToString(i - _:Admin_Custom1 + 1, joins[total++], 6);
ds@1952
   230
		}
ds@1952
   231
	}
ds@1952
   232
	
ds@1952
   233
	ImplodeStrings(joins, total, ",", buffer, maxlength);
ds@1952
   234
	
ds@1952
   235
	return total;
ds@1952
   236
}
ds@1952
   237
ds@1952
   238
FlagsToString(String:buffer[], maxlength, flags)
ds@1952
   239
{
ds@1952
   240
	decl String:joins[FLAG_STRINGS+1][32];
ds@1952
   241
	new total;
ds@1952
   242
ds@1952
   243
	for (new i=0; i<FLAG_STRINGS; i++)
ds@1952
   244
	{
ds@1952
   245
		if (flags & (1<<i))
ds@1952
   246
		{
ds@1952
   247
			strcopy(joins[total++], 32, g_FlagNames[i]);
ds@1952
   248
		}
ds@1952
   249
	}
ds@1952
   250
	
ds@1952
   251
	decl String:custom_flags[32];
ds@1952
   252
	if (CustomFlagsToString(custom_flags, sizeof(custom_flags), flags))
ds@1952
   253
	{
ds@1952
   254
		Format(joins[total++], 32, "custom(%s)", custom_flags);
ds@1952
   255
	}
ds@1952
   256
ds@1952
   257
	ImplodeStrings(joins, total, ", ", buffer, maxlength);
ds@1952
   258
}
ds@1952
   259
ds@1952
   260
public Action:Command_Cvar(client, args)
ds@1952
   261
{
ds@1952
   262
	if (args < 1)
ds@1952
   263
	{
ds@1952
   264
		if (client == 0)
ds@1952
   265
		{
ds@1952
   266
			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar|protect> [value]");
ds@1952
   267
		}
ds@1952
   268
		else
ds@1952
   269
		{
ds@1952
   270
			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
ds@1952
   271
		}
ds@1952
   272
		return Plugin_Handled;
ds@1952
   273
	}
ds@1952
   274
ds@1952
   275
	decl String:cvarname[64];
ds@1952
   276
	GetCmdArg(1, cvarname, sizeof(cvarname));
ds@1952
   277
	
ds@1952
   278
	if (client == 0 && StrEqual(cvarname, "protect"))
ds@1952
   279
	{
ds@1952
   280
		if (args < 2)
ds@1952
   281
		{
ds@1952
   282
			ReplyToCommand(client, "[SM] Usage: sm_cvar <protect> <cvar>");
ds@1952
   283
			return Plugin_Handled;
ds@1952
   284
		}
ds@1952
   285
		GetCmdArg(2, cvarname, sizeof(cvarname));
ds@1952
   286
		ProtectVar(cvarname);
ds@1952
   287
		ReplyToCommand(client, "[SM] %t", "Cvar is now protected", cvarname);
ds@1952
   288
		return Plugin_Handled;
ds@1952
   289
	}
ds@1952
   290
ds@1952
   291
	new Handle:hndl = FindConVar(cvarname);
ds@1952
   292
	if (hndl == INVALID_HANDLE)
ds@1952
   293
	{
ds@1952
   294
		ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
ds@1952
   295
		return Plugin_Handled;
ds@1952
   296
	}
ds@1952
   297
	
ds@1952
   298
	new bool:allowed = false;
ds@1952
   299
	new client_flags = client == 0 ? ADMFLAG_ROOT : GetUserFlagBits(client);
ds@1952
   300
	
ds@1952
   301
	if (client_flags & ADMFLAG_ROOT)
ds@1952
   302
	{
ds@1952
   303
		allowed = true;
ds@1952
   304
	}
ds@1952
   305
	else
ds@1952
   306
	{
ds@1952
   307
		if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
ds@1952
   308
		{
ds@1952
   309
			allowed = ((client_flags & ADMFLAG_PASSWORD) == ADMFLAG_PASSWORD);
ds@1952
   310
		}
ds@1952
   311
		else if (StrEqual(cvarname, "sv_cheats"))
ds@1952
   312
		{
ds@1952
   313
			allowed = ((client_flags & ADMFLAG_CHEATS) == ADMFLAG_CHEATS);
ds@1952
   314
		}
ds@1952
   315
		else if (!IsVarProtected(cvarname))
ds@1952
   316
		{
ds@1952
   317
			allowed = true;
ds@1952
   318
		}
ds@1952
   319
	}
ds@1952
   320
	
ds@1952
   321
	if (!allowed)
ds@1952
   322
	{
ds@1952
   323
		ReplyToCommand(client, "[SM] %t", "No access to cvar");
ds@1952
   324
		return Plugin_Handled;
ds@1952
   325
	}
ds@1952
   326
ds@1952
   327
	decl String:value[255];
ds@1952
   328
	if (args < 2)
ds@1952
   329
	{
ds@1952
   330
		GetConVarString(hndl, value, sizeof(value));
ds@1952
   331
ds@1952
   332
		ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
ds@1952
   333
		return Plugin_Handled;
ds@1952
   334
	}
ds@1952
   335
ds@1952
   336
	GetCmdArg(2, value, sizeof(value));
ds@1952
   337
ds@1952
   338
	if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
ds@1952
   339
	{
ds@1952
   340
		ShowActivity2(client, "[SM] ", "%t", "Cvar changed", cvarname, value);
ds@1952
   341
	}
ds@1952
   342
	else
ds@1952
   343
	{
ds@1952
   344
		ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
ds@1952
   345
	}
ds@1952
   346
ds@1952
   347
	LogAction(client, -1, "\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
ds@1952
   348
ds@1952
   349
	SetConVarString(hndl, value, true);
ds@1952
   350
ds@1952
   351
	return Plugin_Handled;
ds@1952
   352
}
ds@1952
   353
ds@1952
   354
public Action:Command_Rcon(client, args)
ds@1952
   355
{
ds@1952
   356
	if (args < 1)
ds@1952
   357
	{
ds@1952
   358
		ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
ds@1952
   359
		return Plugin_Handled;
ds@1952
   360
	}
ds@1952
   361
ds@1952
   362
	decl String:argstring[255];
ds@1952
   363
	GetCmdArgString(argstring, sizeof(argstring));
ds@1952
   364
ds@1952
   365
	LogAction(client, -1, "\"%L\" console command (cmdline \"%s\")", client, argstring);
ds@1952
   366
ds@1952
   367
	ServerCommand("%s", argstring);
ds@1952
   368
ds@1952
   369
	return Plugin_Handled;
ds@1952
   370
}