RegexBot/RegexBot-Modules/AutoResponder/Definition.cs

163 lines
6.4 KiB
C#
Raw Normal View History

2022-04-23 20:01:27 +00:00
using RegexBot.Common;
using System.Text.RegularExpressions;
2022-04-23 20:01:27 +00:00
namespace RegexBot.Modules.AutoResponder;
/// <summary>
/// Representation of a single <see cref="AutoResponder"/> configuration definition.
/// </summary>
class Definition {
private static readonly Random Chance = new();
public string Label { get; }
public IEnumerable<Regex> Regex { get; }
public IReadOnlyList<string> Reply { get; }
2022-04-23 20:01:27 +00:00
public string? Command { get; }
public FilterList Filter { get; }
public RateLimit<ulong> RateLimit { get; }
public double RandomChance { get; }
/// <summary>
2022-04-23 20:01:27 +00:00
/// Creates an instance based on JSON configuration.
/// </summary>
public Definition(JObject def) {
Label = def[nameof(Label)]?.Value<string>()
?? throw new ModuleLoadException($"Encountered a rule without a defined {nameof(Label)}.");
var errpostfx = $" in the rule definition for '{Label}'.";
// Regex
var opts = RegexOptions.Compiled | RegexOptions.CultureInvariant;
// TODO consider adding an option to specify Singleline and Multiline mode. Defaulting to Singleline.
// Reminder: in Singleline mode, all contents are subject to the same regex (useful if e.g. spammer separates words line by line)
opts |= RegexOptions.Singleline;
// IgnoreCase is enabled by default; must be explicitly set to false
if (def["IgnoreCase"]?.Value<bool>() ?? true) opts |= RegexOptions.IgnoreCase;
const string ErrNoRegex = $"No patterns were defined under {nameof(Regex)}";
var regexRules = new List<Regex>();
var rxconf = def[nameof(Regex)];
if (rxconf == null) throw new ModuleLoadException(ErrNoRegex + errpostfx);
if (rxconf.Type == JTokenType.Array) {
2022-04-23 20:01:27 +00:00
foreach (var input in rxconf.Values<string>()) {
try {
regexRules.Add(new Regex(input!, opts));
} catch (Exception ex) when (ex is ArgumentException or NullReferenceException) {
throw new ModuleLoadException("Unable to parse regular expression pattern" + errpostfx);
}
}
} else {
var rxstr = rxconf.Value<string>();
2022-04-23 20:01:27 +00:00
try {
regexRules.Add(new Regex(rxstr!, opts));
} catch (Exception ex) when (ex is ArgumentException or NullReferenceException) {
throw new ModuleLoadException("Unable to parse regular expression pattern" + errpostfx);
}
2022-04-23 20:01:27 +00:00
}
if (regexRules.Count == 0) {
throw new ModuleLoadException(ErrNoRegex + errpostfx);
2022-04-23 20:01:27 +00:00
}
Regex = regexRules.AsReadOnly();
// Filtering
Filter = new FilterList(def);
bool haveResponse;
// Reply options
var replyConf = def[nameof(Reply)];
if (replyConf?.Type == JTokenType.String) {
// Single string response
Reply = new List<string>() { replyConf.Value<string>()! }.AsReadOnly();
haveResponse = true;
} else if (replyConf?.Type == JTokenType.Array) {
// Have multiple responses
Reply = new List<string>(replyConf.Values<string>()!).AsReadOnly();
haveResponse= true;
} else {
// Have no response
Reply = Array.Empty<string>();
haveResponse = false;
2022-04-23 20:01:27 +00:00
}
// Command options
Command = def[nameof(Command)]?.Value<string>()!;
if (Command != null && haveResponse)
throw new ModuleLoadException($"Only one of either '{nameof(Reply)}' or '{nameof(Command)}' may be defined{errpostfx}");
if (Command != null) {
if (string.IsNullOrWhiteSpace(Command))
throw new ModuleLoadException($"'{nameof(Command)}' must have a non-blank value{errpostfx}");
haveResponse = true;
2022-04-23 20:01:27 +00:00
}
if (!haveResponse) throw new ModuleLoadException($"Neither '{nameof(Reply)}' nor '{nameof(Command)}' were defined{errpostfx}");
2022-04-23 20:01:27 +00:00
// Rate limiting
var rlconf = def[nameof(RateLimit)];
2022-04-23 20:01:27 +00:00
if (rlconf?.Type == JTokenType.Integer) {
var rlval = rlconf.Value<uint>();
RateLimit = new RateLimit<ulong>(rlval);
} else if (rlconf != null) {
throw new ModuleLoadException($"'{nameof(RateLimit)}' must be a non-negative integer{errpostfx}");
2022-04-23 20:01:27 +00:00
} else {
RateLimit = new(0);
}
2022-04-23 20:01:27 +00:00
// Random chance parameter
var randconf = def[nameof(RandomChance)];
if (randconf?.Type == JTokenType.Float) {
RandomChance = randconf.Value<float>();
if (RandomChance is > 1 or < 0) {
throw new ModuleLoadException($"Random value is invalid (not between 0 and 1){errpostfx}");
}
} else if (randconf != null) {
throw new ModuleLoadException($"{nameof(RandomChance)} is not correctly defined{errpostfx}");
} else {
// Default to none if undefined
RandomChance = double.NaN;
2022-04-23 20:01:27 +00:00
}
}
2022-04-23 20:01:27 +00:00
/// <summary>
/// Checks the given message to determine if it matches this rule's constraints.
/// This method also maintains rate limiting and performs random number generation.
/// </summary>
/// <returns>True if the rule's response(s) should be executed.</returns>
public bool Match(SocketMessage m) {
// Filter check
if (Filter.IsFiltered(m, true)) return false;
// Match check
var matchFound = false;
2022-04-23 20:01:27 +00:00
foreach (var item in Regex) {
if (item.IsMatch(m.Content)) {
matchFound = true;
break;
}
}
2022-04-23 20:01:27 +00:00
if (!matchFound) return false;
2022-04-23 20:01:27 +00:00
// Rate limit check - currently per channel
if (!RateLimit.IsPermitted(m.Channel.Id)) return false;
// Random chance check
if (!double.IsNaN(RandomChance)) {
// Fail if randomly generated value is higher than the parameter
// Example: To fail a 75% chance, the check value must be between 0.75000...001 and 1.0.
var chk = Chance.NextDouble();
if (chk > RandomChance) return false;
}
2022-04-23 20:01:27 +00:00
return true;
}
/// <summary>
/// Gets a response string to display in the channel.
/// </summary>
public string GetResponse() {
// TODO feature request: option to show responses in order instead of random
if (Reply.Count == 1) return Reply[0];
return Reply[Chance.Next(0, Reply.Count - 1)];
}
}