/**
* =============================================================================
* MySQL Based Ban Plugin
* Ban & unban players globally from a single command on multiple servers
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see .
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or .
*
**/
//////////////////////////////////////////////////////////////////
// MySQL SQL Table Creation
//////////////////////////////////////////////////////////////////
// Add table layout here
/*
DROP TABLE IF EXISTS `mysql_bans`;
CREATE TABLE IF NOT EXISTS `mysql_bans` (
`id` int(11) NOT NULL auto_increment,
`steam_id` varchar(32) NOT NULL,
`player_name` varchar(65) NOT NULL,
`ipaddr` varchar(24) NOT NULL,
`ban_length` int(1) NOT NULL default '0',
`ban_reason` varchar(100) NOT NULL,
`banned_by` varchar(100) NOT NULL,
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `steam_id` (`steam_id`)
UNIQUE KEY `ipaddr` (`ipaddr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
*/
//////////////////////////////////////////////////////////////////
// Includes
//////////////////////////////////////////////////////////////////
#include
#include
#undef REQUIRE_PLUGIN
#define PLUGIN_VERSION "2.0"
//////////////////////////////////////////////////////////////////
// Plugin Info
//////////////////////////////////////////////////////////////////
public Plugin:myinfo =
{
name = "MySQL Bans",
author = "Team MX | MoggieX",
description = "Ban using a MySQL DB over multiple servers",
version = PLUGIN_VERSION,
url = "http://www.Afterbuy.co.uk"
};
//////////////////////////////////////////////////////////////////
// Handles
//////////////////////////////////////////////////////////////////
new Handle:ErrorChecking;
new Handle:AdminChecking;
new Handle:StoreAllBans;
//////////////////////////////////////////////////////////////////
// Set vars on plugin start
//////////////////////////////////////////////////////////////////
public OnPluginStart()
{
// Load phrases
LoadTranslations("mysqlbans.phrases");
// Reg commands
RegAdminCmd("mysql_ban",Command_Ban,ADMFLAG_BAN,"Bans player by STEAMID")
RegAdminCmd("mysql_addban",Command_AddBan,ADMFLAG_BAN,"Bans player by STEAMID")
RegAdminCmd("mysql_unban",Command_UnBan,ADMFLAG_BAN,"Unbans player by STEAMID")
RegAdminCmd("mysql_ipban",Command_BanIp,ADMFLAG_BAN,"Unbans player by IP")
// Create convars
CreateConVar("mysql_bans_version", "0.1", "MySQL Bans Version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY)
AdminChecking = CreateConVar("mysql_bans_admin_check","1","Check Admins if banned? Speeds things up a touch 0= Do Not - 1 = Do Check", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY);
ErrorChecking = CreateConVar("mysql_bans_error_check","1","Shows Error Messages in-game chat, 0= don't show, 1 = show", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY);
StoreAllBans = CreateConVar("mysql_bans_store_all_bans","1","Store ALL bans in DB, 0=only store mysql bans, 1 = store ALL bans", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY);
VerifyTable();
}
//////////////////////////////////////////////////////////////////
// Player checking on connection (post admin check)
//////////////////////////////////////////////////////////////////
public OnClientPostAdminCheck(client)
{
// Check if a BOT if = then bailout
if(IsFakeClient(client))
return true;
// Check if a ADMIN if = then bailout - again another way of speeding this up as admins will not be banned
if (GetConVarInt(AdminChecking) == 1)
{
/* Error Checking */
if (GetConVarInt(ErrorChecking) == 1)
{
LogAction(client, -1, "[MySQL Bans] Player was admin checked");
PrintToServer("\x04[MySQL Bans]\x03 Player was admin checked");
PrintToChatAll("\x04[MySQL Bans]\x03 Player was admin checked");
}
/* do stuff */
new AdminId:AdminID = GetUserAdmin(client)
if(AdminID != INVALID_ADMIN_ID)
return true;
}
// OK, now thats over lets get thier data for processing!
/* Error Checking */
if (GetConVarInt(ErrorChecking) == 1)
{
LogAction(client, -1, "[MySQL Bans] Player is NOT a BOT or an ADMIN");
PrintToServer("\x04[MySQL Bans]\x03 Player is NOT a BOT or an ADMIN");
PrintToChatAll("\x04[MySQL Bans]\x03 Player is NOT a BOT or an ADMIN");
}
// Delcare & log some stuff
decl String:steam_id[32]; // Steam ID
decl String:player_name[65]; // Player name
decl String:ipaddr[24]; // IP Address
decl ban_length; // Checking thier ban length
decl String:ban_reason[100]; // Reason for ban
decl String:error[255]; // Error!
decl Float:ban_remaining; // Amount of time left in ban
// Get Client Auth & IP
GetClientName(client, player_name, sizeof(player_name));
GetClientIP(client, ipaddr, sizeof(ipaddr));
// Check if on LAN and if so bail out
// why is this like this? I have no idea!
steam_id[0] = '\0';
if (GetClientAuthString(client, steam_id, sizeof(steam_id)))
{
if (StrEqual(steam_id, "STEAM_ID_LAN"))
{
/* Error Checking */
if (GetConVarInt(ErrorChecking) == 1)
{
LogAction(client, -1, "[MySQL Bans] User Steam ID empty. You are on a LAN");
PrintToServer("\x04[MySQL Bans]\x03 User Steam ID empty. You are on a LAN");
PrintToChatAll("\x04[MySQL Bans]\x03 User Steam ID empty. You are on a LAN");
}
//return true;
}
}
// print data to server
/* Error Checking */
if (GetConVarInt(ErrorChecking) == 1)
{
LogAction(client, -1, "[MySQL Bans] Player In Server <%s>,<%s>,<%s>", player_name, ipaddr, steam_id);
PrintToServer("\x04[MySQL Bans]\x03 Player In Server <%s>,<%s>,<%s>", player_name, ipaddr, steam_id);
PrintToChatAll("\x04[MySQL Bans]\x03 Player In Server <%s>,<%s>,<%s>", player_name, ipaddr, steam_id);
}
new Handle:db = SQL_Connect("default", true, error, sizeof(error));
if (db == INVALID_HANDLE)
{
/* Error Checking */
if (GetConVarInt(ErrorChecking) == 1)
{
LogAction(client, -1, "[MySQL Bans] Could Not Connect to Database, error: %s", error);
PrintToServer("\x04[MySQL Bans]\x03 Could Not Connect to Database, error: %s", error);
PrintToChatAll("\x04[MySQL Bans]\x03 Could Not Connect to Database, error: %s", error);
}
CloseHandle(db);
return true;
}
// Form the SQL and add the parameters to it.
// Think this can be improved by only search for steam ID only, then searching again if a match found
decl String:query[255];
Format(query,
sizeof(query),
// OLD query
//"SELECT steam_id, ban_length, ban_reason FROM mysql_bans WHERE steam_id = '%s'", steam_id);
// New query - this one gets the IP and also now checks Steam ID AND IP
"SELECT ban_reason, ban_length, TIME_TO_SEC(TIMEDIFF(ADDTIME(timestamp,SEC_TO_TIME(ban_length*60)),CURRENT_TIMESTAMP))/60 FROM mysql_bans WHERE steam_id = '%s' OR ipaddr = '%s'", steam_id, ipaddr);
/* Error Checking */
if (GetConVarInt(ErrorChecking) == 1)
{
LogAction(client, -1, "[MySQL Bans] Query String: <%s>", query);
PrintToServer("\x04[MySQL Bans]\x03 Query String: <%s>", query);
PrintToChatAll("\x04[MySQL Bans]\x03 Query String: <%s>", query);
}
// make a new handle to stuff the query in
new Handle:hQuery;
// bail out if there is an issue
if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
{
/* Error Checking */
if (GetConVarInt(ErrorChecking) == 1)
{
LogAction(client, -1, "[MySQL Bans] Lookup failed for <%s>,<%s>,<%s>", player_name, ipaddr, steam_id);
PrintToServer("\x04[MySQL Bans]\x03 Lookup failed for <%s>,<%s>,<%s>", player_name, ipaddr, steam_id);
PrintToChatAll("\x04[MySQL Bans]\x03 Lookup failed for <%s>,<%s>,<%s>", player_name, ipaddr, steam_id);
}
CloseHandle(db);
return true;
}
// the lucky lad wasn't banned
if (!SQL_FetchRow(hQuery))
{
/* Error Checking */
if (GetConVarInt(ErrorChecking) == 1)
{
LogAction(client, -1, "[MySQL Bans] <%s>,<%s>,<%s> NOT found in bans database. Play nice now!>", player_name, ipaddr, steam_id);
PrintToServer("\x04[MySQL Bans]\x03 <%s>,<%s>,<%s> NOT found in bans database. Play nice now!>", player_name, ipaddr, steam_id);
PrintToChatAll("\x04[MySQL Bans]\x03 <%s>,<%s>,<%s> NOT found in bans database. Play nice now!>", player_name, ipaddr, steam_id);
}
}
// Must have been a match, so lets deal with the bann'ee
else
{
ban_length = SQL_FetchInt(hQuery,1);
ban_remaining = (ban_length > 0) ? SQL_FetchFloat(hQuery,2) : 0.0;
if (ban_remaining > 0)
{
SQL_FetchString(hQuery,0,ban_reason,sizeof(ban_reason));
LogAction(client, -1, "[MySQL Bans] Player is <%s>,<%s>,<%s> BANNED & has been removed", player_name, ipaddr, steam_id);
/* Error Checking */
if (GetConVarInt(ErrorChecking) == 1)
{
PrintCenterTextAll("[MySQL Bans] Player is <%s>,<%s>,<%s> BANNED & has been removed", player_name, ipaddr, steam_id);
PrintToServer("[MySQL Bans] Player is <%s>,<%s>,<%s> BANNED & has been removed", player_name, ipaddr, steam_id);
PrintToChatAll("[MySQL Bans] Player is <%s>,<%s>,<%s> BANNED & has been removed", player_name, ipaddr, steam_id);
}
// kick em with a reason and length of time
if (ban_length == 0)
KickClient(client,"You have been Banned for %s", ban_reason);
else
{
if (ban_remaining < 60.0)
KickClient(client,"You have been Banned for %s\nyou have %2.1f minutes left", ban_reason, ban_remaining);
else
{
ban_remaining /= 60.0; // convert to hours
if (ban_remaining < 24.0)
KickClient(client,"You have been Banned for %s\nyou have %2.1f hours left", ban_reason, ban_remaining);
else
{
ban_remaining /= 24.0;
if (ban_remaining < 7.0)
KickClient(client,"You have been Banned for %s\nyou have %2.1f days left", ban_reason, ban_remaining);
else
{
ban_remaining /= 7.0;
if (ban_remaining < 4.0)
KickClient(client,"You have been Banned for %s\nyou have %2.1f weeks left", ban_reason, ban_remaining);
else
{
ban_remaining /= 4.0;
KickClient(client,"You have been Banned for %s\nyou have %2.1f months left", ban_reason, ban_remaining);
}
}
}
}
}
}
}
CloseHandle(hQuery);
CloseHandle(db);
return true;
}
//////////////////////////////////////////////////////////////////
// ADD Ban Command - mysql_addban
//////////////////////////////////////////////////////////////////
public Action:Command_AddBan(client, args)
{
if (args < 2)
{
ReplyToCommand(client, "[MySQL Bans] Usage: mysql_addban