plugins/basecommands.sp
author Matt Woodrow <pred@alliedmods.net>
Tue Sep 02 07:10:11 2008 +0000 (2008-09-02)
changeset 2248 d433aa792147
parent 2068 ec32dd185d80
child 2289 44045ead61cd
permissions -rw-r--r--
Added a nominations section to maplists.cfg
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");
dvander@2068
    74
	RegConsoleCmd("sm_revote", Command_ReVote);
ds@1952
    75
	
ds@1952
    76
	/* Account for late loading */
ds@1952
    77
	new Handle:topmenu;
ds@1952
    78
	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
ds@1952
    79
	{
ds@1952
    80
		OnAdminMenuReady(topmenu);
ds@1952
    81
	}
ds@1952
    82
	
ds@1952
    83
	g_MapList = CreateMenu(MenuHandler_ChangeMap);
ds@1952
    84
	SetMenuTitle(g_MapList, "Please select a map");
ds@1952
    85
	SetMenuExitBackButton(g_MapList, true);
ds@1952
    86
	
ds@1952
    87
	decl String:mapListPath[PLATFORM_MAX_PATH];
ds@1952
    88
	BuildPath(Path_SM, mapListPath, sizeof(mapListPath), "configs/adminmenu_maplist.ini");
ds@1952
    89
	SetMapListCompatBind("sm_map menu", mapListPath);
ds@1952
    90
	
ds@1952
    91
	g_ProtectedVars = CreateTrie();
ds@1952
    92
	ProtectVar("rcon_password");
ds@1952
    93
	ProtectVar("sm_show_activity");
ds@1952
    94
	ProtectVar("sm_immunity_mode");
ds@1952
    95
}
ds@1952
    96
ds@1952
    97
public OnMapStart()
ds@1952
    98
{
ds@1952
    99
	ParseConfigs();
ds@1952
   100
}
ds@1952
   101
ds@1952
   102
public OnConfigsExecuted()
ds@1952
   103
{
ds@1952
   104
	LoadMapList(g_MapList);
ds@1952
   105
}
ds@1952
   106
ds@1952
   107
ProtectVar(const String:cvar[])
ds@1952
   108
{
ds@1952
   109
	SetTrieValue(g_ProtectedVars, cvar, 1);
ds@1952
   110
}
ds@1952
   111
ds@1952
   112
bool:IsVarProtected(const String:cvar[])
ds@1952
   113
{
ds@1952
   114
	decl dummy_value;
ds@1952
   115
	return GetTrieValue(g_ProtectedVars, cvar, dummy_value);
ds@1952
   116
}
ds@1952
   117
ds@1952
   118
public OnAdminMenuReady(Handle:topmenu)
ds@1952
   119
{
ds@1952
   120
	/* Block us from being called twice */
ds@1952
   121
	if (topmenu == hTopMenu)
ds@1952
   122
	{
ds@1952
   123
		return;
ds@1952
   124
	}
ds@1952
   125
	
ds@1952
   126
	/* Save the Handle */
ds@1952
   127
	hTopMenu = topmenu;
ds@1952
   128
	
ds@1952
   129
	/* Build the "Player Commands" category */
ds@1952
   130
	new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
ds@1952
   131
	
ds@1952
   132
	if (player_commands != INVALID_TOPMENUOBJECT)
ds@1952
   133
	{
ds@1952
   134
		AddToTopMenu(hTopMenu, 
ds@1952
   135
			"sm_kick",
ds@1952
   136
			TopMenuObject_Item,
ds@1952
   137
			AdminMenu_Kick,
ds@1952
   138
			player_commands,
ds@1952
   139
			"sm_kick",
ds@1952
   140
			ADMFLAG_KICK);
ds@1952
   141
			
ds@1952
   142
		AddToTopMenu(hTopMenu,
ds@1952
   143
			"sm_who",
ds@1952
   144
			TopMenuObject_Item,
ds@1952
   145
			AdminMenu_Who,
ds@1952
   146
			player_commands,
ds@1952
   147
			"sm_who",
ds@1952
   148
			ADMFLAG_GENERIC);
ds@1952
   149
	}
ds@1952
   150
ds@1952
   151
	new TopMenuObject:server_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_SERVERCOMMANDS);
ds@1952
   152
ds@1952
   153
	if (server_commands != INVALID_TOPMENUOBJECT)
ds@1952
   154
	{
ds@1952
   155
		AddToTopMenu(hTopMenu,
ds@1952
   156
			"sm_reloadadmins",
ds@1952
   157
			TopMenuObject_Item,
ds@1952
   158
			AdminMenu_ReloadAdmins,
ds@1952
   159
			server_commands,
ds@1952
   160
			"sm_reloadadmins",
ds@1952
   161
			ADMFLAG_BAN);
ds@1952
   162
			
ds@1952
   163
		AddToTopMenu(hTopMenu,
ds@1952
   164
			"sm_map",
ds@1952
   165
			TopMenuObject_Item,
ds@1952
   166
			AdminMenu_Map,
ds@1952
   167
			server_commands,
ds@1952
   168
			"sm_map",
ds@1952
   169
			ADMFLAG_CHANGEMAP);
ds@1952
   170
			
ds@1952
   171
		AddToTopMenu(hTopMenu,
ds@1952
   172
			"sm_execcfg",
ds@1952
   173
			TopMenuObject_Item,
ds@1952
   174
			AdminMenu_ExecCFG,
ds@1952
   175
			server_commands,
ds@1952
   176
			"sm_execcfg",
ds@1952
   177
			ADMFLAG_CONFIG);		
ds@1952
   178
	}
ds@1952
   179
ds@1952
   180
	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
ds@1952
   181
ds@1952
   182
	if (voting_commands != INVALID_TOPMENUOBJECT)
ds@1952
   183
	{
ds@1952
   184
		AddToTopMenu(hTopMenu,
ds@1952
   185
			"sm_cancelvote",
ds@1952
   186
			TopMenuObject_Item,
ds@1952
   187
			AdminMenu_CancelVote,
ds@1952
   188
			voting_commands,
ds@1952
   189
			"sm_cancelvote",
ds@1952
   190
			ADMFLAG_VOTE);
ds@1952
   191
	}
ds@1952
   192
}
ds@1952
   193
ds@1952
   194
public OnLibraryRemoved(const String:name[])
ds@1952
   195
{
ds@1952
   196
	if (strcmp(name, "adminmenu") == 0)
ds@1952
   197
	{
ds@1952
   198
		hTopMenu = INVALID_HANDLE;
ds@1952
   199
	}
ds@1952
   200
}
ds@1952
   201
ds@1952
   202
#define FLAG_STRINGS		14
ds@1952
   203
new String:g_FlagNames[FLAG_STRINGS][20] =
ds@1952
   204
{
ds@1952
   205
	"res",
ds@1952
   206
	"admin",
ds@1952
   207
	"kick",
ds@1952
   208
	"ban",
ds@1952
   209
	"unban",
ds@1952
   210
	"slay",
ds@1952
   211
	"map",
ds@1952
   212
	"cvars",
ds@1952
   213
	"cfg",
ds@1952
   214
	"chat",
ds@1952
   215
	"vote",
ds@1952
   216
	"pass",
ds@1952
   217
	"rcon",
ds@1952
   218
	"cheat"
ds@1952
   219
};
ds@1952
   220
ds@1952
   221
CustomFlagsToString(String:buffer[], maxlength, flags)
ds@1952
   222
{
ds@1952
   223
	decl String:joins[6][6];
ds@1952
   224
	new total;
ds@1952
   225
	
ds@1952
   226
	for (new i=_:Admin_Custom1; i<=_:Admin_Custom6; i++)
ds@1952
   227
	{
ds@1952
   228
		if (flags & (1<<i))
ds@1952
   229
		{
ds@1952
   230
			IntToString(i - _:Admin_Custom1 + 1, joins[total++], 6);
ds@1952
   231
		}
ds@1952
   232
	}
ds@1952
   233
	
ds@1952
   234
	ImplodeStrings(joins, total, ",", buffer, maxlength);
ds@1952
   235
	
ds@1952
   236
	return total;
ds@1952
   237
}
ds@1952
   238
ds@1952
   239
FlagsToString(String:buffer[], maxlength, flags)
ds@1952
   240
{
ds@1952
   241
	decl String:joins[FLAG_STRINGS+1][32];
ds@1952
   242
	new total;
ds@1952
   243
ds@1952
   244
	for (new i=0; i<FLAG_STRINGS; i++)
ds@1952
   245
	{
ds@1952
   246
		if (flags & (1<<i))
ds@1952
   247
		{
ds@1952
   248
			strcopy(joins[total++], 32, g_FlagNames[i]);
ds@1952
   249
		}
ds@1952
   250
	}
ds@1952
   251
	
ds@1952
   252
	decl String:custom_flags[32];
ds@1952
   253
	if (CustomFlagsToString(custom_flags, sizeof(custom_flags), flags))
ds@1952
   254
	{
ds@1952
   255
		Format(joins[total++], 32, "custom(%s)", custom_flags);
ds@1952
   256
	}
ds@1952
   257
ds@1952
   258
	ImplodeStrings(joins, total, ", ", buffer, maxlength);
ds@1952
   259
}
ds@1952
   260
ds@1952
   261
public Action:Command_Cvar(client, args)
ds@1952
   262
{
ds@1952
   263
	if (args < 1)
ds@1952
   264
	{
ds@1952
   265
		if (client == 0)
ds@1952
   266
		{
ds@1952
   267
			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar|protect> [value]");
ds@1952
   268
		}
ds@1952
   269
		else
ds@1952
   270
		{
ds@1952
   271
			ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
ds@1952
   272
		}
ds@1952
   273
		return Plugin_Handled;
ds@1952
   274
	}
ds@1952
   275
ds@1952
   276
	decl String:cvarname[64];
ds@1952
   277
	GetCmdArg(1, cvarname, sizeof(cvarname));
ds@1952
   278
	
ds@1952
   279
	if (client == 0 && StrEqual(cvarname, "protect"))
ds@1952
   280
	{
ds@1952
   281
		if (args < 2)
ds@1952
   282
		{
ds@1952
   283
			ReplyToCommand(client, "[SM] Usage: sm_cvar <protect> <cvar>");
ds@1952
   284
			return Plugin_Handled;
ds@1952
   285
		}
ds@1952
   286
		GetCmdArg(2, cvarname, sizeof(cvarname));
ds@1952
   287
		ProtectVar(cvarname);
ds@1952
   288
		ReplyToCommand(client, "[SM] %t", "Cvar is now protected", cvarname);
ds@1952
   289
		return Plugin_Handled;
ds@1952
   290
	}
ds@1952
   291
ds@1952
   292
	new Handle:hndl = FindConVar(cvarname);
ds@1952
   293
	if (hndl == INVALID_HANDLE)
ds@1952
   294
	{
ds@1952
   295
		ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
ds@1952
   296
		return Plugin_Handled;
ds@1952
   297
	}
ds@1952
   298
	
ds@1952
   299
	new bool:allowed = false;
ds@1952
   300
	new client_flags = client == 0 ? ADMFLAG_ROOT : GetUserFlagBits(client);
ds@1952
   301
	
ds@1952
   302
	if (client_flags & ADMFLAG_ROOT)
ds@1952
   303
	{
ds@1952
   304
		allowed = true;
ds@1952
   305
	}
ds@1952
   306
	else
ds@1952
   307
	{
ds@1952
   308
		if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
ds@1952
   309
		{
ds@1952
   310
			allowed = ((client_flags & ADMFLAG_PASSWORD) == ADMFLAG_PASSWORD);
ds@1952
   311
		}
ds@1952
   312
		else if (StrEqual(cvarname, "sv_cheats"))
ds@1952
   313
		{
ds@1952
   314
			allowed = ((client_flags & ADMFLAG_CHEATS) == ADMFLAG_CHEATS);
ds@1952
   315
		}
ds@1952
   316
		else if (!IsVarProtected(cvarname))
ds@1952
   317
		{
ds@1952
   318
			allowed = true;
ds@1952
   319
		}
ds@1952
   320
	}
ds@1952
   321
	
ds@1952
   322
	if (!allowed)
ds@1952
   323
	{
ds@1952
   324
		ReplyToCommand(client, "[SM] %t", "No access to cvar");
ds@1952
   325
		return Plugin_Handled;
ds@1952
   326
	}
ds@1952
   327
ds@1952
   328
	decl String:value[255];
ds@1952
   329
	if (args < 2)
ds@1952
   330
	{
ds@1952
   331
		GetConVarString(hndl, value, sizeof(value));
ds@1952
   332
ds@1952
   333
		ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
ds@1952
   334
		return Plugin_Handled;
ds@1952
   335
	}
ds@1952
   336
ds@1952
   337
	GetCmdArg(2, value, sizeof(value));
ds@1952
   338
ds@1952
   339
	if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
ds@1952
   340
	{
ds@1952
   341
		ShowActivity2(client, "[SM] ", "%t", "Cvar changed", cvarname, value);
ds@1952
   342
	}
ds@1952
   343
	else
ds@1952
   344
	{
ds@1952
   345
		ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
ds@1952
   346
	}
ds@1952
   347
ds@1952
   348
	LogAction(client, -1, "\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
ds@1952
   349
ds@1952
   350
	SetConVarString(hndl, value, true);
ds@1952
   351
ds@1952
   352
	return Plugin_Handled;
ds@1952
   353
}
ds@1952
   354
ds@1952
   355
public Action:Command_Rcon(client, args)
ds@1952
   356
{
ds@1952
   357
	if (args < 1)
ds@1952
   358
	{
ds@1952
   359
		ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
ds@1952
   360
		return Plugin_Handled;
ds@1952
   361
	}
ds@1952
   362
ds@1952
   363
	decl String:argstring[255];
ds@1952
   364
	GetCmdArgString(argstring, sizeof(argstring));
ds@1952
   365
ds@1952
   366
	LogAction(client, -1, "\"%L\" console command (cmdline \"%s\")", client, argstring);
ds@1952
   367
ds@1952
   368
	ServerCommand("%s", argstring);
ds@1952
   369
ds@1952
   370
	return Plugin_Handled;
ds@1952
   371
}
dvander@2068
   372
dvander@2068
   373
public Action:Command_ReVote(client, args)
dvander@2068
   374
{
dvander@2068
   375
	if (client == 0)
dvander@2068
   376
	{
dvander@2068
   377
		ReplyToCommand(client, "[SM] %t", "Command is in-game only");
dvander@2068
   378
		return Plugin_Handled;
dvander@2068
   379
	}
dvander@2068
   380
	
dvander@2068
   381
	if (!IsVoteInProgress())
dvander@2068
   382
	{
dvander@2068
   383
		ReplyToCommand(client, "[SM] %t", "Vote Not In Progress");
dvander@2068
   384
		return Plugin_Handled;
dvander@2068
   385
	}
dvander@2068
   386
	
dvander@2068
   387
	if (!IsClientInVotePool(client))
dvander@2068
   388
	{
dvander@2068
   389
		ReplyToCommand(client, "[SM] %t", "Cannot participate in vote");
dvander@2068
   390
		return Plugin_Handled;
dvander@2068
   391
	}
dvander@2068
   392
	
dvander@2068
   393
	if (!RedrawClientVoteMenu(client))
dvander@2068
   394
	{
dvander@2068
   395
		ReplyToCommand(client, "[SM] %t", "Cannot change vote");
dvander@2068
   396
	}
dvander@2068
   397
	
dvander@2068
   398
	return Plugin_Handled;
dvander@2068
   399
}