2021-08-27 15:03:47 +00:00
|
|
|
#nullable enable
|
2020-06-11 21:20:46 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class ProxyTagParser
|
|
|
|
{
|
2020-06-14 16:56:53 +00:00
|
|
|
public bool TryMatch(IEnumerable<ProxyMember> members, string? input, out ProxyMatch result)
|
2020-06-11 21:20:46 +00:00
|
|
|
{
|
|
|
|
result = default;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-14 16:56:53 +00:00
|
|
|
// Null input is valid and is equivalent to empty string
|
|
|
|
if (input == null) return false;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
// If the message starts with a @mention, and then proceeds to have proxy tags,
|
|
|
|
// extract the mention and place it inside the inner message
|
|
|
|
// eg. @Ske [text] => [@Ske text]
|
|
|
|
var leadingMention = ExtractLeadingMention(ref input);
|
|
|
|
|
|
|
|
// "Flatten" list of members to a list of tag-member pairs
|
|
|
|
// Then order them by "tag specificity"
|
2020-06-12 18:29:50 +00:00
|
|
|
// (prefix+suffix length desc = inner message asc = more specific proxy first)
|
2020-06-11 21:20:46 +00:00
|
|
|
var tags = members
|
|
|
|
.SelectMany(member => member.ProxyTags.Select(tag => (tag, member)))
|
|
|
|
.OrderByDescending(p => p.tag.ProxyString.Length);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
// Iterate now-ordered list of tags and try matching each one
|
|
|
|
foreach (var (tag, member) in tags)
|
|
|
|
{
|
|
|
|
result.ProxyTags = tag;
|
|
|
|
result.Member = member;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
// Skip blank tags (shouldn't ever happen in practice)
|
|
|
|
if (tag.Prefix == null && tag.Suffix == null) continue;
|
|
|
|
|
|
|
|
// Can we match with these tags?
|
2020-06-12 18:29:50 +00:00
|
|
|
if (TryMatchTagsInner(input, tag, out result.Content))
|
2020-06-11 21:20:46 +00:00
|
|
|
{
|
|
|
|
// If we extracted a leading mention before, add that back now
|
|
|
|
if (leadingMention != null) result.Content = $"{leadingMention} {result.Content}";
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
// (if not, keep going)
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
// We couldn't match anything :(
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-12 18:29:50 +00:00
|
|
|
private bool TryMatchTagsInner(string input, ProxyTag tag, out string inner)
|
|
|
|
{
|
|
|
|
inner = "";
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
// Normalize null tags to empty strings
|
|
|
|
var prefix = tag.Prefix ?? "";
|
|
|
|
var suffix = tag.Suffix ?? "";
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
// Check if our input starts/ends with the tags
|
2021-08-27 15:03:47 +00:00
|
|
|
var isMatch = input.Length >= prefix.Length + suffix.Length
|
2020-06-11 21:20:46 +00:00
|
|
|
&& input.StartsWith(prefix) && input.EndsWith(suffix);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
// 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)
|
2020-06-14 18:22:29 +00:00
|
|
|
// Note `input` is still "", even if there are spaces between
|
2020-06-11 21:20:46 +00:00
|
|
|
if (!isMatch && input.Trim() == prefix.TrimEnd() + suffix.TrimStart())
|
|
|
|
return true;
|
2021-08-27 15:03:47 +00:00
|
|
|
if (!isMatch) return false;
|
|
|
|
|
2020-06-12 18:29:50 +00:00
|
|
|
// We got a match, extract inner text
|
2020-06-24 15:19:01 +00:00
|
|
|
inner = input.Substring(prefix.Length, input.Length - prefix.Length - suffix.Length);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-12 18:29:50 +00:00
|
|
|
// (see https://github.com/xSke/PluralKit/pull/181)
|
2020-06-24 15:19:01 +00:00
|
|
|
return inner.Trim() != "\U0000fe0f";
|
2020-06-11 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private string? ExtractLeadingMention(ref string input)
|
|
|
|
{
|
|
|
|
var mentionPos = 0;
|
2020-06-28 18:26:14 +00:00
|
|
|
if (!DiscordUtils.HasMentionPrefix(input, ref mentionPos, out _)) return null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
var leadingMention = input.Substring(0, mentionPos);
|
|
|
|
input = input.Substring(mentionPos);
|
|
|
|
return leadingMention;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|