Skip to content

Commit

Permalink
Issue: #34
Browse files Browse the repository at this point in the history
Added MessageHandlerConfig, did not plug in to anything yet.
  • Loading branch information
xforever1313 committed Sep 2, 2018
1 parent 329175f commit 5729a11
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 5 deletions.
8 changes: 5 additions & 3 deletions Chaskis/ChaskisCore/Handlers/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Chaskis.Core
{
public delegate void MessageHandlerAction( IIrcWriter writer, IrcResponse response );

/// <summary>
/// Configuration for responding to a message received from IRC.
/// </summary>
Expand All @@ -23,7 +25,7 @@ public class MessageHandler : IIrcHandler
/// <summary>
/// The irc command that will appear from the server.
/// </summary>
public const string IrcCommand = "PRIVMSG";
public static readonly string IrcCommand = "PRIVMSG";

/// <summary>
/// Last time this event was triggered in the channel.
Expand Down Expand Up @@ -58,7 +60,7 @@ public class MessageHandler : IIrcHandler
/// <param name="respondToSelf">Whether or not the bot should respond to lines sent out by itself. Defaulted to false.</param>
public MessageHandler(
string lineRegex,
Action<IIrcWriter, IrcResponse> lineAction,
MessageHandlerAction lineAction,
int coolDown = 0,
ResponseOptions responseOption = ResponseOptions.ChannelAndPms,
bool respondToSelf = false
Expand Down Expand Up @@ -90,7 +92,7 @@ public MessageHandler(
/// <summary>
/// The action that gets triggered when the line regex matches.
/// </summary>
public Action<IIrcWriter, IrcResponse> LineAction { get; private set; }
public MessageHandlerAction LineAction { get; private set; }

/// <summary>
/// How long to wait in seconds between firing events. 0 for no cool down.
Expand Down
105 changes: 105 additions & 0 deletions Chaskis/ChaskisCore/Handlers/MessageHandlerConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// 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 System.Text.RegularExpressions;
using SethCS.Exceptions;

namespace Chaskis.Core
{
public class MessageHandlerConfig
{
// ---------------- Constructor ----------------

public MessageHandlerConfig()
{
this.RegexOptions = RegexOptions.None;
this.CoolDown = 0;
this.ResponseOption = ResponseOptions.ChannelAndPms;
this.RespondToSelf = false;
}

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

/// <summary>
/// The regex to search for in order to fire the action.
/// For example, if you want !bot help to trigger the action, pass in "!bot\s+help"
///
/// This DOES get Liquified via <see cref="Parsing.LiquefyStringWithIrcConfig(string, string, string, string)'"/>
///
/// Required.
/// </summary>
public string LineRegex { get; set; }

/// <summary>
/// What regex options to use with <see cref="LineRegex"/>.
/// Defaulted to <see cref="RegexOptions.None"/>
/// </summary>
public RegexOptions RegexOptions { get; set; }

/// <summary>
/// The action that gets triggered when the line regex matches.
///
/// Required.
/// </summary>
public MessageHandlerAction LineAction { get; set; }

/// <summary>
/// How long to wait in seconds between firing events. 0 for no cool down.
/// This cool down is on a per-channel basis if the bot is in multiple channels.
///
/// Defaulted to 0.
/// </summary>
public int CoolDown { get; set; }

/// <summary>
/// Whether or not this bot will respond to private messages or not.
///
/// Defaulted to <see cref="ResponseOptions.ChannelAndPms"/>
/// </summary>
public ResponseOptions ResponseOption { get; set; }

/// <summary>
/// Whether or not the action will be triggered if the person
/// who sent the message was this bot.
///
/// Defaulted to false.
/// </summary>
public bool RespondToSelf { get; set; }

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

public void Validate()
{
bool success = true;
StringBuilder errorString = new StringBuilder();
errorString.AppendLine( "Errors when validating " + nameof( MessageHandlerConfig ) );

if( string.IsNullOrEmpty( this.LineRegex ) )
{
success = false;
errorString.AppendLine( "\t- " + nameof( this.LineRegex ) + " can not be null or empty." );
}

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 MessageHandlerConfig Clone()
{
return (MessageHandlerConfig)this.MemberwiseClone();
}
}
}
3 changes: 1 addition & 2 deletions Chaskis/Plugins/XmlBot/XmlBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// http://www.boost.org/LICENSE_1_0.txt)
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -133,7 +132,7 @@ public void Dispose()
/// </summary>
/// <param name="command">The command our bot is listening for.</param>
/// <param name="response">The response our bot will generate.</param>
public static Action<IIrcWriter, IrcResponse> GetMessageHandler( string response, IIrcConfig ircConfig )
public static MessageHandlerAction GetMessageHandler( string response, IIrcConfig ircConfig )
{
ArgumentChecker.StringIsNotNullOrEmpty( response, nameof( response ) );

Expand Down
76 changes: 76 additions & 0 deletions Chaskis/UnitTests/CoreTests/MessageHandlerConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// 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.RegularExpressions;
using Chaskis.Core;
using NUnit.Framework;
using SethCS.Exceptions;

namespace Chaskis.UnitTests.CoreTests
{
[TestFixture]
public class MessageHandlerConfigTests
{
/// <summary>
/// Ensures the default values when constructing are correct.
/// </summary>
[Test]
public void DefaultValueTest()
{
MessageHandlerConfig uut = new MessageHandlerConfig();

Assert.AreEqual( 0, uut.CoolDown );
Assert.AreEqual( RegexOptions.None, uut.RegexOptions );
Assert.AreEqual( ResponseOptions.ChannelAndPms, uut.ResponseOption );
Assert.IsFalse( uut.RespondToSelf );
}

/// <summary>
/// Ensures the Validate() function works as expected.
/// </summary>
[Test]
public void ValidateTest()
{
MessageHandlerConfig config = new MessageHandlerConfig();

// No action should not validate.
config.LineAction = null;
config.LineRegex = @"!bot\s+help";
Assert.Throws<ValidationException>( () => config.Validate() );

// Empty regex should not validate.
config.LineAction = delegate ( IIrcWriter writer, IrcResponse response )
{
};
config.LineRegex = string.Empty;
Assert.Throws<ValidationException>( () => config.Validate() );

// Null regex should not validate.
config.LineAction = delegate ( IIrcWriter writer, IrcResponse response )
{
};
config.LineRegex = null;
Assert.Throws<ValidationException>( () => config.Validate() );

// This should validate.
config.LineAction = delegate ( IIrcWriter writer, IrcResponse response )
{
};
config.LineRegex = @"!bot\s+help";
Assert.DoesNotThrow( () => config.Validate() );
}

[Test]
public void CloneTest()
{
MessageHandlerConfig config1 = new MessageHandlerConfig();
MessageHandlerConfig clone = config1.Clone();

Assert.AreNotSame( config1, clone );
}
}
}

0 comments on commit 5729a11

Please sign in to comment.