#include "melange.h"



int chat_InterpretData(int sender, char *data, int *shutDownServer) 
{
#define MAX_BUFFER 500 /* total size of msg that will be sent to any user.
                       ** This must be large enough to include the message
                       ** the person typed, plus their real name, which gets
                       ** inserted before the message, e.g.:
                       **
                       ** [0,Steve-Narmontas]: this is what i typed
                       */
                       
#define MAX_MSG_SIZE 450  /* Any message typed by a user will get truncated to this
                          ** length.  MAX_MSG_SIZE + length of [0, Steve-Narmontas]
                          ** must be <= MAX_BUFFER
                          */

#define MAX_COMMAND 9    /* Max length of a command, e.g. /help */
#define MAX_OPTION 99    /* Max length of a command option */

#define MAX_PARAM 599    /* what is this???? */

char buffer[MAX_BUFFER + 1];   /* the string that will be send to user sockets */
    
char command[MAX_COMMAND + 1];  /* a command extracted from what a user typed */
char option[MAX_OPTION + 1];    /* the command option extracted from what a user typed */
char parameter[MAX_PARAM + 1];  /* what is this??? */
    
int cut1 = 0, cut2 = 0;
int i;
    
   /* This will be set to 'YES' only when an /admin shutdown command is verified */ 
   *shutDownServer = NO;
    
    /* this user typed something, so reset their idle time */
    mClient[sender].idle=time(NULL);
    
    
    parameter[0]=option[0]=command[0]='\0';

    /* Truncate messages > MAX_MSG_SIZE */
    if (strlen(data)>MAX_MSG_SIZE)
    {
        strncpy(buffer,data, MAX_MSG_SIZE - 2);
        strcat(buffer,"\n");
	strcpy(data,buffer);
    }
    
    
    if ((data[0] != '.') && (data[0]!=',') && (data[0]!='/') )   /* if a command was not typed */ 
    {
        /* no one named 'unknown' should ever get to this point!!!
        ** this prevents people from accessing this chat from outside
        ** of Manhattan
        */
        if(!strcmp(mClient[sender].name, "unknown"))  
               {
                logout_unknown_intruder(sender);
                return 2;
               } 
               
        /* Ignore empty messages and messages from anyone
        ** who is no longer online - else send the message
        ** to everyone in the channel
        */       
	if ((strlen(data)>1)&&(mClient[sender].online==YES)) 
	 {
	    comm_SendGroup(sender,data);
            snprintf(msgText,MAX_MSG_TEXT + 1,"<%d-%d, %s>: %s",mClient[sender].channel,sender,mClient[sender].name,data);
	    *(msgText + MAX_MSG_TEXT) = '\0';
	    util_WriteMsgLog(msgText);
	    return(0);						
	 }
	
	if (mClient[sender].online==NO)
	    comm_SendTo(SYSMSG,sender,MSG_UNKNOWN);
	return(0);						// Not a command !
    }

    strncpy(buffer,data,MAX_BUFFER + 1);
    *(buffer + MAX_BUFFER) = '\0';
    
    strncpy(data,util_FitString(buffer),BUFSIZ);			

    if (strlen(data)<2)					// Can't be a command !
        return(-1);
	
    if ((cut1=util_IndexOf(data,0))==-1) 
        cut1=strlen(data);
    else
        if ((cut2=util_IndexOf(data,cut1+1))==-1) 
    	    cut2=strlen(data);
	
    if ((cut1>10)||(cut2>80)) 
        return(-1);
	    
    for (i=1;i<cut1;i++) 				/* get the command */
        { command[i-1]=data[i]; command[i]=0; }
	
    for (i=cut1+1;i<cut2;i++) 				/* get the option */
	{ option[i-1-cut1]=data[i]; option[i-cut1]=0; }
	
    if ((cut2!=0)&&(cut2!=(int)strlen(data)))			/* get the parameter */
        for (i=cut2+1; i < (int)strlen(data); i++) 
	    { parameter[i-1-cut2]=data[i]; parameter[i-cut2]=0; }

    if ((strcasecmp(command,"ex")==0) ||
	(strcasecmp(command,"quit")==0) ||
	(strcasecmp(command,"qu")==0)) {
        snprintf (infoText,MAX_INFO_TEXT + 1, "LOGOUT:%s:host %s:line %d socket %d:user left nicely\r\n",
                                                  mClient[sender].name,mClient[sender].host, sender,mClient[sender].socket);
        *(infoText + MAX_INFO_TEXT) = '\0';   
        util_WriteLog(1,infoText);
	chat_Logout(sender,DEFAULT);
	return(2);
    }
    
    if ((strcasecmp(command,"n")==0)||
	(strcasecmp(command,"nick")==0)) {
	chat_ChangeNick(sender,option,1);
        return(1);
    }
    
    /** still here? User entered a command other than 'quit' or 'nick'
    *** Kick them off if their name is 'unknown'.  This helps keep out
    *** people port scanning the server and logging in via telnet.
    **/
     if(!strcmp(mClient[sender].name, "unknown"))       /* Manhattan security - immediately logout anyone named unknown */
               {
                logout_unknown_intruder(sender);
                return 2;
               } 

    
    
    
    if ((strcasecmp(command,"f")==0)||
	(strcasecmp(command,"finger")==0)) {
	chat_Finger(sender);
        return(1);
    }
    if ((strcasecmp(command,"he")==0)||
	(strcasecmp(command,"help")==0)) {
        chat_Help(sender);
        return(1);
    }
    if ((strcasecmp(command,"v")==0)||
	(strcasecmp(command,"ver")==0)) {
        comm_SendTo(SYSMSG,sender,VERSION);
        return(1);
    }
    if (strcasecmp(command,"syscap")==0) {
	sys_SetSysCap(sender,option);
	return(1);
    }
    if (strcasecmp(command,"sysreq")==0) {
	sys_SendReqAnswer(sender);
	return(1);
    }
    if (strcasecmp(command,"admin")==0) {
	if (admin_InterpretAdminCommand(sender,option,parameter)==100)
	  {
	    *shutDownServer = YES;
	    return(100);
	  }  
	return(1);
    }
    if ((strcasecmp(command,"newgroup")==0)||
        (strcasecmp(command,"grpadmin")==0)) {
	admin_CreateGroup(sender, option, parameter);
	return(0);
    }
    if ((mAdmin.uniqueNames==YES)&&(mClient[sender].online==NO)) {
	if (strcasecmp(command,"login")==0) {
	    chat_Login(sender,option);
    	    return(1);
	}
        comm_SendTo(SYSMSG,sender,MSG_UNKNOWN);
	return(-1);
    }
    
    if ((strcasecmp(command,"y")==0)||
	(strcasecmp(command,"yell")==0)) {
	chat_Yell(sender,option,parameter);
	return(1);
    }
    if ((strcasecmp(command,"msg")==0)||
	(strcasecmp(command,"notice")==0)) {
	chat_Whisper(sender,option,parameter);
	return(1);
    }

    if ((strcasecmp(command,"p")==0)||
        (strcasecmp(command,"page")==0)) {
        chat_Page(sender,option,parameter);
        return(1);
    }

    if ((strcasecmp(command,"s")==0)||
	(strcasecmp(command,"squelch")==0)) {
	chat_Squelch(sender,option);
	return(1);
    }
    if ((strcasecmp(command,"c")==0)||
	(strcasecmp(command,"join")==0)) {
	chat_JoinChannel(sender,option);
	return(1);
    }
    if ((strcasecmp(command,"o")==0)||
	(strcasecmp(command,"own")==0)) {
	chat_ChangeOwner(sender,option);
	return(1);
    }
    if ((strcasecmp(command,"l")==0)||
	(strcasecmp(command,"lock")==0)) {
	chat_LockChannel(sender);
	return(1);
    }
    if ((strcasecmp(command,"r")==0)||
	(strcasecmp(command,"topic")==0)) {
	chat_ChangeChannelName(sender,option);
	return(1);
    }
    if (strcasecmp(command,"invite")==0) {
	chat_Invite(sender,option);
	return(1);
    }
    if (strcasecmp(command,"kick")==0) {
	chat_KickOutOfChannel(sender,option);
	return(1);
    }
    if ((strcasecmp(command,"a")==0)
	||(strcasecmp(command,"list")==0)) {
	chat_ListChannels(sender);
	return(1);
    }
    if (strcasecmp(command,"fetch")==0) {
	chat_FetchUser(sender,option);
	return(1);
    }
    if ((strcasecmp(command,"id")==0)||
	(strcasecmp(command,"me")==0)) {
        chat_Id(sender);
        return(1);
    }
    if ((strcasecmp(command,"w")==0)||
	(strcasecmp(command,"whois")==0)) {
        chat_Whois(sender);
        return(1);
    }
    if (strcasecmp(command,"up")==0) {
        chat_Uptime(sender);
        return(1);
    }
    if (strcasecmp(command,"time")==0) {
        chat_ShowTime(sender);
        return(1);
    }
    /**** MANHATTAN -- disable 'group' command 
    if (strcasecmp(command,"group")==0) {
        chat_SetUserGroup(sender,option,parameter);
        return(1);
    }
    if ((strcasecmp(command,"chgroup")==0)||
       (strcasecmp(command,"setgroup")==0)) {
        chat_SetChannelGroup(sender,option);
        return(1);
    }
    ****** end MANHATTAN comment out ****/
    
    comm_SendTo(SYSMSG,sender,MSG_UNKNOWN);
    return(-1);
}   
