/* -TAG:LICENSE-
   
   This file is under the DigitalSpark Freeware Class License, see the
   file LICENSE for complete licensing information.
                                                         
                                                         -TAG:LICENSE- */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> 
#include <netdb.h>
#include <Threading.h>
#include <Strings.h>
#include "tricky.h"

#define SA struct sockaddr

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

extern	char    *connectbuf[1024];
extern	int     ConBufSize;

/* thanks sno for the networking help :) */

void *IRCServerManager( void )
{
struct sockaddr_in SrvAddr;
int	SrvPort;
char	*errpos;
char	ServerAddress[1024]; 
struct	hostent	*hostn; 
int	status=0;
char	outbuff[1024];
char	ServerName[1024];
char	ServerPort[1024];
int	i=0;

ServerName[0] = 0;

while ( 1 )
{
/* Check to see if the server is connected, if no, connect */
while (( ServerConnected == 0 ) && ( Authorized == 1 ))
   {
   ZeroZeroFour[0] = 0;    // Server options
   ZeroZeroFive[0] = 0;    // Special server commands list

   /* Get the next server values */
   strncpy(ServerPort,GetConfigList("Servers",1,ServerName),1023);
   strncpy(ServerName,GetConfigList("Servers",0,ServerName),1023);

   if (( ServerName[0] == 0 ) || ( ServerPort[0] == 0 ))
      {
      Error(1,"Invalid Server line in config file! (%s:%s)",ServerName,ServerPort);
      sleep(1);
      break;
      }

   sprintf(outbuff,"NOTICE AUTH :*** [TrickyIRC] Connecting to %s:%s ...\r\n",ServerName,ServerPort);
   Error(0,"Server %s:%s connecting ...",ServerName,ServerPort);
   cBusBroadcast(outbuff,0);

   /* Connect */
   SrvPort = htons(strtol(ServerPort,&errpos,0));

   hostn = gethostbyname(ServerName);
   if ( hostn == NULL )
      {
      cBusBroadcast("NOTICE AUTH :*** [TrickyIRC] Error; Couldn't resolve adddress\r\n",0);
      Error(1,"Server could not be resolved.");
      break;
      }
   strcpy(ServerAddress,addr2ascii(PF_INET,hostn->h_addr,sizeof(hostn->h_addr),NULL));
   memset((char *)&SrvAddr,0,sizeof(SrvAddr)); 

   SrvAddr.sin_family = PF_INET;
   SrvAddr.sin_port   = SrvPort;
   SrvAddr.sin_addr.s_addr = inet_addr(ServerAddress);

   ServerSock = socket(PF_INET,SOCK_STREAM,0);
   
   status = connect(ServerSock,(struct sockaddr *)&SrvAddr,sizeof(SrvAddr)); 

   if ( status == -1 )
      {
      close(ServerSock);
      cBusBroadcast("NOTICE AUTH :*** [TrickyIRC] Error; Couldn't connect\r\n",0);
      Error(0,"Server couldn't be reached");
      break;
      }
   Error(0,"Server connected");
   ServerConnected = 1;
   cBusBroadcast("NOTICE AUTH :*** [TrickyIRC] Connected\r\n",0);
   i=0;
   while ( i < ConBufSize )
      {
      //if ( strcmp("NICK",PullToken(connectbuf[i],' ',0)) == 0 )
      //   {
      //   strncpy(Nick,PullToken(connectbuf[i],' ',1),29);
      //   }
      //Error(0,"Sending [%s]",connectbuf[i]);
      sprintf(outbuff,"%s\r\n",connectbuf[i]);
      Write(ServerSock,outbuff,0);
      //free(connectbuf[i]);
      i++;
      }
   break;
   }
sleep(1);

}

return 0;
}
