Inheritance: System.EventArgs
Beispiel #1
0
        public static Reply Decode(string message)
        {
            var reply = new Reply();
            string trailing = string.Empty;
            int prefixEnd = -1;

            if (message.StartsWith(":"))
            {
                prefixEnd = message.IndexOf(' ');
                reply.Prefix = message.Substring(1, prefixEnd - 1);
            }

            int trailingStart = message.IndexOf(" :", StringComparison.Ordinal);
            if (trailingStart >= 0)
                trailing = message.Substring(trailingStart + 2);
            else
                trailingStart = message.Length;

            string[] commandAndParameters = message.Substring(prefixEnd + 1, trailingStart - prefixEnd - 1).Split(' ');
            foreach (var param in commandAndParameters)
            {
                param.Trim();
            }

            reply.Command = commandAndParameters.First();

            if (commandAndParameters.Length > 1)
                reply.Params = commandAndParameters.Skip(1).ToList();

            reply.Trailing = trailing.Trim();

            return reply;
        }
Beispiel #2
0
 protected virtual void OnReceivedReply(Reply reply)
 {
     if (ReceivedReply != null)
         ReceivedReply(this, reply);
 }
Beispiel #3
0
        public void ProcessReply(object sender, Reply reply)
        {
            // todo maybe check parm[0] for "name" before continuing
            // parm 0 is always channel name on text and nickname on code?!?
            // not sure why we bother to test this!?!
            //if (reply.Params[0] != _client.Nickname)
                //return;

            // todo maybe handle text only in client and delgate codes to the domain classes
            switch (reply.Command)
            {
                case "JOIN" :
                    if (reply.Params.Count <= 0 || reply.Params[0] != Name)
                        OnJoined(new User(_client, reply.Prefix.Substring(0, reply.Prefix.IndexOf('!'))));
                    break;
                case "PRIVMSG" :
                    {
                        if (reply.Params.Count == 0 || reply.Params[0] != Name)
                            return;
                        var user = new User(_client, reply.Prefix.Substring(0, reply.Prefix.IndexOf('!')));
                        OnMessage(new Message(user, reply.Trailing));
                        break;
                    }
                case "QUIT" :
                    if (reply.Params.Count <= 0 || reply.Params[0] != Name)
                    {
                        var user = new User(_client, reply.Prefix.Substring(0, reply.Prefix.IndexOf('!')))
                        {
                            LeaveMessage = reply.Trailing
                        };
                        OnParted(user);
                    }
                    break;
            }

            int code;
            if (!int.TryParse(reply.Command, out code))
                return;

            switch ((ReplyCode) code)
            {
                case ReplyCode.RplTopic :
                    if (reply.Params[1] != Name)
                        return;
                    Topic = new Topic(reply.Trailing);
                    OnTopicChanged();
                    break;
                case ReplyCode.RplTopicSetBy:
                    if (reply.Params[1] != Name) // not this channel
                        return;
                            // 0 is client nickname
                    // todo may not use this
                    _client.Logger("Topic set by " + reply.Params[2]);
                    break;
                case ReplyCode.RplNameReply:
                    if(_names == null) _names = new NamesList();
                   foreach (var user in reply.Trailing.Split().Select(name => new User(_client, name)))
                       _names.Add(user);
                    break;
                case ReplyCode.RplEndOfNames:
                    OnNamesList(new NamesList(_names));
                    break;
                case ReplyCode.RplNoTopic:
                    Topic = new Topic();
                    break;
            }
        }
Beispiel #4
0
        protected void ProcessReply(object sender, Reply reply)
        {
            switch (reply.Command)
            {
                case "NOTICE":
                    Logger(reply.Trailing);
                    System.Media.SystemSounds.Beep.Play();
                    break;
                case "PING":
                    this.Pong(reply.Trailing);
                    Logger(reply.ToString());
                    break;
                case "JOIN" :
                    if (reply.Params.Count <= 0 && !Channels.ContainsKey(reply.Params[0]))
                        return;
                    break;
                case "MODE":
                    break;
                case "ERROR" :
                    Logger("error here");
                    break;
            }

            int code;
            if (!int.TryParse(reply.Command, out code))
                return;

            switch ((ReplyCode)code)
            {
                // welcome messages
                case ReplyCode.RplWelcome:
                case ReplyCode.RplYourHost:
                case ReplyCode.RplCreated:
                case ReplyCode.RplMyInfo:
                //case ReplyCode.RplMap: // map needs to be handled differently
                //case ReplyCode.RplEndOfMap: // describes that the server supports
                case ReplyCode.RplMotdStart:
                case ReplyCode.RplMotd:
                case ReplyCode.RplMotdAlt:
                case ReplyCode.RplMotdAlt2:
                case ReplyCode.RplMotdEnd:
                case ReplyCode.RplUModeIs:
                // LUser
                case ReplyCode.RplLUserClient:
                case ReplyCode.RplLUserOp:
                case ReplyCode.RplLUserUnknown:
                case ReplyCode.RplLUserChannels:
                case ReplyCode.RplLUserMe:
                case ReplyCode.RplLUserLocalUser:
                case ReplyCode.RplLUserGlobalUser:
                    Logger(reply.Trailing);
                    break;
                default:
                    Debug.WriteLine(reply.Trailing);
                    break;
            }

            Debug.WriteLine(reply.ToString());
        }