I've been trying to learn a bit about socket programming in C++ and so have been developing a basic IRC bot. I've got it connecting and working, but I'm having an issue determining who has joined the channel (i.e. was the JOIN command for the bot itself, for a random user, or for me).
I'm using CLion on Windows 7.
I've got the following code:
//determine if message is JOIN command if(regex_search(line, match, std::regex(R"(:([^!]*)!.*?JOIN)"))) { //get the nickname of the bot (details.getNickName() shown in next code segment) const char *trueNick = details.getNickName(); //reformat nickname because the following is returned from the method alone //0x10042d0f9 <_ZStL6ignore+119> "CPlusPlusBotTest" const char *nick = string(details.getNickName()).c_str(); //get the name of the user who joined const char *name = match.str(1).c_str(); //debugging std::cout << name << " - name\n"; std::cout << nick << " - nick\n"; //might not be the correct way to compare the two? but I'll sort that out later if(name != nick) { //welcome the user char param[1024]; sprintf(param, "%s :Hello, %s", details.getChannel(), name); sendData("PRIVMSG", param); } } I am unsure why I get the excess "stuff" (I have no idea what it is) from my getter, as it's simply a case of returning a private variable:
const char* BotDetails::getNickName() { return nickName; } Regardless, that's not my issue given I can get rid of it (despite it possibly being rather hacky).
My issue is that when I connect to the channel for testing, and I set a breakpoint on the line assigning trueNick so I can see what happens as I step through the program, the following occurs:
1) trueNick is assigned the value: 0x10042d0f9 <_ZStL6ignore+119> "CPlusPlusBotTest"
2) nick is assigned the value: "CPlusPlusBotTest"
3) name is assigned the value: "Seanharrs" and nick is assigned the value: "Seanharrs"
This means when my debug statements run, nick and name are the same value. I am not sure why nick is being reassigned to be the same value as name, this should not occur. It happens every time as well, not just for my name. I tried using char arrays and strings instead but to no avail. I also find it strange that trueNick is never affected, it's only these two variables. Any help is appreciated (even if it's just an alt/better way to check this rather than a fix, because it may well be just an oddity on my end that nobody else experiences).
char *s, and usestd::stringinstead. This will automagically solve a lot of your issues.const char *nick = string(details.getNickName()).c_str();Won't usingnickbe UB?char param[1024]; sprintf(param, "%s :Hello, %s", details.getChannel(), name);Hope that thedetails.getChannel()function plus the other text is a string less than 1024 characters. Since you're using C++, usestd::ostringstreamto ensure there is no buffer overflow.