#include "melange.h"

int comm_SendAll(int sender, char *msg);
int comm_SendAllBut(int sender, int mask, char *msg);
int comm_SendGroup(int sender, char *msg);
int comm_SendGroupBut(int sender, int mask, char *msg);
int comm_SendChannel(int sender, int channel, char *msg);
int comm_SendChannelBut(int sender, int channel, int mask, char *msg);
int comm_SendTo(int sender,int receiver,char *msg);
int comm_SendPage(int sender,int receiver,char *msg);


void
log_before_send(int line)
{
 sprintf(infoText,"Going to write to user: %d\r\n", line);
 util_WriteLog(1, infoText);
}


void
log_after_send(int line)
{
 sprintf(infoText,"Done writing to %d \r\n", line);
 util_WriteLog(1, infoText);
}

 
 

void safe_write(int line, char *buffer)
{
  int result;

//   log_before_send(line);
      
   if(!mClient[line].socket)
      return;
          
   result =  send(mClient[line].socket,buffer,strlen(buffer), MSG_DONTWAIT);
   if( (result == -1) || (errno == EAGAIN))
    {
     
     snprintf(infoText,MAX_INFO_TEXT + 1, 
	                 "FORCED LOGOUT:%s: host %s: line %d: socket %d: %s %s\r\n", 
	                                            mClient[line].name, 
	                                            mClient[line].host, 
	                                            line, 
	                                            mClient[line].socket,
	                                            (result == -1)?"write returned - 1,":"",
	                                            (errno == EAGAIN)?"errno was EAGAIN":"");
    *(infoText + MAX_INFO_TEXT) = '\0';
    close(mClient[line].socket);			
    util_WriteLog(1,infoText);
    chat_Logout(line,KILL);
    errno = 0;
   } 
   
   
  //  log_after_send(line);
}




int comm_SendAll(int sender, char *msg) {
#define MAX_BUFFER 499

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

    if (sender==SYSMSG)	
	chars_printed = snprintf (buffer,MAX_BUFFER + 1, ">> %s",msg);
    else 
	chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[%d, %s]: %s",sender,mClient[sender].name,msg);
	
    if( (chars_printed == -1) || (chars_printed >= (MAX_BUFFER + 1)) )
                return -1;     /* buffer overflowed because msg was too long */
                
    *(buffer + MAX_BUFFER) = '\0';            
                
    if (sender==SYSMSG) {
	for (i=0;i<maxClients;i++)
	    if (mClient[i].online==YES)
		safe_write(i,buffer);
    } 
    else {
	for (i=0;i<maxClients;i++)
    	    if ((mClient[i].online==YES)&&(mClient[i].squelch[sender]==NO))
    	        safe_write(i,buffer);
    }
    return(0);
#undef MAX_BUFFER    
}

int comm_SendAllBut(int sender, int mask, char *msg) {
#define MAX_BUFFER 499

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


    if (sender==SYSMSG)	
	chars_printed = snprintf (buffer,MAX_BUFFER + 1, ">> %s",msg);
    else 
	chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[%d, %s]: %s",sender,mClient[sender].name,msg);
	
    if( (chars_printed == -1) || (chars_printed >= (MAX_BUFFER + 1)) )
                return -1;     /* buffer overflowed because msg was too long */
                
    *(buffer + MAX_BUFFER) = '\0';            
    
    if (sender==SYSMSG) {
	for (i=0;i<maxClients;i++)
	    if ((mClient[i].online==YES)&&(i!=mask))
	        safe_write(i,buffer);
    } 
    else {
	for (i=0;i<maxClients;i++)
    	    if ((mClient[i].online==YES)&&(mClient[i].squelch[sender]==NO)&&(i!=mask))
    	        safe_write(i,buffer);
    }
    return(0);

#undef MAX_BUFFER
}



int comm_SendChannel(int sender, int channel, char *msg) {
#define MAX_BUFFER 499

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

    if (sender==SYSMSG)	
	chars_printed = snprintf (buffer,MAX_BUFFER + 1, ">> %s",msg);
    else 
	if (channel!=1)
	    chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[%d, %s]: %s",sender,mClient[sender].name,msg);
	else
	    chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[???]: %s",msg);

    if( (chars_printed == -1) || (chars_printed >= (MAX_BUFFER + 1)))
                return -1;     /* buffer overflowed because msg was too long */
                
    *(buffer + MAX_BUFFER) = '\0';            


    if (sender==SYSMSG) {
	for (i=0;i<maxClients;i++)
	    if ((mClient[i].online==YES)&&(mClient[i].channel==channel))
	        safe_write(i,buffer);
    } 
    else {
	for (i=0;i<maxClients;i++)
    	    if ((mClient[i].online==YES)&&(mClient[i].squelch[sender]==NO)&&(mClient[i].channel==channel))
    	        safe_write(i,buffer);
    }
    return(0);
#undef MAX_BUFFER
}

int comm_SendChannelBut(int sender, int channel, int mask, char *msg) {
#define MAX_BUFFER 499

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

    if (sender==SYSMSG)	
	chars_printed = snprintf (buffer,MAX_BUFFER + 1, ">> %s",msg);
    else 
	if (channel!=1)
	    chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[%d, %s]: %s",sender,mClient[sender].name,msg);
	else
	    chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[???]: %s",msg);

    if( (chars_printed == -1) || (chars_printed >= (MAX_BUFFER + 1)) )
                return -1;     /* buffer overflowed because msg was too long */
    *(buffer + MAX_BUFFER) = '\0';            

    if (sender==SYSMSG) {
	for (i=0;i<maxClients;i++)
	    if ((mClient[i].online==YES)&&(mClient[i].channel==channel)&&(i!=mask))
	        safe_write(i,buffer);
    } 
    else {
	for (i=0;i<maxClients;i++)
    	    if ((mClient[i].online==YES)&&(mClient[i].squelch[sender]==NO)&&(mClient[i].channel==channel)&&(i!=mask))
    	        safe_write(i,buffer);
    }
    return(0);
#undef MAX_BUFFER
}

int comm_SendGroup(int sender, char *msg) {
#define MAX_BUFFER 499
    char buffer[MAX_BUFFER + 1];
    int chars_printed;
    int channel;
    int i;

    if (sender==SYSMSG)
	return(-1);

    channel=mClient[sender].channel;

    if (channel!=1)
        chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[%d, %s]: %s",sender,mClient[sender].name,msg);
    else
        chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[???]: %s",msg);
        
    if( (chars_printed == -1) || (chars_printed >=  (MAX_BUFFER + 1)) )
        return -1 ;    /* msg was too long */
    *(buffer + MAX_BUFFER) = '\0';
        
    for (i=0;i<maxClients;i++)
        if ((mClient[i].online==YES)&&(mClient[i].squelch[sender]==NO)&&(mClient[i].channel==channel))
            safe_write(i, buffer);

	    
    /*** MANHATTAN: if the sender is in his 'home' group, write the
    ***  message to the classroom log
    **/
    if(mClient[sender].group == mChannel[channel].group)
       util_WriteClassLog(sender, mClient[sender].name,msg,mChannel[mClient[sender].group].name);	    
	    
    return(0);
#undef MAX_BUFFER

}




int comm_SendGroupBut(int sender, int mask, char *msg) {
#define MAX_BUFFER 499

    int channel=mClient[mask].channel;
    char buffer[MAX_BUFFER + 1];
    int chars_printed;
    int i;

    if (sender==SYSMSG)	
	chars_printed = snprintf (buffer,MAX_BUFFER + 1, ">> %s",msg);
    else 
	if (channel!=1)
	    chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[%d, %s]: %s",sender,mClient[sender].name,msg);
	else
	    chars_printed = snprintf (buffer,MAX_BUFFER + 1, "[???]: %s",msg);

    if( (chars_printed == -1) || (chars_printed >= (MAX_BUFFER + 1)) )
          return -1;    /* msg was too long */
    *(buffer + MAX_BUFFER) = '\0';      

    if (sender==SYSMSG) {
	for (i=0;i<maxClients;i++)
	    if ((mClient[i].online==YES)&&(mClient[i].channel==channel)&&(i!=mask))
	        safe_write(i, buffer);
    } 
    else {
	for (i=0;i<maxClients;i++)
    	    if ((mClient[i].online==YES)&&(mClient[i].squelch[sender]==NO)&&(mClient[i].channel==channel)&&(i!=mask))
    	        safe_write(i, buffer);
    }
    return(0);
    
#undef MAX_BUFFER    
}

int comm_SendTo(int sender,int receiver,char *msg) {
#define MAX_BUFFER 499

    char buffer[MAX_BUFFER + 1];
    int chars_printed;
    
	
    if (sender==SYSMSG)	
	chars_printed = snprintf (buffer,MAX_BUFFER + 1, ">> %s",msg);
    else 
	chars_printed = snprintf (buffer,MAX_BUFFER + 1, MSG_WHISPER,sender,mClient[sender].name,msg);
	
    if((chars_printed == -1) || (chars_printed >= (MAX_BUFFER + 1)) )
         return -1;    /* msg was too long for buffer */
         
    *(buffer + MAX_BUFFER) = '\0';
         
    if (sender==SYSMSG) {
        if (mClient[receiver].socket!=0)
            safe_write(receiver, buffer);
    }
    else 
	if ((mClient[receiver].socket!=0)&&(mClient[receiver].squelch[sender]==NO))
	    safe_write(receiver, buffer);
    return(0);
#undef MAX_BUFFER    
}




int comm_SendPage(int sender,int receiver,char *msg) {

#define MAX_BUFFER 499
   char buffer[MAX_BUFFER + 1];
   int chars_printed;
   
      
   if (sender==SYSMSG)
       chars_printed = snprintf (buffer,MAX_BUFFER + 1, ">> %s",msg);
   else
       chars_printed = snprintf (buffer,MAX_BUFFER + 1, MSG_PAGE,sender,mClient[sender].name,msg);

   if( (chars_printed == -1) || (chars_printed >= (MAX_BUFFER + 1)))
       return - 1;   /* msg was too long */
   *(buffer + MAX_BUFFER) = '\0';
       
   if (sender==SYSMSG) {
      if (mClient[receiver].socket!=0)
         safe_write(receiver, buffer);
   }
  else
     if ((mClient[receiver].socket!=0)&&(mClient[receiver].squelch[sender]==NO))
          safe_write(receiver, buffer);
    return(0);
}
