-
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 MessageHandlerConfig, did not plug in to anything yet.
- Loading branch information
1 parent
329175f
commit 5729a11
Showing
4 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
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
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(); | ||
} | ||
} | ||
} |
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
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 ); | ||
} | ||
} | ||
} |