0 Members and 1 Guest are viewing this topic.
//Sorry about the lack of comments, I'm a lazy documenter... =\ //Modified to add a few more comments =Pimport 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(); } }}
The example is in the spoiler tags If you need a better explanation of how it works, I can give that too though =P
Code: [Select]//Sorry about the lack of comments, I'm a lazy documenter... =\ //Modified to add a few more comments =Pimport 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(); } }}
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?
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}
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(); } }}