/*********************************************************
 * A replacement for plugin_message - Version 0.8        *
 *********************************************************
 *                                                       *
 * Name: plugin_rindy_message                            *
 * Author: Rinde (rinde@fiatnox.de)                      *
 * Released: 30/06/03                                    *
 *                                                       *
 *                                                       *
 * Vaultdata:                                            *
 *                                                       *
 *   MOTD_FILE  A path pointing to the file of scrolling *
 *              messages                                 *
 *                                                       *
 * File syntax:                                          *
 *                                                       *
 *   The file consists of one message-string per line,   *
 *   plus you can set some predefined environment        *
 *   variables, wich are:                                *
 *     o COLOR  This can be followed by one of red, blue *
 *              green, yellow, magenta, cyan and white,  *
 *              or any hexadecimal-coded RGB-Tripel as   *
 *              known from HTML                          *
 *     o TYPE   This sets the style of the messages,     *
 *              possible settings are csay and tsay      *
 *     o TIME   This sets the time the message shows     *
 *     o WAIT   This sets the delay before the next      *
 *              message is shown                         *
 *   All variables except WAIT are resetted to defaults  *
 *   when the linecounter resets to the first line.      *
 *     COLOR=green (#0AFAFA)                             *
 *     TYPE=CSAY                                         *
 *     TIME=10                                           *
 *   WAIT is resetted after every message, and defaults  *
 *   to the value of admin_repeat_freq                   *
 *                                                       *
 * Changelog:                                            *
 *                                                       *
 * Version 0.8                                           *
 *                                                       *
 *  -  Initial release                                   *
 *                                                       *
 *********************************************************/
 
/* Includes */
#include <adminlib>
#include <plugin>
 
/* Constants */
enum check_type {
    equal,
    contain,
    fileexist
}
 
/* Global Variables */
new g_Version[] = "0.8";
new g_File[50];
new g_Line = 0;
new g_Color[3] = {10,250,10};
new g_Type = 0;
new g_Time = 10;
new const g_ColorNames[7][] = {"red","green","yellow","blue","magenta","cyan","white"};
new const g_ColorRGB[7][3] = {{250,10,10},{10,250,10},{250,250,10},{10,10,250},{250,10,250},{10,250,250},{250,250,250}};
new g_Connected[MAX_PLAYERS];
 
 
/* Function Declarations */
forward TimerRepeatMsg(Timer,Repeat,HLUser,HLParam);
forward get_message(Message[MAX_TEXT_LENGTH]);
forward getcolor(String[]);
forward hextonum(int);
 
/* Event Handlers */
public plugin_init() {
    new error;
    plugin_registerinfo("Rinde's Message Plugin","A replacement for plugin_message",g_Version);
    check_setting(error,"file_access_read",equal,"1");
    check_setting(error,"admin_vault_file",fileexist);
    if(error) {
//        plugin_setdesc("ERROR, check logfile for details");
        return PLUGIN_CONTINUE;
    }
    plugin_registercmd("specmode","connect",ACCESS_ALL,"");
    new Message[MAX_TEXT_LENGTH];
    if(!get_vaultdata("MODT_FILE",g_File,50)) {
        strcpy(g_File,"addons/adminmod/config/motd.ini",50);
        set_vaultdata("MODT_FILE",g_File);
    }
    set_timer("TimerRepeatMsg",get_message(Message),0,Message);
    return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName,UserIndex) {
    g_Connected[UserIndex] = 0;
    return PLUGIN_CONTINUE;
}
 
public connect(HLCommand,HLData,HLUserName,UserIndex) {
    if(g_Connected[UserIndex]) {
        return PLUGIN_CONTINUE;
    }
    g_Connected[UserIndex] = 1;
    new Data[MAX_DATA_LENGTH];
    getstrvar("admin_connect_msg",Data,MAX_DATA_LENGTH);
    if(Data[0] == 0 || Data[0] == '0' && Data[1] == 0) {
        return PLUGIN_CONTINUE;
    }
    set_timer("TimerConnectMsg",2,5,Data);
    return PLUGIN_CONTINUE;
}
 
public TimerConnectMsg(Timer,Repeat,HLUser,HLParam) {
    new Data[MAX_DATA_LENGTH];
    convert_string(HLParam,Data,MAX_DATA_LENGTH);
    new UserName[MAX_NAME_LENGTH];
    convert_string(HLUser,UserName,MAX_NAME_LENGTH);
    messageex(UserName,Data,print_center);
}
 
public TimerRepeatMsg(Timer,Repeat,HLUser,HLParam) {
    new Message[MAX_TEXT_LENGTH];
    convert_string(HLParam,Message,MAX_TEXT_LENGTH);
    if(g_Type) {
        typesay(Message,g_Time,g_Color[0],g_Color[1],g_Color[2]);
    } else {
        centersay(Message,g_Time,g_Color[0],g_Color[1],g_Color[2]);
    }
    set_timer("TimerRepeatMsg",get_message(Message),0,Message);
}
 
/* Helper Functions */
get_message(Message[MAX_TEXT_LENGTH]) {
    new delay = getvar("admin_repeat_freq");
    new statement;
    do {
        if(!readfile(g_File,Message,++g_Line,MAX_DATA_LENGTH)) {
            g_Line = 1;
            g_Color[0] = 10; g_Color[1] = 250; g_Color[2] = 10;
            g_Time = 10;
            g_Type = 0;
            readfile(g_File,Message,1,MAX_DATA_LENGTH);
        }
        statement = 0;
        if(!strncmp(Message,"TYPE=",5)) {
            if(!strcasecmp(Message[5],"TSAY")) {
                g_Type = 1;
            } else if(!strcasecmp(Message[5],"CSAY")) {
                g_Type = 0;
            }
            statement = 1;
        } else if(!strncmp(Message,"TIME=",5)) {
            isnumeric(Message[5],g_Time);
            statement = 1;
        } else if(!strncmp(Message,"WAIT=",5)) {
            isnumeric(Message[5],delay);
            statement = 1;
        } else if(!strncmp(Message,"COLOR=",6)) {
            getcolor(Message[6]);
            statement = 1;
        }
    } while(statement);
    return delay;
}
 
getcolor(String[]) {
    new i;
    if(String[0] == '#') {
        if(strspn(String[1],"0123456789ABCDEF") == 6) {
            for(i=0;i<3;i++) {
                g_Color[i] = hextonum(String[(i<<1)+1])<<4 & hextonum(String[(i<<1)+2]);
            }
        }
    } else {
        for(i=0;i<7;i++) {
            if(!strcasecmp(String,g_ColorNames[i])) {
                g_Color[0] = g_ColorRGB[i][0]; g_Color[1] = g_ColorRGB[i][1]; g_Color[2] = g_ColorRGB[i][2];
                break;
            }
        }
    }
}
 
hextonum(int) {
    switch(int) {
        case '0': return  0;
        case '1': return  1;
        case '2': return  2;
        case '3': return  3;
        case '4': return  4;
        case '5': return  5;
        case '6': return  6;
        case '7': return  7;
        case '8': return  8;
        case '9': return  9;
        case 'A': return 10;
        case 'B': return 11;
        case 'C': return 12;
        case 'D': return 13;
        case 'E': return 14;
        case 'F': return 15;
    }
    return 0;
}
 
check_setting(&error,CvarName[],check_type:type,CvarValue[] = "") {
    new CvarData[MAX_DATA_LENGTH];
    getstrvar(CvarName,CvarData,MAX_DATA_LENGTH);
    switch(type) {
        case equal: {
            if(strcmp(CvarData,CvarValue)) {
                snprintf(CvarData,MAX_DATA_LENGTH,"[ERROR]: %s is required to equal %s",CvarName,CvarValue);
                error = 1;
                plugin_message(CvarData);
            }
        }
        case contain: {
            if(strstr(CvarData,CvarValue) == -1) {
                snprintf(CvarData,MAX_DATA_LENGTH,"[ERROR]: %s is required to contain %s",CvarName,CvarValue);
                error = 1;
                plugin_message(CvarData);
            }
        }
        case fileexist: {
            if(!fileexists(CvarData)) {
                snprintf(CvarData,MAX_DATA_LENGTH,"[ERROR]: %s is required to point to a valid file",CvarName);
                error = 1;
                plugin_message(CvarData);
            }
        }
    }
}
 
isnumeric(string[],&num) {
    new i = 0;
    new n = 0;
    if(string[0] == 0) {
        return 0;
    }
    while(string[i]) {
        i++;
        if('0' <= string[i] <= '9') {
            n *= 10;
            n += string[i] - '0';
        } else {
            return 0;
        }
    }
    num = n;
    return 1;
}