diff --git a/game/addons/sourcemod/scripting/sbpp/core.sp b/game/addons/sourcemod/scripting/sbpp/core.sp new file mode 100644 index 000000000..793a837bd --- /dev/null +++ b/game/addons/sourcemod/scripting/sbpp/core.sp @@ -0,0 +1,20 @@ +#if defined _sbpp_core_included + #endinput +#endif +#define _sbpp_core_included + +#define SBPP_VERSION "1.7.0:1049" + +#define SBPP_LogMsg( LogToFile( SBPP_Core_szLog, +#define SBPP_LogError( LogToFile( SBPP_Core_szError, + +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 ); + 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/errors.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..b454fecfb --- /dev/null +++ b/game/addons/sourcemod/scripting/sbpp/sql.sp @@ -0,0 +1,252 @@ +#if defined _sbpp_sql_included + #endinput +#endif +#define _sbpp_sql_included + +#include + +Database g_SBPP_SQL_dbHandle; +char g_SBPP_SQL_szPrefix[32] = "sb"; + +enum /* States of connection. */ +{ + SBPP_SQL_State_None, + SBPP_SQL_State_Wait, + SBPP_SQL_State_Connecting, + SBPP_SQL_State_Connected +} + +typedef ConnectCallback = function void (); + +#define SaveCallback(%1); if ( %1 != INVALID_FUNCTION ) { s_dpCallbacks.WriteFunction( %1 ); } + +stock static int s_iState; +stock static bool s_bIgnoreForward; +stock static DataPack s_dpCallbacks; +stock static char s_szPrevError[PLATFORM_MAX_PATH]; +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]); +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" ); +#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." ); +} + +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_gfSBPP_SQL_Handle_OnClose ); + Call_Finish(); + s_bIgnoreForward = false; + delete g_SBPP_SQL_dbHandle; + 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_LogError( szBuf[0] ? szBuf : "%t", "Unknown parse error." ); + } + } + else + { + SBPP_LogError( "%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_gfSBPP_SQL_Prefix_OnUpdate ); + Call_PushString( g_SBPP_SQL_szPrefix ); + Call_Finish(); + s_bIgnoreForward = false; + } + } + return SMCParse_Continue; +} + +stock static void SBPP_SQL_Find () +{ + Database db; + s_bIgnoreForward = true; + 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_Finish(); + s_bIgnoreForward = false; + if ( s_iState == SBPP_SQL_State_None ) + { + SBPP_SQL_Connect(); + } + else if ( s_iState == SBPP_SQL_State_Wait ) + { + CreateTimer( 15.0, CheckWait ); + } + else if ( s_iState == SBPP_SQL_State_Connected ) + { + g_SBPP_SQL_dbHandle = view_as( CloneHandle( db ) ); + s_iState = SBPP_SQL_State_Connected; + CallCallbacks(); + } +} + +stock static void SBPP_SQL_Connect () +{ + s_iState = SBPP_SQL_State_Connecting; + Database.Connect( SBPP_SQL_Connect_Callback, "sourcebans" ); +} + +stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szError, const any aData) +{ + if ( db ) + { + g_SBPP_SQL_dbHandle = db; + if ( !g_SBPP_SQL_dbHandle.SetCharset( "utf8mb4" ) ) + { + g_SBPP_SQL_dbHandle.SetCharset( "utf8" ); + } + s_iState = SBPP_SQL_State_Connected; + s_bIgnoreForward = true; + Call_StartForward( s_gfSBPP_SQL_Handle_OnUpdate ); + Call_PushCell( g_SBPP_SQL_dbHandle ); + Call_Finish(); + s_bIgnoreForward = false; + if ( s_szPrevError[0] ) + { + SBPP_LogMsg( "%t", "Successful reconnect to database." ); + s_szPrevError[0] = '\0'; + } + } + else + { + s_iState = SBPP_SQL_State_None; + if ( strcmp( s_szPrevError, szError ) ) + { + SBPP_LogMsg( szError ); + strcopy( s_szPrevError, sizeof s_szPrevError, szError ); + } + } + CallCallbacks(); +} + +public Action _SBPP_SQL_Handle_Find (Database &db, int &iState, char szPrefix[sizeof g_SBPP_SQL_szPrefix]) +{ + if ( !s_bIgnoreForward ) + { + if ( g_SBPP_SQL_dbHandle ) + { + db = g_SBPP_SQL_dbHandle; + iState = SBPP_SQL_State_Connected; + strcopy( szPrefix, sizeof szPrefix, g_SBPP_SQL_szPrefix ); + return Plugin_Stop; + } + else if ( s_iState == SBPP_SQL_State_Connecting ) + { + iState = SBPP_SQL_State_Wait; + strcopy( szPrefix, sizeof szPrefix, g_SBPP_SQL_szPrefix ); + return Plugin_Stop; + } + } + return Plugin_Continue; +} + +public void _SBPP_SQL_Handle_OnClose () +{ + if ( !s_bIgnoreForward ) + { + delete g_SBPP_SQL_dbHandle; + s_iState = SBPP_SQL_State_Wait; + CreateTimer( 15.0, CheckWait ); + } +} + +public void _SBPP_SQL_Handle_OnUpdate (const Database db) +{ + if ( !s_bIgnoreForward ) + { + g_SBPP_SQL_dbHandle = view_as( CloneHandle( db ) ); + s_iState = SBPP_SQL_State_Connected; + CallCallbacks(); + } +} + +public void _SBPP_SQL_Prefix_OnUpdate (const char szPrefix[sizeof g_SBPP_SQL_szPrefix]) +{ + if ( !s_bIgnoreForward ) + { + g_SBPP_SQL_szPrefix = szPrefix; + } +} + +stock static Action CheckWait (const Handle tTimer) +{ + if ( s_iState < SBPP_SQL_State_Connecting ) + { + SBPP_SQL_Find(); + } + return Plugin_Stop; +} + +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(); + } + s_dpCallbacks.Reset( true ); +} + +#undef SaveCallback \ 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..903c8bed9 100644 --- a/game/addons/sourcemod/scripting/sbpp_checker.sp +++ b/game/addons/sourcemod/scripting/sbpp_checker.sp @@ -24,75 +24,57 @@ // // ************************************************************************* -#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 -#define Prefix "\x04[SourceBans++]\x01 " - -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() { - ReadConfig(); -} - -public Action OnReloadCmd(int client, int args) -{ - ReadConfig(); - 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; + SBPP_SQL_Prefix_Read(); } public void OnClientAuthorized(int client, const char[] auth) { - if (g_DB == null) + if ( !g_SBPP_SQL_dbHandle ) + { + SBPP_SQL_Reconnect(); return; - + } /* Do not check bots nor check player with lan steamid. */ if (auth[0] == 'B' || auth[9] == 'L') return; 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_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) @@ -107,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":"")); } } @@ -124,8 +106,9 @@ public Action OnListSourceBansCmd(int client, int args) ReplyToCommand(client, LISTBANS_USAGE); } - if (g_DB == INVALID_HANDLE) + if ( !g_SBPP_SQL_dbHandle ) { + SBPP_SQL_Reconnect(); ReplyToCommand(client, "Error: Database not ready."); return Plugin_Handled; } @@ -150,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)); @@ -159,15 +142,15 @@ 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_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; @@ -187,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()) @@ -290,7 +273,7 @@ public Action OnListSourceCommsCmd(int client, int args) ReplyToCommand(client, LISTCOMMS_USAGE); } - if (g_DB == INVALID_HANDLE) + if ( !g_SBPP_SQL_dbHandle ) { ReplyToCommand(client, "Error: Database not ready."); return Plugin_Handled; @@ -315,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)); @@ -324,15 +307,15 @@ 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_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; @@ -352,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()) @@ -483,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 f09cc2b39..55d08eff4 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,18 +115,6 @@ 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(); @@ -143,37 +133,44 @@ public Action ReloadListCallBack(int client, int args) public void OnClientPostAdminCheck(int client) { - if (CanUseSourcebans && !IsFakeClient(client)) + if ( g_SBPP_SQL_dbHandle ) { - 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; + } + + if (g_hAllowedArray.FindString(steamid) == -1) + { + char IP[32], Prefix[64]; + GetClientIP(client, IP, sizeof(IP)); - g_cVar_sbprefix.GetString(Prefix, sizeof(Prefix)); + 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(); - hDatabase.Query(SQL_CheckHim, query, datapack); + g_SBPP_SQL_dbHandle.Query(SQL_CheckHim, query, datapack); + } } } + else + { + SBPP_SQL_Reconnect(); + } } public void SQL_CheckHim(Database db, DBResultSet results, const char[] error, DataPack dataPack) @@ -188,7 +185,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; } @@ -263,13 +260,13 @@ 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"); 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..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