Created
February 16, 2012 04:38
-
-
Save thefinn93/1842069 to your computer and use it in GitHub Desktop.
Simple Java IRC bawt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package irc; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
public class IRC { | |
public static String host = "seattle.uwirc.com"; // IRC server to connect to. | |
public static int port = 6667; // Port to connect to it on (probably 6667) | |
public static String nick = "FinnTestBawt"; // Nick to use | |
public static String channel = "#css161"; // Channel to join initially. Only one channel supported currently | |
public static String owner = "thefinn93"; // the nickname of the owner. Will only accept administrative commands from this nick | |
public static String quitmsg = "Thank you for using FinnBawt9000"; // What to say when we disconnect. | |
public static void main(String[] args) { | |
try{ // For when shit goes wrong | |
Socket socket = new Socket(host, port); // Open a socket to the server | |
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); | |
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
out.println("NICK " + nick); // tell them our nickname | |
out.println("USER " + nick + " " + nick + " " + host + " :" + nick); // ...and our idnet (which is basically our nickname a bunch | |
String line; | |
while((line = in.readLine()) != null) { // While there is still data coming in | |
System.out.println(line); // Print the data to the console for debugging purpose | |
String[] parsedline = line.split(" ",4); // Split it by the first four spaces. After that is the actual message (except for some cases, see next two lines). | |
if(parsedline[0].equals("PING")) { // If the message starts with PING. | |
out.println("PONG " + parsedline[1]); // Send back "PONG" followed by the ID thing they send us. | |
} else if(parsedline[1].equals("PRIVMSG")) {// If we get a message (Confusing thing about IRC: the PRIVMSG just means a message, could be a channel message or a private message) | |
String from = parsedline[0].split("!")[0].split(":")[1]; // Do some crazy | |
String msg = parsedline[3].split(":",2)[1]; | |
System.out.println(from + ": " + msg ); | |
if(parsedline[2].toLowerCase().equals(nick.toLowerCase())) { //Private Message | |
process(parsedline[3].substring(1), from, from, line, out); | |
} else { //Channel message | |
String chan = parsedline[2]; | |
// out.println("PRIVMSG " + parsedline[2] + " " + parsedline[3]); | |
if(msg.toLowerCase().contains(nick.toLowerCase())) { | |
String cmd = msg.split(nick,2)[1].trim(); | |
process(cmd, chan, from, line, out); | |
} | |
} | |
} else if(parsedline[1].equals("NOTICE")) { | |
System.out.println("NOTICE: " + line.split(":")[1]); | |
} else if(parsedline[1].equals("376")) { | |
out.println("JOIN " + channel); | |
out.println("PRIVMSG " + owner + " :hai"); | |
} | |
else { | |
System.out.println("Recevied " + parsedline[1] + ", not sure what to do about it."); | |
System.out.println(line); | |
} | |
} | |
} catch (UnknownHostException e) { | |
System.out.println("Unknown host: " + host); | |
System.exit(1); | |
} catch (IOException e) { | |
System.out.println("No I/O"); | |
System.exit(1); | |
} | |
} | |
public static void process(String fullcmd, String chan, String from, String in, PrintWriter out) { | |
if(fullcmd.charAt(0) == ',' || fullcmd.charAt(0) == ':') { | |
fullcmd = fullcmd.substring(1).trim(); | |
} | |
String cmd = fullcmd; | |
String args = ""; | |
if(fullcmd.contains(" ")) { | |
cmd = fullcmd.split(" ",2)[0]; | |
args = fullcmd.split(" ",2)[1]; | |
} | |
if(cmd.equals("quit")) { | |
if(isAdmin(from)) { | |
out.println("QUIT :" + quitmsg); | |
out.close(); | |
} else { | |
out.println("PRIVMSG " + chan + " :I'm sorry " + from + ", I'm afraid I can't do that. fuck off."); | |
} | |
} else if(cmd.equals("join")) { | |
if(isAdmin(from)) { | |
if(!args.isEmpty()) { | |
out.println("JOIN " + args); | |
} else | |
out.println("PRIVMSG " + from + " :Usage: \u0002" + nick + " join <#channel>\u0002"); | |
} else { | |
out.println("PRIVMSG " + chan + " :I'm sorry " + from + ", I'm afraid I can't do that. \u0002fuck off\u0002."); | |
} | |
} else if(cmd.equals("colors")) { | |
// The following have been blatantly stolen from http://www.jibble.org/javadocs/pircbot/constant-values.html#org.jibble.pircbot.Colors | |
String black = "\u000301"; | |
String blue = "\u000312"; | |
String bold = "\u0002"; | |
String brown = "\u000305"; | |
String cyan = "\u000311"; | |
String dark_blue = "\u000302"; | |
String dark_grey = "\u000314"; | |
String dark_green = "\u000303"; | |
String green = "\u000309"; | |
String light_grey = "\u000315"; | |
String magenta = "\u000313"; | |
String normal = "\u000f"; | |
String olice = "\u000307"; | |
String purple = "\u000306"; | |
String red = "\u000304"; | |
String reverse = "\u0016"; | |
String teal = "\u000310"; | |
String underline = "\u001f"; | |
String white = "\u000300"; | |
String yellow = "\u000308"; | |
out.println("PRIVMSG " + chan + " :" + dark_blue + "R" + blue + "A" + dark_green + "I" + green + "NBOW!" + normal + " lol"); | |
} else { | |
out.println("PRIVMSG " + chan + " :\u0002I has no idea what " + from + " is talking about...\u0002"); | |
} | |
} | |
public static boolean isAdmin(String user) { | |
if(user.toLowerCase().equals(owner.toLowerCase())) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment