Ejemplo n.º 1
0
        void OnPrivMsg(TwitchIRCMessage message)
        {
            //if (activeChatWindow != null && !message.Message.StartsWith("!") && message.User != ToolkitSettings.Username)
            //{
            //    if ((_voteActive && !int.TryParse(message.Message[0].ToString(), out int result)) || !_voteActive)
            //    {
            //        activeChatWindow.AddMessage(
            //            message.Message,
            //            message.User,
            //            (message.Parameters.ContainsKey("color")) ? message.Parameters["color"].Remove(0, 1) : Viewer.GetViewerColorCode(message.User)
            //        );
            //    }

            //}

            message.Viewer = TwitchViewer.GetTwitchViewer(message.User);

            if (message.Viewer == null)
            {
                Helper.Log(message.User.CapitalizeFirst() + " tried to run a command but is not registed.");
                return;
            }

            if (Helper.ModActive)
            {
                CommandsHandler.CheckCommand(message);
            }

            if (VoteHandler.voteActive && int.TryParse(message.Message[0].ToString(), out int voteKey))
            {
                VoteHandler.currentVote.RecordVote(Viewers.GetViewer(message.User).id, voteKey - 1);
            }
        }
Ejemplo n.º 2
0
 public abstract void ParseCommand(TwitchIRCMessage msg);
Ejemplo n.º 3
0
        public void Parse(byte[] buffer, int length, OnMessage callback)
        {
            Decoder decoder = Helper.LanguageEncoding().GetDecoder();

            char[] chars;

            int charCount = decoder.GetCharCount(buffer, 0, buffer.Length);

            chars = new Char[charCount];
            int charLength = decoder.GetChars(buffer, 0, buffer.Length, chars, 0, true);

            _state = IRCParserState.Start;
            for (int i = 0; i < length; i++)
            {
                if (i > 8024)
                {
                    continue;
                }
                var b = chars[i];
                switch (_state)
                {
                case IRCParserState.Start:
                    //Helper.Log($"{_state} starts at {i} {b}");
                    string msg = "";
                    foreach (char c in chars)
                    {
                        msg += c;
                    }
                    _message = new TwitchIRCMessage();
                    _key     = "";
                    _value   = "";
                    switch (b)
                    {
                    case 'P':
                        _state = IRCParserState.P;
                        break;

                    case '@':
                        _state = IRCParserState.Key;
                        break;

                    case ':':
                        _state = IRCParserState.Host;
                        break;

                    default:
                        _state = IRCParserState.Unknown;
                        break;
                    }
                    break;

                case IRCParserState.P:
                    if (b == 'I')
                    {
                        _state = IRCParserState.Pi;
                    }
                    else if (b == 'O')
                    {
                        _state = IRCParserState.Po;
                    }
                    else
                    {
                        _state = IRCParserState.Unknown;
                    }
                    break;

                case IRCParserState.Pi:
                    if (b == 'N')
                    {
                        _state = IRCParserState.Pin;
                    }
                    else
                    {
                        _state = IRCParserState.Unknown;
                    }
                    break;

                case IRCParserState.Pin:
                    if (b == 'G')
                    {
                        _state = IRCParserState.Ping;
                    }
                    else
                    {
                        _state = IRCParserState.Unknown;
                    }
                    break;

                case IRCParserState.Ping:
                    if (b == '\n')
                    {
                        _message.Cmd = "PING";
                        callback(_message);
                        _state = IRCParserState.Start;
                    }
                    break;

                case IRCParserState.Po:
                    if (b == 'N')
                    {
                        _state = IRCParserState.Pon;
                    }
                    else
                    {
                        _state = IRCParserState.Unknown;
                    }
                    break;

                case IRCParserState.Pon:
                    if (b == 'G')
                    {
                        _state = IRCParserState.Pong;
                    }
                    else
                    {
                        _state = IRCParserState.Unknown;
                    }
                    break;

                case IRCParserState.Pong:
                    if (b == '\n')
                    {
                        _message.Cmd = "PONG";
                        callback(_message);
                        _state = IRCParserState.Start;
                    }
                    break;

                case IRCParserState.Host:
                    if (b == ' ')
                    {
                        _state = IRCParserState.Cmd;
                    }
                    else if (b == '@')
                    {
                        _state = IRCParserState.User;
                    }
                    else if (b != '\r')
                    {
                        _message.Host += b;
                    }
                    break;

                case IRCParserState.User:
                    if (b == '.')
                    {
                        _state = IRCParserState.Host;
                    }
                    else
                    {
                        _message.User += b;
                    }
                    break;

                case IRCParserState.Cmd:
                    if (b == '\n')
                    {
                        callback(_message);
                        _state = IRCParserState.Start;
                    }
                    else if (b == ' ')
                    {
                        _state = IRCParserState.Args;
                    }
                    else if (b != '\r')
                    {
                        _message.Cmd += b;
                    }
                    break;

                case IRCParserState.Args:
                    if (b == '\n')
                    {
                        if (Array.IndexOf(new string[] { "PRIVMSG", "WHISPER", "USERSTATE", "USERNOTICE", "NOTICE" }, _message.Cmd) != -1)
                        {
                            int state = 0;
                            for (int j = 0; j < _message.Args.Length; j++)
                            {
                                switch (state)
                                {
                                case 0:
                                    if (_message.Args[j] == ' ')
                                    {
                                        state = 1;
                                    }
                                    else
                                    {
                                        _message.Channel += _message.Args[j];
                                    }
                                    break;

                                case 1:
                                    if (_message.Args[j] == ':')
                                    {
                                        state = 2;
                                    }
                                    break;

                                default:
                                    _message.Message += _message.Args[j];
                                    break;
                                }
                            }
                        }
                        callback(_message);
                        _state = IRCParserState.Start;
                    }
                    else if (b != '\r')
                    {
                        _message.Args += b;
                    }
                    break;

                case IRCParserState.Unknown:
                    if (b == '\n')
                    {
                        _state = IRCParserState.Start;
                    }
                    break;

                case IRCParserState.Key:
                    if (b == '=')
                    {
                        _state = IRCParserState.Value;
                    }
                    else if (b == ' ')
                    {
                        _state = IRCParserState.Host;
                    }
                    else
                    {
                        _key += b;
                    }
                    break;

                case IRCParserState.Value:
                    if (b == ';' || b == ' ')
                    {
                        _message.Parameters.Add(_key, _value);
                        _key   = "";
                        _value = "";
                    }

                    if (b == ';')
                    {
                        _state = IRCParserState.Key;
                    }
                    else if (b == ' ')
                    {
                        _state = IRCParserState.Host;
                    }
                    else
                    {
                        _value += b;
                    }
                    break;

                default:
                    _state = IRCParserState.Start;
                    break;
                }
            }
        }
Ejemplo n.º 4
0
        void OnCommand(TwitchIRCMessage message)
        {
            Ticker.LastIRCPong = DateTime.Now.ToFileTime();

            _ircMessages.Put(message.Cmd + " " + message.Args);

            switch (message.Cmd)
            {
            case "USERSTATE":
                if (message.Parameters.ContainsKey("mod") && message.Parameters["mod"] == "1")
                {
                    _messageInterval = 1;
                }
                else
                {
                    _messageInterval = 2;
                }
                break;

            case "PING":
                Send("PONG\n");
                break;

            case "376":
                if (ToolkitSettings.UseSeparateChatRoom && ToolkitSettings.ChatroomUUID != "" && ToolkitSettings.ChannelID != "")
                {
                    Send(
                        "CAP REQ :twitch.tv/membership\n" +
                        "CAP REQ :twitch.tv/tags\n" +
                        "CAP REQ :twitch.tv/commands\n" +
                        "JOIN #" + _channel + "\n" +
                        "JOIN #chatrooms:" + ToolkitSettings.ChannelID + ":" + ToolkitSettings.ChatroomUUID + "\n"
                        );
                }
                else
                {
                    Send(
                        "CAP REQ :twitch.tv/membership\n" +
                        "CAP REQ :twitch.tv/tags\n" +
                        "CAP REQ :twitch.tv/commands\n" +
                        "JOIN #" + _channel + "\n"
                        );
                }

                _socketReady = true;
                break;

            case "PRIVMSG":
                if (OnPrivMsg != null && !ToolkitSettings.WhisperCmdsOnly)
                {
                    OnPrivMsg.Invoke(message);
                }
                break;

            case "WHISPER":
                if (OnPrivMsg != null && ToolkitSettings.WhisperCmdsAllowed)
                {
                    message.Whisper = true;
                    OnPrivMsg.Invoke(message);
                }
                break;

            case "PONG":
                break;

            default:
                if (OnUnkwnMsg != null)
                {
                    OnUnkwnMsg.Invoke(message);
                }
                break;
            }
        }
Ejemplo n.º 5
0
        public override void ParseCommand(TwitchIRCMessage msg)
        {
            foreach (KeyValuePair <string, string> pair in msg.Parameters)
            {
                //Helper.Log(pair.Key + " : " + pair.Value);
                switch (pair.Key)
                {
                case "color":
                    if (pair.Value == null)
                    {
                        break;
                    }
                    string colorCode = "";
                    if (pair.Value.Length > 6)
                    {
                        colorCode = pair.Value.Remove(0, 1);
                        ToolkitSettings.ViewerColorCodes[msg.User.ToLower()] = colorCode;
                    }
                    else
                    {
                        break;
                    }
                    GameComponentPawns component = Current.Game.GetComponent <GameComponentPawns>();

                    if (component.HasUserBeenNamed(msg.User))
                    {
                        Pawn pawn = component.PawnAssignedToUser(msg.User);

                        pawn.story.hairColor = GetColorFromHex(colorCode);
                    }

                    break;

                case "mod":
                    if (msg.Viewer.Mod || pair.Value == null)
                    {
                        break;
                    }
                    bool modValue = int.TryParse(pair.Value, out int modStatus);
                    if (!msg.Viewer.Mod && modValue && modStatus == 1)
                    {
                        msg.Viewer.ToggleMod();
                    }
                    break;

                case "subscriber":
                    if (pair.Value == null)
                    {
                        break;
                    }
                    bool subValue = int.TryParse(pair.Value, out int subStatus);
                    if (subValue && subStatus == 1)
                    {
                        msg.Viewer.Subscriber = true;
                    }
                    else
                    {
                        msg.Viewer.Subscriber = false;
                    }
                    break;

                case "badges":
                    if (pair.Value == null)
                    {
                        break;
                    }
                    IEnumerable <string> badges = pair.Value.Split(',');
                    foreach (string badge in badges)
                    {
                        if (badge == "vip/1")
                        {
                            msg.Viewer.VIP = true;
                            break;
                        }
                    }
                    msg.Viewer.VIP = false;
                    break;
                }
            }

            Store_Logger.LogString("Parsed command parameters");
        }