3 * =============================================================================
4 * SourceMod Basic Commands Plugin
5 * Implements basic admin commands.
7 * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
8 * =============================================================================
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.
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
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/>.
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>.
40 public Plugin:myinfo =
42 name = "Basic Ban Commands",
43 author = "AlliedModders LLC",
44 description = "Basic Banning Commands",
45 version = SOURCEMOD_VERSION,
46 url = "http://www.sourcemod.net/"
49 new Handle:hTopMenu = INVALID_HANDLE;
51 new g_BanTarget[MAXPLAYERS+1];
52 new g_BanTargetUserId[MAXPLAYERS+1];
53 new g_BanTime[MAXPLAYERS+1];
55 new g_IsWaitingForChatReason[MAXPLAYERS+1];
56 new Handle:g_hKvBanReasons;
57 new String:g_BanReasonsPath[PLATFORM_MAX_PATH];
59 #include "basebans/ban.sp"
61 public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
63 BuildPath(Path_SM, g_BanReasonsPath, sizeof(g_BanReasonsPath), "configs/banreasons.txt");
66 return APLRes_Success;
70 Format(error, err_max, "Couldn't load file %s: See log for details.", g_BanReasonsPath);
71 return APLRes_Failure;
75 public OnPluginStart()
77 LoadTranslations("common.phrases");
78 LoadTranslations("basebans.phrases");
79 LoadTranslations("core.phrases");
81 RegAdminCmd("sm_ban", Command_Ban, ADMFLAG_BAN, "sm_ban <#userid|name> <minutes|0> [reason]");
82 RegAdminCmd("sm_unban", Command_Unban, ADMFLAG_UNBAN, "sm_unban <steamid|ip>");
83 RegAdminCmd("sm_addban", Command_AddBan, ADMFLAG_RCON, "sm_addban <time> <steamid> [reason]");
84 RegAdminCmd("sm_banip", Command_BanIp, ADMFLAG_BAN, "sm_banip <ip|#userid|name> <time> [reason]");
86 //This to manage custom ban reason messages
87 RegConsoleCmd("sm_abortban", Command_AbortBan, "sm_abortban");
89 /* Account for late loading */
91 if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
93 OnAdminMenuReady(topmenu);
100 if(g_hKvBanReasons != INVALID_HANDLE)
102 CloseHandle(g_hKvBanReasons);
108 //(Re-)Load BanReasons
109 if(g_hKvBanReasons != INVALID_HANDLE)
111 CloseHandle(g_hKvBanReasons);
113 if(!LoadBanReasons())
115 SetFailState("Error trying to load or parse %s: See logfile for details", g_BanReasonsPath);
119 public OnClientDisconnect(client)
121 g_IsWaitingForChatReason[client] = false;
124 public bool:LoadBanReasons()
126 g_hKvBanReasons = CreateKeyValues("banreasons");
127 if(FileToKeyValues(g_hKvBanReasons, g_BanReasonsPath))
129 decl String:sectionName[255];
130 if(!KvGetSectionName(g_hKvBanReasons, sectionName, sizeof(sectionName)))
132 LogMessage("Error in %s: File corrupt or in the wrong format", g_BanReasonsPath);
135 if(strcmp(sectionName, "banreasons") != 0)
137 LogMessage("Error in %s: Couldn't find 'banreasons'", g_BanReasonsPath);
142 KvRewind(g_hKvBanReasons);
148 LogMessage("Error in %s: File not found, corrupt or in the wrong format", g_BanReasonsPath);
153 public OnAdminMenuReady(Handle:topmenu)
155 /* Block us from being called twice */
156 if (topmenu == hTopMenu)
161 /* Save the Handle */
164 /* Find the "Player Commands" category */
165 new TopMenuObject:player_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_PLAYERCOMMANDS);
167 if (player_commands != INVALID_TOPMENUOBJECT)
169 AddToTopMenu(hTopMenu,
180 public Action:Command_BanIp(client, args)
184 ReplyToCommand(client, "[SM] Usage: sm_banip <ip|#userid|name> <time> [reason]");
185 return Plugin_Handled;
189 decl String:Arguments[256];
190 decl String:arg[50], String:time[20];
192 GetCmdArgString(Arguments, sizeof(Arguments));
193 len = BreakString(Arguments, arg, sizeof(arg));
195 if ((next_len = BreakString(Arguments[len], time, sizeof(time))) != -1)
205 if (StrEqual(arg, "0"))
207 ReplyToCommand(client, "[SM] %t", "Cannot ban that IP");
208 return Plugin_Handled;
211 decl String:target_name[MAX_TARGET_LENGTH];
212 decl target_list[1], bool:tn_is_ml;
213 new found_client = -1;
215 if (ProcessTargetString(
220 COMMAND_FILTER_CONNECTED|COMMAND_FILTER_NO_MULTI,
225 found_client = target_list[0];
230 if (client == 0 || (client == 1 && !IsDedicatedServer()))
236 new AdminId:id = GetUserAdmin(client);
237 has_rcon = (id == INVALID_ADMIN_ID) ? false : GetAdminFlag(id, Admin_RCON);
241 if (found_client != -1
242 && !IsFakeClient(found_client)
243 && (has_rcon || CanUserTarget(client, found_client)))
245 GetClientIP(found_client, arg, sizeof(arg));
246 hit_client = found_client;
249 if (hit_client == -1 && !has_rcon)
251 ReplyToCommand(client, "[SM] %t", "No Access");
252 return Plugin_Handled;
255 new minutes = StringToInt(time);
259 "\"%L\" added ban (minutes \"%d\") (ip \"%s\") (reason \"%s\")",
265 ReplyToCommand(client, "[SM] %t", "Ban added");
274 if (hit_client != -1)
276 KickClient(hit_client, "Banned: %s", Arguments[len]);
279 return Plugin_Handled;
282 public Action:Command_AddBan(client, args)
286 ReplyToCommand(client, "[SM] Usage: sm_addban <time> <steamid> [reason]");
287 return Plugin_Handled;
290 decl String:arg_string[256];
292 new String:authid[50];
294 GetCmdArgString(arg_string, sizeof(arg_string));
299 if ((len = BreakString(arg_string, time, sizeof(time))) == -1)
301 ReplyToCommand(client, "[SM] Usage: sm_addban <time> <steamid> [reason]");
302 return Plugin_Handled;
307 if ((len = BreakString(arg_string[total_len], authid, sizeof(authid))) != -1)
314 arg_string[0] = '\0';
318 if (strncmp(authid, "STEAM_", 6) != 0 || authid[7] != ':')
320 ReplyToCommand(client, "[SM] %t", "Invalid SteamID specified");
321 return Plugin_Handled;
324 new minutes = StringToInt(time);
328 "\"%L\" added ban (minutes \"%d\") (id \"%s\") (reason \"%s\")",
332 arg_string[total_len]);
336 arg_string[total_len],
340 ReplyToCommand(client, "[SM] %t", "Ban added");
342 return Plugin_Handled;
345 public Action:Command_Unban(client, args)
349 ReplyToCommand(client, "[SM] Usage: sm_unban <steamid|ip>");
350 return Plugin_Handled;
354 GetCmdArgString(arg, sizeof(arg));
356 ReplaceString(arg, sizeof(arg), "\"", "");
359 if (strncmp(arg, "STEAM_", 6) == 0 && arg[7] == ':')
361 ban_flags |= BANFLAG_AUTHID;
365 ban_flags |= BANFLAG_IP;
368 LogAction(client, -1, "\"%L\" removed ban (filter \"%s\")", client, arg);
369 RemoveBan(arg, ban_flags, "sm_unban", client);
371 ReplyToCommand(client, "[SM] %t", "Removed bans matching", arg);
373 return Plugin_Handled;
376 public Action:Command_AbortBan(client, args)
378 if(!CheckCommandAccess(client, "sm_ban", ADMFLAG_BAN))
380 ReplyToCommand(client, "[SM] %t", "No Access");
381 return Plugin_Handled;
383 if(g_IsWaitingForChatReason[client])
385 g_IsWaitingForChatReason[client] = false;
386 ReplyToCommand(client, "[SM] %t", "AbortBan applied successfully");
390 ReplyToCommand(client, "[SM] %t", "AbortBan not waiting for custom reason");
393 return Plugin_Handled;
396 public Action:OnClientSayCommand(client, const String:command[], const String:sArgs[])
398 if(g_IsWaitingForChatReason[client])
400 g_IsWaitingForChatReason[client] = false;
402 decl String:message[192];
403 GetCmdArgString(message, sizeof(message));
404 StripQuotes(message);
406 PrepareBan(client, g_BanTarget[client], g_BanTime[client], message);
408 return Plugin_Handled;
410 return Plugin_Continue;