According to the Internet Relay Chat Protocol (RFC 1459), your message (control part at the start) will always start with :
, as will your params (if you have any - e.g. chat message).
The easiest way to start would be to separate those two components by simply looking for the first colon that is not at the start of the line.
string example = @":*****!*****@*****.tmi.twitch.tv PRIVMSG #***** :http://www.twitch.tv/";
int indexOfColon = example.IndexOf(':', 1);
if (indexOfColon > 0)
{
string command = example.SubString(0,indexOfColon);
string message = example.SubString(indexOfColon +1);
}
Demo: https://dotnetfiddle.net/wBoKlC
With the same concept, you can parse any part of the line. Here for example you could extract the command (:PRIVMSG
), username (!*****
) and host (@*****.tmi.twitch.tv
) simply by understanding the protocol structure and without and unneccessary Split
and Join
or even RegEx
.
So instead of looking for PRIVMSG
, you should just parse every line and decide what to do with it later. This line could be troublesome:
if (message.Contains("PRIVMSG"))
Imagine any other command contained that string (Username, Channel or a regular message). It would totally break your code.
Btw: The 'pseudo' BNF for IRC is:
<message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
<command> ::= <letter> { <letter> } | <number> <number> <number>
<SPACE> ::= ' ' { ' ' }
<params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
<middle> ::= <Any *non-empty* sequence of octets not including SPACE
or NUL or CR or LF, the first of which may not be ':'>
<trailing> ::= <Any, possibly *empty*, sequence of octets not including
NUL or CR or LF>
<crlf> ::= CR LF