#include "melange.h"

void sys_SetSysCap(int user, char *capability);
int sys_SendSysMsg(int mask,int channel,char *msg,int user);
int sys_SendChannelUpdate(int user);
int sys_SendUserUpdate(int channel,int user);
int sys_SendAllUserUpdate(int user);
void sys_SendShutdownCommand(int user);
void sys_SendLoginCommand(int user, int mode);
int sys_SendReqAnswer(int user);

void sys_SetSysCap(int user, char *capability) {
    unsigned char mySysCap=0x00;

    if (strlen(capability)>2) {
    if (capability[0]=='1')
	mySysCap|=0x080;
    if (capability[1]=='1')
	mySysCap|=0x040;
    if (capability[2]=='1')
	mySysCap|=0x020;
    if (capability[3]=='1')
	mySysCap|=0x010;
    }
    mClient[user].syscap=mySysCap;
}

int sys_SendReqAnswer(int user) {
    if (mAdmin.sysMsg==NO)
	return(-1);

    sys_SendChannelUpdate(user);
    sys_SendUserUpdate(mClient[user].channel,user);
    sys_SendAllUserUpdate(user);
    return(0);
}

int sys_SendChannelUpdate(int user) {
    #define MAX_CTXT 119
    #define MAX_BUFFER 2044
    char buffer[MAX_BUFFER + 1];
    char ctxt[MAX_CTXT + 1];
    int i;

    if (mAdmin.sysMsg==NO)
	return(-1);
    strncpy(buffer,"C", MAX_BUFFER + 1);
    for (i=0;i<maxChannels;i++) {
	if (mChannel[i].owner!=NOBODY) {
	    if (mChannel[i].locked==YES)
		snprintf(ctxt,MAX_CTXT + 1, "%d,%s,1;",i,mChannel[i].name);
	    else
		if (mChannel[i].owner==SYSTEM)
		    snprintf(ctxt,MAX_CTXT + 1, "%d,%s,2;",i,mChannel[i].name);
		else
		    snprintf(ctxt,MAX_CTXT + 1, "%d,%s,0;",i,mChannel[i].name);
            *(ctxt + MAX_CTXT) = '\0';		    
	    if ((strlen(buffer) + strlen(ctxt)) <= MAX_BUFFER)
		strcat(buffer,ctxt);
	}
    }
    sys_SendSysMsg(0x020,0,buffer,user);
    return(0);
#undef MAX_CTXT    
#undef MAX_BUFFER
}

int sys_SendUserUpdate(int channel,int user) {
#define MAX_BUFFER 2044
#define MAX_UTX 119


    char buffer[MAX_BUFFER + 1];
    char utxt[MAX_UTX + 1];
    int i;

    if (mAdmin.sysMsg==NO)
	return(-1);
    snprintf(buffer,MAX_BUFFER + 1, "B%d,%s,%d,%s",
                    channel,
                    mChannel[channel].name,
                    mChannel[channel].owner,
                    mChannel[channel].locked == YES? "1;" : "0;");
    *(buffer + MAX_BUFFER) = '\0';                
    for (i=0;i<maxClients;i++)
	if ((mClient[i].online==YES)&&(mClient[i].channel==channel)) {
	    snprintf (utxt,MAX_UTX + 1, "%d,%s;",i,mClient[i].name);
	    *(utxt + MAX_UTX) = '\0';
	    if ((strlen(buffer) + strlen(utxt)) <= MAX_BUFFER)
		strcat(buffer,utxt);
	}
    sys_SendSysMsg(0x040,channel,buffer,user);
    return(0);
#undef MAX_BUFFER
#undef MAX_UTX    
}

int sys_SendAllUserUpdate(int user) {
#define MAX_BUFFER 2044
#define MAX_UTX 119


    char buffer[MAX_BUFFER + 1];
    char utxt[MAX_UTX + 1];
    int i;

    if (mAdmin.sysMsg==NO)
	return(-1);
    strncpy(buffer,"A", MAX_BUFFER + 1);
    for (i=0;i<maxClients;i++)
	if (mClient[i].online==YES) {
	    snprintf (utxt, MAX_UTX + 1, "%d,%s,%d;",i,mClient[i].name,mClient[i].channel);
	    *(utxt + MAX_UTX) = '\0';
	    if ((strlen(buffer) + strlen(utxt)) <= MAX_BUFFER )
		strcat(buffer,utxt);
	}
    sys_SendSysMsg(0x080,0,buffer,user);
    return(0);
#undef MAX_BUFFER
#undef MAX_UTX    


}

void sys_SendShutdownCommand(int user) {
#define MAX_BUFFER 14
    char buffer[MAX_BUFFER + 1];
    
    strncpy(buffer,"+++Quit\r\n", MAX_BUFFER + 1);
    *(buffer + MAX_BUFFER) = '\0';
    if (mClient[user].socket!=0)
	write(mClient[user].socket,buffer,strlen(buffer));

#undef MAX_BUFFER	
}




void sys_SendLoginCommand(int user, int mode) {
#define MAX_BUFFER 14
    char buffer[MAX_BUFFER + 1];

    if (mode==YES)    
	strncpy(buffer,"+++Online\r\n", MAX_BUFFER + 1);
    else
	strncpy(buffer,"+++Failed\r\n", MAX_BUFFER + 1);
    *(buffer + MAX_BUFFER) = '\0';	
    if (mClient[user].socket!=0)
	write(mClient[user].socket,buffer,strlen(buffer));
#undef MAX_BUFFER

}

int sys_SendSysMsg(int mask,int channel,char *msg,int user) {
#define MAX_BUFFER 2044

    char buffer[MAX_BUFFER + 1];
    int i;
    int buffer_len;

    if( (strlen(msg) + strlen("+++\r\n")) <= MAX_BUFFER)
     {
        snprintf (buffer,MAX_BUFFER + 1, "+++%s\r\n",msg);
        *(buffer + MAX_BUFFER) = '\0';
     }
    else
       return -1;  
       
   buffer_len = strlen(buffer);      

    if (user==SYSTEM) {
	if (mask==0x040) {
	    for (i=0;i<maxClients;i++)
    		if (mClient[i].socket!=0)
		    if ((mClient[i].syscap & mask) == mask)
			if (mClient[i].channel==channel) 
			    write(mClient[i].socket,buffer,buffer_len);
	}
	else {
	    for (i=0;i<maxClients;i++)
    		if (mClient[i].socket!=0)
		    if ((mClient[i].syscap & mask) == mask)
			write(mClient[i].socket,buffer,buffer_len);
	}
	return(0);
    }
    if (mask==0x040) {
	if (mClient[user].socket!=0)
	    if ((mClient[user].syscap & mask) == mask)
		if (mClient[user].channel==channel) 
		    write(mClient[user].socket,buffer,buffer_len);
    }
    else {
	if (mClient[user].socket!=0)
	    if ((mClient[user].syscap & mask) == mask)
		write(mClient[user].socket,buffer,buffer_len);
    }
    return(0);
#undef MAX_BUFFER    
}
