Skip to content

Commit

Permalink
Issue: #57
Browse files Browse the repository at this point in the history
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
xforever1313 committed Nov 15, 2020
1 parent 18291c9 commit bbf4290
Show file tree
Hide file tree
Showing 15 changed files with 602 additions and 21 deletions.
1 change: 1 addition & 0 deletions Chaskis/ChaskisCore/Chaskis.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Handlers\AnyInterPluginEvent\" />
<Folder Include="Handlers\SendKick\" />
<Folder Include="Handlers\SendPart\" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public sealed class AnyChaskisEventHandler : IIrcHandler

private readonly AnyChaskisEventHandlerConfig config;

private static readonly Regex chaskisEventRegex = new Regex(
internal static readonly Regex Regex = new Regex(
@"^<chaskis_",
RegexOptions.Compiled | RegexOptions.ExplicitCapture
);
Expand All @@ -43,14 +43,6 @@ public AnyChaskisEventHandler( AnyChaskisEventHandlerConfig allConfig )

// ---------------- 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
Expand Down Expand Up @@ -85,7 +77,7 @@ public void HandleEvent( HandlerArgs args )
{
ArgumentChecker.IsNotNull( args, nameof( args ) );

if( chaskisEventRegex.IsMatch( args.Line ) == false )
if( Regex.IsMatch( args.Line ) == false )
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public AnyChaskisEventHandlerArgs( IIrcWriter writer, string line )
public IIrcWriter Writer { get; private set; }

/// <summary>
/// The raw line that was read from the server.
/// The raw line that was received.
/// </summary>
public string Line { get; private set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public AnyChaskisEventHandlerConfig()
// ---------------- Properties ----------------

/// <summary>
/// The action to take when ANY chaskis event. It is up to this action to
/// The action to take when ANY chaskis event occurs. It is up to this action to
/// actually parse the string.
/// </summary>
public AnyChaskisEventHandlerAction LineAction { get; set; }
Expand Down
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 );
}
}
}
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; }
}
}
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();
}
}
}
4 changes: 2 additions & 2 deletions Chaskis/ChaskisCore/Handlers/InterPluginEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class InterPluginEventHandler : IIrcHandler

private static string interPluginHelper = $@"^\<{InterPluginEventExtensions.XmlRootName}.+\</{InterPluginEventExtensions.XmlRootName}\>$";

private static readonly Regex chaskisRegex = new Regex(
internal static readonly Regex Regex = new Regex(
interPluginHelper,
RegexOptions.Compiled | RegexOptions.ExplicitCapture
);
Expand Down Expand Up @@ -91,7 +91,7 @@ Action<ChaskisEventHandlerLineActionArgs> lineAction
/// </summary>
public void HandleEvent( HandlerArgs args )
{
if( chaskisRegex.IsMatch( args.Line ) == false )
if( Regex.IsMatch( args.Line ) == false )
{
return;
}
Expand Down
13 changes: 6 additions & 7 deletions Chaskis/ChaskisCore/Handlers/Receive/ReceiveHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ 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 @@ -88,9 +83,13 @@ public void HandleEvent( HandlerArgs args )
{
ArgumentChecker.IsNotNull( args, nameof( args ) );

// Do not handle Chaskis Events. This handler is only
// Do not handle Chaskis Events or inter-plugin events. This handler is only
// for receiving messages via IRC.
if( chaskisEventRegex.IsMatch( args.Line ) )
if( AnyChaskisEventHandler.Regex.IsMatch( args.Line ) )
{
return;
}
else if( InterPluginEventHandler.Regex.IsMatch( args.Line ) )
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ public void Init( PluginInitor pluginInit )
this.handlers.Add( handler );
}

{
AnyInterPluginEventHandlerConfig interPluginConfig = new AnyInterPluginEventHandlerConfig
{
LineAction = PrintInterPluginEvent
};
AnyInterPluginEventHandler handler = new AnyInterPluginEventHandler( interPluginConfig );
this.handlers.Add( handler );
}

{
MessageHandlerConfig msgConfig = new MessageHandlerConfig
{
Expand Down Expand Up @@ -270,5 +279,10 @@ private void PrintChaskisEvent( AnyChaskisEventHandlerArgs args )
{
Console.WriteLine( args.Line );
}

private void PrintInterPluginEvent( AnyInterPluginEventHandlerArgs args )
{
Console.WriteLine( args.Line );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Moq;
using NUnit.Framework;
using SethCS.Exceptions;
using System.Collections.Generic;

namespace Chaskis.UnitTests.CoreTests.Handlers.AnyChaskisEvent
{
Expand Down Expand Up @@ -196,6 +197,30 @@ public void SendJoinAppears()
this.CheckResponse( eventString );
}

[Test]
public void SendInterPluginEvent()
{
var args = new Dictionary<string, string>
{
["arq1"] = "1"
};

var passArgs = new Dictionary<string, string>
{
["pass1"] = "2"
};

Core.InterPluginEvent e = new Core.InterPluginEvent(
"plugin1",
"plugin2",
args,
passArgs
);

this.uut.HandleEvent( this.ConstructArgs( e.ToXml() ) );
Assert.IsNull( this.responseReceived );
}

// -------- Test Helpers --------

/// <summary>
Expand Down
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 );
}
}
}
Loading

0 comments on commit bbf4290

Please sign in to comment.