Visual C++/MFC Sample on Internet Relay Chat

This article takes a look at developing an IRC client program using Microsoft Visual C++, MFC and WinSock. For the development this uses the IRC reference published in  ftp://ftp.irc.org/irc/docs/rfc1459.txt and ftp://ftp.irc.org/irc/docs/rfc2812.txt.

Development of an IRC client in Visual C++:

There must be some basic steps for the development:

  • Create a class to establish a connection to an IRC server
  • Create a class to send and receive messages to/from the server
  • Create MFC classes to wrap the previous ones and display them.

For the first two features these classes were used.

CIrcMessage (irc.h,irc.cpp) – Parse raw IRC commands into their individual components, and re-build raw commands back from those components.

IIrcSessionMonitor (irc.h,irc.cpp) – Base interface for IRC session monitors.

CIrcSession (irc.h,irc.cpp) – IRC session manager. Maintains a connection to the remote IRC server in a secondary thread, and distributes parsed IRC messages to registered monitors.

CIrcSessionInfo (irc.h,irc.cpp) – IRC connection specs holder.

CIrcMonitor (irc.h,irc.cpp) – Basic session monitor object (derived from IIrcSessionMonitor). This class implements a mechanism that maps IRC commands to user-defined member functions, same as what MFC does with Windows(tm) messages. All notifications are posted outside the context of the connection thread.

CIrcDefaultMonitor (irc.h,irc.cpp) – Provides some automatic responses the IRC server requires. (Derived from CIrcMonitor).

CIrcIdentServer (irc.h,irc.cpp) – Identification server. This class is used by the CIrcSession class.

To use the messages sent by the IRC classes this class uses the IRC_MESSAGE_MAP.
DEFINE_IRC_MAP() (irc.h) – Must be defined inside the CIrcMonitor derived class’s definition (.H file).

DECLARE_IRC_MAP(this_class_name, base_class_name) (irc.h) – Must be defined in your CIrcMonitor derived class’s implementation file (.CPP).

IRC_MAP_ENTRY(class_name, command, member) (irc.h) – Add entries like this in your class’s constructor for each IRC command you’d like to respond to.

Also this has a wrapper for the windows sockets to make them easier to use:

WinsockInit (socket.h,socket.cpp) – Provides automatic winsock initialization. All address resolving code is contained by this class.

Socket (socket.h,socket.cpp) – WIN32 socket’s wrapper class.

InetAddr (socket.h,socket.cpp) – Internet address helper class.

CCrossThreadsMessagingDevice: (CrossThreadsMessagingDevice.h,CrossThreadsMessagingDevice.cpp) – Used by CIrcMonitor to post IRC notifications back to the thread that the monitor was created in.
To connect to the IRC server first we need its name (here we use irc.dalnet.org) then a port to connect to. Various servers support various TCP/IP ports but it?s almost certain that any server supports the universal IRC port 6667. So the port this project uses here is this. Then you need a nickname and an ident. So we created a dialog box that performs this connection. You can change the values set by this box to whatever you like.

To wrap theses classes we use MFC Doc/View architecture. We make an interface similar to the one of mIRC ? an edit box for writing text and a rich-edit for displaying the received stuff with various colors so you can easily make the difference between your messages and other people?s messages. With the IRC map we capture messages sent by the IRC classes and display them on the control. There is a message for each command that our client will support:

bool OnIrc_YOURHOST(const CIrcMessage* pmsg);
bool OnIrc_NICK(const CIrcMessage* pmsg);
bool OnIrc_PRIVMSG(const CIrcMessage* pmsg);
bool OnIrc_JOIN(const CIrcMessage* pmsg);
bool OnIrc_PART(const CIrcMessage* pmsg);
bool OnIrc_KICK(const CIrcMessage* pmsg);
bool OnIrc_MODE(const CIrcMessage* pmsg);

And for the not supported ones we use

virtual void OnIrcDefault(const CIrcMessage* pmsg);

This function can be overridden should you choose to support more IRC commands.
This IRC client is a very simple one ? much simpler than the free clients that you can find on the internet but gives a good example of how to communicate with the IRC servers should you decide to make program that uses them for communication. The IRC server are free to use and are easy to use. So you can fairly easy add a communication module to your program using them.

Download the sample code here.