/// <summary> /// Parse the message and call the callback methods /// on the listeners. /// /// </summary> /// <param name="tokens">The text received from the IRC server</param> private void ParseCommand(string[] tokens) { //Remove colon user info string tokens[0] = RemoveLeadingColon(tokens[0]); switch (tokens[1]) { case NOTICE: tokens[3] = RemoveLeadingColon(tokens[3]); if (Rfc2812Util.IsValidChannelName(tokens[2])) { if (OnPublicNotice != null) { OnPublicNotice( Rfc2812Util.UserInfoFromString(tokens[0]), tokens[2], CondenseStrings(tokens, 3)); } } else { if (OnPrivateNotice != null) { OnPrivateNotice( Rfc2812Util.UserInfoFromString(tokens[0]), CondenseStrings(tokens, 3)); } } break; case JOIN: if (OnJoin != null) { OnJoin(Rfc2812Util.UserInfoFromString(tokens[0]), RemoveLeadingColon(tokens[2])); } break; case PRIVMSG: tokens[3] = RemoveLeadingColon(tokens[3]); if (tokens[3] == ACTION) { if (Rfc2812Util.IsValidChannelName(tokens[2])) { if (OnAction != null) { int last = tokens.Length - 1; tokens[last] = RemoveTrailingQuote(tokens[last]); OnAction(Rfc2812Util.UserInfoFromString(tokens[0]), tokens[2], CondenseStrings(tokens, 4)); } } else { if (OnPrivateAction != null) { int last = tokens.Length - 1; tokens[last] = RemoveTrailingQuote(tokens[last]); OnPrivateAction(Rfc2812Util.UserInfoFromString(tokens[0]), CondenseStrings(tokens, 4)); } } } else if (channelPattern.IsMatch(tokens[2])) { if (OnPublic != null) { OnPublic(Rfc2812Util.UserInfoFromString(tokens[0]), tokens[2], CondenseStrings(tokens, 3)); } } else { if (OnPrivate != null) { OnPrivate(Rfc2812Util.UserInfoFromString(tokens[0]), CondenseStrings(tokens, 3)); } } break; case NICK: if (OnNick != null) { OnNick(Rfc2812Util.UserInfoFromString(tokens[0]), RemoveLeadingColon(tokens[2])); } break; case TOPIC: if (OnTopicChanged != null) { tokens[3] = RemoveLeadingColon(tokens[3]); OnTopicChanged( Rfc2812Util.UserInfoFromString(tokens[0]), tokens[2], CondenseStrings(tokens, 3)); } break; case PART: if (OnPart != null) { OnPart( Rfc2812Util.UserInfoFromString(tokens[0]), tokens[2], tokens.Length >= 4 ? RemoveLeadingColon(CondenseStrings(tokens, 3)) : ""); } break; case QUIT: if (OnQuit != null) { tokens[2] = RemoveLeadingColon(tokens[2]); OnQuit(Rfc2812Util.UserInfoFromString(tokens[0]), CondenseStrings(tokens, 2)); } break; case INVITE: if (OnInvite != null) { OnInvite( Rfc2812Util.UserInfoFromString(tokens[0]), RemoveLeadingColon(tokens[3])); } break; case KICK: if (OnKick != null) { tokens[4] = RemoveLeadingColon(tokens[4]); OnKick(Rfc2812Util.UserInfoFromString(tokens[0]), tokens[2], tokens[3], CondenseStrings(tokens, 4)); } break; case MODE: if (channelPattern.IsMatch(tokens[2])) { if (OnChannelModeChange != null) { UserInfo who = Rfc2812Util.UserInfoFromString(tokens[0]); try { ChannelModeInfo[] modes = ChannelModeInfo.ParseModes(tokens, 3); OnChannelModeChange(who, tokens[2], modes); } catch (Exception) { if (OnError != null) { OnError(ReplyCode.UnparseableMessage, CondenseStrings(tokens, 0)); } } } } else { if (OnUserModeChange != null) { tokens[3] = RemoveLeadingColon(tokens[3]); OnUserModeChange(Rfc2812Util.CharToModeAction(tokens[3][0]), Rfc2812Util.CharToUserMode(tokens[3][1])); } } break; case KILL: if (OnKill != null) { string reason = ""; if (tokens.Length >= 4) { tokens[3] = RemoveLeadingColon(tokens[3]); reason = CondenseStrings(tokens, 3); } OnKill(Rfc2812Util.UserInfoFromString(tokens[0]), tokens[2], reason); } break; default: if (OnError != null) { OnError(ReplyCode.UnparseableMessage, CondenseStrings(tokens, 0)); } break; } }