C++ Class Library for IRC



Click here for larger image

Environment: eg VC6, WIN32

Introduction

This article describes a C++ class library to help programmers implement their own IRC client (like the famous mIRC). The protocols supported by this class library are :

Features

  1. Maintaining a TCP/IP connection to a remote IRC server in a secondary thread.
  2. Identification server (optional. see CIrcSessionInfo).
  3. Full IRC messages two-way parsing.
  4. IRC notifications are posted back to the thread that created the monitor object.
  5. Efficient mechaism of mapping IRC commands to their associated user-defined member functions. This mechanism resembles the way MFC maps Windows(tm) messages to response functions.
  6. Even though the demo project was written using MFC, the library itself was written using only plain C++, STL and WIN32 API.

Classes

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.

Macros

DEFINE_IRC_MAP() (irc.h) - Must be defined inside your 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.

Utility Classes

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.

Usage

  1. The library comes in the form of source-code only (sorry, no DLL yet, although it would certainly make sense to create one), so you just have to add all those source files to your C++ project.
  2. Somewhere inside your project, create an instance of CIrcSession. no derivation needed. This object is your actual connection to the remote IRC server.
  3. To receive notification from the server, you'll have to create a 'monitor' object. This monitor can be any class that is derived from the IIrcSessionMonitor, directory or indirectly. My personal recommendation is to derive from CIrcDefaultMonitor, because it also provides some automatic responses the IRC server requires. (The demo project i provided here is a multiple-document MFC project so there i decided to make the document object as my monitor).
  4. Add the notification handler functions to your monitor object as needed.

Downloads

Download demo project - 34 Kb
Download source - 9 Kb

Yet to be done

Things I haven't implemented in the current version :

  1. DCC connections (both char and file-transfer).
  2. DLL version.
  3. Unicode support.

Preferences

  1. RFC #1459 - IRC Protocol
  2. RFC #2812 - IRC Client
  3. RFC #1413 - Identification Protocol
  4. RFC #2813 - IRC Server
  5. Microsoft's MSDN library
  6. "Windows(tm) Sockets Network Programming" by Bob Quinn & Dave Shute, Addison Wesley, ISBN 0-201-63372-8