Add globalbans

This commit is contained in:
Jackzie 2021-04-20 21:34:32 -05:00
parent ac0d8846f1
commit f37301cae8
No known key found for this signature in database
GPG key ID: 1E834FE36520537A
3 changed files with 164 additions and 0 deletions

View file

@ -25,6 +25,7 @@ Useful things:
* [l4d2_extraplayeritems](#l4d2_extraplayeritems)
* [l4d2_population_control](#l4d2_population_control)
* [l4d2_extrafinaletanks](#l4d2_extrafinaletanks)
* [globalbans](#globalbans)
### Modified Others
* [200IQBots_FlyYouFools](#200IQBots_FlyYouFools)
@ -239,3 +240,9 @@ This plugin will automatically spawn an extra amount of tanks (determined by `l4
* `l4d2_eft_count <#>` - Default is 1, determines how many tanks that are allowed to spawn in the extra tank stage
* `l4d2_eft_chance <0.0-1.0> - Default is 0.0, determines the chance of each tank spawning in extra tank stage.
* If the spawn fails, it will still count as a spawn, the percentage is PER tank
### globalbans
This plugin will store bans in a database and read from it on connect. This allows you to easily have bans global between servers.
It will automatically intercept any ban that calls OnBanIdentity or OnBanClient (so sm_ban will work normally)
* **Convars:**
* `sm_hKickOnDBFailure <0/1>` - Should the plugin kick players if it cannot connect to the database?

BIN
plugins/globalbans.smx Normal file

Binary file not shown.

157
scripting/globalbans.sp Normal file
View file

@ -0,0 +1,157 @@
#pragma semicolon 1
#pragma newdecls required
#define DEBUG 0
#define PLUGIN_VERSION "1.0"
#define DB_NAME "globalbans"
#include <sourcemod>
#include <sdktools>
#include <geoip>
public Plugin myinfo =
{
name = "Global Bans",
author = "jackzmc",
description = "Manages bans via MySQL",
version = PLUGIN_VERSION,
url = ""
};
static Database g_db;
static ConVar hKickOnDBFailure;
public void OnPluginStart() {
if(!SQL_CheckConfig(DB_NAME)) {
SetFailState("No database entry for " ... DB_NAME ... "; no database to connect to.");
}
if(!ConnectDB()) {
SetFailState("Failed to connect to database.");
}
hKickOnDBFailure = CreateConVar("sm_hKickOnDBFailure", "0", "Should the plugin kick players if it cannot connect to the database?", FCVAR_NONE, true, 0.0, true, 1.0);
}
///////////////////////////////////////////////////////////////////////////////
// DB Connections
///////////////////////////////////////////////////////////////////////////////
bool ConnectDB() {
char error[255];
g_db = SQL_Connect(DB_NAME, true, error, sizeof(error));
if (g_db == null) {
LogError("Database error %s", error);
delete g_db;
return false;
} else {
PrintToServer("Connected to sm database " ... DB_NAME);
SQL_LockDatabase(g_db);
SQL_FastQuery(g_db, "SET NAMES \"UTF8mb4\"");
SQL_UnlockDatabase(g_db);
g_db.SetCharset("utf8mb4");
return true;
}
}
///////////////////////////////////////////////////////////////////////////////
// EVENTS
///////////////////////////////////////////////////////////////////////////////
public void OnClientAuthorized(int client, const char[] auth) {
if(!StrEqual(auth, "BOT", true)) {
char query[128];
Format(query, sizeof(query), "SELECT steamid, reason, timestamp, time FROM bans WHERE `steamid` = '%s'", auth);
g_db.Query(DB_OnConnectCheck, query, GetClientUserId(client), DBPrio_High);
}
}
public Action OnBanIdentity(const char[] identity, int time, int flags, const char[] reason, const char[] command, any source) {
if(flags == BANFLAG_AUTHID) {
char executor[32];
if(source > 0 && source <= MaxClients) {
GetClientAuthId(source, AuthId_Steam2, executor, sizeof(executor));
}else{
executor = "CONSOLE";
}
DBBan(identity, reason, time, executor);
}
}
public Action OnBanClient(int client, int time, int flags, const char[] reason, const char[] kick_message, const char[] command, any source) {
char executor[32], identity[32];
if(source > 0 && source <= MaxClients) {
GetClientAuthId(source, AuthId_Steam2, executor, sizeof(executor));
}else{
executor = "CONSOLE";
}
GetClientAuthId(client, AuthId_Steam2, identity, sizeof(identity));
DBBan(identity, reason, time, executor);
}
public Action OnRemoveBan(const char[] identity, int flags, const char[] command, any source) {
if(flags == BANFLAG_AUTHID) {
char query[128];
Format(query, sizeof(query), "DELETE FROM `bans` WHERE steamid = '%s'", identity);
g_db.Query(DB_OnRemoveBanQuery, query, flags);
}
}
///////////////////////////////////////////////////////////////////////////////
// DB Callbacks
///////////////////////////////////////////////////////////////////////////////
public void DB_OnConnectCheck(Database db, DBResultSet results, const char[] error, int user) {
int client = GetClientOfUserId(user);
if(db == INVALID_HANDLE || results == null) {
LogError("DB_OnConnectCheck returned error: %s", error);
if(client > 0 && hKickOnDBFailure.BoolValue) {
KickClient(client, "Could not authenticate at this time.");
LogMessage("Could not connect to database to authorize user, %d", user);
}
}else{
//No failure, check the data.
if(results.RowCount > 0 && client) {
results.FetchRow();
char reason[128];
DBResult result;
results.FetchString(1, reason, sizeof(reason), result);
if(result == DBVal_Data)
KickClient(client, "You have been banned: %s", reason);
else
KickClient(client, "You have been banned from this server.");
}
}
}
public void DB_OnBanQuery(Database db, DBResultSet results, const char[] error, any data) {
if(db == INVALID_HANDLE || results == null) {
LogError("DB_OnBanQuery returned error: %s", error);
}
}
public void DB_OnRemoveBanQuery(Database db, DBResultSet results, const char[] error, any data) {
if(db == INVALID_HANDLE || results == null) {
LogError("DB_OnRemoveBanQuery returned error: %s", error);
}
}
////////////////////////////////////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////////////////////////////////////
void DBBan(const char[] identity, const char[] reason, int time, const char[] executor) {
char query[255];
Format(query, sizeof(query), "INSERT INTO bans"
..."(steamid, reason, time, executor, ip_banned)"
..."VALUES ('%s', '%s', %d, '%s', 0)",
identity,
reason,
time,
executor
);
g_db.Query(DB_OnBanQuery, query);
}