Skip to content
View in the app

A better way to browse. Learn more.

Learn more

contact-info-command/addons/amxmodx/scripting/olds_contact.sma

olds_contact.sma
291 lines · 7.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Contact Info Command (olds.ro workshop card LIB-1506)
 *
 * A player types /contact or /info in chat and sees the server's website,
 * Discord and admin contact, either as coloured chat lines that only that
 * player sees or in a MOTD window.
 *
 * Chat commands (chat and team chat):
 *   /contact   /info
 *
 * Cvars (saved with their descriptions in configs/plugins/olds_contact.cfg
 * the first time the plugin starts):
 *   olds_contact_web      "https://olds.ro"  Website line; empty = no line.
 *   olds_contact_discord  ""                 Discord line; empty = no line.
 *   olds_contact_admin    ""                 Admin contact line; empty = no line.
 *   olds_contact_mode     1    1 = chat lines, 2 = MOTD window.
 *   olds_contact_cd       5    Seconds a player waits between two uses,
 *                              0 = no wait.
 * The list is built again whenever one of the three contact cvars changes,
 * so a new value is shown at once.
 *
 * Requires AMX Mod X 1.10 or newer. No extra modules; ReAPI is not used.
 *
 * 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>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Contact Info Command";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_contact.txt";

// Chat commands, registered for both say and say_team.
new const CHAT_COMMANDS[][] = { "/contact", "/info" };

// Longest contact value kept; a longer cvar value is cut.
const VALUE_LENGTH = 96;

// The contact entries, in the order they are shown.
enum _:ContactEntry
{
	ENTRY_WEB,
	ENTRY_DISCORD,
	ENTRY_ADMIN
};

new const ENTRY_CVARS[ContactEntry][] =
{
	"olds_contact_web",
	"olds_contact_discord",
	"olds_contact_admin"
};

new const ENTRY_DEFAULTS[ContactEntry][] =
{
	"https://olds.ro",
	"",
	""
};

new const ENTRY_DESCRIPTIONS[ContactEntry][] =
{
	"Website shown by /contact (empty = no website line)",
	"Discord invite shown by /contact (empty = no Discord line)",
	"Admin contact shown by /contact (empty = no admin line)"
};

// Dictionary keys of the labels in front of each value.
new const ENTRY_LABELS[ContactEntry][] =
{
	"OLDS_CONTACT_WEB",
	"OLDS_CONTACT_DISCORD",
	"OLDS_CONTACT_ADMIN"
};

enum
{
	MODE_CHAT = 1,
	MODE_MOTD
};

new g_mode;        // olds_contact_mode
new g_cooldown;    // olds_contact_cd

new g_pcvarEntry[ContactEntry];

// The pre-built list: only the entries that have a value, in display order.
new g_listEntry[ContactEntry];
new g_listValue[ContactEntry][VALUE_LENGTH + 1];
new g_listCount;

// Value of g_lastUse[] for a player who has not used the command yet.
const Float:NEVER = -1.0;

// Game time of each player's last use. The wait is worked out from it with
// the current olds_contact_cd, so a lower cooldown takes effect at once.
new Float:g_lastUse[MAX_PLAYERS + 1] = { NEVER, ... };

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	register_dictionary(DICTIONARY);

	new command[32];

	for (new i = 0; i < sizeof CHAT_COMMANDS; i++)
	{
		formatex(command, charsmax(command), "say %s", CHAT_COMMANDS[i]);
		register_clcmd(command, "cmd_contact");
		formatex(command, charsmax(command), "say_team %s", CHAT_COMMANDS[i]);
		register_clcmd(command, "cmd_contact");
	}

	for (new entry = 0; entry < ContactEntry; entry++)
	{
		g_pcvarEntry[entry] = create_cvar(ENTRY_CVARS[entry], ENTRY_DEFAULTS[entry], FCVAR_NONE,
			ENTRY_DESCRIPTIONS[entry]);
		hook_cvar_change(g_pcvarEntry[entry], "on_entry_changed");
	}

	bind_pcvar_num(create_cvar("olds_contact_mode", "1", FCVAR_NONE,
		"1 = contact details as chat lines, 2 = in a MOTD window",
		true, float(MODE_CHAT), true, float(MODE_MOTD)), g_mode);

	bind_pcvar_num(create_cvar("olds_contact_cd", "5", FCVAR_NONE,
		"Seconds a player waits between two uses of /contact (0 = no wait)",
		true, 0.0, true, 3600.0), g_cooldown);

	AutoExecConfig(true, "olds_contact");

	build_list();
}

public OnConfigsExecuted()
{
	build_list();
}

public on_entry_changed(pcvar, const oldValue[], const newValue[])
{
	build_list();
}

public client_putinserver(id)
{
	g_lastUse[id] = NEVER;
}

/**
 * Builds the list of entries that have a value. Control characters are
 * removed and spaces trimmed, so a value of only spaces counts as empty.
 */
build_list()
{
	g_listCount = 0;

	for (new entry = 0; entry < ContactEntry; entry++)
	{
		new value[VALUE_LENGTH + 1];
		get_pcvar_string(g_pcvarEntry[entry], value, charsmax(value));

		remove_control_characters(value);
		trim(value);

		if (!value[0])
			continue;

		g_listEntry[g_listCount] = entry;
		copy(g_listValue[g_listCount], VALUE_LENGTH, value);
		g_listCount++;
	}
}

public cmd_contact(id)
{
	new Float:now = get_gametime();

	if (g_cooldown > 0 && g_lastUse[id] != NEVER && now < g_lastUse[id] + float(g_cooldown))
	{
		new seconds = floatround(g_lastUse[id] + float(g_cooldown) - now, floatround_ceil);
		client_print_color(id, print_team_default, "^1%L", id,
			seconds == 1 ? "OLDS_CONTACT_WAIT_ONE" : "OLDS_CONTACT_WAIT", seconds);
		return PLUGIN_CONTINUE;
	}

	g_lastUse[id] = now;

	if (!g_listCount)
	{
		client_print_color(id, print_team_default, "^1%L", id, "OLDS_CONTACT_NONE");
		return PLUGIN_CONTINUE;
	}

	if (g_mode == MODE_MOTD)
		show_window(id);
	else
		show_chat(id);

	// The typed command stays a normal chat line, so other plugins that read
	// chat (logs, anti-spam, gag) still see it.
	return PLUGIN_CONTINUE;
}

show_chat(id)
{
	for (new i = 0; i < g_listCount; i++)
	{
		client_print_color(id, print_team_default, "^4%L^1 %s", id, ENTRY_LABELS[g_listEntry[i]], g_listValue[i]);
	}
}

show_window(id)
{
	// Room for the closing tags is kept back, so a very long value can only
	// shorten its own line, never leave the page unclosed.
	new const CLOSING[] = "</body></html>";

	new html[1536], length;
	new value[VALUE_LENGTH * 6 + 1];

	length = formatex(html, charsmax(html),
		"<html><head><meta charset=^"utf-8^"></head><body style=^"background:#111;color:#ddd;font-family:Verdana,Arial;font-size:15px;margin:24px^">");

	for (new i = 0; i < g_listCount; i++)
	{
		html_escape(g_listValue[i], value, charsmax(value));
		length += formatex(html[length], charsmax(html) - charsmax(CLOSING) - length,
			"<p><b style=^"color:#7c3^">%L</b> %s</p>", id, ENTRY_LABELS[g_listEntry[i]], value);
	}

	add(html, charsmax(html), CLOSING);

	new title[64];
	formatex(title, charsmax(title), "%L", id, "OLDS_CONTACT_TITLE");
	show_motd(id, html, title);
}

/**
 * Copies text to out with the characters that have a meaning in HTML
 * written as entities, so a value cannot break the window.
 */
html_escape(const text[], out[], maxLength)
{
	new length;

	for (new i = 0; text[i] && length < maxLength; i++)
	{
		switch (text[i])
		{
			case '<': length += copy(out[length], maxLength - length, "&lt;");
			case '>': length += copy(out[length], maxLength - length, "&gt;");
			case '&': length += copy(out[length], maxLength - length, "&amp;");
			case '"': length += copy(out[length], maxLength - length, "&quot;");
			default: out[length++] = text[i];
		}
	}

	out[length] = EOS;
}

/**
 * Drops bytes below 32 (including the chat colour codes), which have no
 * place in a contact line.
 */
remove_control_characters(text[])
{
	new to;

	for (new from = 0; text[from]; from++)
	{
		if ((text[from] & 0xFF) >= ' ')
			text[to++] = text[from];
	}

	text[to] = EOS;
}

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.