Beispiel #1
0
        /// <summary>
        /// Logs the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Log(Message message)
        {
            // Discard system messages
            if (string.IsNullOrEmpty(message.To))
            {
                return;
            }

            var now = DateTime.Now;
            var fileName = GetLogFileName(now);
            var path = Path.GetDirectoryName(fileName);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            var timestamp = string.Format("{0:yyyy-MM-dd HH:mm:ss} [{1}] {2}: {3}{4}", now, message.To, message.From, message.Body, Environment.NewLine);

            try
            {
                File.AppendAllText(fileName, timestamp);
            }
            catch (Exception)
            {
            }
        }
Beispiel #2
0
        public void TestAddWatch()
        {
            var message = new Message {Body = "watch remove test.txt"};

            handler.Receive(message);

            Mock<IFileWatcherService>()
                .Verify(call => call.RemoveWatch("test.txt"));
        }
Beispiel #3
0
        public void TestAddWatch()
        {
            var message = new Message { Body = "watch c:\\test.txt channel #test" };

            handler.Receive(message);

            Mock<IFileWatcherService>()
                .Verify(call => call.AddWatch("c:\\test.txt", "#test"));
        }
Beispiel #4
0
        public void TestGetHelp()
        {
            var message = new Message();
            message.Body = "help hello";

            handler.Receive(message);

            Mock<IChatService>()
                .Verify(call => call.Reply(message, "Says hello"));
        }
        public void TestMessageIsAddressedToMe()
        {
            ExpectCallToGetConfiguration();

            var message = new Message();
            message.Body = "bob hello";

            var result = service.IsAddressedToMe(message);

            Assert.IsTrue(result);
        }
Beispiel #6
0
        /// <summary>
        /// Parses the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public Message Parse(string input)
        {
            var message = new Message
            {
                Received = DateTime.Now,
                Body = input.SubstringAfterChar("+"),
                Type = MessageType.SetMode
            };

            return message;
        }
Beispiel #7
0
        public void TestAddNickname()
        {
            var message = new Message();

            message.Body = "nick remove tracy";

            handler.Receive(message);

            Mock<INicknameService>()
                .Verify(call => call.Remove("tracy"));
        }
Beispiel #8
0
        public void TestAddNickname()
        {
            var message = new Message();

            message.Body = "nick add bob";

            handler.Receive(message);

            Mock<INicknameService>()
                .Verify(call => call.Add("bob"));
        }
        public void TestMessageIsAddressedToMeIgnoresBlankMessages()
        {
            var message = new Message();
            message.Body = "";

            var result = service.IsAddressedToMe(message);

            Assert.IsFalse(result);

            Mock<IConfigService>()
                .Verify(call => call.GetConfig(), Times.Never());
        }
Beispiel #10
0
        public void TestAddChannel()
        {
            var message = new Message();
            var options = new AddChannel.Options { Channel = "test" };

            handler.Receive(message, options);

            Mock<IChannelService>()
                .Verify(call => call.Join("test"));

            Mock<IChatService>()
                .Verify(call => call.ReplyFormat(message, "Added channel: {0}", "test"));
        }
Beispiel #11
0
        public void TestHandleMessage()
        {
            var message = new Message { Body = "two", Type = MessageType.PrivateMessage };

            Mock<INicknameService>()
                .Setup(call => call.IsAddressedToMe(message))
                .Returns(true);

            service.Handle(message);

            Assert.IsFalse(((FakeHandler) service.Handlers[0]).Fired);
            Assert.IsTrue(((FakeHandler) service.Handlers[1]).Fired);
            Assert.IsFalse(((FakeHandler) service.Handlers[2]).Fired);
        }
Beispiel #12
0
        /// <summary>
        /// Parses the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public Message Parse(string input)
        {
            var message = new Message
            {
                Received = DateTime.Now,
                Body = input.SubstringAfterChar("PRIVMSG").SubstringAfterChar(":"),
                Type = MessageType.PrivateMessage,
                From = input.SubstringAfterChar(":").SubstringBeforeChar("!"),
                UserHost = input.SubstringAfterChar("!").SubstringBeforeChar(" "),
                To = input.SubstringAfterChar("PRIVMSG ").SubstringBeforeChar(" ")
            };

            return message;
        }
Beispiel #13
0
        public void TestAddWatch()
        {
            var message = new Message {Body = "watch list"};
            var fileName = Path.GetFullPath("test.txt");

            var watchers = new List<IFileWatcher> { new FileWatcher(fileName, "channel") };

            Mock<IFileWatcherService>()
                .SetupGet(call => call.FileWatchers)
                .Returns(watchers);

            handler.Receive(message);

            Mock<IChatService>()
                .Verify(call => call.Reply(message, fileName));
        }
Beispiel #14
0
        /// <summary>
        /// Determines whether the specified message is addressed to me.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>
        ///   <c>true</c> if the specified message is addressed to me; otherwise, <c>false</c>.
        /// </returns>
        public bool IsAddressedToMe(Message message)
        {
            // Ignore empty messages
            if (string.IsNullOrWhiteSpace(message.Body))
            {
                return false;
            }

            // Check each list
            foreach (var nick in List())
            {
                if (message.Body.IndexOf(nick + " ", 0, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    return true;
                }
            };

            return false;
        }
Beispiel #15
0
        public void TestListChannel()
        {
            var message = new Message();
            var options = new ListChannels.Options();

            Mock<IChannelService>()
                .Setup(call => call.List())
                .Returns(new List<string> { "one", "two", "three" });

            handler.Receive(message, options);

            Mock<IChatService>()
                .Verify(call => call.ReplyFormat(message, "one"));

            Mock<IChatService>()
                .Verify(call => call.ReplyFormat(message, "two"));

            Mock<IChatService>()
                .Verify(call => call.ReplyFormat(message, "three"));
        }
Beispiel #16
0
        /// <summary>
        /// Parses the specified input into an IRC message.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public Message Parse(string input)
        {
            // Default unknown message
            var message = new Message
            {
                Body = input,
                Type = MessageType.Unknown
            };

            // Process each parser link
            foreach (var link in Links)
            {
                if (link.CanParse(input))
                {
                    message = link.Parse(input);

                    break;
                }
            }

            return message;
        }
Beispiel #17
0
        /// <summary>
        /// Determines whether this instance can handle the specified message in the channel.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>
        ///   <c>true</c> if this instance can handle the specified channel; otherwise, <c>false</c>.
        /// </returns>
        private bool CanHandle(Message message)
        {
            var canHandle = false;

            // Check message is not one-on-one message
            if (message.Type == MessageType.PrivateMessage)
            {
                // Always accept if a DM
                if (message.To == Context.Nick)
                {
                    canHandle = true;
                }

                // Check message is addressed to this bot
                else if (NicknameService.IsAddressedToMe(message))
                {
                    canHandle = true;

                    // Strip bot name from message
                    message.Body = message.Body.SubstringAfterChar(" ");
                }
            }

            return canHandle;
        }
Beispiel #18
0
        /// <summary>
        /// Handles the Changed event of the FileWatcher control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="FileSystemEventArgs"/> instance containing the event data.</param>
        public void FileChanged(object sender, FileSystemEventArgs e)
        {
            try
            {
                var fullPath = e.FullPath;

                // Stip leading slash
                if (fullPath.StartsWith("\\"))
                {
                    fullPath = fullPath.Substring(1);
                }

                if (!File.Exists(fullPath)) return;

                var contents = File.ReadAllText(fullPath);

                var notificationChannel = FileWatchers
                    .First(w => w.FileName == fullPath)
                    .Channel;

                var message = new Message();
                message.To = notificationChannel;
                message.From = notificationChannel;
                message.Type = MessageType.Unknown;
                message.UserHost = "";

                ChatService.Reply(message, contents);

                File.Delete(fullPath);
            }
            catch
            {
            }
        }
Beispiel #19
0
        public void TestHandleMessageWhenDirectMessage()
        {
            var message = new Message { To = "nick", Body = "three", Type = MessageType.PrivateMessage };

            Context.Nick = "nick";

            service.Handle(message);

            Mock<INicknameService>()
                .Verify(call => call.IsAddressedToMe(message), Times.Never());

            Assert.IsFalse(((FakeHandler)service.Handlers[0]).Fired);
            Assert.IsFalse(((FakeHandler)service.Handlers[1]).Fired);
            Assert.IsTrue(((FakeHandler)service.Handlers[2]).Fired);
        }
Beispiel #20
0
        public void TestHandleMessageWhenLoggingEnabled()
        {
            var message = new Message { Body = "test", Type = MessageType.PrivateMessage };

            Mock<ILogService>()
                .Setup(call => call.LoggingEnabled())
                .Returns(true);

            Mock<INicknameService>()
                .Setup(call => call.IsAddressedToMe(message))
                .Returns(true);

            service.Handle(message);

            Mock<ILogService>()
                .Verify(call => call.Log(message));
        }
Beispiel #21
0
        /// <summary>
        /// Handles the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Handle(Message message)
        {
            // Logging
            if (LogService.LoggingEnabled())
            {
                LogService.Log(message);
            }

            // Check bot can handle this message
            if (!CanHandle(message))
            {
                return;
            }

            // Check for aliases
            var command = message.Body.SubstringBeforeChar(" ");
            if (AliasService.IsAlias(command))
            {
                message.Body = AliasService.FormatAlias(command, message.Body);
            }

            // Remove Alias bypass
            if (message.Body.StartsWith("!"))
            {
                message.Body = message.Body.Substring(1);
            }

            var handled = false;

            // Check each handler
            foreach (var handler in Handlers)
            {
                if (!handler.CanHandle(message)) continue;

                handler.Receive(message);

                handled = true;

                break;
            }

            if (!handled)
            {
                ChatService.Reply(message, string.Format("I didn't understand: {0}", message.Body));
            }
        }
Beispiel #22
0
 /// <summary>
 /// Replies the specified message with the given response.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="response">The response.</param>
 /// <param name="args">The args.</param>
 public void ReplyFormat(Message message, string response, params object[] args)
 {
     Reply(message, string.Format(response, args));
 }
Beispiel #23
0
        /// <summary>
        /// Replies the specified message with the given respons.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="response">The response.</param>
        public void Reply(Message message, string response)
        {
            var to = message.From;

            // Check for a group reply
            if (message.To.StartsWith("#"))
            {
                to = message.To;
            }

            // Send message
            Reply(to, response);
        }
Beispiel #24
0
 public bool CanHandle(Message message)
 {
     return message.Body == respondsTo;
 }
Beispiel #25
0
 public void Receive(Message message)
 {
     Fired = true;
 }