/*
* Custom Chat Commands (olds.ro workshop card LIB-1517)
*
* Extra chat commands from a file, without writing a plugin: a word typed
* in chat prints a text to the player, runs a command for the player, or
* runs a server command.
*
* File configs/olds_saycustom.ini, one entry per line:
* word|action|text
* word what the player types in chat (any case), for example /rules
* action print the text is shown in chat to that player only
* cmd the text is run as a command of that player, as if
* typed in the console (for example: say /rank)
* server the text is run as a server command, never as the
* player (for example: amx_say Welcome)
* Empty lines and lines starting with ; or // are skipped; a line that
* cannot be read is skipped and logged. At most 100 entries: more are
* ignored and the log says so once.
*
* Works in say and say_team. With olds_saycustom_hide 1 the typed word is
* not shown in chat.
*
* Admin command (ADMIN_CFG, flag "h"; also from the server console):
* amx_saycustom_reload reads the file again, without a map change
*
* Cvars (saved with their descriptions in configs/plugins/olds_saycustom.cfg
* the first time the plugin starts):
* olds_saycustom_hide 1 1 = the typed word is not shown in chat
* olds_saycustom_log 0 1 = write every use to the AMX Mod X log
*
* Requires AMX Mod X 1.10 or newer. No extra modules and no ReAPI.
*
* Copyright (C) 2026 alexit (olds.ro workshop)
* SPDX-License-Identifier: GPL-3.0-or-later
*
* 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/>.
*/
#include <amxmodx>
#include <amxmisc>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Custom Chat Commands";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_saycustom.txt";
new const ENTRIES_FILE[] = "olds_saycustom.ini";
const MAX_ENTRIES = 100;
const MAX_WORD = 32;
const MAX_TEXT = 192;
enum
{
ACTION_PRINT = 0,
ACTION_CMD,
ACTION_SERVER
};
new const ACTION_NAMES[][] = { "print", "cmd", "server" };
new g_hide; // olds_saycustom_hide
new g_log; // olds_saycustom_log
new g_count;
new g_word[MAX_ENTRIES][MAX_WORD];
new g_action[MAX_ENTRIES];
new g_text[MAX_ENTRIES][MAX_TEXT];
new g_skipped;
// A cmd entry runs "say ..." for the player: no entry may start itself.
new bool:g_running[MAX_PLAYERS + 1];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
register_clcmd("say", "hook_say");
register_clcmd("say_team", "hook_say");
register_concmd("amx_saycustom_reload", "cmd_reload", ADMIN_CFG, "OLDS_SC_HELP", -1, true);
bind_pcvar_num(create_cvar("olds_saycustom_hide", "1", FCVAR_NONE,
"1 = the typed chat command is not shown in chat, 0 = it is shown",
true, 0.0, true, 1.0), g_hide);
bind_pcvar_num(create_cvar("olds_saycustom_log", "0", FCVAR_NONE,
"1 = write every use of a chat command to the AMX Mod X log",
true, 0.0, true, 1.0), g_log);
AutoExecConfig(true, "olds_saycustom");
}
public plugin_cfg()
{
load_entries();
}
public hook_say(id)
{
if (g_running[id] || !g_count)
return PLUGIN_CONTINUE;
new message[MAX_WORD + 8];
read_args(message, charsmax(message));
remove_quotes(message);
trim(message);
if (!message[0])
return PLUGIN_CONTINUE;
for (new i = 0; i < g_count; i++)
{
if (!equali(message, g_word[i]))
continue;
run_entry(id, i);
return g_hide ? PLUGIN_HANDLED : PLUGIN_CONTINUE;
}
return PLUGIN_CONTINUE;
}
run_entry(id, entry)
{
if (g_log)
{
new name[MAX_NAME_LENGTH];
get_user_name(id, name, charsmax(name));
log_amx("%s used %s (%s): %s", name, g_word[entry], ACTION_NAMES[g_action[entry]], g_text[entry]);
}
switch (g_action[entry])
{
case ACTION_PRINT:
{
client_print_color(id, print_team_default, "^1%s", g_text[entry]);
}
case ACTION_CMD:
{
g_running[id] = true;
run_player_command(id, g_text[entry]);
g_running[id] = false;
}
case ACTION_SERVER:
{
server_cmd("%s", g_text[entry]);
server_exec();
}
}
}
/**
* Runs text as a command of the player, through the server so plugins and
* the game both see it. say and say_team get the rest of the line as their
* text; other commands get up to two arguments.
*/
run_player_command(id, const text[])
{
new command[32], rest[MAX_TEXT];
strtok2(text, command, charsmax(command), rest, charsmax(rest), ' ', 1);
if (equali(command, "say") || equali(command, "say_team"))
{
amxclient_cmd(id, command, rest);
return;
}
new first[64], second[MAX_TEXT];
strtok2(rest, first, charsmax(first), second, charsmax(second), ' ', 1);
if (second[0])
amxclient_cmd(id, command, first, second);
else if (first[0])
amxclient_cmd(id, command, first);
else
amxclient_cmd(id, command);
}
/**
* amx_saycustom_reload
*/
public cmd_reload(id, level, cid)
{
if (!admin_allowed(id, level))
return PLUGIN_HANDLED;
load_entries();
console_print(id, "%L", id, "OLDS_SC_LOADED", g_count, ENTRIES_FILE, g_skipped);
if (id)
{
new name[MAX_NAME_LENGTH];
get_user_name(id, name, charsmax(name));
log_amx("Cmd: %s reloaded configs/%s: %d entries.", name, ENTRIES_FILE, g_count);
}
return PLUGIN_HANDLED;
}
load_entries()
{
g_count = 0;
g_skipped = 0;
new path[PLATFORM_MAX_PATH];
get_configsdir(path, charsmax(path));
format(path, charsmax(path), "%s/%s", path, ENTRIES_FILE);
new file = fopen(path, "rt");
if (!file)
{
log_amx("configs/%s not found: no chat commands.", ENTRIES_FILE);
return;
}
new line[256], lineNumber, bool:limitLogged;
while (fgets(file, line, charsmax(line)))
{
lineNumber++;
trim(line);
if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
continue;
if (g_count >= MAX_ENTRIES)
{
g_skipped++;
if (!limitLogged)
{
limitLogged = true;
log_amx("configs/%s has more than %d entries: line %d and the ones after it are ignored.",
ENTRIES_FILE, MAX_ENTRIES, lineNumber);
}
continue;
}
if (!read_entry(line, lineNumber))
g_skipped++;
}
fclose(file);
}
bool:read_entry(const line[], lineNumber)
{
new word[64], action[16], text[MAX_TEXT], rest[256];
strtok2(line, word, charsmax(word), rest, charsmax(rest), '|', 1);
strtok2(rest, action, charsmax(action), text, charsmax(text), '|', 1);
if (!word[0] || !action[0] || !text[0])
{
log_amx("configs/%s line %d skipped (write it as word|action|text): %s", ENTRIES_FILE, lineNumber, line);
return false;
}
if (strlen(word) >= MAX_WORD)
{
log_amx("configs/%s line %d skipped (the word is longer than %d characters): %s",
ENTRIES_FILE, lineNumber, MAX_WORD - 1, line);
return false;
}
new type = -1;
for (new i = 0; i < sizeof(ACTION_NAMES); i++)
{
if (equali(action, ACTION_NAMES[i]))
{
type = i;
break;
}
}
if (type == -1)
{
log_amx("configs/%s line %d skipped (the action must be print, cmd or server): %s",
ENTRIES_FILE, lineNumber, line);
return false;
}
copy(g_word[g_count], MAX_WORD - 1, word);
g_action[g_count] = type;
copy(g_text[g_count], MAX_TEXT - 1, text);
g_count++;
return true;
}
/**
* The same rules as cmd_access: the server console (and the host of a listen
* server) may always; ADMIN_ALL = everyone; ADMIN_ADMIN = any admin flag.
* The level can be changed in configs/cmdaccess.ini.
*/
bool:admin_allowed(id, level)
{
if (id == (is_dedicated_server() ? 0 : 1) || level == ADMIN_ALL)
return true;
if ((level == ADMIN_ADMIN && is_user_admin(id)) || (get_user_flags(id) & level))
return true;
console_print(id, "%L", id, "OLDS_SC_NO_ACCESS");
return false;
}