dialect.parsing

Functions related to parsing IRC events.

IRC events come in very heterogeneous forms along the lines of:

:sender.address.tld TYPE [args...] :content

:sender!~ident@address.tld 123 [args...] :content

The number and syntax of arguments for types vary wildly. As such, one common parsing routine can't be used; there are simply too many exceptions. The beginning :sender.address.tld is *almost* always the same form, but only almost. It's guaranteed to be followed by the type however, which come either in alphanumeric name (e.g. PRIVMSG, INVITE, MODE, ...), or in numeric form of 001 to 999 inclusive.

What we can do then is to parse this type, and interpret the arguments following as befits it.

This translates to large switches, which can't be helped. There are simply too many variations, which switches lend themselves well to. You could make it into long if...else if chains, but it would just be the same thing in a different form. Likewise a nested function is not essentially different from a switch case.

1 IRCParser parser;
2 
3 string fromServer = ":zorael!~NaN@address.tld MODE #channel +v nickname";
4 IRCEvent event = parser.toIRCEvent(fromServer);
5 
6 with (event)
7 {
8     assert(type == IRCEvent.Type.MODE);
9     assert(sender.nickname == "zorael");
10     assert(sender.ident == "~NaN");
11     assert(sender.address == "address.tld");
12     assert(target.nickname == "nickname");
13     assert(channel == "#channel");
14     assert(aux = "+v");
15 }
16 
17 string alsoFromServer = ":cherryh.freenode.net 435 oldnick newnick #d " ~
18     ":Cannot change nickname while banned on channel";
19 IRCEvent event2 = parser.toIRCEvent(alsoFromServer);
20 
21 with (event2)
22 {
23     assert(type == IRCEvent.Type.ERR_BANONCHAN);
24     assert(sender.address == "cherryh.freenode.net");
25     assert(channel == "#d");
26     assert(target.nickname == "oldnick");
27     assert(content == "Cannot change nickname while banned on channel");
28     assert(aux == "newnick");
29     assert(num == 435);
30 }
31 
32 string furtherFromServer = ":kameloso^!~ident@81-233-105-99-no80.tbcn.telia.com NICK :kameloso_";
33 IRCEvent event3 = parser.toIRCEvent(furtherFromServer);
34 
35 with (event3)
36 {
37     assert(type == IRCEvent.Type.NICK);
38     assert(sender.nickname == "kameloso^");
39     assert(sender.ident == "~ident");
40     assert(sender.address == "81-233-105-99-no80.tbcn.telia.com");
41     assert(target.nickname = "kameloso_");
42 }

See the /tests directory for more example parses.

Members

Functions

onISUPPORT
void onISUPPORT(IRCParser parser, IRCEvent event, string slice)

Handles dialect.defs.IRCEvent.Type.RPL_ISUPPORT events.

onMode
void onMode(IRCParser parser, IRCEvent event, string slice)

Handle dialect.defs.IRCEvent.Type.MODE changes.

onMyInfo
void onMyInfo(IRCParser parser, IRCEvent event, string slice)

Handle dialect.defs.IRCEvent.Type.RPL_MYINFO events.

onNotice
void onNotice(IRCParser parser, IRCEvent event, string slice)

Handle dialect.defs.IRCEvent.Type.NOTICE events.

onPRIVMSG
void onPRIVMSG(IRCParser parser, IRCEvent event, string slice)

Handle dialect.defs.IRCEvent.Type.QUERY and dialect.defs.IRCEvent.Type.CHAN messages (dialect.defs.IRCEvent.Type.PRIVMSG).

parseBasic
void parseBasic(IRCParser parser, IRCEvent event)

Parses the most basic of IRC events; dialect.defs.IRCEvent.Type.PING, dialect.defs.IRCEvent.Type.ERROR, dialect.defs.IRCEvent.Type.PONG, dialect.defs.IRCEvent.Type.NOTICE (plus NOTICE AUTH), and AUTHENTICATE.

parseGeneralCases
void parseGeneralCases(IRCParser parser, IRCEvent event, string slice)

Takes a slice of a raw IRC string and continues parsing it into an dialect.defs.IRCEvent struct.

parsePrefix
void parsePrefix(IRCParser parser, IRCEvent event, string slice)

Takes a slice of a raw IRC string and starts parsing it into an dialect.defs.IRCEvent struct.

parseSpecialcases
void parseSpecialcases(IRCParser parser, IRCEvent event, string slice)

Takes a slice of a raw IRC string and continues parsing it into an dialect.defs.IRCEvent struct.

parseTypestring
void parseTypestring(IRCParser parser, IRCEvent event, string slice)

Takes a slice of a raw IRC string and continues parsing it into an dialect.defs.IRCEvent struct.

postparseSanityCheck
void postparseSanityCheck(IRCParser parser, IRCEvent event)

Checks for some specific erroneous edge cases in an dialect.defs.IRCEvent.

toIRCEvent
IRCEvent toIRCEvent(IRCParser parser, string raw)

Parses an IRC string into an dialect.defs.IRCEvent.

Structs

IRCParser
struct IRCParser

Parser that takes raw IRC strings and produces dialect.defs.IRCEvents based on them.