The message object sent in the signal has the following basic parts:
if(msg.from() == mysession->server()) { cout << "It's a server message" << endl; } else { cout << "It's a message from " << msg.from().nick() << endl; }
A message's command can be retrieved with its command() function, and it is an object. However, it has been designed so you can treat it like a number. This is very useful if you want to use a switch() statement on it:
switch(msg.command()) { case PRIVMSG: cout << "This message is a PRIVMSG" << endl; break; case QUIT: cout << "This message is a QUIT message" << endl; break; case 311: cout << "This message is a whois reply" << endl; break; default: cout << "This message is an unhandled message of type " << msg.command().str() << endl; }libircpp has a set of defines which define PRIVMSG, QUIT, and other textual commands as numbers so they can be directly compared with command objects. In addition, if you want a string representation of the command, just use msg.command().str() as shown above.
Messages can be sent to both users and channels. Because of this, the recipient could be either a user or a channel object. As explained in the next chapter, libircpp's user and channel objects are therefore derived from a base class called entity. The recipient is therefore of type entity, and you must convert it to a user or channel if you want to do any user-specific actions on it. For example:
if(msg.to().is_channel()) { const channel& c = msg.to().to_channel(); process_channel_message(c); // Your function to process channel messages } else { const user& u = msg.to().to_user(); process_private_message(u); // Your function to process private messages }
If a message has a target, it can be retrieved with the message's target() function. If the message doesn't have a target, the target() function will return the null user. You can check for this as follows:
if(!msg.target()) { // Message has no target }
The param() function allows you to access parameters one word at a time. For example, param(0) would give you the first word of the message, param(1) the second, etc. This is useful when you know which parameter of a message you want.
The params() function allows you to access all the parameters of a message as one string, or all parameters starting at a specified one, and ending at the end of the message. For example:
msg.params(0); // Returns all parameters as one string msg.params(); // Does the same as above msg.params(2); // Returns a string containing parameters, starting with the third parameter, and ending with the lastNormally, the param() and params() functions will skip the recipient and target, if present, and remove any colon at the beginning of the parameters. This normally gives a sensible starting point for the parameter list - e.g. for PRIVMSGs, params(0) is the beginning of the message the user typed. If you want to retrieve the unprocessed parameters, starting after the command, and with the colon intact, set the optional second parameter to param() and params() to true, like this:
msg.params(0, true);
To make things more complicated, multiple mode changes may be combined into one line, and this is frequently done. An extreme example might be:
MODE #channel +oio-v+k joe fred ernest paswardThis must be parsed if the modes are to be kept track of, for example when indicating channel operators in a channel userlist.
To help deal with this, where mode messages contain multiple mode changes, libircpp can split the mode message up into individual mode changes. When a mode message is recieved, two separate signals are emitted: sig_mode_msg (once), and sig_singlemode_msg (once for each separate mode change). sig_singlemode_message consists of mode messages each of which contains only one mode change.
So, from the example above, if you had connected to sig_mode_msg, you would see:
+oio-v+k joe fred ernest paswardAnd if you had connected to sig_singlemode_msg, you would see, as separate messages:
MODE #channel +o joe MODE #channel +i MODE #channel +o fred MODE #channel -v ernest MODE #channel -v ernest MODE #channel +k paswardIt is important to note that the messages emitted by sig_singlemode_msg, while complete message objects, have been generated by libircpp, not the irc server. sig_mode_msg contains the messages as received by the server; sig_singlemode_msg is there only for convenience. In particular, if you were to parse both sig_mode_msg and sig_singlemode_msg, you would see every mode change twice.