diff --git a/game/addons/sourcemod/scripting/sbpp_checker.sp b/game/addons/sourcemod/scripting/sbpp_checker.sp index 7e5f5e214..e58e30c82 100644 --- a/game/addons/sourcemod/scripting/sbpp_checker.sp +++ b/game/addons/sourcemod/scripting/sbpp_checker.sp @@ -23,535 +23,364 @@ // Page: // // ************************************************************************* - #pragma semicolon 1 #pragma newdecls required -#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 = -{ +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, - url = "https://sbpp.github.io" -}; + version = "1.7.0", + url = "https://sbpp.github.io" }; -public void OnPluginStart() -{ - 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"); -} +static const char PREFIX[] = "\x04[SourceBans++]\x01 "; +static const char LISTBANS_USAGE[] = "sm_listbans <#userid|name> - Lists a user's prior bans from Sourcebans"; +static const char LISTCOMMS_USAGE[] = "sm_listcomms <#userid|name> - Lists a user's prior comms from Sourcebans"; +Database g_DB; +char g_DatabasePrefix[10] = "sb"; +bool g_bConnecting; /* One connecting per time. */ +char g_szLastError[256]; /* To prevent spam same error. */ -public void OnMapStart() +public void OnPluginStart () { + DB_Connect(); + LoadTranslations( "common.phrases" ); + LoadTranslations( "sbpp_checker.phrases" ); + RegAdminCmd( "sm_listbans", sm_list_Handler, ADMFLAG_GENERIC, LISTBANS_USAGE ); + RegAdminCmd( "sm_listcomms", sm_list_Handler, ADMFLAG_GENERIC, LISTCOMMS_USAGE ); + RegAdminCmd( "sb_reload", sb_reload_Handler, ADMFLAG_CONFIG, "Reload sourcebans config and ban reason menu options." ); ReadConfig(); } -public Action OnReloadCmd(int client, int args) +public void OnMapStart () { ReadConfig(); - return Plugin_Handled; } -public void OnDatabaseConnected(Database db, const char[] error, any data) +public void OnClientAuthorized (int iClient, const char[] szAuth) { - 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) - 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); -} - -public void OnConnectBanCheck(Database db, DBResultSet results, const char[] error, any userid) -{ - int client = GetClientOfUserId(userid); - if (!client || results == null || !results.FetchRow()) - return; - - int bancount = results.FetchInt(0); - int commcount = 0; - if(results.FetchRow()){ - 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":"")); - } - else if ( commcount ) { - PrintToBanAdmins("%s%t", 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":"")); - } -} - -public Action OnListSourceBansCmd(int client, int args) -{ - if (args < 1) + if ( !g_DB ) { - ReplyToCommand(client, LISTBANS_USAGE); + DB_Connect(); } - - if (g_DB == INVALID_HANDLE) - { - ReplyToCommand(client, "Error: Database not ready."); - return Plugin_Handled; - } - - char targetarg[64]; - GetCmdArg(1, targetarg, sizeof(targetarg)); - - int target = FindTarget(client, targetarg, true, true); - if (target == INVALID_TARGET) - { - ReplyToCommand(client, "Error: Could not find a target matching '%s'.", targetarg); - return Plugin_Handled; - } - - char auth[32]; - if (!GetClientAuthId(target, AuthId_Steam2, auth, sizeof(auth)) - || auth[0] == 'B' || auth[9] == 'L') + else if ( szAuth[0] != 'B' && szAuth[9] != 'L' ) { - ReplyToCommand(client, "Error: Could not retrieve %N's steam id.", target); - return Plugin_Handled; + /* Do not check bots nor check player with lan steamid. */ + char szIP[30], szQuery[320 + sizeof g_DatabasePrefix * 2 + sizeof szIP]; + GetClientIP( iClient, szIP, sizeof szIP ); + FormatEx( szQuery, sizeof szQuery, "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, szAuth[8], szIP, g_DatabasePrefix, szAuth[8] ); + g_DB.Query( DB_OnClientAuthorized_Callback, szQuery, GetClientUserId( iClient ), DBPrio_Low ); } - - 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); - - char targetName[MAX_NAME_LENGTH]; - GetClientName(target, targetName, sizeof(targetName)); - - DataPack dataPack = new DataPack(); - dataPack.WriteCell((client == 0) ? 0 : GetClientUserId(client)); - dataPack.WriteString(targetName); - - g_DB.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); - } - else - { - ReplyToCommand(client, "\x04%s\x01 Look for %N's ban results in console.", Prefix, target); - } - - return Plugin_Handled; } -public void OnListBans(Database db, DBResultSet results, const char[] error, DataPack dataPack) +stock Action sm_list_Handler (int iClient, int iArgs) { - dataPack.Reset(); - int clientuid = dataPack.ReadCell(); - int client = GetClientOfUserId(clientuid); - char targetName[MAX_NAME_LENGTH]; - dataPack.ReadString(targetName, sizeof(targetName)); - delete dataPack; - - if (clientuid > 0 && client == 0) - return; - - if (results == null) + if ( !g_DB ) { - PrintListResponse(clientuid, client, "%sDB error while retrieving bans for %s:\n%s", Prefix, targetName, error); - return; + DB_Connect(); + ReplyToCommand( iClient, "Plugin not connected to database. Try later." ); } - - if (results.RowCount == 0) - { - PrintListResponse(clientuid, client, "%sNo bans found for %s.", Prefix, targetName); - return; - } - - PrintListResponse(clientuid, client, "%sListing bans for %s", Prefix, targetName); - PrintListResponse(clientuid, client, "Ban Date Banned By Length End Date R Reason"); - PrintListResponse(clientuid, client, "-------------------------------------------------------------------------------"); - while (results.FetchRow()) + else { - char createddate[11] = " "; - char bannedby[11] = " "; - char lenstring[11] = "N/A "; - char enddate[11] = "N/A "; - char reason[28]; - char RemoveType[2] = " "; - - if (!results.IsFieldNull(0)) - { - FormatTime(createddate, sizeof(createddate), "%Y-%m-%d", results.FetchInt(0)); - } - - if (!results.IsFieldNull(1)) + char szBuf[30]; + GetCmdArg( 0, szBuf, sizeof szBuf ); + bool bBans = StrEqual( "sm_listbans", szBuf, false ); + if ( iArgs ) { - int size_bannedby = sizeof(bannedby); - results.FetchString(1, bannedby, size_bannedby); - int len = results.FetchSize(1); - if (len > size_bannedby - 1) - { - reason[size_bannedby - 4] = '.'; - reason[size_bannedby - 3] = '.'; - reason[size_bannedby - 2] = '.'; - } - else + char szTarget[64]; + GetCmdArg( 1, szTarget, sizeof szTarget ); + int iTarget = FindTarget( iClient, szTarget, true, true ); + if ( iTarget != -1 ) { - for (int i = len; i < size_bannedby - 1; i++) + char szAuth[32]; + if ( GetClientAuthId( iTarget, AuthId_Steam2, szAuth, sizeof szAuth ) && szAuth[0] != 'B' && szAuth[9] != 'L' ) + { + char szQuery[1024], szTargetName[MAX_NAME_LENGTH]; + GetClientName( iTarget, szTargetName, sizeof szTargetName ); + if ( bBans ) + { + GetClientIP( iTarget, szBuf, sizeof szBuf ); + FormatEx( szQuery, sizeof szQuery, "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, szAuth[8], szBuf ); + } + else + { + FormatEx( szQuery, sizeof szQuery, "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, szAuth[8] ); + } + DataPack hPack = new DataPack(); + hPack.WriteCell( !iClient ? 0 : GetClientUserId( iClient ) ); + hPack.WriteCell( bBans ); + hPack.WriteString( szTargetName ); + g_DB.Query( DB_list_Callback, szQuery, hPack, DBPrio_Low ); + if ( iClient ) + { + ReplyToCommand( iClient, "\x04%s\x01 Look for %N's %s results in console.", PREFIX, iTarget, bBans ? "ban" : "comm" ); + } + else + { + ReplyToCommand( iClient, "%sNote: if you are using this command through an rcon tool, you will not see results.", PREFIX ); + } + } + else { - bannedby[i] = ' '; + ReplyToCommand( iClient, "Error: Could not retrieve %N's steam id.", iTarget ); } } - } - - // NOT NULL - int size_lenstring = sizeof(lenstring); - int length = results.FetchInt(3); - if (length == 0) - { - strcopy(lenstring, size_lenstring, "Permanent "); - } - else - { - int len = IntToString(length, lenstring, size_lenstring); - if (len < size_lenstring - 1) + else { - // change the '\0' to a ' '. the original \0 at the end will still be there - lenstring[len] = ' '; + ReplyToCommand( iClient, "Error: Could not find a target matching '%s'.", szTarget ); } } - - if (!results.IsFieldNull(2)) - { - FormatTime(enddate, sizeof(enddate), "%Y-%m-%d", results.FetchInt(2)); - } - - // NOT NULL - int reason_size = sizeof(reason); - results.FetchString(4, reason, reason_size); - int len = results.FetchSize(4); - if (len > reason_size - 1) - { - reason[reason_size - 4] = '.'; - reason[reason_size - 3] = '.'; - reason[reason_size - 2] = '.'; - } else { - for (int i = len; i < reason_size - 1; i++) - { - reason[i] = ' '; - } + ReplyToCommand( iClient, bBans ? LISTBANS_USAGE : LISTBANS_USAGE ); } - - if (!results.IsFieldNull(5)) - { - results.FetchString(5, RemoveType, sizeof(RemoveType)); - } - - PrintListResponse(clientuid, client, "%s %s %s %s %s %s", createddate, bannedby, lenstring, enddate, RemoveType, reason); } + return Plugin_Handled; } -public Action OnListSourceCommsCmd(int client, int args) +stock Action sb_reload_Handler (int iClient, int iArgs) { - if (args < 1) - { - ReplyToCommand(client, LISTCOMMS_USAGE); - } - - if (g_DB == INVALID_HANDLE) - { - ReplyToCommand(client, "Error: Database not ready."); - return Plugin_Handled; - } - - char targetarg[64]; - GetCmdArg(1, targetarg, sizeof(targetarg)); - - int target = FindTarget(client, targetarg, true, true); - if (target == INVALID_TARGET) - { - ReplyToCommand(client, "Error: Could not find a target matching '%s'.", targetarg); - return Plugin_Handled; - } - - char auth[32]; - if (!GetClientAuthId(target, AuthId_Steam2, auth, sizeof(auth)) - || auth[0] == 'B' || auth[9] == 'L') - { - ReplyToCommand(client, "Error: Could not retrieve %N's steam id.", target); - return Plugin_Handled; - } - - 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]); - - char targetName[MAX_NAME_LENGTH]; - GetClientName(target, targetName, sizeof(targetName)); - - DataPack dataPack = new DataPack(); - dataPack.WriteCell((client == 0) ? 0 : GetClientUserId(client)); - dataPack.WriteString(targetName); - - g_DB.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); - } - else + ReadConfig(); + if ( !g_DB || g_szLastError[0] ) { - ReplyToCommand(client, "\x04%s\x01 Look for %N's comm results in console.", Prefix, target); + DB_Connect(); } - return Plugin_Handled; } -public void OnListComms(Database db, DBResultSet results, const char[] error, DataPack dataPack) +stock void PrintToAdmins (const char[] szFormat, any ...) { - dataPack.Reset(); - int clientuid = dataPack.ReadCell(); - int client = GetClientOfUserId(clientuid); - char targetName[MAX_NAME_LENGTH]; - dataPack.ReadString(targetName, sizeof(targetName)); - delete dataPack; - - if (clientuid > 0 && client == 0) - return; - - if (results == null) - { - PrintListResponse(clientuid, client, "%sDB error while retrieving comms for %s:\n%s", Prefix, targetName, error); - return; - } - - if (results.RowCount == 0) - { - PrintListResponse(clientuid, client, "%sNo comms found for %s.", Prefix, targetName); - return; - } - - PrintListResponse(clientuid, client, "%sListing comms for %s", Prefix, targetName); - PrintListResponse(clientuid, client, "Ban Date Banned By Length End Date T R Reason"); - PrintListResponse(clientuid, client, "-------------------------------------------------------------------------------"); - while (results.FetchRow()) + char szMsg[256]; + for ( int i = 1; i <= MaxClients; ++i ) { - char createddate[11] = " "; - char bannedby[11] = " "; - char lenstring[11] = "N/A "; - char enddate[11] = "N/A "; - char reason[23]; - char CommType[2] = " "; - char RemoveType[2] = " "; - - if (!results.IsFieldNull(0)) + if ( IsClientInGame(i) && !IsFakeClient(i) && CheckCommandAccess( i, "sm_listsourcebans", ADMFLAG_GENERIC ) ) { - FormatTime(createddate, sizeof(createddate), "%Y-%m-%d", results.FetchInt(0)); - } - - if (!results.IsFieldNull(1)) - { - int size_bannedby = sizeof(bannedby); - results.FetchString(1, bannedby, size_bannedby); - int len = results.FetchSize(1); - if (len > size_bannedby - 1) - { - reason[size_bannedby - 4] = '.'; - reason[size_bannedby - 3] = '.'; - reason[size_bannedby - 2] = '.'; - } - else - { - for (int i = len; i < size_bannedby - 1; i++) - { - bannedby[i] = ' '; - } - } - } - - // NOT NULL - int size_lenstring = sizeof(lenstring); - int length = results.FetchInt(3); - if (length == 0) - { - strcopy(lenstring, size_lenstring, "Permanent "); - } - else - { - int len = IntToString(length, lenstring, size_lenstring); - if (len < size_lenstring - 1) - { - // change the '\0' to a ' '. the original \0 at the end will still be there - lenstring[len] = ' '; - } - } - - if (!results.IsFieldNull(2)) - { - FormatTime(enddate, sizeof(enddate), "%Y-%m-%d", results.FetchInt(2)); - } - - // NOT NULL - int reason_size = sizeof(reason); - results.FetchString(4, reason, reason_size); - int len = results.FetchSize(4); - if (len > reason_size - 1) - { - reason[reason_size - 4] = '.'; - reason[reason_size - 3] = '.'; - reason[reason_size - 2] = '.'; - } - else - { - for (int i = len; i < reason_size - 1; i++) - { - reason[i] = ' '; - } - } - - if (!results.IsFieldNull(5)) - { - results.FetchString(5, RemoveType, sizeof(RemoveType)); + SetGlobalTransTarget(i); + VFormat( szMsg, sizeof szMsg, szFormat, 2 ); + PrintToChat( i, szMsg ); } - // NOT NULL - results.FetchString(6, CommType, sizeof(RemoveType)); - if(StrEqual(CommType,"1")) - strcopy(CommType, sizeof(CommType), "M"); - if(StrEqual(CommType,"2")) - strcopy(CommType, sizeof(CommType), "G"); - - PrintListResponse(clientuid, client, "%s %s %s %s %s %s %s", createddate, bannedby, lenstring, enddate, CommType, RemoveType, reason); } } -void PrintListResponse(int userid, int client, const char[] format, any ...) +stock void ReadConfig () { - char msg[192]; - VFormat(msg, sizeof(msg), format, 4); - - if (userid == 0) + SMCParser smc = new SMCParser(); + smc.OnKeyValue = ReadConfig_KeyValue; + char szBuf[PLATFORM_MAX_PATH]; + BuildPath( Path_SM, szBuf, sizeof szBuf, "configs/sourcebans/sourcebans.cfg" ); + if ( FileExists( szBuf ) ) { - PrintToServer("%s", msg); + SMCError err = smc.ParseFile( szBuf ); + if ( err != SMCError_Okay ) + { + LogError( smc.GetErrorString( err, szBuf, sizeof szBuf ) ? szBuf : "Unknown config parse error." ); + } } else { - PrintToConsole(client, "%s", msg); + LogError( "Can't find '%s'. Check it to exists and use `sb_reload` command (or change map) to reparse config.", szBuf ); } } -void PrintToBanAdmins(const char[] format, any ...) +stock SMCResult ReadConfig_KeyValue (SMCParser smc, const char[] szKey, const char[] szValue, bool key_quotes, bool value_quotes) { - char msg[256]; - - for (int i = 1; i <= MaxClients; i++) + if ( !strcmp( "DatabasePrefix", szKey, false ) ) { - if (IsClientInGame(i) && !IsFakeClient(i) && CheckCommandAccess(i, "sm_listsourcebans", ADMFLAG_GENERIC)) + if ( !szValue[0] ) { - SetGlobalTransTarget(i); - VFormat(msg, sizeof(msg), format, 2); - PrintToChat(i, "%s", msg); + g_DatabasePrefix = "sb"; + } + else + { + strcopy( g_DatabasePrefix, sizeof g_DatabasePrefix, szValue ); } } + return SMCParse_Continue; } -stock void ReadConfig() +stock void DB_Connect () { - InitializeConfigParser(); - - if (g_ConfigParser == null) + if ( !g_bConnecting ) { - return; + Database.Connect( DB_Connect_Callback, "sourcebans" ); + g_bConnecting = true; } +} - char ConfigFile[PLATFORM_MAX_PATH]; - BuildPath(Path_SM, ConfigFile, sizeof(ConfigFile), "configs/sourcebans/sourcebans.cfg"); - - if (FileExists(ConfigFile)) +stock void DB_Connect_Callback (Database db, char[] szError, any aData) +{ + if ( !db ) { - InternalReadConfig(ConfigFile); + DB_HandleError( szError ); } else { - char Error[PLATFORM_MAX_PATH + 64]; - FormatEx(Error, sizeof(Error), "FATAL *** ERROR *** can not find %s", ConfigFile); - SetFailState(Error); + g_DB = db; + if ( g_szLastError[0] ) + { + g_szLastError[0] = '\0'; + } } + g_bConnecting = false; } -static void InitializeConfigParser() +stock void DB_OnClientAuthorized_Callback (Database db, DBResultSet results, const char[] szError, int iUserid) { - if (g_ConfigParser == null) + if ( results ) { - g_ConfigParser = new SMCParser(); - g_ConfigParser.OnEnterSection = ReadConfig_NewSection; - g_ConfigParser.OnKeyValue = ReadConfig_KeyValue; - g_ConfigParser.OnLeaveSection = ReadConfig_EndSection; + int iClient = GetClientOfUserId(iUserid); + if ( iClient && results && results.FetchRow() ) + { + int iBanCount = results.FetchInt(0), iCommCount; + if ( results.FetchRow() ) + { + iCommCount = results.FetchInt(0); + } + if ( iBanCount && iCommCount ) + { + PrintToAdmins( "%s%t", PREFIX, "Ban and Comm Warning", iClient, iBanCount, ((iBanCount > 1 || iBanCount == 0) ? "s" : ""), iCommCount, ((iCommCount > 1 || iCommCount == 0) ? "s" : "") ); + } + else if ( iCommCount ) + { + PrintToAdmins( "%s%t", PREFIX, "Comm Warning", iClient, iCommCount, ((iCommCount > 1 || iCommCount == 0) ? "s" : "") ); + } + else if ( iBanCount ) + { + PrintToAdmins( "%s%t", PREFIX, "Ban Warning", iClient, iBanCount, ((iBanCount > 1 || iBanCount == 0) ? "s" : "") ); + } + } } -} - -static void InternalReadConfig(const char[] path) -{ - SMCError err = g_ConfigParser.ParseFile(path); - - if (err != SMCError_Okay) + else { - char buffer[64]; - PrintToServer("%s", g_ConfigParser.GetErrorString(err, buffer, sizeof(buffer)) ? buffer : "Fatal parse error"); + DB_HandleError( szError ); } } -public SMCResult ReadConfig_NewSection(SMCParser smc, const char[] name, bool opt_quotes) +stock void DB_list_Callback (Database db, DBResultSet results, const char[] szError, DataPack hPack) { - 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) + hPack.Reset(); + int iClient = hPack.ReadCell(); + ReplySource rsOld = SetCmdReplySource( SM_REPLY_TO_CONSOLE ); + if ( !iClient || (iClient = GetClientOfUserId( iClient )) ) { - strcopy(g_DatabasePrefix, sizeof(g_DatabasePrefix), value); - - if (g_DatabasePrefix[0] == '\0') + bool bBans = hPack.ReadCell(); + char szTargetName[MAX_NAME_LENGTH]; + hPack.ReadString( szTargetName, sizeof szTargetName ); + if ( results ) { - g_DatabasePrefix = "sb"; + if ( results.RowCount ) + { + char szStartDate[11], szBannedBy[11], szLength[11], szEndDate[11], szRemoveType[2], szCommType[2]; + int iLen, iLength, iReasonSize = bBans ? 28 : 23, i; + char[] szReason = new char[iReasonSize]; + ReplyToCommand( iClient, "%sListing %ss for %s", PREFIX, bBans ? "ban" : "comm", szTargetName ); + ReplyToCommand( iClient, "Ban Date Banned By Length End Date %sR Reason", bBans ? "" : "T " ); + ReplyToCommand( iClient, "-------------------------------------------------------------------------------" ); + while ( results.FetchRow() ) + { + szStartDate = " ", szBannedBy = " ", szLength = "N/A ", szEndDate = "N/A ", szRemoveType = " ", szCommType = " "; + if ( !results.IsFieldNull(0) ) + { + FormatTime( szStartDate, sizeof szStartDate, "%Y-%m-%d", results.FetchInt(0) ); + } + if ( !results.IsFieldNull(1) ) + { + results.FetchString( 1, szBannedBy, sizeof szBannedBy ); + iLen = results.FetchSize(1); + if ( iLen > sizeof szBannedBy - 1 ) + { + for ( i = 2; i < 5; ++i ) + { + szBannedBy[sizeof szBannedBy - i] = '.'; + } + } + else + { + for ( i = iLen; i < sizeof szBannedBy - 1; ++i ) + { + szBannedBy[i] = ' '; + } + } + } + iLength = results.FetchInt(3); /* NOT NULL */ + if ( !iLength ) + { + szLength = "Permanent "; + } + else if ( iLength == -1 ) + { + szLength = "Session "; + } + else + { + iLen = IntToString( iLength, szLength, sizeof szLength ); + if ( iLen < sizeof szLength - 1 ) + { + szLength[iLen] = ' '; + } + } /* change the '\0' to a ' '. the original \0 at the end will still be there */ + if ( !results.IsFieldNull(2) ) + { + FormatTime( szEndDate, sizeof szEndDate, "%Y-%m-%d", results.FetchInt(2) ); + } + results.FetchString( 4, szReason, iReasonSize ); /* NOT NULL */ + iLen = results.FetchSize(4); + if ( iLen > iReasonSize - 1 ) + { + for ( i = 2; i < 5; ++i ) + { + szReason[iReasonSize - i] = '.'; + } + } + else + { + for ( i = iLen; i < iReasonSize - 1; ++i ) + { + szReason[i] = ' '; + } + } + if ( !results.IsFieldNull(5) ) + { + results.FetchString( 5, szRemoveType, sizeof szRemoveType ); + } + if ( bBans ) + { + ReplyToCommand( iClient, "%s %s %s %s %s %s", szStartDate, szBannedBy, szLength, szEndDate, szRemoveType, szReason ); + } + else + { + results.FetchString( 6, szCommType, sizeof szCommType ); /* NOT NULL */ + if ( szCommType[0] == '1' ) + { + szCommType = "M"; + } + else if ( szCommType[0] == '2' ) + { + szCommType = "G"; + } + ReplyToCommand( iClient, "%s %s %s %s %s %s %s", szStartDate, szBannedBy, szLength, szEndDate, szCommType, szRemoveType, szReason ); + } + } + } + else + { + ReplyToCommand( iClient, "%sNo %ss found for %s.", PREFIX, bBans ? "ban" : "comm", szTargetName ); + } + } + else + { + ReplyToCommand( iClient, "%sDB error while retrieving %ss for %s:\n%s", PREFIX, bBans ? "ban" : "comm", szTargetName, szError ); } } - - return SMCParse_Continue; + delete hPack; + SetCmdReplySource( rsOld ); } -public SMCResult ReadConfig_EndSection(SMCParser smc) +stock void DB_HandleError (const char[] szError) { - return SMCParse_Continue; + if ( !StrEqual(g_szLastError, szError, false) ) + { + strcopy( g_szLastError, sizeof g_szLastError, szError ); + LogError( szError ); + } }