Skip to content

Commit

Permalink
Issue: #57
Browse files Browse the repository at this point in the history
Added SendAction event handlers.
  • Loading branch information
xforever1313 committed Nov 8, 2020
1 parent 7839fb6 commit 9208d64
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 1 deletion.
42 changes: 42 additions & 0 deletions Chaskis/ChaskisCore/Handlers/SendAction/SendActionEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// 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 into <see cref="SendActionEventHandler"/>
/// </summary>
public sealed class SendActionEventArgs : BaseSendMessageEventArgs
{
// ---------------- Fields ----------------

internal const string XmlRootName = "chaskis_sendaction_event";

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

public SendActionEventArgs() :
base()
{
}

// ---------------- Properties ----------------

protected override string XmlElementName => XmlRootName;
}

internal static class SendActionEventArgsExtensions
{
public static SendActionEventArgs FromXml( string xmlString, IIrcWriter writer )
{
SendActionEventArgs args = new SendActionEventArgs();

BaseSendMessageEventArgsExtensions.FromXml( args, xmlString, writer );

return args;
}
}
}
37 changes: 37 additions & 0 deletions Chaskis/ChaskisCore/Handlers/SendAction/SendActionEventConfig.cs
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 System.Collections.Generic;

namespace Chaskis.Core
{
/// <summary>
/// Configuration for setting up <see cref="SendActionEventHandler"/>.
/// </summary>
public sealed class SendActionEventConfig :
BaseCoreEvent<SendActionEventConfig, SendActionEventHandlerAction, SendActionEventArgs>
{
// ---------------- Constructor ----------------

public SendActionEventConfig()
{
}

// ---------------- Functions ----------------

public override SendActionEventConfig Clone()
{
return (SendActionEventConfig)this.MemberwiseClone();
}

protected override IEnumerable<string> ValidateChild()
{
// Nothing to validate.
return null;
}
}
}
42 changes: 42 additions & 0 deletions Chaskis/ChaskisCore/Handlers/SendAction/SendActionEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// 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.RegularExpressions;

namespace Chaskis.Core
{
public delegate void SendActionEventHandlerAction( SendActionEventArgs args );

/// <summary>
/// Event that is fired when the bot sends an action to
/// the IRC Server.
/// </summary>
public sealed class SendActionEventHandler : BaseCoreEventHandler<SendActionEventConfig>
{
// ---------------- Fields ----------------

private static readonly Regex regex = new Regex(
$@"^<{SendActionEventArgs.XmlRootName}>.+</{SendActionEventArgs.XmlRootName}>",
RegexOptions.ExplicitCapture | RegexOptions.Compiled | RegexOptions.IgnoreCase
);

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

public SendActionEventHandler( SendActionEventConfig config ) :
base( config, regex )
{
}

// ---------------- Functions ----------------

protected override void HandleEventInternal( HandlerArgs args, Match match )
{
SendActionEventArgs eventArgs = SendActionEventArgsExtensions.FromXml( args.Line, args.IrcWriter );
this.config.LineAction( eventArgs );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Chaskis.Core
public delegate void SendNoticeEventHandlerAction( SendNoticeEventArgs args );

/// <summary>
/// Event that is fired when the bot sends a CTCP Pong response to
/// Event that is fired when the bot sends a NOTICE to
/// the IRC Server.
/// </summary>
public sealed class SendNoticeEventHandler : BaseCoreEventHandler<SendNoticeEventConfig>
Expand Down
10 changes: 10 additions & 0 deletions Chaskis/ChaskisCore/IrcConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ public void SendMessage( string msg, string channel )
/// <param name="channel">Which channel to send the action to.</param>
public void SendAction( string msg, string channel )
{
this.OnReadLine(
new SendActionEventArgs
{
ChannelOrUser = channel,
Message = msg,
Protocol = ChaskisEventProtocol.IRC,
Server = this.Config.Server,
Writer = this
}.ToXml()
);
this.SendMessageInternal( msg, channel, "\u0001ACTION ", "\u0001", PrivateMessageHelper.IrcCommand );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//
// 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;
using Chaskis.Core;
using Moq;
using NUnit.Framework;
using SethCS.Exceptions;

namespace Chaskis.UnitTests.CoreTests.Handlers.SendAction
{
[TestFixture]
public class SendActionEventArgsTests
{
// ---------------- Fields ----------------

private const string channel = "#somechannel";
private const string message = "Some Message";
private const string server = "irc.somewhere.net";
private const ChaskisEventProtocol protocol = ChaskisEventProtocol.IRC;

private Mock<IIrcWriter> mockWriter;

// ---------------- Setup / Teardown ----------------

[SetUp]
public void TestSetup()
{
this.mockWriter = new Mock<IIrcWriter>( MockBehavior.Strict );
}

[TearDown]
public void TestTeardown()
{
this.mockWriter = null;
}

// ---------------- Tests ----------------

[Test]
public void XmlRoundTripTest()
{
SendActionEventArgs uut = new SendActionEventArgs
{
Protocol = protocol,
Server = server,
Writer = mockWriter.Object,

ChannelOrUser = channel,
Message = message
};

DoRoundTripTest( uut );
}

[Test]
public void InvalidXmlRootName()
{
string xmlString = $"<lol><server>{server}</server><protocol>{protocol}</protocol><channel>{channel}</channel><message>{message}</message></lol>";

Assert.Throws<ValidationException>(
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object )
);
}

[Test]
public void MissingServerDuringXmlParsing()
{
string xmlString = $"<chaskis_sendaction_event><protocol>{protocol}</protocol><channel>{channel}</channel><message>{message}</message></chaskis_sendaction_event>";

Assert.Throws<ValidationException>(
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object )
);
}

[Test]
public void MissingProtocolDuringXmlParsing()
{
string xmlString = $"<chaskis_sendaction_event><server>{server}</server><channel>{channel}</channel><message>{message}</message></chaskis_sendaction_event>";

Assert.Throws<ValidationException>(
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object )
);
}

[Test]
public void MissingChannelDuringXmlParsing()
{
string xmlString = $"<chaskis_sendaction_event><server>{server}</server><protocol>{protocol}</protocol><message>{message}</message></chaskis_sendaction_event>";

Assert.Throws<ValidationException>(
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object )
);
}

[Test]
public void MissingMessageDuringXmlParsing()
{
string xmlString = $"<chaskis_sendaction_event><server>{server}</server><protocol>{protocol}</protocol><channel>{channel}</channel></chaskis_sendaction_event>";

Assert.Throws<ValidationException>(
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object )
);
}

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

private void DoRoundTripTest( SendActionEventArgs uut )
{
string xmlString = uut.ToXml();
SendActionEventArgs postXml = SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object );

Console.WriteLine( xmlString );

Assert.AreEqual( uut.Server, postXml.Server );
Assert.AreEqual( uut.Protocol, postXml.Protocol );
Assert.AreEqual( uut.ChannelOrUser, postXml.ChannelOrUser );
Assert.AreEqual( uut.Message, postXml.Message );
Assert.AreSame( uut.Writer, postXml.Writer );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// 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.SendAction
{
[TestFixture]
public class SendActionEventConfigTests
{
// ---------------- Tests ----------------

[Test]
public void ValidateTest()
{
SendActionEventConfig config = new SendActionEventConfig
{
LineAction = null
};
Assert.Throws<ListedValidationException>( () => config.Validate() );

config.LineAction = delegate ( SendActionEventArgs args )
{
};

Assert.DoesNotThrow( () => config.Validate() );
}

[Test]
public void CloneTest()
{
SendActionEventConfig config = new SendActionEventConfig();
SendActionEventConfig clone = config.Clone();

Assert.AreNotSame( config, clone );
}
}
}
Loading

0 comments on commit 9208d64

Please sign in to comment.