/*
* Command Scheduler (olds.ro workshop card LIB-1500)
*
* Runs server commands at set times of day (on chosen weekdays) or every
* N minutes, from the file configs/olds_cron.ini.
*
* File format, one job per line:
* HH:MM|days|command
* days = * for every day, or a list such as Sat,Sun or Mon,Wed,Fri
* every N minutes|command
* N = 1 to 1440; the job runs when the clock's minute count is a
* multiple of N (every 5 minutes = :00, :05, :10 ...), so its
* spacing stays the same over map changes
* Empty lines and lines starting with ; or // are skipped. A line that
* cannot be read is skipped and written to the AMX Mod X log; the other
* lines still work. At most 64 jobs.
*
* A repeating task looks at the clock every 5 seconds; each new minute is
* handled exactly once, also over a map change (the last minute handled is
* kept in the server's localinfo). Minutes missed during a map change (up
* to 5) are handled late. Commands run with server_cmd and server_exec.
*
* Cvar (saved in configs/plugins/olds_cron.cfg the first time the plugin
* starts):
* olds_cron_enable 1 1 = run the jobs, 0 = run nothing
*
* Server commands:
* olds_cron_status lists the jobs and how often each one ran
* olds_cron_reload reads configs/olds_cron.ini again
*
* 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[] = "Command Scheduler";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_cron.txt";
new const JOBS_FILE[] = "olds_cron.ini";
// Server localinfo key that keeps the last minute handled over map changes.
new const LAST_MINUTE_KEY[] = "olds_cron_last";
new const DAY_NAMES[][] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
const MAX_JOBS = 64;
const MAX_COMMAND = 192;
const ALL_DAYS = 0x7F;
const MAX_CATCH_UP = 5; // minutes
const Float:CHECK_SECONDS = 5.0;
const TASK_CHECK = 15001;
enum
{
JOB_TIME = 0,
JOB_EVERY
};
new g_enabled; // olds_cron_enable
new g_jobCount;
new g_jobType[MAX_JOBS];
new g_jobLine[MAX_JOBS];
new g_jobTime[MAX_JOBS][6]; // "HH:MM"
new g_jobDays[MAX_JOBS]; // bit 0 = Sunday ... bit 6 = Saturday
new g_jobEvery[MAX_JOBS]; // minutes
new g_jobCommand[MAX_JOBS][MAX_COMMAND];
new g_jobRuns[MAX_JOBS];
new g_skippedLines;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
bind_pcvar_num(create_cvar("olds_cron_enable", "1", FCVAR_NONE,
"1 = run the jobs of configs/olds_cron.ini, 0 = run nothing",
true, 0.0, true, 1.0), g_enabled);
AutoExecConfig(true, "olds_cron");
register_srvcmd("olds_cron_status", "cmd_status");
register_srvcmd("olds_cron_reload", "cmd_reload");
}
public plugin_cfg()
{
load_jobs();
new clock[8];
get_time("%H:%M", clock, charsmax(clock));
log_amx("%d job(s) read from configs/%s at %s.", g_jobCount, JOBS_FILE, clock);
set_task(CHECK_SECONDS, "task_check", TASK_CHECK, _, _, "b");
}
/**
* Every CHECK_SECONDS: handles each minute that began since the last check.
*/
public task_check()
{
new now = get_systime() / 60;
new last = get_last_minute();
if (!g_enabled)
{
// Nothing runs, and switching back on does not run old minutes.
set_last_minute(now);
return;
}
if (last == now)
return;
if (last <= 0 || last > now || now - last > MAX_CATCH_UP)
last = now - 1;
for (new minute = last + 1; minute <= now; minute++)
run_minute(minute * 60);
set_last_minute(now);
}
/**
* Runs every job due in the minute that starts at stamp (unix time).
*/
public run_minute(stamp)
{
new clock[8], weekday[4];
format_time(clock, charsmax(clock), "%H:%M", stamp);
format_time(weekday, charsmax(weekday), "%w", stamp);
new day = clamp(str_to_num(weekday), 0, 6);
new minute = stamp / 60;
for (new i = 0; i < g_jobCount; i++)
{
if (g_jobType[i] == JOB_TIME)
{
if (equal(g_jobTime[i], clock) && (g_jobDays[i] & (1 << day)))
run_job(i, clock, day);
}
else if (minute % g_jobEvery[i] == 0)
{
run_job(i, clock, day);
}
}
}
run_job(job, const clock[], day)
{
g_jobRuns[job]++;
log_amx("Job %d (line %d) runs at %s %s: %s", job + 1, g_jobLine[job], clock, DAY_NAMES[day], g_jobCommand[job]);
server_cmd("%s", g_jobCommand[job]);
server_exec();
}
get_last_minute()
{
new text[16];
get_localinfo(LAST_MINUTE_KEY, text, charsmax(text));
return str_to_num(text);
}
set_last_minute(minute)
{
new text[16];
num_to_str(minute, text, charsmax(text));
set_localinfo(LAST_MINUTE_KEY, text);
}
load_jobs()
{
g_jobCount = 0;
g_skippedLines = 0;
new path[PLATFORM_MAX_PATH];
get_configsdir(path, charsmax(path));
format(path, charsmax(path), "%s/%s", path, JOBS_FILE);
new file = fopen(path, "rt");
if (!file)
{
log_amx("configs/%s not found: no jobs.", JOBS_FILE);
return;
}
new line[256], lineNumber;
while (fgets(file, line, charsmax(line)))
{
lineNumber++;
trim(line);
if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
continue;
if (g_jobCount >= MAX_JOBS)
{
log_amx("configs/%s line %d skipped: only %d jobs are read.", JOBS_FILE, lineNumber, MAX_JOBS);
g_skippedLines++;
continue;
}
if (!parse_job(line, lineNumber))
g_skippedLines++;
}
fclose(file);
}
/**
* Reads one line into the next job slot. Logs and returns false when the
* line cannot be used.
*/
bool:parse_job(const line[], lineNumber)
{
new first[64], rest[256];
if (!split_at_bar(line, first, charsmax(first), rest, charsmax(rest)))
return skip_line(lineNumber, "no | separator", line);
new job = g_jobCount;
g_jobLine[job] = lineNumber;
g_jobRuns[job] = 0;
if (containi(first, "every") == 0)
{
new word[16], number[16], unit[16];
parse(first, word, charsmax(word), number, charsmax(number), unit, charsmax(unit));
new minutes = str_to_num(number);
if (!equali(word, "every") || !is_str_num(number) || minutes < 1 || minutes > 1440)
return skip_line(lineNumber, "the minutes must be a number from 1 to 1440", line);
if (!equali(unit, "minutes") && !equali(unit, "minute") && !equali(unit, "min") && !equali(unit, "mins"))
return skip_line(lineNumber, "write it as every N minutes", line);
trim(rest);
if (!rest[0])
return skip_line(lineNumber, "no command", line);
g_jobType[job] = JOB_EVERY;
g_jobEvery[job] = minutes;
copy(g_jobCommand[job], MAX_COMMAND - 1, rest);
g_jobCount++;
return true;
}
new days[64], command[256];
if (!split_at_bar(rest, days, charsmax(days), command, charsmax(command)))
return skip_line(lineNumber, "a time line needs HH:MM|days|command", line);
new hour, minute;
if (!parse_clock(first, hour, minute))
return skip_line(lineNumber, "the time must be HH:MM from 00:00 to 23:59", line);
new dayBits = parse_days(days);
if (!dayBits)
return skip_line(lineNumber, "the days must be * or names such as Sat,Sun", line);
trim(command);
if (!command[0])
return skip_line(lineNumber, "no command", line);
g_jobType[job] = JOB_TIME;
formatex(g_jobTime[job], charsmax(g_jobTime[]), "%02d:%02d", hour, minute);
g_jobDays[job] = dayBits;
copy(g_jobCommand[job], MAX_COMMAND - 1, command);
g_jobCount++;
return true;
}
bool:skip_line(lineNumber, const reason[], const line[])
{
log_amx("configs/%s line %d skipped (%s): %s", JOBS_FILE, lineNumber, reason, line);
return false;
}
/**
* left = text before the first |, right = the text after it, both trimmed.
*/
bool:split_at_bar(const text[], left[], leftLength, right[], rightLength)
{
new bar = contain(text, "|");
if (bar == -1)
return false;
copy(left, min(bar, leftLength), text);
copy(right, rightLength, text[bar + 1]);
trim(left);
trim(right);
return true;
}
/**
* "H:MM" or "HH:MM".
*/
bool:parse_clock(const text[], &hour, &minute)
{
new colon = contain(text, ":");
if (colon < 1 || colon > 2 || strlen(text) != colon + 3)
return false;
new hourText[4], minuteText[4];
copy(hourText, colon, text);
copy(minuteText, 2, text[colon + 1]);
if (!is_str_num(hourText) || !is_str_num(minuteText))
return false;
hour = str_to_num(hourText);
minute = str_to_num(minuteText);
return (hour <= 23 && minute <= 59);
}
/**
* "*" = every day; otherwise names separated by commas. Returns the day
* bits, or 0 when a name is wrong.
*/
parse_days(const text[])
{
if (equal(text, "*"))
return ALL_DAYS;
new bits, name[16], length, i, found;
for (;;)
{
// One name, up to the next comma.
length = 0;
while (text[i] && text[i] != ',')
{
if (length < charsmax(name))
name[length++] = text[i];
i++;
}
name[length] = EOS;
trim(name);
if (name[0])
{
found = find_day(name);
if (found == -1)
return 0;
bits |= (1 << found);
}
if (!text[i])
break;
i++;
}
return bits;
}
/**
* Sat or Saturday (any case) = 6; -1 when it is no day name.
*/
find_day(const name[])
{
for (new day = 0; day < sizeof(DAY_NAMES); day++)
{
if (equali(name, DAY_NAMES[day]) || is_full_day_name(name, day))
return day;
}
return -1;
}
/**
* Accepts full names too: Saturday, Sunday ...
*/
bool:is_full_day_name(const name[], day)
{
static const FULL_NAMES[][] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
return bool:equali(name, FULL_NAMES[day]);
}
public cmd_reload()
{
load_jobs();
server_print("%L", LANG_SERVER, "OLDS_CRON_LOADED", g_jobCount, JOBS_FILE, g_skippedLines);
return PLUGIN_HANDLED;
}
public cmd_status()
{
new last = get_last_minute(), clock[24];
if (last > 0)
format_time(clock, charsmax(clock), "%Y-%m-%d %H:%M", last * 60);
else
copy(clock, charsmax(clock), "none yet");
server_print("%L", LANG_SERVER, "OLDS_CRON_STATUS", g_enabled, g_jobCount, clock);
new days[40];
for (new i = 0; i < g_jobCount; i++)
{
if (g_jobType[i] == JOB_EVERY)
{
server_print("%L", LANG_SERVER, "OLDS_CRON_JOB_EVERY", i + 1, g_jobLine[i],
g_jobEvery[i], g_jobRuns[i], g_jobCommand[i]);
continue;
}
describe_days(g_jobDays[i], days, charsmax(days));
server_print("%L", LANG_SERVER, "OLDS_CRON_JOB_TIME", i + 1, g_jobLine[i],
g_jobTime[i], days, g_jobRuns[i], g_jobCommand[i]);
}
return PLUGIN_HANDLED;
}
describe_days(bits, out[], maxLength)
{
if (bits == ALL_DAYS)
{
copy(out, maxLength, "every day");
return;
}
out[0] = EOS;
for (new day = 0; day < sizeof(DAY_NAMES); day++)
{
if (!(bits & (1 << day)))
continue;
if (out[0])
add(out, maxLength, ",");
add(out, maxLength, DAY_NAMES[day]);
}
}