//////////////////////////////auth : kernel/////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/////////////////////////////mail : x86kernel@gmail.com/////////////////////
/////////////////////////////nateon : x86kernel@nate.com////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <cstring>
#include "main.h"

using namespace std;

//#pragma comment(lib, "ws2_32.lib") /// 이거 나중에 컴파일할때 저 라이브러리 추가해줘야됨 ... 안그러면 배포가 안됨

int main(void)
{
	int state = 0,no,state2,closestate = 0;

//	char urlData[MAX_Html_Value];
	char *urlData;

	SOCKET URL;

	state = urlconnect(URL,wsaData);

	if(state == SOCKET_FUNC)
	{
		cout << "in urlconnect() socket() function" << endl;
	}
	else if(state == CONNECT_FUNC)
	{
		cout << "in urlconnect() connect() function" << endl;
	}

	urlData = (char *)malloc(sizeof(char) * Buffer_Size);
	memset(urlData,0,sizeof(urlData));

while(1)
{

	if(closestate == 1)
	{
		urlconnect(URL,wsaData);
		closestate = 0;
	}

	state = getHtml(URL,urlData);

	if(state == SEND_FUNC)
	{
		cout << "in getHtml() send() function" << endl;

		return 0;
	}
	else if(state == RECV_FUNC)
	{
		cout << "in getHtml() recv() function" << endl;

		return 0;
	}
	
	state2 = Handling(ParseHtmlGetNo(urlData));

	if(state2 == STRSTR_FUNC)
	{
		WSACleanup();
		Sleep(HandlingMSc);
		closestate = 1;
	}
	else if(state2 == 0)
	{
		WSACleanup();
		Sleep(HandlingMSc);
		closestate = 1;
	}
		
}
	cout << "Board No : " << no << endl;

	return 0;

}

int urlconnect(SOCKET &s, WSADATA &Wsa)
{

	WSAStartup(WINSOCK_VERSION,&Wsa);

	s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	if(S == INVALID_SOCKET)
		return SOCKET_FUNC;

	lphost = gethostbyname("hackerschool.org");

	dest.sin_family = AF_INET;
	dest.sin_addr.s_addr = ((struct in_addr *)(lphost->h_addr))->s_addr;
	dest.sin_port = htons(80);

	if(connect(s,(sockaddr *)&dest, sizeof(dest)) == SOCKET_ERROR)
		return CONNECT_FUNC;

	return 0;
}

int getHtml(SOCKET &s, char *URL_VALUE)
{

	int i = 1;
	int Test;
	int sendsize,recvsize;
		
	char urlData[MAX_Recv_Size],GetHeader[MAX_Get_Value],Buffer[Buffer_Size];

	FILE *fp;
		
	fp = fopen("C:\\Html.txt","w+r");

//	char *urlData,*GetHeader,*DUMMY;
	//char *GetHeader,*DUMMY;

//	GetHeader = (char *)malloc(sizeof(char) * MAX_Get_Value);
//	urlData = (char *)malloc(sizeof(char) * MAX_Recv_Size);
//	DUMMY = (char *)malloc(sizeof(char) * MAX_Recv_Size);

	memset(urlData,0,MAX_Recv_Size);
	memset(GetHeader,0,MAX_Get_Value);

	sprintf(GetHeader,"GET /HS_Boards/zboard.php?id=Free_Board&category=?groups=Board HTTP/1.1\r\nHost: hackerschool.org\r\nUser-Agent: IE-8\r\nUA-CPU: x86\r\nAccept-Language: ko\r\nConnection: Keep-Alive\r\n\r\n");

	sendsize = send(s,GetHeader,strlen(GetHeader),0);

	if(sendsize == -1)
	{
		cout <<  WSAGetLastError() << endl;
		return SEND_FUNC;
	}

	cout << "sendsize : " << sendsize << endl;

	while(recvsize != 0)
	{

	recvsize = recv(s,urlData,sizeof(urlData),0); 

	if(recvsize == -1)
	{ 
		return RECV_FUNC;
	}

	fprintf(fp,"%s",urlData); //////////// 쓰기까지는 잘됨.
		
	memset(urlData,0,MAX_Recv_Size);

	}
	
	fseek(fp, 0, SEEK_SET);

	Test = fread(Buffer,1,sizeof(Buffer),fp); ///////// 쓴걸 다시 읽어서,

	cout << "Read Size : " << Test << endl;

	//cout << Buffer << endl;

	strcpy(URL_VALUE, Buffer); /////////// 읽은거 복사해서 넘김,

//	recv(s,DUMMY,MAX_Html_Value,0);

//	fprintf(fp2,DUMMY);

//	fprintf(fp,urlData);

	memset(urlData,0,MAX_Recv_Size);
	memset(GetHeader,0,MAX_Get_Value);
//	memset(DUMMY,0,MAX_Html_Value);

//	free(DUMMY);
//	free(GetHeader);
//	free(urlData);
	
	return 0;
}

int ParseHtmlGetNo(char *URL_VALUE)
{
	char *NowPwd, *BoardNo,Temp[20];

	//FILE *test = fopen("COPYCOM.txt","w");  // Test Object

//	fprintf(test,"%s",URL_VALUE);

//	FILE *fp = fopen("strstr.txt","w"); // Test Object 2

	int	no;

	NowPwd = strstr(URL_VALUE,"<span>");

	if(NowPwd == NULL)
	{
	//	cout << "first strstr() func" << endl;
	//	fprintf(fp,URL_VALUE); // Test Func 
		return STRSTR_FUNC;
	}

	BoardNo = strstr(NowPwd,"</span>");

	if(BoardNo == NULL)
	{
		cout << "second strstr() func" << endl;
		return STRSTR_FUNC;
	}

	memset(BoardNo,0,sizeof(BoardNo));

	BoardNo = strstr(NowPwd,"&no=");

	if(BoardNo == NULL)
	{
		cout << "third strstr() func" << endl;
		return STRSTR_FUNC;
	}

	NowPwd = strstr(BoardNo,"\"");

	if(NowPwd == NULL)
	{
		cout << "fourred strstr() func" << endl;
		return STRSTR_FUNC;
	}

	memset(NowPwd,0,sizeof(NowPwd));

	NowPwd = strstr(BoardNo,"&no=");

	strcpy(Temp,NowPwd+4);

	cout << "Getting Number : " << Temp << endl;

	no = atoi(Temp);
	
	return no;
}

int Handling(int BoardNo)
{
	static int NowBoardNo = 0, newBoardNo = 0;

	if(BoardNo == STRSTR_FUNC)
	{
		cout << "RETRY!!~ WAIT 30 SEC" << endl;
		return STRSTR_FUNC;
	}

	if(NowBoardNo == 0)
	{
		NowBoardNo = BoardNo;
		cout << "NowBoardNo : " << NowBoardNo << " ,NewBoardNo : " << newBoardNo << endl;
		return 0;
	}
	else if((NowBoardNo != 0) && (newBoardNo == 0))
	{
		newBoardNo = BoardNo;

		if(NowBoardNo < newBoardNo)
		{
			cout << "NowBoardNo : " << NowBoardNo << " ,NewBoardNo : " << newBoardNo << endl;
			MessageBox(NULL,"세 게시글 생김","kernel",MB_OK);
			NowBoardNo = newBoardNo;
		}
		cout << "NowBoardNo : " << NowBoardNo << " ,NewBoardNo : " << newBoardNo << endl;

		return 0;
	}
	else if((NowBoardNo != 0) && (newBoardNo != 0))
	{
		newBoardNo = BoardNo;

		if(NowBoardNo < newBoardNo)
		{
			cout << "NowBoardNo : " << NowBoardNo << " ,NewBoardNo : " << newBoardNo << endl;
			MessageBox(NULL,"세 게시글 생김","kernel",MB_OK);
			NowBoardNo = newBoardNo;

			return 0;
		}
		else if(NowBoardNo == newBoardNo)
		{
			cout << "NowBoardNo : " << NowBoardNo << " ,NewBoardNo : " << newBoardNo << endl;
			return 0;
		}
		else if(NowBoardNo > newBoardNo)
			return 0;
	}

	return 1;
}