Arctic_Wolfy

IRC Bot Server

Nov 6th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. package com.bbw2.ircBot;;
  2.  
  3. import javax.swing.*;
  4. import java.io.*;
  5. import java.net.Socket;
  6.  
  7. /**
  8.  * Created on 10/14/2015.
  9.  */
  10. public class Server {
  11.     private String serverName, nick, login, pass;
  12.     private Channel[] channels;
  13.     private Socket socket;
  14.     private BufferedWriter writer;
  15.     private BufferedReader reader;
  16.  
  17.     private JTabbedPane serverTabs;
  18.  
  19.     public Server(String serverName, String nick, String login, String pass, Channel... channels){
  20.         this.serverName = serverName;
  21.         this.nick = nick;
  22.         this.login = login;
  23.         this.pass = pass;
  24.         this.channels = channels;
  25.  
  26.         this.serverTabs = new JTabbedPane();
  27.     }
  28.  
  29.     public JTabbedPane getServerTabs() {return serverTabs;}
  30.     public String getServerName() {return serverName;}
  31.  
  32.     public void init() throws IOException {
  33.         socket = new Socket(serverName,6667);
  34.         writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  35.         reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  36.     }
  37.  
  38.     public boolean start() throws IOException {
  39.         writer.write("NICK " + nick + "\r\n");
  40.         writer.write("USER " + login + " . . .\r\n");
  41.         writer.flush();
  42.  
  43.         String line;
  44.         while ((line = reader.readLine()) != null){
  45.             System.out.println(line);
  46.             if (line.startsWith("PING ")){
  47.                 writer.write("PONG " + line.substring(5) + "\r\n");
  48.                 writer.flush();
  49.             } else if (line.contains("004")){
  50.                 if (pass!=null) {
  51.                     writer.write("PRIVMSG NickServ IDENTIFY " + pass);
  52.                     writer.flush();
  53.                 }
  54.                 return true;
  55.             } else if (line.contains("443")){
  56.                 System.out.println("Nick already in use.");
  57.                 return false;
  58.             }
  59.         }
  60.  
  61.         return false;
  62.     }
  63.  
  64.     public void joinChannels() throws IOException {
  65.         for (Channel channel : channels) {
  66.             channel.setServer(this);
  67.             writer.write("JOIN " + channel.getChannelName() + "\r\n");
  68.         }
  69.         writer.flush();
  70.  
  71.         String line;
  72.         Bot bot = new Bot(this);
  73.         while ((line = reader.readLine())!=null){
  74.             if (line.startsWith("PING ")){
  75.                 writer.write("PONG " + line.substring(5) + "\r\n");
  76.                 writer.flush();
  77.             } else {
  78.                 System.out.println(line);
  79.  
  80.                 String[] split0 = line.split(":");
  81.                 String[] split1 = split0[1].split(" ");
  82.                 String code = split1[1];
  83.  
  84.                 if (!"005 251 252 254 255 265 266 353 366 372 375 376 NOTICE MODE JOIN".contains(code) && !code.contains(" "))
  85.                     bot.translate(line);
  86.  
  87.             }
  88.         }
  89.     }
  90.  
  91.     public String getNick() {
  92.         return nick;
  93.     }
  94.  
  95.     public Channel[] getChannels() {return channels;}
  96.     public BufferedWriter getWriter() {return writer;}
  97. }
Advertisement
Add Comment
Please, Sign In to add comment