2022-04-23 20:01:27 +00:00
|
|
|
|
using RegexBot.Common;
|
2018-09-22 04:21:43 +00:00
|
|
|
|
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; }
|
2022-06-14 05:38:58 +00:00
|
|
|
|
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; }
|
|
|
|
|
|
2018-09-22 04:21:43 +00:00
|
|
|
|
/// <summary>
|
2022-04-23 20:01:27 +00:00
|
|
|
|
/// Creates an instance based on JSON configuration.
|
2018-09-22 04:21:43 +00:00
|
|
|
|
/// </summary>
|
2022-06-14 05:38:58 +00:00
|
|
|
|
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>();
|
2022-07-06 04:01:30 +00:00
|
|
|
|
List<string> inputs;
|
|
|
|
|
try {
|
2022-07-09 20:22:39 +00:00
|
|
|
|
inputs = Utilities.LoadStringOrStringArray(def[nameof(Regex)]);
|
2022-07-06 04:01:30 +00:00
|
|
|
|
} catch (ArgumentNullException) {
|
|
|
|
|
throw new ModuleLoadException(ErrNoRegex + errpostfx);
|
|
|
|
|
}
|
|
|
|
|
foreach (var inputRule in inputs) {
|
2022-04-23 20:01:27 +00:00
|
|
|
|
try {
|
2022-07-06 04:01:30 +00:00
|
|
|
|
regexRules.Add(new Regex(inputRule, opts));
|
2022-06-14 05:38:58 +00:00
|
|
|
|
} catch (Exception ex) when (ex is ArgumentException or NullReferenceException) {
|
|
|
|
|
throw new ModuleLoadException("Unable to parse regular expression pattern" + errpostfx);
|
2018-09-22 04:21:43 +00:00
|
|
|
|
}
|
2022-04-23 20:01:27 +00:00
|
|
|
|
}
|
2022-07-06 04:01:30 +00:00
|
|
|
|
if (regexRules.Count == 0) throw new ModuleLoadException(ErrNoRegex + errpostfx);
|
2022-06-14 05:38:58 +00:00
|
|
|
|
Regex = regexRules.AsReadOnly();
|
|
|
|
|
|
|
|
|
|
// Filtering
|
|
|
|
|
Filter = new FilterList(def);
|
|
|
|
|
|
|
|
|
|
bool haveResponse;
|
|
|
|
|
|
|
|
|
|
// Reply options
|
|
|
|
|
var replyConf = def[nameof(Reply)];
|
2022-07-06 04:01:30 +00:00
|
|
|
|
try {
|
2022-07-09 20:22:39 +00:00
|
|
|
|
Reply = Utilities.LoadStringOrStringArray(replyConf);
|
2022-07-06 04:01:30 +00:00
|
|
|
|
haveResponse = Reply.Count > 0;
|
|
|
|
|
} catch (ArgumentNullException) {
|
2022-06-14 05:38:58 +00:00
|
|
|
|
Reply = Array.Empty<string>();
|
|
|
|
|
haveResponse = false;
|
2022-07-06 04:01:30 +00:00
|
|
|
|
} catch (ArgumentException) {
|
|
|
|
|
throw new ModuleLoadException($"Encountered a problem within 'Reply'{errpostfx}");
|
2022-04-23 20:01:27 +00:00
|
|
|
|
}
|
2022-06-14 05:38:58 +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
|
|
|
|
}
|
2018-09-22 04:21:43 +00:00
|
|
|
|
|
2022-06-14 05:38:58 +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
|
2022-06-14 05:38:58 +00:00
|
|
|
|
var rlconf = def[nameof(RateLimit)];
|
2022-04-23 20:01:27 +00:00
|
|
|
|
if (rlconf?.Type == JTokenType.Integer) {
|
2022-07-21 03:34:29 +00:00
|
|
|
|
var rlval = rlconf.Value<int>();
|
2022-04-23 20:01:27 +00:00
|
|
|
|
RateLimit = new RateLimit<ulong>(rlval);
|
|
|
|
|
} else if (rlconf != null) {
|
2022-06-14 05:38:58 +00:00
|
|
|
|
throw new ModuleLoadException($"'{nameof(RateLimit)}' must be a non-negative integer{errpostfx}");
|
2022-04-23 20:01:27 +00:00
|
|
|
|
} else {
|
|
|
|
|
RateLimit = new(0);
|
2018-09-22 04:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-23 20:01:27 +00:00
|
|
|
|
// Random chance parameter
|
2022-06-14 05:38:58 +00:00
|
|
|
|
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}");
|
2018-09-22 04:21:43 +00:00
|
|
|
|
}
|
2022-06-14 05:38:58 +00:00
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-22 04:21:43 +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
|
2022-06-14 05:38:58 +00:00
|
|
|
|
var matchFound = false;
|
2022-04-23 20:01:27 +00:00
|
|
|
|
foreach (var item in Regex) {
|
|
|
|
|
if (item.IsMatch(m.Content)) {
|
|
|
|
|
matchFound = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-09-22 04:21:43 +00:00
|
|
|
|
}
|
2022-04-23 20:01:27 +00:00
|
|
|
|
if (!matchFound) return false;
|
2018-09-22 04:21:43 +00:00
|
|
|
|
|
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;
|
2018-09-22 04:21:43 +00:00
|
|
|
|
}
|
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
|
2022-06-14 05:38:58 +00:00
|
|
|
|
if (Reply.Count == 1) return Reply[0];
|
|
|
|
|
return Reply[Chance.Next(0, Reply.Count - 1)];
|
2018-09-22 04:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|