Exemplo n.º 1
0
        private async Task ReadLinesBackground(CancellationToken cancellationToken)
        {
            Task cancellationTask = Task.Delay(Timeout.Infinite, cancellationToken);

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                // This allows us to cancel quickly without blocking on network
                Task <string> streamReadTask = _streamReader.ReadLineAsync();
                Task          completedTask  = await Task.WhenAny(streamReadTask, cancellationTask);

                if (completedTask == cancellationTask)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                }

                var message = await HandleLine(streamReadTask.Result);

                if (message.HasValue)
                {
                    if (message.Value.Command == "PRIVMSG")
                    {
                        TwitchChatMessage chatMessage = ParseMessage(message.Value);

                        lock (_outgoingChatMessages)
                        {
                            _outgoingChatMessages.Enqueue(chatMessage);
                        }
                    }
                    else
                    {
                        ServiceEventSource.Current.Message("Unkown IRC command type {0}", message.Value.Command);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private TwitchChatMessage ParseMessage(IrcMessage message)
        {
            if (message.Command != "PRIVMSG")
            {
                //Console.WriteLine($"Unknown command type {message.Command}", Color.Orange);
                return(null);
            }

            var chatMessage = new TwitchChatMessage();

            // Get the nickname from the prefix
            string nickname = message.Prefix.Split('!')[0];

            chatMessage.Nickname   = nickname;
            chatMessage.Message    = message.Parameters[1];
            chatMessage.IrcChannel = message.Parameters[0];

            //Console.Write($"{message.Parameters[0]}: ", Color.MediumSpringGreen);
            //Console.Write($"<{nickname}> ", Color.Aqua);
            //Console.WriteLine(message.Parameters[1], Color.LightSkyBlue);

            if (!string.IsNullOrEmpty(message.TwitchPart))
            {
                string   twitchPart = message.TwitchPart.TrimStart('@');
                string[] parameters = twitchPart.Split(';');

                foreach (string parameter in parameters)
                {
                    (string name, string value) = ParseParameter(parameter);

                    switch (name)
                    {
                    case "color":
                        chatMessage.DisplayColor = value;
                        break;

                    case "display-name":
                        chatMessage.DisplayName = value;
                        break;

                    case "user-id":
                        chatMessage.UserId = int.Parse(value);
                        break;

                    case "room-id":
                        chatMessage.RoomId = int.Parse(value);
                        break;

                    case "user-type":
                        chatMessage.UserType = value;
                        break;

                    case "id":
                        chatMessage.MessageId = Guid.Parse(value);
                        break;

                    case "badges":
                        chatMessage.Badges = value.Split(',');
                        break;

                    case "mod":
                        chatMessage.IsMod = value == "1";
                        break;

                    case "subscriber":
                        chatMessage.IsSubsrciber = value == "1";
                        break;

                    case "turbo":
                        chatMessage.IsTurbo = value == "1";
                        break;

                    case "emotes":
                        chatMessage.Emotes = ParseEmotes(value);
                        break;

                    case "tmi-sent-ts":
                        var      epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                        DateTime ts    = epoch.AddMilliseconds(double.Parse(value));
                        chatMessage.Timestamp = ts;
                        break;

                    default:
                        //Console.WriteLine($"Unkown parameter: {name}={value}", Color.GreenYellow);
                        break;
                    }
                    //Console.WriteLine($"\t{parameter}", Color.Navy);
                }
            }

            // Fallback to using nickname
            if (chatMessage.DisplayName == null)
            {
                chatMessage.DisplayName = chatMessage.Nickname;
            }

            return(chatMessage);

            //_logWriter.AddMessage(chatMessage);
            //Console.WriteLine(JsonConvert.SerializeObject(chatMessage), Color.HotPink);
        }