-
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 an AnyChaskisEvent handler which, well, handles any ChaskisEvent. This is so ChaskisEvents don't get mixed up in the ReceiveHandler.
- Loading branch information
1 parent
01b1c35
commit c957195
Showing
9 changed files
with
559 additions
and
1 deletion.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
Chaskis/ChaskisCore/Handlers/AnyChaskisEvent/AnyChaskisEventHandler.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,97 @@ | ||
// | ||
// 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 AnyChaskisEventHandlerAction( AnyChaskisEventHandlerArgs args ); | ||
|
||
/// <summary> | ||
/// This class will fire for ALL chaskis events that are triggered. | ||
/// | ||
/// Note, this should really only be used when you want to get ALL output | ||
/// from the chaskis event without any filtering. Really only meant to be used for debugging. | ||
/// </summary> | ||
public sealed class AnyChaskisEventHandler : IIrcHandler | ||
{ | ||
// ---------------- Fields ---------------- | ||
|
||
private readonly AnyChaskisEventHandlerConfig config; | ||
|
||
private static readonly Regex chaskisEventRegex = new Regex( | ||
@"^<chaskis_", | ||
RegexOptions.Compiled | RegexOptions.ExplicitCapture | ||
); | ||
|
||
// ---------------- Constructor ---------------- | ||
|
||
public AnyChaskisEventHandler( AnyChaskisEventHandlerConfig allConfig ) | ||
{ | ||
ArgumentChecker.IsNotNull( allConfig, nameof( allConfig ) ); | ||
|
||
allConfig.Validate(); | ||
|
||
this.config = allConfig.Clone(); | ||
this.KeepHandling = true; | ||
} | ||
|
||
// ---------------- Properties ---------------- | ||
|
||
/// <summary> | ||
/// The action to take when ANY message appears from IRC (JOIN, PART, PRIVMSG, PING, etc). | ||
/// As far as the passed in IrcResponse to the action goes, the channel and remote user | ||
/// will be String.Empty, since this class does no parsing of the IRC message. | ||
/// It just grabs the line from the IRC channel and passes it into the AllAction | ||
/// with no parsing. It is up to the AllAction to parse the channel and user | ||
/// name if they so desire. | ||
/// </summary> | ||
public AnyChaskisEventHandlerAction 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; | ||
} | ||
|
||
AnyChaskisEventHandlerArgs allArgs = new AnyChaskisEventHandlerArgs( args.IrcWriter, args.Line ); | ||
this.LineAction( allArgs ); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Chaskis/ChaskisCore/Handlers/AnyChaskisEvent/AnyChaskisEventHandlerArgs.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="AnyChaskisEventHandler"/> is triggered. | ||
/// </summary> | ||
public class AnyChaskisEventHandlerArgs | ||
{ | ||
// ---------------- Constructor ---------------- | ||
|
||
public AnyChaskisEventHandlerArgs( IIrcWriter writer, string line ) | ||
{ | ||
this.Writer = writer; | ||
this.Line = line; | ||
} | ||
|
||
// ---------------- Properties ---------------- | ||
|
||
/// <summary> | ||
/// The Writer to use so we can respond to the chaskis event. | ||
/// </summary> | ||
public IIrcWriter Writer { get; private set; } | ||
|
||
/// <summary> | ||
/// The raw line that was read from the server. | ||
/// </summary> | ||
public string Line { get; private set; } | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Chaskis/ChaskisCore/Handlers/AnyChaskisEvent/AnyChaskisEventHandlerConfig.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 2018. | ||
// 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="AnyChaskisEventHandler"/> | ||
/// </summary> | ||
public class AnyChaskisEventHandlerConfig | ||
{ | ||
// ---------------- Constructor ---------------- | ||
|
||
public AnyChaskisEventHandlerConfig() | ||
{ | ||
} | ||
|
||
// ---------------- Properties ---------------- | ||
|
||
/// <summary> | ||
/// The action to take when ANY chaskis event. It is up to this action to | ||
/// actually parse the string. | ||
/// </summary> | ||
public AnyChaskisEventHandlerAction LineAction { get; set; } | ||
|
||
// ---------------- Functions ---------------- | ||
|
||
public void Validate() | ||
{ | ||
bool success = true; | ||
StringBuilder errorString = new StringBuilder(); | ||
errorString.AppendLine( "Errors when validating " + nameof( AnyChaskisEventHandlerConfig ) ); | ||
|
||
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 AnyChaskisEventHandlerConfig Clone() | ||
{ | ||
return (AnyChaskisEventHandlerConfig)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
37 changes: 37 additions & 0 deletions
37
Chaskis/UnitTests/CoreTests/Handlers/AnyChaskisEvent/AnyChaskisEventHandlerArgsTests.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.AnyChaskisEvent | ||
{ | ||
[TestFixture] | ||
public class AnyChaskisEventHandlerArgsTests | ||
{ | ||
// ---------------- 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"; | ||
|
||
AnyChaskisEventHandlerArgs uut = new AnyChaskisEventHandlerArgs( | ||
writer.Object, | ||
line | ||
); | ||
|
||
Assert.AreSame( writer.Object, uut.Writer ); | ||
Assert.AreEqual( line, uut.Line ); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Chaskis/UnitTests/CoreTests/Handlers/AnyChaskisEvent/AnyChaskisEventHandlerConfigTests.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,46 @@ | ||
// | ||
// 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 NUnit.Framework; | ||
using SethCS.Exceptions; | ||
|
||
namespace Chaskis.UnitTests.CoreTests.Handlers.AnyChaskisEvent | ||
{ | ||
[TestFixture] | ||
public class AnyChaskisEventHandlerConfigTests | ||
{ | ||
// ---------------- Tests ---------------- | ||
|
||
/// <summary> | ||
/// Ensures the Validate() function works as expected. | ||
/// </summary> | ||
[Test] | ||
public void ValidateTest() | ||
{ | ||
AnyChaskisEventHandlerConfig config = new AnyChaskisEventHandlerConfig(); | ||
|
||
config.LineAction = null; | ||
Assert.Throws<ValidationException>( () => config.Validate() ); | ||
|
||
config.LineAction = delegate ( AnyChaskisEventHandlerArgs args ) | ||
{ | ||
}; | ||
|
||
Assert.DoesNotThrow( () => config.Validate() ); | ||
} | ||
|
||
[Test] | ||
public void CloneTest() | ||
{ | ||
AnyChaskisEventHandlerConfig config1 = new AnyChaskisEventHandlerConfig(); | ||
AnyChaskisEventHandlerConfig clone = config1.Clone(); | ||
|
||
Assert.AreNotSame( config1, clone ); | ||
} | ||
} | ||
} |
Oops, something went wrong.