Skip to content

Commit

Permalink
Issue: #57
Browse files Browse the repository at this point in the history
Added an AnyChaskisEvent handler which, well, handles any ChaskisEvent.

This is so ChaskisEvents don't get mixed up in the ReceiveHandler.
  • Loading branch information
xforever1313 committed Nov 14, 2020
1 parent 01b1c35 commit c957195
Show file tree
Hide file tree
Showing 9 changed files with 559 additions and 1 deletion.
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 );
}
}
}
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; }
}
}
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();
}
}
}
13 changes: 13 additions & 0 deletions Chaskis/ChaskisCore/Handlers/Receive/ReceiveHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// http://www.boost.org/LICENSE_1_0.txt)
//

using System.Text.RegularExpressions;
using SethCS.Exceptions;

namespace Chaskis.Core
Expand All @@ -26,6 +27,11 @@ public sealed class ReceiveHandler : IIrcHandler

private readonly ReceiveHandlerConfig config;

private static readonly Regex chaskisEventRegex = new Regex(
@"^<chaskis_",
RegexOptions.Compiled | RegexOptions.ExplicitCapture
);

// ---------------- Constructor ----------------

public ReceiveHandler( ReceiveHandlerConfig allConfig )
Expand Down Expand Up @@ -82,6 +88,13 @@ public void HandleEvent( HandlerArgs args )
{
ArgumentChecker.IsNotNull( args, nameof( args ) );

// Do not handle Chaskis Events. This handler is only
// for receiving messages via IRC.
if( chaskisEventRegex.IsMatch( args.Line ) )
{
return;
}

ReceiveHandlerArgs allArgs = new ReceiveHandlerArgs( args.IrcWriter, args.Line );

this.LineAction( allArgs );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public void Init( PluginInitor pluginInit )
{
this.log = pluginInit.Log;

{
AnyChaskisEventHandlerConfig chaskisEventConfig = new AnyChaskisEventHandlerConfig
{
LineAction = PrintChaskisEvent
};

AnyChaskisEventHandler handler = new AnyChaskisEventHandler( chaskisEventConfig );
this.handlers.Add( handler );
}

{
MessageHandlerConfig msgConfig = new MessageHandlerConfig
{
Expand Down Expand Up @@ -252,5 +262,13 @@ private void HandleCanary( MessageHandlerArgs args )
args.Channel
);
}

/// <summary>
/// So chaskis events show up in the logs.
/// </summary>
private void PrintChaskisEvent( AnyChaskisEventHandlerArgs args )
{
Console.WriteLine( args.Line );
}
}
}
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 );
}
}
}
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 );
}
}
}
Loading

0 comments on commit c957195

Please sign in to comment.