/*
 * warning.c
 */
#include "copyright.h"

#include <stdio.h>
#include <math.h>
#include <signal.h>
#include <varargs.h>
#include "defs.h"
#include "struct.h"
#include "data.h"
#include "packets.h"


/* 
  All warning messages should use new_warning() from now on. The index
  is a define found in defs.h that is specific to a particular message. If
  the warning doesn't have a define then use UNDEF.
*/

/*VARARGS*/
new_warning(index,va_alist)
int index;
va_dcl
{
	va_list args;
	char *fmt;
	char temp[150];

	va_start(args);

	fmt = va_arg(args, char *);
        vsprintf(temp,fmt,args);

	va_end(args);

#ifdef SHORT_PACKETS
	if (index == UNDEF) {
	    warning(temp);
	}else {
	    spwarning(temp,index);
	}
#else
	warning(temp);
#endif
#ifdef CHECKMESG
      if (eventlog) {
         char from_str[9]="WRN->\0\0\0";
         strcat(from_str,me->p_mapchars);
         pmessage2(0, 0, from_str, me->p_no, temp);
      }
#endif
}


/*
** The warning in text will be printed in the warning window.
** The message will last WARNTIME/10 seconds unless another message
** comes through and overwrites it.
*/

#ifdef SHORT_PACKETS
extern int sizes[];

warning(text)
char *text;
{
    struct warning_spacket warn;

        if(!send_short){
                warn.type=SP_WARNING;
                STRNCPY(warn.mesg, text, MSG_LEN);
                warn.mesg[MSG_LEN-1]='\0';
                sendClientPacket((CVOID) &warn);
        }
        else {  /* send it like a CP_S_MESSAGE */
                int size;
                STRNCPY(warn.mesg, text, MSG_LEN);
                warn.mesg[MSG_LEN-1]='\0';
                size = strlen(warn.mesg);
                size += 5; /* 1 for '\0', 4 packetheader */
                if((size % 4) != 0)
                	size += (4 - (size % 4));
                warn.pad3 = (char) size;
                sizes[SP_S_WARNING] = size;
                warn.type = SP_S_WARNING;
                warn.pad1 = SHORT_WARNING;
                warn.pad2 = 0;
                sendClientPacket((CVOID) &warn);
                sizes[SP_S_WARNING] = sizeof(struct warning_s_spacket); /* Back to default */
        }
}


/* This is the interface to the new warning scheme. If send_short = 1 all
    warnings are send with SP_S_WARNING */

spwarning(text, index)
char *text;
int index;      /* The index into the warnings array in client */
{
    struct warning_spacket warn;
    struct warning_s_spacket swarn;

        if(!send_short) {
                warn.type=SP_WARNING;
                STRNCPY(warn.mesg, text, MSG_LEN);
                warn.mesg[MSG_LEN-1]='\0';
                sendClientPacket((CVOID) &warn);
        }
        else {  /* These are only 'TEXTE' warnings */
        swarn.type = SP_S_WARNING;
        swarn.whichmessage = TEXTE;
        swarn.argument = (u_char)index & 0xff;
        swarn.argument2 = ((u_char) index >> 8) & 0xff;
        sendClientPacket((CVOID) &swarn);
        }
}

/* This function is for new warnings that are not compiled in the client */
s_warning(text, index)
char *text;
int index;      /* The index into the warnings array in client */
{
struct warning_spacket warn;
struct warning_s_spacket swarn;
int size;
static char sent_to_client[256]; /* This is a (sortof) boolean arry to remind the server that he has sent */
                          /* a warning to the client */

        if(!send_short) {
                warn.type=SP_WARNING;
                STRNCPY(warn.mesg, text, MSG_LEN);
                warn.mesg[MSG_LEN-1]='\0';
                sendClientPacket((CVOID) &warn);
        }
        else if (sent_to_client[index]){ /* The client has that warning */
                swarn.type = SP_S_WARNING;
                swarn.whichmessage = STEXTE;
                swarn.argument = (u_char) index;
                swarn.argument2 = 0;
                sendClientPacket((CVOID) &swarn);
        }
        else  { /* first time */
                STRNCPY(warn.mesg, text, MSG_LEN);
                warn.mesg[MSG_LEN-1]='\0';
                size = strlen(warn.mesg);
                size += 5; /* 1 for '\0', 4 packetheader */
                if((size % 4) != 0)
                    size += (4 - (size % 4));
                warn.pad3 = (char) size;
                sizes[SP_S_WARNING] = size;
                warn.type = SP_S_WARNING;
                warn.pad1 = STEXTE_STRING;
                warn.pad2 = (u_char)index; /* This is for future reference */
                sent_to_client[index] = 1;
                sendClientPacket((CVOID) &warn);
                sizes[SP_S_WARNING] = sizeof(struct warning_s_spacket); /* Back to default */
        }
}

swarning(whichmessage, argument,argument2)
u_char whichmessage,argument,argument2;
{
        struct warning_s_spacket swarn;
        swarn.type = SP_S_WARNING;
        swarn.whichmessage = whichmessage;
        swarn.argument = argument;
        swarn.argument2 = argument2;
        sendClientPacket((CVOID) &swarn);
}


#else	/* SHORT_PACKETS */

warning(text)
char *text;
{
    struct warning_spacket warn;

    warn.type=SP_WARNING;
    STRNCPY(warn.mesg, text, MSG_LEN);
    warn.mesg[MSG_LEN-1]='\0';
    sendClientPacket((CVOID) &warn);
}
#endif	/* SHORT_PACKETS */

