#include <libircpp/libircpp.h> using namespace std; using namespace ircpp; int main(int argc, char **argv) { session* ircsession = new session(); ircsession->set_nick("libircpp"); ircsession->set_username("libircpp"); ircsession->set_realname("libircpp test client"); ircsession->connect("my.irc.server.net"); ircsession->cmd_join("#mychannel"); ircsession->cmd_privmsg("#mychannel", "Hello world!"); ircsession->run(); }
I hope it's pretty self-explanatory, but we'll go through it line by line anyway, just to clear up any murky bits.
#include <libircpp/libircpp.h>
libircpp.h is the main header file which you need to include in all files that use libircpp.
using namespace std; using namespace ircpp;
These two lines set us up to use the std and ircpp namespaces by default. They are for convenience, so we don't have to type std:: in front of C++ standard names like "string" and ircpp:: in front of libircpp-related names like "session". ircpp is the namepace used by all of libircpp to separate it from other library functions.
session* ircsession = new session();
Moving on to the main body of the program, the first thing we do is create a session object. The session object is always your starting point when using libircpp, and it represents your connection to the IRC server. If you have more than one connection to a server, or connections to different servers, you must create a session object for each. "ircsession" is the name we have given to the single session object we have created for this Hello World application.
ircsession->set_nick("libircclient"); ircsession->set_username("libircclient"); ircsession->set_realname("libircclient++ test client");
So, now we have our session, we must set it up before we make it connect to the irc server. Here we are setting the nick we will use on irc, our username (ident), and our realname (gecos). You can only use these functions when not connected to IRC - when you are connected, the IRC server does not allow you to change your username or realname. You can, however, change your nick with the session's nick() function.
Replace the quoted strings with your own values if you want.
ircsession->connect("my.irc.server.net");
With the session set up, we can connect to the IRC server. Replace the server name with your favourite server.
You don't have to deal with the login yourself (unless you want to) - the NICK and USER messages are dealt with by the session object.
ircsession->cmd_join("#mychannel");
We can join a channel (replace #mychannel with your favourite channel).
ircsession->cmd_privmsg("#mychannel", "Hello world!");
Now we've joined, we can send a message to the channel to say hello. Again, replace #mychannel with the channel we joined in the previous line.
ircsession->run();
Finally, we call the session's run function. This is a special function that goes into an endless loop, receiving and processing messages from the IRC server and emitting signals (more on this later). run() is actually a convenience function - if you have more than one connection or want to deal with the select() system call yourself, you can use service_socket() instead.