Guest User

Untitled

a guest
May 2nd, 2017
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // *************************************************************************
  2. // This file is part of SourceBans++.
  3. //
  4. // Copyright (C) 2014-2016 SourceBans++ Dev Team <https://github.com/sbpp>
  5. //
  6. // SourceBans++ is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, per version 3 of the License.
  9. //
  10. // SourceBans++ is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SourceBans++. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. // This file is based off work(s) covered by the following copyright(s):
  19. //
  20. // SourceSleuth 1.3 fix
  21. // Copyright (C) 2013-2015 ecca
  22. // Licensed under GNU GPL version 3, or later.
  23. // Page: <https://forums.alliedmods.net/showthread.php?p=1818793> - <https://github.com/ecca/SourceMod-Plugins>
  24. //
  25. // *************************************************************************
  26.  
  27. #pragma semicolon 1
  28. #include <sourcemod>
  29. #undef REQUIRE_PLUGIN
  30. #include <sourcebans>
  31.  
  32. #define PLUGIN_VERSION "1.6.0"
  33.  
  34. #define LENGTH_ORIGINAL 1
  35. #define LENGTH_CUSTOM 2
  36. #define LENGTH_DOUBLE 3
  37. #define LENGTH_NOTIFY 4
  38.  
  39. //- Handles -//
  40. new Handle:hDatabase = INVALID_HANDLE;
  41. new Handle:g_hAllowedArray = INVALID_HANDLE;
  42.  
  43. //- ConVars -//
  44. ConVar g_cVar_actions;
  45. ConVar g_cVar_banduration;
  46. ConVar g_cVar_sbprefix;
  47. ConVar g_cVar_bansAllowed;
  48. ConVar g_cVar_bantype;
  49. ConVar g_cVar_bypass;
  50.  
  51. //- Bools -//
  52. new bool:CanUseSourcebans = false;
  53.  
  54. public Plugin:myinfo =
  55. {
  56. name = "SourceBans++: SourceSleuth",
  57. author = "ecca, SourceBans++ Dev Team",
  58. description = "Useful for TF2 servers. Plugin will check for banned ips and ban the player.",
  59. version = PLUGIN_VERSION,
  60. url = "https://sbpp.sarabveer.me/"
  61. };
  62.  
  63. public OnPluginStart()
  64. {
  65. LoadTranslations("sourcesleuth.phrases");
  66.  
  67. CreateConVar("sm_sourcesleuth_version", PLUGIN_VERSION, "SourceSleuth plugin version", FCVAR_SPONLY | FCVAR_REPLICATED | FCVAR_NOTIFY | FCVAR_DONTRECORD);
  68.  
  69. g_cVar_actions = CreateConVar("sm_sleuth_actions", "3", "Sleuth Ban Type: 1 - Original Length, 2 - Custom Length, 3 - Double Length, 4 - Notify Admins Only", 0, true, 1.0, true, 4.0);
  70. g_cVar_banduration = CreateConVar("sm_sleuth_duration", "0", "Required: sm_sleuth_actions 1: Bantime to ban player if we got a match (0 = permanent (defined in minutes) )", 0);
  71. g_cVar_sbprefix = CreateConVar("sm_sleuth_prefix", "sb", "Prexfix for sourcebans tables: Default sb", 0);
  72. g_cVar_bansAllowed = CreateConVar("sm_sleuth_bansallowed", "0", "How many active bans are allowed before we act", 0);
  73. g_cVar_bantype = CreateConVar("sm_sleuth_bantype", "0", "0 - ban all type of lengths, 1 - ban only permanent bans", 0, true, 0.0, true, 1.0);
  74. g_cVar_bypass = CreateConVar("sm_sleuth_adminbypass", "0", "0 - Inactivated, 1 - Allow all admins with ban flag to pass the check", 0, true, 0.0, true, 1.0);
  75.  
  76. g_hAllowedArray = CreateArray(256);
  77.  
  78. AutoExecConfig(true, "Sm_SourceSleuth");
  79.  
  80. SQL_TConnect(SQL_OnConnect, "sourcebans");
  81.  
  82. RegAdminCmd("sm_sleuth_reloadlist", ReloadListCallBack, ADMFLAG_ROOT);
  83.  
  84. LoadWhiteList();
  85. }
  86.  
  87. public OnAllPluginsLoaded()
  88. {
  89. CanUseSourcebans = LibraryExists("sourcebans");
  90. }
  91.  
  92. public OnLibraryAdded(const String:name[])
  93. {
  94. if (StrEqual("sourcebans", name))
  95. {
  96. CanUseSourcebans = true;
  97. }
  98. }
  99.  
  100. public OnLibraryRemoved(const String:name[])
  101. {
  102. if (StrEqual("sourcebans", name))
  103. {
  104. CanUseSourcebans = false;
  105. }
  106. }
  107.  
  108. public SQL_OnConnect(Handle:owner, Handle:hndl, const String:error[], any:data)
  109. {
  110. if (hndl == INVALID_HANDLE)
  111. {
  112. LogError("SourceSleuth: Database connection error: %s", error);
  113. }
  114. else
  115. {
  116. hDatabase = hndl;
  117. }
  118. }
  119.  
  120. public Action:ReloadListCallBack(client, args)
  121. {
  122. ClearArray(g_hAllowedArray);
  123.  
  124. LoadWhiteList();
  125.  
  126. LogMessage("%L reloaded the whitelist", client);
  127.  
  128. if (client != 0)
  129. {
  130. PrintToChat(client, "[SourceSleuth] WhiteList has been reloaded!");
  131. }
  132.  
  133. return Plugin_Continue;
  134. }
  135.  
  136. public OnClientPostAdminCheck(client)
  137. {
  138. if (CanUseSourcebans && !IsFakeClient(client))
  139. {
  140. new String:steamid[32];
  141. GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
  142.  
  143. if (g_cVar_bypass.BoolValue && CheckCommandAccess(client, "sleuth_admin", ADMFLAG_BAN, false))
  144. {
  145. return;
  146. }
  147.  
  148. if (FindStringInArray(g_hAllowedArray, steamid) == -1)
  149. {
  150. new String:IP[32], String:Prefix[64];
  151. GetClientIP(client, IP, sizeof(IP));
  152.  
  153. g_cVar_sbprefix.GetString(Prefix, sizeof(Prefix));
  154.  
  155. new String:query[1024];
  156.  
  157. FormatEx(query, sizeof(query), "SELECT * FROM %s_bans WHERE ip='%s' AND RemoveType IS NULL AND (ends > %d OR length = 0)", Prefix, IP, g_cVar_bantype.IntValue == 0 ? GetTime() : 0);
  158.  
  159. new Handle:datapack = CreateDataPack();
  160.  
  161. WritePackCell(datapack, GetClientUserId(client));
  162. WritePackString(datapack, steamid);
  163. WritePackString(datapack, IP);
  164. ResetPack(datapack);
  165.  
  166. SQL_TQuery(hDatabase, SQL_CheckHim, query, datapack);
  167. }
  168. }
  169. }
  170.  
  171. public SQL_CheckHim(Handle:owner, Handle:hndl, const String:error[], any:datapack)
  172. {
  173. new client;
  174. decl String:steamid[32], String:IP[32];
  175.  
  176. if (datapack != INVALID_HANDLE)
  177. {
  178. client = GetClientOfUserId(ReadPackCell(datapack));
  179. ReadPackString(datapack, steamid, sizeof(steamid));
  180. ReadPackString(datapack, IP, sizeof(IP));
  181. CloseHandle(datapack);
  182. }
  183.  
  184. if (hndl == INVALID_HANDLE)
  185. {
  186. LogError("SourceSleuth: Database query error: %s", error);
  187. return;
  188. }
  189.  
  190. if (SQL_FetchRow(hndl))
  191. {
  192. new TotalBans = SQL_GetRowCount(hndl);
  193.  
  194. if (TotalBans > g_cVar_bansAllowed.IntValue)
  195. {
  196. switch (g_cVar_actions.IntValue)
  197. {
  198. case LENGTH_ORIGINAL:
  199. {
  200. new length = SQL_FetchInt(hndl, 6);
  201. new time = length * 60;
  202.  
  203. BanPlayer(client, time);
  204. }
  205. case LENGTH_CUSTOM:
  206. {
  207. new time = g_cVar_banduration.IntValue;
  208. BanPlayer(client, time);
  209. }
  210. case LENGTH_DOUBLE:
  211. {
  212. new length = SQL_FetchInt(hndl, 6);
  213.  
  214. new time = 0;
  215.  
  216. if (length != 0)
  217. {
  218. time = length / 60 * 2;
  219. }
  220.  
  221. BanPlayer(client, time);
  222. }
  223. case LENGTH_NOTIFY:
  224. {
  225. /* Notify Admins when a client with an ip on the bans list connects */
  226. PrintToAdmins("[SourceSleuth] %t", "sourcesleuth_admintext", client, steamid, IP);
  227. }
  228. }
  229. }
  230. }
  231. }
  232.  
  233. stock BanPlayer(client, time)
  234. {
  235. decl String:Reason[255];
  236. Format(Reason, sizeof(Reason), "[SourceSleuth] %t", "sourcesleuth_banreason");
  237. SourceBans_BanPlayer(0, client, time, Reason);
  238. }
  239.  
  240. PrintToAdmins(const String:format[], any:...)
  241. {
  242. new String:g_Buffer[256];
  243.  
  244. for (new i = 1; i <= MaxClients; i++)
  245. {
  246. if (CheckCommandAccess(i, "sm_sourcesleuth_printtoadmins", ADMFLAG_BAN) && IsClientInGame(i))
  247. {
  248. VFormat(g_Buffer, sizeof(g_Buffer), format, 2);
  249.  
  250. PrintToChat(i, "%s", g_Buffer);
  251. }
  252. }
  253. }
  254.  
  255. public LoadWhiteList()
  256. {
  257. decl String:path[PLATFORM_MAX_PATH], String:line[256];
  258.  
  259. BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "configs/sourcesleuth_whitelist.cfg");
  260.  
  261. new Handle:fileHandle = OpenFile(path, "r");
  262.  
  263. if (fileHandle == INVALID_HANDLE)
  264. {
  265. LogError("Could not find the config file (addons/sourcemod/configs/sourcesleuth_whitelist.cfg)");
  266.  
  267. return;
  268. }
  269.  
  270. while (!IsEndOfFile(fileHandle) && ReadFileLine(fileHandle, line, sizeof(line)))
  271. {
  272. ReplaceString(line, sizeof(line), "\n", "", false);
  273.  
  274. PushArrayString(g_hAllowedArray, line);
  275. }
  276.  
  277. CloseHandle(fileHandle);
  278. }
RAW Paste Data