Skip to content

Commit

Permalink
Make list matches caps insensitive, move cyrillic logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Erisa committed Jun 19, 2024
1 parent d1c295b commit 57a54d2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
63 changes: 57 additions & 6 deletions Checks/ListChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,58 @@ namespace Cliptok.Checks
{
public class ListChecks
{
// Map of Cyrillic to Latin characters, to catch attempted bypasses using Cyrillic lookalikes
// <string, string> is <Cyrillic, Latin>
static Dictionary<string, string> alphabetMap = new()
{
{ "А", "A" },
{ "В", "B" },
{ "С", "C" },
{ "Е", "E" },
{ "Ԍ", "G" },
{ "Н", "H" },
{ "І", "I" },
{ "Ӏ", "I" },
{ "ӏ", "I" },
{ "Ј", "J" },
{ "К", "K" },
{ "М", "M" },
{ "О", "O" },
{ "Р", "P" },
{ "Ѕ", "S" },
{ "Т", "T" },
{ "Ѵ", "V" },
{ "Ԝ", "W" },
{ "Х", "X" },
{ "Ү", "Y" },
{ "ү", "Y" },
{ "а", "a" },
{ "Ь", "b" },
{ "с", "c" },
{ "ԁ", "d" },
{ "е", "e" },
{ "ҽ", "e" },
{ "һ", "h" },
{ "і", "i" },
{ "ј", "j" },
{ "о", "o" },
{ "р", "p" },
{ "ԛ", "q" },
{ "г", "r" },
{ "ѕ", "s" },
{ "ѵ", "v" },
{ "ѡ", "w" },
{ "х", "x" },
{ "у", "y" },
{ "У", "y" }
};

public static (bool success, string? flaggedWord) CheckForNaughtyWords(string input, WordListJson naughtyWordList)
{
// Replace any Cyrillic letters found in message with Latin characters, if in the dictionary
foreach (var letter in alphabetMap)
input = input.Replace(letter.Key, letter.Value);

string[] naughtyWords = naughtyWordList.Words;
input = input.Replace("\0", "");
if (naughtyWordList.WholeWord)
Expand All @@ -30,18 +80,19 @@ public static (bool success, string? flaggedWord) CheckForNaughtyWords(string in
bool isNaughty = false;
foreach (string naughty in naughtyWords)
{
string distinctString = new(arrayOfWords[i].Replace(naughty, "#").Distinct().ToArray());
if (distinctString.Length <= 3 && arrayOfWords[i].Contains(naughty))
string naughtyWord = naughty.ToLower();
string distinctString = new(arrayOfWords[i].Replace(naughtyWord, "#").Distinct().ToArray());
if (distinctString.Length <= 3 && arrayOfWords[i].Contains(naughtyWord))
{
if (distinctString.Length == 1)
{
isNaughty = true;
}
else if (distinctString.Length == 2 && (naughty.EndsWith(distinctString[1].ToString()) || naughty.StartsWith(distinctString[0].ToString())))
else if (distinctString.Length == 2 && (naughtyWord.EndsWith(distinctString[1].ToString()) || naughtyWord.StartsWith(distinctString[0].ToString())))
{
isNaughty = true;
}
else if (distinctString.Length == 3 && naughty.EndsWith(distinctString[1].ToString()) && naughty.StartsWith(distinctString[0].ToString()))
else if (distinctString.Length == 3 && naughtyWord.EndsWith(distinctString[1].ToString()) && naughtyWord.StartsWith(distinctString[0].ToString()))
{
isNaughty = true;
}
Expand All @@ -52,7 +103,7 @@ public static (bool success, string? flaggedWord) CheckForNaughtyWords(string in
}
if (isNaughty)
{
return (true, naughty);
return (true, naughtyWord);
}
}
}
Expand All @@ -71,7 +122,7 @@ public static (bool success, string? flaggedWord) CheckForNaughtyWords(string in
{
foreach (string word in naughtyWords)
{
if (!string.IsNullOrWhiteSpace(word) && input.Contains(word))
if (!string.IsNullOrWhiteSpace(word) && input.Contains(word.ToLower()))
{
return (true, word);
}
Expand Down
55 changes: 2 additions & 53 deletions Events/MessageEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,59 +193,8 @@ public static async Task MessageHandlerAsync(DiscordClient client, DiscordMessag
continue;
}
else
{
// Map of Cyrillic to Latin characters, to catch attempted bypasses using Cyrillic lookalikes
// <string, string> is <Cyrillic, Latin>
Dictionary<string, string> alphabetMap = new()
{
{ "А", "A" },
{ "В", "B" },
{ "С", "C" },
{ "Е", "E" },
{ "Ԍ", "G" },
{ "Н", "H" },
{ "І", "I" },
{ "Ӏ", "I" },
{ "ӏ", "I" },
{ "Ј", "J" },
{ "К", "K" },
{ "М", "M" },
{ "О", "O" },
{ "Р", "P" },
{ "Ѕ", "S" },
{ "Т", "T" },
{ "Ѵ", "V" },
{ "Ԝ", "W" },
{ "Х", "X" },
{ "Ү", "Y" },
{ "ү", "Y" },
{ "а", "a" },
{ "Ь", "b" },
{ "с", "c" },
{ "ԁ", "d" },
{ "е", "e" },
{ "ҽ", "e" },
{ "һ", "h" },
{ "і", "i" },
{ "ј", "j" },
{ "о", "o" },
{ "р", "p" },
{ "ԛ", "q" },
{ "г", "r" },
{ "ѕ", "s" },
{ "ѵ", "v" },
{ "ѡ", "w" },
{ "х", "x" },
{ "у", "y" },
{ "У", "y" }
};

// Replace any Cyrillic letters found in message with Latin characters, if in the dictionary
string msgContent = message.Content;
foreach (var letter in alphabetMap)
msgContent = msgContent.Replace(letter.Key, letter.Value);

(bool success, string flaggedWord) = Checks.ListChecks.CheckForNaughtyWords(msgContent.ToLower(), listItem);
{
(bool success, string flaggedWord) = Checks.ListChecks.CheckForNaughtyWords(message.Content.ToLower(), listItem);
if (success)
{
string reason = listItem.Reason;
Expand Down

0 comments on commit 57a54d2

Please sign in to comment.