Advertisement
thefinn93

IRC bawt

Feb 10th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. package irc;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.Socket;
  8. import java.net.UnknownHostException;
  9. import java.util.Random;
  10.  
  11. public class IRC {
  12.     public static void main(String[] args) {
  13.         Random randomGenerator = new Random();
  14.         String host = "seattle.uwirc.com";
  15.         int port = 6667;
  16.         String nick = "FinnBawt" + randomGenerator.nextInt(100);
  17.         String channel = "#css161";
  18.         try{
  19.             Socket socket = new Socket(host, port);
  20.             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
  21.             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  22.             out.println("NICK " + nick);
  23.             out.println("USER " + nick + " " + nick + " " + host + " " + nick);
  24.             out.println("JOIN " + channel);
  25.             out.println("PRIVMSG thefinn93 hai");
  26.             String line;
  27.             while((line = in.readLine()) != null) {
  28.                 System.out.println(line);
  29.                 String[] parsedline = line.split(" ",4);
  30.                 if(parsedline[0].equals("PING")) {
  31.                     out.println("PONG");
  32.                 } else if(parsedline[1].equals("PRIVMSG")) {
  33.                     if(parsedline[2].equals(nick)) {
  34.                         String from = parsedline[0].split("!")[0].split(":")[1];
  35.                         String msg = parsedline[3].split(":",2)[1];
  36.                         out.println("PRIVMSG " + from + " " + parsedline[3]);
  37.                         if(from.equals("thefinn93") && parsedline[3].equals(":quit")) {
  38.                             System.out.println("Quitting!");
  39.                             out.println("QUIT :Good night");
  40.                             socket.close();
  41.                         }
  42.                     } else {
  43.                         out.println("PRIVMSG " + parsedline[2] + " " + parsedline[3]);
  44.                     }
  45.                 } else if(parsedline[1].equals("NOTICE")) {
  46.                     System.out.println("NOTICE: " + line.split(":")[1]);
  47.                 } else {
  48.                     System.out.println("Recevied " + parsedline[1] + ", not sure what to do about it.");
  49.                     System.out.println(line);
  50.                 }
  51.             }
  52.         } catch (UnknownHostException e) {
  53.             System.out.println("Unknown host: " + host);
  54.             System.exit(1);
  55.         } catch  (IOException e) {
  56.             System.out.println("No I/O");
  57.             System.exit(1);
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement