feat(bot): Case insensitive proxy tags matching (#490)

This commit is contained in:
Katrix
2022-11-23 09:48:24 +01:00
committed by GitHub
parent 09ac002d26
commit 4f0236d766
14 changed files with 89 additions and 19 deletions

View File

@@ -20,17 +20,17 @@ public class ProxyMatcher
public bool TryMatch(MessageContext ctx, AutoproxySettings settings, IReadOnlyCollection<ProxyMember> members, out ProxyMatch match,
string messageContent,
bool hasAttachments, bool allowAutoproxy)
bool hasAttachments, bool allowAutoproxy, bool caseSensitive)
{
if (TryMatchTags(members, messageContent, hasAttachments, out match)) return true;
if (TryMatchTags(members, messageContent, hasAttachments, caseSensitive, out match)) return true;
if (allowAutoproxy && TryMatchAutoproxy(ctx, settings, members, messageContent, out match)) return true;
return false;
}
private bool TryMatchTags(IReadOnlyCollection<ProxyMember> members, string messageContent, bool hasAttachments,
out ProxyMatch match)
bool caseSensitive, out ProxyMatch match)
{
if (!_parser.TryMatch(members, messageContent, out match)) return false;
if (!_parser.TryMatch(members, messageContent, caseSensitive, out match)) return false;
// Edge case: If we got a match with blank inner text, we'd normally just send w/ attachments
// However, if there are no attachments, the user probably intended something else, so we "un-match" and proceed to autoproxy

View File

@@ -78,7 +78,7 @@ public class ProxyService
members = (await _repo.GetProxyMembers(message.Author.Id, message.GuildId!.Value)).ToList();
if (!_matcher.TryMatch(ctx, autoproxySettings, members, out var match, message.Content, message.Attachments.Length > 0,
allowAutoproxy)) return false;
allowAutoproxy, ctx.CaseSensitiveProxyTags)) return false;
// this is hopefully temporary, so not putting it into a separate method
if (message.Content != null && message.Content.Length > 2000)
@@ -208,8 +208,9 @@ public class ProxyService
"Proxying was disabled in this channel by a server administrator (via the proxy blacklist).");
var autoproxySettings = await _repo.GetAutoproxySettings(ctx.SystemId.Value, msg.Guild!.Value, null);
var config = await _repo.GetSystemConfig(ctx.SystemId.Value);
var prevMatched = _matcher.TryMatch(ctx, autoproxySettings, members, out var prevMatch, originalMsg.Content,
originalMsg.Attachments.Length > 0, false);
originalMsg.Attachments.Length > 0, false, ctx.CaseSensitiveProxyTags);
var match = new ProxyMatch
{

View File

@@ -10,7 +10,7 @@ public class ProxyTagParser
private readonly Regex prefixPattern = new(@"^<(?:@!?|#|@&|a?:[\d\w_]+?:)\d+>");
private readonly Regex suffixPattern = new(@"<(?:@!?|#|@&|a?:[\d\w_]+?:)\d+>$");
public bool TryMatch(IEnumerable<ProxyMember> members, string? input, out ProxyMatch result)
public bool TryMatch(IEnumerable<ProxyMember> members, string? input, bool caseSensitive, out ProxyMatch result)
{
result = default;
@@ -42,7 +42,7 @@ public class ProxyTagParser
if (tag.Suffix == ">" && suffixPattern.IsMatch(input)) continue;
// Can we match with these tags?
if (TryMatchTagsInner(input, tag, out result.Content))
if (TryMatchTagsInner(input, tag, caseSensitive, out result.Content))
{
// If we extracted a leading mention before, add that back now
if (leadingMention != null) result.Content = $"{leadingMention} {result.Content}";
@@ -56,7 +56,7 @@ public class ProxyTagParser
return false;
}
private bool TryMatchTagsInner(string input, ProxyTag tag, out string inner)
private bool TryMatchTagsInner(string input, ProxyTag tag, bool caseSensitive, out string inner)
{
inner = "";
@@ -64,9 +64,14 @@ public class ProxyTagParser
var prefix = tag.Prefix ?? "";
var suffix = tag.Suffix ?? "";
var comparision = caseSensitive
? StringComparison.CurrentCulture
: StringComparison.CurrentCultureIgnoreCase;
// Check if our input starts/ends with the tags
var isMatch = input.Length >= prefix.Length + suffix.Length
&& input.StartsWith(prefix) && input.EndsWith(suffix);
&& input.StartsWith(prefix, comparision)
&& input.EndsWith(suffix, comparision);
// Special case: image-only proxies + proxy tags with spaces
// Trim everything, then see if we have a "contentless tag pair" (normally disallowed, but OK if we have an attachment)