Guest User

Untitled

a guest
Apr 9th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7.  
  8. public class MyBot {
  9.  
  10.     private Socket socket;
  11.     private BufferedReader serverIn;
  12.     private PrintWriter botOut;
  13.     private boolean connectionClosed = false;
  14.  
  15.     static int port = 6667;
  16.     static String server = "irc.freenode.net";
  17.     String nick = "IncreasinglyAcid", user = "skutr34",
  18.             password = "";
  19.  
  20.     private enum messageToSend {
  21.         HELLO, JAVADOC, FEDORA_LINK, WAITING
  22.     }
  23.  
  24.     public MyBot(String serv, int p) {
  25.         try {
  26.             System.out.println("Attempting to connect");
  27.             socket = new Socket(serv, p);
  28.             serverIn = new BufferedReader(new InputStreamReader(
  29.                     socket.getInputStream()));
  30.             botOut = new PrintWriter(socket.getOutputStream());
  31.         } catch (UnknownHostException e) {
  32.  
  33.             e.printStackTrace();
  34.         } catch (IOException e) {
  35.  
  36.             e.printStackTrace();
  37.         }
  38.  
  39.         if (socket == null) {
  40.             System.out.println("Connection failed.");
  41.             connectionClosed = true;
  42.         }
  43.  
  44.         if (!connectionClosed) {
  45.             System.out.println("Connection is valid, attempting to sign in");
  46.             String lineIn = "";
  47.             botOut.write("NICK " + nick + "\r\n");
  48.             botOut.write("USER  " + user + " 8 * : " + user + "\r\n");
  49.             botOut.flush(); // clear the printwriter
  50.             botOut.write("JOIN #skutr34\r\n");
  51.             botOut.flush();
  52.             botOut.write("NICKSERV IDENTIFY " + password + "\r\n");
  53.             botOut.flush(); // clear printwriter
  54.             try {
  55.                 while ((lineIn = serverIn.readLine()) != null) {
  56.                     System.out.println(lineIn);
  57.  
  58.                     try {
  59.                         if (lineIn.startsWith("PING")) {
  60.                             botOut.write("PONG" + lineIn.split(" ")[1] + "\r\n");
  61.                             botOut.flush();
  62.                         }
  63.                         if (lineIn.contains("PRIVMSG")) {
  64.                             System.out.println(lineIn);
  65.                             String fromNick = lineIn.split(":")[1].split("!")[0];
  66.                             String message = lineIn.substring(1).split(":", 2)[1];
  67.                             String fromHost = lineIn.split("@")[1].split(" ")[0];
  68.                             switch (processCommand(fromNick, fromHost,
  69.                                     lineIn.split(" ")[2], message)) {
  70.                             case HELLO:
  71.                                 sendMessage("#skutr34", "Hello, " + fromNick); // this line
  72.                        // is it okay to use that variable there?
  73.                                 break;
  74.                             }
  75.  
  76.                         }
  77.  
  78.                         if (lineIn.contains("PART")) {
  79.                             System.out.print(lineIn);
  80.                             printQuitMessage();
  81.                         }
  82.  
  83.                         if (lineIn.contains("JOIN")) {
  84.                             System.out.println(lineIn);
  85.                             String fromNick = lineIn.split(":")[1].split("!")[0];
  86.                             onJoin(fromNick);
  87.                         }
  88.  
  89.                     } catch (Exception ignore) {
  90.                     }
  91.                 }
  92.             } catch (IOException e) {
  93.  
  94.                 e.printStackTrace();
  95.             }
  96.         }
  97.     }
  98.  
  99.     public static void main(String[] args) {
  100.         new MyBot(server, port);
  101.  
  102.     }
  103.  
  104.     public void printQuitMessage() {
  105.         sendMessage("#skutr34", "That kid was a loser anyway :P");
  106.     }
  107.  
  108.     public void onJoin(String nick) {
  109.  
  110.         if (!nick.equals("IncreasinglyAcid")) {
  111.             sendMessage(
  112.                     "#skutr34",
  113.                     nick
  114.                             + ": "
  115.                             + "Welcome to #skutr34. Our only rule here is to have fun!...");
  116.         }
  117.     }
  118.  
  119.     public messageToSend processCommand(String nick, String host,
  120.             String channel, String message) {
  121.         if (message.equals("Hello") || message.equals("hey")) {
  122.             return messageToSend.HELLO;
  123.         } else if (message.equals(".fedora")) {
  124.             return messageToSend.FEDORA_LINK;
  125.         } else if (message.equals(".api")) {
  126.             return messageToSend.JAVADOC;
  127.         }
  128.  
  129.         return messageToSend.WAITING;
  130.     }
  131.  
  132.     public void sendMessage(String location, String message) {
  133.         botOut.write("PRIVMSG " + location + " :" + message + "\r\n");
  134.  
  135.         botOut.flush();
  136.     }
  137.  
  138. }
Add Comment
Please, Sign In to add comment