#include #if defined _steam_backend #include "steam_backend.sp" #endif //MYSQL QUERYS char g_SQL_QUERY_UNBAN[512] = "UPDATE %s SET active = '0', unbanned_by_id = '%s', unbanned_timestamp = CURRENT_TIMESTAMP WHERE id = %i"; char g_SQL_QUERY_DETECT_NEW_BAN[512] = "INSERT INTO %s (player_name, steam_id, account_id, ban_length, ban_reason, banned_by, banned_by_id, ip, timestamp, active) VALUES ('%s','%s','%d','%d','%s','SERVER','STEAM_0:0:63666481','%s',CURRENT_TIMESTAMP, '1')"; char g_SQL_QUERY_SEARCH_BAN[512] = "SELECT ban_length*60, ban_reason, UNIX_TIMESTAMP(timestamp), steam_id, ip, player_name, id, active FROM %s WHERE active = '1' and steam_id = '%s' OR active = '1' and ip = '%s' ORDER BY `id` DESC"; char g_SQL_QUERY_SEARCH_BAN_WITHOUT_IP[512] = "SELECT ban_length*60, ban_reason, UNIX_TIMESTAMP(timestamp), steam_id, ip, player_name, id, active FROM %s WHERE active = '1' and steam_id = '%s' ORDER BY `id` DESC"; char g_SQL_QUERY_NEW_BAN[512] = "INSERT INTO %s (player_name, steam_id, account_id, ban_length, ban_reason, banned_by, banned_by_id, ip, timestamp) VALUES ('%s','%s','%d','%d','%s','%s','%s','%s',CURRENT_TIMESTAMP)"; //RESPONDES char g_Responde_ClientInBan[256] = "You banned on this server!\nTime remain: {minutes_remain}"; char g_Responde_ClientInPermaBan[256] = "You perma banned on this server!"; char g_Responde_DetectNewClient[256] = "You use new account on banned IP"; //CONST char g_LocalhostIP[15] = "127.0.0.1"; char g_BansChatPrefix[16] = "[FSB.Bans]"; char g_BansTableName[32] = "bans_tablename"; Handle g_hBansDatabase = INVALID_HANDLE; bool g_ipCheckNewAccount = false; //EVENTS //player_connect public Action:EventConnect(Handle:event, const String:name[], bool:dontBroadcast) { SetEventBroadcast(event, true); char player_name[64]; GetEventString(event, "name", player_name, 64); PrintToConsoleAll("%s connect to server", player_name); return Plugin_Continue; } //player_disconnect public Action:EventDisconnect(Handle:event, const String:name[], bool:dontBroadcast) { SetEventBroadcast(event, true); char player_name[64]; char reason[64]; GetEventString(event, "name", player_name, 64); GetEventString(event, "reason", reason, 64); PrintToConsoleAll("%s leave from server with reason: %s", player_name, reason); return Plugin_Continue; } //FUNCTIONS stock void BanCheckPlayer(int client){ char Query[512], client_ip[15], client_auth[32]; GetClientIP(client, client_ip, sizeof(client_ip)); GetClientAuthId(client, AuthId_Steam2, client_auth, sizeof(client_auth)); Format(Query, sizeof(Query), g_SQL_QUERY_SEARCH_BAN, g_BansTableName, client_auth, client_ip); SQL_TQuery(g_hBansDatabase, Callback_BanCheck, Query, GetClientUserId(client)); return; } //MYSQL public DB_Processing(Handle:owner, Handle:hndl, const String:error[], any:data) { if (hndl == INVALID_HANDLE) { LogError("%s Query failed! %s", g_BansChatPrefix, error); } return; } stock bool BansTable_VerifyTable(const char[] database_name, const char[] table_name){ char error[255], query[2048]; new db = SQL_Connect(database_name, true, error, sizeof(error)); if (db == INVALID_HANDLE) { LogError("%s Cannot connect to Database: %s", g_BansChatPrefix, database_name); return false; } Format(query, sizeof(query), "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", "CREATE TABLE IF NOT EXISTS `", g_BansTableName, "` (", " `id` int(11) NOT NULL,", " `steam_id` varchar(32) NOT NULL,", " `account_id` bigint(20) NOT NULL,", " `player_name` varchar(65) NOT NULL,", " `ban_length` int(1) NOT NULL DEFAULT 0,", " `ban_reason` varchar(100) NOT NULL,", " `banned_by` varchar(100) NOT NULL,", " `banned_by_id` varchar(32) NOT NULL,", " `ip` varchar(15) NOT NULL,", " `timestamp` timestamp NOT NULL DEFAULT current_timestamp(),", " `active` tinyint(1) NOT NULL DEFAULT 1,", " `unbanned_by_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,", " `unbanned_timestamp` timestamp NULL DEFAULT NULL", " PRIMARY KEY (`id`)", " ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1;"); bool success = SQL_FastQuery(db, query); if(!success) { SQL_GetError(db, error, sizeof(error)); LogError("%s [ERROR] Unable to verify mysql_bans table:%s", g_BansChatPrefix, query); LogError("%s [ERROR] %s", g_BansChatPrefix, error); } CloseHandle(db); return true; } //MYSQL CALLBACKS public Callback_BanCheck(Handle:owner, Handle:hndl, const String:error[], any:data) { int client; if ((client = GetClientOfUserId(data)) == 0) { return; } if (hndl == INVALID_HANDLE) { LogError("%s Query failed! %s", g_BansChatPrefix, error); char lKickReason[256] = "DB ERROR"; //GetConVarString(cvKickReasonIfQueryFailed, lKickReason, 256); /////////////////////////////////////// #if defined _steam_backend KickClient_WithBackendMessage(client, lKickReason); #else KickClient(client, lKickReason); #endif /////////////////////////////////////// return; } if (SQL_FetchRow(hndl)) { char Query[512]; ///////////////////////////////////////////////////////////////////////// char cl_PlayerName[64]; GetClientName(client, cl_PlayerName, sizeof(cl_PlayerName)); char cl_SteamID[32]; GetClientAuthId(client, AuthId_Steam2, cl_SteamID, sizeof(cl_SteamID)); char cl_IP[15]; GetClientIP(client, cl_IP, sizeof(cl_IP)); //Get VARS from DB int db_BanLength = SQL_FetchInt(hndl, 0); char db_BanReason[256]; SQL_FetchString(hndl, 1, db_BanReason, 256); int db_BanRemaining = db_BanLength - (GetTime() - SQL_FetchInt(hndl, 2)); char db_SteamID[32]; SQL_FetchString(hndl, 3, db_SteamID, 32); char db_IP[15]; SQL_FetchString(hndl, 4, db_IP, 15); int db_BanID = SQL_FetchInt(hndl, 6); //PrintToServer("Time Remaining: %d sec", db_BanRemaining); //Check Ban is done if not perma if (db_BanLength != 0) { if (db_BanRemaining <= 0) { Format(Query, sizeof(Query), g_SQL_QUERY_UNBAN, g_BansTableName, "STEAM_0:0:0", db_BanID); SQL_TQuery(g_hBansDatabase, DB_Processing, Query); LogMessage("User %s(%s) has been unbanned by elapse of time: %d seconds", cl_PlayerName, cl_SteamID, db_BanRemaining); return; } } //Check if client conncted from other steam, use old IP //DONT CHECKING NEW ACCOUNT ALREADY IN DB OR NOT!!!!!!!! char kick_reason[256]; if (g_ipCheckNewAccount && !StrEqual(g_LocalhostIP,db_IP) && !StrEqual(cl_SteamID, db_SteamID) && StrEqual(cl_IP, db_IP)) { int cl_AccountID = GetSteamAccountID(client, true); char db_PlayerName[64]; SQL_FetchString(hndl, 5, db_PlayerName, sizeof(db_PlayerName)); //LogMessage("%s(%s) new account, other banned account %s(%s), have equal IP Addresses. Ban new account perma.", cl_PlayerName, cl_SteamID, db_PlayerName, db_SteamID); char es_PlayerName[64]; SQL_EscapeString(g_hBansDatabase, cl_PlayerName, es_PlayerName, sizeof(es_PlayerName)); char cl_BanReason[256]; Format(cl_BanReason, sizeof(cl_BanReason), "Use new account, other account(%s) is banned", db_SteamID); char es_BanReason[256]; SQL_EscapeString(g_hBansDatabase, cl_BanReason, es_BanReason, sizeof(es_BanReason)); Format(Query, sizeof(Query), g_SQL_QUERY_DETECT_NEW_BAN, g_BansTableName, es_PlayerName, cl_SteamID, cl_AccountID, 0, es_BanReason, cl_IP); //LogMessage(Query); SQL_TQuery(g_hBansDatabase, DB_Processing, Query); Format(kick_reason, sizeof(kick_reason), g_Responde_DetectNewClient); } // if (db_BanLength == 0) { Format(kick_reason, sizeof(kick_reason), g_Responde_ClientInPermaBan); } else { Format(kick_reason, sizeof(kick_reason), g_Responde_ClientInBan); } /////////////////////////////////////// if (StrContains(kick_reason, "{reason}") != 0){ ReplaceString(kick_reason, sizeof(kick_reason), "{reason}", db_BanReason); } if (StrContains(kick_reason, "{minutes_remain}") != 0){ char s_time[16]; IntToString(db_BanRemaining / 60, s_time, sizeof(s_time)); ReplaceString(kick_reason, sizeof(kick_reason), "{minutes_remain}", s_time); } if (StrContains(kick_reason, "{banid}") != 0){ char banid[16]; IntToString(db_BanID, banid, sizeof(banid)); ReplaceString(kick_reason, sizeof(kick_reason), "{banid}", banid); } /////////////////////////////////////// #if defined _steam_backend KickClient_WithBackendMessage(client, kick_reason); #else KickClient(client, kick_reason); #endif /////////////////////////////////////// return; } else { return; } } public Callback_SearchAndUnban(Handle:owner, Handle:hndl, const String:error[], any:data) { int admin; if ((admin = GetClientOfUserId(data)) == 0) { return; } if (hndl == INVALID_HANDLE) { LogError("Query failed! %s", error); return; } if (SQL_FetchRow(hndl)) { char Query[1024], admin_steam[32]; GetClientAuthId(admin, AuthId_Steam2, admin_steam, sizeof(admin_steam)); int db_BanID = SQL_FetchInt(hndl, 6); Format(Query, sizeof(Query), g_SQL_QUERY_UNBAN, g_BansTableName, admin_steam, db_BanID); SQL_TQuery(g_hBansDatabase, Callback_Unban, Query, GetClientUserId(admin)); } else { ReplyToCommand(admin, "%s Karlik not found in bans...", g_BansChatPrefix); } return; } public Callback_Unban(Handle:owner, Handle:hndl, const String:error[], any:data) { int admin; if ((admin = GetClientOfUserId(data)) == 0) { return; } if (hndl == INVALID_HANDLE) { LogError("Query failed! %s", error); return; } if(SQL_HasResultSet(hndl)) { ReplyToCommand(admin, "%s Karlik unbanned", g_BansChatPrefix); } else { ReplyToCommand(admin, "%s Karlik not found in bans...", g_BansChatPrefix); } return; } //ACTIONS public Action:OnBanClient(int client, int time, int flags, const String:reason[], const String:kick_message[], const String:command[], any:admin) { if (client == 0) { ReplyToCommand(client, "Cannot ban from RCON"); return Plugin_Stop; } char Query[512]; char QueryPayload[11][256]; /* 0/5 - Client SteamID 1/6 - Admin SteamID 2/7 - Client Name 3/8 - Admin Name 4/9 - Client IP 10 - Ban Reason */ GetClientAuthId(client, AuthId_Steam2, QueryPayload[0], sizeof(QueryPayload[])); GetClientAuthId(admin, AuthId_Steam2, QueryPayload[1], sizeof(QueryPayload[])); GetClientName(client, QueryPayload[2], sizeof(QueryPayload[])); GetClientName(admin, QueryPayload[3], sizeof(QueryPayload[])); GetClientIP(client,QueryPayload[4], sizeof(QueryPayload[])); for (new i = 0; i < 5; i++) { SQL_EscapeString(g_hBansDatabase, QueryPayload[i], QueryPayload[5+i], 256); } SQL_EscapeString(g_hBansDatabase, reason, QueryPayload[10], 256); int cl_AccountID = GetSteamAccountID(client, true); Format(Query, sizeof(Query), g_SQL_QUERY_NEW_BAN, g_BansTableName, QueryPayload[7], QueryPayload[5], cl_AccountID, time, QueryPayload[10], QueryPayload[8], QueryPayload[6], QueryPayload[9]); SQL_TQuery(g_hBansDatabase, Callback_BanCheck, Query, GetClientOfUserId(admin)); return Plugin_Stop; } public Action:OnRemoveBan(const String:steam_id[], flags, const String:command[], any:admin) { char Query[512]; Format(Query, sizeof(Query), g_SQL_QUERY_SEARCH_BAN_WITHOUT_IP, g_BansTableName, steam_id); SQL_TQuery(g_hBansDatabase, Callback_SearchAndUnban, Query, GetClientOfUserId(admin)); return Plugin_Stop; }