/* -TAG:LICENSE-
   
   This file is under the DigitalSpark Freeware Class License, see the
   file LICENSE for complete licensing information.
                                                         
                                                         -TAG:LICENSE- */
#include <Threading.h>
#include <Strings.h>
#include "tricky.h"

extern	char	*NetGets( int sock, int end );
extern  size_t	Write( int slot, void *data, size_t size );
extern	void	UpdateSessionInfo( int Block );

int ServerFilter( char *ogdata )
{
char	data[1024];
char	outbuff[1024];
char	pongback[1024];
char	*buff = 0;
char	chanlist[1024];
char	chanbuff[1024];
char	temp[1024];
int	i=0,k=0,j=0;
char	*MesgType;
char	*MesgSource;
char	*MesgDest;
char	*MesgValue;

strncpy(data,ogdata,1023);

/* Strip away trailing newlines and returns */
if ( data[strlen(data)-1] == 0x0d )
   data[strlen(data)-1] = 0;
if ( data[strlen(data)-1] == 0x0a )
   data[strlen(data)-1] = 0;
if ( data[strlen(data)-1] == 0x0d )
   data[strlen(data)-1] = 0;

MesgSource = PullToken(data,' ',0);
MesgType = PullToken(data,' ',1);
MesgDest = PullToken(data,' ',2);
MesgValue = PullToken(data,' ',3);

//if ( strcmp("372",MesgType) != 0 )
//   Error(9,data);

if ( strcmp("001",MesgType) == 0 )
   {
   // Getting connected to the server, update some things
   buff = PullToken(data,' ',0);
   buff[0] = ' ';
   strcpy(IRCServer,SingleSpace(buff));
   strcpy(Nick,MesgDest);
   // Update the ID string
   UpdateSessionInfo(0);
   free(buff);
   }
if ( strcmp("376",MesgType) == 0 )
   {
   ServerAuth = 1;
   }

if ( strcmp("004",MesgType) == 0 )
   {
   strncpy(ZeroZeroFour,data,1020);
   i = strlen(ZeroZeroFour);
   ZeroZeroFour[i] = 0x0a;
   ZeroZeroFour[i+1] = 0x0a;
   ZeroZeroFour[i+2] = 0;
   }
if ( strcmp("005",MesgType) == 0 )
   {
   strncpy(ZeroZeroFive,data,1020);
   i = strlen(ZeroZeroFive);
   ZeroZeroFive[i] = 0x0a;
   ZeroZeroFive[i+1] = 0x0a;
   ZeroZeroFive[i+2] = 0;
   }

if ( ClientConnected == 1 )
   {
   if ( strcmp(Nick,MesgValue) == 0 )
      {
      /* Something on me */
      if ( strcmp("302",MesgType) == 0 )
         {
         /* nick to hostmask info */
         if ( RefreshWhois == 0 )
            return -1;
         }
      if ( strcmp("311",MesgType) == 0 )
         {
         /* whois client info, update local ID */
         sprintf(IDString,"%s!%s@%s",MesgValue,PullToken(data,' ',4),PullToken(data,' ',5));
         //Error(9,"IDString = %s",IDString);
         if ( RefreshWhois == 0 )
            return -1;
         }
      if ( strcmp("312",MesgType) == 0 )
         {
         /* server info */
         if ( RefreshWhois == 0 )
            return -1;
         }
      if ( strcmp("317",MesgType) == 0 )
         {
         /* time info */
         if ( RefreshWhois == 0 )
            return -1;
         }
      if ( strcmp("221",MesgType) == 0 )
         {
         /* something info */
         if ( RefreshWhois == 0 )
            return -1;
         }
      if ( strcmp("319",MesgType) == 0 )
         {
         /* whois channel list, update channel list */
         buff = malloc(strlen(data)+1);
         strncpy(buff,data,1023);
         i++;
         while ( buff[i] != ':' )
            i++;
         i++;
         while ( buff[i] != 0 )
            {
            chanlist[k] = buff[i];
            i++;
            k++;
            }
         chanlist[k] = 0;
         //Error(9,"Channlist = [%s]",chanlist);
         i=0;
         k=0;
         while ( chanlist[i] != 0 )
            {
            if (( chanlist[i] == '@' ) || ( chanlist[i] == '+' ))
               i++;
            chanbuff[k] = chanlist[i];
            if (( chanlist[i] == ' ' ) || ( chanlist[i+1] == 0 ))
               {
               chanbuff[k] = 0;
               if ( Channels[j] != 0 )
                  free(Channels[j]);
               Channels[j] = malloc(strlen(chanbuff)+1);
               strcpy(Channels[j],chanbuff);
               //Error(0,chanbuff);
               j++;
               k=-1;
               }
            if ( chanlist[i+1] == 0 )
               break;
            k++;
            i++;
            }
         if ( RefreshWhois == 0 )
            return -1;
         }
      if ( strcmp("318",MesgType) == 0 )
         {
         /* End of whois */
         if ( RefreshWhois == 0 )
            {
            RefreshWhois=1;
            return -1;
            }
         }
      }
   }


if ( strcmp("PONG",MesgType) == 0 )
   {
   /* PONG */
   return -1;
   }

if ( strcmp("PING",MesgSource) == 0 )
   {
   /* respond to PING */
   strncpy(pongback,MesgType,1023);
   sprintf(outbuff,"PONG %s\r\n",pongback);
   Write(ServerSock,outbuff,0);
   //Error(0,"PONG");
   return -1;
   }

// Check for stuff directed at me
sprintf(temp,":%s",IDString);
if ( strcmp(temp, MesgSource) == 0 )
   {
   // Client change nick
   if ( strcmp("NICK",MesgType) == 0 )
      {
      strcpy(temp,MesgDest);
      for( i=1; temp[i] != 0; i++ )
         temp[i-1] = temp[i];
      temp[i-1] = 0;
      strcpy(Nick,temp);
      Error(0,"Nick = [%s]",Nick);
      }
   }

if ( MesgSource != 0 )
   free(MesgSource);
if ( MesgType != 0 )
   free(MesgType);
if ( MesgDest != 0 )
   free(MesgDest);
if ( MesgValue != 0 )
   free(MesgValue); 

return 0;
}
