/* -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  size_t	Write( int slot, void *data, size_t size );
extern	void	ResumeIRCState( void );
extern	int	Command( char *command );

int ClientFilter( char *ordata )
{
char	UserT[1024];
char	data[1024];
// Switching to pointers to eliminate memory issues
// and to have sane multithreading
char	*MesgType;
char	*MesgValue;

strncpy(data,ordata,1023);

//Error(9,data);

// Note how these aren't the same fields as in serverfilter.c
MesgType = PullToken(data,' ',0);
MesgValue = PullToken(data,' ',1);

/* 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;

if ( Authorized == 1 )
   {
   if ( strcmp("COMMAND",MesgType) == 0 )
      {
      return Command(data);
      }
   }

if ( strcmp("QUIT",MesgType) == 0 )
   {
   //Error(0,"Client QUIT supressed");
   return -1;
   }

if ( strcmp("USER",MesgType) == 0 )
   {
   // This needs to be cleaned up
   strncpy(UserT,data,49);
   UserT[0] = ' ';
   UserT[1] = ' ';
   UserT[2] = ' ';
   UserT[3] = ' ';
   strcpy(User,SingleSpace(UserT));
   //Error(1,User);
   }


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


if ( ServerConnected == 0 )
   return -1;


return 0;
}
