package ysitd.ircbot.java.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class CommandHandler implements Runnable{
	/*其實算是CommandEvent
	  *不過不使用Event的語法banfile
	  *@Runnable
	  *@prefix=\
	  *解析後若為指令
	  *呼叫UserCommandProcessEvent
	  *同時傳入資訊
	  *再由Event處理 , 呼叫
	  */
	Socket server;
	BufferedReader reader;
	
	public CommandHandler() throws IOException{
		server=JIRCBOTPlugin.getServer();
		reader=new BufferedReader( new InputStreamReader( server.getInputStream() ) );
	}

	@Override
	public void run() {
		//Can while( ( msline=reader.readLine() ) != null) run successfully? infinity loop?
		while(true){
			String msline;
			String username;
		
			try{
				while( ( msline=reader.readLine() ) != null){
			
					if( msline.matches("\bPRIVMSG\b") ){
						username=new String(msline).substring(msline.indexOf("#" , 2) , msline.indexOf(":" , 2));
						String[] arr=msline.split(":*:");
						msline=msline.substring(arr[0].length());
						if( msline.startsWith("\\") ){ //prefix setting還有event開始了.
							for(CommandExecutor command : JIRCBOTPlugin.commandlist){
								new UserCommandProcessEvent(username , "\\" , command.getName() , msline.replaceFirst("\\" , "").split(" ") , command).Do();
								System.out.println(msline.replaceFirst("\\" , "").split(" ")); //debug
							}
						}
					}
			
				}
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
	}		
}







@@ -195,16 +195,21 @@
 ble%7B%0A%09/*
+%0A%09  *
 %E5%85%B6%E5%AF%A6%E7%AE%97%E6%98%AFComm
@@ -213,24 +213,24 @@
 ommandEvent%0A
-
 %09  *%E4%B8%8D%E9%81%8E%E4%B8%8D%E4%BD%BF%E7%94%A8Eve
@@ -591,17 +591,23 @@
 () %7B%0A%09%09/
-/
+*%0A%09%09 * 
 Can whil
@@ -680,16 +680,159 @@
 y loop?%0A
+%09%09 * %E5%B0%8D%E8%A9%B1%E4%BE%8B%E5%AD%90/%0A%09%09 * :petjelinux!~petjelinu@61-230-157-131.dynamic.hinet.net PRIVMSG #ysitd :%E8%A6%81%E4%BF%AE%E9%82%84%E9%9C%80%E8%A6%81%E4%BF%AE%E4%B8%80%E9%99%A3%E5%AD%90 , %E6%89%80%E4%BB%A5%E8%83%BD%E5%A4%A0%E7%B5%A6%E6%88%91%E4%B8%80%E6%AE%B5%E5%97%8E %E5%8C%85%E6%8B%AC%E5%B0%8D%E8%A9%B1%0A%09%09 * 01    ~    10   %0A%09%09 */%0A
 %09%09while(
@@ -876,19 +876,16 @@
 ername;%0A
-%09%09%0A
 %09%09%09try%7B%0A
@@ -934,20 +934,16 @@
  null)%7B%0A
-%09%09%09%0A
 %09%09%09%09%09if(
@@ -1016,24 +1016,28 @@
 ).substring(
+1 , 
 msline.index
@@ -1044,43 +1044,39 @@
 Of(%22
-#
+!
 %22 , 
-2) , msline.indexOf(%22:%22 , 2));
+1)); //result=%22petjelinux%22
 %0A%09%09%09
@@ -1110,13 +1110,51 @@
 t(%22:
+.
 *:%22);
+//result=%7B%E8%A6%81%E4%BF%AE%E9%82%84%E9%9C%80%E8%A6%81%E4%BF%AE%E4%B8%80%E9%99%A3%E5%AD%90 , %E6%89%80%E4%BB%A5%E8%83%BD%E5%A4%A0%E7%B5%A6%E6%88%91%E4%B8%80%E6%AE%B5%E5%97%8E %E5%8C%85%E6%8B%AC%E5%B0%8D%E8%A9%B1%7D
 %0A%09%09%09
@@ -1197,16 +1197,53 @@
 ngth());
+//result=%22%E8%A6%81%E4%BF%AE%E9%82%84%E9%9C%80%E8%A6%81%E4%BF%AE%E4%B8%80%E9%99%A3%E5%AD%90 , %E6%89%80%E4%BB%A5%E8%83%BD%E5%A4%A0%E7%B5%A6%E6%88%91%E4%B8%80%E6%AE%B5%E5%97%8E %E5%8C%85%E6%8B%AC%E5%B0%8D%E8%A9%B1%22
 %0A%09%09%09%09%09%09i
@@ -1298,17 +1298,16 @@
 event%E9%96%8B%E5%A7%8B%E4%BA%86
-.
 %0A%09%09%09%09%09%09%09

rewrite regex