/*********************************************************************************/
/*                                                                               */
/*  main.cpp                                                                     */
/*  bkenwright@xbdev.net                                                         */
/*  simple coding of irc chat client - tutorial code                             */
/*  Saying: "Code doens't have to be complex"                                    */
/*                                                                               */
/*********************************************************************************/

#include <windows.h>


// These few lines are compiler directives that include the windows librarys
// during linking.  You could instead inside visual studio goto Project->
// settings" menu and under the "Link" tab add the necessary librarys instead.
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "comctl32.lib")

#pragma comment(lib, "wsock32.lib")

#include <windows.h>
#include <stdio.h>

#include <string.h>


WSADATA ws;
int d;
char aa[5000];
char bb[5000];

SOCKET s; //unsigned int

int ii;

struct sockaddr_in a;

void abc(char *p)
{
	FILE *fp = fopen("s.txt", "a+");
	fprintf(fp, "%s\n", p);
	fclose(fp);
}

_stdcall WinMain(HINSTANCE i, HINSTANCE j, char*k, int l)
{
	//PART -1-
	d = WSAStartup( 0x101, &ws);
	sprintf(aa, "WSAStartup = %d", d);
	abc(aa);

	//PART -2-
	s = socket(AF_INET, SOCK_STREAM, 0); //AF_INET - Internet Protocol (IP)
	                                     //SOCK_STREAM - Stream Protocol (TCP in this case)
	sprintf(aa, "SOCKET = %d",s);
	abc(aa);

	//PART -3-
	a.sin_family = AF_INET;				// IP protocol
	a.sin_port = htons(6665);             // Port to connect to, 80 = HTTP Server

	// EFnet: EU, UK, London		efnet.demon.co.uk		6665-6669
	// EFnet: US, TX, Houston		ircd.lagged.org			6660-6669
	// EFnet: CA, QB, Montreal		irc.qeast.net			6667

	hostent *ff = gethostbyname("efnet.demon.co.uk");
	a.sin_addr = *((LPIN_ADDR)*ff->h_addr_list);

	//a.sin_addr.s_addr = inet_addr("202.54.1.18");
	
	d = connect(s, (struct sockaddr *)&a, sizeof(a));
	sprintf(aa, "CONNECT = %d",d);
	abc(aa);

	//PART -4-

	ii = recv(s, aa, 5000, 0);
	abc(aa);

	abc("Sending NICK << etc \n");

	strcpy(aa, "NICK bobyvodi\r\n");
	strcat(aa, "USER bobyvodi bobyvodi bobyvodi :bobyvodi\r\n");
	send(s, aa, strlen(aa), 0);

	strcpy(aa, "JOIN #xfactor\r\n");
	strcat(aa, "PRIVMSG #xfactor :Hi!\r\n");
	send(s, aa, strlen(aa), 0);
	abc("Receive Loop\n");

	ii = 1;
	while( ii != 0)
	{
		ii = recv(s, aa, 5000, 0);
		abc(aa);
	}

	/*
	//NOTE: Since irc is a pointer, we need to dereference it first.
	*irc << "NICK " << nick << std::endl;
	//NOTE: Normally you would not send 'nick' 3 times!
	*irc << "USER " << nick << " "
		<< nick << " " << nick << " :" << nick << '\n';

	std::cout << "|-> Joining channel " << channel << "..." << std::endl;

	//Join the Channel 'channel' ...
	*irc << "JOIN " << channel << std::endl;
	//and send 'Hi!' to it. 
	*irc << "PRIVMSG " << channel << " :Hi!" << std::endl;
	*/

	closesocket(s);
	WSACleanup();
}// End of main()



