Guest User

Untitled

a guest
May 20th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.52 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.net.Socket;
  7.  
  8. /**
  9.  *
  10.  * @author Ramon Saraiva
  11.  */
  12. class WrongParametersException extends Exception {
  13.     String from;
  14.     String message;
  15.    
  16.     public WrongParametersException(String from, String message) {
  17.         this.from = from;
  18.         this.message = "exception -> wrong parameters: " + message;
  19.     }
  20.    
  21.     public String toString() {
  22.         return message;
  23.     }
  24.    
  25.     public String from() {
  26.         return from;
  27.     }
  28. }
  29.  
  30. public class IRCBot extends Thread {
  31.  
  32.     private final String NETWORK;
  33.     private final int PORT;
  34.     private String nickname;
  35.     private String defaultChannel;
  36.     private String admin;
  37.     private Socket connection;
  38.     private BufferedReader input;
  39.     private BufferedWriter output;
  40.     private final String VERSION = "1.00";
  41.  
  42.     public IRCBot(String NETWORK, int PORT, String nickname, String defaultChannel, String admin) {
  43.         this.NETWORK = NETWORK;
  44.         this.PORT = PORT;
  45.         this.nickname = nickname;
  46.         this.defaultChannel = defaultChannel;
  47.         this.admin = admin;
  48.     }
  49.  
  50.     private void connect() throws Exception {
  51.         connection = new Socket(NETWORK, PORT);
  52.         input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  53.         output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
  54.     }
  55.  
  56.     private void disconnect() throws Exception {
  57.         connection.close();
  58.     }
  59.  
  60.     private void sendLoginData() throws Exception {
  61.         output.write("NICK " + nickname + "\n");
  62.         output.write("USER " + nickname + " codemonkey.com CM: " + nickname + "\n");
  63.         output.flush();
  64.     }
  65.  
  66.     private void pingPong(String[] data) throws Exception {
  67.         if (data[0].equals("PING")) {
  68.             System.out.println("--> PONG " + data[1]);
  69.             output.write("PONG " + data[1] + "\n");
  70.             output.flush();
  71.         }
  72.     }
  73.  
  74.     private void joinChannel(String channel) throws Exception {
  75.         output.write("JOIN " + channel + "\n");
  76.         output.flush();
  77.     }
  78.  
  79.     private void sendMessage(String to, String message) throws Exception {
  80.         System.out.println("--> PRIVMSG " + to + " :" + message + "\n");
  81.         output.write("PRIVMSG " + to + " :" + message + "\n");
  82.         output.flush();
  83.     }
  84.  
  85.     private void verifyMOTD(String[] data) throws Exception {
  86.         if (data.length >= 2) {
  87.             /**
  88.              * 376 is the protocol number (end of MOTD)
  89.              */
  90.             if (data[1].equals("376")) {
  91.                 joinChannel(defaultChannel);
  92.                 sendMessage(defaultChannel, "wazup monkeys!");
  93.             }
  94.         }
  95.     }
  96.  
  97.     private boolean isCommand(String[] data) {
  98.         if (data.length >= 4) {
  99.             if (data[1].equals("PRIVMSG")) {
  100.                 String[] split = data[0].split("!");
  101.                 if (split[0].substring(1).equals(admin)) {
  102.                     if (data[3].substring(1, 2).equals("-")) {
  103.                         return true;
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         return false;
  109.     }
  110.  
  111.     private void verifyCommand(String[] data) throws Exception {
  112.         if (isCommand(data)) {
  113.             String from = data[2];
  114.             String command = data[3].substring(2);
  115.             switch (command) {
  116.                 case "admin":
  117.                     sendMessage(from, admin);
  118.                     break;
  119.                 case "version":
  120.                     sendMessage(from, VERSION);
  121.                     break;
  122.                 case "avapro":
  123.                     sendMessage(from, String.valueOf(Runtime.getRuntime().availableProcessors()));
  124.                     break;
  125.                 case "freememory":
  126.                     sendMessage(from, String.valueOf(Runtime.getRuntime().freeMemory()));
  127.                     break;
  128.                 case "totalmemory":
  129.                     sendMessage(from, String.valueOf(Runtime.getRuntime().totalMemory()));
  130.                     break;
  131.                 case "new":
  132.                     if (data.length < 9) {
  133.                         throw new WrongParametersException(from, "-new (network) (port) (nickname) (defaultChannel) (admin)");
  134.                     }
  135.                     IRCBot newBot = new IRCBot(data[4],Integer.parseInt(data[5]),data[6],data[7],data[8]);
  136.                     Thread threadNewBot = new Thread(newBot);
  137.                     threadNewBot.start();
  138.                     break;
  139.                 default:
  140.                     sendMessage(from, "exception -> uknown function: \"" + command + "\"");
  141.                     break;
  142.             }
  143.         }
  144.     }
  145.  
  146.     public void run() {
  147.         try {
  148.             connect();
  149.             sendLoginData();
  150.  
  151.             while (true) {
  152.                 String data = null;
  153.                 while ((data = input.readLine()) != null) {
  154.                     System.out.println("<-- " + data);
  155.                     String[] dataSplitted = data.split(" ");
  156.                     pingPong(dataSplitted);
  157.                     verifyMOTD(dataSplitted);
  158.                     try {
  159.                         verifyCommand(dataSplitted);
  160.                     } catch (WrongParametersException ex) {
  161.                         sendMessage(ex.from(), ex.toString());
  162.                     }
  163.                 }
  164.             }
  165.         } catch (Exception ex) {
  166.             System.out.println(ex.getMessage());
  167.         }
  168.     }
  169. }
Add Comment
Please, Sign In to add comment