• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Vote for the theme of Hive's HD Modeling Contest #7! Click here to vote! - Please only vote if you plan on participating❗️

C++ Help

Status
Not open for further replies.
Level 22
Joined
Dec 31, 2006
Messages
2,216
Hello there, O' Mighty reader!
I'm trying to make my own chat bot (Some kind of 8-ball), but I'm having trouble finding a way to read what's being said in the chat. I know it's possible to read the whole script for the chat, but I don't know how. And other solutions are also accepted, as long as it doesn't cost anything.

Any help is greatly appreciated!
 
Level 11
Joined
Feb 14, 2009
Messages
884
Hmm, I have an idea, but I don't know how you can implement it (I don't know C#,C++). Create a variable that will store the chat's length (have a variable that will store the whole chat, and then count the characters in it or something like that). When something new is written, have a second variable store the new chat's length. Then, create a 4th variable containing only the last thing that was typed (intNewChat - intOldChat). After that, check if that new variable contains any keywords etc... Here's a layout:

Code:
strChat
intOldChat
intNewChat
strLatestTransmission

> App initialization
> Save current chat in strChat
> Count the number of characters in strChat and store it in intOldChat
> A client submits a new message
> Count the number of chars in the chat including the submitted message
> Set strLatestTransmission to start from (intOldChat + 1) and end at (intNewChat)
> Scan strLatestTransmission

I hope this helped :)
 
Level 11
Joined
Feb 14, 2009
Messages
884
I found this while searching the internet. It searches for a word inside a string, so if you need to scan for a phrase you'll just have to create a new str variable and add words as you find them. I didn't get many relevant results, I hope you are luckier than me :)
 
Level 9
Joined
May 10, 2009
Messages
542
I will learn C!! Maybe. And its going to be like Galaxy, the scripting language used in SC2!! Omfg? You can make your own applications in Sc2 (uh...can u do that in C?)!!
 
Level 9
Joined
Jan 22, 2009
Messages
346
Is there a really simple tutorial on how to program C++? Many seem to confuse me and are rather old.

Making games in C++ is really excellent and is best suited to Windows gaming; it is a recommendation in making games with this programming language than the .NET languages.
 
Level 9
Joined
Jan 22, 2009
Messages
346
WINDOWS...no, I won't make anything for the vile filth! Work for Mac??
Yes, C++ is for Windows (Win32 applications). It wouldn't work in any other operating system. For the .Net programming languages, it can be crossed-platformed with the help of Mono.

A Mac programming language, I'm not so sure... I can't really find one, but many Windows based programming languages.
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
Yes, C++ is for Windows (Win32 applications). It wouldn't work in any other operating system. For the .Net programming languages, it can be crossed-platformed with the help of Mono.

A Mac programming language, I'm not so sure... I can't really find one, but many Windows based programming languages.

wtf?

c++ is a cross-platform language. Never heard of the all-known GNU g++ compiler for unix?
Visual c++ only works under windows, but visual c++ as well as .net programming sucks ass in the first place.

QT also tends to be buggy under windows in my experience...

I will learn C!! Maybe. And its going to be like Galaxy, the scripting language used in SC2!! Omfg? You can make your own applications in Sc2 (uh...can u do that in C?)!!
Galaxy is going to be like C, not the other way around. And no you cannot make your own applications in SC2, that would be both pointless and dangerous.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
I have no idea why you are using C++ for this—most other languages are better suited.

Anyhow, here's the code to log into vBulletin (written in Java) and to request/parse the gochat.php page from it: (Don't mind the Client stuff, that's specific to my program).

Code:
package components;

import java.security.MessageDigest;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.net.Socket;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.Hashtable;

import main.Client;

public class VBulletinHandler {
    private static String bbsessionhash;
    private static String encodePOST(String field,String val) {
        try {
            return URLEncoder.encode(field,"UTF-8") + "=" + URLEncoder.encode(val,"UTF-8");
        } catch (Exception ex) {
            ex.printStackTrace(Client.theConsole);
            return null;
        }
    }
    public static String md5(String in) {
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(in.getBytes(),0,in.length());
            String s = new BigInteger(1,m.digest()).toString(16);
            if (s.length() == 31) {
                s = "0" + s;
            }
            return s;
        } catch (Exception ex) {
            ex.printStackTrace(Client.theConsole);
            return null;
        }
    }
    public static boolean login(String username, String password) {
        try {
            Socket s = new Socket("www.hiveworkshop.com",80);
            java.io.PrintWriter pw = new java.io.PrintWriter(s.getOutputStream(),false);
            String data = encodePOST("do","login") + "&" +
                          encodePOST("securitytoken","guest") + "&" +
                          encodePOST("vb_login_username",username) + "&" +
                          encodePOST("vb_login_password","") + "&" +
                          encodePOST("vb_login_md5password",md5(password)) + "&" +
                          encodePOST("vb_login_md5password_utf","");
            pw.print("POST /forums/login.php?do=login HTTP/1.1\r\n");
            pw.print("Host: www.hiveworkshop.com\r\n");
            pw.print("Content-Type: application/x-www-form-urlencoded\r\n");
            pw.print("Content-Length: "+data.length()+"\r\n");
            pw.print("\r\n");
            pw.print(data);
            pw.flush();
            InputStreamReader isr = new InputStreamReader(s.getInputStream());
            BufferedReader br = new BufferedReader(isr);
            String line;
            String target = "bbsessionhash=";
            while ((line=br.readLine())!=null) {
                int ind = line.indexOf(target);
                if (ind!=-1) {
                    bbsessionhash = line.substring(ind).split(";")[0];
                    break;
                }
            }
            s.close();
            br.close();
            pw.close();
            isr.close();
            return bbsessionhash!=null;
        } catch (Exception ex) {
            ex.printStackTrace(Client.theConsole);
            return false;
        }
    }
    public static String requestPage(String page) {
        try {
            Socket s = new Socket("www.hiveworkshop.com",80);
            s.setReceiveBufferSize(600000);
            java.io.PrintWriter pw = new java.io.PrintWriter(s.getOutputStream(),false);
            pw.print("GET " + page + " HTTP/1.1\r\n");
            pw.print("Host: www.hiveworkshop.com\r\n");
            pw.print("Cookie: "+bbsessionhash+"\r\n");
            pw.print("Referer: http://www.hiveworkshop.com/\r\n");
            pw.print("\r\n");
            pw.flush();
            InputStreamReader isr = new InputStreamReader(s.getInputStream());
            BufferedReader br = new BufferedReader(isr);
            while (!isr.ready()) {
                Thread.sleep(10);
            }
            for (int i=0;i<11;i++) {
            	br.readLine();
            }
            int size = 1;
            try {
            	size = Integer.parseInt(br.readLine(),16);
            } catch (Exception ex) {
            	ex.printStackTrace(Client.theConsole);
            }
            char[] cbuf = new char[size];
            for (int i=0;i<cbuf.length;i++) {
            	cbuf[i]=(char)br.read();
            }
            s.close();
            pw.close();
            br.close();
            isr.close();
            return new String(cbuf);
        } catch (Exception ex) {
            ex.printStackTrace(Client.theConsole);
            return null;
        }
    }
    public static Hashtable<String,String> getGoChatData(String in) {
        String line = "";
        try {
            Hashtable<String,String> uhash = new Hashtable<String,String>();
            StringReader sr = new StringReader(in);
            BufferedReader br = new BufferedReader(sr);
            br.skip(22000);
            String check = "\t<form method=\"POST\" action=\"http://chat.hiveworkshop.com?channel=\" id=\"chatform\">";
            while (!(line=br.readLine()).equals(check)) {
            	
            }
            for (int i=0;i<6;i++) {
                line = br.readLine();
                if (!(line.equals("") || line.equals("\t"))) {
                    line = line.split("name=\"")[1];
                    String[] lines = line.split("\" value=\"");
                    try {
                        lines[1] = lines[1].split("\" />")[0];
                        uhash.put(lines[0],lines[1]);
                    } catch (Exception ex) {
                        uhash.put(lines[0],"");
                    }
                } else {
                    i--;
                }
            }
            br.close();
            sr.close();
            return uhash;
        } catch (Exception ex) {
            ex.printStackTrace(Client.theConsole);
            return null;
        }
    }
}

And here's the code to communicate with the chat (Again, don't mind the Window/Client/Message/User/etc stuff, that's all specific to my program).

Code:
package components;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URLEncoder;
import main.Client;

public class ChatHandler {
    private static boolean connected = false;
    private static String PHPSESSID;
    private static String[] lastList = {"",""};
    private static String lastBan = "";
    private static int numFails = 0;
    public static boolean isConnected() {
        return connected;
    }
    public static void confirmReloadUserList() {
        lastList = new String[] {"",""};
    }
    private static String encodePOST(String key, String val) {
        try {
            return URLEncoder.encode(key,"UTF-8") + "=" + URLEncoder.encode(val,"UTF-8");
        } catch (Exception ex) {
            ex.printStackTrace(Client.theConsole);
            return null;
        }
    }
    public static void login(Window target, String userid, String username, String groups, String permissions, String rep, String check) {
        try {
            Socket s = new Socket("chat.hiveworkshop.com",80);
            java.io.PrintWriter pw = new java.io.PrintWriter(s.getOutputStream());
            String data = encodePOST("userid",userid)+"&"+encodePOST("username",username)+"&"+encodePOST("groups",groups)+"&"+encodePOST("permissions",permissions)+"&"+encodePOST("rep",rep)+"&"+encodePOST("check",check);
            pw.print("POST / HTTP/1.1\r\n");
            pw.print("Host: chat.hiveworkshop.com\r\n");
            pw.print("Content-Length: "+data.length()+"\r\n");
            pw.print("Content-Type: application/x-www-form-urlencoded\r\n");
            pw.print("\r\n");
            pw.print(data);
            pw.flush();
            InputStreamReader isr = new InputStreamReader(s.getInputStream());
            BufferedReader br = new BufferedReader(isr);
            String line;
            String targ = "PHPSESSID=";
            while ((line=br.readLine())!=null) {
                if (line.contains(targ)) {
                    for (int i=0;i<line.length();i++) {
                        if (line.substring(i,i+targ.length()).equals(targ)) {
                            line = line.substring(i);
                            PHPSESSID=line.split(";")[0];
                            target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Successfully connected to server"));
                            break;
                        }
                    }
                    break;
                }
            }
            s.close();
            br.close();
            pw.close();
            isr.close();
            connected = true;
        } catch (IOException ex) {
            ex.printStackTrace(Client.theConsole);
            target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Failed to connect to server"));
        } catch (Exception ex) {
            ex.printStackTrace(Client.theConsole);
        }
    }
    public static Message[] getMessages(Window target, int channel) {
        if (!connected) {
            return null;
        }
        try {
            Socket s = new Socket("chat.hiveworkshop.com",80);
            PrintWriter pw = new PrintWriter(s.getOutputStream());
            pw.print("GET /chat_ajax.php?do=get&channel="+channel+" HTTP/1.1\r\n");
            pw.print("Host: chat.hiveworkshop.com\r\n");
            pw.print("Cookie: "+PHPSESSID+"\r\n");
            pw.print("Referer: http://chat.hiveworkshop.com/\r\n");
            pw.print("\r\n");
            pw.flush();
            InputStreamReader isr = new InputStreamReader(s.getInputStream());
            while (!isr.ready()) {
                Thread.sleep(10);
            }
            BufferedReader br = new BufferedReader(isr);
            for (int i=0;i<10;i++) {
                br.readLine();
            }
            int len = 1;
            try {
                len = Integer.parseInt(br.readLine(),16);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            char[] chars = new char[len];
            for (int i=0;i<chars.length;i++) {
                chars[i] = (char)br.read();
            }
            String line = new String(chars);
            line = new String(line.getBytes(),"UTF-8");
            if (line.contains("<banned>")) {
                String message = "You are banned. Time until unban: ";
                try {
                    message += line.split("<banned>")[1].split("</banned>")[0];
                } catch (Exception ex) {
                    ex.printStackTrace(Client.theConsole);
                }
                if (!lastBan.equals(message)) {
                    lastBan = message;
                    target.writeToAll(new Message(target,Message.TYPE_SYSTEM,message));
                }
                return null;
            }
            lastBan = "";
            String[] msgs = line.split("<m>");
            Message[] messages = new Message[msgs.length-1];
            for (int i=1;i<msgs.length;i++) {
                String username = msgs[i].split("<username>")[1].split("</username>")[0];
                String message = msgs[i].split("<message>")[1].split("</message>")[0];
                String rank = msgs[i].split("<image>")[1].split("</image>")[0];
                String type = msgs[i].split("<type>")[1].split("</type>")[0];
                String ucolor = msgs[i].split("<color>")[1].split("</color>")[0];
                username = username.substring(9,username.length()-3);
                message = message.substring(9,message.length()-3).replace("http://hiveworkshop.com/resources_new/images/smileys/","file:///"+target.getClient().getImagesFolderPath()+"emotes"+System.getProperty("file.separator"));
                rank = rank.substring(9,rank.length()-3);
                if (rank.length()>4) {
                    rank = rank.substring(0,rank.length()-4);
                }
                type = type.substring(9,type.length()-3);
                ucolor = ucolor.substring(9,ucolor.length()-3);
                User u = new User(target,rank,username,ucolor);
                int mType = 0;
                if (type.equals("0")) {
                    if (Client.getBotOwner().equals(username)) {
                        mType = Message.TYPE_SELF;
                    } else {
                        mType = Message.TYPE_NORMAL;
                    }
                } else if (type.equals("1")) {
                    mType = Message.TYPE_EMOTE;
                } else if (type.equals("2") | type.equals("5")) {
                    mType = Message.TYPE_SYSTEM;
                } else if (type.equals("3")) {
                    mType = Message.TYPE_WHISPER;
                } else if (type.equals("4")) {
                    mType = Message.TYPE_ANNOUNCE;
                } else if (type.equals("6")) {
                    mType = Message.TYPE_DING;
                }
                if (channel==1) {
                    messages[i-1] = new Message(target,u,target.getChannelObject("Lobby"),mType,message);
                } else if (target.getChannelObject("Staff")!=null){
                    messages[i-1] = new Message(target,u,target.getChannelObject("Staff"),mType,message);
                }
            }
            s.close();
            s.close();
            br.close();
            pw.close();
            isr.close();
            return messages;
        } catch (IOException ex) {
            ex.printStackTrace(Client.theConsole);
            target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Failed to receive messages"));
            numFails++;
            if (numFails>4) {
                numFails=0;
                connected = false;
                target.disconnect();
            }
        } catch (Exception ex) {
            ex.printStackTrace(Client.theConsole);
        }
        return null;
    }
    public static User[] getUserList(Window target, int channel) {
        if (!connected) {
            return null;
        }
        try {
            Socket s = new Socket("chat.hiveworkshop.com",80);
            java.io.PrintWriter pw = new java.io.PrintWriter(s.getOutputStream());
            pw.print("GET /chat_ajax.php?do=listpeople&channel="+channel+" HTTP/1.1\r\n");
            pw.print("Host: chat.hiveworkshop.com\r\n");
            pw.print("Cookie: "+PHPSESSID+"\r\n");
            pw.print("Referer: http://chat.hiveworkshop.com/\r\n");
            pw.print("\r\n");
            pw.flush();
            java.io.InputStreamReader isr = new java.io.InputStreamReader(s.getInputStream());
            while (!isr.ready()) {
                Thread.sleep(10);
            }
            java.io.BufferedReader br = new java.io.BufferedReader(isr);
            for (int i=0;i<10;i++) {
                br.readLine();
            }
            int len = 1;
            try {
                len = Integer.parseInt(br.readLine(),16);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            char[] chars = new char[len];
            for (int i=0;i<chars.length;i++) {
                chars[i] = (char)br.read();
            }
            String line = new String(chars);
            if (line==null || line.equals(lastList[channel-1])) {
                return null;
            } else {
                lastList[channel-1] = line;
            }
            line = new String(line.getBytes(),"UTF-8");
            String[] users = line.split("<user>");
            User[] userList = new User[users.length-1];
            for (int i=1;i<users.length;i++) {
                String username = users[i].split("<username>")[1].split("</username>")[0];
                String uid = users[i].split("<userid>")[1].split("</userid>")[0];
                String at = users[i].split("<status>")[1].split("</status>")[0];
                String ucolor = users[i].split("<color>")[1].split("</color>")[0];
                String rank = users[i].split("<rank>")[1].split("</rank>")[0];
                username = username.substring(9,username.length()-3);
                uid = uid.substring(9,uid.length()-3);
                at = at.substring(9,at.length()-3);
                ucolor = ucolor.substring(9,ucolor.length()-3);
                rank = rank.substring(9,rank.length()-7);
                if (ucolor.equals(" ") | ucolor.isEmpty()) {
                    ucolor = "#B8A26E";
                }
                System.out.println(rank+"::"+username+"::"+ucolor+"::"+at);
                userList[i-1] = new User(target,rank,username,ucolor,at);
            }
            s.close();
            br.close();
            isr.close();
            pw.close();
            return userList;
        } catch (IOException ex) {
            ex.printStackTrace(Client.theConsole);
            target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Failed to receive members list"));
        } catch (Exception ex) {
            ex.printStackTrace();//Client.theConsole);
        }
        return null;
    }
    public static void sendMessage(String message, Window target, int channel) {
        if (!connected) {
            target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Not connected to server"));
            return;
        }
        try {
            Socket s = new Socket("chat.hiveworkshop.com",80);
            message = java.net.URLEncoder.encode(message,"UTF-8");
            java.io.PrintWriter pw = new java.io.PrintWriter(s.getOutputStream());
            pw.print("GET /chat_ajax.php?do=write&channel="+channel+"&message="+message+" HTTP/1.1\r\n");
            pw.print("Host: chat.hiveworkshop.com\r\n");
            pw.print("Cookie: "+PHPSESSID+"\r\n");
            pw.print("Referer: http://chat.hiveworkshop.com/\r\n");
            pw.print("\r\n");
            pw.flush();
            s.close();
        } catch (IOException ex) {
            ex.printStackTrace(Client.theConsole);
            target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Failed to send message"));
        } catch (Exception ex) {
            ex.printStackTrace(Client.theConsole);
        }
    }
}

Hell, if you want my chat client's full source I'd be happy to give it to you.

Yes, C++ is for Windows (Win32 applications).
Uh, no, it supports multiple platforms, although when you write code in it the code is platform-specific.
 
Last edited:
Level 21
Joined
Aug 21, 2005
Messages
3,699
Uh, no, it supports multiple platforms, although when you write code in it the code is platform-specific.

No it isn't. Only the compiled machinecode is machinespecific, that is, after compilation. The code itself is crossplatform.
I mean, really, what were you thinking? That blizzard invents warcraft 3 two times, once for windows and once for Mac? That'd be plain stupid.
 
No it isn't. Only the compiled machinecode is machinespecific, that is, after compilation. The code itself is crossplatform.
I mean, really, what were you thinking? That blizzard invents warcraft 3 two times, once for windows and once for Mac? That'd be plain stupid.

Almost all of the code is the same for both versions, yes, but the code itself is not 100% the same. Changes have to be made to graphics rendering and stuff like that mostly due to driver and hardware differences, as well as stuff like how files are processed or something.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
No it isn't. Only the compiled machinecode is machinespecific, that is, after compilation. The code itself is crossplatform.
I mean, really, what were you thinking? That blizzard invents warcraft 3 two times, once for windows and once for Mac? That'd be plain stupid.
Not all of it is, but a good amount of it is. For example, OS-specific libraries (like windows.h) come in to play. Also, the way some libraries are handled (such as windows DLLs) varies between OSes.

If it was so easy, every company would do it, and Blizzard wouldn't pay a separate group to make the OS X versions.

Also, there are compiling differences from compiler to compiler, and you are obviously not using the same compiler on OS X as on Windows.
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
Almost all of the code is the same for both versions, yes, but the code itself is not 100% the same. Changes have to be made to graphics rendering and stuff like that mostly due to driver and hardware differences, as well as stuff like how files are processed or something.

If you use platform specific libraries such as Direct3D, obviously. But you got to be stupid to do that... There are alternatives such as openGL that is platform independant.
Stuff like files are processed, as far as I know c++ has standard libraries for file management, which is obviously platform independant. Like I said: there are plenty of good platform independant libraries. Only if you are intentionally trying to write software that only works on one OS will you use platform specific libraries...

Not all of it is, but a good amount of it is. For example, OS-specific libraries (like windows.h) come in to play. Also, the way some libraries are handled (such as windows DLLs) varies between OSes.
As I have said: there's always a good and solid alternative library, usually released under GNU licenses.

About dll's: they aren't c++ code. typical for dll's is that they are already compiled and link on run-time. So a dll shouldn't be making your c++ code platform dependant. Besides, I think dll's are way overrated. Only in very specific cases is it actually useful to have a library that's dynamically linked...

If it was so easy, every company would do it, and Blizzard wouldn't pay a separate group to make the OS X versions.
Blizzard is a good company, unlike many other companies. I'm not saying it's *that* easy, but it's not at all as hard as you make it sound. ONly if you're looking for trouble will you find it.
It's also the first time I've heard that blizzard pays a separate group for the OS X versions.

Also, there are compiling differences from compiler to compiler, and you are obviously not using the same compiler on OS X as on Windows.
Uh, sure, but isn't that what compilers are all about? Your code is platform independant, your compiler just takes care of the details and translates it for a specific platform. It makes the output platform dependant, but not the c++ input code.
And therefor: using different compilers for different platforms is NOT "hard" to do, and does not involve months of additional development time.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
It's also the first time I've heard that blizzard pays a separate group for the OS X versions.
I'm not sure about Warcraft, as I didn't really pay attention to the credits, but for Starcraft the mac version is done by a group called Future Point.

Uh, sure, but isn't that what compilers are all about? Your code is platform independant, your compiler just takes care of the details and translates it for a specific platform. It makes the output platform dependant, but not the c++ input code.
And therefor: using different compilers for different platforms is NOT "hard" to do, and does not involve months of additional development time.
What I'm saying is stuff that will compile under one compiler will not necessarily compile under other compilers.
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
That's because windows.h is NOT a standard header file of the c++ standard library, obviously because windows.h is a specific windows OS header file. You need to download the platform SDK on code::blocks in order to be able to use windows.h. Don't confuse an IDE with a compiler. The compilers work just fine, the visual studio IDE just has windows.h support on default while code::blocks IDE does not. Blame the IDE, not the compiler nor the c++ code.

It's not a compiler issue, you simply don't have the right header files available.
 
Status
Not open for further replies.
Top