예제 #1
0
        private void ProcessModeCommand(string[] tokens)
        {
            if (channelPattern.IsMatch(tokens[2]))
            {
                if (OnChannelModeChange == null)
                {
                    return;
                }

                User who = Rfc2812Util.UserFromString(tokens[0]);
                try
                {
                    ChannelModeInfo[] modes = ChannelModeInfo.ParseModes(tokens, 3);
                    string            raw   = CondenseStrings(tokens, 3);
                    OnChannelModeChange(who, tokens[2], modes, raw);
                    Trace.WriteLine("Channel mode change", "IRC");
                }
                catch (Exception)
                {
                    OnError.Fire(this, new ErrorMessageEventArgs(ReplyCode.UnparseableMessage, CondenseStrings(tokens, 0)));
                    Debug.WriteLineIf(Rfc2812Util.IrcTrace.TraceWarning,
                                      "[" + Thread.CurrentThread.Name + "] Listener::ParseCommand() Bad IRC MODE string=" + tokens[0]);
                }
            }
            else
            {
                tokens[3] = RemoveLeadingColon(tokens[3]);
                OnUserModeChange?.Invoke(this, new UserModeChangeEventArgs(Rfc2812Util.CharToModeAction(tokens[3][0]),
                                                                           Rfc2812Util.CharToUserMode(tokens[3][1])));
                //Trace.WriteLine("User mode change", "IRC");
            }
        }
예제 #2
0
        /// <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 PONG:
                break;

            case NOTICE:
                tokens[3] = RemoveLeadingColon(tokens[3]);
                if (Rfc2812Util.IsValidChannelName(tokens[2]))
                {
                    if (OnPublicNotice != null)
                    {
                        OnPublicNotice(this, new UserChannelMessageEventArgs(
                                           Rfc2812Util.UserFromString(tokens[0]),
                                           tokens[2],
                                           CondenseStrings(tokens, 3)));
                        //Trace.WriteLine("Public notice", "IRC");
                    }
                }
                else
                {
                    if (OnPrivateNotice != null)
                    {
                        OnPrivateNotice(this, new UserMessageEventArgs(
                                            Rfc2812Util.UserFromString(tokens[0]),
                                            CondenseStrings(tokens, 3)));
                        //Trace.WriteLine("Private notice", "IRC");
                    }
                }
                break;

            case JOIN:
                if (OnJoin != null)
                {
                    OnJoin(Rfc2812Util.UserFromString(tokens[0]), RemoveLeadingColon(tokens[2]));
                    //Trace.WriteLine("Join", "IRC");
                }
                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(this, new UserChannelMessageEventArgs(Rfc2812Util.UserFromString(tokens[0]), tokens[2], CondenseStrings(tokens, 4)));
                            //Trace.WriteLine("Channel action", "IRC");
                        }
                    }
                    else
                    {
                        if (OnPrivateAction != null)
                        {
                            int last = tokens.Length - 1;
                            tokens[last] = RemoveTrailingQuote(tokens[last]);
                            OnPrivateAction(this, new UserMessageEventArgs(Rfc2812Util.UserFromString(tokens[0]), CondenseStrings(tokens, 4)));
                            //Trace.WriteLine("Private action", "IRC");
                        }
                    }
                }
                else if (channelPattern.IsMatch(tokens[2]))
                {
                    if (OnPublic != null)
                    {
                        OnPublic(this, new UserChannelMessageEventArgs(Rfc2812Util.UserFromString(tokens[0]), tokens[2], CondenseStrings(tokens, 3)));
                        Trace.WriteLine("Public msg", "IRC");
                    }
                }
                else
                {
                    if (OnPrivate != null)
                    {
                        OnPrivate(this, new UserMessageEventArgs(Rfc2812Util.UserFromString(tokens[0]), CondenseStrings(tokens, 3)));
                        //Trace.WriteLine("Private msg", "IRC");
                    }
                }
                break;

            case NICK:
                if (OnNick != null)
                {
                    OnNick(this, new NickChangeEventArgs(Rfc2812Util.UserFromString(tokens[0]), RemoveLeadingColon(tokens[2])));
                    //Trace.WriteLine("Nick", "IRC");
                }
                break;

            case TOPIC:
                if (OnTopicChanged != null)
                {
                    tokens[3] = RemoveLeadingColon(tokens[3]);
                    OnTopicChanged(this, new UserChannelMessageEventArgs(
                                       Rfc2812Util.UserFromString(tokens[0]), tokens[2], CondenseStrings(tokens, 3)));
                    //Trace.WriteLine("Topic changed", "IRC");
                }
                break;

            case PART:
                if (OnPart != null)
                {
                    OnPart(
                        Rfc2812Util.UserFromString(tokens[0]),
                        RemoveLeadingColon(tokens[2]),
                        tokens.Length >= 4 ? RemoveLeadingColon(CondenseStrings(tokens, 3)) : "");
                    //Trace.WriteLine("Part", "IRC");
                }
                break;

            case QUIT:
                if (OnQuit != null)
                {
                    tokens[2] = RemoveLeadingColon(tokens[2]);
                    OnQuit(Rfc2812Util.UserFromString(tokens[0]), CondenseStrings(tokens, 2));
                    //Trace.WriteLine("Quit", "IRC");
                }
                break;

            case INVITE:
                if (OnInvite != null)
                {
                    OnInvite(
                        Rfc2812Util.UserFromString(tokens[0]), RemoveLeadingColon(tokens[3]));
                    //Trace.WriteLine("Invite", "IRC");
                }
                break;

            case KICK:
                if (OnKick != null)
                {
                    tokens[4] = RemoveLeadingColon(tokens[4]);
                    OnKick(Rfc2812Util.UserFromString(tokens[0]), tokens[2], tokens[3], CondenseStrings(tokens, 4));
                    //Trace.WriteLine("Kick", "IRC");
                }
                break;

            case MODE:
                if (channelPattern.IsMatch(tokens[2]))
                {
                    if (OnChannelModeChange != null)
                    {
                        User who = Rfc2812Util.UserFromString(tokens[0]);
                        try
                        {
                            ChannelModeInfo[] modes = ChannelModeInfo.ParseModes(tokens, 3);
                            string            raw   = CondenseStrings(tokens, 3);
                            OnChannelModeChange(who, tokens[2], modes, raw);
                            Trace.WriteLine("Channel mode change", "IRC");
                        }
                        catch (Exception)
                        {
                            if (OnError != null)
                            {
                                OnError(this, new ErrorMessageEventArgs(ReplyCode.UnparseableMessage, CondenseStrings(tokens, 0)));
                            }
                            Debug.WriteLineIf(Rfc2812Util.IrcTrace.TraceWarning, "[" + Thread.CurrentThread.Name + "] Listener::ParseCommand() Bad IRC MODE string=" + tokens[0]);
                        }
                    }
                }
                else
                {
                    if (OnUserModeChange != null)
                    {
                        tokens[3] = RemoveLeadingColon(tokens[3]);
                        OnUserModeChange(this, new UserModeChangeEventArgs(Rfc2812Util.CharToModeAction(tokens[3][0]),
                                                                           Rfc2812Util.CharToUserMode(tokens[3][1])));
                        //Trace.WriteLine("User mode change", "IRC");
                    }
                }
                break;

            case KILL:
                if (OnKill != null)
                {
                    string reason = "";
                    if (tokens.Length >= 4)
                    {
                        tokens[3] = RemoveLeadingColon(tokens[3]);
                        reason    = CondenseStrings(tokens, 3);
                    }
                    OnKill(Rfc2812Util.UserFromString(tokens[0]), tokens[2], reason);
                }
                break;

            default:
                if (OnError != null)
                {
                    OnError(this, new ErrorMessageEventArgs(ReplyCode.UnparseableMessage, CondenseStrings(tokens, 0)));
                }
                Debug.WriteLineIf(Rfc2812Util.IrcTrace.TraceWarning, "[" + Thread.CurrentThread.Name + "] Listener::ParseCommand() Unknown IRC command=" + tokens[1]);
                //Trace.WriteLine("Unknown command", "IRC");
                break;
            }
        }