private static void HandleKarmaQuery(ChaskisEventHandlerLineActionArgs args) { if (args.PluginName == "karmabot") { HandleKarmaBotResponse(args); } }
private void HandleChaskisVersionCommand(ChaskisEventHandlerLineActionArgs args) { if (args.EventArgs.ContainsKey("QUERY") && args.EventArgs["QUERY"] == "VERSION") { Dictionary <string, string> responseArgs = new Dictionary <string, string>(); if (args.EventArgs.ContainsKey("PLUGIN")) { string pluginName = args.EventArgs["PLUGIN"]; if (this.plugins.ContainsKey(pluginName)) { responseArgs["VERSION"] = this.plugins[pluginName].Plugin.Version; } else { responseArgs["ERROR"] = "Plugin Name Not Found"; } } else { responseArgs["ERROR"] = "Need Plugin Key"; } ChaskisEvent responseEvent = this.chaskisEventCreator.CreateTargetedEvent( args.PluginName, responseArgs, args.PassThroughArgs ); this.chaskisEventSender.SendChaskisEvent(responseEvent); } }
private void HandleChaskisEvent(ChaskisEventHandlerLineActionArgs args) { if (args.PluginName.Equals("CHASKIS", StringComparison.InvariantCultureIgnoreCase)) { this.HandleChaskisPlugin(args); } }
private void OnConnect(ChaskisEventHandlerLineActionArgs args) { if (args.EventArgs["event_id"] == ChaskisCoreEvents.DisconnectInProgress) { if (this.httpResponseHandler != null) { this.httpResponseHandler.IsIrcConnected = false; } } }
private static void HandleKarmaBotResponse(ChaskisEventHandlerLineActionArgs args) { if (args.EventArgs.ContainsKey("ERROR") == false) { string channel = args.PassThroughArgs["CHANNEL"]; string karma = args.EventArgs["KARMA"]; string user = args.PassThroughArgs["NAME"]; args.IrcWriter.SendMessage( "User " + user + " has " + karma + " karma", channel ); } }
private async void HandleChaskisPlugin(ChaskisEventHandlerLineActionArgs args) { if (args.EventArgs.ContainsKey("ERROR")) { return; } if (args.EventArgs.ContainsKey("VERSION")) { string channel = args.PassThroughArgs["CHANNEL"]; if (this.channelsNotified.Contains(channel)) { this.logger.WriteLine( "Channel {0} already notified of version update, skipping message", channel ); return; } string versString = args.EventArgs["VERSION"]; if (versString.Equals(this.cachedVersion) == false) { string msg = this.config.Message.Replace("{%version%}", versString); args.IrcWriter.SendMessage(msg, channel); // Event handlers all happen on one thread, // which is why we don't need a lock here. this.channelsNotified.Add(channel); if (this.channelsNotified.Count == 1) { await Task.Run( () => { // If our version is new, we need to update the cached file // so the next time the application runs, we don't // send an unneeded message. File.WriteAllText(this.cachedFilePath, versString); this.logger.WriteLine("{0}'s {1} file has been updated", PluginName, cacheFileName); } ); } } else { this.logger.WriteLine("Bot not updated, skipping message"); } } }
private async void HandleChaskisQueryCommand(ChaskisEventHandlerLineActionArgs args) { Dictionary <string, string> responseArgs = new Dictionary <string, string>(); if (args.EventArgs.ContainsKey("ACTION")) { switch (args.EventArgs["ACTION"]) { case "QUERY": if (args.EventArgs.ContainsKey("NAME") == false) { responseArgs["ERROR"] = "'NAME' argument missing"; } else { string userName = args.EventArgs["NAME"]; int karma = await this.dataBase.QueryKarma(userName); responseArgs["KARMA"] = karma.ToString(); } break; default: responseArgs["ERROR"] = "NOT IMPLEMENTED YET"; return; } ChaskisEvent e = this.eventCreator.CreateTargetedEvent( args.PluginName, responseArgs, args.PassThroughArgs ); this.eventSender.SendChaskisEvent(e); } }
public void TestSetup() { this.argsCaptured = null; this.mockWriter = new Mock <IIrcWriter>(MockBehavior.Strict); }
// ---------------- Tests --------------- /// <summary> /// Ensure that if we give bad args, exceptions get thrown. /// </summary> // [Test] // public void InvalidConstructorTest() // { // // Pattern can not be null. // Assert.Throws<ArgumentNullException>( // () => this.creator.CreateCoreEventHandler( ChaskisEventProtocol.IRC, this.LineAction ) // ); // Assert.Throws<ArgumentNullException>( // () => this.creator.CreatePluginEventHandler( this.LineAction ) // ); // Assert.Throws<ArgumentNullException>( // () => this.creator.CreatePluginEventHandler( this.sourcePluginName, this.LineAction ) // ); // // // Line action can not be null. // Assert.Throws<ArgumentNullException>( // () => this.creator.CreateCoreEventHandler( ChaskisEventProtocol.IRC, null ) // ); // Assert.Throws<ArgumentNullException>( // () => this.creator.CreatePluginEventHandler( null ) // ); // Assert.Throws<ArgumentNullException>( // () => this.creator.CreatePluginEventHandler( this.sourcePluginName, null ) // ); // } // // // Four Possible Cases: // // Chaskis Source SourceName Target Arguments // // 1. CHASKIS CORE Protocol BCAST ARGS // // 2. CHASKIS CORE PluginName TargetPlugin ARGS <- Not possible ATM (Core will ALWAYS Be a bcast). // // 3. CHASKIS PLUGIN PluginName BCAST ARGS // // 4. CHASKIS PLUGIN SourcePlugin TargetPlugin ARGS // // // -------- Case 1 -------- // // /// <summary> // /// Tests to ensure we can capture a core event. // /// Tests Case 1 from above list. // /// </summary> // [Test] // public void CaptureCoreIrcEvent() // { // ChaskisEventHandler handler = this.creator.CreateCoreEventHandler( // Regexes.ChaskisIrcDisconnectEvent, // ChaskisEventProtocol.IRC, // this.LineAction // ); // // this.DoCase1( handler, true ); // } // // /// <summary> // /// Ensures that if we are not expecting a core event, we ignore it. // /// Tests the negative case 1 from the above list. // /// </summary> // [Test] // public void IgnoresCoreIrcEvent() // { // ChaskisEventHandler handler = this.creator.CreatePluginEventHandler( // @"USERLIST\s+(?<users>.+)", // this.LineAction // ); // // this.DoCase1( handler, false ); // } // // private void DoCase1( ChaskisEventHandler handler, bool expectSuccess ) // { // string expectedArgs = string.Format( // "DISCONNECT FROM {0} AS {1}", // this.ircConfig.Server, // this.ircConfig.Nick // ); // // string disconnectEventStr = "CHASKIS CORE IRC BCAST " + expectedArgs; // // HandlerArgs handlerArgs = this.CreateHandlerArgs( disconnectEventStr ); // // handler.HandleEvent( handlerArgs ); // // if( expectSuccess ) // { // Assert.IsNotNull( this.argsCaptured ); // Assert.AreEqual( expectedArgs, this.argsCaptured.EventArgs ); // Assert.AreEqual( "IRC", this.argsCaptured.PluginName ); // } // else // { // Assert.IsNull( this.argsCaptured ); // } // } // // // -------- Case 3 -------- // // /// <summary> // /// Ensures that if we are expecting an any plugin event, we capture it. // /// Tests case 3 from list above, but expecting any plugin. // /// In this case, we expect the event to NOT fire... BCAST events need // /// to subscribe to a specific plugin. // /// </summary> // [Test] // public void CaptureAnyPluginEvent_Bcast() // { // ChaskisEventHandler handler = this.creator.CreatePluginEventHandler( // @"USERLIST\s+(?<users>.+)", // this.LineAction // ); // // this.DoCase3( handler, false ); // } // // /// <summary> // /// Ensures that if we are expecting a specfiic target plugin event, we capture it. // /// Tests case 3 from list above, but expecting a specific target plugin. // /// </summary> // [Test] // public void CaptureSpecificPluginEvent_Bcast() // { // ChaskisEventHandler handler = this.creator.CreatePluginEventHandler( // @"USERLIST\s+(?<users>.+)", // "userlistbot", // this.LineAction // ); // // this.DoCase3( handler, true ); // } // // // /// <summary> // /// Ensures that during a bcast, if it is not a plugin we care about, // /// we ignore it. // /// </summary> // [Test] // public void IgnoreSpecificPluginEvent_BCast() // { // ChaskisEventHandler handler = this.creator.CreatePluginEventHandler( // @"USERLIST\s+(?<users>.+)", // "notmyplugin", // this.LineAction // ); // // this.DoCase3( handler, false ); // } // // private void DoCase3( ChaskisEventHandler handler, bool expectSuccess ) // { // // In this case, this is UserListBot boardcasting all the users. // // We want to capture this, since we might care about it. // // string expectedArgs = "USERLIST EVERGREEN MARKEM HARRIS"; // // string eventStr = "CHASKIS PLUGIN USERLISTBOT BCAST " + expectedArgs; // // HandlerArgs handlerArgs = this.CreateHandlerArgs( eventStr ); // // handler.HandleEvent( handlerArgs ); // // if( expectSuccess ) // { // Assert.IsNotNull( this.argsCaptured ); // Assert.AreEqual( expectedArgs, this.argsCaptured.EventArgs ); // Assert.AreEqual( "USERLISTBOT", this.argsCaptured.PluginName ); // Assert.AreEqual( "EVERGREEN MARKEM HARRIS", this.argsCaptured.Match.Groups["users"].Value ); // } // else // { // Assert.IsNull( this.argsCaptured ); // } // } // // // -------- Case 4 -------- // // /// <summary> // /// Ensures that if we are expecting an any plugin event, we capture it. // /// Tests case 4 from list above, but we are expecting any plugin. // /// </summary> // [Test] // public void CaptureAnyPluginEvent_Specific() // { // // This will technically work with any Plugin, not just USERLISTBOT. // ChaskisEventHandler handler = this.creator.CreatePluginEventHandler( // @"USERLIST\s+(?<users>.+)", // this.LineAction // ); // // this.DoCase4( handler, true ); // } // // /// <summary> // /// Ensures that if we are expecting an specific plugin event, we capture it. // /// Tests case 4 from list above, but we are expecting a specifc plugin. // /// </summary> // [Test] // public void CaptureSpecificPluginEvent_Specific() // { // // This will only respond to UserListBot. // ChaskisEventHandler handler = this.creator.CreatePluginEventHandler( // @"USERLIST\s+(?<users>.+)", // "userlistbot", // this.LineAction // ); // // this.DoCase4( handler, true ); // } // // /// <summary> // /// Ensures that if we are waiting for a specific plugin, // /// and we don't see it, we don't fire. // /// </summary> // [Test] // public void IgnoreSpecificPluginEvent_Specific() // { // // This will only respond to notmyplugin, not USERLISTBOT. // ChaskisEventHandler handler = this.creator.CreatePluginEventHandler( // @"USERLIST\s+(?<users>.+)", // "notmyplugin", // this.LineAction // ); // // this.DoCase4( handler, false ); // } // // private void DoCase4( ChaskisEventHandler handler, bool expectSuccess ) // { // // For this test case, let's pretend our creator plugin asked UserListBot // // for a userlist. This is userlist responding to our creator plugin. // // string expectedArgs = "USERLIST EVERGREEN MARKEM HARRIS"; // // // SourceOfPlugin EventDestination // string eventStr = "CHASKIS PLUGIN USERLISTBOT " + this.creatorPluginName + " " + expectedArgs; // // HandlerArgs handlerArgs = this.CreateHandlerArgs( eventStr ); // // handler.HandleEvent( handlerArgs ); // // if( expectSuccess ) // { // Assert.IsNotNull( this.argsCaptured ); // Assert.AreEqual( expectedArgs, this.argsCaptured.EventArgs ); // Assert.AreEqual( "USERLISTBOT", this.argsCaptured.PluginName ); // Assert.AreEqual( "EVERGREEN MARKEM HARRIS", this.argsCaptured.Match.Groups["users"].Value ); // } // else // { // Assert.IsNull( this.argsCaptured ); // } // } // // // -------- Ignores -------- // // /// <summary> // /// Ensures if we get an unknown protocol/plugin, we don't fire. // /// </summary> // [Test] // public void IgnoreUnknownProtocol() // { // string expectedArgs = "USERLIST EVERGREEN MARKEM HARRIS"; // // string unknownProtocolString = "CHASKIS UNKNOWN USERLISTBOT BCAST " + expectedArgs; // // ChaskisEventHandler handler = this.creator.CreatePluginEventHandler( // @"USERLIST\s+(?<users>.+)", // "userlistbot", // this.LineAction // ); // // HandlerArgs handlerArgs = this.CreateHandlerArgs( unknownProtocolString ); // handler.HandleEvent( handlerArgs ); // // Assert.IsNull( this.argsCaptured ); // } // // /// <summary> // /// Ensure that if everything matches BUT our pattern, we don't fire. // /// </summary> // [Test] // public void IgnoreNoMatch() // { // string expectedArgs = "USERLIST EVERGREEN MARKEM HARRIS"; // // string messageStr = "CHASKIS PLUGIN USERLISTBOT BCAST " + expectedArgs; // // ChaskisEventHandler handler = this.creator.CreatePluginEventHandler( // @"SOMETHINGELSE\s+(?<users>.+)", // "userlistbot", // this.LineAction // ); // // HandlerArgs handlerArgs = this.CreateHandlerArgs( messageStr ); // handler.HandleEvent( handlerArgs ); // // Assert.IsNull( this.argsCaptured ); // } // // // -------- Ignore non-Chaskis events -------- // // /// <summary> // /// Ensures we ignore join. // /// </summary> // [Test] // public void IgnoreJoin() // { // ChaskisEventHandler handler = this.creator.CreateCoreEventHandler( // Regexes.ChaskisIrcDisconnectEvent, // ChaskisEventProtocol.IRC, // this.LineAction // ); // // string ircString = TestHelpers.ConstructIrcString( // "user", // JoinHandler.IrcCommand, // "#channel", // null // ); // HandlerArgs handlerArgs = this.CreateHandlerArgs( ircString ); // handler.HandleEvent( handlerArgs ); // // Assert.IsNull( this.argsCaptured ); // } // // /// <summary> // /// Ensures we ignore a part. // /// </summary> // [Test] // public void IgnorePart() // { // ChaskisEventHandler handler = this.creator.CreateCoreEventHandler( // Regexes.ChaskisIrcDisconnectEvent, // ChaskisEventProtocol.IRC, // this.LineAction // ); // // string ircString = TestHelpers.ConstructIrcString( // "user", // PartHandler.IrcCommand, // "#channel", // null // ); // HandlerArgs handlerArgs = this.CreateHandlerArgs( ircString ); // handler.HandleEvent( handlerArgs ); // // Assert.IsNull( this.argsCaptured ); // } // // /// <summary> // /// Ensures we ignore a message. // /// </summary> // [Test] // public void IgnoreMessage() // { // ChaskisEventHandler handler = this.creator.CreateCoreEventHandler( // Regexes.ChaskisIrcDisconnectEvent, // ChaskisEventProtocol.IRC, // this.LineAction // ); // // string ircString = TestHelpers.ConstructIrcString( // "user", // MessageHandler.IrcCommand, // "#channel", // "This is my message!" // ); // HandlerArgs handlerArgs = this.CreateHandlerArgs( ircString ); // handler.HandleEvent( handlerArgs ); // // Assert.IsNull( this.argsCaptured ); // } // // /// <summary> // /// Ensures we ignore a ping. // /// </summary> // [Test] // public void IgnorePing() // { // ChaskisEventHandler handler = this.creator.CreateCoreEventHandler( // Regexes.ChaskisIrcDisconnectEvent, // ChaskisEventProtocol.IRC, // this.LineAction // ); // // string ircString = TestHelpers.ConstructPingString( "irc.somewhere.com" ); // // HandlerArgs handlerArgs = this.CreateHandlerArgs( ircString ); // handler.HandleEvent( handlerArgs ); // // Assert.IsNull( this.argsCaptured ); // } // // /// <summary> // /// Ensures we ignore a pong. // /// </summary> // [Test] // public void IgnorePong() // { // ChaskisEventHandler handler = this.creator.CreateCoreEventHandler( // Regexes.ChaskisIrcDisconnectEvent, // ChaskisEventProtocol.IRC, // this.LineAction // ); // // string ircString = TestHelpers.ConstringPongString( "irc.somewhere.com", "myMessage" ); // // HandlerArgs handlerArgs = this.CreateHandlerArgs( ircString ); // handler.HandleEvent( handlerArgs ); // // Assert.IsNull( this.argsCaptured ); // } // ---------------- Test Helpers ----------------- private void LineAction(ChaskisEventHandlerLineActionArgs args) { this.argsCaptured = args; }