Author Topic: Random Java Libs  (Read 3944 times)

0 Members and 1 Guest are viewing this topic.

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
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 =)
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 =)
« Last Edit: July 24, 2011, 03:15:36 pm by cooliojazz »
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random Java Libs
« Reply #1 on: July 24, 2011, 09:23:32 am »
Great CoolioJazz, could you perhaps add a sample so we can know how to use it? Thanks :)

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Random Java Libs
« Reply #2 on: July 24, 2011, 03:06:37 pm »
The example is in the spoiler tags ;) If you need a better explanation of how it works, I can give that too though =P
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random Java Libs
« Reply #3 on: July 24, 2011, 04:57:35 pm »
The example is in the spoiler tags ;) If you need a better explanation of how it works, I can give that too though =P

I didn't notice it, perhaps because it is code inside spoiler.

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();
        }
    }
}

Just post it all like this, the code tags already shorten it, so it's not too big.

EDIT: And now that I read the code, it looks so easy to create a JIRC Object and connect to IRC! Awesome job! +1
« Last Edit: July 24, 2011, 04:58:11 pm by ephan »

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Random Java Libs
« Reply #4 on: July 24, 2011, 08:24:00 pm »
Yeah, but potentially, I was going to put quite a few libs in that one post, and with many examples, I just wanted to collapse them... Meh, w/e, it works =P Glad you like it though!  See anything you think it needs?
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random Java Libs
« Reply #5 on: July 27, 2011, 08:59:57 am »
Yeah, but potentially, I was going to put quite a few libs in that one post, and with many examples, I just wanted to collapse them... Meh, w/e, it works =P Glad you like it though!  See anything you think it needs?

Is there an easy way to inherit JIRC and do thins from there?

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Random Java Libs
« Reply #6 on: July 29, 2011, 03:04:45 pm »
Well, I guess I'm not exactly sure what you're asking, but as far as I can tell, thats how you would do it without a "nameless" class
Code: [Select]
import jirc.*;

class myJIRC extends JIRC {

     myJIRC(String server, String nickname, String realname) {
          super(server, nickname, realname);
     }

     protected void onConnect() {
          //Event stuff!
          joinChannel("IRP"); //Join channel #IRP at startup!
     }
     //etc etc
}
So if that's not it, I'm not really sure what you're asking... =P
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random Java Libs
« Reply #7 on: July 29, 2011, 03:14:21 pm »
Oh sorry you're a Java programmer, you guys don't say "inherit", you say "extend", but yeah that's what I meant, cool!

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Random Java Libs
« Reply #8 on: August 05, 2012, 09:58:00 pm »
In light of the contest, I figured I might post all the amazing updates I've made to JIRC if anyone wants to use java and make their life easy ;D

Updated example:
Code: [Select]
import com.up.jirc.*;
import java.io.*;
import java.text.DateFormat;
import java.util.*;
import java.util.regex.*;

public class Test {
   
    static PrintStream sout = System.out;
    static BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
    static String ActiveIRCChannel = "";
   
    public static void main(String args[]) {
        JIRC omni = new JIRC("withg.us.to", "JIRC_Test", "Test Implementation of JIRC", 6667) {
            protected void onConnect() {
                sout.println("Connected!");
            }

            protected void onPingged(String server) {
                sout.println("Ping? Pong!");
            }

            protected void onServerMessage(String message) {
                try {
                    ServerResponse sr = ServerResponse.parseString(message);
                    switch (sr.numeric) {
                        case MOTD:
                            sout.println("MOTD " + sr.response);
                            break;
                        case MOTDSTART:
                            sout.println("Message of the Day, " + sr.server);
                            break;
                        case ENDOFMOTD:
                            sout.println("End of MOTD");
                            break;
                        default:
                            sout.println(sr.numeric.toString() + ": " + sr.response);
                            break;
                    }
                } catch (Exception e) {
                    sout.println("[" + DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date()) + "] - " + message);
                }
            }

            protected void onChannelMessageReceive(String channel, String nick, String message) {
                sout.println("[" + DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date()) + "] #" + channel + ": <" + nick + "> " + message);
            }

            protected void onPrivateMessageReceive(String nick, String message) {
                sout.println("[" + DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date()) + "] <" + nick + "> " + message);
            }

            protected void onChannelMemberPart(String channel, String nick) {
                sout.println(channel + ": " + nick + " has left");
            }

            protected void onChannelMemberJoin(String channel, String nick) {
                sout.println(channel + ": " + nick + " has joined");
            }

            protected void onChannelMemberQuit(String nick, String message) {
                sout.println(nick + " has quit (" + message + ")");
            }
        };
        omni.setAddColors(true);
//        BLACKF "\033[30m"
//        GREYF "\033[1;30m"
//        REDF "\033[31m"
//        LIGHTREDF "\033[1;31m"
//        GREENF "\033[32m"
//        LIGHTGREENF "\033[1;32m"
//        YELLOWF "\033[33m"
//        LIGHTYELLOWF "\033[1;33m"
//        BLUEF "\033[34m"
//        LIGHTBLUEF "\033[1;34m"
//        MAGENTAF "\033[35m"
//        LIGHTMAGENTAF "\033[1;35m"
//        CYANF "\033[36m"
//        LIGHTCYANF "\033[1;36m"
//        WHITEF "\033[37m"
//        LIGHTWHITEF "\033[1;37m"
//        RESET "\033[0m"
        omni.setColors(new String[] {"\033[37m", "\033[30m", "\033[34m", "\033[32m", "\033[1;31m", "\033[31m", "\033[35m", "\033[1;31m", "\033[1;33m", "\033[1;32m", "\033[36m", "\033[1;36m", "\033[1;34m", "\033[1;35m", "\033[1;30m", "\033[1;30m", "\033[0m"});
        omni.start();
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);
        try {
            while (true) {
                while (sin.ready()) {
                    String msg = sin.readLine();
                    if (msg != null) {
                        Matcher m;
                        if ((m = JoinCommand.matcher(msg)).find(0)) {
                            omni.joinChannel(m.group(1).replace("#", ""));
                        } else if ((m = PartCommand.matcher(msg)).find(0)) {
                            omni.partChannel(ActiveIRCChannel);
                        } else if ((m = NickCommand.matcher(msg)).find(0)) {
                            omni.changeNick(m.group(1));
                        } else if ((m = OpCommand.matcher(msg)).find(0)) {
                            omni.opChannelMember(ActiveIRCChannel, m.group(1));
                        } else if ((m = DeOpCommand.matcher(msg)).find(0)) {
                            omni.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 = omni.getChannelList().get(JIRC.inChannelList(omni.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)) {
                            omni.sendRawMessage(m.group(1));
                        } else if ((m = ExitCommand.matcher(msg)).find(0)) {
                            sout.close();
                            sin.close();
                            System.exit(0);
                        } else if (msg.startsWith("/")) {

                        } else {
                            omni.sendMessage(ActiveIRCChannel, msg);
                            sout.println("#" + ActiveIRCChannel + ": <" + omni.getNick() + "> " + msg);
                        }
                    }
                }
                Thread.sleep(10);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Major things:
-Package change, now is com.up.jirc instead of just jirc
-Color support
-Server response parsing.  Does not include every single IRC response numeric, or even most of them, but it does have most of the more common ones, I probably will add the rest eventually =P
-And then just fixing a lot of stupid various connection things
-Also, the constructor now requires the port to connect to

I dislike how much time I spent javadoc-ing though...  And I still left a lot un-done! D= That's why I like personal projects, documentation is for losers ;P
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)