import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.*;
import org.apache.commons.httpclient.methods.*;
import java.util.*;
import org.apache.commons.httpclient.params.*;
import org.apache.commons.httpclient.methods.multipart.*;
import java.io.*;
import java.lang.Thread;

public class Jbrush
{
	private static String user;
	private static String pass;
	private static String message;
	private static int start;
	private static int stop;

	public static void main(String[] args) throws IOException {
	readfile();

	Login xnlogin = new Login(user,pass,message,start,stop);
	}

	private static void readfile() throws IOException {
		File file = new File("./Jbrush.conf");
		if (!file.exists() || file.isDirectory()) {
			throw new FileNotFoundException();
		}
		BufferedReader br = new BufferedReader(new FileReader(file));
		String temp = null;
		while ((temp = br.readLine()) != null ) {
			String[] Array;
			if (temp.indexOf("user") != -1) {
                        	Array = temp.split("=");
				user = Array[1];
				//System.out.println("user:"+user);
				continue;


                	}
			if (temp.indexOf("pass") != -1) {
				Array = temp.split("=");
				pass = Array[1];
				//System.out.println("pass:"+pass);
				continue;
			}
			if (temp.indexOf("message") != -1) {
				Array = temp.split("=");
				message = Array[1];
				//System.out.println("message:"+message);
				continue;
			}
			if (temp.indexOf("start") != -1) {
				Array = temp.split("=");
				start = Integer.parseInt(Array[1]);
				continue;
			}
			if (temp.indexOf("stop") != -1) {
				Array = temp.split("=");
				stop = Integer.parseInt(Array[1]);
				continue;
			}
;
			
		}
	}
}

class Login extends Thread
{
	public Login (String user,String pass,String message,int start,int stop) throws IOException {

		HttpClient client = new HttpClient();
	        client.getHostConfiguration().setHost("www.xiaonei.com", 80, "http");
	        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
		/*设置cookies为最大兼容 不设置校内会出错*/


	
		/*登录论坛*/
       		PostMethod authpost = new PostMethod("/Login.do");
     		NameValuePair userid   = new NameValuePair("email", user);
       		NameValuePair password = new NameValuePair("password", pass);
		NameValuePair hideid   = new NameValuePair("autoLogin","false");

        	authpost.setRequestBody( new NameValuePair[] {userid, password,hideid});

       		int status = client.executeMethod(authpost);

		System.out.println("status = "+status);
        	authpost.releaseConnection();
		/*登录完成*/

		/*取得用户的ID*/
		GetMethod getid = new GetMethod("http://www.xiaonei.com/Home.do");
		client.executeMethod(getid);
		StringBuffer HomeBuffer = new StringBuffer();
	        BufferedReader in = new BufferedReader(
	        new InputStreamReader(

        	getid.getResponseBodyAsStream(),
	        getid.getResponseCharSet()));

	        String inputLine = null;
  	 	String myid = null;
       	 	while ((inputLine = in.readLine()) != null) {

                	String myhome = "myhome";
                	if (inputLine.indexOf(myhome) != -1) {
                        	myid = inputLine.substring(65,74);/*verify值在一行>的指定位置*/
                	}

        	}

		//System.out.println("my id is :"+myid);
		try {
			sleep(10);
		}catch (Exception e){
			;
		}
		
		/*开始访问校内网用户*/

		String url = "http://xiaonei.com/getuser.do?id=";
		String urla = "http://xiaonei.com/RandImageServlet?post=uservalidate";
		String verifyPostURL = "http://xiaonei.com/validateuser.do";
		String postmessageURL = "http://xiaonei.com/gossip.do";

		String verifyURL = urla.concat(myid);
		//System.out.println("verify url is "+verifyURL);


		int count = 91;		/*访问数量计数，超过90则获取验证码*/

		for (int i = start;i < stop;i++) {

				/*访问一定数量后获取验证码,并且识别*/
			if (count > 90) {
				        
				System.out.println("Get verify Code");

				GetMethod getverify = new GetMethod(verifyURL);
                 		client.executeMethod(getverify);

        			int bytesum=0;
			        int byteread=0; 
	        		InputStream inStream=getverify.getResponseBodyAsStream();
        			FileOutputStream fs=new FileOutputStream( "1.png");
				byte[]  buffer =new  byte[1024];
	        		int length;
        			while ((byteread=inStream.read(buffer))!=-1) {
                       			bytesum+=byteread;
                                	fs.write(buffer,0,byteread);
                		} 
        			inStream.close();
				/*获取验证码完成*/

				/*调用codeHacker识别获取的验证码*/

        			CodeHacker codeHacker = new CodeHacker("1.png");
        			String verifyCode = codeHacker.getCode() ;
				//System.out.println(""+verifyCode);
				/*提交验证码 */

				PostMethod postverify = new PostMethod(verifyPostURL);
				NameValuePair userValidateCode   = new NameValuePair("userValidateCode",verifyCode);

        			postverify.setRequestBody( new NameValuePair[] {userValidateCode});
				client.executeMethod(postverify);
				postverify.releaseConnection();
				System.out.println("submit verify code success!");

				count = 0;
				continue;
			}
			/*验证码识别完毕*/

			/*连续访问页面*/
			String str = ""+i;
			String visiturl = url.concat(str);	
			//System.out.println("url= "+visiturl);
 			GetMethod visit = new GetMethod(visiturl);
			int status1 = client.executeMethod(visit);

			StringBuffer akBuffer = new StringBuffer();
		        in = new BufferedReader(
		        new InputStreamReader(
	
        		visit.getResponseBodyAsStream(),
	        	visit.getResponseCharSet()));
	
		        inputLine = null;
  	 		String strak = null;
	       	 	while ((inputLine = in.readLine()) != null) {
	
        	        	String strofak = "name=\"ak\"";
                		if (inputLine.indexOf(strofak) != -1) {
                        		strak = inputLine.substring(41,73);/*verify值在一行>的指定位置*/
                		}
	
        		}

			//System.out.println("ak is "+strak);


			visit.releaseConnection();

			/*留言*/
			visit.removeRequestHeader("Content-Type");
        		visit.addRequestHeader(new Header("Content-Type","text/html; charset=UTF-8"));

			//message = java.net.URLEncoder.encode(message,"utf8");
			PostMethod postmessage  = new PostMethod(postmessageURL);
			NameValuePair curpage   = new NameValuePair("curpage","");
			NameValuePair from	= new NameValuePair("from","main");
			NameValuePair id	= new NameValuePair("id",str);
			NameValuePair cc	= new NameValuePair("cc",str);
			NameValuePair ak	= new NameValuePair("ak",strak);
			NameValuePair body	= new NameValuePair("body",message);


        		postmessage.setRequestBody( new NameValuePair[] {curpage,from,id,cc,ak,body});
			client.executeMethod(postmessage);
			String response2= new String(postmessage.getResponseBodyAsString().getBytes("utf8"));

        		System.out.println(response2);
			postmessage.releaseConnection();

			try {
				sleep(3000);		/*访问间隔为3秒*/
			}catch(Exception e) {
				;
			}
			if (status1 == 200) {
				System.out.println("Visited :"+i+" success!");
			}
			/*String response = new String(visit.getResponseBodyAsString().getBytes("utf8"));

        		System.out.println(response);
			*/
			count++;
		}

	}

	private static String ConverterStringCode(String source, String srcEncode, String destEncode) {
		if (source != null) {
			try {
				return new String(source.getBytes(srcEncode), destEncode);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return "";
			}
		} else {
			return "";
		}
	}	
    
																				    
}

