-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added another any event that captures any inter-plugin event. Added it to the RegressionTestPlugin so these events show up in the logs.
- Loading branch information
1 parent
18291c9
commit bbf4290
Showing
15 changed files
with
602 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
Chaskis/ChaskisCore/Handlers/AnyInterPluginEvent/AnyInterPluginEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// Copyright Seth Hendrick 2016-2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using System.Text.RegularExpressions; | ||
using SethCS.Exceptions; | ||
|
||
namespace Chaskis.Core | ||
{ | ||
public delegate void AnyInterPluginEventHandlerAction( AnyInterPluginEventHandlerArgs args ); | ||
|
||
/// <summary> | ||
/// This class will fire for ALL inter-plugin events that are triggered. | ||
/// | ||
/// Note, this should really only be used when you want to get ALL output | ||
/// from the inter-plugin event without any filtering. Really only meant to be used for debugging. | ||
/// </summary> | ||
public sealed class AnyInterPluginEventHandler : IIrcHandler | ||
{ | ||
// ---------------- Fields ---------------- | ||
|
||
private readonly AnyInterPluginEventHandlerConfig config; | ||
|
||
private static readonly Regex chaskisEventRegex = new Regex( | ||
$@"^\<{InterPluginEventExtensions.XmlRootName}.+", | ||
RegexOptions.Compiled | RegexOptions.ExplicitCapture | ||
); | ||
|
||
// ---------------- Constructor ---------------- | ||
|
||
public AnyInterPluginEventHandler( AnyInterPluginEventHandlerConfig allConfig ) | ||
{ | ||
ArgumentChecker.IsNotNull( allConfig, nameof( allConfig ) ); | ||
|
||
allConfig.Validate(); | ||
|
||
this.config = allConfig.Clone(); | ||
this.KeepHandling = true; | ||
} | ||
|
||
// ---------------- Properties ---------------- | ||
|
||
public AnyInterPluginEventHandlerAction LineAction | ||
{ | ||
get | ||
{ | ||
return this.config.LineAction; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Whether or not the handler should keep handling or not. | ||
/// Set to true to keep handling the event when it appears in the chat. | ||
/// Set to false so when the current IRC message is finished processing being, | ||
/// it leaves the event queue and never | ||
/// happens again. Useful for events that only need to happen once. | ||
/// | ||
/// This is a public get/set. Either classes outside of the handler can | ||
/// tell the handler to cancel the event, or it can cancel itself. | ||
/// | ||
/// Note: when this is set to false, there must be one more IRC message that appears | ||
/// before it is removed from the queue. | ||
/// | ||
/// Defaulted to true. | ||
/// </summary> | ||
public bool KeepHandling { get; set; } | ||
|
||
// ---------------- Functions ---------------- | ||
|
||
/// <summary> | ||
/// Handles the event. | ||
/// </summary> | ||
public void HandleEvent( HandlerArgs args ) | ||
{ | ||
ArgumentChecker.IsNotNull( args, nameof( args ) ); | ||
|
||
if( chaskisEventRegex.IsMatch( args.Line ) == false ) | ||
{ | ||
return; | ||
} | ||
|
||
AnyInterPluginEventHandlerArgs allArgs = new AnyInterPluginEventHandlerArgs( args.IrcWriter, args.Line ); | ||
this.LineAction( allArgs ); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Chaskis/ChaskisCore/Handlers/AnyInterPluginEvent/AnyInterPluginEventHandlerArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
namespace Chaskis.Core | ||
{ | ||
/// <summary> | ||
/// Arguments that are passed in when <see cref="AnyInterPluginEventHandler"/> is triggered. | ||
/// </summary> | ||
public class AnyInterPluginEventHandlerArgs | ||
{ | ||
// ---------------- Constructor ---------------- | ||
|
||
public AnyInterPluginEventHandlerArgs( IIrcWriter writer, string line ) | ||
{ | ||
this.Writer = writer; | ||
this.Line = line; | ||
} | ||
|
||
// ---------------- Properties ---------------- | ||
|
||
/// <summary> | ||
/// The Writer to use so we can respond to the event. | ||
/// </summary> | ||
public IIrcWriter Writer { get; private set; } | ||
|
||
/// <summary> | ||
/// The raw line that was received. | ||
/// </summary> | ||
public string Line { get; private set; } | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Chaskis/ChaskisCore/Handlers/AnyInterPluginEvent/AnyInterPluginEventHandlerConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using System.Text; | ||
using SethCS.Exceptions; | ||
|
||
namespace Chaskis.Core | ||
{ | ||
/// <summary> | ||
/// Configuration used for <see cref="AnyInterPluginEventHandler"/> | ||
/// </summary> | ||
public class AnyInterPluginEventHandlerConfig | ||
{ | ||
// ---------------- Constructor ---------------- | ||
|
||
public AnyInterPluginEventHandlerConfig() | ||
{ | ||
} | ||
|
||
// ---------------- Properties ---------------- | ||
|
||
/// <summary> | ||
/// The action to take for ANY inter-plugin event. It is up to this action to | ||
/// actually parse the string. | ||
/// </summary> | ||
public AnyInterPluginEventHandlerAction LineAction { get; set; } | ||
|
||
// ---------------- Functions ---------------- | ||
|
||
public void Validate() | ||
{ | ||
bool success = true; | ||
StringBuilder errorString = new StringBuilder(); | ||
errorString.AppendLine( "Errors when validating " + nameof( AnyInterPluginEventHandlerConfig ) ); | ||
|
||
if( this.LineAction == null ) | ||
{ | ||
success = false; | ||
errorString.AppendLine( "\t- " + nameof( this.LineAction ) + " can not be null" ); | ||
} | ||
|
||
if( success == false ) | ||
{ | ||
throw new ValidationException( errorString.ToString() ); | ||
} | ||
} | ||
|
||
public AnyInterPluginEventHandlerConfig Clone() | ||
{ | ||
return (AnyInterPluginEventHandlerConfig)this.MemberwiseClone(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...s/UnitTests/CoreTests/Handlers/AnyInterPluginEvent/AnyInterPluginEventHandlerArgsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using Chaskis.Core; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
namespace Chaskis.UnitTests.CoreTests.Handlers.AnyInterPluginEvent | ||
{ | ||
[TestFixture] | ||
public class AnyInterPluginEventHandlerArgsTests | ||
{ | ||
// ---------------- Tests ---------------- | ||
|
||
/// <summary> | ||
/// Ensures the properties get set correctly during construction. | ||
/// </summary> | ||
[Test] | ||
public void ConstructorTest() | ||
{ | ||
Mock<IIrcWriter> writer = new Mock<IIrcWriter>( MockBehavior.Strict ); | ||
const string line = "line"; | ||
|
||
AnyInterPluginEventHandlerArgs uut = new AnyInterPluginEventHandlerArgs( | ||
writer.Object, | ||
line | ||
); | ||
|
||
Assert.AreSame( writer.Object, uut.Writer ); | ||
Assert.AreEqual( line, uut.Line ); | ||
} | ||
} | ||
} |
Oops, something went wrong.