#include <amxmodx>
#include <amxmisc>
#define FILE_CRON "crontab.ini"
public plugin_init()
{
register_plugin("CronTab Lite", "0.1", "uMk0");
}
enum _:settings
{
G_CRON[64],
G_TASK[512]
}
new gSetting[1024][settings];
public plugin_precache()
{
new szLine[1024], szPath[64], szPathFile[128], i = 0;
get_localinfo("amxx_configsdir", szPath, charsmax(szPath));
formatex(szPathFile, charsmax(szPathFile), "%s/%s", szPath, FILE_CRON);
new fp = fopen(szPathFile, "rt");
if (fp)
{
while (!feof(fp))
{
fgets(fp, szLine, sizeof(szLine));
trim(szLine);
if (!szLine[0] || szLine[0] == ';')
continue;
parse(szLine, gSetting[i][G_CRON], charsmax(gSetting[][G_CRON]), gSetting[i][G_TASK], charsmax(gSetting[][G_TASK]));
i++;
}
fclose(fp);
}
new szCurrentSec[32];
get_time("%S", szCurrentSec, 31);
new Float:fCurrentSec = str_to_float(szCurrentSec);
set_task((60.0 - fCurrentSec), "globalMinuteStart", 0, _, _, "a", 1);
}
public globalMinuteStart()
{
Task_Execute()
set_task(60.0, "Task_Execute", 0, _, _, "b");
}
public Task_Execute()
{
for (new i = 0; i < sizeof(gSetting); i++)
{
if (!gSetting[i][G_CRON]) break;
if (Cron_Matches(gSetting[i][G_CRON]))
{
server_cmd(gSetting[i][G_TASK]);
}
}
}
public Cron_Matches(szCron[])
{
new szMinute[8], szHour[8], szDay[8], szMonth[8], szWeekday[8], szTmp[64];
strtok(szCron, szMinute, charsmax(szMinute), szTmp, charsmax(szTmp), _, 1);
strtok(szTmp, szHour, charsmax(szHour), szTmp, charsmax(szTmp), _, 1);
strtok(szTmp, szDay, charsmax(szDay), szTmp, charsmax(szTmp), _, 1);
strtok(szTmp, szMonth, charsmax(szMonth), szTmp, charsmax(szTmp), _, 1);
strtok(szTmp, szWeekday, charsmax(szWeekday), szTmp, charsmax(szTmp), _, 1);
new szCurrentMinute[8], szCurrentHour[8], szCurrentDay[8], szCurrentMonth[8], szCurrentWeekday[8];
get_time("%M", szCurrentMinute, charsmax(szCurrentMinute));
get_time("%H", szCurrentHour, charsmax(szCurrentHour));
get_time("%d", szCurrentDay, charsmax(szCurrentDay));
get_time("%m", szCurrentMonth, charsmax(szCurrentMonth));
get_time("%w", szCurrentWeekday, charsmax(szCurrentWeekday));
new iCurrentMinute = str_to_num(szCurrentMinute);
new iCurrentHour = str_to_num(szCurrentHour);
new iCurrentDay = str_to_num(szCurrentDay);
new iCurrentMonth = str_to_num(szCurrentMonth);
new iCurrentWeekday = str_to_num(szCurrentWeekday);
if (!Cron_Field_Matches(szMinute, iCurrentMinute, "0-59") || !Cron_Field_Matches(szHour, iCurrentHour, "0-23") || !Cron_Field_Matches(szDay, iCurrentDay, "1-31") || !Cron_Field_Matches(szMonth, iCurrentMonth, "1-12") || !Cron_Field_Matches(szWeekday, iCurrentWeekday, "0-6"))
{
return false;
}
return true;
}
public Cron_Field_Matches(szField[], iValue, szRange[])
{
new szRangeStart[8], szRangeEnd[8], szStep[8], szArg1[10];
strtok(szRange, szRangeStart, charsmax(szRangeStart), szRangeEnd, charsmax(szRangeEnd), '-');
new iStart = str_to_num(szRangeStart);
new iEnd = str_to_num(szRangeEnd);
if (contain(szField, "/") != -1)
{
strtok(szField, szArg1, charsmax(szArg1), szStep, charsmax(szStep), '/');
new iStep = str_to_num(szStep);
if (iStart < iStep || iStep > iEnd)
return false;
if (iStep == 0)
return false;
if (iValue % iStep != 0)
return false;
return true;
}
if (contain(szField, ",") != -1)
{
new szValue[8];
while (strtok(szField, szValue, charsmax(szValue), szArg1, charsmax(szArg1), ','))
{
new iStep = str_to_num(szValue);
if (!(iStart < iStep || iStep > iEnd))
{
if (iStep == iValue)
return true;
}
}
return false;
}
if (contain(szField, "-") != -1)
{
new szFieldRangeStart[8], szFieldRangeEnd[8];
strtok(szField, szFieldRangeStart, charsmax(szFieldRangeStart), szFieldRangeEnd, charsmax(szFieldRangeEnd), '-');
new iFieldStart = str_to_num(szFieldRangeStart);
new iFieldEnd = str_to_num(szFieldRangeEnd);
if (iStart < iFieldStart || iFieldStart > iEnd)
return false;
if (iStart < iFieldEnd || iFieldEnd > iEnd)
return false;
if (iValue < iFieldStart || iValue > iFieldEnd)
return false;
return true
}
if (contain(szField, "*") != -1)
{
return true
}
new iStep = str_to_num(szField);
if (!(iStart < iStep || iStep > iEnd))
{
if (iStep == iValue)
return true;
}
return false;
}