#include <iostream>
#include <netdb.h>
#include <sys/socket.h>
#include <cstring>
#include <stdlib.h>

using namespace std;

#define PORT 80
#define MAXDATASIZE 90000

char wynik[4];

char* urlencode(char d)
{
	char *t = "0123456789abcdef";
	int h;
	h=d;
	if( 'a' <= h && h <= 'z' || 'A' <= h && h <= 'Z' || '0' <= h && h <= '9' || h == '-' || h == '_' || h == '.' )
	{
		wynik[0]=h;
		wynik[1]='\0';
	}
	else if( h == ' ' )
	{
		wynik[0]='+';
		wynik[1]='\0';
	}
	else 
	{
		wynik[0]='%';
		wynik[1]=(t[h >> 4]);
		wynik[2]=(t[h & 0x0f]);
		wynik[3]='\0';
	}
	return wynik;
}

int main()
{
	int sockfd, numbytes;
	char buf[MAXDATASIZE];
	char polecenie[10000];
	char prefix;
	char *bufor;
	char *podciag;
	char odpowiedz[MAXDATASIZE];
	int suma=39;
	int czas;
	char numer[9];
	char nadawca[15];
	char tresc[260];
	struct hostent *he;
	struct sockaddr_in their_addr;
	cout << "Podaj treść: " << endl;
	cin.getline(tresc, sizeof(tresc));
	cout << "Podaj numer odbiorcy: ";
	cin >> numer;
	cout << "Podaj nadawcę: ";
	cin >> nadawca;
	if((he=gethostbyname("www.text.plusgsm.pl"))==NULL) perror("gethostbyname");
	if((sockfd=socket(AF_INET, SOCK_STREAM, 0)) == -1) perror("socket");
	their_addr.sin_family=AF_INET;
	their_addr.sin_port=htons(PORT);
	their_addr.sin_addr=*((struct in_addr *)he->h_addr);
	memset(&(their_addr.sin_zero), '\0', 8);
	if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) perror("connect");
	strcpy(polecenie, "POST /sms/sendsms.php HTTP/1.1\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "Host: www.text.plusgsm.pl\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "User-Agent: Mozilla/5.0\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "Accept-Language: pl,en-us;q=0.7,en;q=0.3\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "Accept-Encoding: gzip,deflate\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "Keep-Alive: 300\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "Connection: keep-alive\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "Referer: http://www.text.plusgsm.pl/sms/\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "Content-Type: application/x-www-form-urlencoded\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	suma+=strlen(nadawca);
	for(int i=0; i<strlen(tresc); i++)
	{
		bufor=urlencode(tresc[i]);
		suma+=strlen(bufor);
	}
	strcpy(polecenie, "Content-Length: ");
	send(sockfd, polecenie, strlen(polecenie), 0);
	sprintf(polecenie,"%d",suma);
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "\r\n\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "tprefix=");
	send(sockfd, polecenie, strlen(polecenie), 0);
	for(int i=0; i<3; i++) polecenie[i]=numer[i];
	polecenie[3]='\0';
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "&numer=");
	send(sockfd, polecenie, strlen(polecenie), 0);
	for(int i=3; i<9; i++) polecenie[i-3]=numer[i];
	polecenie[6]='\0';
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "&odkogo=");
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, nadawca);
	send(sockfd, polecenie, strlen(polecenie), 0);
	strcpy(polecenie, "&tekst=");
	send(sockfd, polecenie, strlen(polecenie), 0);
	for(int i=0; i<strlen(tresc); i++)
	{
		bufor=urlencode(tresc[i]);
		send(sockfd, bufor, strlen(bufor), 0);
	}
	strcpy(polecenie, "\r\n");
	send(sockfd, polecenie, strlen(polecenie), 0);
	numbytes=recv(sockfd, odpowiedz, MAXDATASIZE-1, 0);
	odpowiedz[numbytes]='\0';
	cout << "Przekazano żądanie SMS do operatora. Serwer zwrócił:" << endl;
	podciag=strstr(odpowiedz, "<title>");
	for(int i=7; podciag[i]!='<'; i++) cout << podciag[i];
	cout << endl;
	return 0;
}

