chatfilter: add config option for stripping accents
This commit is contained in:
@@ -164,4 +164,15 @@ public interface ChatFilterConfig extends Config
|
|||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ConfigItem(
|
||||||
|
keyName = "stripAccents",
|
||||||
|
name = "Strip accents",
|
||||||
|
description = "Remove accents before applying filters",
|
||||||
|
position = 13
|
||||||
|
)
|
||||||
|
default boolean stripAccents()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -321,7 +321,7 @@ public class ChatFilterPlugin extends Plugin
|
|||||||
{
|
{
|
||||||
String strippedMessage = jagexPrintableCharMatcher.retainFrom(message)
|
String strippedMessage = jagexPrintableCharMatcher.retainFrom(message)
|
||||||
.replace('\u00A0', ' ');
|
.replace('\u00A0', ' ');
|
||||||
String strippedAccents = StringUtils.stripAccents(strippedMessage);
|
String strippedAccents = stripAccents(strippedMessage);
|
||||||
assert strippedMessage.length() == strippedAccents.length();
|
assert strippedMessage.length() == strippedAccents.length();
|
||||||
|
|
||||||
if (username != null && shouldFilterByName(username))
|
if (username != null && shouldFilterByName(username))
|
||||||
@@ -377,23 +377,28 @@ public class ChatFilterPlugin extends Plugin
|
|||||||
filteredNamePatterns.clear();
|
filteredNamePatterns.clear();
|
||||||
|
|
||||||
Text.fromCSV(config.filteredWords()).stream()
|
Text.fromCSV(config.filteredWords()).stream()
|
||||||
.map(StringUtils::stripAccents)
|
.map(this::stripAccents)
|
||||||
.map(s -> Pattern.compile(Pattern.quote(s), Pattern.CASE_INSENSITIVE))
|
.map(s -> Pattern.compile(Pattern.quote(s), Pattern.CASE_INSENSITIVE))
|
||||||
.forEach(filteredPatterns::add);
|
.forEach(filteredPatterns::add);
|
||||||
|
|
||||||
NEWLINE_SPLITTER.splitToList(config.filteredRegex()).stream()
|
NEWLINE_SPLITTER.splitToList(config.filteredRegex()).stream()
|
||||||
.map(StringUtils::stripAccents)
|
.map(this::stripAccents)
|
||||||
.map(ChatFilterPlugin::compilePattern)
|
.map(ChatFilterPlugin::compilePattern)
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.forEach(filteredPatterns::add);
|
.forEach(filteredPatterns::add);
|
||||||
|
|
||||||
NEWLINE_SPLITTER.splitToList(config.filteredNames()).stream()
|
NEWLINE_SPLITTER.splitToList(config.filteredNames()).stream()
|
||||||
.map(StringUtils::stripAccents)
|
.map(this::stripAccents)
|
||||||
.map(ChatFilterPlugin::compilePattern)
|
.map(ChatFilterPlugin::compilePattern)
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.forEach(filteredNamePatterns::add);
|
.forEach(filteredNamePatterns::add);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String stripAccents(String input)
|
||||||
|
{
|
||||||
|
return config.stripAccents() ? StringUtils.stripAccents(input) : input;
|
||||||
|
}
|
||||||
|
|
||||||
private static Pattern compilePattern(String pattern)
|
private static Pattern compilePattern(String pattern)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -191,6 +191,7 @@ public class ChatFilterPluginTest
|
|||||||
{
|
{
|
||||||
when(chatFilterConfig.filterType()).thenReturn(ChatFilterType.CENSOR_WORDS);
|
when(chatFilterConfig.filterType()).thenReturn(ChatFilterType.CENSOR_WORDS);
|
||||||
when(chatFilterConfig.filteredWords()).thenReturn("filterme");
|
when(chatFilterConfig.filteredWords()).thenReturn("filterme");
|
||||||
|
when(chatFilterConfig.stripAccents()).thenReturn(true);
|
||||||
|
|
||||||
chatFilterPlugin.updateFilteredPatterns();
|
chatFilterPlugin.updateFilteredPatterns();
|
||||||
assertEquals("plëäsë ******** plügïn", chatFilterPlugin.censorMessage("Blue", "plëäsë fïltërmë plügïn"));
|
assertEquals("plëäsë ******** plügïn", chatFilterPlugin.censorMessage("Blue", "plëäsë fïltërmë plügïn"));
|
||||||
@@ -211,6 +212,7 @@ public class ChatFilterPluginTest
|
|||||||
{
|
{
|
||||||
when(chatFilterConfig.filterType()).thenReturn(ChatFilterType.CENSOR_WORDS);
|
when(chatFilterConfig.filterType()).thenReturn(ChatFilterType.CENSOR_WORDS);
|
||||||
when(chatFilterConfig.filteredWords()).thenReturn("plëäsë, filterme");
|
when(chatFilterConfig.filteredWords()).thenReturn("plëäsë, filterme");
|
||||||
|
when(chatFilterConfig.stripAccents()).thenReturn(true);
|
||||||
|
|
||||||
chatFilterPlugin.updateFilteredPatterns();
|
chatFilterPlugin.updateFilteredPatterns();
|
||||||
assertEquals("****** ******** plügïn", chatFilterPlugin.censorMessage("Blue", "plëäsë fïltërmë plügïn"));
|
assertEquals("****** ******** plügïn", chatFilterPlugin.censorMessage("Blue", "plëäsë fïltërmë plügïn"));
|
||||||
|
|||||||
Reference in New Issue
Block a user