views:

110

answers:

2

I am writing a simple IRC Bot in C++. I am having issues, however, parsing "PING" properly. I cannot seem to extract the "PING" string from the output. I have a sample program setup (statically) with the same setup as my IRC bot and I can parse the string, but it does not seem to write properly when I actually apply the method to the bot.

Here is what I am trying to do:

char* Process::pingPong(std::string data)
{
    char* pong = (char*)malloc(10);

    if(strstr(data.c_str(),"PING"))
    {
        if(sscanf(data.c_str(), "%*[^\n|^PING] PING :%s", pong) > 0)
        {
            return pong;
        }
    }

    free(pong);
    return NULL; // Don't do anything if there is no ping to respond to
}

This function is called within the reading loop and (theoretically, after I return properly) will write the proper output to the server if it does not return NULL.

So this is being handled in the read-loop as follows:

        char* ping = proc.pingPong(data);

        if(ping != NULL)
        {
            char* pong = "PONG :";
            std::strcat(pong,ping);
            sock.writeData(sock.Connection,pong);
        }

Thanks for your help!

Also, please note, I have done several checks and it IS making it to sscanf(), but in my test (where the std::string var is static) this method works to find PING. However, in practice it does not seem to work.

A: 

Horrible, horrible code. Please learn how to use std::string properly. But you have illegal code here:

char* pong = "PONG :";
std::strcat(pong,ping);

You are trying to change a string literal (pong) which is illegal in both C++ and C.

anon
Sure, but this is not where the problem is as of now. I cannot return the PING :VALUE back so I can PONG :VALUE.
RageD
A: 

Your problem is in the sscanf() - this function doesn't do regexes, it just has some format specifiers that look a little like simple regexes.

You really should be building a single solid parser that breaks each incoming line into:

  • Source
  • Command / Numeric
  • Arguments

rather than building a broken, ad-hoc parser for each IRC command/numeric.

caf