/* by pmsac@toxyn.org, 1999, GPLed */
/* little-endian... */

#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define QUOTES 9
char *quotes[QUOTES] = {
"Viva Zapata!",
"Fa&ccedil;a uma pausa... Kit-Kat",
"25 de Abril, sempre!",
"Fascismo nunca mais!",
"Don't hesitate, fornicate.",
"Internet - Liberdade de express&atilde;o",
"Don't secure Hash, Smoke it",
"When I grow up I wannabe a PTnet Coder",
"Free the Source Luke<A HREF=\"http://gsm.toxyn.org/ptnot.c\">.</A>"
};

#define MAXSAVEDIPS 256
u_long savedIPs[MAXSAVEDIPS];


struct {
	char *text;
	u_long mask;
#define MAXFILTERS 11
} ipFilters[MAXFILTERS] = {
	{"EUnet", 0x7EC1},
	{"FCCN/RCCN", 0x88C1},
	{"FCCN/RCCN", 0x89C1},
	{"Comnexo", 0x26C2},
	{"Telepac", 0x41C2},
	{"Esoterica", 0x16C3},
	{"IP-Global", 0x17C3},
	{"Global-One", 0x3DC3},
	{"Teleweb", 0x10D4},
	{"ITnet", 0x36D4},
};

char *randomQuote(void) {
int i;

	i = time(NULL) % QUOTES;
	return quotes[i];
}

void printHeader(const char *title) {
	printf("Content-Type: text/html\n\n"
	       "<HTML>\n<HEAD>\n<TITLE>%s</TITLE>\n</HEAD>\n"
	       "<BODY BGCOLOR=\"#000000\" TEXT=\"#FFFFFF\">\n"
	       "<H1 ALIGN=\"CENTER\">%s</H1>\n<HR>\n", title, title);
}

void printForm(void) {
	printf("<FORM METHOD=POST ACTION=\""
	       "http://gsm.toxyn.org/cgi-bin/ptnot.cgi"
	       "\">\n127."
	       "<INPUT NAME=\"\" MAXLENGTH=\"11\" SIZE=\"11\" VALUE=\"\">\n"
	       "<INPUT TYPE=\"SUBMIT\" VALUE=\"Translate\">\n");

}

void printFilter(u_long fakeIP, int saved) {
int i, j, count;

	fakeIP |= 0x0000007F;
	count = 0;
	for (i = 0; i < saved; i++)
		for (j = 0; j < MAXFILTERS; j++)
			if (((savedIPs[i] & 0x0000FFFF) ^ ipFilters[j].mask)
			    == 0) {
				count = 1;
				printf("<HR>\n<H3>%s &eacute; provavelmente da ",
				       inet_ntoa(*(struct in_addr*)&fakeIP));
				printf("%s, %s</H3>\n",
				       ipFilters[j].text,
				       inet_ntoa(*(struct in_addr*)&savedIPs[i]));
				break;
			}
	if (!count) {
		printf("<HR>\n<H3>Rede ainda n&atilde;o filtrada... azar.</H3>\n"
		       "<H3>(ou entrou por um ircd versao 1.5.38U+, /version server.ptnet.org para verificar)</H3>\n");
	}
}

void printError(const char *error) {
	printf("<HR>\n<H2>%s</H2>\n", error);
}

void printFooter(const char *text) {
	printf("<HR>\n<A HREF=\"http://www.toxyn.org\">"
	       "<IMG SRC=\"../toxyn.gif\" ALT=\"We luv your computer\""
	       "BORDER=\"0\"></A>\n<BR>\n%s\n</BODY>\n</HTML>\n", text);
}

void getIP(u_long *fakeIP) {
#define MAXBUFFER 18
#define MAXREAD 12
char buffer[MAXBUFFER] = "127.";

	if (fgetc(stdin) == EOF) {
		*fakeIP = 0xFFFFFFFF;
		return;
	}
	fgets(&buffer[4], MAXREAD, stdin);
	if (buffer[strlen(buffer)-1] == '\n')
		buffer[strlen(buffer)-1] = '\0';
	if ((*fakeIP = inet_addr(buffer)) == -1)
		*fakeIP = 0;
}


int main(int argc, char *argv[])
{
u_long fakeIP, ip, offset;
unsigned long long int realIP;
int saved;

	printHeader("PTnot - PT ne(a)t obfuscator translator");
	printForm();
	if (argc != 1) {
		printError("Game Over, Insert Coin");
		printFooter(randomQuote());
		return -1;
	}
	getIP(&fakeIP);
	if (fakeIP == 0xFFFFFFFF) {
		printFooter(randomQuote());
		return 0;
	}
	fakeIP &= 0xFFFFFF00;
	if (fakeIP == 0) {
		printError("IP inv&aacute;lido, lerdo...");
		printFooter(randomQuote());
		return -1;
	}
	for (realIP = 0x0, offset = 1, saved = 0;
	     realIP < 0xFFFFFFFF;
	     realIP += offset) {
		ip = htonl(realIP);
		ip ^= (ip << 8);
		ip ^= (ip << 16);
		ip ^= (ip >> 12);
		ip &= 0xFFFFFF00;
		if (ip == fakeIP) {
			if (saved == MAXSAVEDIPS) {
				printError("Critical error, mission aborted");
				printFooter(randomQuote());
				return -1;
			}
			savedIPs[saved++] = htonl(realIP);
			offset = 0x10000;
		}
	}
	printFilter(fakeIP, saved);
	printFooter(randomQuote());
	return 0;
}
