import java.io.*; import java.net.*; public class Bot { private Socket socket; private InetAddress IP; private int port = 6667; private PrintWriter out; private BufferedReader in; private boolean connectionactive = false; public Bot() { } public void Connect() { try { IP = java.net.InetAddress.getByName("irc.gtanet.com"); } catch (UnknownHostException ex) { System.out.println("UnknownHost!"); } try { socket = new Socket( IP, port); } catch (IOException blah) { System.out.println("IOException: " + blah.getMessage() ); } setConnectionActive(true); MaintainConnection(); // out.println("NICK Wackobot\r\n"); // out.println("USER Henk irc.gtanet.com Henk\r\n"); } public void MaintainConnection() { System.out.println(""); try { out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ); } catch (UnknownHostException e) { System.out.println(" Unknownhost!"); } catch (IOException e) { System.out.println(" IOException (owned!) "); } String userInput; BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); try{ out.println("MOTD\r\n"); while ( true ) { if( (userInput = stdIn.readLine() ) != null) { out.println(userInput + "\r\n"); } handleServerMessage( in.readLine() ); } } catch ( IOException e ) { System.out.println("IOException: " + e.getMessage() ) ; } } public boolean connectionIsActive() { return connectionactive; } public void setConnectionActive(boolean toggle) { connectionactive = toggle; } public void handleServerMessage( String msg ) { if( msg != null ) { String array[] = msg.split(" "); /* for(int i = 0; i < array.length; i++) { System.out.println( array[i] ); } */ if(array[0] == "PING") { out.println("PONG " + array[1]); } else{ System.out.println(msg); } } } public void getConnectionState() { if( socket != null && socket.isConnected() ) { System.out.println(" Connected: " + socket.isConnected() ); } else{ System.out.println(" Connected: false"); } } public void Disconnect() { setConnectionActive(false); try { out.println("QUIT"); socket.close(); in.close(); out.close(); } catch (IOException moo) { System.out.println("IOException: " + moo.getMessage() ); } } }