From 4f6185ba7a9d5a1e164e2b6c6bf82a73d9aa4a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Mon, 14 Oct 2019 19:57:00 +0500 Subject: [PATCH 01/17] Single SQL handle. Init. --- game/addons/sourcemod/scripting/sbpp/core.sp | 18 ++ game/addons/sourcemod/scripting/sbpp/sql.sp | 178 ++++++++++++++++++ .../sourcemod/scripting/sbpp_checker.sp | 38 ++-- .../addons/sourcemod/scripting/sbpp_sleuth.sp | 60 +++--- .../translations/sbpp.sql.phrases.txt | 8 + 5 files changed, 244 insertions(+), 58 deletions(-) create mode 100644 game/addons/sourcemod/scripting/sbpp/core.sp create mode 100644 game/addons/sourcemod/scripting/sbpp/sql.sp create mode 100644 game/addons/sourcemod/translations/sbpp.sql.phrases.txt diff --git a/game/addons/sourcemod/scripting/sbpp/core.sp b/game/addons/sourcemod/scripting/sbpp/core.sp new file mode 100644 index 000000000..e924d4679 --- /dev/null +++ b/game/addons/sourcemod/scripting/sbpp/core.sp @@ -0,0 +1,18 @@ +#if defined _sbpp_core_included + #endinput +#endif +#define _sbpp_core_included +#include /* DEBUG ONLY. Remove me later. */ + +#define SBPP_VERSION "1.7.0:1049" + +#define SBPP_LogMsg( LogToFile( szLog, +#define SBPP_LogIssue( LogToFile( szIssues, + +stock char szLog[PLATFORM_MAX_PATH], szIssues[PLATFORM_MAX_PATH], PREFIX[] = "\x04[SourceBans++]\x01 "; + +stock void SBPP_Core_Init () +{ + BuildPath( Path_SM, szLog, sizeof szLog, "logs/sbpp.log" ); + BuildPath( Path_SM, szIssues, sizeof szIssues, "logs/sbpp.issues.log" ); +} \ No newline at end of file diff --git a/game/addons/sourcemod/scripting/sbpp/sql.sp b/game/addons/sourcemod/scripting/sbpp/sql.sp new file mode 100644 index 000000000..3092d3996 --- /dev/null +++ b/game/addons/sourcemod/scripting/sbpp/sql.sp @@ -0,0 +1,178 @@ +#if defined _sbpp_sql_included + #endinput +#endif +#define _sbpp_sql_included + +#include + +#define SBPP_SQL_Init() SBPP_SQL_Init( DoNothing ) /* This something like function overloading. For now, we can't set default parameter-function. */ + +enum { /* States */ + SBPP_SQL_State_None = 0, + SBPP_SQL_State_Connecting = 1 << 0, + SBPP_SQL_State_Wait = 1 << 1, + SBPP_SQL_State_Connected = 1 << 2, } + +forward Action _SBPP_SQL_Find (Database &db, int &iState); +forward void _SBPP_SQL_Close (); +forward void _SBPP_SQL_Release (const Database db); + +typedef ConnectCallback = function void (const bool bSuccessful); + +stock Database g_dbSQL; +stock static int s_iState; +stock static bool s_bIgnoreForward; +stock static ConnectCallback s_fnCallback; /* For now, Database.Connect() can't have function as pass data. */ +stock static char s_szPrevError[PLATFORM_MAX_PATH]; +stock static Handle s_hSBPP_SQL_Find, s_hSBPP_SQL_Close, s_hSBPP_SQL_Release; + +stock void SBPP_SQL_Init (const ConnectCallback fnCallback) +{ + LoadTranslations( "sbpp.sql.phrases.txt" ); + s_hSBPP_SQL_Find = CreateGlobalForward( "_SBPP_SQL_Find", ET_Hook, Param_CellByRef, Param_CellByRef ); + s_hSBPP_SQL_Close = CreateGlobalForward( "_SBPP_SQL_Close", ET_Ignore ); + s_hSBPP_SQL_Release = CreateGlobalForward( "_SBPP_SQL_Release", ET_Ignore, Param_Cell ); + s_fnCallback = fnCallback; + SBPP_SQL_Find(); +} + +stock void SBPP_SQL_Reconnect (const ConnectCallback fnCallback) +{ + if ( s_iState != SBPP_SQL_State_Connecting && s_iState != SBPP_SQL_State_Wait ) + { + s_bIgnoreForward = true; + Call_StartForward( s_hSBPP_SQL_Close ); + Call_Finish(); + s_bIgnoreForward = false; + delete g_dbSQL; + s_fnCallback = fnCallback; + SBPP_SQL_Connect(); + } +} + +stock static void SBPP_SQL_Find () +{ + int iState; + Database db; + s_bIgnoreForward = true; + Call_StartForward( s_hSBPP_SQL_Find ); + Call_PushCellRef( db ); + Call_PushCellRef( iState ); + Call_Finish(); + s_bIgnoreForward = false; + switch ( iState ) + { + case SBPP_SQL_State_None: + { + SBPP_SQL_Connect(); + } + case SBPP_SQL_State_Connecting: + { + s_iState = SBPP_SQL_State_Wait; + CreateTimer( 15.0, CheckWait, _ ); + } + case SBPP_SQL_State_Connected: + { + _SBPP_SQL_Release( db ); + } + } +} + +stock static void SBPP_SQL_Connect () +{ + s_iState = SBPP_SQL_State_Connecting; + Database.Connect( SBPP_SQL_Connect_Callback, "sbpp" ); +} + +stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szError, const any aData) +{ + bool bSuccessful; + if ( db ) + { + g_dbSQL = db; + g_dbSQL.SetCharset( "utf8" ); + s_bIgnoreForward = true; + Call_StartForward( s_hSBPP_SQL_Release ); + Call_PushCell( g_dbSQL ); + Call_Finish(); + s_bIgnoreForward = false; + bSuccessful = true; + if ( s_szPrevError[0] ) + { + SBPP_LogMsg( "%t", "Successful reconnect" ); + s_szPrevError = ""; + } + } + else if ( szError[0] ) + { + s_iState = SBPP_SQL_State_None; + if ( strcmp( s_szPrevError, szError ) ) + { + SBPP_LogMsg( szError ); + strcopy( s_szPrevError, sizeof s_szPrevError, szError ); + } + } + s_iState = SBPP_SQL_State_Connected; + CallCallback( bSuccessful ); +} + +stock static void CallCallback (const bool bSuccessful) +{ + if ( s_fnCallback != DoNothing ) + { + Call_StartFunction( null, s_fnCallback ); + Call_PushCell( bSuccessful ); + Call_Finish(); + s_fnCallback = DoNothing; + } +} + +stock static Action CheckWait (Handle tTimer) +{ + if ( s_iState == SBPP_SQL_State_Wait ) + { + SBPP_SQL_Find(); + } + return Plugin_Stop; +} + +public Action _SBPP_SQL_Find (Database &db, int &iState) +{ + if ( !s_bIgnoreForward ) + { + if ( g_dbSQL ) + { + db = g_dbSQL; + iState = SBPP_SQL_State_Connected; + return Plugin_Stop; + } + if ( s_iState == SBPP_SQL_State_Connecting ) + { + iState = SBPP_SQL_State_Connecting; + return Plugin_Stop; + } + } + return Plugin_Continue; +} + +public void _SBPP_SQL_Close () +{ + if ( !s_bIgnoreForward ) + { + delete g_dbSQL; + s_iState = SBPP_SQL_State_Wait; + CreateTimer( 15.0, CheckWait, _ ); + } +} + +public void _SBPP_SQL_Release (const Database db) +{ + if ( !s_bIgnoreForward ) + { + g_dbSQL = view_as( CloneHandle( db ) ); + s_iState = SBPP_SQL_State_Connected; + CallCallback( true ); + } +} + +stock void DoNothing (const bool bSuccessful) {} \ No newline at end of file diff --git a/game/addons/sourcemod/scripting/sbpp_checker.sp b/game/addons/sourcemod/scripting/sbpp_checker.sp index 7cec36c63..0770a5fed 100644 --- a/game/addons/sourcemod/scripting/sbpp_checker.sp +++ b/game/addons/sourcemod/scripting/sbpp_checker.sp @@ -24,12 +24,13 @@ // // ************************************************************************* -#pragma semicolon 1 #pragma newdecls required +#pragma semicolon 1 #include +#include +#include -#define VERSION "1.7.0" #define LISTBANS_USAGE "sm_listbans <#userid|name> - Lists a user's prior bans from Sourcebans" #define LISTCOMMS_USAGE "sm_listcomms <#userid|name> - Lists a user's prior comms from Sourcebans" #define INVALID_TARGET -1 @@ -37,28 +38,26 @@ char g_DatabasePrefix[10] = "sb"; SMCParser g_ConfigParser; -Database g_DB; public Plugin myinfo = { name = "SourceBans++: Bans Checker", author = "psychonic, Ca$h Munny, SourceBans++ Dev Team", description = "Notifies admins of prior bans from Sourcebans upon player connect.", - version = VERSION, + version = SBPP_VERSION, url = "https://sbpp.github.io" }; public void OnPluginStart() { + SBPP_Core_Init(); + SBPP_SQL_Init(); LoadTranslations("common.phrases"); LoadTranslations("sbpp_checker.phrases"); - CreateConVar("sbchecker_version", VERSION, "", FCVAR_NOTIFY); RegAdminCmd("sm_listbans", OnListSourceBansCmd, ADMFLAG_GENERIC, LISTBANS_USAGE); RegAdminCmd("sm_listcomms", OnListSourceCommsCmd, ADMFLAG_GENERIC, LISTCOMMS_USAGE); RegAdminCmd("sb_reload", OnReloadCmd, ADMFLAG_RCON, "Reload sourcebans config and ban reason menu options"); - - Database.Connect(OnDatabaseConnected, "sourcebans"); } public void OnMapStart() @@ -72,17 +71,10 @@ public Action OnReloadCmd(int client, int args) return Plugin_Handled; } -public void OnDatabaseConnected(Database db, const char[] error, any data) -{ - if (db == null) - SetFailState("Failed to connect to SourceBans DB, %s", error); - - g_DB = db; -} public void OnClientAuthorized(int client, const char[] auth) { - if (g_DB == null) + if (g_dbSQL == null) return; /* Do not check bots nor check player with lan steamid. */ @@ -91,8 +83,8 @@ public void OnClientAuthorized(int client, const char[] auth) char query[512], ip[30]; GetClientIP(client, ip, sizeof(ip)); - FormatEx(query, sizeof(query), "SELECT COUNT(bid) FROM %s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) UNION SELECT COUNT(bid) FROM %s_comms WHERE authid REGEXP '^STEAM_[0-9]:%s$'", g_DatabasePrefix, auth[8], ip, g_DatabasePrefix,auth[8]); - g_DB.Query(OnConnectBanCheck, query, GetClientUserId(client), DBPrio_Low); + FormatEx(query, sizeof(query), "SELECT COUNT(bid) FROM `%s_bans` WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) UNION SELECT COUNT(bid) FROM `%s_comms` WHERE authid REGEXP '^STEAM_[0-9]:%s$'", g_DatabasePrefix, auth[8], ip, g_DatabasePrefix,auth[8]); + g_dbSQL.Query(OnConnectBanCheck, query, GetClientUserId(client), DBPrio_Low); } public void OnConnectBanCheck(Database db, DBResultSet results, const char[] error, any userid) @@ -124,7 +116,7 @@ public Action OnListSourceBansCmd(int client, int args) ReplyToCommand(client, LISTBANS_USAGE); } - if (g_DB == INVALID_HANDLE) + if (g_dbSQL == INVALID_HANDLE) { ReplyToCommand(client, "Error: Database not ready."); return Plugin_Handled; @@ -150,7 +142,7 @@ public Action OnListSourceBansCmd(int client, int args) char query[1024], ip[30]; GetClientIP(target, ip, sizeof(ip)); - FormatEx(query, sizeof(query), "SELECT created, %s_admins.user, ends, length, reason, RemoveType FROM %s_bans LEFT JOIN %s_admins ON %s_bans.aid = %s_admins.aid WHERE ((type = 0 AND %s_bans.authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8], ip); + FormatEx(query, sizeof(query), "SELECT created, `%s_admins`.user, ends, length, reason, RemoveType FROM `%s_bans` LEFT JOIN `%s_admins` ON `%s_bans`.aid = `%s_admins`.aid WHERE ((type = 0 AND `%s_bans`.authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8], ip); char targetName[MAX_NAME_LENGTH]; GetClientName(target, targetName, sizeof(targetName)); @@ -159,7 +151,7 @@ public Action OnListSourceBansCmd(int client, int args) dataPack.WriteCell((client == 0) ? 0 : GetClientUserId(client)); dataPack.WriteString(targetName); - g_DB.Query(OnListBans, query, dataPack, DBPrio_Low); + g_dbSQL.Query(OnListBans, query, dataPack, DBPrio_Low); if (client == 0) { @@ -290,7 +282,7 @@ public Action OnListSourceCommsCmd(int client, int args) ReplyToCommand(client, LISTCOMMS_USAGE); } - if (g_DB == INVALID_HANDLE) + if (g_dbSQL == INVALID_HANDLE) { ReplyToCommand(client, "Error: Database not ready."); return Plugin_Handled; @@ -315,7 +307,7 @@ public Action OnListSourceCommsCmd(int client, int args) } char query[1024]; - FormatEx(query, sizeof(query), "SELECT created, %s_admins.user, ends, length, reason, RemoveType, type FROM %s_comms LEFT JOIN %s_admins ON %s_comms.aid = %s_admins.aid WHERE %s_comms.authid REGEXP '^STEAM_[0-9]:%s$' AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8]); + FormatEx(query, sizeof(query), "SELECT created, `%s_admins`.user, ends, length, reason, RemoveType, type FROM `%s_comms` LEFT JOIN `%s_admins` ON `%s_comms`.aid = `%s_admins`.aid WHERE `%s_comms`.authid REGEXP '^STEAM_[0-9]:%s$' AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8]); char targetName[MAX_NAME_LENGTH]; GetClientName(target, targetName, sizeof(targetName)); @@ -324,7 +316,7 @@ public Action OnListSourceCommsCmd(int client, int args) dataPack.WriteCell((client == 0) ? 0 : GetClientUserId(client)); dataPack.WriteString(targetName); - g_DB.Query(OnListComms, query, dataPack, DBPrio_Low); + g_dbSQL.Query(OnListComms, query, dataPack, DBPrio_Low); if (client == 0) { diff --git a/game/addons/sourcemod/scripting/sbpp_sleuth.sp b/game/addons/sourcemod/scripting/sbpp_sleuth.sp index f09cc2b39..6ed0b29df 100644 --- a/game/addons/sourcemod/scripting/sbpp_sleuth.sp +++ b/game/addons/sourcemod/scripting/sbpp_sleuth.sp @@ -28,20 +28,32 @@ #pragma newdecls required #include +#include +#include + #undef REQUIRE_PLUGIN -#include + #include -#define PLUGIN_VERSION "1.7.0" +public Plugin myinfo = +{ + name = "SourceBans++: SourceSleuth", + author = "ecca, SourceBans++ Dev Team", + description = "Useful for TF2 servers. Plugin will check for banned ips and ban the player.", + version = SBPP_VERSION, + url = "https://sbpp.github.io" +}; -#define LENGTH_ORIGINAL 1 -#define LENGTH_CUSTOM 2 -#define LENGTH_DOUBLE 3 -#define LENGTH_NOTIFY 4 +enum +{ + LENGTH_ORIGINAL = 1, + LENGTH_CUSTOM, + LENGTH_DOUBLE, + LENGTH_NOTIFY, +}; #define PREFIX "[SourceSleuth] " //- Handles -// -Database hDatabase = null; ArrayList g_hAllowedArray = null; //- ConVars -// @@ -57,21 +69,13 @@ ConVar g_cVar_excludeTime; //- Bools -// bool CanUseSourcebans = false; -public Plugin myinfo = -{ - name = "SourceBans++: SourceSleuth", - author = "ecca, SourceBans++ Dev Team", - description = "Useful for TF2 servers. Plugin will check for banned ips and ban the player.", - version = PLUGIN_VERSION, - url = "https://sbpp.github.io" -}; public void OnPluginStart() { + SBPP_Core_Init(); + SBPP_SQL_Init(); LoadTranslations("sbpp_sleuth.phrases"); - CreateConVar("sm_sourcesleuth_version", PLUGIN_VERSION, "SourceSleuth plugin version", FCVAR_SPONLY | FCVAR_REPLICATED | FCVAR_NOTIFY | FCVAR_DONTRECORD); - 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); 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); g_cVar_sbprefix = CreateConVar("sm_sleuth_prefix", "sb", "Prexfix for sourcebans tables: Default sb", 0); @@ -85,8 +89,6 @@ public void OnPluginStart() AutoExecConfig(true, "Sm_SourceSleuth"); - Database.Connect(SQL_OnConnect, "sourcebans"); - RegAdminCmd("sm_sleuth_reloadlist", ReloadListCallBack, ADMFLAG_ROOT); LoadWhiteList(); @@ -113,25 +115,13 @@ public void OnLibraryRemoved(const char[] name) } } -public void SQL_OnConnect(Database db, const char[] error, any data) -{ - if (db == null) - { - LogError("SourceSleuth: Database connection error: %s", error); - } - else - { - hDatabase = db; - } -} - public Action ReloadListCallBack(int client, int args) { g_hAllowedArray.Clear(); LoadWhiteList(); - LogMessage("%L reloaded the whitelist", client); + SBPP_LogMsg("%L reloaded the whitelist", client); if (client != 0) { @@ -171,7 +161,7 @@ public void OnClientPostAdminCheck(int client) datapack.WriteString(IP); datapack.Reset(); - hDatabase.Query(SQL_CheckHim, query, datapack); + g_dbSQL.Query(SQL_CheckHim, query, datapack); } } } @@ -188,7 +178,7 @@ public void SQL_CheckHim(Database db, DBResultSet results, const char[] error, D if (results == null) { - LogError("SourceSleuth: Database query error: %s", error); + SBPP_LogMsg("SourceSleuth: Database query error: %s", error); return; } @@ -269,7 +259,7 @@ public void LoadWhiteList() if (fileHandle == null) { - LogError("Could not find the config file (%s)", path); + SBPP_LogMsg("Could not find the config file (%s)", path); return; } diff --git a/game/addons/sourcemod/translations/sbpp.sql.phrases.txt b/game/addons/sourcemod/translations/sbpp.sql.phrases.txt new file mode 100644 index 000000000..6e3c723ab --- /dev/null +++ b/game/addons/sourcemod/translations/sbpp.sql.phrases.txt @@ -0,0 +1,8 @@ +"Phrases" +{ + "Successful reconnect" + { + "en" "Successful reconnect to database." + "ru" "Успешное переподключение к базе данных." + } +} \ No newline at end of file From b886d9ac84b0f54a4fcde539ce565e26a0a18658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 08:52:34 +0500 Subject: [PATCH 02/17] Remove debug string. --- game/addons/sourcemod/scripting/sbpp/core.sp | 1 - 1 file changed, 1 deletion(-) diff --git a/game/addons/sourcemod/scripting/sbpp/core.sp b/game/addons/sourcemod/scripting/sbpp/core.sp index e924d4679..275e81397 100644 --- a/game/addons/sourcemod/scripting/sbpp/core.sp +++ b/game/addons/sourcemod/scripting/sbpp/core.sp @@ -2,7 +2,6 @@ #endinput #endif #define _sbpp_core_included -#include /* DEBUG ONLY. Remove me later. */ #define SBPP_VERSION "1.7.0:1049" From 567c2c9f35792f0a811a72b4a25b654329d6d8b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 09:00:53 +0500 Subject: [PATCH 03/17] fixes and code style cleanup --- game/addons/sourcemod/scripting/sbpp/sql.sp | 1 + .../sourcemod/scripting/sbpp_checker.sp | 6 ++- .../addons/sourcemod/scripting/sbpp_sleuth.sp | 51 +++++++++++-------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/game/addons/sourcemod/scripting/sbpp/sql.sp b/game/addons/sourcemod/scripting/sbpp/sql.sp index 3092d3996..d5389f9a3 100644 --- a/game/addons/sourcemod/scripting/sbpp/sql.sp +++ b/game/addons/sourcemod/scripting/sbpp/sql.sp @@ -6,6 +6,7 @@ #include #define SBPP_SQL_Init() SBPP_SQL_Init( DoNothing ) /* This something like function overloading. For now, we can't set default parameter-function. */ +#define SBPP_SQL_Reconnect() SBPP_SQL_Reconnect( DoNothing ) /* This something like function overloading. For now, we can't set default parameter-function. */ enum { /* States */ SBPP_SQL_State_None = 0, diff --git a/game/addons/sourcemod/scripting/sbpp_checker.sp b/game/addons/sourcemod/scripting/sbpp_checker.sp index 0770a5fed..fcb7f48de 100644 --- a/game/addons/sourcemod/scripting/sbpp_checker.sp +++ b/game/addons/sourcemod/scripting/sbpp_checker.sp @@ -74,7 +74,8 @@ public Action OnReloadCmd(int client, int args) public void OnClientAuthorized(int client, const char[] auth) { - if (g_dbSQL == null) + if ( !g_dbSQL ) + SBPP_SQL_Reconnect(); return; /* Do not check bots nor check player with lan steamid. */ @@ -116,8 +117,9 @@ public Action OnListSourceBansCmd(int client, int args) ReplyToCommand(client, LISTBANS_USAGE); } - if (g_dbSQL == INVALID_HANDLE) + if ( !g_dbSQL ) { + SBPP_SQL_Reconnect(); ReplyToCommand(client, "Error: Database not ready."); return Plugin_Handled; } diff --git a/game/addons/sourcemod/scripting/sbpp_sleuth.sp b/game/addons/sourcemod/scripting/sbpp_sleuth.sp index 6ed0b29df..ca87215a5 100644 --- a/game/addons/sourcemod/scripting/sbpp_sleuth.sp +++ b/game/addons/sourcemod/scripting/sbpp_sleuth.sp @@ -121,7 +121,7 @@ public Action ReloadListCallBack(int client, int args) LoadWhiteList(); - SBPP_LogMsg("%L reloaded the whitelist", client); + LogMessage("%L reloaded the whitelist", client); if (client != 0) { @@ -133,37 +133,44 @@ public Action ReloadListCallBack(int client, int args) public void OnClientPostAdminCheck(int client) { - if (CanUseSourcebans && !IsFakeClient(client)) + if ( g_dbSQL ) { - char steamid[32]; - GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid)); - - if (g_cVar_bypass.BoolValue && CheckCommandAccess(client, "sleuth_admin", ADMFLAG_BAN, false)) + if (CanUseSourcebans && !IsFakeClient(client)) { - return; - } + char steamid[32]; + GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid)); - if (g_hAllowedArray.FindString(steamid) == -1) - { - char IP[32], Prefix[64]; - GetClientIP(client, IP, sizeof(IP)); + if (g_cVar_bypass.BoolValue && CheckCommandAccess(client, "sleuth_admin", ADMFLAG_BAN, false)) + { + return; + } - g_cVar_sbprefix.GetString(Prefix, sizeof(Prefix)); + if (g_hAllowedArray.FindString(steamid) == -1) + { + char IP[32], Prefix[64]; + GetClientIP(client, IP, sizeof(IP)); + + g_cVar_sbprefix.GetString(Prefix, sizeof(Prefix)); - char query[1024]; + char query[1024]; - FormatEx(query, sizeof(query), "SELECT * FROM %s_bans WHERE ip='%s' AND RemoveType IS NULL AND (ends > %d OR ((1 = %d AND length = 0 AND ends > %d) OR (0 = %d AND length = 0)))", Prefix, IP, g_cVar_bantype.IntValue == 0 ? GetTime() : 0, g_cVar_excludeOld.IntValue, GetTime() - g_cVar_excludeTime.IntValue, g_cVar_excludeOld.IntValue); + FormatEx(query, sizeof(query), "SELECT * FROM %s_bans WHERE ip='%s' AND RemoveType IS NULL AND (ends > %d OR ((1 = %d AND length = 0 AND ends > %d) OR (0 = %d AND length = 0)))", Prefix, IP, g_cVar_bantype.IntValue == 0 ? GetTime() : 0, g_cVar_excludeOld.IntValue, GetTime() - g_cVar_excludeTime.IntValue, g_cVar_excludeOld.IntValue); - DataPack datapack = new DataPack(); + DataPack datapack = new DataPack(); - datapack.WriteCell(GetClientUserId(client)); - datapack.WriteString(steamid); - datapack.WriteString(IP); - datapack.Reset(); + datapack.WriteCell(GetClientUserId(client)); + datapack.WriteString(steamid); + datapack.WriteString(IP); + datapack.Reset(); - g_dbSQL.Query(SQL_CheckHim, query, datapack); + g_dbSQL.Query(SQL_CheckHim, query, datapack); + } } } + else + { + SBPP_SQL_Reconnect(); + } } public void SQL_CheckHim(Database db, DBResultSet results, const char[] error, DataPack dataPack) @@ -253,7 +260,7 @@ public void LoadWhiteList() { char path[PLATFORM_MAX_PATH], line[256]; - BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "configs/sourcebans/sourcesleuth_whitelist.cfg"); + BuildPath(Path_SM, path, sizeof path, "configs/sourcebans/sourcesleuth_whitelist.cfg"); File fileHandle = OpenFile(path, "r"); From 5ea54da8a2693c0d3f05a3151fc85d1bbd542a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 09:05:57 +0500 Subject: [PATCH 04/17] Update sql.sp --- game/addons/sourcemod/scripting/sbpp/sql.sp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/addons/sourcemod/scripting/sbpp/sql.sp b/game/addons/sourcemod/scripting/sbpp/sql.sp index d5389f9a3..e71eea9f5 100644 --- a/game/addons/sourcemod/scripting/sbpp/sql.sp +++ b/game/addons/sourcemod/scripting/sbpp/sql.sp @@ -82,7 +82,7 @@ stock static void SBPP_SQL_Find () stock static void SBPP_SQL_Connect () { s_iState = SBPP_SQL_State_Connecting; - Database.Connect( SBPP_SQL_Connect_Callback, "sbpp" ); + Database.Connect( SBPP_SQL_Connect_Callback, "sourcebans" ); } stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szError, const any aData) From 26bb09f2a6f0ca21810cef8a0b35b9d2cdbb273b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 09:10:04 +0500 Subject: [PATCH 05/17] Cleanup. Again. --- game/addons/sourcemod/scripting/sbpp_checker.sp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/addons/sourcemod/scripting/sbpp_checker.sp b/game/addons/sourcemod/scripting/sbpp_checker.sp index fcb7f48de..f47e06d17 100644 --- a/game/addons/sourcemod/scripting/sbpp_checker.sp +++ b/game/addons/sourcemod/scripting/sbpp_checker.sp @@ -284,7 +284,7 @@ public Action OnListSourceCommsCmd(int client, int args) ReplyToCommand(client, LISTCOMMS_USAGE); } - if (g_dbSQL == INVALID_HANDLE) + if ( !g_dbSQL ) { ReplyToCommand(client, "Error: Database not ready."); return Plugin_Handled; From b8afb5eb344f86ad613833bc4ce5dc3f73e41da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 09:15:09 +0500 Subject: [PATCH 06/17] Trying fix test build --- game/addons/sourcemod/scripting/sbpp_checker.sp | 4 ++-- game/addons/sourcemod/scripting/sbpp_sleuth.sp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/game/addons/sourcemod/scripting/sbpp_checker.sp b/game/addons/sourcemod/scripting/sbpp_checker.sp index f47e06d17..8133ebcac 100644 --- a/game/addons/sourcemod/scripting/sbpp_checker.sp +++ b/game/addons/sourcemod/scripting/sbpp_checker.sp @@ -28,8 +28,8 @@ #pragma semicolon 1 #include -#include -#include +#include "sbpp/core.sp" +#include "sbpp/sql.sp" #define LISTBANS_USAGE "sm_listbans <#userid|name> - Lists a user's prior bans from Sourcebans" #define LISTCOMMS_USAGE "sm_listcomms <#userid|name> - Lists a user's prior comms from Sourcebans" diff --git a/game/addons/sourcemod/scripting/sbpp_sleuth.sp b/game/addons/sourcemod/scripting/sbpp_sleuth.sp index ca87215a5..1b871a875 100644 --- a/game/addons/sourcemod/scripting/sbpp_sleuth.sp +++ b/game/addons/sourcemod/scripting/sbpp_sleuth.sp @@ -28,8 +28,8 @@ #pragma newdecls required #include -#include -#include +#include "sbpp/core.sp" +#include "sbpp/sql.sp" #undef REQUIRE_PLUGIN #include From 48a8d2948a8cd8fd09097351e8037d28ac6cd5b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 09:52:40 +0500 Subject: [PATCH 07/17] My fault. --- game/addons/sourcemod/scripting/sbpp_checker.sp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/game/addons/sourcemod/scripting/sbpp_checker.sp b/game/addons/sourcemod/scripting/sbpp_checker.sp index 8133ebcac..a65978b0e 100644 --- a/game/addons/sourcemod/scripting/sbpp_checker.sp +++ b/game/addons/sourcemod/scripting/sbpp_checker.sp @@ -75,9 +75,10 @@ public Action OnReloadCmd(int client, int args) public void OnClientAuthorized(int client, const char[] auth) { if ( !g_dbSQL ) + { SBPP_SQL_Reconnect(); return; - + } /* Do not check bots nor check player with lan steamid. */ if (auth[0] == 'B' || auth[9] == 'L') return; From 096c52837857b3cc818e4dfd4570a78b9c15d31a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 10:42:12 +0500 Subject: [PATCH 08/17] New charset --- game/addons/sourcemod/scripting/sbpp/sql.sp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/addons/sourcemod/scripting/sbpp/sql.sp b/game/addons/sourcemod/scripting/sbpp/sql.sp index e71eea9f5..cbd13c996 100644 --- a/game/addons/sourcemod/scripting/sbpp/sql.sp +++ b/game/addons/sourcemod/scripting/sbpp/sql.sp @@ -91,7 +91,7 @@ stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szE if ( db ) { g_dbSQL = db; - g_dbSQL.SetCharset( "utf8" ); + if ( !g_dbSQL.SetCharset( "utf8mb4" ) ) { g_dbSQL.SetCharset( "utf8" ); } s_bIgnoreForward = true; Call_StartForward( s_hSBPP_SQL_Release ); Call_PushCell( g_dbSQL ); From e0f0669cc76e8e9448f011438373f4d9ec5f1f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 10:53:56 +0500 Subject: [PATCH 09/17] Revert "Trying fix test build" This reverts commit b8afb5eb344f86ad613833bc4ce5dc3f73e41da8. --- game/addons/sourcemod/scripting/sbpp_checker.sp | 4 ++-- game/addons/sourcemod/scripting/sbpp_sleuth.sp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/game/addons/sourcemod/scripting/sbpp_checker.sp b/game/addons/sourcemod/scripting/sbpp_checker.sp index a65978b0e..b9cda1aa5 100644 --- a/game/addons/sourcemod/scripting/sbpp_checker.sp +++ b/game/addons/sourcemod/scripting/sbpp_checker.sp @@ -28,8 +28,8 @@ #pragma semicolon 1 #include -#include "sbpp/core.sp" -#include "sbpp/sql.sp" +#include +#include #define LISTBANS_USAGE "sm_listbans <#userid|name> - Lists a user's prior bans from Sourcebans" #define LISTCOMMS_USAGE "sm_listcomms <#userid|name> - Lists a user's prior comms from Sourcebans" diff --git a/game/addons/sourcemod/scripting/sbpp_sleuth.sp b/game/addons/sourcemod/scripting/sbpp_sleuth.sp index 1b871a875..ca87215a5 100644 --- a/game/addons/sourcemod/scripting/sbpp_sleuth.sp +++ b/game/addons/sourcemod/scripting/sbpp_sleuth.sp @@ -28,8 +28,8 @@ #pragma newdecls required #include -#include "sbpp/core.sp" -#include "sbpp/sql.sp" +#include +#include #undef REQUIRE_PLUGIN #include From 05df75531124a7ea55ed85f0f524a0c694b42983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 10:58:27 +0500 Subject: [PATCH 10/17] Moved it to `include` dir for now. Trying fix builder. --- game/addons/sourcemod/scripting/{ => include}/sbpp/core.sp | 0 game/addons/sourcemod/scripting/{ => include}/sbpp/sql.sp | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename game/addons/sourcemod/scripting/{ => include}/sbpp/core.sp (100%) rename game/addons/sourcemod/scripting/{ => include}/sbpp/sql.sp (100%) diff --git a/game/addons/sourcemod/scripting/sbpp/core.sp b/game/addons/sourcemod/scripting/include/sbpp/core.sp similarity index 100% rename from game/addons/sourcemod/scripting/sbpp/core.sp rename to game/addons/sourcemod/scripting/include/sbpp/core.sp diff --git a/game/addons/sourcemod/scripting/sbpp/sql.sp b/game/addons/sourcemod/scripting/include/sbpp/sql.sp similarity index 100% rename from game/addons/sourcemod/scripting/sbpp/sql.sp rename to game/addons/sourcemod/scripting/include/sbpp/sql.sp From 10b7cce1127c5c3a61887a4049c0f155f3320abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 15 Oct 2019 11:22:01 +0500 Subject: [PATCH 11/17] Fix by project style. --- game/addons/sourcemod/scripting/include/sbpp/sql.sp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/game/addons/sourcemod/scripting/include/sbpp/sql.sp b/game/addons/sourcemod/scripting/include/sbpp/sql.sp index cbd13c996..dd0aa068e 100644 --- a/game/addons/sourcemod/scripting/include/sbpp/sql.sp +++ b/game/addons/sourcemod/scripting/include/sbpp/sql.sp @@ -91,7 +91,10 @@ stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szE if ( db ) { g_dbSQL = db; - if ( !g_dbSQL.SetCharset( "utf8mb4" ) ) { g_dbSQL.SetCharset( "utf8" ); } + if ( !g_dbSQL.SetCharset( "utf8mb4" ) ) + { + g_dbSQL.SetCharset( "utf8" ); + } s_bIgnoreForward = true; Call_StartForward( s_hSBPP_SQL_Release ); Call_PushCell( g_dbSQL ); From 379878ddd1c773758e34ab739e4bf00dbf9d7bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Thu, 17 Oct 2019 16:54:20 +0500 Subject: [PATCH 12/17] Optimization "freeing" prev error string. --- game/addons/sourcemod/scripting/include/sbpp/sql.sp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/addons/sourcemod/scripting/include/sbpp/sql.sp b/game/addons/sourcemod/scripting/include/sbpp/sql.sp index dd0aa068e..51c3a2375 100644 --- a/game/addons/sourcemod/scripting/include/sbpp/sql.sp +++ b/game/addons/sourcemod/scripting/include/sbpp/sql.sp @@ -104,7 +104,7 @@ stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szE if ( s_szPrevError[0] ) { SBPP_LogMsg( "%t", "Successful reconnect" ); - s_szPrevError = ""; + s_szPrevError[0] = '\0'; } } else if ( szError[0] ) From 4647e67fdc6f1af3ef9d242d0ae8d46d78bc77ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Wed, 30 Oct 2019 12:14:27 +0500 Subject: [PATCH 13/17] Revert "Moved it to `include` dir for now. Trying fix builder." This reverts commit 05df75531124a7ea55ed85f0f524a0c694b42983. --- game/addons/sourcemod/scripting/{include => }/sbpp/core.sp | 0 game/addons/sourcemod/scripting/{include => }/sbpp/sql.sp | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename game/addons/sourcemod/scripting/{include => }/sbpp/core.sp (100%) rename game/addons/sourcemod/scripting/{include => }/sbpp/sql.sp (100%) diff --git a/game/addons/sourcemod/scripting/include/sbpp/core.sp b/game/addons/sourcemod/scripting/sbpp/core.sp similarity index 100% rename from game/addons/sourcemod/scripting/include/sbpp/core.sp rename to game/addons/sourcemod/scripting/sbpp/core.sp diff --git a/game/addons/sourcemod/scripting/include/sbpp/sql.sp b/game/addons/sourcemod/scripting/sbpp/sql.sp similarity index 100% rename from game/addons/sourcemod/scripting/include/sbpp/sql.sp rename to game/addons/sourcemod/scripting/sbpp/sql.sp From 02c2fc2c5813848e3378b0b8412a9e2a12eac043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Fri, 27 Mar 2020 16:09:20 +0500 Subject: [PATCH 14/17] v2 --- game/addons/sourcemod/scripting/sbpp/core.sp | 15 +- game/addons/sourcemod/scripting/sbpp/sql.sp | 211 +++++++++++------- .../sourcemod/scripting/sbpp_checker.sp | 131 ++--------- .../addons/sourcemod/scripting/sbpp_sleuth.sp | 4 +- .../translations/sbpp.sql.phrases.txt | 8 - .../translations/sbpp/sql.phrases.txt | 19 ++ 6 files changed, 188 insertions(+), 200 deletions(-) delete mode 100644 game/addons/sourcemod/translations/sbpp.sql.phrases.txt create mode 100644 game/addons/sourcemod/translations/sbpp/sql.phrases.txt diff --git a/game/addons/sourcemod/scripting/sbpp/core.sp b/game/addons/sourcemod/scripting/sbpp/core.sp index 275e81397..6f783c8d8 100644 --- a/game/addons/sourcemod/scripting/sbpp/core.sp +++ b/game/addons/sourcemod/scripting/sbpp/core.sp @@ -5,13 +5,16 @@ #define SBPP_VERSION "1.7.0:1049" -#define SBPP_LogMsg( LogToFile( szLog, -#define SBPP_LogIssue( LogToFile( szIssues, +#define SBPP_LogMsg( LogToFile( SBPP_Core_szLog, +#define SBPP_LogIssue( LogToFile( SBPP_Core_szIssues, -stock char szLog[PLATFORM_MAX_PATH], szIssues[PLATFORM_MAX_PATH], PREFIX[] = "\x04[SourceBans++]\x01 "; +stock char SBPP_Core_szLog[PLATFORM_MAX_PATH], SBPP_Core_szIssues[PLATFORM_MAX_PATH]; +stock const char SBPP_PREFIX[] = "\x04[SourceBans++]\x01 "; -stock void SBPP_Core_Init () +void SBPP_Core_Init () { - BuildPath( Path_SM, szLog, sizeof szLog, "logs/sbpp.log" ); - BuildPath( Path_SM, szIssues, sizeof szIssues, "logs/sbpp.issues.log" ); + BuildPath( Path_SM, SBPP_Core_szLog, sizeof SBPP_Core_szLog, "logs/sbpp" ); + CreateDirectory( SBPP_Core_szLog, 1 << 6 & 1 << 7 & 1 << 8 ); + BuildPath( Path_SM, SBPP_Core_szLog, sizeof SBPP_Core_szLog, "logs/sbpp/sbpp.log" ); + BuildPath( Path_SM, SBPP_Core_szIssues, sizeof SBPP_Core_szIssues, "logs/sbpp/issues.log" ); } \ No newline at end of file diff --git a/game/addons/sourcemod/scripting/sbpp/sql.sp b/game/addons/sourcemod/scripting/sbpp/sql.sp index 51c3a2375..26d6b74c2 100644 --- a/game/addons/sourcemod/scripting/sbpp/sql.sp +++ b/game/addons/sourcemod/scripting/sbpp/sql.sp @@ -5,78 +5,125 @@ #include -#define SBPP_SQL_Init() SBPP_SQL_Init( DoNothing ) /* This something like function overloading. For now, we can't set default parameter-function. */ -#define SBPP_SQL_Reconnect() SBPP_SQL_Reconnect( DoNothing ) /* This something like function overloading. For now, we can't set default parameter-function. */ +stock Database g_SBPP_SQL_dbHandle; +stock char g_SBPP_SQL_szPrefix[32] = "sb"; -enum { /* States */ +enum /* States of connection. */ +{ SBPP_SQL_State_None = 0, SBPP_SQL_State_Connecting = 1 << 0, SBPP_SQL_State_Wait = 1 << 1, - SBPP_SQL_State_Connected = 1 << 2, } - -forward Action _SBPP_SQL_Find (Database &db, int &iState); -forward void _SBPP_SQL_Close (); -forward void _SBPP_SQL_Release (const Database db); + SBPP_SQL_State_Connected = 1 << 2 +} typedef ConnectCallback = function void (const bool bSuccessful); -stock Database g_dbSQL; -stock static int s_iState; +stock static int s_iState, s_iTimestamp; stock static bool s_bIgnoreForward; -stock static ConnectCallback s_fnCallback; /* For now, Database.Connect() can't have function as pass data. */ +stock static ConnectCallback s_fnCallback = INVALID_FUNCTION; /* For now, Database.Connect() can't have function as pass data. */ stock static char s_szPrevError[PLATFORM_MAX_PATH]; -stock static Handle s_hSBPP_SQL_Find, s_hSBPP_SQL_Close, s_hSBPP_SQL_Release; +stock static Handle s_hSBPP_SQL_Handle_Find, s_hSBPP_SQL_Handle_OnClose, s_hSBPP_SQL_Handle_OnUpdate, s_hSBPP_SQL_Prefix_OnUpdate; -stock void SBPP_SQL_Init (const ConnectCallback fnCallback) +forward Action _SBPP_SQL_Handle_Find (Database &db, int &iState, char szPrefix[sizeof g_SBPP_SQL_szPrefix], int &iTimestamp); +forward void _SBPP_SQL_Handle_OnClose (); +forward void _SBPP_SQL_Handle_OnUpdate (const Database db); +forward void _SBPP_SQL_Prefix_OnUpdate (const char[] szPrefix, const int iTimestamp); + +void SBPP_SQL_Init (const ConnectCallback fnCallback = INVALID_FUNCTION) { - LoadTranslations( "sbpp.sql.phrases.txt" ); - s_hSBPP_SQL_Find = CreateGlobalForward( "_SBPP_SQL_Find", ET_Hook, Param_CellByRef, Param_CellByRef ); - s_hSBPP_SQL_Close = CreateGlobalForward( "_SBPP_SQL_Close", ET_Ignore ); - s_hSBPP_SQL_Release = CreateGlobalForward( "_SBPP_SQL_Release", ET_Ignore, Param_Cell ); + LoadTranslations( "sbpp/sql.phrases.txt" ); + s_hSBPP_SQL_Handle_Find = CreateGlobalForward( "_SBPP_SQL_Handle_Find", ET_Hook, Param_CellByRef, Param_CellByRef, Param_String, Param_CellByRef ); + s_hSBPP_SQL_Handle_OnClose = CreateGlobalForward( "_SBPP_SQL_Handle_OnClose", ET_Ignore ); + s_hSBPP_SQL_Handle_OnUpdate = CreateGlobalForward( "_SBPP_SQL_Handle_OnUpdate", ET_Ignore, Param_Cell ); + s_hSBPP_SQL_Prefix_OnUpdate = CreateGlobalForward( "_SBPP_SQL_Prefix_OnUpdate", ET_Ignore, Param_String ); s_fnCallback = fnCallback; SBPP_SQL_Find(); + SBPP_SQL_Prefix_Read(); + RegAdminCmd( "sbpp_prefix_reload", sbpp_prefix_reload_Handler, ADMFLAG_CONFIG, "Reload prefix of database tables." ); } -stock void SBPP_SQL_Reconnect (const ConnectCallback fnCallback) +stock void SBPP_SQL_Reconnect (const ConnectCallback fnCallback = INVALID_FUNCTION) { if ( s_iState != SBPP_SQL_State_Connecting && s_iState != SBPP_SQL_State_Wait ) { s_bIgnoreForward = true; - Call_StartForward( s_hSBPP_SQL_Close ); + Call_StartForward( s_hSBPP_SQL_Handle_OnClose ); Call_Finish(); s_bIgnoreForward = false; - delete g_dbSQL; + delete g_SBPP_SQL_dbHandle; s_fnCallback = fnCallback; SBPP_SQL_Connect(); } } +stock void SBPP_SQL_Prefix_Read () +{ + SMCParser Parser = new SMCParser(); + Parser.OnKeyValue = ReadConfig_KeyValue; + char szBuf[PLATFORM_MAX_PATH]; + BuildPath( Path_SM, szBuf, sizeof szBuf, "configs/sourcebans/sourcebans.cfg" ); + if ( FileExists( szBuf ) ) + { + SMCError err = Parser.ParseFile( szBuf ); + if ( err ) + { + Parser.GetErrorString( err, szBuf, sizeof szBuf ); + SBPP_LogIssue( szBuf[0] ? szBuf : "%t", "Unknown parse error." ); + } + } + else + { + SBPP_LogIssue( "%t", "Database config not found '%s'.", szBuf ); + } + CloseHandle( Parser ); +} + +stock static Action sbpp_prefix_reload_Handler (const int iClient, const int iArgs) +{ + SBPP_SQL_Prefix_Read(); + return Plugin_Stop; /* Prevent to call it multiple times, we send prefix to other plugins through forwards. */ +} + +stock static SMCResult ReadConfig_KeyValue (const SMCParser Parser, const char[] szKey, const char[] szVal, bool key_quotes, bool value_quotes ) +{ + if ( !strcmp( "DatabasePrefix", szKey ) ) + { + if ( strcmp( g_SBPP_SQL_szPrefix, szVal ) ) + { + strcopy( g_SBPP_SQL_szPrefix, sizeof g_SBPP_SQL_szPrefix, szVal ); + s_bIgnoreForward = true; + Call_StartForward( s_hSBPP_SQL_Prefix_OnUpdate ); + Call_PushString( g_SBPP_SQL_szPrefix ); + Call_Finish(); + s_bIgnoreForward = false; + } + } + return SMCParse_Continue; +} + stock static void SBPP_SQL_Find () { - int iState; Database db; s_bIgnoreForward = true; - Call_StartForward( s_hSBPP_SQL_Find ); + Call_StartForward( s_hSBPP_SQL_Handle_Find ); Call_PushCellRef( db ); - Call_PushCellRef( iState ); + Call_PushCellRef( s_iState ); + Call_PushStringEx( g_SBPP_SQL_szPrefix, sizeof g_SBPP_SQL_szPrefix, SM_PARAM_STRING_UTF8, SM_PARAM_COPYBACK ); + Call_PushCellRef( s_iTimestamp ); Call_Finish(); s_bIgnoreForward = false; - switch ( iState ) + if ( s_iState == SBPP_SQL_State_None ) { - case SBPP_SQL_State_None: - { - SBPP_SQL_Connect(); - } - case SBPP_SQL_State_Connecting: - { - s_iState = SBPP_SQL_State_Wait; - CreateTimer( 15.0, CheckWait, _ ); - } - case SBPP_SQL_State_Connected: - { - _SBPP_SQL_Release( db ); - } - } + SBPP_SQL_Connect(); + } + else if ( s_iState == SBPP_SQL_State_Wait ) + { + CreateTimer( 15.0, CheckWait ); + } + else if ( s_iState == SBPP_SQL_State_Connected ) + { + _SBPP_SQL_Handle_OnUpdate( db ); + } } stock static void SBPP_SQL_Connect () @@ -90,24 +137,24 @@ stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szE bool bSuccessful; if ( db ) { - g_dbSQL = db; - if ( !g_dbSQL.SetCharset( "utf8mb4" ) ) + g_SBPP_SQL_dbHandle = db; + if ( !g_SBPP_SQL_dbHandle.SetCharset( "utf8mb4" ) ) { - g_dbSQL.SetCharset( "utf8" ); + g_SBPP_SQL_dbHandle.SetCharset( "utf8" ); } s_bIgnoreForward = true; - Call_StartForward( s_hSBPP_SQL_Release ); - Call_PushCell( g_dbSQL ); + Call_StartForward( s_hSBPP_SQL_Handle_OnUpdate ); + Call_PushCell( g_SBPP_SQL_dbHandle ); Call_Finish(); s_bIgnoreForward = false; - bSuccessful = true; if ( s_szPrevError[0] ) { - SBPP_LogMsg( "%t", "Successful reconnect" ); + SBPP_LogMsg( "%t", "Successful reconnect to database." ); s_szPrevError[0] = '\0'; } + bSuccessful = true; } - else if ( szError[0] ) + else { s_iState = SBPP_SQL_State_None; if ( strcmp( s_szPrevError, szError ) ) @@ -120,63 +167,73 @@ stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szE CallCallback( bSuccessful ); } -stock static void CallCallback (const bool bSuccessful) -{ - if ( s_fnCallback != DoNothing ) - { - Call_StartFunction( null, s_fnCallback ); - Call_PushCell( bSuccessful ); - Call_Finish(); - s_fnCallback = DoNothing; - } -} - -stock static Action CheckWait (Handle tTimer) -{ - if ( s_iState == SBPP_SQL_State_Wait ) - { - SBPP_SQL_Find(); - } - return Plugin_Stop; -} - -public Action _SBPP_SQL_Find (Database &db, int &iState) +public Action _SBPP_SQL_Handle_Find (Database &db, int &iState, char szPrefix[sizeof g_SBPP_SQL_szPrefix], int &iTimestamp) { if ( !s_bIgnoreForward ) { - if ( g_dbSQL ) + if ( g_SBPP_SQL_dbHandle ) { - db = g_dbSQL; + db = g_SBPP_SQL_dbHandle; iState = SBPP_SQL_State_Connected; + strcopy( szPrefix, sizeof szPrefix, g_SBPP_SQL_szPrefix ); + iTimestamp = s_iTimestamp; return Plugin_Stop; } - if ( s_iState == SBPP_SQL_State_Connecting ) + else if ( s_iState == SBPP_SQL_State_Connecting ) { - iState = SBPP_SQL_State_Connecting; + iState = SBPP_SQL_State_Wait; + strcopy( szPrefix, sizeof szPrefix, g_SBPP_SQL_szPrefix ); + iTimestamp = s_iTimestamp; return Plugin_Stop; } } return Plugin_Continue; } -public void _SBPP_SQL_Close () +public void _SBPP_SQL_Handle_OnClose () { if ( !s_bIgnoreForward ) { - delete g_dbSQL; + delete g_SBPP_SQL_dbHandle; s_iState = SBPP_SQL_State_Wait; - CreateTimer( 15.0, CheckWait, _ ); + CreateTimer( 15.0, CheckWait ); } } -public void _SBPP_SQL_Release (const Database db) +public void _SBPP_SQL_Handle_OnUpdate (const Database db) { if ( !s_bIgnoreForward ) { - g_dbSQL = view_as( CloneHandle( db ) ); + g_SBPP_SQL_dbHandle = view_as( CloneHandle( db ) ); s_iState = SBPP_SQL_State_Connected; CallCallback( true ); } } -stock void DoNothing (const bool bSuccessful) {} \ No newline at end of file +public void _SBPP_SQL_Prefix_OnUpdate (const char[] szPrefix, const int iTimestamp) +{ + if ( !s_bIgnoreForward ) + { + strcopy( g_SBPP_SQL_szPrefix, sizeof g_SBPP_SQL_szPrefix, szPrefix ); + } +} + +stock static void CallCallback (const bool bSuccessful) +{ + if ( s_fnCallback != INVALID_FUNCTION ) + { + Call_StartFunction( null, s_fnCallback ); + Call_PushCell( bSuccessful ); + Call_Finish(); + s_fnCallback = INVALID_FUNCTION; + } +} + +stock static Action CheckWait (Handle tTimer) +{ + if ( s_iState != SBPP_SQL_State_Connected ) + { + SBPP_SQL_Find(); + } + return Plugin_Stop; +} \ No newline at end of file diff --git a/game/addons/sourcemod/scripting/sbpp_checker.sp b/game/addons/sourcemod/scripting/sbpp_checker.sp index b9cda1aa5..903c8bed9 100644 --- a/game/addons/sourcemod/scripting/sbpp_checker.sp +++ b/game/addons/sourcemod/scripting/sbpp_checker.sp @@ -34,10 +34,6 @@ #define LISTBANS_USAGE "sm_listbans <#userid|name> - Lists a user's prior bans from Sourcebans" #define LISTCOMMS_USAGE "sm_listcomms <#userid|name> - Lists a user's prior comms from Sourcebans" #define INVALID_TARGET -1 -#define Prefix "\x04[SourceBans++]\x01 " - -char g_DatabasePrefix[10] = "sb"; -SMCParser g_ConfigParser; public Plugin myinfo = { @@ -57,24 +53,16 @@ public void OnPluginStart() RegAdminCmd("sm_listbans", OnListSourceBansCmd, ADMFLAG_GENERIC, LISTBANS_USAGE); RegAdminCmd("sm_listcomms", OnListSourceCommsCmd, ADMFLAG_GENERIC, LISTCOMMS_USAGE); - RegAdminCmd("sb_reload", OnReloadCmd, ADMFLAG_RCON, "Reload sourcebans config and ban reason menu options"); } public void OnMapStart() { - ReadConfig(); -} - -public Action OnReloadCmd(int client, int args) -{ - ReadConfig(); - return Plugin_Handled; + SBPP_SQL_Prefix_Read(); } - public void OnClientAuthorized(int client, const char[] auth) { - if ( !g_dbSQL ) + if ( !g_SBPP_SQL_dbHandle ) { SBPP_SQL_Reconnect(); return; @@ -85,8 +73,8 @@ public void OnClientAuthorized(int client, const char[] auth) char query[512], ip[30]; GetClientIP(client, ip, sizeof(ip)); - FormatEx(query, sizeof(query), "SELECT COUNT(bid) FROM `%s_bans` WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) UNION SELECT COUNT(bid) FROM `%s_comms` WHERE authid REGEXP '^STEAM_[0-9]:%s$'", g_DatabasePrefix, auth[8], ip, g_DatabasePrefix,auth[8]); - g_dbSQL.Query(OnConnectBanCheck, query, GetClientUserId(client), DBPrio_Low); + FormatEx(query, sizeof(query), "SELECT COUNT(bid) FROM `%s_bans` WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) UNION SELECT COUNT(bid) FROM `%s_comms` WHERE authid REGEXP '^STEAM_[0-9]:%s$'", g_SBPP_SQL_szPrefix, auth[8], ip, g_SBPP_SQL_szPrefix,auth[8]); + g_SBPP_SQL_dbHandle.Query(OnConnectBanCheck, query, GetClientUserId(client), DBPrio_Low); } public void OnConnectBanCheck(Database db, DBResultSet results, const char[] error, any userid) @@ -101,13 +89,13 @@ public void OnConnectBanCheck(Database db, DBResultSet results, const char[] err commcount = results.FetchInt(0); } if ( bancount && commcount ) { - PrintToBanAdmins("%s%t", Prefix, "Ban and Comm Warning", client, bancount, ((bancount > 1 || bancount == 0) ? "s":""), commcount, ((commcount > 1 || commcount == 0) ? "s":"")); + PrintToBanAdmins("%s%t", SBPP_PREFIX, "Ban and Comm Warning", client, bancount, ((bancount > 1 || bancount == 0) ? "s":""), commcount, ((commcount > 1 || commcount == 0) ? "s":"")); } else if ( commcount ) { - PrintToBanAdmins("%s%t", Prefix, "Comm Warning", client, commcount, ((commcount > 1 || commcount == 0) ? "s":"")); + PrintToBanAdmins("%s%t", SBPP_PREFIX, "Comm Warning", client, commcount, ((commcount > 1 || commcount == 0) ? "s":"")); } else if ( bancount ) { - PrintToBanAdmins("%s%t", Prefix, "Ban Warning", client, bancount, ((bancount > 1 || bancount == 0) ? "s":"")); + PrintToBanAdmins("%s%t", SBPP_PREFIX, "Ban Warning", client, bancount, ((bancount > 1 || bancount == 0) ? "s":"")); } } @@ -118,7 +106,7 @@ public Action OnListSourceBansCmd(int client, int args) ReplyToCommand(client, LISTBANS_USAGE); } - if ( !g_dbSQL ) + if ( !g_SBPP_SQL_dbHandle ) { SBPP_SQL_Reconnect(); ReplyToCommand(client, "Error: Database not ready."); @@ -145,7 +133,7 @@ public Action OnListSourceBansCmd(int client, int args) char query[1024], ip[30]; GetClientIP(target, ip, sizeof(ip)); - FormatEx(query, sizeof(query), "SELECT created, `%s_admins`.user, ends, length, reason, RemoveType FROM `%s_bans` LEFT JOIN `%s_admins` ON `%s_bans`.aid = `%s_admins`.aid WHERE ((type = 0 AND `%s_bans`.authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8], ip); + FormatEx(query, sizeof(query), "SELECT created, `%s_admins`.user, ends, length, reason, RemoveType FROM `%s_bans` LEFT JOIN `%s_admins` ON `%s_bans`.aid = `%s_admins`.aid WHERE ((type = 0 AND `%s_bans`.authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, auth[8], ip); char targetName[MAX_NAME_LENGTH]; GetClientName(target, targetName, sizeof(targetName)); @@ -154,15 +142,15 @@ public Action OnListSourceBansCmd(int client, int args) dataPack.WriteCell((client == 0) ? 0 : GetClientUserId(client)); dataPack.WriteString(targetName); - g_dbSQL.Query(OnListBans, query, dataPack, DBPrio_Low); + g_SBPP_SQL_dbHandle.Query(OnListBans, query, dataPack, DBPrio_Low); if (client == 0) { - ReplyToCommand(client, "%sNote: if you are using this command through an rcon tool, you will not see results.", Prefix); + ReplyToCommand(client, "%sNote: if you are using this command through an rcon tool, you will not see results.", SBPP_PREFIX); } else { - ReplyToCommand(client, "\x04%s\x01 Look for %N's ban results in console.", Prefix, target); + ReplyToCommand(client, "\x04%s\x01 Look for %N's ban results in console.", SBPP_PREFIX, target); } return Plugin_Handled; @@ -182,17 +170,17 @@ public void OnListBans(Database db, DBResultSet results, const char[] error, Dat if (results == null) { - PrintListResponse(clientuid, client, "%sDB error while retrieving bans for %s:\n%s", Prefix, targetName, error); + PrintListResponse(clientuid, client, "%sDB error while retrieving bans for %s:\n%s", SBPP_PREFIX, targetName, error); return; } if (results.RowCount == 0) { - PrintListResponse(clientuid, client, "%sNo bans found for %s.", Prefix, targetName); + PrintListResponse(clientuid, client, "%sNo bans found for %s.", SBPP_PREFIX, targetName); return; } - PrintListResponse(clientuid, client, "%sListing bans for %s", Prefix, targetName); + PrintListResponse(clientuid, client, "%sListing bans for %s", SBPP_PREFIX, targetName); PrintListResponse(clientuid, client, "Ban Date Banned By Length End Date R Reason"); PrintListResponse(clientuid, client, "-------------------------------------------------------------------------------"); while (results.FetchRow()) @@ -285,7 +273,7 @@ public Action OnListSourceCommsCmd(int client, int args) ReplyToCommand(client, LISTCOMMS_USAGE); } - if ( !g_dbSQL ) + if ( !g_SBPP_SQL_dbHandle ) { ReplyToCommand(client, "Error: Database not ready."); return Plugin_Handled; @@ -310,7 +298,7 @@ public Action OnListSourceCommsCmd(int client, int args) } char query[1024]; - FormatEx(query, sizeof(query), "SELECT created, `%s_admins`.user, ends, length, reason, RemoveType, type FROM `%s_comms` LEFT JOIN `%s_admins` ON `%s_comms`.aid = `%s_admins`.aid WHERE `%s_comms`.authid REGEXP '^STEAM_[0-9]:%s$' AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8]); + FormatEx(query, sizeof(query), "SELECT created, `%s_admins`.user, ends, length, reason, RemoveType, type FROM `%s_comms` LEFT JOIN `%s_admins` ON `%s_comms`.aid = `%s_admins`.aid WHERE `%s_comms`.authid REGEXP '^STEAM_[0-9]:%s$' AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, g_SBPP_SQL_szPrefix, auth[8]); char targetName[MAX_NAME_LENGTH]; GetClientName(target, targetName, sizeof(targetName)); @@ -319,15 +307,15 @@ public Action OnListSourceCommsCmd(int client, int args) dataPack.WriteCell((client == 0) ? 0 : GetClientUserId(client)); dataPack.WriteString(targetName); - g_dbSQL.Query(OnListComms, query, dataPack, DBPrio_Low); + g_SBPP_SQL_dbHandle.Query(OnListComms, query, dataPack, DBPrio_Low); if (client == 0) { - ReplyToCommand(client, "%sNote: if you are using this command through an rcon tool, you will not see results.", Prefix); + ReplyToCommand(client, "%sNote: if you are using this command through an rcon tool, you will not see results.", SBPP_PREFIX); } else { - ReplyToCommand(client, "\x04%s\x01 Look for %N's comm results in console.", Prefix, target); + ReplyToCommand(client, "\x04%s\x01 Look for %N's comm results in console.", SBPP_PREFIX, target); } return Plugin_Handled; @@ -347,17 +335,17 @@ public void OnListComms(Database db, DBResultSet results, const char[] error, Da if (results == null) { - PrintListResponse(clientuid, client, "%sDB error while retrieving comms for %s:\n%s", Prefix, targetName, error); + PrintListResponse(clientuid, client, "%sDB error while retrieving comms for %s:\n%s", SBPP_PREFIX, targetName, error); return; } if (results.RowCount == 0) { - PrintListResponse(clientuid, client, "%sNo comms found for %s.", Prefix, targetName); + PrintListResponse(clientuid, client, "%sNo comms found for %s.", SBPP_PREFIX, targetName); return; } - PrintListResponse(clientuid, client, "%sListing comms for %s", Prefix, targetName); + PrintListResponse(clientuid, client, "%sListing comms for %s", SBPP_PREFIX, targetName); PrintListResponse(clientuid, client, "Ban Date Banned By Length End Date T R Reason"); PrintListResponse(clientuid, client, "-------------------------------------------------------------------------------"); while (results.FetchRow()) @@ -478,75 +466,4 @@ void PrintToBanAdmins(const char[] format, any ...) PrintToChat(i, "%s", msg); } } -} - -stock void ReadConfig() -{ - InitializeConfigParser(); - - if (g_ConfigParser == null) - { - return; - } - - char ConfigFile[PLATFORM_MAX_PATH]; - BuildPath(Path_SM, ConfigFile, sizeof(ConfigFile), "configs/sourcebans/sourcebans.cfg"); - - if (FileExists(ConfigFile)) - { - InternalReadConfig(ConfigFile); - } - else - { - char Error[PLATFORM_MAX_PATH + 64]; - FormatEx(Error, sizeof(Error), "FATAL *** ERROR *** can not find %s", ConfigFile); - SetFailState(Error); - } -} - -static void InitializeConfigParser() -{ - if (g_ConfigParser == null) - { - g_ConfigParser = new SMCParser(); - g_ConfigParser.OnEnterSection = ReadConfig_NewSection; - g_ConfigParser.OnKeyValue = ReadConfig_KeyValue; - g_ConfigParser.OnLeaveSection = ReadConfig_EndSection; - } -} - -static void InternalReadConfig(const char[] path) -{ - SMCError err = g_ConfigParser.ParseFile(path); - - if (err != SMCError_Okay) - { - char buffer[64]; - PrintToServer("%s", g_ConfigParser.GetErrorString(err, buffer, sizeof(buffer)) ? buffer : "Fatal parse error"); - } -} - -public SMCResult ReadConfig_NewSection(SMCParser smc, const char[] name, bool opt_quotes) -{ - return SMCParse_Continue; -} - -public SMCResult ReadConfig_KeyValue(SMCParser smc, const char[] key, const char[] value, bool key_quotes, bool value_quotes) -{ - if (strcmp("DatabasePrefix", key, false) == 0) - { - strcopy(g_DatabasePrefix, sizeof(g_DatabasePrefix), value); - - if (g_DatabasePrefix[0] == '\0') - { - g_DatabasePrefix = "sb"; - } - } - - return SMCParse_Continue; -} - -public SMCResult ReadConfig_EndSection(SMCParser smc) -{ - return SMCParse_Continue; -} +} \ No newline at end of file diff --git a/game/addons/sourcemod/scripting/sbpp_sleuth.sp b/game/addons/sourcemod/scripting/sbpp_sleuth.sp index ca87215a5..55d08eff4 100644 --- a/game/addons/sourcemod/scripting/sbpp_sleuth.sp +++ b/game/addons/sourcemod/scripting/sbpp_sleuth.sp @@ -133,7 +133,7 @@ public Action ReloadListCallBack(int client, int args) public void OnClientPostAdminCheck(int client) { - if ( g_dbSQL ) + if ( g_SBPP_SQL_dbHandle ) { if (CanUseSourcebans && !IsFakeClient(client)) { @@ -163,7 +163,7 @@ public void OnClientPostAdminCheck(int client) datapack.WriteString(IP); datapack.Reset(); - g_dbSQL.Query(SQL_CheckHim, query, datapack); + g_SBPP_SQL_dbHandle.Query(SQL_CheckHim, query, datapack); } } } diff --git a/game/addons/sourcemod/translations/sbpp.sql.phrases.txt b/game/addons/sourcemod/translations/sbpp.sql.phrases.txt deleted file mode 100644 index 6e3c723ab..000000000 --- a/game/addons/sourcemod/translations/sbpp.sql.phrases.txt +++ /dev/null @@ -1,8 +0,0 @@ -"Phrases" -{ - "Successful reconnect" - { - "en" "Successful reconnect to database." - "ru" "Успешное переподключение к базе данных." - } -} \ No newline at end of file diff --git a/game/addons/sourcemod/translations/sbpp/sql.phrases.txt b/game/addons/sourcemod/translations/sbpp/sql.phrases.txt new file mode 100644 index 000000000..af43a56bc --- /dev/null +++ b/game/addons/sourcemod/translations/sbpp/sql.phrases.txt @@ -0,0 +1,19 @@ +"Phrases" +{ + "Successful reconnect to database." + { + "en" "Successful reconnect to database." + "ru" "Успешное переподключение к базе данных." + } + "Unknown parse error." + { + "en" "Unknown parse error." + "ru" "Неизвестная ошибка чтения настроек." + } + "Database config not found '{1}'." + { + "#format" "{1:s}" + "en" "Database config not found '{1}'." + "ru" "Конфиг базы данных не найден '{1}'." + } +} \ No newline at end of file From 1cb2fa47bc4e826a87512bed350c8fb1962d04ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Mon, 20 Apr 2020 17:14:49 +0500 Subject: [PATCH 15/17] v3 Replaced static function variable to DataPack to store callbacks. Renamed `Issues` log to `Error`. Fixed `sbpp` dir permisson. Fixed tabulation. Changed ConnectCallback signature. Clean-up static forwards params. --- game/addons/sourcemod/scripting/sbpp/core.sp | 8 +- game/addons/sourcemod/scripting/sbpp/sql.sp | 102 ++++++++++--------- 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/game/addons/sourcemod/scripting/sbpp/core.sp b/game/addons/sourcemod/scripting/sbpp/core.sp index 6f783c8d8..b5ff7013b 100644 --- a/game/addons/sourcemod/scripting/sbpp/core.sp +++ b/game/addons/sourcemod/scripting/sbpp/core.sp @@ -6,15 +6,15 @@ #define SBPP_VERSION "1.7.0:1049" #define SBPP_LogMsg( LogToFile( SBPP_Core_szLog, -#define SBPP_LogIssue( LogToFile( SBPP_Core_szIssues, +#define SBPP_LogError( LogToFile( SBPP_Core_szError, -stock char SBPP_Core_szLog[PLATFORM_MAX_PATH], SBPP_Core_szIssues[PLATFORM_MAX_PATH]; +stock char SBPP_Core_szLog[PLATFORM_MAX_PATH], SBPP_Core_szError[PLATFORM_MAX_PATH]; stock const char SBPP_PREFIX[] = "\x04[SourceBans++]\x01 "; void SBPP_Core_Init () { BuildPath( Path_SM, SBPP_Core_szLog, sizeof SBPP_Core_szLog, "logs/sbpp" ); - CreateDirectory( SBPP_Core_szLog, 1 << 6 & 1 << 7 & 1 << 8 ); + CreateDirectory( SBPP_Core_szLog, 1 << 6 | 1 << 7 | 1 << 8 ); BuildPath( Path_SM, SBPP_Core_szLog, sizeof SBPP_Core_szLog, "logs/sbpp/sbpp.log" ); - BuildPath( Path_SM, SBPP_Core_szIssues, sizeof SBPP_Core_szIssues, "logs/sbpp/issues.log" ); + BuildPath( Path_SM, SBPP_Core_szError, sizeof SBPP_Core_szError, "logs/sbpp/issues.log" ); } \ No newline at end of file diff --git a/game/addons/sourcemod/scripting/sbpp/sql.sp b/game/addons/sourcemod/scripting/sbpp/sql.sp index 26d6b74c2..f4fd1f344 100644 --- a/game/addons/sourcemod/scripting/sbpp/sql.sp +++ b/game/addons/sourcemod/scripting/sbpp/sql.sp @@ -5,53 +5,56 @@ #include -stock Database g_SBPP_SQL_dbHandle; -stock char g_SBPP_SQL_szPrefix[32] = "sb"; +Database g_SBPP_SQL_dbHandle; +char g_SBPP_SQL_szPrefix[32] = "sb"; enum /* States of connection. */ { - SBPP_SQL_State_None = 0, - SBPP_SQL_State_Connecting = 1 << 0, - SBPP_SQL_State_Wait = 1 << 1, - SBPP_SQL_State_Connected = 1 << 2 + SBPP_SQL_State_None, + SBPP_SQL_State_Wait, + SBPP_SQL_State_Connecting, + SBPP_SQL_State_Connected } -typedef ConnectCallback = function void (const bool bSuccessful); +typedef ConnectCallback = function void (); -stock static int s_iState, s_iTimestamp; +#define SaveCallback(%1); if ( %1 != INVALID_FUNCTION ) { s_dpCallbacks.WriteFunction( %1 ); } + +stock static int s_iState; stock static bool s_bIgnoreForward; -stock static ConnectCallback s_fnCallback = INVALID_FUNCTION; /* For now, Database.Connect() can't have function as pass data. */ +stock static DataPack s_dpCallbacks; stock static char s_szPrevError[PLATFORM_MAX_PATH]; -stock static Handle s_hSBPP_SQL_Handle_Find, s_hSBPP_SQL_Handle_OnClose, s_hSBPP_SQL_Handle_OnUpdate, s_hSBPP_SQL_Prefix_OnUpdate; +stock static Handle s_gfSBPP_SQL_Handle_Find, s_gfSBPP_SQL_Handle_OnClose, s_gfSBPP_SQL_Handle_OnUpdate, s_gfSBPP_SQL_Prefix_OnUpdate; -forward Action _SBPP_SQL_Handle_Find (Database &db, int &iState, char szPrefix[sizeof g_SBPP_SQL_szPrefix], int &iTimestamp); -forward void _SBPP_SQL_Handle_OnClose (); -forward void _SBPP_SQL_Handle_OnUpdate (const Database db); -forward void _SBPP_SQL_Prefix_OnUpdate (const char[] szPrefix, const int iTimestamp); +forward Action _SBPP_SQL_Handle_Find (Database &db, int &iState, char szPrefix[sizeof g_SBPP_SQL_szPrefix]); +forward void _SBPP_SQL_Handle_OnClose (); +forward void _SBPP_SQL_Handle_OnUpdate (const Database db); +forward void _SBPP_SQL_Prefix_OnUpdate (const char szPrefix[sizeof g_SBPP_SQL_szPrefix]); void SBPP_SQL_Init (const ConnectCallback fnCallback = INVALID_FUNCTION) { + s_dpCallbacks = new DataPack(); + SaveCallback( fnCallback ); LoadTranslations( "sbpp/sql.phrases.txt" ); - s_hSBPP_SQL_Handle_Find = CreateGlobalForward( "_SBPP_SQL_Handle_Find", ET_Hook, Param_CellByRef, Param_CellByRef, Param_String, Param_CellByRef ); - s_hSBPP_SQL_Handle_OnClose = CreateGlobalForward( "_SBPP_SQL_Handle_OnClose", ET_Ignore ); - s_hSBPP_SQL_Handle_OnUpdate = CreateGlobalForward( "_SBPP_SQL_Handle_OnUpdate", ET_Ignore, Param_Cell ); - s_hSBPP_SQL_Prefix_OnUpdate = CreateGlobalForward( "_SBPP_SQL_Prefix_OnUpdate", ET_Ignore, Param_String ); - s_fnCallback = fnCallback; - SBPP_SQL_Find(); + s_gfSBPP_SQL_Handle_Find = new GlobalForward( "_SBPP_SQL_Handle_Find", ET_Hook, Param_CellByRef, Param_CellByRef, Param_String ); + s_gfSBPP_SQL_Handle_OnClose = new GlobalForward( "_SBPP_SQL_Handle_OnClose", ET_Ignore ); + s_gfSBPP_SQL_Handle_OnUpdate = new GlobalForward( "_SBPP_SQL_Handle_OnUpdate", ET_Ignore, Param_Cell ); + s_gfSBPP_SQL_Prefix_OnUpdate = new GlobalForward( "_SBPP_SQL_Prefix_OnUpdate", ET_Ignore, Param_String ); SBPP_SQL_Prefix_Read(); + SBPP_SQL_Find(); RegAdminCmd( "sbpp_prefix_reload", sbpp_prefix_reload_Handler, ADMFLAG_CONFIG, "Reload prefix of database tables." ); } stock void SBPP_SQL_Reconnect (const ConnectCallback fnCallback = INVALID_FUNCTION) { + SaveCallback( fnCallback ); if ( s_iState != SBPP_SQL_State_Connecting && s_iState != SBPP_SQL_State_Wait ) { s_bIgnoreForward = true; - Call_StartForward( s_hSBPP_SQL_Handle_OnClose ); + Call_StartForward( s_gfSBPP_SQL_Handle_OnClose ); Call_Finish(); s_bIgnoreForward = false; delete g_SBPP_SQL_dbHandle; - s_fnCallback = fnCallback; SBPP_SQL_Connect(); } } @@ -68,12 +71,12 @@ stock void SBPP_SQL_Prefix_Read () if ( err ) { Parser.GetErrorString( err, szBuf, sizeof szBuf ); - SBPP_LogIssue( szBuf[0] ? szBuf : "%t", "Unknown parse error." ); + SBPP_LogError( szBuf[0] ? szBuf : "%t", "Unknown parse error." ); } } else { - SBPP_LogIssue( "%t", "Database config not found '%s'.", szBuf ); + SBPP_LogError( "%t", "Database config not found '%s'.", szBuf ); } CloseHandle( Parser ); } @@ -92,7 +95,7 @@ stock static SMCResult ReadConfig_KeyValue (const SMCParser Parser, const char[] { strcopy( g_SBPP_SQL_szPrefix, sizeof g_SBPP_SQL_szPrefix, szVal ); s_bIgnoreForward = true; - Call_StartForward( s_hSBPP_SQL_Prefix_OnUpdate ); + Call_StartForward( s_gfSBPP_SQL_Prefix_OnUpdate ); Call_PushString( g_SBPP_SQL_szPrefix ); Call_Finish(); s_bIgnoreForward = false; @@ -105,11 +108,10 @@ stock static void SBPP_SQL_Find () { Database db; s_bIgnoreForward = true; - Call_StartForward( s_hSBPP_SQL_Handle_Find ); + Call_StartForward( s_gfSBPP_SQL_Handle_Find ); Call_PushCellRef( db ); Call_PushCellRef( s_iState ); Call_PushStringEx( g_SBPP_SQL_szPrefix, sizeof g_SBPP_SQL_szPrefix, SM_PARAM_STRING_UTF8, SM_PARAM_COPYBACK ); - Call_PushCellRef( s_iTimestamp ); Call_Finish(); s_bIgnoreForward = false; if ( s_iState == SBPP_SQL_State_None ) @@ -122,7 +124,9 @@ stock static void SBPP_SQL_Find () } else if ( s_iState == SBPP_SQL_State_Connected ) { - _SBPP_SQL_Handle_OnUpdate( db ); + g_SBPP_SQL_dbHandle = view_as( CloneHandle( db ) ); + s_iState = SBPP_SQL_State_Connected; + CallCallbacks(); } } @@ -134,7 +138,6 @@ stock static void SBPP_SQL_Connect () stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szError, const any aData) { - bool bSuccessful; if ( db ) { g_SBPP_SQL_dbHandle = db; @@ -142,8 +145,9 @@ stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szE { g_SBPP_SQL_dbHandle.SetCharset( "utf8" ); } + s_iState = SBPP_SQL_State_Connected; s_bIgnoreForward = true; - Call_StartForward( s_hSBPP_SQL_Handle_OnUpdate ); + Call_StartForward( s_gfSBPP_SQL_Handle_OnUpdate ); Call_PushCell( g_SBPP_SQL_dbHandle ); Call_Finish(); s_bIgnoreForward = false; @@ -152,7 +156,6 @@ stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szE SBPP_LogMsg( "%t", "Successful reconnect to database." ); s_szPrevError[0] = '\0'; } - bSuccessful = true; } else { @@ -163,11 +166,10 @@ stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szE strcopy( s_szPrevError, sizeof s_szPrevError, szError ); } } - s_iState = SBPP_SQL_State_Connected; - CallCallback( bSuccessful ); + CallCallbacks(); } -public Action _SBPP_SQL_Handle_Find (Database &db, int &iState, char szPrefix[sizeof g_SBPP_SQL_szPrefix], int &iTimestamp) +public Action _SBPP_SQL_Handle_Find (Database &db, int &iState, char szPrefix[sizeof g_SBPP_SQL_szPrefix]) { if ( !s_bIgnoreForward ) { @@ -176,14 +178,12 @@ public Action _SBPP_SQL_Handle_Find (Database &db, int &iState, char szPrefix[si db = g_SBPP_SQL_dbHandle; iState = SBPP_SQL_State_Connected; strcopy( szPrefix, sizeof szPrefix, g_SBPP_SQL_szPrefix ); - iTimestamp = s_iTimestamp; return Plugin_Stop; } else if ( s_iState == SBPP_SQL_State_Connecting ) { iState = SBPP_SQL_State_Wait; strcopy( szPrefix, sizeof szPrefix, g_SBPP_SQL_szPrefix ); - iTimestamp = s_iTimestamp; return Plugin_Stop; } } @@ -206,34 +206,36 @@ public void _SBPP_SQL_Handle_OnUpdate (const Database db) { g_SBPP_SQL_dbHandle = view_as( CloneHandle( db ) ); s_iState = SBPP_SQL_State_Connected; - CallCallback( true ); + CallCallbacks(); } } -public void _SBPP_SQL_Prefix_OnUpdate (const char[] szPrefix, const int iTimestamp) +public void _SBPP_SQL_Prefix_OnUpdate (const char szPrefix[sizeof g_SBPP_SQL_szPrefix]) { if ( !s_bIgnoreForward ) { - strcopy( g_SBPP_SQL_szPrefix, sizeof g_SBPP_SQL_szPrefix, szPrefix ); + g_SBPP_SQL_szPrefix = szPrefix; } } -stock static void CallCallback (const bool bSuccessful) +stock static Action CheckWait (const Handle tTimer) { - if ( s_fnCallback != INVALID_FUNCTION ) + if ( s_iState < SBPP_SQL_State_Connecting ) { - Call_StartFunction( null, s_fnCallback ); - Call_PushCell( bSuccessful ); - Call_Finish(); - s_fnCallback = INVALID_FUNCTION; + SBPP_SQL_Find(); } + return Plugin_Stop; } -stock static Action CheckWait (Handle tTimer) +stock static void CallCallbacks () { - if ( s_iState != SBPP_SQL_State_Connected ) + s_dpCallbacks.Reset(); + while ( s_dpCallbacks.IsReadable() ) { - SBPP_SQL_Find(); + Call_StartFunction( null, s_dpCallbacks.ReadFunction() ); + Call_Finish(); } - return Plugin_Stop; -} \ No newline at end of file + s_dpCallbacks.Reset( true ); +} + +#undef SaveCallback \ No newline at end of file From 35c5d387adffe221d96a6e73387bb19600d83da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Thu, 30 Apr 2020 14:26:29 +0500 Subject: [PATCH 16/17] Fix build with older SM versions --- game/addons/sourcemod/scripting/sbpp/sql.sp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/game/addons/sourcemod/scripting/sbpp/sql.sp b/game/addons/sourcemod/scripting/sbpp/sql.sp index f4fd1f344..b454fecfb 100644 --- a/game/addons/sourcemod/scripting/sbpp/sql.sp +++ b/game/addons/sourcemod/scripting/sbpp/sql.sp @@ -36,10 +36,17 @@ void SBPP_SQL_Init (const ConnectCallback fnCallback = INVALID_FUNCTION) s_dpCallbacks = new DataPack(); SaveCallback( fnCallback ); LoadTranslations( "sbpp/sql.phrases.txt" ); +#if SOURCEMOD_V_MINOR < 10 + s_gfSBPP_SQL_Handle_Find = CreateGlobalForward( "_SBPP_SQL_Handle_Find", ET_Hook, Param_CellByRef, Param_CellByRef, Param_String ); + s_gfSBPP_SQL_Handle_OnClose = CreateGlobalForward( "_SBPP_SQL_Handle_OnClose", ET_Ignore ); + s_gfSBPP_SQL_Handle_OnUpdate = CreateGlobalForward( "_SBPP_SQL_Handle_OnUpdate", ET_Ignore, Param_Cell ); + s_gfSBPP_SQL_Prefix_OnUpdate = CreateGlobalForward( "_SBPP_SQL_Prefix_OnUpdate", ET_Ignore, Param_String ); +#else s_gfSBPP_SQL_Handle_Find = new GlobalForward( "_SBPP_SQL_Handle_Find", ET_Hook, Param_CellByRef, Param_CellByRef, Param_String ); s_gfSBPP_SQL_Handle_OnClose = new GlobalForward( "_SBPP_SQL_Handle_OnClose", ET_Ignore ); s_gfSBPP_SQL_Handle_OnUpdate = new GlobalForward( "_SBPP_SQL_Handle_OnUpdate", ET_Ignore, Param_Cell ); s_gfSBPP_SQL_Prefix_OnUpdate = new GlobalForward( "_SBPP_SQL_Prefix_OnUpdate", ET_Ignore, Param_String ); +#endif SBPP_SQL_Prefix_Read(); SBPP_SQL_Find(); RegAdminCmd( "sbpp_prefix_reload", sbpp_prefix_reload_Handler, ADMFLAG_CONFIG, "Reload prefix of database tables." ); @@ -230,7 +237,11 @@ stock static Action CheckWait (const Handle tTimer) stock static void CallCallbacks () { s_dpCallbacks.Reset(); +#if SOURCEMOD_V_MINOR < 10 + while ( s_dpCallbacks.IsReadable(0) ) +#else while ( s_dpCallbacks.IsReadable() ) +#endif { Call_StartFunction( null, s_dpCallbacks.ReadFunction() ); Call_Finish(); From 0adf6e7dd74d2d14e59d1090672fdc27524bc6f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E1=BB=9D=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Tue, 12 May 2020 11:55:57 +0500 Subject: [PATCH 17/17] Renamed `Issues` log to `Errors`. --- game/addons/sourcemod/scripting/sbpp/core.sp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/addons/sourcemod/scripting/sbpp/core.sp b/game/addons/sourcemod/scripting/sbpp/core.sp index b5ff7013b..793a837bd 100644 --- a/game/addons/sourcemod/scripting/sbpp/core.sp +++ b/game/addons/sourcemod/scripting/sbpp/core.sp @@ -16,5 +16,5 @@ void SBPP_Core_Init () BuildPath( Path_SM, SBPP_Core_szLog, sizeof SBPP_Core_szLog, "logs/sbpp" ); CreateDirectory( SBPP_Core_szLog, 1 << 6 | 1 << 7 | 1 << 8 ); BuildPath( Path_SM, SBPP_Core_szLog, sizeof SBPP_Core_szLog, "logs/sbpp/sbpp.log" ); - BuildPath( Path_SM, SBPP_Core_szError, sizeof SBPP_Core_szError, "logs/sbpp/issues.log" ); + BuildPath( Path_SM, SBPP_Core_szError, sizeof SBPP_Core_szError, "logs/sbpp/errors.log" ); } \ No newline at end of file