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) {
}
}
}
}
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.
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
Separate files for cleanliness and organization is always preferred, although it's merely at your discretion.