Skip to content
View in the app

A better way to browse. Learn more.

Learn more

admin-action-log/addons/amxmodx/scripting/olds_adminlog.sma

olds_adminlog.sma
297 lines · 9 KB · Pawn (AMX Mod X)
Raw
/*
 * Admin Action Log (olds.ro workshop card LIB-1070)
 *
 * Moderation: every amx_ command is written to a daily log for the owners:
 * the time, who ran it (name, SteamID, IP and admin flags, or the server
 * console / rcon), the target player when the first argument names one, and
 * the command text.
 *   addons/amxmodx/logs/olds_adminlog_YYYYMMDD.log
 *
 * Players: FM_ClientCommand sees every client command starting with amx_.
 * Server console and rcon: at map start the plugin adds a register_srvcmd
 * echo to every amx_ console command the loaded plugins registered.
 *
 * Cvars
 *   olds_alog_hidepass 1   passwords become stars: every argument of a command
 *                          whose name holds "pass" (amx_pass ...), and every
 *                          word that follows a word holding "pass"
 *                          (rcon_password x, sv_password x, _pw x)
 *   olds_alog_days 30      at map start, logs older than this are deleted
 *                          (0 = keep all)
 *
 * When the log cannot be written (folder read-only, disk full) one error goes
 * to the server console; nothing else stops. Writing works again by itself.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

#define PLUGIN  "Admin Action Log"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

#pragma dynamic 32768

new const PREFIX[] = "olds_adminlog_"
const LINE_LEN = 512
const MAX_TOKENS = 32
const DAY = 86400

new g_hidePass, g_days
new g_dir[128]
new bool:g_failed

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    bind_pcvar_num(create_cvar("olds_alog_hidepass", "1", FCVAR_PROTECTED,
        "1 = write passwords in admin commands as stars.", true, 0.0, true, 1.0), g_hidePass)
    bind_pcvar_num(create_cvar("olds_alog_days", "30", FCVAR_NONE,
        "Delete admin logs older than this many days at map start (0 = keep).", true, 0.0, true, 3650.0), g_days)
    AutoExecConfig(true, "olds_adminlog")
    register_forward(FM_ClientCommand, "on_client_command")
    get_localinfo("amxx_logs", g_dir, charsmax(g_dir))
    if (!g_dir[0])
        copy(g_dir, charsmax(g_dir), "addons/amxmodx/logs")
}

public plugin_cfg()
{
    echo_server_commands()
    delete_old()
}

/* ---------- catching commands ---------- */

public on_client_command(id)
{
    new command[64]
    read_argv(0, command, charsmax(command))
    if (!equali(command, "amx_", 4))
        return FMRES_IGNORED
    new args[LINE_LEN]
    read_args(args, charsmax(args))
    write_entry(id, command, args)
    return FMRES_IGNORED
}

echo_server_commands()
{
    new command[64], info[4], flags, count, done[2048], echoed
    count = get_concmdsnum(-1, -1)
    for (new i = 0; i < count; i++)
    {
        get_concmd(i, command, charsmax(command), flags, info, charsmax(info), -1, -1)
        if (!equali(command, "amx_", 4))
            continue
        // A command can be registered by several plugins: echo it once.
        new key[66]
        formatex(key, charsmax(key), "|%s|", command)
        if (contain(done, key) != -1)
            continue
        if (strlen(done) + strlen(key) < charsmax(done))
            add(done, charsmax(done), key)
        register_srvcmd(command, "on_server_command")
        echoed++
    }
    log_amx("logging %d amx_ command(s) from the server console and rcon", echoed)
}

public on_server_command()
{
    new command[64], args[LINE_LEN]
    read_argv(0, command, charsmax(command))
    read_args(args, charsmax(args))
    write_entry(0, command, args)
    return PLUGIN_CONTINUE
}

/* ---------- the entry ---------- */

bool:has_pass(const word[])
{
    return containi(word, "pass") != -1 || equali(word, "_pw")
}

// Splits on spaces, keeping quoted parts whole; stars every password.
mask(const command[], const args[], out[], len)
{
    new tokens[MAX_TOKENS][128], count, i, n = strlen(args)
    while (i < n && count < MAX_TOKENS)
    {
        while (i < n && args[i] == ' ')
            i++
        if (i >= n)
            break
        new t
        if (args[i] == '"')
        {
            i++
            while (i < n && args[i] != '"' && t < charsmax(tokens[]))
                tokens[count][t++] = args[i++]
            i++
        }
        else
        {
            while (i < n && args[i] != ' ' && t < charsmax(tokens[]))
                tokens[count][t++] = args[i++]
        }
        tokens[count][t] = EOS
        count++
    }
    new bool:all = g_hidePass && has_pass(command)
    out[0] = EOS
    new bool:hideNext
    for (new k = 0; k < count; k++)
    {
        if (k)
            add(out, len, " ")
        // A quoted argument can hold a whole command line: mask inside it too.
        if (contain(tokens[k], " ") != -1)
        {
            new inner[128]
            if (all || hideNext)
                copy(inner, charsmax(inner), "****")
            else
                mask_words(tokens[k], inner, charsmax(inner))
            add(out, len, "^"")
            add(out, len, inner)
            add(out, len, "^"")
        }
        else if (all || hideNext)
            add(out, len, "****")
        else
            add(out, len, tokens[k])
        hideNext = g_hidePass && has_pass(tokens[k])
    }
}

mask_words(const text[], out[], len)
{
    new word[64], rest[128], left[128], bool:hideNext
    copy(rest, charsmax(rest), text)
    out[0] = EOS
    while (rest[0])
    {
        strtok2(rest, word, charsmax(word), left, charsmax(left), ' ', 1)
        copy(rest, charsmax(rest), left)
        if (!word[0])
            continue
        if (out[0])
            add(out, len, " ")
        if (hideNext)
            add(out, len, "****")
        else
            add(out, len, word)
        hideNext = g_hidePass && has_pass(word)
    }
}

// The player the first argument names (#userid, SteamID or name part), or 0.
target_of(const args[])
{
    new first[64]
    parse(args, first, charsmax(first))
    if (!first[0])
        return 0
    new target
    if (first[0] == '#' && is_str_num(first[1]))
        target = find_player_ex(FindPlayer_MatchUserId, str_to_num(first[1]))
    if (!target)
        target = find_player_ex(FindPlayer_MatchAuthId, first)
    if (!target)
        target = find_player_ex(FindPlayer_MatchName | FindPlayer_CaseInsensitive, first)
    if (!target)
        target = find_player_ex(FindPlayer_MatchNameSubstring | FindPlayer_CaseInsensitive | FindPlayer_LastMatched, first)
    return target
}

describe(id, out[], len)
{
    if (!id)
    {
        copy(out, len, "server console/rcon")
        return
    }
    new name[MAX_NAME_LENGTH], auth[34], ip[24], flags[27]
    get_user_name(id, name, charsmax(name))
    get_user_authid(id, auth, charsmax(auth))
    get_user_ip(id, ip, charsmax(ip), 1)
    get_flags(get_user_flags(id), flags, charsmax(flags))
    formatex(out, len, "^"%s^" <%s> %s flags ^"%s^"", name, auth, ip, flags)
}

write_entry(id, const command[], const args[])
{
    new who[128], what[LINE_LEN], target[128], line[LINE_LEN + 320]
    describe(id, who, charsmax(who))
    mask(command, args, what, charsmax(what))
    new victim = target_of(args)
    if (victim)
        describe(victim, target, charsmax(target))
    else
        copy(target, charsmax(target), "-")
    new stamp[12]
    get_time("%H:%M:%S", stamp, charsmax(stamp))
    formatex(line, charsmax(line), "%s | %s | target %s | %s %s^n", stamp, who, target, command, what)

    new path[192], day[12]
    get_time("%Y%m%d", day, charsmax(day))
    formatex(path, charsmax(path), "%s/%s%s.log", g_dir, PREFIX, day)
    new file = fopen(path, "at")
    if (!file)
    {
        if (!g_failed)
        {
            g_failed = true
            server_print("[Admin log] cannot write %s: admin commands are not logged until it can be written", path)
        }
        return
    }
    fputs(file, line)
    fclose(file)
    if (g_failed)
    {
        g_failed = false
        server_print("[Admin log] %s can be written again", path)
    }
}

/* ---------- old logs ---------- */

delete_old()
{
    if (g_days <= 0)
        return
    new file[64], FileType:type
    new dir = open_dir(g_dir, file, charsmax(file), type)
    if (!dir)
        return
    new now = get_systime(), deleted
    do
    {
        if (type != FileType_File || !equal(file, PREFIX, sizeof(PREFIX) - 1))
            continue
        // olds_adminlog_YYYYMMDD.log
        new date[9]
        copy(date, charsmax(date), file[sizeof(PREFIX) - 1])
        if (strlen(date) != 8 || !is_str_num(date) || !equal(file[sizeof(PREFIX) - 1 + 8], ".log"))
            continue
        new stamp = parse_time(date, "%Y%m%d")
        if (stamp <= 0 || (now - stamp) / DAY <= g_days)
            continue
        new path[192]
        formatex(path, charsmax(path), "%s/%s", g_dir, file)
        if (delete_file(path))
            deleted++
        else
            server_print("[Admin log] cannot delete %s", path)
    }
    while (next_file(dir, file, charsmax(file), type))
    close_dir(dir)
    if (deleted)
        log_amx("deleted %d admin log(s) older than %d day(s)", deleted, g_days)
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.