#include "EMail.h"

MailClass::MailClass()
{
}

void MailClass::Wyslij(const char *user, const char *pass,const char *serwer,const char *smail,
					   const char *rmail,const char *fuser,const char *topic,const char *msg,bool log)
{
	char daneOdebrane[1024] = {'\0'};
	char daneWysylane[1024] = {'\0'};
	CString encode,desc;
    CFileDialog * fileDialog = NULL;
	Base64 base64;

	if(log) fileDialog = new CFileDialog(false,L"txt",L"log.txt");

	if(!ZainicjujWinsock(serwer)) return;

	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	sprintf(daneWysylane,"EHLO [%s]\r\n",serwer);
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneOdebrane,1024);
	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	ZeroMemory(daneWysylane,1024);
	sprintf(daneWysylane,"AUTH LOGIN\r\n");
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneOdebrane,1024);
	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	encode = base64.base64_encode((const unsigned char*)user,strlen(user));
	ZeroMemory(daneWysylane,1024);
	wcstombs(daneWysylane,(LPWSTR)encode.GetBuffer(),1024);
	sprintf(daneWysylane,"%s\r\n", daneWysylane);
	//wysyłamy zakodowaną nazwe użytkownika
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneOdebrane,1024);
	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	encode = base64.base64_encode((const unsigned char*)pass,strlen(pass));
	ZeroMemory(daneWysylane,1024);
	wcstombs(daneWysylane,(LPWSTR)encode.GetBuffer(),1024);
	sprintf(daneWysylane,"%s\r\n",daneWysylane );
	//wysyłamy zakodowane hasło
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneOdebrane,1024);
	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	ZeroMemory(daneWysylane,1024);
	sprintf(daneWysylane,"MAIL FROM:<%s>\r\n",smail);
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneOdebrane,1024);
	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	ZeroMemory(daneWysylane,1024);
	sprintf(daneWysylane,"RCPT TO:<%s>\r\n",rmail);
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneOdebrane,1024);
	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	ZeroMemory(daneWysylane,1024);
	sprintf(daneWysylane,"DATA\r\n");
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneOdebrane,1024);
	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	ZeroMemory(daneWysylane,1024);
	sprintf(daneWysylane,"From: %s <%s>\r\n",fuser,smail);
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneWysylane,1024);
	sprintf(daneWysylane,"To: <%s>\r\n",rmail);
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneWysylane,1024);
	sprintf(daneWysylane,"Subject: %s\r\n",topic);
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneWysylane,1024);
	sprintf(daneWysylane,"%s%s",msg,"\r\n.\r\n");
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneOdebrane,1024);
	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	ZeroMemory(daneWysylane,1024);
	sprintf(daneWysylane,"QUIT\r\n");
	send(mojSocket,daneWysylane,strlen(daneWysylane),0);
	desc.AppendFormat(L"Klient: %s",daneWysylane);

	ZeroMemory(daneOdebrane,1024);
	recv(mojSocket,daneOdebrane,1024,0);
	desc.AppendFormat(L"Serwer: %s",daneOdebrane);

	closesocket(mojSocket);

	if(log)
	{
		if(fileDialog->DoModal() == IDOK)
		{
			CString h = fileDialog->GetFileName();
			CFile file; 
			file.Open(fileDialog->GetFileName(), CFile::modeCreate |
				CFile::modeWrite | CFile::shareExclusive);
			file.Write(desc,sizeof(wchar_t)*wcslen(desc));
			file.Close();			
		}
	}
}

bool MailClass::ZainicjujWinsock(const char * serwer)
{
	WSADATA wsaData;
	WORD wersja = MAKEWORD( 2, 0 );
	int blad = WSAStartup(wersja, &wsaData );
	struct hostent *host;
	struct sockaddr_in sin;
	memset( &sin, 0, sizeof(sin));
	//pobierz adres serwera pocztowego
	host=gethostbyname(serwer);
	//zainicjuj socket
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = ((struct in_addr *)(host->h_addr))->s_addr;
	sin.sin_port = htons(25); //port smtp-25
	mojSocket = socket( AF_INET, SOCK_STREAM, 0 );
	//polacz sie z serwerem
	if(connect(mojSocket,(struct sockaddr *) &sin, sizeof(sin)))
	{
		return false;
	}
	return true;
}

