changed SOUND_FROM_PLAYER from -1 to -2 to maintain compatibility with SOUND_FROM_LOCAL_PLAYER
3 * Implements basic admin commands.
4 * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
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.
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.
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.
27 public Plugin:myinfo =
29 name = "Basic Commands",
30 author = "AlliedModders LLC",
31 description = "Basic Admin Commands",
32 version = SOURCEMOD_VERSION,
33 url = "http://www.sourcemod.net/"
36 new Handle:hBanForward = INVALID_HANDLE;
37 new Handle:hAddBanForward = INVALID_HANDLE;
38 new Handle:hBanRemoved = INVALID_HANDLE;
40 public OnPluginStart()
42 LoadTranslations("common.phrases");
43 LoadTranslations("plugin.basecommands");
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]");
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);
60 public Action:Command_BanIp(client, args)
64 ReplyToCommand(client, "[SM] Usage: sm_banip <time> <ip> [reason]");
65 return Plugin_Handled;
68 new String:arg[50], String:time[20];
69 GetCmdArg(1, time, sizeof(time));
70 GetCmdArg(2, arg, sizeof(arg));
72 new minutes = StringToInt(time);
74 new String:reason[128];
77 GetCmdArg(3, reason, sizeof(reason));
80 new Action:act = Plugin_Continue;
81 Call_StartForward(hAddBanForward);
82 Call_PushCell(client);
84 Call_PushCell(minutes);
85 Call_PushString(reason);
88 if (act < Plugin_Handled)
90 LogMessage("\"%L\" added ban (minutes \"%d\") (ip \"%s\") (reason \"%s\")", client, minutes, arg, reason);
91 ReplyToCommand(client, "[SM] %t", "Ban added");
94 if (act < Plugin_Stop)
96 ServerCommand("banip %d %s", minutes, arg);
97 ServerCommand("writeip");
100 return Plugin_Handled;
103 public Action:Command_AddBan(client, args)
107 ReplyToCommand(client, "[SM] Usage: sm_addban <time> <steamid> [reason]");
108 return Plugin_Handled;
111 new String:arg[50], String:time[20];
112 GetCmdArg(1, time, sizeof(time));
113 GetCmdArg(2, arg, sizeof(arg));
115 new minutes = StringToInt(time);
117 new String:reason[128];
120 GetCmdArg(3, reason, sizeof(reason));
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);
131 if (act < Plugin_Handled)
133 LogMessage("\"%L\" added ban (minutes \"%d\") (id \"%s\") (reason \"%s\")", client, minutes, arg, reason);
134 ReplyToCommand(client, "[SM] %t", "Ban added");
137 if (act < Plugin_Stop)
139 ServerCommand("banid %d %s", minutes, arg);
140 ServerCommand("writeid");
143 return Plugin_Handled;
146 public Action:Command_Unban(client, args)
150 ReplyToCommand(client, "[SM] Usage: sm_unban <steamid>");
151 return Plugin_Handled;
154 new String:arg[50], start=0;
155 new String:new_arg[50];
156 GetCmdArgString(arg, sizeof(arg));
158 if (strncmp(arg, "STEAM_0:", 8) == 0)
161 } else if (strncmp(arg, "0:1:", 4) == 0 || strncmp(arg, "0:0:", 4) == 0)
166 Format(new_arg, sizeof(new_arg), "STEAM_0:%s", arg[start]);
168 new Action:act = Plugin_Continue;
169 Call_StartForward(hBanRemoved);
170 Call_PushCell(client);
171 Call_PushString(new_arg);
174 if (act < Plugin_Handled)
176 LogMessage("\"%L\" removed ban (filter \"%s\")", client, arg[start]);
177 ReplyToCommand(client, "[SM] %t", "Removed bans matching", arg);
180 if (act < Plugin_Stop)
182 ServerCommand("removeid \"%s\"", arg[start]);
183 ServerCommand("writeid");
186 return Plugin_Handled;
189 public Action:Command_Ban(client, args)
193 ReplyToCommand(client, "[SM] Usage: sm_ban <#userid|name> <minutes|0> [reason]");
194 return Plugin_Handled;
198 GetCmdArg(1, arg, sizeof(arg));
201 new numClients = SearchForClients(arg, clients, 2);
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;
218 new String:s_time[12];
219 GetCmdArg(2, s_time, sizeof(s_time));
221 new time = StringToInt(s_time);
223 decl String:reason[128];
226 GetCmdArg(3, reason, sizeof(reason));
231 new userid = GetClientUserId(clients[0]);
232 GetClientName(clients[0], arg, sizeof(arg));
234 /* Fire the ban forward */
235 new Action:act = Plugin_Continue;
236 Call_StartForward(hBanForward);
237 Call_PushCell(client);
238 Call_PushCell(clients[0]);
240 Call_PushString(reason);
243 if (act < Plugin_Handled)
247 if (reason[0] == '\0')
249 ShowActivity(client, "%t", "Permabanned player", arg);
251 ShowActivity(client, "%t", "Permabanned player reason", arg, reason);
254 if (reason[0] == '\0')
256 ShowActivity(client, "%t", "Banned player", arg, time);
258 ShowActivity(client, "%t", "Banned player reason", arg, time, reason);
261 LogMessage("\"%L\" banned \"%L\" (minutes \"%d\") (reason \"%s\")", client, clients[0], time, reason);
264 if (act < Plugin_Stop)
266 if (reason[0] == '\0')
268 strcopy(reason, sizeof(reason), "Banned");
271 ServerCommand("banid %d %d", time, userid);
272 KickClient(clients[0], "%s", reason);
276 ServerCommand("writeid");
280 return Plugin_Handled;
283 #define FLAG_STRINGS 14
284 new String:g_FlagNames[FLAG_STRINGS][20] =
302 FlagsToString(String:buffer[], maxlength, flags)
304 new String:joins[FLAG_STRINGS][20];
307 for (new i=0; i<FLAG_STRINGS; i++)
311 strcopy(joins[total++], 20, g_FlagNames[i]);
315 ImplodeStrings(joins, total, ", ", buffer, maxlength);
318 public Action:Command_Who(client, args)
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);
327 PrintToConsole(client, "%-24.23s %s", t_name, t_access);
329 /* List all players */
330 new maxClients = GetMaxClients();
331 new String:flagstring[255];
333 for (new i=1; i<=maxClients; i++)
335 if (!IsClientInGame(i))
339 new flags = GetUserFlagBits(i);
342 strcopy(flagstring, sizeof(flagstring), "none");
343 } else if (flags & ADMFLAG_ROOT) {
344 strcopy(flagstring, sizeof(flagstring), "root");
346 FlagsToString(flagstring, sizeof(flagstring), flags);
348 decl String:name[65];
349 GetClientName(i, name, sizeof(name));
350 PrintToConsole(client, "%d. %-24.23s %s", i, name, flagstring);
353 if (GetCmdReplySource() == SM_REPLY_TO_CHAT)
355 ReplyToCommand(client, "[SM] %t", "See console for output");
358 return Plugin_Handled;
362 GetCmdArg(1, arg, sizeof(arg));
365 new numClients = SearchForClients(arg, clients, 2);
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;
376 new flags = GetUserFlagBits(clients[0]);
377 new String:flagstring[255];
380 strcopy(flagstring, sizeof(flagstring), "none");
381 } else if (flags & ADMFLAG_ROOT) {
382 strcopy(flagstring, sizeof(flagstring), "root");
384 FlagsToString(flagstring, sizeof(flagstring), flags);
387 ReplyToCommand(client, "[SM] %t: %s", "Access", flagstring);
389 return Plugin_Handled;
392 public Action:Command_ExecCfg(client, args)
396 ReplyToCommand(client, "[SM] Usage: sm_execcfg <filename>");
397 return Plugin_Handled;
400 new String:path[64] = "cfg/";
401 GetCmdArg(1, path[4], sizeof(path)-4);
403 if (!FileExists(path))
405 ReplyToCommand(client, "[SM] %t", "Config not found", path[4]);
406 return Plugin_Handled;
409 ShowActivity(client, "%t", "Executed config", path[4]);
411 LogMessage("\"%L\" executed config (file \"%s\")", client, path[4]);
413 ServerCommand("exec \"%s\"", path[4]);
415 return Plugin_Handled;
418 public Action:Command_Cvar(client, args)
422 ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
423 return Plugin_Handled;
426 new String:cvarname[33];
427 GetCmdArg(1, cvarname, sizeof(cvarname));
429 new Handle:hndl = FindConVar(cvarname);
430 if (hndl == INVALID_HANDLE)
432 ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
433 return Plugin_Handled;
436 new bool:allowed = false;
437 if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
439 /* If they're root, allow anything */
440 if ((GetUserFlagBits(client) & ADMFLAG_ROOT) == ADMFLAG_ROOT)
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))
450 /* Do a check for the cheat cvar */
451 else if (StrEqual(cvarname, "sv_cheats"))
453 if (GetUserFlagBits(client) & ADMFLAG_CHEATS
454 || GetUserFlagBits(client) & ADMFLAG_ROOT)
459 /* If we drop down to here, it was a normal cvar. */
467 ReplyToCommand(client, "[SM] %t", "No access to cvar");
468 return Plugin_Handled;
471 decl String:value[255];
474 GetConVarString(hndl, value, sizeof(value));
476 ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
477 return Plugin_Handled;
480 GetCmdArg(2, value, sizeof(value));
482 if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
484 ShowActivity(client, "%t", "Cvar changed", cvarname, value);
486 ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
489 LogMessage("\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
491 SetConVarString(hndl, value, true);
493 return Plugin_Handled;
496 public Action:Command_Rcon(client, args)
500 ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
501 return Plugin_Handled;
504 decl String:argstring[255];
505 GetCmdArgString(argstring, sizeof(argstring));
507 LogMessage("\"%L\" console command (cmdline \"%s\")", client, argstring);
509 ServerCommand("%s", argstring);
511 return Plugin_Handled;
514 public Action:Command_Map(client, args)
518 ReplyToCommand(client, "[SM] Usage: sm_map <map>");
519 return Plugin_Handled;
523 GetCmdArg(1, map, sizeof(map));
525 if (!IsMapValid(map))
527 ReplyToCommand(client, "[SM] %t", "Map was not found", map);
528 return Plugin_Handled;
531 ShowActivity(client, "%t", "Changing map", map);
533 LogMessage("\"%L\" changed map to \"%s\"", client, map);
536 CreateDataTimer(3.0, Timer_ChangeMap, dp);
537 WritePackString(dp, map);
539 return Plugin_Handled;
542 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
547 ReadPackString(dp, map, sizeof(map));
549 ServerCommand("changelevel \"%s\"", map);
554 public Action:Command_Kick(client, args)
558 ReplyToCommand(client, "[SM] Usage: sm_kick <#userid|name> [reason]");
559 return Plugin_Handled;
563 GetCmdArg(1, arg, sizeof(arg));
566 new numClients = SearchForClients(arg, clients, 2);
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;
582 GetClientName(clients[0], name, sizeof(name));
584 decl String:reason[256];
587 /* Safely null terminate */
590 GetCmdArg(2, reason, sizeof(reason));
593 ShowActivity(client, "%t", "Kicked player", name);
595 LogMessage("\"%L\" kicked \"%L\" (reason \"%s\")", client, clients[0], reason);
597 KickClient(clients[0], "%s", reason);
599 return Plugin_Handled;