import win;
import win.ui; 
import win.ui.menu
import win.ole
import wsock 
import inet.http
import string.list	 
 
//io.open() //打开控制台可以显示后台线程的错误信息
/*DSG{{*/
var winform = win.form(parent=...; bgcolor=15790320;bottom=428;max=1;text="网站机器人制作工具";left=0;exmode="";min=1;title="AAuto Form";style=0;right=560;top=0 )
winform.add( 
txtCode={ dr=1;dl=1;bgcolor=16777215;color=0;right=550;left=8;dt=1;db=1;cls="richedit";bottom=419;text="";multiline=1;top=114;acceptfiles=1;tabstop=1;edge=1;z=1 };
btnRecord={ dl=1;bgcolor=15790320;bottom=55;color=0;right=99;left=30;dt=1;top=22;acceptfiles=1;tabstop=1;text="录制";multiline=1;z=2;cls="button" };
btnGen={ dl=1;disabled=1;color=0;right=308;left=204;dt=1;cls="button";bottom=55;text="生成模拟代码";multiline=1;top=22;acceptfiles=1;tabstop=1;bgcolor=15790320;z=3 };
btnPause={ dl=1;disabled=1;color=0;right=187;left=118;dt=1;cls="button";bottom=55;text="暂停";multiline=1;top=22;acceptfiles=1;tabstop=1;bgcolor=15790320;z=4 };
static={ dl=1;bgcolor=15790320;bottom=103;color=0;right=484;left=38;dt=1;top=61;acceptfiles=1;tabstop=1;text="把你上网过程中数据的流动记录下来，
录制程序会进行分析然后模仿你的上网的动作！即你在网上做什么它就做什么.";z=5;transparent=1;cls="static" };
copyCode={ dl=1;bgcolor=16053492;color=0;right=403;left=330;dt=1;cls="button";bottom=55;text="复制代码";multiline=1;top=22;acceptfiles=1;z=6;disabled=1;tabstop=1;transparent=1 };
groupbox={ dr=1;dl=1;bgcolor=15790320;color=0;right=551;left=10;dt=1;cls="groupbox";z=0;bottom=106;text="Ecranesoft";top=3;acceptfiles=1;tabstop=1;edge=1;transparent=1 }
)
/*}}*/

import win.clip;
winform.copyCode.oncommand = function(id,event){
	//win.msgbox( winform.copyCode.text );
	win.clip.write(winform.txtCode.text);
	win.msgbox("复制完成","AAuto提示")
	//无返回值则继续调用默认回调函数
}//endproc
winform.wndproc = function(hwnd,message,wparam,lparam){
select(message) {//判断消息类型 
	case 0x10/*_WM_CLOSE*///窗口关闭消息
	{ 
		var hThreadSniff = thread.get("hThreadSniff") ; //在一个进程中，仅允许启动一次录制程序
		if(hThreadSniff){
		thread.suspend(hThreadSniff)
		}
	} 
	else
	{ }
}
}//endproc

 
GEN_CODE = /***
import inet.http;
import string.list;

getInputNameValues = function(html){
	var tab ={}
	var regexExpression =  //\<<input>|<INPUT>[^>]+\s*<name>|<NAME>\s*=\s*['"]*([^>"']+)['"]*[^>]+\s*<value>|<VALUE>\s*=\s*['"]*([^>"']*)['"]*\s
	for name,value in string.gmatch( html, regexExpression ){
   	tab[name] = value;
	}
	return tab;
}


 inet.post = function( tinfo ){
 http = inet.http( tinfo["useragent"] );  
 if(tinfo.referer ){
		var html,errmsg   = http.down(tinfo.referer);//下载页面
		
		if(html)
		{
			//..string.save("\referer.txt",strinput);
			//process.execute( io.fullpath("\referer.txt")  );
			tinfo.inputs = getInputNameValues(html)
			
			if(tinfo.postdata && tinfo.inputs){
				//提交表单修正
				var tpostdata = string.list(tinfo.postdata,"&");
				for(kp,vp in tpostdata){
						for(ki,vi in tinputs){
							if(kp == ki)
								tpostdata[kp] = vi;
						}
				}
				tinfo.postdata = ..tostring(tpostdata);
			}
		}
		else{
			io.print(errmsg);
		}
}
var html2,errmsg2 = http.down(tinfo.url,tinfo.postdata,tinfo.referer) 
return html2,errmsg
			
			
}

***/

threadsniff = function( ){
    import wsock.sniff
	import inet.http
	import string.list	
    thread.set("生成代码","" ) 
	/*记录登录过程的数据*/
	while(1){
	    sleep(1) 
 		for(sockdata,sniff in wsock.sniff())
		{
		    
		    sleep(1)
			if(sockdata.ok)
			{
				if(sockdata.tcpheader) 
				{  
  		    			tinfo = wsock.decodeHttpPack( sockdata.tcpdata )
						
						if(tinfo.method == "POST")
  		    			{ 
  		    			    var tinfo2  = {}
  		    			    //修正不是合法标志符的键名，避免table.tostring出现错误的结果
  		    			    for(k,v in tinfo){
  		    			        var k2 = string.replace(k,"-","");
  		    			        tinfo2[k2] = v; 
  		    			    }
  		    			    
  		    			    thread.set("生成代码",thread.get("生成代码")  ++ string.format('html,errmsg = inet.post(%s)\r\n', ..table.tostring(tinfo2) )  )
  		        			break ;
  		    			}  
				}
			}		
		} 
				 
	//无返回值则继续调用默认回调函数
}
}


var hThreadSniff;

winform.btnGen.oncommand = function(id,event){
	//win.msgbox( winform.btnGen.text );
	var txtCode = thread.get("生成代码");
	thread.set("生成代码","");
	
	winform.txtCode.text = GEN_CODE ++ txtCode;
	winform.copyCode.disabled = false;
	winform.txtCode.redraw();
	
	//无返回值则继续调用默认回调函数
}//endproc

winform.btnPause.oncommand = function(id,event){
	//win.msgbox( winform.btnPause.text );
	var hThreadSniff = thread.get("hThreadSniff") ; //在一个进程中，仅允许启动一次录制程序
	if(hThreadSniff){
	thread.suspend(hThreadSniff)
	
	winform.btnPause.disabled = true;
	winform.btnRecord.disabled = false;
	}
	//无返回值则继续调用默认回调函数
}//endproc



winform.btnRecord.oncommand = function(id,event){
	//win.msgbox( winform.btnRecord.text );
	var hThreadSniff = thread.get("hThreadSniff") ; //在一个进程中，仅允许启动一次录制程序
	if(!hThreadSniff){
		hThreadSniff  = thread.create( threadsniff )
		thread.set("hThreadSniff", hThreadSniff )
	}
	
	thread.resume(hThreadSniff)
	winform.btnGen.disabled = false;
	winform.btnPause.disabled = false;
	winform.btnRecord.disabled = true;
	//无返回值则继续调用默认回调函数
}//endproc


 
winform.show(true) 
win.loopMessage(winform);
