16
Computer Projects and Ideas / Random Java Libs
« on: July 23, 2011, 02:52:04 pm »
I just finished converting the back end of my IRC bot into an IRC library, JIRC. It's a fairly easy way to add IRC stuff to anything. I only did the javadoc for the main class, so... =P The attached zip has the jar, javadoc, and source. Here's an example of how to use it (This is a simple command line IRC client, basically) Freel free to comment on things it needs or things you find odd =)
Additionally, notice the "s" in "Java Libs". That's because I also plan on converting the backend of TFE into a general purpose TI File lib, and maybe some others if I feel like it. This is it atm though =)
Spoiler For Example Below:
Code: [Select]
//Sorry about the lack of comments, I'm a lazy documenter... =\
//Modified to add a few more comments =P
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class Test {
static PrintStream sout = System.out;
static BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[]) {
JIRC jirc = new JIRC("withg.us.to", "JIRC_Test", "JIRC Test Bot") {
//I just used a nameless class (whatever the correct term is, idk), you probably would want to create a seperate extending class for code neatness, but this is small, so...
//All the methods that need to be implemented are the event handlers. I tried to make the event names pretty obvious, but...
protected void onConnect() { //When JIRC connects to IRC (Or reconnects, it auto reconnects on disconnect)
sout.println("Connected!");
joinChannel("test");
}
protected void onPingged(String server) { //When you are pinged
sout.println("Ping? Pong!");
}
protected void onServerMessage(String message) { //When the server sends you a message
Date now = new Date();
sout.println("-[" + now.toGMTString() + "]- " + message);
}
protected void onChannelMessageReceive(String channel, String nick, String message) { //When you receive a message directed at a channel you are in
sout.println("[" + now.toGMTString() + "] #" + channel + ": <" + nick + "> " + message);
}
protected void onPrivateMessageReceive(String nick, String message) { //When you receive a private message directed at you
sout.println("[" + now.toGMTString() + "] <" + nick + "> " + message);
}
protected void onChannelMemberPart(String channel, String nick) { //When someone leaves a channel you are in
sout.println(channel + ": " + nick + " has left");
}
protected void onChannelMemberJoin(String channel, String nick) { //When someone joins a channel you are in
sout.println(channel + ": " + nick + " has joined");
}
protected void onChannelMemberQuit(String nick, String message) { //When someone who is in one or more channels with you quits
sout.println(nick + " has quit (" + message + ")");
}
};
jirc.start(); //Start JIRC
Pattern JoinCommand = Pattern.compile("/(?:join|j) (.+)", Pattern.CASE_INSENSITIVE);
Pattern PartCommand = Pattern.compile("/(?:part|p)", Pattern.CASE_INSENSITIVE);
Pattern CACCommand = Pattern.compile("/cac (.+)", Pattern.CASE_INSENSITIVE);
Pattern NickCommand = Pattern.compile("/(?:nick|n) (.+)", Pattern.CASE_INSENSITIVE);
Pattern OpCommand = Pattern.compile("/op (.+)", Pattern.CASE_INSENSITIVE);
Pattern DeOpCommand = Pattern.compile("/deop (.+)", Pattern.CASE_INSENSITIVE);
Pattern RawCommand = Pattern.compile("/raw (.+)", Pattern.CASE_INSENSITIVE);
Pattern ListCommand = Pattern.compile("/(?:list|l) #(.+)", Pattern.CASE_INSENSITIVE);
Pattern ExitCommand = Pattern.compile("/(?:exit|e)", Pattern.CASE_INSENSITIVE); //Because I'm lazy, so I used regex to detect commands etc
String ActiveIRCChannel = ""; //To keep track of the channel you are currently talking in
try {
while (true) { //Loop for console input
while (sin.ready()) {
String msg = sin.readLine();
if (msg != null) {
Matcher m;
if ((m = JoinCommand.matcher(msg)).find(0)) {
jirc.joinChannel(m.group(1).replace("#", ""));
} else if ((m = PartCommand.matcher(msg)).find(0)) {
jirc.partChannel(ActiveIRCChannel);
} else if ((m = NickCommand.matcher(msg)).find(0)) {
jirc.changeNick(m.group(1));
} else if ((m = OpCommand.matcher(msg)).find(0)) {
jirc.opChannelMember(ActiveIRCChannel, m.group(1));
} else if ((m = DeOpCommand.matcher(msg)).find(0)) {
jirc.deOpChannelMember(ActiveIRCChannel, m.group(1));
} else if ((m = CACCommand.matcher(msg)).find(0)) {
ActiveIRCChannel = m.group(1).replace("#", "");
sout.println("Changed the active channel to " + ActiveIRCChannel);
} else if ((m = ListCommand.matcher(msg)).find(0)) {
Channel curchan = jirc.getChannelList().get(JIRC.inChannelList(jirc.getChannelList(), m.group(1)));
Person[] People = curchan.getAllMembers();
for (int x = 0; x < People.length; x++) {
sout.println(People[x].getName() + " (" + People[x].getModes() + ") Active: " + People[x].isActive());
}
} else if ((m = RawCommand.matcher(msg)).find(0)) {
jirc.sendRawMessage(m.group(1));
} else if ((m = ExitCommand.matcher(msg)).find(0)) {
sout.close();
sin.close();
System.exit(0);
} else if (msg.startsWith("/")) {
} else {
jirc.sendMessage(ActiveIRCChannel, msg);
sout.println("#" + ActiveIRCChannel + ": <" + jirc.getNick() + "> " + msg);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Additionally, notice the "s" in "Java Libs". That's because I also plan on converting the backend of TFE into a general purpose TI File lib, and maybe some others if I feel like it. This is it atm though =)