Skip to content

teraa/IrcMessageParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IRC Message Parser

NuGet

Description

Library containing types and parsers for IRC message, as specified in RFC 1459. Compatible with Twitch Messaging Interface (TMI).

Library provides multiple implementations of parsers, depending on the needs. Custom parsers can be implemented by implementing one of the appropriate interfaces; ICommandParser, IContentParser, ITagsParser, IPrefixParser and/or IMessageParser.

ITagsParser implementations

  • TagsParser - default implementation, validates and parses all of the message tags (both keys and values) in a dictionary. This allocates most memory.

  • LazyTagsParser - recommended if messages are expected to contain many tags, and tags are expected to be used only via the indexer. Any other behaviour of LazyTags will fall back to the default implementation and allocate whole dictionary containing all the tags. Format of the input is not validated apart from empty/null check.

ICommandParser implementations

  • CommandParser - default but slower implementation. Allows for case insensitive commands (e.g. PRIVMSG, privmsg, etc), as per the spec.

  • FastCommandParser - faster implementation using the source generated parser for the Command enum. The parsing is case-sensitive, and only all-uppercase command names or 3 digit numerics are accepted.

Example

using System.Diagnostics;
using Teraa.Irc;
using Teraa.Irc.Parsing;

// Recommended for Twitch
var parser = new MessageParser
{
    TagsParser = new LazyTagsParser(),
    CommandParser = new FastCommandParser(),
};

// Parse Example
string rawMessage = "@emote-only=0;followers-only=-1;r9k=0;slow=0;subs-only=0 :tmi.twitch.tv ROOMSTATE #channel";
IMessage message = parser.Parse(rawMessage);

Debug.Assert(message.Tags is not null);
Debug.Assert(message.Prefix is not null);
Debug.Assert(message.Arg is not null);

Console.WriteLine(message.Tags["followers-only"]); // -1
Console.WriteLine(message.Prefix.Name); // tmi.twitch.tv
Console.WriteLine(message.Command); // ROOMSTATE
Console.WriteLine(message.Arg); // #channel

// ToString Example
message = new Message(
    Command: Command.PRIVMSG,
    Prefix: new Prefix(
        Name: "nick",
        User: "user",
        Host: "host"),
    Arg: "#channel",
    Content: new Content(
        Text: "Hello"));

rawMessage = parser.ToString(message);

Console.WriteLine(rawMessage); // :nick!user@host PRIVMSG #channel :Hello

References