/// <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) { } }
public void TestAddWatch() { var message = new Message {Body = "watch remove test.txt"}; handler.Receive(message); Mock<IFileWatcherService>() .Verify(call => call.RemoveWatch("test.txt")); }
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")); }
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); }
/// <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; }
public void TestAddNickname() { var message = new Message(); message.Body = "nick remove tracy"; handler.Receive(message); Mock<INicknameService>() .Verify(call => call.Remove("tracy")); }
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()); }
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")); }
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); }
/// <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; }
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)); }
/// <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; }
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")); }
/// <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; }
/// <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; }
/// <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 { } }
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); }
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)); }
/// <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)); } }
/// <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)); }
/// <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); }
public bool CanHandle(Message message) { return message.Body == respondsTo; }
public void Receive(Message message) { Fired = true; }