Skip to content
View in the app

A better way to browse. Learn more.

Learn more

access-control-library/addons/amxmodx/scripting/olds_acl.sma

olds_acl.sma
357 lines · 10.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Access Control Library (olds.ro) - one place for other plugins to ask
 * whether a player has a named admin right, what rank (level) they have, and
 * whether one admin may act on another (immunity).
 *
 * 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. You should have received a copy of the GNU General Public
 * License along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include <amxmodx>
#include <amxmisc>

#define PLUGIN  "Access Control Library"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_LEVELS 16
#define TASK_REREAD 6600

new Trie:g_rights               // right name (lower case) -> flag bits
new Trie:g_warned               // unknown right names already logged
new g_levelValue[MAX_LEVELS]
new g_levelFlags[MAX_LEVELS]
new g_levels

new g_flags[MAX_PLAYERS + 1]    // flags at the last re-read, to see changes
new g_forwardChanged
new g_cvarImmunity

public plugin_natives()
{
    register_library("olds_acl")
    register_native("acl_has_access", "native_has_access")
    register_native("acl_get_level", "native_get_level")
    register_native("acl_can_target", "native_can_target")
}

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    g_cvarImmunity = create_cvar("olds_acl_default_immunity", "a", FCVAR_NONE,
        "Flag letters that make a player immune to lower or equal ranked admins")
    AutoExecConfig(true, "olds_acl")

    register_concmd("amx_acl_reload", "cmd_reload", ADMIN_CFG, "- read configs/olds_acl.ini again")
    register_concmd("amx_acl_status", "cmd_status", ADMIN_CFG, "- rights, levels and players")

    g_rights = TrieCreate()
    g_warned = TrieCreate()
    g_forwardChanged = CreateMultiForward("acl_changed", ET_IGNORE, FP_CELL)
    load_config()
}

public plugin_end()
{
    TrieDestroy(g_rights)
    TrieDestroy(g_warned)
    DestroyForward(g_forwardChanged)
}

/* ---------- configuration ---------- */

bool:valid_flags(const text[])
{
    if (!text[0])
        return false
    for (new i = 0; text[i]; i++)
    {
        if (!('a' <= text[i] <= 'z'))
            return false
    }
    return true
}

bool:is_digits(const text[])
{
    if (!text[0])
        return false
    for (new i = 0; text[i]; i++)
    {
        if (!('0' <= text[i] <= '9'))
            return false
    }
    return true
}

add_defaults()
{
    new const names[][] = { "immunity", "reservation", "kick", "ban", "slay", "map", "cvar", "cfg",
        "chat", "vote", "password", "rcon" }
    new const letters[][] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l" }
    for (new i = 0; i < sizeof(names); i++)
        TrieSetCell(g_rights, names[i], read_flags(letters[i]))
}

load_config()
{
    TrieClear(g_rights)
    TrieClear(g_warned)
    g_levels = 0

    new path[192]
    get_configsdir(path, charsmax(path))
    add(path, charsmax(path), "/olds_acl.ini")
    new file = fopen(path, "rt")
    if (!file)
    {
        add_defaults()
        server_print("[ACL] %s not found: using the standard rights (kick = c, ban = d, ...).", path)
        return
    }

    new line[128], key[64], value[32], lineNo, rights
    while (!feof(file) && fgets(file, line, charsmax(line)))
    {
        lineNo++
        trim(line)
        if (!line[0] || line[0] == ';' || line[0] == '#' || line[0] == '[')
            continue
        new equals = contain(line, "=")
        if (equals <= 0)
        {
            log_amx("[ACL] %s line %d skipped: expected name = flags", path, lineNo)
            continue
        }
        copy(key, min(equals, charsmax(key)), line)
        copy(value, charsmax(value), line[equals + 1])
        trim(key)
        trim(value)
        strtolower(key)
        strtolower(value)
        if (!key[0] || !valid_flags(value))
        {
            log_amx("[ACL] %s line %d skipped: flags must be letters a-z", path, lineNo)
            continue
        }

        // "level 50 = d" defines a rank: players with all of those flags have at least that level.
        if (equal(key, "level", 5) && (key[5] == ' ' || key[5] == EOS))
        {
            new number[16]
            copy(number, charsmax(number), key[5])
            trim(number)
            if (!is_digits(number) || g_levels >= MAX_LEVELS)
            {
                log_amx("[ACL] %s line %d skipped: expected level <number> = flags (at most %d levels)", path, lineNo, MAX_LEVELS)
                continue
            }
            g_levelValue[g_levels] = str_to_num(number)
            g_levelFlags[g_levels] = read_flags(value)
            g_levels++
            continue
        }
        if (contain(key, " ") >= 0)
        {
            log_amx("[ACL] %s line %d skipped: a right name has no spaces", path, lineNo)
            continue
        }
        TrieSetCell(g_rights, key, read_flags(value))
        rights++
    }
    fclose(file)
    if (!rights)
        add_defaults()
    server_print("[ACL] %d right(s) and %d level(s) loaded from %s.", rights ? rights : TrieGetSize(g_rights), g_levels, path)
}

/* ---------- players ---------- */

public client_authorized(id)
{
    // admin.amxx may set the flags after this forward runs: read them a moment later.
    remove_task(TASK_REREAD + id)
    set_task(0.1, "task_reread", TASK_REREAD + id)
}

public client_putinserver(id)
{
    remove_task(TASK_REREAD + id)
    set_task(0.1, "task_reread", TASK_REREAD + id)
}

public client_disconnected(id)
{
    remove_task(TASK_REREAD + id)
    g_flags[id] = 0
}

public task_reread(task)
{
    reread(task - TASK_REREAD, false)
}

reread(id, bool:always)
{
    if (!is_user_connected(id))
        return
    new flags = get_user_flags(id)
    if (flags == g_flags[id] && !always)
        return
    g_flags[id] = flags
    ExecuteForward(g_forwardChanged, _, id)
}

level_of(id)
{
    new flags = get_user_flags(id)
    if (!g_levels)
    {
        // No levels in the file: one level per admin flag a-u held.
        new level
        for (new bit = 0; bit < 21; bit++)
        {
            if (flags & (1 << bit))
                level++
        }
        return level
    }
    new best
    for (new i = 0; i < g_levels; i++)
    {
        if ((flags & g_levelFlags[i]) == g_levelFlags[i] && g_levelValue[i] > best)
            best = g_levelValue[i]
    }
    return best
}

immunity_bits()
{
    new letters[27]
    get_pcvar_string(g_cvarImmunity, letters, charsmax(letters))
    strtolower(letters)
    return valid_flags(letters) ? read_flags(letters) : 0
}

bool:can_target(admin, target)
{
    if (admin == target || admin == 0)
        return true                          // yourself, or the server console
    new immune = immunity_bits()
    if (!immune || !(get_user_flags(target) & immune))
        return true
    return level_of(admin) > level_of(target)
}

bool:valid_player(id)
{
    if (1 <= id <= MaxClients && is_user_connected(id))
        return true
    log_error(AMX_ERR_NATIVE, "[ACL] Player %d is not connected", id)
    return false
}

/* ---------- natives ---------- */

// acl_has_access(id, const right[]): true when the player holds any flag mapped to the right.
public native_has_access(plugin, params)
{
    new id = get_param(1), right[64], bits
    get_string(2, right, charsmax(right))
    strtolower(right)
    if (id == 0)
        return 1                             // the server console has every right
    if (!valid_player(id))
        return 0
    if (!TrieGetCell(g_rights, right, bits))
    {
        if (!TrieKeyExists(g_warned, right))
        {
            TrieSetCell(g_warned, right, 1)
            new caller[64]
            get_plugin(plugin, caller, charsmax(caller))
            log_amx("[ACL] Unknown right ^"%s^" asked by %s: answered no. Add it to configs/olds_acl.ini.", right, caller)
        }
        return 0
    }
    return (get_user_flags(id) & bits) ? 1 : 0
}

// acl_get_level(id): the player's rank, 0 for players without admin flags.
public native_get_level(plugin, params)
{
    new id = get_param(1)
    if (!valid_player(id))
        return 0
    return level_of(id)
}

// acl_can_target(admin, target): false when target is immune and not outranked by admin.
public native_can_target(plugin, params)
{
    new admin = get_param(1), target = get_param(2)
    if (admin != 0 && !valid_player(admin))
        return 0
    if (!valid_player(target))
        return 0
    return can_target(admin, target) ? 1 : 0
}

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

public cmd_reload(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    load_config()
    for (new player = 1; player <= MaxClients; player++)
        reread(player, true)                 // the mapping changed: let plugins check again
    console_print(id, "[ACL] Reloaded: %d right(s), %d level(s).", TrieGetSize(g_rights), g_levels)
    return PLUGIN_HANDLED
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new immunity[27]
    get_pcvar_string(g_cvarImmunity, immunity, charsmax(immunity))
    console_print(id, "[ACL] %d right(s), %d level(s), immunity flags ^"%s^"", TrieGetSize(g_rights), g_levels, immunity)

    new TrieIter:iter = TrieIterCreate(g_rights), key[64], bits, letters[27]
    while (!TrieIterEnded(iter))
    {
        TrieIterGetKey(iter, key, charsmax(key))
        TrieIterGetCell(iter, bits)
        get_flags(bits, letters, charsmax(letters))
        console_print(id, "  %s = %s", key, letters)
        TrieIterNext(iter)
    }
    TrieIterDestroy(iter)
    for (new i = 0; i < g_levels; i++)
    {
        get_flags(g_levelFlags[i], letters, charsmax(letters))
        console_print(id, "  level %d = %s", g_levelValue[i], letters)
    }
    new name[32]
    for (new player = 1; player <= MaxClients; player++)
    {
        if (!is_user_connected(player))
            continue
        get_user_name(player, name, charsmax(name))
        get_flags(get_user_flags(player), letters, charsmax(letters))
        console_print(id, "  %s: flags %s, level %d", name, letters, level_of(player))
    }
    return PLUGIN_HANDLED
}

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.