import java.io.*;
import java.net.*;
import java.util.*;

public class BotIRC
{
	private int Port;
	private Socket Socket;
	private String Nom;
	private String Host;
	private OutputStream out;
	private InputStream in; 
	private BufferedReader br; 

	public void envoiePong()	
		throws IOException
	{ 
		out.write(("PONG\r\n").getBytes()); 
	}

	public void envoieNick()	
		throws IOException
	{ 
		out.write(("NICK "+Nom+"\r\n").getBytes()); 
	}

	public void envoieUser()	
		throws IOException
	{ 
		out.write( ("USER BOT "
		+Socket.getLocalAddress().getHostName()
		+" "+Host
		+" NONO\r\n").getBytes() );
	}
	
	public	void joinChannel(String Channel)
		throws IOException
	{
		out.write( ("JOIN "+Channel+"\r\n").getBytes());
	}

	public	void envoieMessage(String AQui,String Message)
		throws IOException
	{
		out.write( ("PRIVMSG "+AQui+" :"+Message+"\r\n").getBytes());
	}

	public	void envoieOP(String Channel,String AQui)
		throws IOException
	{
		out.write( ("MODE "+Channel+" +o "+AQui+"\r\n").getBytes());
	}

	public String litEntree()
		throws IOException
	{
		byte [] b = new byte[100000];
		in.read(b) ;
		return new String(b);
	}


	public BotIRC2(int Port,String Host,String Nom)
	{
		this.Port 	= Port;
		this.Nom 	= Nom;
		this.Host 	= Host;
		try 
		{ 
			Socket = new Socket(Host,Port);
			out = Socket.getOutputStream();
			in = Socket.getInputStream();
			br = new BufferedReader(new InputStreamReader(in));
		}
		catch (IOException ioe) { System.err.println(ioe);}
	}

	public BotIRC2(String Host, String Nom)
	{
		this(6667,Host,Nom);
	}

	public BotIRC2(String Host)
	{
		this(6667,Host,"Bot");	
	}

	public void connecte(String Channel)
	{
		try
		{
			envoieNick();
			envoieUser();
			System.err.println(litEntree());
			joinChannel(Channel);
			System.err.println(litEntree());
			envoieMessage(Channel,"HelloWorld");
		}
		catch (IOException ioe) {System.err.println(ioe);}
	}

	public String [] decoupeLigne(String chaine)
	{
		StringTokenizer st 	= new StringTokenizer(chaine);
		int l			= st.countTokens();
		String [] tab		= new String[l];
		for (int i=0;i<l;i++)
			tab[i]=st.nextToken();
		return tab;	
	}

	public void Run(String Channel,String Nick)
	{
		try 
		{
			String lit=null;
			String [] commandes = null;
			String WHO;
			String CMD;
			String QUI;
			String QUOI;
			while (true)
			{
				lit = litEntree();
System.err.println(lit);
				if (lit.indexOf("JOIN")!=-1)
				{
					envoieMessage(Channel,"Bienvenue");
					if (lit.indexOf("Tom")!=-1)
						envoieOP(Channel,"Tom");
					if (lit.indexOf("Dav")!=-1)
						envoieOP(Channel,"Dav");
				}
				if (lit.indexOf("PING")!=-1)
				{
					envoiePong();
				}
				if ( lit.indexOf("PRIVMSG")!=-1 ) 
				{
					if ( lit.toLowerCase().indexOf("Au revoir "+Nick.toLowerCase())!=-1)
					{
					 	envoieMessage(Channel,"Au revoir");
						System.exit(0);
					}
					if ( lit.toLowerCase().indexOf(Nick.toLowerCase())!=-1)
						envoieMessage(Channel,"Je suis plutôt d'accord");
				}
					
			}
			
		}
		catch (IOException ioe) {System.err.println(ioe); }
	}

	public static void main(String [] a)
	{
		if (a.length!=3) 
		{
			System.err.println("Usage host nick chann");
			System.exit(0);
		}
		BotIRC birc = new BotIRC(a[0],a[1]);
		birc.connecte(a[2]);
		birc.Run(a[2],a[1]);
	}
}

