Ejemplo n.º 1
0
        private void ProcessData(string data, Badge badge)
        {
            string[] ircData = data.Split(' ');

            var indx = 1;

            if (badge.DisplayName == string.Empty || badge.DisplayName == null)
            {
                indx = 1;
            }
            else
            {
                indx = 2;
            }
            var ircCommand = ircData[indx];


            if (data.Length > 4)
            {
                if (data.Substring(0, 4) == "PING")
                {
                    // hardcoded to twitch server
                    Send("PONG :tmi.twitch.tv");
                    return;
                }
            }

            // re-act according to the IRC Commands
            switch (ircCommand)
            {
            case "001":     // server welcome message, after this we can join
                Send("MODE " + nick + " +B");
                log.Add($"Connected to {serverName}");
                OnConnect?.Invoke(this, new EventArgs());
                break;

            case "353":     // member list
            {
                var channel = ircData[5];
                if (ircData.Length > 5)
                {
                    string[] userList = JoinArray(ircData, 5).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                }
            }
            break;

            case "433":
                var takenNick = ircData[4];
                break;

            case "JOIN":     // someone joined
            {
                var channel = ircData[2];
                var user    = ircData[0].Substring(1, ircData[1].IndexOf("!", StringComparison.Ordinal) - 1);
            }
            break;

            case "MODE":     // MODE was set
            {
                var channel = ircData[3];
                if (channel != Username)
                {
                    string from;
                    if (ircData[0].Contains("!"))
                    {
                        from = ircData[1].Substring(1, ircData[1].IndexOf("!", StringComparison.Ordinal) - 1);
                    }
                    else
                    {
                        from = ircData[1].Substring(1);
                    }

                    var to   = ircData[5];
                    var mode = ircData[4];
                }

                // TODO: event for userMode's
            }
            break;

            case "NICK":     // someone changed their nick
                var oldNick = ircData[1].Substring(1, ircData[1].IndexOf("!", StringComparison.Ordinal) - 1);
                var newNick = JoinArray(ircData, 4);

                //Fire_NickChanged(new UserNickChangedEventArgs(oldNick, newNick));
                break;

            case "NOTICE":     // someone sent a notice
            {
                var from    = ircData[1];
                var message = JoinArray(ircData, 4);
                if (from.Contains("!"))
                {
                    from = from.Substring(1, ircData[1].IndexOf('!') - 1);
                    NoticeMessage?.Invoke(this, new NoticeMessageEventArgs(from, message));
                }
                else
                {
                    NoticeMessage?.Invoke(this, new NoticeMessageEventArgs(serverName, message));
                }

                log.Add($"We get NOTICE: {message}");
            }
            break;

            case "PRIVMSG":     // message was sent to the channel or as private
            {
                var from    = ircData[1].Substring(1, ircData[1].IndexOf('!') - 1);
                var to      = ircData[3];
                var message = JoinArray(ircData, 4);

                // if it's a private message
                if (string.Equals(to, nick, StringComparison.CurrentCultureIgnoreCase))
                {
                    PrivateMessage?.Invoke(this, new PrivateMessageEventArgs(from, message));
                }
                else
                {
                    ChannelMessage?.Invoke(this, new ChannelMessageEventArgs(to, from, message, badge));
                }
            }
            break;

            case "WHISPER":
            {
                var from    = ircData[1].Substring(1, ircData[1].IndexOf('!') - 1);
                var to      = ircData[3];
                var message = JoinArray(ircData, 4);

                WhisperMessage?.Invoke(this, new PrivateMessageEventArgs(from, message));
            }
            break;

            case "PART":
            case "QUIT":     // someone left
            {
                var channel = ircData[3];
                var user    = ircData[1].Substring(1, data.IndexOf("!") - 1);

                //Fire_UserLeft(new UserLeftEventArgs(channel, user));
                Send("NAMES " + ircData[3]);
                log.Add("We QUIT");
            }
            break;

            default:
                // still using this while debugging

                if (ircData.Length > 4)
                {
                    ServerMessage?.Invoke(this, JoinArray(ircData, 3));
                }

                break;
            }
        }
Ejemplo n.º 2
0
        private void ParseBagedData(string data)
        {
            // split the data into parts
            string[] ircData = data.Split(' ');

            var ircCommand = ircData[2];
            var tagString  = ircData[0].Substring(1);

            string[] tags = tagString.Split(';');

            // Actually, we can get 3 different tags compound, but we want to know only about two of them
            if (ircCommand == "ROOMSTATE")
            {
                tagString = ircData[0].Substring(1);
                //@broadcaster-lang=;emote-only=0;followers-only=-1;r9k=0;rituals=0;room-id=30773965;slow=0;subs-only=0 :tmi.twitch.tv ROOMSTATE #redcrafting
                RoomStateBadge badge = new RoomStateBadge();
                foreach (var it in tags)
                {
                    string[] pair = it.Split('=');
                    switch (pair[0])
                    {
                    case "broadcaster-lang":
                        badge.BroadcasterLang = pair[1];
                        break;

                    case "followers-only":
                        if (pair[1] == "1")
                        {
                            badge.FollowersOny = true;
                        }
                        break;

                    case "r9k":
                        if (pair[1] == "1")
                        {
                            badge.r9k = true;
                        }
                        break;

                    case "slow":
                        if (pair[1] == "1")
                        {
                            badge.SlowMode = true;
                        }
                        break;

                    case "subs-only":
                        if (pair[1] == "1")
                        {
                            badge.SubChat = true;
                        }
                        break;

                    case "room-id":
                        badge.ChannelID = pair[1];
                        break;
                    }
                }

                RoomStateChanged?.Invoke(data, badge);
            }
            else
            {
                Badge badge = new Badge(false);
                foreach (var it in tags)
                {
                    string[] pair = it.Split('=');
                    switch (pair[0])
                    {
                    case "display-name":
                        badge.DisplayName = pair[1];
                        break;

                    case "subscriber":
                        if (pair[1] == "1")
                        {
                            badge.sub = true;
                        }
                        break;

                    case "turbo":
                        if (pair[1] == "1")
                        {
                            badge.turbo = true;
                        }
                        break;

                    case "mod":
                        if (pair[1] == "1")
                        {
                            badge.mod = true;
                        }
                        break;

                    case "emotes":
                        badge.emotes = pair[1].Split('/');

                        break;
                    }
                }


                ProcessData(data, badge);
            }
        }