Esempio n. 1
0
        public static void OnChannelMessage(object sender, IrcEventArgs e)
        {
            // Active Flags
            User u = null;

            lock (Users.All)
            {
                u = Users.All.Find(x => x.Name.Equals(e.Data.Nick.ToLower()));
            }
            if (u == null)
            {
                u = CreateUser(e.Data.Nick, false);
            }

            u.LastActive = DateTime.Now;
            u.IncrementVotes();
            LastInteraction = DateTime.Now;

            Log(e.Data.Nick + ": " + e.Data.Message);
            if (!_logform.InputEnabled)
            {
                return;                         // Disable Voting UI Flag
            }
            // Website Filters
            string[] explicit_blacklist = { "dotcom", "(dot)", "░", "█" };
            foreach (string m in explicit_blacklist)
            {
                if (e.Data.Message.ToLower().Contains(m))
                {
                    BanUser(u, false, "filter evasion / Unicode art");
                    return;
                }
            }

            Regex         filter           = new Regex(@"[A-z0-9-]+(?:\.[A-z]{2,3})+", RegexOptions.RightToLeft);
            string        stripped_message = Regex.Replace(e.Data.Message, @"[()]", "");
            bool          is_clean         = true;
            List <string> dirty_matches    = new List <string>();

            foreach (Match m in filter.Matches(stripped_message))
            {
                WebsiteFilter website_filter = WebsiteFilters.Find(x => x.Domain.Equals(m.Value));
                if (website_filter != null)
                {
                    if (website_filter.Type == WebsiteFilter.FilterType.BLACKLIST)
                    {
                        BanUser(u, false, "blacklisted website: '" + m.Value + "'");
                    }
                    return;
                }
                else
                {
                    is_clean = false;
                    dirty_matches.Add(m.Value);
                }
            }
            if (!is_clean)
            {
                BanUser(u, true, "unlisted website(s): " + String.Join(", ", dirty_matches));
                return;
            }

            Nominee keyNominee = null;
            string  command    = e.Data.Message.ToUpper();
            bool    downvote   = false;

            if (command.StartsWith("NO") || command.StartsWith("-"))
            {
                downvote = true;
                if (command.StartsWith("NOT"))
                {
                    command = command.Substring(3);
                }
                else if (command.StartsWith("NO"))
                {
                    command = command.Substring(2);
                }
                else if (command.StartsWith("-"))
                {
                    command = command.Substring(1);
                }
                command.TrimStart(' ');
            }
            if (command.StartsWith("@")) // Function Command
            {
                string[] function_msg = command.Substring(1).Split(' ');
                if (function_msg.Count() == 0)
                {
                    return;
                }
                string   opcode  = function_msg.First();
                string[] payload = function_msg.Skip(1).ToArray();

                Console.WriteLine("Function! Opcode: " + opcode + ", Payload: " + String.Join(" ", payload));

                switch (opcode)
                {
                case "BLAME":
                    if (payload.Count() == 0)
                    {
                        return;
                    }
                    switch (payload.First())
                    {
                    case "GOAL": ModFunctions.BlameGoal(u); break;
                    }
                    break;

                case "GET":
                    if (payload.Count() == 0)
                    {
                        return;
                    }
                    switch (payload.First())
                    {
                    case "GOAL": ModFunctions.GetGoal(u); break;

                    case "UPTIME": ModFunctions.GetUptime(u); break;
                    }
                    break;

                case "SET":
                    if (payload.Count() == 0)
                    {
                        return;
                    }
                    switch (payload.First())
                    {
                    case "GOAL": ModFunctions.SetGoal(u, String.Join(" ", payload.Skip(1))); break;

                    case "QUIETMODE": if (ModFunctions.SetQuietMode(u))
                        {
                            SendIRCNotice("Quiet mode on.");
                        }
                        break;

                    case "R9K": if (ModFunctions.SetAprilFools(u))
                        {
                            SendIRCNotice("r9k mode on.");
                        }
                        break;
                    }
                    break;

                case "CLEAR":
                    if (payload.Count() == 0)
                    {
                        return;
                    }
                    switch (payload.First())
                    {
                    case "GOAL": if (!ModFunctions.ClearGoal(u))
                        {
                            Log("Unable to get goal, last get goal was less than 1 minute ago.");
                        }
                        break;

                    case "QUIETMODE": if (ModFunctions.ClearQuietMode(u))
                        {
                            SendIRCNotice("Quiet mode off.");
                        }
                        break;

                    case "R9K": if (ModFunctions.ClearAprilFools(u))
                        {
                            SendIRCNotice("r9k mode off.");
                        }
                        break;
                    }
                    break;

                case "ENABLE":
                    if (payload.Count() == 0)
                    {
                        return;
                    }
                    switch (payload.First())
                    {
                    case "INPUT": break;         // TODO
                    }
                    break;

                case "DISABLE":
                    if (payload.Count() == 0)
                    {
                        return;
                    }
                    switch (payload.First())
                    {
                    case "INPUT": break;         // TODO
                    }
                    break;

                case "SAVE":
                    if (payload.Count() == 0)
                    {
                        return;
                    }
                    switch (payload.First())
                    {
                    case "STATE":
                        if (ModFunctions.SaveState(u))
                        {
                            SendIRCNotice("Saved the twitchplays state.");
                        }
                        break;

                    case "GAME":
                        if (ModFunctions.SaveGame(u))
                        {
                            SendIRCNotice("The game has been saved.");
                        }
                        break;
                    }
                    break;

                case "RELOAD":
                    if (payload.Count() == 0)
                    {
                        return;
                    }
                    switch (payload.First())
                    {
                    case "FILTERS":
                        if (ModFunctions.ReloadFilters(u))
                        {
                            SendIRCNotice("Reloaded filter lists...");
                        }
                        break;

                    case "STATE":
                        if (ModFunctions.LoadState(u))
                        {
                            SendIRCNotice("Reloaded the twitchplays state.");
                        }
                        break;

                    case "GAME":
                        if (ModFunctions.ReloadGame(u))
                        {
                            SendIRCNotice("The game has been reloaded.");
                        }
                        break;
                    }
                    break;

                case "MUTE":     // TODO
                    break;

                default:
                    break;
                }
            }
            else if (command.Equals("ANARCHY"))
            {
                if (IsAprilFools)
                {
                    ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.DEMOCRACY) ? "*DEMOCRACY" : "DEMOCRACY");
                    u.GameMode = User.ModeType.DEMOCRACY;
                }
                else
                {
                    ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.ANARCHY) ? "*ANARCHY" : "ANARCHY");
                    u.GameMode = User.ModeType.ANARCHY;
                }
            }
            else if (command.Equals("DEMOCRACY"))
            {
                if (IsAprilFools)
                {
                    ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.ANARCHY) ? "*ANARCHY" : "ANARCHY");
                    u.GameMode = User.ModeType.ANARCHY;
                }
                else
                {
                    ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.DEMOCRACY) ? "*DEMOCRACY" : "DEMOCRACY");
                    u.GameMode = User.ModeType.DEMOCRACY;
                }
            }
            else
            {
                if (e.Data.Message.Length > 35)
                {
                    return;                             // don't parse long messages
                }
                // Check to make sure it starts with one of our keys.
                foreach (string key in KeyMap.Keys)
                {
                    if (command.StartsWith(key))
                    {
                        goto _goto_valid_command;
                    }
                }
                return; // not valid command

_goto_valid_command:
                Regex regex = new Regex(@"(?:(?<key>[a-z]+)\s*(?<count>\d?\d?))\s*", RegexOptions.IgnoreCase);

                List <KeyType> keys   = new List <KeyType>();
                List <uint>    counts = new List <uint>();
                foreach (Match match in regex.Matches(command))
                {
                    GroupCollection groups = match.Groups;

                    string _key = groups["key"].Value.ToUpper();
                    if (keyMap.ContainsKey(_key))
                    {
                        uint count = 0;
                        UInt32.TryParse(groups["count"].Value, out count);

                        if (count == 0 || (_key.Equals("START") && count > 1))
                        {
                            count = 1;
                        }

                        keys.Add(keyMap[_key]);
                        counts.Add(count);
                    }
                    else
                    {
                        break;
                    }
                }
                if (keys.Count > 0)
                {
                    keyNominee = new Nominee(keys, counts); // Load if already exists
                    if (keyNominee.CountsSum > 20)          // No more than 20 queued inputs
                    {
                        ShowMessage(e.Data.Nick, "#" + keyNominee.Name);
                        return;
                    }

                    Nominee existing = null;
                    if (null != (existing = nominees.Find(x => x.Name.Equals(keyNominee.Name))))
                    {
                        keyNominee = existing;
                    }
                }
            }

            if (keyNominee != null)
            {
                if (GameMode == User.ModeType.ANARCHY) // Live Mode
                {
                    Nominee n = new Nominee(keyNominee.Keys[0], 1);
                    //Thread vJoyThread = new Thread(() => _logform.vJoySendNominee(n));
                    Thread vjoy_thread = new Thread(() => _logform.vJoySendButton((uint)n.Keys[0], 1));
                    vjoy_thread.Start();

                    u.LastNominee = n;
                    ShowMessage(e.Data.Nick, n.Name);
                    return;
                }
                else if (GameMode == User.ModeType.DEMOCRACY)
                {
                    if (!nominees.Contains(keyNominee))
                    {
                        nominees.Add(keyNominee);
                    }

                    string right_msg = (downvote) ? "-" + keyNominee.Name : keyNominee.Name;

                    if (IsVotingClosed)
                    {
                        right_msg = "#" + right_msg;
                    }
                    else
                    {
                        bool contains = false;
                        if (downvote)
                        {
                            lock (Users.Downvoted)
                            {
                                contains = Users.Downvoted.Contains(u);
                            }
                        }
                        else
                        {
                            lock (Users.Voted)
                            {
                                contains = Users.Voted.Contains(u);
                            }
                        }
                        if (contains)
                        {
                            Nominee lastNominee = (downvote) ? u.LastDownNominee : u.LastNominee;

                            if (lastNominee == keyNominee)
                            {
                                right_msg = "*" + right_msg;
                            }
                            else
                            {
                                if (lastNominee != null)
                                {
                                    if (downvote)
                                    {
                                        lastNominee.Vote();
                                    }
                                    else
                                    {
                                        lastNominee.Unvote();
                                    }
                                }
                                if (downvote)
                                {
                                    keyNominee.Unvote();
                                }
                                else
                                {
                                    keyNominee.Vote();
                                }

                                right_msg = ((downvote) ? "-" + lastNominee.Name : lastNominee.Name) + "=>" + right_msg;
                            }
                        }
                        else
                        {
                            if (downvote)
                            {
                                lock (Users.Downvoted)
                                {
                                    Users.Downvoted.Add(u);
                                }
                                keyNominee.Unvote();
                            }
                            else
                            {
                                lock (Users.Voted)
                                {
                                    Users.Voted.Add(u);
                                }
                                keyNominee.Vote();
                            }
                        }

                        if (downvote)
                        {
                            u.LastDownNominee = keyNominee;
                        }
                        else
                        {
                            u.LastNominee = keyNominee;
                        }
                    }

                    ShowMessage(e.Data.Nick, right_msg);
                }
                else
                {
                    // Unable to find key, wtf?
                }
            }
        }
Esempio n. 2
0
        public static void ConnectIRC(LogForm _form)
        {
            if (_form != null)
            {
                _logform = _form;
            }

            if (Thread.CurrentThread.Name == null)
            {
                Thread.CurrentThread.Name = "IRC Thread";

                irc.Encoding             = System.Text.Encoding.UTF8;
                irc.SendDelay            = 200;
                irc.ActiveChannelSyncing = true;
                irc.AutoRetry            = true;

                //irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
                irc.OnError += new Meebey.SmartIrc4net.ErrorEventHandler(OnError);
                //irc.OnPong += new PongEventHandler(DetermineMode);
                irc.OnJoin  += new JoinEventHandler(OnJoin);
                irc.OnPart  += new PartEventHandler(OnPart);
                irc.OnOp    += new OpEventHandler(OnOp);
                irc.OnDeop  += new DeopEventHandler(OnDeop);
                irc.OnNames += new NamesEventHandler(OnNames);
                //irc.OnRawMessage += new IrcEventHandler(OnRawMessage);
                irc.OnChannelMessage += new IrcEventHandler(OnChannelMessage);
                irc.OnConnected      += new EventHandler(OnConnected);
                irc.OnDisconnected   += new EventHandler(OnDisconnected);

                //if (GameMode == User.ModeType.NONE) GameMode = User.ModeType.ANARCHY;

                // KeyMap
                keyMap.Add("HOLD", KeyType.HOLD);
                keyMap.Add("STAY", KeyType.HOLD);
                keyMap.Add("STOP", KeyType.HOLD);
                keyMap.Add("START", KeyType.START);
                keyMap.Add("SELECT", KeyType.SELECT);
                keyMap.Add("UP", KeyType.UP);
                keyMap.Add("NORTH", KeyType.UP);
                keyMap.Add("DOWN", KeyType.DOWN);
                keyMap.Add("SOUTH", KeyType.DOWN);
                keyMap.Add("LEFT", KeyType.LEFT);
                keyMap.Add("WEST", KeyType.LEFT);
                keyMap.Add("RIGHT", KeyType.RIGHT);
                keyMap.Add("EAST", KeyType.RIGHT);
                keyMap.Add("A", KeyType.A);
                keyMap.Add("B", KeyType.B);

                User u = new User("Command-line Argument");
                u.IsAdmin = true;
                ModFunctions.ReloadFilters(u);
            }

            // Connect
            if (username == null || password == null)
            {
                ExitWithMsg("Username or password has not been set!");
            }

            string[] serverlist;
            serverlist = new string[] { server };

            try
            {
                irc.Connect(serverlist, port);
            }
            catch (Exception e)
            {
                //ExitWithError(e, "Couldn't connect.");
            }
        }