Author Topic: TAHCBot v4!  (Read 6505 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
TAHCBot v4!
« on: November 06, 2005, 03:13:35 am »
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.

Code: [Select]
/*
 * 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);
}
 
}
« Last Edit: November 06, 2005, 03:55:47 am by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: TAHCBot v4!
« Reply #1 on: November 06, 2005, 05:48:36 am »
Use iago's persistantmap. It isn't hard.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: TAHCBot v4!
« Reply #2 on: November 06, 2005, 09:35:20 am »
Use iago's persistantmap. It isn't hard.

What is persistantmap?
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: TAHCBot v4!
« Reply #3 on: November 06, 2005, 11:02:34 am »
His class for reading .jdb files as if they were INI files.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: TAHCBot v4!
« Reply #4 on: November 06, 2005, 05:19:09 pm »
I suppose I could implement the persistantmap, but then I'd owe someone something, and it wouldn't all be written by me. =(. I'll get over it.

Anyhow, I put my little friend under the knife, and it came out as TAHCBot v4.1. It's more than one class now, easily expandable, and looks exactly like the old one (except I added a recieve loop has been forked message).

Also, I now parse emotes.

[INFO]  TAHCBot v4.0 by Joe[e2] loaded.
[BNET]  Connecting..
[BNET]  Connected!
[DEBUG] Streams created.
[BNET]  Authorization sent..
[DEBUG] Recieve loop forked from Main.
[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 Obfuscate is in the channel.
[BNCS]  User ETErNal_SolJA@Lordaeron is in the channel.
[BNCS]  User Dark_Shneider@Lordaeron is in the channel.
[BNCS]  User Thi3v3s.rec.b0t#2 is in the channel.
[BNCS]  User kills-365 is in the channel.
[BNCS]  User chris121212 is in the channel.
[BNCS]  User ThaClan#2 is in the channel.
[BNCS]  User partyboi85@Lordaeron is in the channel.
[BNCS]  User hudelsonaustin1@Lordaeron is in the channel.
[BNCS]  User ramybling@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 272 users in Chat, and 145877 users playing 30594 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:11 AM
[BNCS]  User familyguy-c34 has joined the channel.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: TAHCBot v4!
« Reply #5 on: November 06, 2005, 05:30:41 pm »
Live a little! Be open source. + It's public domain.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: TAHCBot v4!
« Reply #6 on: November 06, 2005, 10:04:46 pm »
yeah, it was a joke. =p

EDIT -
PersistantMap implemented. Settings file:
~/.tahcbot.ini (Linux/UNIX)
C:\Documents and Settings\[Username]\.tahcbot.ini (Windows, ugly)

EDIT -
[INFO]  TAHCBot v4.1 by Joe[e2] loaded.
[BNET]  Connecting to uswest.battle.net:6112..
[DEBUG] Streams created.
[BNET]  Connected!
[BNET]  Authorization sent..
[DEBUG] Recieve loop forked from Main.
[BNCS]  Logged on as Anonymous#22

Default settings = loaded.

EDIT -
Weird, RelativeFile seems to point the file to the directory the bot was run from, not the user's home directory. To the JavaDocs!
« Last Edit: November 07, 2005, 05:16:21 pm by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: TAHCBot v4!
« Reply #7 on: November 08, 2005, 09:30:47 am »
Using new File(System.getPreferences("user.home") + "...") instead of RelativeFile.

~/.tahcbot/settings.ini (Linux/UNIX)
C:\Documents and Settings\[Username]\.tahcbot\Settings.ini (Windows, ugly)
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: TAHCBot v4!
« Reply #8 on: November 09, 2005, 06:50:03 pm »
It's a single source code file, which makes it rock.
No, that makes it suck. Write a (G)UI and then show us the code. Or show us an example of a well designed OOP program that you've written. If you can't do one of the above, then you haven't gotten out of your VB habits. The biggest accomplishment that you could make would be to transition from VB to OOP.

Sorry if I'm being harsh =\
« Last Edit: November 09, 2005, 07:02:00 pm by Ender »

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: TAHCBot v4!
« Reply #9 on: November 09, 2005, 07:34:39 pm »
It's a single source code file, which makes it rock.
No, that makes it suck. Write a (G)UI and then show us the code. Or show us an example of a well designed OOP program that you've written. If you can't do one of the above, then you haven't gotten out of your VB habits. The biggest accomplishment that you could make would be to transition from VB to OOP.

Sorry if I'm being harsh =\


Anything that a user can see is a  UI. If it's graphical shouldn't affect the overall program. It's quite obvious he just started Java so I don't think it's fair to be askiing someone to write a "well designed OOP program", you want one? Search google.

From his source he isn't thinking VB anymore (which would be almost impossible since Java is almost fully OO) so please name these "habbits" you are referring to.

Hey, sorry if I'm being harsh.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: TAHCBot v4!
« Reply #10 on: November 09, 2005, 10:26:26 pm »
I changed it around a whole lot. Its 12 source files now, four of which are taken (nearly) directly from JavaOp (util classes, parsing written entirely by me =) ).
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: TAHCBot v4!
« Reply #11 on: November 12, 2005, 03:23:06 am »
You can actually chat from the console now!

Code: [Select]
package ui;

/*
 * TAHCBot v4
 * ReceiveLoop Class
 * Author: Joe[e2]
 *
 * Houses the timer that causes the bot to receive data
 */

import java.util.Timer;
import java.util.TimerTask;
import protocol.*;

public class ConsoleInputLoop {
    Timer timer;
    TAHCProtocol bot;
    static Console ConsoleUI = new Console();
    static ConsoleInput Input = new ConsoleInput();
   
    public ConsoleInputLoop(TAHCProtocol p_bot) {
    this.bot = p_bot;
        this.timer = new Timer();
        this.timer.schedule(new RecvTask(), 0, 50);
}

    class RecvTask extends TimerTask {
        public void run() {
        if(Input.hasData()) {
        bot.sendData(Input.getData() + "\r\n");
        }
        }
    }
}
I'd personally do as Joe suggests

You might be right about that, Joe.