PDA

View Full Version : (Java) Skeleton of an IRC Bot



The Soul
June 17th, 2010, 23:19
This is old and the design is poor, I only did this for learning purposes. Here it is...

IRCConnection class:

import java.net.Socket;
import java.io.IOException;
import java.util.regex.*;

public class IRCConnection extends Client implements Runnable {

InputOutput io;

Socket s;

public IRCConnection() throws java.net.UnknownHostException {
try {
s = new Socket(getServ(), getPort());
io = new InputOutput(s);
} catch(IOException ioe) {
ioe.printStackTrace();
}
}

public void run() {
String l = "";


try {
new IRCConnection();
io.bw().write("NICK "+getNick()+"\r\n");
io.bw().write("USER "+getNick()+" 8 * :TheSoul\r\n");
io.bw().flush();

while((l = io.br().readLine()) != null) {
System.out.println(l);
Pattern pingRegex = Pattern.compile("^PING", Pattern.CASE_INSENSITIVE);
Matcher ping = pingRegex.matcher(l);
io.bw().write("PONG "+channel+"\n");
io.bw().flush();
}

String s = ":"+getServ()+" 376 "+getNick();

if(l.startsWith(s)) {
io.bw().write("JOIN "+getChan()+"\r\n");
io.bw().flush();
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}


InputOutput class:

import java.io.*;
import java.net.Socket;

public class InputOutput {

BufferedReader br;

BufferedWriter bw;

public InputOutput(Socket s) throws IOException {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
}

public BufferedReader br() {
return br;
}

public BufferedWriter bw() {
return bw;
}
}

Client class:


public class Client {

public static void main(String[] args) throws java.net.UnknownHostException {
new Thread(new IRCConnection()).start();
}

String server = "godirc.com";

int port = 6667;

String nick = "SBot";

String channel = "#bot";


public String getServ() {
return server;
}

public int getPort() {
return port;
}

public String getNick() {
return nick;
}

public String getChan() {
return channel;
}
}

Fly
June 17th, 2010, 23:20
Fix your code tag.

The Soul
June 17th, 2010, 23:23
Fix your code tag.

I did...It just took me a while as the DNS refresh time is very slow for me and so it's taking me a while to do things.

Fly
June 17th, 2010, 23:25
I did...

Sorry, I most have posted to early.

Ninga Nun
June 18th, 2010, 07:55
Nice but i cant say much else cause im not a bot expert lol =p but anyway its good for learning on =]

Comboed
June 18th, 2010, 09:07
loled.


if(l.startsWith("PING")) {
io.bw().write("PONG "+l.substring(5)+"\r\n");
io.bw().flush();
}

The Soul
June 18th, 2010, 13:08
loled.


if(l.startsWith("PING")) {
io.bw().write("PONG "+l.substring(5)+"\r\n");
io.bw().flush();
}


I would have used regex but I couldn't be fucked.

Aaron
June 18th, 2010, 13:15
Very nice program The Soul:D

The Soul
June 18th, 2010, 13:37
Alright, updated so now it uses regex patterns.

Pie`
June 18th, 2010, 18:45
this is bad

The Soul
June 18th, 2010, 18:47
this is bad

Did I not say that it was?

Markus
June 18th, 2010, 18:47
this is bad
mtsmts

Pie`
June 18th, 2010, 18:50
package com.a.jotta.irc;

import java.util.ArrayList;

public class IRCMessage {

private String in;

public IRCMessage(String in) {
this.in = in;
}

public String setMessage(String in) {
in.replace((char)1, ' ').replace((char)2, ' ').replace((char)3, ' ');
return this.in = in.trim();
}

public String getSource() {
if(in.split(" ")[0].charAt(0) == 'P') {
return in.split(" ")[1];
} else {
return (in.split(" ")[2].startsWith("#")) ? in.split(" ")[2] : getSender();
}
}

public String getSender() {
return in.split("!")[0].replace(":", "");
}

public String getMode() {
return in.split(" ")[3].charAt(0) == '+' || in.split(" ")[3].charAt(0) == '-'? in.split(" ")[3] : null;
}

public String getModeOn() {
return (in.split(" ").length > 4) && (in.split(" ")[3].charAt(0) != ':') ? in.split(" ")[4] : null;
}

public String getCommand() {
return (in.split(" ")[0].charAt(0) == 'P') ? "PING" : in.split(" ")[1];
}

public boolean isBotCommand() {
return getMessage().charAt(0) == '$';
}

public boolean isAction() {
return getMessage().charAt(0) == (char)1 && getMessage().charAt(1) == 'A';
}

public boolean isUserPing() {
return getMessage().charAt(0) == (char)1 && getMessage().charAt(1) == 'P';
}

public boolean isWelcomeMessage() {
return getCommand().equals("001");
}

public long getPingTime() {
return Long.parseLong(getMessage().substring(5).trim());
}

public String getAction() {
return getMessage().substring(7).trim();
}

public String getCmd() {
return getMessage().split(" ")[0];
}

public String getMessage() {
return (in.indexOf(":", 1) > -1) ? in.substring(in.indexOf(":", 1)+1) : null;
}

public String getHost() {
return in.split(" ")[0].substring(getSender().length()+2);
}

public ArrayList<String> getMessageArgs() {
ArrayList<String> args = new ArrayList<String>();
for(String arg : getMessage().split(" ")) {
if(!(arg.trim().equals("")) && arg != null) {
args.add(arg);
}
}
if(args.size() > 0)
args.remove(0);
return args;
}

public String toString() {
return this.in;
}

}


as I said in irc

Anthony`
June 18th, 2010, 21:14
someone needs some polymorphic methodz 8)

Anthony
June 18th, 2010, 21:33
you all need to l2regex patterns.