Ejemplo n.º 1
0
        // ---- About Command Handler ----

        private void AddAboutHandler()
        {
            string aboutCommand = "^[!@]" + this.ircConfig.Nick + @":?\s+about(\s+(?<pluginName>\w+))?";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = aboutCommand,
                LineAction = this.HandleAboutCommand
            };

            MessageHandler aboutHandler = new MessageHandler(
                config
                );

            this.handlers.Add(aboutHandler);
        }
Ejemplo n.º 2
0
        // ---- Source Command Handler ----

        private void AddSourceHandler()
        {
            string sourceCommand = "^[!@]" + this.ircConfig.Nick + @":?\s+source(\s+(?<pluginName>\w+))?";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = sourceCommand,
                LineAction = this.HandleSourceCommand
            };

            MessageHandler sourceHandler = new MessageHandler(
                config
                );

            this.handlers.Add(sourceHandler);
        }
Ejemplo n.º 3
0
        // ---- Admin Command Handler ----
        private void AddAdminHandler()
        {
            string adminCommand = "^[!@]" + this.ircConfig.Nick + @":?\s+admins";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = adminCommand,
                LineAction = this.HandleAdminCommand
            };

            MessageHandler helpHandler = new MessageHandler(
                config
                );

            this.handlers.Add(helpHandler);
        }
Ejemplo n.º 4
0
        // ---- Help Command Handler ----

        private void AddHelpHandler()
        {
            string helpCommand = "^[!@]" + this.ircConfig.Nick + @":?\s+help(\s+(?<args>.+))?";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = helpCommand,
                LineAction = this.HandleHelpCommand
            };

            MessageHandler helpHandler = new MessageHandler(
                config
                );

            this.handlers.Add(helpHandler);
        }
Ejemplo n.º 5
0
        // -------- Functions --------

        /// <summary>
        /// Initializes the plugin.  This includes loading any configuration files,
        /// starting services, etc.  Allowed to throw Exceptions.
        ///
        /// This function should be used to validates that the environment is good for the plugin.
        /// For example, it has all dependencies installed, config files are in the correct spot, etc.
        /// It should also load GetHandlers() with the handlers.
        /// </summary>
        /// <param name="pluginInit">The class that has information required for initing the plugin.</param>
        public void Init(PluginInitor initor)
        {
            this.urlReader = new UrlReader(initor.Log, initor.HttpClient);

            MessageHandlerConfig msgConfig = new MessageHandlerConfig
            {
                LineRegex  = ".+",
                LineAction = this.HandleMessage
            };

            MessageHandler handler = new MessageHandler(
                msgConfig
                );

            this.handlers.Add(handler);
        }
Ejemplo n.º 6
0
        // ---- Debug Handlers ----

        private void AddDebugHandlers()
        {
            string debugVerbosityCommand = @"^[!@]" + this.ircConfig.Nick + @":?\s+debug\s+verbosity\s+(?<plugin>\S+)\s+(?<verbose>\d+)";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex      = debugVerbosityCommand,
                LineAction     = this.HandleDebugVerbosityCommand,
                ResponseOption = ResponseOptions.PmsOnly // Debug commands will only be with private messages.
            };

            MessageHandler handler = new MessageHandler(
                config
                );

            this.handlers.Add(handler);
        }
Ejemplo n.º 7
0
        // ---------------- Functions ----------------

        /// <summary>
        /// Converts an XML node to a config object.
        /// </summary>
        /// <param name="rng">
        /// Leave this null to use the default RNG, otherwise pass this in if you want to use your own (e.g. with a different seed)
        /// </param>
        public static void Deserialize(
            this MessageHandlerConfig msgConfig,
            XmlNode handlerNode,
            IIrcConfig ircConfig,
            Random rng = null
            )
        {
            IReadOnlyList <string> responses = DeserializeBase(msgConfig, handlerNode);

            // I can not for the life of me figure out how to make this generic between this and
            // action... maybe we can't?
            MessageHandlerAction action = delegate(MessageHandlerArgs args)
            {
                HandleResponse(args, responses, ircConfig, rng);
            };

            msgConfig.LineAction = action;
        }
Ejemplo n.º 8
0
        // -------- Functions --------

        /// <summary>
        /// Initializes the plugin.  This includes loading any configuration files,
        /// starting services, etc.
        /// </summary>
        /// <param name="pluginInit">The class that has information required for initing the plugin.</param>

        public void Init(PluginInitor initor)
        {
            string configPath = Path.Combine(
                initor.ChaskisConfigPluginRoot,
                "CowSayBot",
                "CowSayBotConfig.xml"
                );

            if (File.Exists(configPath) == false)
            {
                throw new FileNotFoundException(
                          "Can not open " + configPath
                          );
            }

            this.cowSayConfig = XmlLoader.LoadCowSayBotConfig(configPath);

            if (File.Exists(cowSayConfig.ExeCommand) == false)
            {
                throw new InvalidOperationException("Can not load cowsay program from " + cowSayConfig.ExeCommand);
            }

            this.cowSayInfo.FileName = cowSayConfig.ExeCommand;
            this.cowsayRegex         = ConstructRegex(this.cowSayConfig);

            Console.WriteLine("CowSayBot: Using Regex '" + this.cowsayRegex + "'");

            MessageHandlerConfig cowsayHandlerConfig = new MessageHandlerConfig
            {
                LineRegex      = this.cowsayRegex,
                LineAction     = this.HandleCowsayCommand,
                CoolDown       = (int)cowSayConfig.CoolDownTimeSeconds,
                ResponseOption = ResponseOptions.ChannelOnly
            };

            IIrcHandler cowSayHandler = new MessageHandler(
                cowsayHandlerConfig
                );

            this.handlers.Add(
                cowSayHandler
                );
        }
Ejemplo n.º 9
0
        public void TestMisMatchedMessage()
        {
            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = @"!bot\s+help",
                LineAction = this.MessageFunction
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            // Does not match pattern.  No response expected.
            const string expectedMessage = "hello world!";
            string       ircString       = this.GenerateMessage(remoteUser, this.ircConfig.Channels[0], expectedMessage);

            uut.HandleEvent(this.ConstructArgs(ircString));

            Assert.IsNull(this.responseReceived);
        }
Ejemplo n.º 10
0
        public void TestBridgeUserNoResponse()
        {
            const string expectedMessage = "hello world!";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = @"!bot\s+help",
                LineAction = this.MessageFunction
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            string ircString = this.GenerateMessage(TestHelpers.BridgeBotUser, this.ircConfig.Channels[0], remoteUser + ": " + expectedMessage);

            uut.HandleEvent(this.ConstructArgs(ircString));

            Assert.IsNull(this.responseReceived);
        }
Ejemplo n.º 11
0
        public void TestChannelInPmOnlyMessage()
        {
            const string expectedMessage = "!bot help";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex      = @"!bot\s+help",
                LineAction     = this.MessageFunction,
                ResponseOption = ResponseOptions.PmsOnly
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            string ircString = this.GenerateMessage(remoteUser, this.ircConfig.Channels[0], expectedMessage);

            uut.HandleEvent(this.ConstructArgs(ircString));

            Assert.IsNull(this.responseReceived);
        }
Ejemplo n.º 12
0
        public void IgnoreCtcpPingTest()
        {
            const string expectedMessage = "\u0001PING 1234567890\u0001";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex     = @".+", // Take any string so if we weren't filtering out PING, it would incorrectly trigger
                LineAction    = this.MessageFunction,
                RespondToSelf = false
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            string ircString = this.GenerateMessage(remoteUser, this.ircConfig.Channels[0], expectedMessage);

            uut.HandleEvent(this.ConstructArgs(ircString));

            Assert.IsNull(this.responseReceived);
        }
Ejemplo n.º 13
0
        public void DontRespondToSelfTest()
        {
            const string expectedMessage = "!bot help";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex     = @"!bot\s+help",
                LineAction    = this.MessageFunction,
                RespondToSelf = false
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            string ircString = this.GenerateMessage(this.ircConfig.Nick, this.ircConfig.Nick, expectedMessage);

            uut.HandleEvent(this.ConstructArgs(ircString));

            Assert.IsNull(this.responseReceived);
        }
Ejemplo n.º 14
0
        public void TestBridgeUserMessageIsSameAsRemoteName()
        {
            const string expectedMessage = remoteUser;

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = @".+",
                LineAction = this.MessageFunction
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            string ircString = this.GenerateMessage(TestHelpers.BridgeBotUser, this.ircConfig.Channels[0], remoteUser + ": " + expectedMessage);

            uut.HandleEvent(this.ConstructArgs(ircString));

            Assert.AreEqual(this.ircConfig.Channels[0], this.responseReceived.Channel);
            Assert.AreEqual(remoteUser, this.responseReceived.User);
            Assert.AreEqual(expectedMessage, this.responseReceived.Message);
        }
Ejemplo n.º 15
0
        public void TestBridgeBotNotBridgedMessage()
        {
            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = @"!bot\s+help",
                LineAction = this.MessageFunction
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            const string expectedMessage = "!bot help";

            string ircString = this.GenerateMessage(TestHelpers.BridgeBotUser, this.ircConfig.Channels[0], expectedMessage);

            uut.HandleEvent(this.ConstructArgs(ircString));

            Assert.AreEqual(this.ircConfig.Channels[0], this.responseReceived.Channel);
            Assert.AreEqual(TestHelpers.BridgeBotUser, this.responseReceived.User);
            Assert.AreEqual(expectedMessage, this.responseReceived.Message);
        }
Ejemplo n.º 16
0
        // ---- Version Command Handler ----

        private void AddVersionHandler()
        {
            string versionCommand = "^[!@]" + this.ircConfig.Nick + @":?\s+version(\s+(?<pluginName>\w+))?";

            MessageHandlerConfig messageHandlerConfig = new MessageHandlerConfig
            {
                LineRegex  = versionCommand,
                LineAction = this.HandleVersionCommand
            };

            MessageHandler versionHandler = new MessageHandler(
                messageHandlerConfig
                );

            this.handlers.Add(versionHandler);

            ChaskisEventHandler chaskisHandler = this.chaskisEventCreator.CreatePluginEventHandler(
                this.HandleChaskisVersionCommand
                );

            this.handlers.Add(chaskisHandler);
        }
Ejemplo n.º 17
0
        public void ValidateTest()
        {
            MessageHandlerConfig config = new MessageHandlerConfig();

            // No action should not validate.
            config.LineAction = null;
            config.LineRegex  = @"!bot\s+help";
            Assert.Throws <ListedValidationException>(() => config.Validate());

            // Empty regex should not validate.
            config.LineAction = delegate(MessageHandlerArgs args)
            {
            };
            config.LineRegex = string.Empty;
            Assert.Throws <ListedValidationException>(() => config.Validate());

            // Null regex should not validate.
            config.LineAction = delegate(MessageHandlerArgs args)
            {
            };
            config.LineRegex = null;
            Assert.Throws <ListedValidationException>(() => config.Validate());

            // Cooldown can not be less than 0.
            config.LineAction = delegate(MessageHandlerArgs args)
            {
            };
            config.LineRegex = @"!bot\s+help";
            config.CoolDown  = -1;
            Assert.Throws <ListedValidationException>(() => config.Validate());

            // This should validate.
            config.LineAction = delegate(MessageHandlerArgs args)
            {
            };
            config.LineRegex = @"!bot\s+help";
            config.CoolDown  = 1;
            Assert.DoesNotThrow(() => config.Validate());
        }
Ejemplo n.º 18
0
        // -------- Test Helpers --------

        private void DoGoodMessageTest(string user, string channel)
        {
            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = @"!bot\s+help",
                LineAction = this.MessageFunction
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            const string expectedMessage = "!bot help";

            string ircString = this.GenerateMessage(user, channel, expectedMessage);

            uut.HandleEvent(this.ConstructArgs(ircString));

            Assert.AreEqual(channel, this.responseReceived.Channel);
            Assert.AreEqual(user, this.responseReceived.User);
            Assert.AreEqual(expectedMessage, this.responseReceived.Message);
        }
Ejemplo n.º 19
0
        public void TestBridgeUserRegex()
        {
            const string expectedMessage = "!bot help";

            this.ircConfig.BridgeBots.Remove(TestHelpers.BridgeBotUser);
            this.ircConfig.BridgeBots.Add(TestHelpers.BridgeBotUser + @"\d*", @"(?<bridgeUser>[\w\d]+):\s+(?<bridgeMessage>.+)");

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex  = @"!bot\s+help",
                LineAction = this.MessageFunction
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            for (int i = 0; i < 5; ++i)
            {
                string bridgeBotNick;
                if (i == 0)
                {
                    bridgeBotNick = TestHelpers.BridgeBotUser;
                }
                else
                {
                    bridgeBotNick = TestHelpers.BridgeBotUser + (10 * i);
                }

                string ircString = this.GenerateMessage(bridgeBotNick, this.ircConfig.Channels[0], remoteUser + ": " + expectedMessage);
                uut.HandleEvent(this.ConstructArgs(ircString));

                Assert.AreEqual(this.ircConfig.Channels[0], this.responseReceived.Channel);
                Assert.AreEqual(remoteUser, this.responseReceived.User);
                Assert.AreEqual(expectedMessage, this.responseReceived.Message);
            }
        }
Ejemplo n.º 20
0
        public void TestPmOnlyMessage()
        {
            const string expectedMessage = "!bot help";

            MessageHandlerConfig config = new MessageHandlerConfig
            {
                LineRegex      = @"!bot\s+help",
                LineAction     = this.MessageFunction,
                ResponseOption = ResponseOptions.PmsOnly
            };

            MessageHandler uut = new MessageHandler(
                config
                );

            string ircString = this.GenerateMessage(remoteUser, this.ircConfig.Nick, expectedMessage);

            uut.HandleEvent(this.ConstructArgs(ircString));

            // Need to set the remote user as the channel so the message goes to the right channel.
            Assert.AreEqual(remoteUser, this.responseReceived.Channel);
            Assert.AreEqual(remoteUser, this.responseReceived.User);
            Assert.AreEqual(expectedMessage, this.responseReceived.Message);
        }
Ejemplo n.º 21
0
        public void Init(PluginInitor pluginInit)
        {
            this.log = pluginInit.Log;

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = sleepPattern,
                    LineAction = this.HandleSleep
                };

                MessageHandler sleepHandler = new MessageHandler(
                    msgConfig
                    );

                this.handlers.Add(sleepHandler);
            }

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = forceSleepPattern,
                    LineAction = this.HandleForceSleep
                };

                MessageHandler forceSleepHandler = new MessageHandler(
                    msgConfig
                    );

                this.handlers.Add(forceSleepHandler);
            }

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = canaryPattern,
                    LineAction = this.HandleCanary
                };

                MessageHandler canaryHandler = new MessageHandler(
                    msgConfig
                    );

                this.handlers.Add(canaryHandler);
            }

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = asyncAwaitThreadTestPattern,
                    LineAction = this.DoAsyncTest
                };

                MessageHandler asyncHandler = new MessageHandler(
                    msgConfig
                    );

                this.handlers.Add(asyncHandler);
            }

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = asyncAwaitExceptionTestPattern,
                    LineAction = this.DoAsyncExceptionTest
                };

                MessageHandler asyncHandler = new MessageHandler(
                    msgConfig
                    );

                this.handlers.Add(asyncHandler);
            }
        }
Ejemplo n.º 22
0
        // -------- Functions --------

        /// <summary>
        /// Inits this plugin.
        /// </summary>
        /// <param name="pluginInit">The class that has information required for initing the plugin.</param>
        public void Init(PluginInitor initor)
        {
            string karmaBotRoot = Path.Combine(
                initor.ChaskisConfigPluginRoot,
                "KarmaBot"
                );

            string dbPath = Path.Combine(
                karmaBotRoot,
                "karmabot.ldb"
                );

            string configPath = Path.Combine(
                karmaBotRoot,
                "KarmaBotConfig.xml"
                );

            if (File.Exists(configPath) == false)
            {
                throw new FileNotFoundException(
                          "Can not open " + configPath
                          );
            }

            this.config       = XmlLoader.LoadKarmaBotConfig(configPath);
            this.dataBase     = new KarmaBotDatabase(dbPath);
            this.eventSender  = initor.ChaskisEventSender;
            this.eventCreator = initor.ChaskisEventCreator;

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = this.config.IncreaseCommandRegex,
                    LineAction = HandleIncreaseCommand
                };

                MessageHandler increaseHandler = new MessageHandler(
                    msgConfig
                    );
                this.handlers.Add(increaseHandler);
            }

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = this.config.DecreaseCommandRegex,
                    LineAction = HandleDecreaseCommand
                };

                MessageHandler decreaseCommand = new MessageHandler(
                    msgConfig
                    );
                this.handlers.Add(decreaseCommand);
            }

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = this.config.QueryCommand,
                    LineAction = HandleQueryCommand
                };

                MessageHandler queryCommand = new MessageHandler(
                    msgConfig
                    );
                this.handlers.Add(queryCommand);
            }

            {
                ChaskisEventHandler chaskisQuery = initor.ChaskisEventCreator.CreatePluginEventHandler(
                    this.HandleChaskisQueryCommand
                    );
                this.handlers.Add(chaskisQuery);
            }
        }
Ejemplo n.º 23
0
        // -------- Functions --------

        /// <summary>
        /// Inits this plugin.
        /// </summary>
        /// <param name="pluginInit">The class that has information required for initing the plugin.</param>
        public void Init(PluginInitor initor)
        {
            string configPath = Path.Combine(
                initor.ChaskisConfigPluginRoot,
                "ServerDiagnostics",
                "ServerDiagnosticsConfig.xml"
                );

            this.config    = XmlLoader.LoadConfig(configPath);
            this.ircConfig = initor.IrcConfig;

            if (string.IsNullOrEmpty(config.UpTimeCmd) == false)
            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = config.UpTimeCmd,
                    LineAction = HandleUpTimeCmd,
                    CoolDown   = coolDown
                };

                MessageHandler utimeHandler = new MessageHandler(
                    msgConfig
                    );
                this.handlerList.Add(utimeHandler);
            }

            if (string.IsNullOrEmpty(config.OsVersionCmd) == false)
            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = config.OsVersionCmd,
                    LineAction = HandleOsVersionCmd,
                    CoolDown   = coolDown
                };

                MessageHandler osHandler = new MessageHandler(
                    msgConfig
                    );
                this.handlerList.Add(osHandler);
            }

            if (string.IsNullOrEmpty(config.ProcessorCountCmd) == false)
            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = config.ProcessorCountCmd,
                    LineAction = HandleProcessorCountCmd,
                    CoolDown   = coolDown
                };

                MessageHandler procCoundHandler = new MessageHandler(
                    msgConfig
                    );
                this.handlerList.Add(procCoundHandler);
            }

            if (string.IsNullOrEmpty(config.TimeCmd) == false)
            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = config.TimeCmd,
                    LineAction = HandleTimeCmd,
                    CoolDown   = coolDown
                };

                MessageHandler timeHandler = new MessageHandler(
                    msgConfig
                    );
                this.handlerList.Add(timeHandler);
            }
        }
Ejemplo n.º 24
0
        // ---------------- Functions ----------------

        /// <summary>
        /// Initializes the plugin.
        /// </summary>
        /// <param name="pluginInit">The class that has information required for initing the plugin.</param>
        public void Init(PluginInitor initor)
        {
            string quoteBotRoot = Path.Combine(
                initor.ChaskisConfigPluginRoot,
                "QuoteBot"
                );

            string configPath = Path.Combine(
                quoteBotRoot,
                "QuoteBotConfig.xml"
                );

            if (File.Exists(configPath) == false)
            {
                throw new FileNotFoundException(
                          "Can not open " + configPath
                          );
            }

            this.ircConfig = initor.IrcConfig;

            this.quoteBotConfig = XmlLoader.LoadConfig(configPath);
            this.parser         = new QuoteBotParser(this.quoteBotConfig);
            this.db             = new QuoteBotDatabase(Path.Combine(quoteBotRoot, "quotes.ldb"));

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = this.quoteBotConfig.AddCommand,
                    LineAction = this.AddHandler
                };

                MessageHandler addHandler = new MessageHandler(
                    msgConfig
                    );
                this.handlers.Add(addHandler);
            }

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = this.quoteBotConfig.DeleteCommand,
                    LineAction = this.DeleteHandler
                };

                MessageHandler deleteHandler = new MessageHandler(
                    msgConfig
                    );
                this.handlers.Add(deleteHandler);
            }

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = this.quoteBotConfig.RandomCommand,
                    LineAction = this.RandomHandler
                };

                MessageHandler randomHandler = new MessageHandler(
                    msgConfig
                    );
                this.handlers.Add(randomHandler);
            }

            {
                MessageHandlerConfig msgConfig = new MessageHandlerConfig
                {
                    LineRegex  = this.quoteBotConfig.GetCommand,
                    LineAction = this.GetHandler
                };

                MessageHandler getHandler = new MessageHandler(
                    msgConfig
                    );
                this.handlers.Add(getHandler);
            }
        }