Many times, people have wanted to make their own IRC parsing library from scratch. This was often met by digs that its a waste of time or that there are many free and open source bot frameworks out there(especially in python). But these frameworks were once a new library too, so how did they manage to build up such a huge and working IRC parsing library? Read on to get the know-how.
IRC libraries, in the approach i made BamBaka, is event based. A entry code parses the string, finds the type of the message by parsing the IRC Numeric next to the prefix and returns a Event. This event can be a PingEvent or a UnknownCommandEvent(the previous a string, the latter a numeric). Parsing should be user specific, though if you are into OOP I could suggest you to follow the Publisher Subscriber Pattern. Read through this following links and google “Publisher Subscriber Pattern”.
For a example, I'm laying out how my bot does it. The main class in my bot is IRCMessage(you can look at the irc submodule for implementation details, though its in kotlin). This class has a function call Parse() which parses a string that is set at the constructor. Also another type(MsgType here) parses the IRC numeric from the original string and stores it.In the Parse() function, a function which switch cases through a list of Event types returns the event for the numeric. After this we apply the EventHandler or Publisher Subscriber pattern class, return the message wrapped in a struct or data class or whatever you wish to use. This example was told in a OOP format, though if you prefer functional style, I think it can be done in that way too. But do feel free to experiment on the approach to be taken while parsing the messages.
If you want to use SSL but have only defined a Plain connection class you will have to use a Base class (in OOP Style) which is inherited by both SSL connection class and Plain connection class. Though networking is language dependent, so I wont continue about it here.
I also defined useful functions in a file where each function returns a string. You can implement a series of such functions for your own use, like a ping, quit, privmsg, etc.
I hope by now you can create that awesome library of yours, which you have dreamt of for so long. IRC is a simple protocol, and I think people should be given the chance to play with it. I hope you enjoyed this tutorial. If you want to add something which you discovered while making your bot, like a better parsing approach than the function based type throws, or anything, then please feel free to add to this tutorial.