/*
* Active Ban Search (olds.ro)
* Copyright (C) 2026 daemon (olds.ro), written by daemon's plugin agent
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* 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: <https://www.gnu.org/licenses/>.
*
* amx_banfind <text|STEAM_ID> (ADMIN_BAN) searches the active bans of an AMXBans database
* (table <prefix>bans) by part of a name, or an exact SteamID, in a threaded SQLx query, so
* the server never waits for the database. Active means not expired and either permanent
* (ban_length 0) or still running (ban_created + ban_length minutes is in the future). Each
* hit shows the name, SteamID, ban date, time left, reason and admin, in the console and in
* a MOTD list. The text is escaped for LIKE (% and _) and for SQL (SQL_QuoteString).
*
* Cvars: olds_bsearch_host, olds_bsearch_user, olds_bsearch_pass, olds_bsearch_db,
* olds_bsearch_prefix "amx_", olds_bsearch_max 20 (1-50)
*/
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#pragma semicolon 0
#define PLUGIN "Active Ban Search"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define MIN_TEXT 2
#define MAX_TEXT 32
#define COOLDOWN 2.0
#define MOTD_LIMIT 1500
new g_pHost
new g_pUser
new g_pPass
new g_pDb
new g_pPrefix
new g_max
new bool:g_busy[MAX_PLAYERS + 1]
new Float:g_next[MAX_PLAYERS + 1]
new g_searchText[MAX_PLAYERS + 1][MAX_TEXT + 1]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
g_pHost = create_cvar("olds_bsearch_host", "127.0.0.1", FCVAR_PROTECTED, "Ban database host")
g_pUser = create_cvar("olds_bsearch_user", "amxbans", FCVAR_PROTECTED, "Ban database user")
g_pPass = create_cvar("olds_bsearch_pass", "", FCVAR_PROTECTED, "Ban database password")
g_pDb = create_cvar("olds_bsearch_db", "amxbans", FCVAR_PROTECTED, "Ban database name")
g_pPrefix = create_cvar("olds_bsearch_prefix", "amx_", FCVAR_NONE, "Table prefix (the table is <prefix>bans)")
bind_pcvar_num(create_cvar("olds_bsearch_max", "20", FCVAR_NONE,
"Most results per search", true, 1.0, true, 50.0), g_max)
register_concmd("amx_banfind", "cmd_banfind", ADMIN_BAN, "<name part|STEAM_ID> - search active bans")
register_dictionary("olds_bansearch.txt")
AutoExecConfig(true, "olds_bansearch")
}
public plugin_cfg()
{
if (!SQL_SetAffinity("mysql"))
log_amx("The sqlx mysql driver is not loaded: amx_banfind cannot reach the database")
}
public client_disconnected(id)
{
g_busy[id] = false
}
bool:steam_id(const text[])
{
if (!equali(text, "STEAM_", 6) && !equali(text, "VALVE_", 6))
return false
for (new i = 6; text[i]; i++)
{
if (!isdigit(text[i]) && text[i] != ':')
return false
}
return true
}
bool:good_prefix(const prefix[])
{
new len = strlen(prefix)
if (len > 16)
return false
for (new i = 0; i < len; i++)
{
if (!isalnum(prefix[i]) && prefix[i] != '_')
return false
}
return true
}
/* % and _ match anything in LIKE: put a backslash before them (and before backslashes). */
like_escape(const src[], dest[], len)
{
new n
for (new i = 0; src[i] && n < len - 1; i++)
{
if (src[i] == '%' || src[i] == '_' || src[i] == '\')
{
if (n >= len - 2)
break
dest[n++] = '\'
}
dest[n++] = src[i]
}
dest[n] = EOS
}
public cmd_banfind(id, level, cid)
{
if (!cmd_access(id, level, cid, 2))
return PLUGIN_HANDLED
if (g_busy[id])
{
console_print(id, "%L", id, "BSEARCH_BUSY")
return PLUGIN_HANDLED
}
new Float:now = get_gametime()
if (now < g_next[id])
{
console_print(id, "%L", id, "BSEARCH_WAIT")
return PLUGIN_HANDLED
}
new raw[96], text[MAX_TEXT + 1], n
read_args(raw, charsmax(raw))
remove_quotes(raw)
trim(raw)
for (new i = 0; raw[i] && n < MAX_TEXT; i++)
{
if (raw[i] >= 0 && raw[i] < 32)
continue
text[n++] = raw[i]
}
text[n] = EOS
trim(text)
if (strlen(text) < MIN_TEXT)
{
console_print(id, "%L", id, "BSEARCH_USAGE", MIN_TEXT, MAX_TEXT)
return PLUGIN_HANDLED
}
new prefix[24]
get_pcvar_string(g_pPrefix, prefix, charsmax(prefix))
if (!good_prefix(prefix))
{
console_print(id, "%L", id, "BSEARCH_PREFIX")
log_amx("olds_bsearch_prefix ^"%s^" may only hold letters, digits and _", prefix)
return PLUGIN_HANDLED
}
/* Escape for LIKE, then for SQL; the SteamID is compared exactly. */
new like[MAX_TEXT * 2 + 2], quotedLike[MAX_TEXT * 4 + 8], quotedId[MAX_TEXT * 2 + 8]
like_escape(text, like, charsmax(like))
SQL_QuoteString(Empty_Handle, quotedLike, charsmax(quotedLike), like)
SQL_QuoteString(Empty_Handle, quotedId, charsmax(quotedId), text)
new sql[768], len, limit = clamp(g_max, 1, 50)
len = formatex(sql, charsmax(sql), "SELECT player_nick, player_id, ban_created, ban_length, ban_reason, admin_nick FROM %sbans WHERE ", prefix)
if (steam_id(text))
len += formatex(sql[len], charsmax(sql) - len, "player_id = '%s'", quotedId)
else
len += formatex(sql[len], charsmax(sql) - len, "(player_nick LIKE '%%%s%%' OR player_id = '%s')", quotedLike, quotedId)
formatex(sql[len], charsmax(sql) - len, " AND expired = 0 AND (ban_length = 0 OR ban_created + ban_length * 60 > UNIX_TIMESTAMP()) ORDER BY ban_created DESC LIMIT %d", limit)
new host[64], user[32], pass[64], db[64]
get_pcvar_string(g_pHost, host, charsmax(host))
get_pcvar_string(g_pUser, user, charsmax(user))
get_pcvar_string(g_pPass, pass, charsmax(pass))
get_pcvar_string(g_pDb, db, charsmax(db))
new Handle:tuple = SQL_MakeDbTuple(host, user, pass, db, 5)
new data[2]
data[0] = id
data[1] = id ? get_user_userid(id) : 0
copy(g_searchText[id], charsmax(g_searchText[]), text)
g_busy[id] = true
g_next[id] = now + COOLDOWN
SQL_ThreadQuery(tuple, "sql_result", sql, data, sizeof(data))
SQL_FreeHandle(tuple)
new name[32]
if (id)
get_user_name(id, name, charsmax(name))
else
copy(name, charsmax(name), "server")
log_amx("%s searched the bans for ^"%s^"", name, text)
console_print(id, "%L", id, "BSEARCH_SEARCHING", text)
return PLUGIN_HANDLED
}
/* "3d 4h 10m", or the word for permanent. */
time_left(id, created, length, out[], len)
{
if (length <= 0)
{
formatex(out, len, "%L", id, "BSEARCH_PERMANENT")
return
}
new left = created + length * 60 - get_systime()
if (left < 60)
left = 60
new days = left / 86400, hours = (left % 86400) / 3600, mins = (left % 3600) / 60
if (days)
formatex(out, len, "%dd %dh %dm", days, hours, mins)
else if (hours)
formatex(out, len, "%dh %dm", hours, mins)
else
formatex(out, len, "%dm", mins)
}
html_escape(const src[], dest[], len)
{
new n
for (new i = 0; src[i] && n < len - 6; i++)
{
switch (src[i])
{
case '<': n += copy(dest[n], len - n, "<")
case '>': n += copy(dest[n], len - n, ">")
case '&': n += copy(dest[n], len - n, "&")
case '"': n += copy(dest[n], len - n, """)
default: dest[n++] = src[i]
}
}
dest[n] = EOS
}
public sql_result(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime)
{
new id = data[0]
if (id && (!is_user_connected(id) || get_user_userid(id) != data[1]))
return
g_busy[id] = false
if (failstate == TQUERY_CONNECT_FAILED)
{
console_print(id, "%L", id, "BSEARCH_CONNECT", errnum, error)
if (id)
client_print_color(id, print_team_red, "%L", id, "BSEARCH_DB_ERROR_CHAT")
log_amx("Ban search: could not connect to the database (%d): %s", errnum, error)
return
}
if (failstate == TQUERY_QUERY_FAILED)
{
console_print(id, "%L", id, "BSEARCH_QUERY", errnum, error)
if (id)
client_print_color(id, print_team_red, "%L", id, "BSEARCH_DB_ERROR_CHAT")
log_amx("Ban search: query failed (%d): %s", errnum, error)
return
}
new rows = SQL_NumResults(query)
if (!rows)
{
console_print(id, "%L", id, "BSEARCH_NONE", g_searchText[id])
if (id)
client_print_color(id, print_team_default, "%L", id, "BSEARCH_NONE_CHAT", g_searchText[id])
return
}
new nick[64], steam[40], reason[128], admin[64], date[24], left[24]
new motd[MOTD_LIMIT + 200], mlen, shown, safe[160], safe2[160], safe3[160]
mlen = formatex(motd, charsmax(motd), "<html><body style=^"background:#111;color:#ddd;font-family:Tahoma;font-size:12px^"><b>%L</b><table border=0 cellpadding=2>",
id, "BSEARCH_MOTD_HEAD", rows)
console_print(id, "%L", id, "BSEARCH_FOUND", rows, g_searchText[id])
new n = 0
while (SQL_MoreResults(query))
{
n++
SQL_ReadResult(query, 0, nick, charsmax(nick))
SQL_ReadResult(query, 1, steam, charsmax(steam))
new created = SQL_ReadResult(query, 2)
new length = SQL_ReadResult(query, 3)
SQL_ReadResult(query, 4, reason, charsmax(reason))
SQL_ReadResult(query, 5, admin, charsmax(admin))
format_time(date, charsmax(date), "%Y-%m-%d %H:%M", created)
time_left(id, created, length, left, charsmax(left))
console_print(id, "%d. %s <%s> | %s | %L %s | %s | %L %s", n, nick, steam, date, id, "BSEARCH_LEFT", left,
reason, id, "BSEARCH_BY", admin)
if (mlen < MOTD_LIMIT)
{
html_escape(nick, safe, charsmax(safe))
html_escape(reason, safe2, charsmax(safe2))
html_escape(admin, safe3, charsmax(safe3))
mlen += formatex(motd[mlen], charsmax(motd) - mlen,
"<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
n, safe, steam, date, left, safe2, safe3)
shown++
}
SQL_NextRow(query)
}
mlen += formatex(motd[mlen], charsmax(motd) - mlen, "</table>")
if (shown < n)
mlen += formatex(motd[mlen], charsmax(motd) - mlen, "<i>%L</i>", id, "BSEARCH_MOTD_MORE", n - shown)
formatex(motd[mlen], charsmax(motd) - mlen, "</body></html>")
if (id)
{
new title[48]
formatex(title, charsmax(title), "%L", id, "BSEARCH_MOTD_TITLE")
show_motd(id, motd, title)
}
}