/**
* ==================================================================================
* Server Clean Up Change Log
* ==================================================================================
*
* 0.1
* - Initial release ported from SourceMod.
* ==================================================================================
*/
// notes
//
// custom.hpk cleaning?
#include <amxmodx>
#include <amxmisc>
#if AMXX_VERSION_NUM < 183
#error "Requires AMX Mod X 1.8.3-dev to compile, go to http://www.amxmodx.org/snapshots.php to update your install."
#endif
#define PLUGIN_VERSION "0.1 beta"
#define DEBUG 0
#define LOG 0
#define AXL 1
#define DEM 2
#define SPR 3
#define MAX 4
new cvar_type[MAX]
new cvar_time[MAX]
new cvar_demopath
new cvar_demofilename
new cvar_arch
new cvar_enable
new cvar_logtype
new cvar_logging
new g_logsdir
#if DEBUG
new LogFilePath[255]
#endif
public plugin_init()
{
register_plugin("Server Clean Up", PLUGIN_VERSION, "Jamster")
register_dictionary("servercleanup.txt")
cvar_enable = register_cvar("amx_srvcln_enable", "1")
cvar_type[LOG] = register_cvar("amx_srvcln_logs", "0")
cvar_type[AXL] = register_cvar("amx_srvcln_amxlogs", "0")
cvar_type[DEM] = register_cvar("amx_srvcln_demos", "0")
cvar_logging = register_cvar("amx_srvcln_logging_mode", "0")
cvar_demofilename = register_cvar("amx_srvcln_demos_filename", "auto-")
cvar_demopath = register_cvar("amx_srvcln_demos_path", "")
cvar_type[SPR] = register_cvar("amx_srvcln_sprays", "0")
cvar_arch = register_cvar("amx_srvcln_demos_archives", "0")
cvar_logtype = register_cvar("amx_srvcln_amxlogs_type", "0")
cvar_time[LOG] = register_cvar("amx_srvcln_logs_time", "168")
cvar_time[SPR] = register_cvar("amx_srvcln_sprays_time", "168")
cvar_time[AXL] = register_cvar("amx_srvcln_amxlogs_time", "168")
cvar_time[DEM] = register_cvar("amx_srvcln_demos_time", "168")
new desc[64]
formatex(desc, sizeof(desc), "%L", LANG_SERVER, "SRVCLN_NOW");
register_srvcmd("amx_srvcln_now", "command_clean_now", -1, desc)
new basepath[255]
get_basedir(basepath, sizeof(basepath))
g_logsdir = get_cvar_pointer("logsdir")
#if DEBUG
formatex(LogFilePath, sizeof(LogFilePath), "%s/logs/srvcln_debug.log", basepath)
#endif
}
public plugin_cfg()
{
if (get_pcvar_num(cvar_enable))
{
for (new i; i < MAX; i++)
if (get_pcvar_num(cvar_type[i]))
CleanServer(i)
}
}
public command_clean_now(id, level, cid)
{
console_print(id, "%L", LANG_SERVER, "SRVCLN_STRT")
for (new i; i < MAX; i++)
if (get_pcvar_num(cvar_type[i]))
CleanServer(i)
console_print(id, "%L", LANG_SERVER, "SRVCLN_END")
log_amx("%L", LANG_SERVER, "SRVCLN_LOG")
return PLUGIN_HANDLED
}
CleanServer(const type)
{
new Time32
new TimeType = get_pcvar_num(cvar_time[type])
new filename[255]
new dir[255]
new basepath[255]
get_basedir(basepath, sizeof(basepath))
if (TimeType != -1)
{
if (TimeType < 12)
{
Time32 = get_systime() / 3600 - 12
TimeType = 12
}
else
{
Time32 = get_systime() / 3600 - TimeType
}
}
else
{
new day[10]
format_time(day, sizeof(day), "%Y%j")
Time32 = str_to_num(day)
}
switch (type)
{
case LOG:
get_pcvar_string(g_logsdir, dir, sizeof(dir))
case AXL:
formatex(dir, sizeof(dir), "%s/logs", basepath)
case DEM:
get_pcvar_string(cvar_demopath, dir, sizeof(dir))
case SPR:
formatex(dir, sizeof(dir), "downloads")
}
if (!dir_exists(dir) && strlen(dir))
return false
#if DEBUG
switch (type)
{
case LOG:
log_to_file(LogFilePath, "~~ Regular logs dir files ~~")
case AXL:
log_to_file(LogFilePath, "~~ AMXX logs dir files ~~")
case DEM:
log_to_file(LogFilePath, "~~ Demo files ~~")
case SPR:
log_to_file(LogFilePath, "~~ Spray files ~~")
}
#endif
new strLength
new DelArch = get_pcvar_num(cvar_arch)
new LogType = get_pcvar_num(cvar_logtype)
new logging = get_pcvar_num(cvar_logging)
new demoname[255];
get_pcvar_string(cvar_demofilename, demoname, sizeof(demoname))
new h_dir;
new lastfile[255] = '?'
for (h_dir = open_dir(dir, filename, sizeof(filename));;next_file(h_dir, filename, sizeof(filename)))
{
// Living in the danger zone. This is honestly the only way I could get this loop to work properly.
// If there's a better way please do tell.
if (equali(filename, lastfile))
break;
copy(lastfile, sizeof(lastfile), filename)
if (equali(filename, ".") || equali(filename, ".."))
continue
strLength = strlen(filename)
if (type == LOG)
{
if (containi(filename, ".log") == strLength-4)
{
CanDelete(Time32, dir, filename, type, logging)
continue
}
}
else if (type == AXL)
{
if (!LogType)
{
if (containi(filename, "L") == 0 && containi(filename, ".log") == strLength-4)
{
CanDelete(Time32, dir, filename, type, logging)
continue
}
}
else if (LogType == 1)
{
if ((containi(filename, "L") == 0 || containi(filename, "error_") == 0) && containi(filename, ".log") == strLength-4)
{
CanDelete(Time32, dir, filename, type, logging)
continue
}
}
else if (LogType == 2 && containi(filename, ".log") == strLength-4)
{
CanDelete(Time32, dir, filename, type, logging)
continue
}
}
else if (type == DEM)
{
if (containi(filename, demoname) == 0 && containi(filename, ".dem") == strLength-4)
{
CanDelete(Time32, dir, filename, type, logging)
continue
}
else if (DelArch && containi(filename, demoname) == 0 && (containi(filename, ".zip") == strLength-4 || containi(filename, ".bz2") == strLength-4 || containi(filename, ".rar") == strLength-4 || containi(filename, ".7z") == strLength-3))
{
CanDelete(Time32, dir, filename, type, logging)
continue
}
}
else if (type == SPR)
{
if (containi(filename, ".dat") == strLength-4 || containi(filename, ".ztmp") == strLength-5)
{
CanDelete(Time32, dir, filename, type, logging)
continue
}
}
}
close_dir(h_dir)
return true
}
CanDelete(const Time32, const dir[], const filename[], const type, const logging)
{
#if DEBUG
log_to_file(LogFilePath, "%s", filename)
#endif
new TimeStamp
new file[255]
formatex(file, sizeof(file), "%s/%s", dir, filename)
if (type == SPR)
{
// It's Happening
TimeStamp = GetFileTime(file, FileTime_LastAccess)
if (TimeStamp == -1)
TimeStamp = GetFileTime(file, FileTime_LastChange)
}
else
TimeStamp = GetFileTime(file, FileTime_LastChange)
if (TimeStamp == -1)
log_message("%L", LANG_SERVER, "SRVCLN_ERR_TS", file)
TimeStamp /= 3600
if (Time32 > TimeStamp)
{
if (!delete_file(file))
log_message("%L", LANG_SERVER, "SRVCLN_ERR_DEL", file)
else if (logging)
log_message("%L", LANG_SERVER, "SRVCLN_DEL_FILE", file)
#if DEBUG
log_to_file(LogFilePath, "*deleted file*")
#endif
}
return
}