#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>

#include "goo.h"

//written by l0om


int main(int argc, char **argv)
{
	int i, max;
	FILE *fd;
	char buf[255];
	goo_t goo;
	cli_ans ans;
	time_t thetime;

	if(argc != 2) {
		printf("%s <dork-file> [proxy.domain.de:port]\n",argv[0]);
		return 0;
	}
	if( (fd = fopen(argv[1], "r")) == NULL) {
		perror("fopen");
		return 0;
	}

	goo = init_goo();
	if(goo.resultc != SUCCESS) {
		goo_prnt_er(&goo);
		return 0;
	}
	goo_cli(1,&goo);
	if(!goo_cli_cnt(&goo)) {
		goo_prnt_er(&goo);
		return 0;
	}
	goo_cli_set(1, GETRESLT, NULL, &goo);
	if(argc == 3) 
		goo_cli_set(1, SETPRXY, argv[2], &goo);
	

	while(!feof(fd)) {
		if(fgets(buf,255,fd) == NULL) break;
		buf[strlen(buf)-1] = '\0';
		goo_cli_req(1, buf, &goo,0);

		thetime = time(NULL);
		while(1) {
			if(thetime < (time(NULL)-10)) break;
			ans = goo_results(&goo);
			if(goo.resultc != NOANSWER)   {  // read answers for 10 seconds
				if(goo.resultc != SUCCESS) {
					goo_prnt_er(&goo);
					break;
				}
                                printf("\t\t%s: %u results\n",ans.req,ans.goo_res);
				break;
			}
			usleep(100000);  //dont harm your cpu
		}
	}
	fclose(fd);
	delete_goo(&goo);
	return 1;
}


