Esempio n. 1
0
        /// <summary>
        /// Handles the response when the user wants to calculate something.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="response"></param>
        private static void MathHandler(IIrcWriter writer, IrcResponse response)
        {
            Match  match      = Regex.Match(response.Message, handlerRegex);
            string expression = match.Groups["expression"].Value;

            if (match.Success)
            {
                try
                {
                    string answer = MathBotCalculator.Calculate(expression);
                    writer.SendMessageToUser(
                        "'" + expression + "' calculates to '" + answer + "'",
                        response.Channel
                        );
                }
                catch (Exception)
                {
                    writer.SendMessageToUser(
                        "'" + expression + "' is not something I can calculate :(",
                        response.Channel
                        );
                }
            }
            else
            {
                writer.SendMessageToUser(
                    "'" + expression + "' is not something I can calculate :(",
                    response.Channel
                    );
            }
        }
Esempio n. 2
0
 public void TestSetup()
 {
     this.ircConfig   = TestHelpers.GetTestIrcConfig();
     ircConnection    = new MockIrcConnection(this.ircConfig);
     responseReceived = null;
     this.uut         = new PartHandler(PartFunction);
 }
Esempio n. 3
0
 /// <summary>
 /// Handles a message from the channel.
 /// </summary>
 /// <param name="writer">The IRC Writer to write to.</param>
 /// <param name="response">The response from the channel.</param>
 private void HandleMessage(IIrcWriter writer, IrcResponse response)
 {
     if (CheckForCaps(response.Message))
     {
         string msgToSend = SelectMessage().Replace("{%user%}", response.RemoteUser);
         writer.SendCommandToChannel(msgToSend);
     }
 }
Esempio n. 4
0
        // ---- Handlers ----

        /// <summary>
        /// Ran when someone joins the channel.
        /// </summary>
        /// <param name="writer">The means to write to an IRC channel.</param>
        /// <param name="response">The command from the server.</param>
        private static void JoinMessage(IIrcWriter writer, IrcResponse response)
        {
            writer.SendMessageToUser(
                "Greetings " + response.RemoteUser + ", welcome to " + response.Channel + "!",
                response.RemoteUser
                );
            writer.SendCommandToChannel(response.RemoteUser + " has joined " + response.Channel);
        }
Esempio n. 5
0
 public void TestSetup()
 {
     this.uut = new IrcResponse(
         "SomeUser",
         "#SomeChannel",
         "Some Message"
         );
 }
Esempio n. 6
0
        /// <summary>
        /// Handles the time command.
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private static void HandleTimeCmd(IIrcWriter writer, IrcResponse response)
        {
            DateTime time = DateTime.UtcNow;

            writer.SendCommandToChannel(
                "My time is " + time.ToString("yyyy-MM-dd hh:mm:ss") + " UTC."
                );
        }
Esempio n. 7
0
        public void TestBridgeUserRegexMultipleBridgeBots()
        {
            const string expectedMessage = "!bot help";
            const string bridgeBotUser2  = "slackbot";

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

            MessageHandler uut = new MessageHandler(
                @"!bot\s+help",
                MessageFunction
                );

            for (int i = 0; i < 5; ++i)
            {
                string bridgeBotNick;
                if (i == 0)
                {
                    bridgeBotNick = bridgeBotUser2;
                }
                else
                {
                    bridgeBotNick = bridgeBotUser2 + i;
                }

                // This is the wrong regex, ensure the remote user is set to the bot, not the bridged user.
                {
                    string expectedReceivedMessage = remoteUser + ": " + expectedMessage;
                    uut.HandleEvent(
                        GenerateMessage(bridgeBotNick, this.ircConfig.Channel, expectedReceivedMessage),
                        this.ircConfig,
                        ircConnection
                        );

                    Assert.AreEqual(this.ircConfig.Channel, responseReceived.Channel);
                    Assert.AreEqual(bridgeBotNick, responseReceived.RemoteUser);
                    Assert.AreEqual(expectedReceivedMessage, responseReceived.Message);

                    responseReceived = null;
                }

                // Next, use the right regex.
                {
                    uut.HandleEvent(
                        GenerateMessage(bridgeBotNick, this.ircConfig.Channel, "<" + remoteUser + "> " + expectedMessage),
                        this.ircConfig,
                        ircConnection
                        );

                    Assert.AreEqual(this.ircConfig.Channel, responseReceived.Channel);
                    Assert.AreEqual(remoteUser, responseReceived.RemoteUser);
                    Assert.AreEqual(expectedMessage, responseReceived.Message);

                    responseReceived = null;
                }
            }
        }
Esempio n. 8
0
        protected void OnResponseReceived(IrcResponse resp)
        {
            IrcResponseEventArgs e = new IrcResponseEventArgs(resp);

            if (ResponseReceived != null)
            {
                ResponseReceived.Invoke(this, e);
            }
        }
Esempio n. 9
0
 /// <summary>
 /// Ran when someone parts the channel.
 /// </summary>
 /// <param name="writer">The means to write to an IRC channel.</param>
 /// <param name="response">The command from the server.</param>
 private static void PartMessage(IIrcWriter writer, IrcResponse response)
 {
     writer.SendMessageToUser(
         "Thanks for visiting " + response.Channel + "!  Please come back soon!",
         response.RemoteUser
         );
     writer.SendCommandToChannel(
         response.RemoteUser + " has left " + response.Channel
         );
 }
Esempio n. 10
0
        /// <summary>
        /// Handles the query command.
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private async void HandleQueryCommand(IIrcWriter writer, IrcResponse response)
        {
            Match match = Regex.Match(response.Message, this.config.QueryCommand);

            if (match.Success)
            {
                string userName = match.Groups["name"].Value;
                int    karma    = await this.dataBase.QueryKarma(userName);

                writer.SendMessageToUser(userName + " has " + karma + " karma.", response.Channel);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Handles the weather command by doing a GET request to NOAA's API
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private async void HandleWeatherCommand(IIrcWriter writer, IrcResponse response)
        {
            Match match = Regex.Match(response.Message, weatherCommand);

            if (match.Success)
            {
                string zip        = match.Groups["zipCode"].Value;
                string strToWrite = await this.reporter.QueryWeather(zip);

                writer.SendCommandToChannel(strToWrite);
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Handles the decrease command.
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private async void HandleDecreaseCommand(IIrcWriter writer, IrcResponse response)
        {
            Match match = Regex.Match(response.Message, this.config.DecreaseCommandRegex);

            if (match.Success)
            {
                string userName = match.Groups["name"].Value;
                int    karma    = await this.dataBase.DecreaseKarma(userName);

                writer.SendMessageToUser(userName + " has had their karma decreased to " + karma, response.Channel);
            }
        }
Esempio n. 13
0
        public void CloneTest()
        {
            IrcResponse clone = this.uut.Clone();

            // Ensure all the properties match.
            Assert.AreEqual(this.uut.Channel, clone.Channel);
            Assert.AreEqual(this.uut.Message, clone.Message);
            Assert.AreEqual(this.uut.RemoteUser, clone.RemoteUser);

            // Ensure they are NOT the same reference.
            Assert.AreNotSame(this.uut, clone);
        }
Esempio n. 14
0
        protected void OnStandardReplyReceived(IrcResponse resp)
        {
            IrcResponseEventArgs e = new IrcResponseEventArgs(resp);

            if (StandardReplyReceived != null)
            {
                StandardReplyReceived.Invoke(this, e);
            }
            if (resp.Source.Equals("PING", StringComparison.OrdinalIgnoreCase))
            {
                Pong(resp);
            }
        }
Esempio n. 15
0
        protected void OnNumericReplyReceived(IrcResponse resp)
        {
            IrcResponseEventArgs e = new IrcResponseEventArgs(resp);

            if (NumericReplyReceived != null)
            {
                NumericReplyReceived.Invoke(this, e);
            }
            switch (resp.NumericReply)
            {
            case IrcReplyCode.RPL_MYINFO:
                _registeredLock.Set();
                break;
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Handles the end-of-names response from the server.
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private void HandleEndOfNamesResponse(IIrcWriter writer, IrcResponse response)
        {
            Tuple <string, string> userList = this.userList.CheckAndHandleEndMessage(response.Message);

            if (userList != null)
            {
                if (this.isQueried.ContainsKey(userList.Item1) && this.isQueried[userList.Item1])
                {
                    writer.SendMessageToUser(
                        string.Format("Users in {0}: {1}", userList.Item1, userList.Item2),
                        userList.Item1
                        );
                }
            }
        }
Esempio n. 17
0
        // -------- Functions --------

        /// <summary>
        /// Determines whether the specified object is equal to the current object.
        /// </summary>
        /// <param name="obj">The object to compare with the current.</param>
        /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            IrcResponse other = obj as IrcResponse;

            if (other == null)
            {
                return(false);
            }

            return(
                (this.RemoteUser == other.RemoteUser) &&
                (this.Channel == other.Channel) &&
                (this.Message == other.Message)
                );
        }
Esempio n. 18
0
        /// <summary>
        /// Handles a message from the channel.
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private async void HandleMessage(IIrcWriter writer, IrcResponse response)
        {
            string url;

            if (UrlReader.TryParseUrl(response.Message, out url))
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendFormat("@{0}: Here's a description of that URL: ", response.RemoteUser);
                builder.Append(await this.urlReader.GetDescription(url));

                writer.SendMessageToUser(
                    builder.ToString(),
                    response.Channel
                    );
            }
        }
Esempio n. 19
0
        // ---- Handlers ----

        /// <summary>
        /// Handles the up time command.
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private static void HandleUpTimeCmd(IIrcWriter writer, IrcResponse response)
        {
            TimeSpan span = DateTime.UtcNow - startTime;

            string str = string.Format(
                "I have been running for {0} Day(s), {1} Hour(s), {2} Minute(s), and {3} Second(s).",
                span.Days,
                span.Hours,
                span.Minutes,
                span.Seconds
                );

            writer.SendCommandToChannel(
                str
                );
        }
Esempio n. 20
0
        /// <summary>
        /// Handles the cowsay command.
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private void HandleCowsayCommand(IIrcWriter writer, IrcResponse response)
        {
            try
            {
                Match cowMatch = Regex.Match(response.Message, this.cowsayRegex);
                if (cowMatch.Success)
                {
                    string cowFile = this.cowSayConfig.CowFileInfoList.CommandList[cowMatch.Groups["command"].Value];
                    if (cowFile == "DEFAULT")
                    {
                        cowFile = null;
                    }

                    string cowSayedMessage;
                    int    exitCode = LaunchCowsay(cowMatch.Groups["msg"].Value, out cowSayedMessage, cowFile);

                    if ((string.IsNullOrEmpty(cowSayedMessage) == false) && (exitCode == 0))
                    {
                        writer.SendCommandToChannel(cowSayedMessage);
                    }
                    else if (exitCode != 0)
                    {
                        Console.Error.WriteLine("CowSayBot: Exit code not 0.  Got: " + exitCode);
                    }
                    else if (string.IsNullOrEmpty(cowSayedMessage))
                    {
                        Console.Error.WriteLine("CowSayBot: Nothing returned from cowsay process.");
                    }
                }
                else
                {
                    Console.Error.WriteLine("CowSayBot: Saw unknown line:" + response.Message);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("*********************");
                Console.Error.WriteLine("CowSayBot: Caught Exception:");
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine(e.StackTrace);
                Console.Error.WriteLine("**********************");
            }
        }
Esempio n. 21
0
        private void _lineHandler(string line)
        {
            // Generally, raw line received (even if it is not valid)
            OnRawLineReceived(line);

            // Parse the response
            var response = IrcResponse.FromRawLine(line);

            // Generally, response received
            OnResponseReceived(response);

            // Specifically, response received
            if (response.IsNumericReply)
            {
                OnNumericReplyReceived(response);
            }
            else if (StandardReplyReceived != null)
            {
                OnStandardReplyReceived(response);
            }
        }
Esempio n. 22
0
        public void EqualsTest()
        {
            // Ensure false returns when a null or an unrelated
            // reference goes in.
            Assert.IsFalse(this.uut.Equals(null));
            Assert.IsFalse(this.uut.Equals(1));
            Assert.IsFalse(this.uut.Equals(this));

            IrcResponse other = this.uut.Clone();

            // Ensure they are equal.
            Assert.AreEqual(this.uut, other);

            // Now, start changing properties and making sure they are not.
            other = new IrcResponse(this.uut.RemoteUser, this.uut.Channel, "A different Message");
            Assert.AreNotEqual(this.uut, other);

            other = new IrcResponse(this.uut.RemoteUser, "#newChannel", this.uut.Message);
            Assert.AreNotEqual(this.uut, other);

            other = new IrcResponse("newUser", this.uut.Channel, this.uut.Message);
            Assert.AreNotEqual(this.uut, other);
        }
Esempio n. 23
0
        /// <summary>
        /// Handles the source command, which returns the source code of the plugin.
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private void HandleSourceCommand(IIrcWriter writer, IrcResponse response)
        {
            Match match = Regex.Match(response.Message, this.sourceCommand, RegexOptions.IgnoreCase);

            if (match.Success)
            {
                string pluginName = match.Groups["pluginName"].Value.ToLower();
                if (this.plugins.ContainsKey(pluginName))
                {
                    string msg = "Source of the plugin " + pluginName + ": " + this.plugins[pluginName].SourceCodeLocation;
                    writer.SendMessageToUser(msg, response.Channel);
                }
                else if (pluginName == "chaskis")
                {
                    string msg = "My source code is located: https://github.com/xforever1313/Chaskis";
                    writer.SendMessageToUser(msg, response.Channel);
                }
                else
                {
                    writer.SendMessageToUser(pluginName + " is not a plugin I have loaded...", response.Channel);
                }
            }
        }
Esempio n. 24
0
        // -------- Default Handlers --------

        /// <summary>
        /// Handles the "list plugin" command.
        /// </summary>
        /// <param name="writer">The IRC Writer to write to.</param>
        /// <param name="response">The response from the channel.</param>
        private void HandlePluginListCommand(IIrcWriter writer, IrcResponse response)
        {
            writer.SendMessageToUser(this.pluginListResponse, response.Channel);
        }
Esempio n. 25
0
 private void Pong(IrcResponse data)
 {
     SendCommand("pong", data.Command);
 }
Esempio n. 26
0
 /// <summary>
 /// Handles the names response from the server.
 /// Adds the names to the list.
 /// </summary>
 /// <param name="writer">The IRC Writer to write to.</param>
 /// <param name="response">The response from the channel.</param>
 private void HandleNamesResponse(IIrcWriter writer, IrcResponse response)
 {
     this.userList.ParseNameResponse(response.Message);
 }
Esempio n. 27
0
 /// <summary>
 /// Handles the get users command, which queries the server for the user list.
 /// </summary>
 /// <param name="writer">The IRC Writer to write to.</param>
 /// <param name="response">The response from the channel.</param>
 private void HandleGetUsersCommand(IIrcWriter writer, IrcResponse response)
 {
     writer.SendRawCmd("NAMES " + this.ircConfig.Channel);
     this.isQueried[response.Channel] = true;
 }
Esempio n. 28
0
 protected void OnNumericReplyReceived(IrcResponse resp)
 {
     IrcResponseEventArgs e = new IrcResponseEventArgs(resp);
     if (NumericReplyReceived != null)
         NumericReplyReceived.Invoke(this, e);
     switch (resp.NumericReply)
     {
         case IrcReplyCode.RPL_MYINFO:
             _registeredLock.Set();
             break;
     }
 }
Esempio n. 29
0
        // -------- Test Helpers --------

        /// <summary>
        /// The function that is called
        /// </summary>
        /// <param name="writer">The writer that can be written to.</param>
        /// <param name="response">The response from the server.</param>
        private static void PartFunction(IIrcWriter writer, IrcResponse response)
        {
            Assert.AreSame(ircConnection, writer);
            responseReceived = response;
        }
Esempio n. 30
0
 private void Pong(IrcResponse data)
 {
     SendCommand("pong", data.Command);
 }
Esempio n. 31
0
 protected void OnResponseReceived(IrcResponse resp)
 {
     IrcResponseEventArgs e = new IrcResponseEventArgs(resp);
     if (ResponseReceived != null)
         ResponseReceived.Invoke(this, e);
 }
Esempio n. 32
0
 protected void OnStandardReplyReceived(IrcResponse resp)
 {
     IrcResponseEventArgs e = new IrcResponseEventArgs(resp);
     if (StandardReplyReceived != null)
         StandardReplyReceived.Invoke(this, e);
     if (resp.Source.Equals("PING", StringComparison.OrdinalIgnoreCase))
         Pong(resp);
 }
Esempio n. 33
0
 /// <summary>
 /// Handles the OS version command.
 /// </summary>
 /// <param name="writer">The IRC Writer to write to.</param>
 /// <param name="response">The response from the channel.</param>
 private static void HandleOsVersionCmd(IIrcWriter writer, IrcResponse response)
 {
     writer.SendCommandToChannel(
         "My system is " + Environment.OSVersion.ToString() + "."
         );
 }
Esempio n. 34
0
 /// <summary>
 /// Handles the processor count command.
 /// </summary>
 /// <param name="writer">The IRC Writer to write to.</param>
 /// <param name="response">The response from the channel.</param>
 private static void HandleProcessorCountCmd(IIrcWriter writer, IrcResponse response)
 {
     writer.SendCommandToChannel(
         "My system has " + Environment.ProcessorCount + " processors."
         );
 }