Advertisement
Nickster258

Simple Java IRC bot class

Jul 27th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. import java.util.Date;
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. public class ircbot {
  6.  
  7.   String server;
  8.   int port;
  9.   String channel;
  10.   String username;
  11.   String password;
  12.   BufferedWriter writer;
  13.   BufferedReader reader;
  14.   Socket socket;
  15.  
  16.   public ircbot (String server, int port, String username) {
  17.     this.server = server;
  18.     this.port = port;
  19.     this.username = username;
  20.   }
  21.  
  22.   public ircbot (String server, int port, String username, String channel) {
  23.     this(server, port, username);
  24.     this.channel = channel;
  25.   }
  26.  
  27.   public ircbot (String server, int port, String username, String channel, String password) {
  28.     this(server, port, username, channel);
  29.     this.password = password;
  30.   }
  31.  
  32.   public String getServer() {
  33.     return server;
  34.   }
  35.  
  36.   public int getPort() {
  37.     return port;
  38.   }
  39.  
  40.   public String getUsername() {
  41.     return username;
  42.   }
  43.  
  44.   public String getChannel() {
  45.     return channel;
  46.   }
  47.  
  48.   public void connect () {
  49.     try {
  50.       this.socket = new Socket(server, port);
  51.       this.writer = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream( )));
  52.       this.reader = new BufferedReader( new InputStreamReader(  socket.getInputStream(  )));
  53.  
  54.       this.sendLine("NICK " + username + "\r\n");
  55.       this.sendLine("USER " + password + " 8 * : IRC Bot\r\n");
  56.  
  57.       String line = null;
  58.       while ((line = this.reader.readLine( )) != null) {
  59.         if (line.indexOf("004") >= 0) {
  60.           break;
  61.         } else if (line.indexOf("433") >= 0) {
  62.           System.out.println("Nickname is already in use.");
  63.           return;
  64.         }
  65.       }
  66.  
  67.       if (channel!=null){
  68.         this.sendLine("JOIN " + channel + "\r\n");
  69.       }
  70.       if (username!=null){
  71.         this.sendLine("PRIVMSG NICKSERV IDENTIFY " + password + "\r\n");
  72.       }
  73.  
  74.     } catch (IOException e) {
  75.       System.out.println(e);
  76.     }
  77.   }
  78.  
  79.   public void nick (String username) {
  80.     this.username = username;
  81.     this.sendLine("NICK " + username + "\r\n");
  82.   }
  83.  
  84.   public void pass (String password) {
  85.     this.password = password;
  86.     this.sendLine("USER " + password + " 8 * : \r\n");
  87.   }
  88.  
  89.   public void join (String channel) {
  90.     this.channel = channel;
  91.     this.sendLine("JOIN " + channel + "\r\n");
  92.   }
  93.  
  94.   public void leave () {
  95.     this.sendLine("PART\r\n");
  96.   }
  97.  
  98.   public void change (String channel) {
  99.     this.channel = channel;
  100.     this.leave();
  101.     this.join(channel);
  102.   }
  103.  
  104.   public String readLine () {
  105.     String blah = null;
  106.     try {
  107.       blah = this.reader.readLine();
  108.     } catch (IOException e) {
  109.       System.out.println(e);
  110.     }
  111.     return blah;
  112.   }
  113.  
  114.   public void sendLine (String line) {
  115.     try {
  116.       this.writer.write(line);
  117.       this.writer.flush();
  118.     } catch (IOException e) {
  119.       System.out.println(e);
  120.     }
  121.   }
  122.  
  123.   public static void main (String[] args) {
  124. // USE BELOW AS SAMPLE CODE
  125.  
  126.     ircbot blah = new ircbot("myserver", 6667, "mybot", "#mychannel", "mypass");
  127.     blah.connect();
  128.  
  129.     String line = null;
  130.     while ((line = blah.readLine( )) != null) {
  131.       System.out.println(line);
  132.       try {
  133.         if (line.contains("PING")) {
  134.           blah.sendLine("PONG " + line.substring(5) + "\r\n");
  135.         } else if (line.contains("hi bot")) {
  136.           String send = "PRIVMSG " + blah.channel + " hi there.\r\n";
  137.           blah.sendLine(send);
  138.           System.out.println("\nRECEIVED - " + line + "\nSENT - " + send);
  139.         } else {
  140.         }
  141.       } catch (IOException e) {
  142.         System.out.println(e);
  143.       }
  144.     }
  145.   }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement