Yup. "Learn a real language!" Java.
[INFO] TAHCBot v4.0 by Joe[e2] loaded.
[BNET] Connecting..
[BNET] Connected!
[DEBUG] Streams created.
[BNET] Sent authorization info.
[BNCS] Logged on as JoeBot-Test
[BNCS] Joined channel PuBLiC CHaT 1
[BNCS] User Matt is in the channel.
[BNCS] User Flushot is in the channel.
[BNCS] User !~^Ozzy^~! is in the channel.
[BNCS] User SoLo-SouL#2 is in the channel.
[BNCS] User EvilBot-X is in the channel.
[BNCS] User ThaClan#2 is in the channel.
[BNCS] User Obfuscate is in the channel.
[BNCS] User Thug4Life]T4L[@Lordaeron is in the channel.
[BNCS] User Thi3v3s.rec.b0t#2 is in the channel.
[BNCS] User ElmO-4PrEsidenT@Lordaeron is in the channel.
[BNCS] User Wiems is in the channel.
[BNCS] User Celski is in the channel.
[BNCS] User ryanipoo@Lordaeron is in the channel.
[BNCS] User SnuG-KiKiBeaR@Lordaeron is in the channel.
[BNCS] User CrEaMoFdAcRoP is in the channel.
[BNCS] User mccola@Lordaeron is in the channel.
[BNCS] User JoeBot-Test is in the channel.
[BNET] Info: Welcome to Battle.net!
[BNET] Info: This server is hosted by AT&T.
[BNET] Info: There are currently 210 users in Chat, and 251382 users playing 53433 games on Battle.net.
[BNET] Error: Chatting with this game is restricted to the channels listed in the channel menu.
[BNET] Info: Last logon: Sun Nov 6 9:05 AM
I don't handle whispers (because nobody likes me enough to whisper me) and configuration is hard coded. I'm pretty sure you can figure out the configuration, and live with the whispers not being handled. It's a single source code file, which makes it rock.
/*
* TAHCBot Java
* Main Class
* Author: Joe[e2]
*
* This is the entry-point of TAHCBot
*/
/*
* Credit:
* Every single byte of this program was written by yours truely (and it shows!)
* I owe nobody anything.
* Special thanks to iago for teaching me Java a long time ago.
* Special thanks to Newby for bitching at me about using VB.
*/
import java.net.*;
import java.io.*;
public class Main {
static Socket sckBNET;
static BufferedReader sckBNET_IS;
static OutputStream sckBNET_OS;
static String sServer = "uswest.battle.net";
static int iPort = 6112;
static String sUsername = "JoeBot-Test";
static String sPassword = "Haha, no.";
public static void main(String args[]) {
out(3, "TAHCBot v4.0 by Joe[e2] loaded.");
out(4, "Connecting..");
try {
sckBNET = new Socket(sServer, iPort);
}catch(IOException e) {
out(4, "Socket error: " + e.toString());
}
out(4, "Connected!");
makeStreams();
sendAuth();
doRecvLoop();
}
private static void out(int iType, String sMessage) {
switch(iType) {
case 0: System.out.println(" " + sMessage); break;
case 1: System.out.println("[DEBUG] " + sMessage); break;
case 2: System.out.println("[ERROR] " + sMessage); break;
case 3: System.out.println("[INFO] " + sMessage); break;
case 4: System.out.println("[BNET] " + sMessage); break;
case 5: System.out.println("[BNCS] " + sMessage); break;
}
}
private static void makeStreams() {
try {
sckBNET_OS = sckBNET.getOutputStream();
sckBNET_IS = new BufferedReader(new InputStreamReader(sckBNET.getInputStream()));
}catch(IOException e) {
out(2, "Could not create input/output stream. " + e.toString());
}
out(1, "Streams created.");
}
private static void sendAuth() {
sendData("\3");
sendData(sUsername + "\r\n");
sendData(sPassword + "\r\n");
out(4, "Sent authorization info.");
}
private static void sendData(String data) {
for(int i = 0; i < data.length(); i++) {
try {
sckBNET_OS.write(data.charAt(i));
}catch(IOException e) {
out(2, "Unable to write to output stream. " + e.toString());
}
}
try {
sckBNET_OS.flush();
}catch(IOException e) {
out(2, "Unable to flush output stream. " + e.toString());
}
}
private static void doRecvLoop() {
String data;
try {
while ((data = sckBNET_IS.readLine()) != null) {
parse(data);
}
}catch(IOException e) {
out(2, "Unable to recieve data. " + e.toString());
}
}
private static void parse(String data) {
String splt[] = data.split(" ");
try {
switch(Integer.parseInt(data.substring(0, 4))) {
case 1001: {
out(5, "User " + splt[2] + " is in the channel.");
break;
}
case 1002: {
out(5, "User " + splt[2] + " has joined the channel.");
break;
}
case 1003: {
out(5, "User " + splt[2] + " has left the channel.");
break;
}
case 1005: {
out(5, "<" + splt[2] + "> " + getTalkMessage(data));
break;
}
case 1007: {
out(5, "Joined channel " + getInfoMessage(data));
break;
}
case 1018: {
out(4, "Info: " + getInfoMessage(data));
break;
}
case 1019: {
out(4, "Error: " + getInfoMessage(data));
break;
}
case 2010: {
out(5, "Logged on as " + splt[2]);
break;
}
case 2000: {
// keepalive
break;
}
default: {
out(4, "Unhandled packet: " + data);
}
}
}catch(NumberFormatException e) {
//out(2, "Unable to parse number " + data.substring(0, 4) + ". " + e.toString());
// It weeds out all the bad packets ("Connection from [IP]") anyhow
// Ignore it
}catch(StringIndexOutOfBoundsException e) {
//out(2, "String index out of bounds. " + e.toString());
// This happens when we try to parse a CRLF. Ignore it.
}
}
private static String getTalkMessage(String data) {
String splt[] = data.split(" ");
String ret = data.substring(splt[0].length() + splt[1].length() + splt[2].length() + splt[3].length() + 5);
return ret.substring(0, ret.length() - 1);
}
private static String getInfoMessage(String data) {
String splt[] = data.split(" ");
String ret = data.substring(splt[0].length() + splt[1].length() + 3);
return ret.substring(0, ret.length() - 1);
}
}