///////////////////////////////////////////////////////////////////////////////////////
// BiffSocko
// biffclient.cpp
//
// PLEASE SEE THE README
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT 6667
#define MAXDATASIZE 1024  

/////////////////////////////////
// sockclient class
/////////////////////////////////
class sockclient{
    public:
        int opensock(char*);
};

/////////////////////////////////
// create a socket and connect to
// the server
/////////////////////////////////
int sockclient::opensock(char *host){
        int sockfd;
        struct hostent *he;
        struct sockaddr_in their_addr; // connector's address information

        if ((he=gethostbyname(host)) == NULL) {  // get the host info
            perror("gethostbyname");
            exit(1);
        }

        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        their_addr.sin_family = AF_INET;    // host byte order
        their_addr.sin_port = htons(PORT);  // short, network byte order
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        memset(&(their_addr.sin_zero), '\0', 8);  // zero the rest of the struct

        if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
            perror("connect");
            exit(1);
        }

	return(sockfd);
}

/////////////////////////////
// driver program
/////////////////////////////
int main(int argc, char *argv[])
{
	sockclient sock1;                                  // socketclient object
	int descriptor;                                    // int for socket descriptor 
	char buf[MAXDATASIZE];                             // char array to read in from the socket stream
	char usrstring[MAXDATASIZE];                       // char array containing user information
	char msgstring[MAXDATASIZE];                       // char array containing message information
	FILE *sockio;                                      // file stream to read and write to the socket
	char *seperator = ":";                             // seperator for strtok
        char *tokenptr;                                    // strtok pointer for string1 


	//if (argc != 2) {
	 //   fprintf(stderr,"usage: client hostname\n");
	  //  exit(1);
	//}

	descriptor = sock1.opensock("newyork.ny.us.undernet.org");

	if(!(sockio = fdopen(descriptor, "r+"))){
		perror("cant open sockio stream file\n");
		exit(1);
	}

	fgets(buf,MAXDATASIZE-1,sockio);
	printf("%s",buf);

	fgets(buf,MAXDATASIZE-1,sockio);
	printf("%s",buf);

	fgets(buf,MAXDATASIZE-1,sockio);
	printf("%s",buf);

	fgets(buf,MAXDATASIZE-1,sockio);
	printf("%s",buf);

	fprintf(sockio, "NICK BiffyBoo\n");
	printf("NICK BiffyBoo\n");

	fgets(buf,MAXDATASIZE-1,sockio);
	if((strcmp(buf,"PING")) > 0){
                buf[1]='O';
                fprintf(sockio, "%s\n",buf);
                printf("%s\n",buf);
		fprintf(sockio,"USER biffyboo localhost localhost :no nick\n");
		printf("USER $nick localhost localhost :no nick\n");
       }

	
	while(fgets(buf,MAXDATASIZE-1,sockio)){
		printf("%s",buf);
		fprintf(sockio, "join #bifftest\n");

		// break up the irc  string
		if(buf[0] = ':'){
			tokenptr = strtok(buf,seperator);
			sprintf(usrstring,"%s",tokenptr);
			printf("usrstring = %s\n",usrstring);
			tokenptr = strtok(NULL,seperator);
			sprintf(msgstring,"%s",tokenptr);
			printf("msgstring = %s\n",msgstring);
		}

			
		if((strcmp(buf,"PING")) > 0){
			buf[1]='O';
			fprintf(sockio, "%s\n",buf);
			printf("%s\n",buf);
		}


		memset(buf,0,0);
	}

	fclose(sockio);
	close(descriptor);

	return 0;
}