#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>\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(2,&goo);
	if(!goo_cli_cnt(&goo)) {
		goo_prnt_er(&goo);
		return 0;
	}
	goo_cli_set(1, GETRESLT, NULL, &goo);
	goo_cli_set(2, GETRESLT, NULL, &goo);

	while(!feof(fd)) {
		max = 0;
		for(i = 0; i < 8; i++) {
			if(fgets(buf,255,fd) == NULL) break;
			buf[strlen(buf)-1] = '\0';

			if(!i)
				max += goo_cli_req(1, buf, &goo,0);
			else max += goo_cli_req(2, buf, &goo,0);
			printf("have sent request: %s\n",buf);
		}
		thetime = time(NULL);
		while(max) {

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

