/*
    pg2ipfilter.cpp - Convert PeerGuardian to Donkey Blocklists
    Copyright (C) 2009, me@maeyanie.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define SECLEVEL 0

inline int iprewrite(char* out, const char* in) {
	unsigned char ipbytes[4];
	if (sscanf(in, "%hhu.%hhu.%hhu.%hhu", &ipbytes[3], &ipbytes[2], &ipbytes[1], &ipbytes[0]) != 4) return 0;
	sprintf(out, "%.3hhu.%.3hhu.%.3hhu.%.3hhu", ipbytes[3], ipbytes[2], ipbytes[1], ipbytes[0]);
	return 1;
}

int usage(char* progname) {
	printf("Usage: %s [<input file> [<output file>]]\n"
		"If file are not given, or are a dash, stdin/stdout are used instead.\n"
		"However, I'll refuse to read from a console.\n"
		"\n"
		"Example: zcat guarding.p2p.gz | %s | gzip --best > ipfilter.dat.gz\n", progname, progname);
	return 1;
}

int main(int argc, char* argv[]) {
	FILE* ifp;
	FILE* ofp;
	char* line = NULL;
	size_t linelen = 0;
	char* split;
	char* startipstr, * endipstr;
	char startipout[16], endipout[16];
	unsigned int linecount = 0;

	if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) return usage(argv[0]);

	if (argc < 2 || !strcmp(argv[1], "-")) {
		if (isatty(STDIN_FILENO)) return usage(argv[0]);
		ifp = stdin;
	} else {
		ifp = fopen(argv[1], "r");
		if (!ifp) { fprintf(stderr, "Error opening input file %s: %m\n", argv[1]); return 2; }
	}

	if (argc < 3 || !strcmp(argv[2], "-")) {
		ofp = stdout;
	} else {
		ofp = fopen(argv[2], "w");
		if (!ifp) { fprintf(stderr, "Error opening output file %s: %m\n", argv[2]); return 2; }
	}
	
	while (getline(&line, &linelen, ifp) >= 0) {
		/*
		PG: www.pchell.com:8.6.223.13-8.6.223.13
		ED: 008.006.223.013 - 008.006.223.013 , 200 , www.pchell.com
		*/
		split = strrchr(line, ':');
		if (split == NULL) { fprintf(stderr, "Warning: Could not parse line '%s'\n", line); continue; }
		*split = 0;
		
		startipstr = strtok(split+1, "-");
		endipstr = strtok(NULL, "\r\n");
		if (!startipstr || !endipstr) { fprintf(stderr, "Warning: Could not parse IPs.\n"); continue; }
		
		if (!iprewrite(startipout, startipstr)) { fprintf(stderr, "Warning: Invalid start IP '%s'\n", startipstr); continue; }
		if (!iprewrite(endipout, endipstr)) { fprintf(stderr, "Warning: Invalid end IP '%s'\n", endipstr); continue; }
		
		fprintf(ofp, "%s - %s , %.3d , %s\r\n", startipout, endipout, SECLEVEL, line);
		linecount++;
	}
	
	fprintf(stderr, "Converted %u lines.\n", linecount);
	fclose(ifp);
	fclose(ofp);
	return 0;
}




// EOF

