plugins/basecommands.sp
author Scott Ehlert <ds@alliedmods.net>
Fri Jun 22 04:35:27 2007 +0000 (2007-06-22)
changeset 999 8d7e956f996d
parent 989 365bacaddd5b
child 1013 4225e70fba06
permissions -rw-r--r--
don't fix what isn't broken I guess :\ (Fixed reserved slots once and for all...)
     1 /**
     2  * basecommands.sp
     3  * Implements basic admin commands.
     4  * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
     5  *
     6  * This program is free software; you can redistribute it and/or
     7  * modify it under the terms of the GNU General Public License
     8  * as published by the Free Software Foundation; either version 2
     9  * of the License, or (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  * 
    16  * You should have received a copy of the GNU General Public License
    17  * along with this program; if not, write to the Free Software
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    19  *
    20  * Version: $Id$
    21  */
    22 
    23 #pragma semicolon 1
    24 
    25 #include <sourcemod>
    26 
    27 public Plugin:myinfo = 
    28 {
    29 	name = "Basic Commands",
    30 	author = "AlliedModders LLC",
    31 	description = "Basic Admin Commands",
    32 	version = SOURCEMOD_VERSION,
    33 	url = "http://www.sourcemod.net/"
    34 };
    35 
    36 new Handle:hBanForward = INVALID_HANDLE;
    37 new Handle:hAddBanForward = INVALID_HANDLE;
    38 new Handle:hBanRemoved = INVALID_HANDLE;
    39 
    40 public OnPluginStart()
    41 {
    42 	LoadTranslations("common.cfg");
    43 	LoadTranslations("plugin.basecommands.cfg");
    44 	RegAdminCmd("sm_kick", Command_Kick, ADMFLAG_KICK, "sm_kick <#userid|name> [reason]");
    45 	RegAdminCmd("sm_map", Command_Map, ADMFLAG_CHANGEMAP, "sm_map <map>");
    46 	RegAdminCmd("sm_rcon", Command_Rcon, ADMFLAG_RCON, "sm_rcon <args>");
    47 	RegAdminCmd("sm_cvar", Command_Cvar, ADMFLAG_CONVARS, "sm_cvar <cvar> [value]");
    48 	RegAdminCmd("sm_execcfg", Command_ExecCfg, ADMFLAG_CONFIG, "sm_execcfg <filename>");
    49 	RegAdminCmd("sm_who", Command_Who, ADMFLAG_GENERIC, "sm_who [#userid|name]");
    50 	RegAdminCmd("sm_ban", Command_Ban, ADMFLAG_BAN, "sm_ban <#userid|name> <minutes|0> [reason]");
    51 	RegAdminCmd("sm_unban", Command_Unban, ADMFLAG_UNBAN, "sm_unban <steamid>");
    52 	RegAdminCmd("sm_addban", Command_AddBan, ADMFLAG_RCON, "sm_addban <time> <steamid> [reason]");
    53 	RegAdminCmd("sm_banip", Command_BanIp, ADMFLAG_RCON, "sm_banip <time> <ip> [reason]");
    54 	
    55 	hBanForward = CreateGlobalForward("OnClientBanned", ET_Hook, Param_Cell, Param_Cell, Param_Cell, Param_String);
    56 	hAddBanForward = CreateGlobalForward("OnBanAdded", ET_Hook, Param_Cell, Param_String, Param_Cell, Param_String);
    57 	hBanRemoved = CreateGlobalForward("OnBanRemoved", ET_Hook, Param_Cell, Param_String);
    58 }
    59 
    60 public Action:Command_BanIp(client, args)
    61 {
    62 	if (args < 2)
    63 	{
    64 		ReplyToCommand(client, "[SM] Usage: sm_banip <time> <ip> [reason]");
    65 		return Plugin_Handled;
    66 	}
    67 	
    68 	new String:arg[50], String:time[20];
    69 	GetCmdArg(1, time, sizeof(time));
    70 	GetCmdArg(2, arg, sizeof(arg));
    71 	
    72 	new minutes = StringToInt(time);
    73 	
    74 	new String:reason[128];
    75 	if (args >= 3)
    76 	{
    77 		GetCmdArg(3, reason, sizeof(reason));
    78 	}
    79 	
    80 	new Action:act = Plugin_Continue;
    81 	Call_StartForward(hAddBanForward);
    82 	Call_PushCell(client);
    83 	Call_PushString(arg);
    84 	Call_PushCell(minutes);
    85 	Call_PushString(reason);
    86 	Call_Finish(act);
    87 	
    88 	if (act < Plugin_Handled)
    89 	{
    90 		LogMessage("\"%L\" added ban (minutes \"%d\") (ip \"%s\") (reason \"%s\")", client, minutes, arg, reason);
    91 		ReplyToCommand(client, "[SM] %t", "Ban added");
    92 	}
    93 	
    94 	if (act < Plugin_Stop)
    95 	{
    96 		ServerCommand("banip %d %s", minutes, arg);
    97 		ServerCommand("writeip");
    98 	}
    99 	
   100 	return Plugin_Handled;
   101 }
   102 
   103 public Action:Command_AddBan(client, args)
   104 {
   105 	if (args < 2)
   106 	{
   107 		ReplyToCommand(client, "[SM] Usage: sm_addban <time> <steamid> [reason]");
   108 		return Plugin_Handled;
   109 	}
   110 	
   111 	new String:arg[50], String:time[20];
   112 	GetCmdArg(1, time, sizeof(time));
   113 	GetCmdArg(2, arg, sizeof(arg));
   114 	
   115 	new minutes = StringToInt(time);
   116 	
   117 	new String:reason[128];
   118 	if (args >= 3)
   119 	{
   120 		GetCmdArg(3, reason, sizeof(reason));
   121 	}
   122 	
   123 	new Action:act = Plugin_Continue;
   124 	Call_StartForward(hAddBanForward);
   125 	Call_PushCell(client);
   126 	Call_PushString(arg);
   127 	Call_PushCell(minutes);
   128 	Call_PushString(reason);
   129 	Call_Finish(act);
   130 	
   131 	if (act < Plugin_Handled)
   132 	{
   133 		LogMessage("\"%L\" added ban (minutes \"%d\") (id \"%s\") (reason \"%s\")", client, minutes, arg, reason);
   134 		ReplyToCommand(client, "[SM] %t", "Ban added");
   135 	}
   136 	
   137 	if (act < Plugin_Stop)
   138 	{
   139 		ServerCommand("banid %d %s", minutes, arg);
   140 		ServerCommand("writeid");
   141 	}
   142 	
   143 	return Plugin_Handled;
   144 }
   145 
   146 public Action:Command_Unban(client, args)
   147 {
   148 	if (args < 1)
   149 	{
   150 		ReplyToCommand(client, "[SM] Usage: sm_unban <steamid>");
   151 		return Plugin_Handled;
   152 	}
   153 	
   154 	new String:arg[50], start=0;
   155 	new String:new_arg[50];
   156 	GetCmdArgString(arg, sizeof(arg));
   157 	
   158 	if (strncmp(arg, "STEAM_0:", 8) == 0)
   159 	{
   160 		start = 8;
   161 	} else if (strncmp(arg, "0:1:", 4) == 0 || strncmp(arg, "0:0:", 4) == 0)
   162 	{
   163 		start = 2;
   164 	}
   165 	
   166 	Format(new_arg, sizeof(new_arg), "STEAM_0:%s", arg[start]);
   167 	
   168 	new Action:act = Plugin_Continue;
   169 	Call_StartForward(hBanRemoved);
   170 	Call_PushCell(client);
   171 	Call_PushString(new_arg);
   172 	Call_Finish();
   173 	
   174 	if (act < Plugin_Handled)
   175 	{
   176 		LogMessage("\"%L\" removed ban (filter \"%s\")", client, arg[start]);
   177 		ReplyToCommand(client, "[SM] %t", "Removed bans matching", arg);
   178 	}
   179 	
   180 	if (act < Plugin_Stop)
   181 	{
   182 		ServerCommand("removeid \"%s\"", arg[start]);
   183 		ServerCommand("writeid");
   184 	}
   185 	
   186 	return Plugin_Handled;
   187 }
   188 
   189 public Action:Command_Ban(client, args)
   190 {
   191 	if (args < 2)
   192 	{
   193 		ReplyToCommand(client, "[SM] Usage: sm_ban <#userid|name> <minutes|0> [reason]");
   194 		return Plugin_Handled;
   195 	}
   196 	
   197 	new String:arg[65];
   198 	GetCmdArg(1, arg, sizeof(arg));
   199 	
   200 	new clients[2];
   201 	new numClients = SearchForClients(arg, clients, 2);
   202 	
   203 	if (numClients == 0)
   204 	{
   205 		ReplyToCommand(client, "[SM] %t", "No matching client");
   206 		return Plugin_Handled;
   207 	} else if (numClients > 1) {
   208 		ReplyToCommand(client, "[SM] %t", "More than one client matches", arg);
   209 		return Plugin_Handled;
   210 	} else if (!CanUserTarget(client, clients[0])) {
   211 		ReplyToCommand(client, "[SM] %t", "Unable to target");
   212 		return Plugin_Handled;
   213 	} else if (IsFakeClient(clients[0])) {
   214 		ReplyToCommand(client, "[SM] %t", "Cannot target bot");
   215 		return Plugin_Handled;
   216 	}
   217 	
   218 	new String:s_time[12];
   219 	GetCmdArg(2, s_time, sizeof(s_time));
   220 	
   221 	new time = StringToInt(s_time);
   222 	
   223 	decl String:reason[128];
   224 	if (args >= 3)
   225 	{
   226 		GetCmdArg(3, reason, sizeof(reason));
   227 	} else {
   228 		reason[0] = '\0';
   229 	}
   230 	
   231 	new userid = GetClientUserId(clients[0]);
   232 	GetClientName(clients[0], arg, sizeof(arg));
   233 	
   234 	/* Fire the ban forward */
   235 	new Action:act = Plugin_Continue;
   236 	Call_StartForward(hBanForward);
   237 	Call_PushCell(client);
   238 	Call_PushCell(clients[0]);
   239 	Call_PushCell(time);
   240 	Call_PushString(reason);
   241 	Call_Finish();
   242 	
   243 	if (act < Plugin_Handled)
   244 	{
   245 		if (!time)
   246 		{
   247 			if (reason[0] == '\0')
   248 			{
   249 				ShowActivity(client, "%t", "Permabanned player", arg);
   250 			} else {
   251 				ShowActivity(client, "%t", "Permabanned player reason", arg, reason);
   252 			}
   253 		} else {
   254 			if (reason[0] == '\0')
   255 			{
   256 				ShowActivity(client, "%t", "Banned player", arg, time);
   257 			} else {
   258 				ShowActivity(client, "%t", "Banned player reason", arg, time, reason);
   259 			}
   260 		}
   261 		LogMessage("\"%L\" banned \"%L\" (minutes \"%d\") (reason \"%s\")", client, clients[0], time, reason);
   262 	}
   263 
   264 	if (act < Plugin_Stop)
   265 	{
   266 		if (reason[0] == '\0')
   267 		{
   268 			strcopy(reason, sizeof(reason), "Banned");
   269 		}
   270 		
   271 		ServerCommand("banid %d %d", time, userid);
   272 		ServerCommand("kickid %d \"%s\"", userid, reason);
   273 		
   274 		if (time == 0)
   275 		{
   276 			ServerCommand("writeid");
   277 		}
   278 	}
   279 	
   280 	return Plugin_Handled;
   281 }
   282 
   283 #define FLAG_STRINGS		14
   284 new String:g_FlagNames[FLAG_STRINGS][20] =
   285 {
   286 	"reservation",
   287 	"admin",
   288 	"kick",
   289 	"ban",
   290 	"unban",
   291 	"slay",
   292 	"map",
   293 	"cvars",
   294 	"cfg",
   295 	"chat",
   296 	"vote",
   297 	"pass",
   298 	"rcon",
   299 	"cheat"
   300 };
   301 
   302 FlagsToString(String:buffer[], maxlength, flags)
   303 {
   304 	new String:joins[FLAG_STRINGS][20];
   305 	new total;
   306 	
   307 	for (new i=0; i<FLAG_STRINGS; i++)
   308 	{
   309 		if (flags & (1<<i))
   310 		{
   311 			strcopy(joins[total++], 20, g_FlagNames[i]);
   312 		}
   313 	}
   314 	
   315 	ImplodeStrings(joins, total, ", ", buffer, maxlength);
   316 }
   317 
   318 public Action:Command_Who(client, args)
   319 {
   320 	if (args < 1)
   321 	{
   322 		/* Display header */
   323 		new String:t_access[16], String:t_name[16];
   324 		Format(t_access, sizeof(t_access), "%t", "Access", client);
   325 		Format(t_name, sizeof(t_name), "%t", "Name", client);
   326 		
   327 		PrintToConsole(client, "%-24.23s %s", t_name, t_access);
   328 		
   329 		/* List all players */
   330 		new maxClients = GetMaxClients();
   331 		new String:flagstring[255];
   332 				
   333 		for (new i=1; i<=maxClients; i++)
   334 		{
   335 			if (!IsClientInGame(i))
   336 			{
   337 				continue;
   338 			}
   339 			new flags = GetUserFlagBits(i);
   340 			if (flags == 0)
   341 			{
   342 				strcopy(flagstring, sizeof(flagstring), "none");
   343 			} else if (flags & ADMFLAG_ROOT) {
   344 				strcopy(flagstring, sizeof(flagstring), "root");
   345 			} else {
   346 				FlagsToString(flagstring, sizeof(flagstring), flags);
   347 			}
   348 			decl String:name[65];
   349 			GetClientName(i, name, sizeof(name));
   350 			PrintToConsole(client, "%d. %-24.23s %s", i, name, flagstring);
   351 		}
   352 		
   353 		if (GetCmdReplySource() == SM_REPLY_TO_CHAT)
   354 		{
   355 			ReplyToCommand(client, "[SM] %t", "See console for output");
   356 		}
   357 		
   358 		return Plugin_Handled;
   359 	}
   360 	
   361 	new String:arg[65];
   362 	GetCmdArg(1, arg, sizeof(arg));
   363 	
   364 	new clients[2];
   365 	new numClients = SearchForClients(arg, clients, 2);
   366 	
   367 	if (numClients == 0)
   368 	{
   369 		ReplyToCommand(client, "[SM] %t", "No matching client");
   370 		return Plugin_Handled;
   371 	} else if (numClients > 1) {
   372 		ReplyToCommand(client, "[SM] %t", "More than one client matches", arg);
   373 		return Plugin_Handled;
   374 	}
   375 	
   376 	new flags = GetUserFlagBits(clients[0]);
   377 	new String:flagstring[255];
   378 	if (flags == 0)
   379 	{
   380 		strcopy(flagstring, sizeof(flagstring), "none");
   381 	} else if (flags & ADMFLAG_ROOT) {
   382 		strcopy(flagstring, sizeof(flagstring), "root");
   383 	} else {
   384 		FlagsToString(flagstring, sizeof(flagstring), flags);
   385 	}
   386 	
   387 	ReplyToCommand(client, "[SM] %t: %s", "Access", flagstring);
   388 	
   389 	return Plugin_Handled;
   390 }
   391 
   392 public Action:Command_ExecCfg(client, args)
   393 {
   394 	if (args < 1)
   395 	{
   396 		ReplyToCommand(client, "[SM] Usage: sm_execcfg <filename>");
   397 		return Plugin_Handled;
   398 	}
   399 	
   400 	new String:path[64] = "cfg/";
   401 	GetCmdArg(1, path[4], sizeof(path)-4);
   402 	
   403 	if (!FileExists(path))
   404 	{
   405 		ReplyToCommand(client, "[SM] %t", "Config not found", path[4]);
   406 		return Plugin_Handled;
   407 	}
   408 	
   409 	ShowActivity(client, "%t", "Executed config", path[4]);
   410 	
   411 	LogMessage("\"%L\" executed config (file \"%s\")", client, path[4]);
   412 		
   413 	ServerCommand("exec \"%s\"", path[4]);
   414 	
   415 	return Plugin_Handled;
   416 }
   417 
   418 public Action:Command_Cvar(client, args)
   419 {
   420 	if (args < 1)
   421 	{
   422 		ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
   423 		return Plugin_Handled;
   424 	}
   425 	
   426 	new String:cvarname[33];
   427 	GetCmdArg(1, cvarname, sizeof(cvarname));
   428 	
   429 	new Handle:hndl = FindConVar(cvarname);
   430 	if (hndl == INVALID_HANDLE)
   431 	{
   432 		ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
   433 		return Plugin_Handled;
   434 	}
   435 	
   436 	new bool:allowed = false;
   437 	if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
   438 	{
   439 		/* If they're root, allow anything */
   440 		if ((GetUserFlagBits(client) & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   441 		{
   442 			allowed = true;
   443 		}
   444 		/* If they're not root, and getting sv_password, see if they have ADMFLAG_PASSWORD */
   445 		else if (StrEqual(cvarname, "sv_password") && (GetUserFlagBits(client) & ADMFLAG_PASSWORD))
   446 		{
   447 			allowed = true;
   448 		}
   449 	} 
   450 	/* Do a check for the cheat cvar */
   451 	else if (StrEqual(cvarname, "sv_cheats"))
   452 	{
   453 		if (GetUserFlagBits(client) & ADMFLAG_CHEATS
   454 			|| GetUserFlagBits(client) & ADMFLAG_ROOT)
   455 		{
   456 			allowed = true;
   457 		}
   458 	}
   459 	/* If we drop down to here, it was a normal cvar. */
   460 	else
   461 	{
   462 		allowed = true;
   463 	}
   464 	
   465 	if (!allowed)
   466 	{
   467 		ReplyToCommand(client, "[SM] %t", "No access to cvar");
   468 		return Plugin_Handled;
   469 	}
   470 	
   471 	decl String:value[255];
   472 	if (args < 2)
   473 	{
   474 		GetConVarString(hndl, value, sizeof(value));
   475 		
   476 		ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
   477 		return Plugin_Handled;
   478 	}
   479 	
   480 	GetCmdArg(2, value, sizeof(value));
   481 	
   482 	if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
   483 	{
   484 		ShowActivity(client, "%t", "Cvar changed", cvarname, value);
   485 	} else {
   486 		ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
   487 	}
   488 	
   489 	LogMessage("\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
   490 	
   491 	SetConVarString(hndl, value, true);
   492 		
   493 	return Plugin_Handled;
   494 }
   495 
   496 public Action:Command_Rcon(client, args)
   497 {
   498 	if (args < 1)
   499 	{
   500 		ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
   501 		return Plugin_Handled;
   502 	}
   503 	
   504 	decl String:argstring[255];
   505 	GetCmdArgString(argstring, sizeof(argstring));
   506 	
   507 	LogMessage("\"%L\" console command (cmdline \"%s\")", client, argstring);
   508 	
   509 	ServerCommand("%s", argstring);
   510 	
   511 	return Plugin_Handled;
   512 }
   513 
   514 public Action:Command_Map(client, args)
   515 {
   516 	if (args < 1)
   517 	{
   518 		ReplyToCommand(client, "[SM] Usage: sm_map <map>");
   519 		return Plugin_Handled;
   520 	}
   521 	
   522 	new String:map[64];
   523 	GetCmdArg(1, map, sizeof(map));
   524 	
   525 	if (!IsMapValid(map))
   526 	{
   527 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
   528 		return Plugin_Handled;
   529 	}
   530 	
   531 	ShowActivity(client, "%t", "Changing map", map);
   532 	
   533 	LogMessage("\"%L\" changed map to \"%s\"", client, map);
   534 	
   535 	new Handle:dp;
   536 	CreateDataTimer(3.0, Timer_ChangeMap, dp);
   537 	WritePackString(dp, map);
   538 	
   539 	return Plugin_Handled;
   540 }
   541 
   542 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   543 {
   544 	new String:map[65];
   545 	
   546 	ResetPack(dp);
   547 	ReadPackString(dp, map, sizeof(map));
   548 	
   549 	ServerCommand("changelevel \"%s\"", map);
   550 	
   551 	return Plugin_Stop;
   552 }
   553 
   554 public Action:Command_Kick(client, args)
   555 {
   556 	if (args < 1)
   557 	{
   558 		ReplyToCommand(client, "[SM] Usage: sm_kick <#userid|name> [reason]");
   559 		return Plugin_Handled;
   560 	}
   561 	
   562 	new String:arg[65];
   563 	GetCmdArg(1, arg, sizeof(arg));
   564 	
   565 	new clients[2];
   566 	new numClients = SearchForClients(arg, clients, 2);
   567 	
   568 	if (numClients == 0)
   569 	{
   570 		ReplyToCommand(client, "[SM] %t", "No matching client");
   571 		return Plugin_Handled;
   572 	} else if (numClients > 1) {
   573 		ReplyToCommand(client, "[SM] %t", "More than one client matches", arg);
   574 		return Plugin_Handled;
   575 	} else if (!CanUserTarget(client, clients[0])) {
   576 		ReplyToCommand(client, "[SM] %t", "Unable to target");
   577 		return Plugin_Handled;
   578 	}
   579 	
   580 	new userid = GetClientUserId(clients[0]);
   581 	new String:name[65];
   582 	
   583 	GetClientName(clients[0], name, sizeof(name));
   584 	
   585 	decl String:reason[256];
   586 	if (args < 2)
   587 	{
   588 		/* Safely null terminate */
   589 		reason[0] = '\0';
   590 	} else {
   591 		GetCmdArg(2, reason, sizeof(reason));
   592 	}
   593 	
   594 	ShowActivity(client, "%t", "Kicked player", name);
   595 	
   596 	LogMessage("\"%L\" kicked \"%L\" (reason \"%s\")", client, clients[0], reason);
   597 	
   598 	if (args < 2)
   599 	{
   600 		ServerCommand("kickid %d", userid);
   601 	} else {
   602 		ServerCommand("kickid %d \"%s\"", userid, reason);
   603 	}
   604 	
   605 	return Plugin_Handled;
   606 }