Java IRC bot

By supafreky on Jan 24, 2009

Connects to an IRC server you specify and idles. Basically this is a frame for an IRC bot.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.Socket;

public class Client implements Runnable {
   private String server;

   protected void server(String server) {
      this.server = server;
   }

   protected String server() {
      return this.server;
   }

   private int port;

   protected void port(int port) {
      this.port = port;
   }

   protected int port() {
      return this.port;
   }

   private String nick, user, name;

   protected void nick(String nick) {
      this.nick = nick;
   }

   protected String nick() {
      return this.nick;
   }

   protected void user(String user) {
      this.user = user;
   }

   protected String user() {
      return this.user;
   }

   protected void name(String name) {
      this.name = name;
   }

   protected String name() {
      return this.name;
   }

   private boolean isActive;

   protected void isActive(boolean bool) {
      this.isActive = bool;
   }

   protected boolean isActive() {
      return this.isActive;
   }

   public static void main(String args[]) {
      System.out.println("Starting program.");
      try {
         new Client().start();
      } catch (java.io.IOException e) {
      }
   }

   protected Client() {
      System.out.println("Initializing.");
      this.server("irc.kingskrown.com");
      this.port(6667);
      this.nick("oak");
      this.user("svs");
      this.name("java");
   }

   private Socket socket;

   private BufferedReader in;

   private BufferedWriter out;

   protected void start() throws java.io.IOException {
      this.socket = new Socket(this.server(), this.port());
      this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
      if (socket.isConnected()) {
         out.write("NICK " + this.nick() + "\r\n");
         out.write("USER " + this.user() + " \"\" \"\" :" + this.name() + "\r\n");
         out.flush();
         this.isActive(true);
         System.out.println("Starting thread.");
         new Thread(this).start();
      }
   }

   public void run() {
      String buffer;
      while (this.isActive()) {
         try {
            while ((buffer = in.readLine()) != null) {
               if (buffer.startsWith("PING")) {
                  out.write("PONG " + buffer.substring(5) + "\r\n");
                  out.flush();
               }
            }
         } catch (java.io.IOException e) {
         }
      }
   }
}

Comments

Sign in to comment.
R3ido101   -  Feb 24, 2016

What is the best way to make commands? in a new class or just keep it all in one?

supafreky  -  Jan 03, 2017

Separate files for cleanliness and organization is always preferred, although it's merely at your discretion.

Sign in to comment

techkid6   -  Aug 26, 2012

I hate starting conversations from a while ago, but I have to thank you, you just saved me big time! Forget PircBot :D

 Respond  
Sorasyn   -  May 08, 2012

No joke, haha. Didn't realize the first comment was about 3 years ago. Lol

 Respond  
Joshuaxiong1   -  May 08, 2012

Lol wow this is long time ago.

 Respond  
Sorasyn   -  May 08, 2012

The thread is necessary for any advanced use of sockets. Otherwise, that much data flowing into another object outside of the console window would freeze the component.

 Respond  
alphakilos   -  Dec 29, 2011

It is rather irrelevant to spawn a thread when you only have one bot going in an 'infinite' loop. I could see the point of the thread if you were managing multiple bots on different channels and had command delegates each thread can access, but if this is a template for that, there is a bunch missing to start anyone off.

 Respond  
EnKrypt   -  Dec 08, 2011

Good job kingskrown, pretty good for starters.
this could get anyone started in a few minutes.
I was looking for a good IRC connection tutorial for a friend of mine in Java seeing that mine was too big and complicated and i was too lazy to break it down to make him understand.
This might just be what i was looking for :D

 Respond  
Joshuaxiong1   -  Jul 10, 2011

I remember kingskrown. kingskrown is a bad guy.

 Respond  
AlexRapso   -  Jul 10, 2011

perfect for starting me off, no good tutorials for sockets. Thanks!

 Respond  
sunslayer   -  Jan 18, 2011

it doesn't look like there are any built in commands, you'd have to make your own.

 Respond  
koolaidman   -  Jan 18, 2011

whats the commands?

 Respond  
Joshuaxiong1   -  Nov 16, 2009

It work. But I can't see any logs of what it is receiving. etc ctcp/msg/notice

 Respond  
miniCruzer   -  Jul 06, 2009

I'd use this if I could figure out how. I've written Java code in the past, but I'm not sure what to do lol.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.