Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Begin to break into modules #625

Open
wants to merge 18 commits into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions game/addons/sourcemod/scripting/sbpp/core.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if defined _sbpp_core_included
#endinput
#endif
#define _sbpp_core_included

#define SBPP_VERSION "1.7.0:1049"

#define SBPP_LogMsg( LogToFile( szLog,
#define SBPP_LogIssue( LogToFile( szIssues,
rumblefrog marked this conversation as resolved.
Show resolved Hide resolved

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" );
TheByKotik marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would change the name to sbpp.error.log

Suggested change
BuildPath( Path_SM, szLog, sizeof szLog, "logs/sbpp.log" );
BuildPath( Path_SM, szIssues, sizeof szIssues, "logs/sbpp.issues.log" );
BuildPath( Path_SM, szLog, sizeof szLog, "logs/sbpp.log" );
BuildPath( Path_SM, szIssues, sizeof szIssues, "logs/sbpp.error.log" );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the errors are logged by sourcemod itself, most often there will be exactly problems there like can't connect to or can't find file.
My english is not perfect, but they should be called as issues in this case, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error is the more universally adapted name, especially when it comes to logs and log levels. My intention behind that change is to make the naming more inline with SM and the other software stack that is needed to run SBPP (e.g nginx / Apache, PHP, etc) since they all use error logs and not issue logs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed. (Absolute forgot about string)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed. Sorry for that.

}
182 changes: 182 additions & 0 deletions game/addons/sourcemod/scripting/sbpp/sql.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#if defined _sbpp_sql_included
#endinput
#endif
#define _sbpp_sql_included

#include <sbpp/core.sp>

#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,
SBPP_SQL_State_Connecting = 1 << 0,
SBPP_SQL_State_Wait = 1 << 1,
SBPP_SQL_State_Connected = 1 << 2, }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation seems to be all over the place with this enum


forward Action _SBPP_SQL_Find (Database &db, int &iState);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, indentation.

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the case here for defining symbol usage as stock? Also, this seems like a UB.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand your question correctly, stock is not include if never used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if they include that file, will it ever be not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone forgets to call SBPP_SQL_Init().


stock void SBPP_SQL_Init (const ConnectCallback fnCallback)
{
LoadTranslations( "sbpp.sql.phrases.txt" );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unnecessary to add a separate translation file for one phrase, this would do better added into sbpp_main.phrases.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unnecessary to add a separate translation file for one phrase, this would do better added into sbpp_main.phrases.txt

I planned that this module will be included not only in sbpp_main. Therefore, it would be nonsensical to add a dependency on sbpp_main translations, it is desirable that each plugin can work absolutely independently.

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, "sourcebans" );
}

stock static void SBPP_SQL_Connect_Callback (const Database db, const char[] szError, const any aData)
{
bool bSuccessful;
if ( db )
{
g_dbSQL = db;
if ( !g_dbSQL.SetCharset( "utf8mb4" ) )
{
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[0] = '\0';
}
}
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<Database>( CloneHandle( db ) );
s_iState = SBPP_SQL_State_Connected;
CallCallback( true );
}
}

stock void DoNothing (const bool bSuccessful) {}
43 changes: 19 additions & 24 deletions game/addons/sourcemod/scripting/sbpp_checker.sp
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,40 @@
//
// *************************************************************************

#pragma semicolon 1
#pragma newdecls required
#pragma semicolon 1

#include <sourcemod>
#include <sbpp/core.sp>
#include <sbpp/sql.sp>

#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()
Expand All @@ -72,27 +71,22 @@ 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 )
{
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_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)
Expand Down Expand Up @@ -124,8 +118,9 @@ public Action OnListSourceBansCmd(int client, int args)
ReplyToCommand(client, LISTBANS_USAGE);
}

if (g_DB == INVALID_HANDLE)
if ( !g_dbSQL )
{
SBPP_SQL_Reconnect();
ReplyToCommand(client, "Error: Database not ready.");
return Plugin_Handled;
}
Expand All @@ -150,7 +145,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));
Expand All @@ -159,7 +154,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)
{
Expand Down Expand Up @@ -290,7 +285,7 @@ public Action OnListSourceCommsCmd(int client, int args)
ReplyToCommand(client, LISTCOMMS_USAGE);
}

if (g_DB == INVALID_HANDLE)
if ( !g_dbSQL )
{
ReplyToCommand(client, "Error: Database not ready.");
return Plugin_Handled;
Expand All @@ -315,7 +310,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));
Expand All @@ -324,7 +319,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)
{
Expand Down