#include "connect_donkey.h"
#include <iostream.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pcre.h>


int connect_donkey::get(char *s,int p, char *u, char *pass) {
	
	struct hostent *hp;
	struct sockaddr_in pin;
	int sd;
	
	strcpy(server,s);
	port=p;
	
	if ((hp = gethostbyname(server)) == 0) {
		strcpy(error,"ERROR: getting hostname");
		return -1;
	}
	
	memset(&pin, 0, sizeof(pin));
	pin.sin_family = AF_INET;
	pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
	pin.sin_port = htons(port);
	
	if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		strcpy(error,"ERROR: binding to socket");
		return -1;
	}

	if (connect(sd,(struct sockaddr *)  &pin, sizeof(pin)) == -1) {
		strcpy(error,"ERROR: connecting to server");
		return -1;
	}
	
	char data[4048+1];
	int start;
	start=0;
	int offset;
	int ovector1[30];
	const char *error1;
	pcre *test1;
	int t;
	t=-1;
	test1=pcre_compile("help",0,&error1,&offset,NULL);
	
	
	while (t<1) {
		int i;
		for (i=0;i<4048;i++) {
				data[i]='\0';
		}
		if (recv(sd, data, 4048, 0) == -1) {
			strcpy(error,"ERROR: can not read from descriptor");
			return -1;
		}
		t=pcre_exec(test1,NULL,data,strlen(data),start,0,ovector1,30);
		//cout << "DEBUG: get: |"<<data<<"|"<<endl;
	}
	int i;	
	for (i=0;i<4048;i++) {
	                    data[i]='\0';
	}
	
	char msg[400];
	for (i=0;i<400;i++) {
	            msg[i]='\0';
	}
	if ( (strcmp(u,"none")!=0) && (strcmp(pass,"none")!=0) ) {
		strcat(msg,"auth ");
		strcat(msg,u);
		strcat(msg," ");
		strcat(msg,pass);
		strcat(msg,"\n");
		if (send(sd, msg, strlen(msg), 0) == -1) {
			strcpy(error,"ERROR: can not write to descriptor");
			return -1;
		}
		
		if (recv(sd, data, 4048, 0) == -1) {
			strcpy(error,"ERROR: can not read from descriptor");
                        return -1;
                }

		if (strcmp(data,"Full access enabled\n")!=0) {
			cout << "DEBUG(vdr-mldonkey): access to server not granted" << endl;
			strcpy(error,"ERROR: Access to server not granted");
			close(sd);
			return -1;
		}
	}
	
	for (i=0;i<4048;i++) {
	       data[i]='\0';
	}
	if (send(sd, "vd\n", strlen("vd\n"), 0) == -1) {
		return -1;
	}
		
	if (recv(sd, data, 4048, 0) == -1) {
        	return -1;
	}
	
		
	if (send(sd, "q\n", strlen("q\n"), 0) == -1) {
        	return -1;
	}
	
	close(sd);
	//cout << "DEBUG: data |" << data << "|" <<endl;	
	pcre *test;
	int rc;
	char buffer[250];
	test=pcre_compile("\\[[B|D|F|G|O|T].*\\]\\s(.*)\\s\\s+(\\d+\\.*\\d*)\\s+(\\d+\\.*\\d*\\S+)\\s+(\\d+\\.*\\d*\\S+)\\s+\\d+\\.*\\d*\\%\\s+\\d+:[\\d\\-]*\\s+\\d+\\/\\d+\\s+([\\d\\-]+\\.*\\d*)\\s",0,&error1,&offset,NULL);
	

	rc=1;
	start=0;
	int ovector[30];
	int count;
	count=0;
	while(rc>0) {
		rc=pcre_exec(test,NULL,data,strlen(data),start,0,ovector,30);
		//cout << "DEBUG: rc=" << rc << endl;
		if (rc>0) {
			int h;
			char file[60];
			char pct[10];
			char done[10];
			char size[10];
			char rate[10];
			int i;

			for (i=0;i<250;i++) {
				buffer[i]='\0';
			}
			h=pcre_copy_substring(data,ovector,rc,1,buffer,250);
			strncpy(file,buffer,59);
			//cout << "DEBUG: file:" << file << "|" << endl;	
			for (i=0;i<250;i++) {
                                buffer[i]='\0';
                        }
			h=pcre_copy_substring(data,ovector,rc,2,buffer,250);
			strcpy(pct,buffer);
			//cout << "DEBUG: pct:" << pct << "|"<<endl;	
			for (i=0;i<250;i++) {
                                buffer[i]='\0';
                        }
			h=pcre_copy_substring(data,ovector,rc,3,buffer,250);
			strncpy(done,buffer,9);
			//cout << "DEBUG: done:" << done << endl;
			for (i=0;i<250;i++) {
                                buffer[i]='\0';
                        }		
			h=pcre_copy_substring(data,ovector,rc,4,buffer,250);
			strncpy(size,buffer,9);
			//cout << "DEBUG: size:" << size << endl;
			for (i=0;i<250;i++) {
                                buffer[i]='\0';
                        }
			h=pcre_copy_substring(data,ovector,rc,5,buffer,250);
			strncpy(rate,buffer,5);
			//cout << "DEBUG: rate:" << rate << endl;
			
			count++;
			start=ovector[rc-1];
			down.add(file,pct,size,done,rate);
		}
	}
	return 0;
}
