Example #1
0
        internal void Update(ChatResponse msg)
        {
            int nameEndIndex = msg.Prefix.IndexOf('!');

            UserName = msg.Prefix.Substring(0, nameEndIndex);

            Id = msg.Tags["id"];

            if (msg.Tags.TryGetValue("sent-ts", out string sentTs))
            {
                SentTimestamp = DateTimeHelper.GetDateTime(sentTs);
            }

            TmiSentTimestamp = DateTimeHelper.GetDateTime(msg.Tags["tmi-sent-ts"]);
            Badges           = msg.Tags["badges"];
            Emotes           = msg.Tags["emotes"];

            ChannelId = ulong.Parse(msg.Tags["room-id"]);

            UserId       = ulong.Parse(msg.Tags["user-id"]);
            Color        = msg.Tags["color"];
            DisplayName  = msg.Tags["display-name"];
            UserType     = msg.Tags["user-type"];
            IsMod        = msg.Tags["mod"] == "1";
            IsSubscriber = msg.Tags["subscriber"] == "1";
            IsTurbo      = msg.Tags["turbo"] == "1";

            ChannelName = msg.Parameters.First().Substring(1);
            Content     = msg.Parameters.Last().Trim();
        }
Example #2
0
        }                                   // Last

        internal static MessageReceivedEvent Create(ChatResponse msg)
        {
            var entity = new MessageReceivedEvent();

            entity.Update(msg);
            return(entity);
        }
Example #3
0
        }                                       // First

        internal static UserStateEvent Create(ChatResponse msg)
        {
            var entity = new UserStateEvent();

            entity.Update(msg);
            return(entity);
        }
Example #4
0
        internal void Update(ChatResponse msg)
        {
            Badges       = msg.Tags["badges"];
            Color        = msg.Tags["color"];
            DisplayName  = msg.Tags["display-name"];
            EmoteSets    = msg.Tags["emote-sets"];
            IsMod        = msg.Tags["mod"] == "1";
            IsSubscriber = msg.Tags["subscriber"] == "1";
            UserType     = msg.Tags["user-type"];

            ChannelName = msg.Parameters.First().Substring(1).Trim();
        }
Example #5
0
        public static ChatResponse Parse(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                throw new FormatException("Input string is empty.");
            }

            var msg = new ChatResponse();
            int pos = 0;
            int next;

            // Parse tags
            if (input[0] == '@')
            {
                next = input.IndexOf(' ');
                if (next < 0)
                {
                    throw new FormatException();
                }

                string tagPart = input.Substring(1, next - 1);
                var    rawTags = tagPart.Split(';');

                foreach (var tag in rawTags.Select(x => x.Split('=')))
                {
                    string key   = tag[0];
                    string value = tag[1] == "" ? null : tag[1];

                    msg.Tags.Add(key, value);
                }

                pos = next + 1;
            }

            // Parse prefix
            if (input[pos] == ':')
            {
                next = input.IndexOf(' ', pos);
                if (next < 0)
                {
                    throw new FormatException();
                }

                string prefixPart = input.Substring(pos + 1, next - pos - 1);
                msg.Prefix = prefixPart;

                pos = next + 1;
            }

            // Check for command and parameters
            next = input.IndexOf(' ', pos);
            if (next < 0)
            {
                string cmdPart = input.Substring(pos);
                msg.Command = cmdPart;
            }
            else
            {
                string cmdPart = input.Substring(pos, next - pos);
                msg.Command = cmdPart;

                pos = next + 1;
                while (pos < input.Length)
                {
                    bool hasContent = input[pos] == ':';
                    if (hasContent || next < 0)
                    {
                        string contentPart = input.Substring(hasContent ? pos + 1 : pos);
                        msg.Parameters.Add(contentPart);
                        break;
                    }

                    next = input.IndexOf(' ', pos);

                    if (next > -1)
                    {
                        string paramPart = input.Substring(pos, next - pos);
                        msg.Parameters.Add(paramPart);

                        pos = next + 1;
                    }
                }
            }

            return(msg);
        }