chatfilter: add filtering by username
This commit is contained in:
@@ -65,11 +65,22 @@ public interface ChatFilterConfig extends Config
|
||||
return "";
|
||||
}
|
||||
|
||||
@ConfigItem(
|
||||
keyName = "filteredNames",
|
||||
name = "Filtered Names",
|
||||
description = "List of filtered names, one per line. Accepts regular expressions",
|
||||
position = 4
|
||||
)
|
||||
default String filteredNames()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
@ConfigItem(
|
||||
keyName = "filterFriends",
|
||||
name = "Filter Friends",
|
||||
description = "Filter your friends' messages",
|
||||
position = 4
|
||||
position = 5
|
||||
)
|
||||
default boolean filterFriends()
|
||||
{
|
||||
@@ -80,7 +91,7 @@ public interface ChatFilterConfig extends Config
|
||||
keyName = "filterClan",
|
||||
name = "Filter Clan Chat Members",
|
||||
description = "Filter your clan chat members' messages",
|
||||
position = 5
|
||||
position = 6
|
||||
)
|
||||
default boolean filterClan()
|
||||
{
|
||||
@@ -91,7 +102,7 @@ public interface ChatFilterConfig extends Config
|
||||
keyName = "filterLogin",
|
||||
name = "Filter Logged In/Out Messages",
|
||||
description = "Filter your private chat to remove logged in/out messages",
|
||||
position = 6
|
||||
position = 7
|
||||
)
|
||||
default boolean filterLogin()
|
||||
{
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
*/
|
||||
package net.runelite.client.plugins.chatfilter;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.inject.Provides;
|
||||
@@ -62,10 +63,12 @@ public class ChatFilterPlugin extends Plugin
|
||||
.omitEmptyStrings()
|
||||
.trimResults();
|
||||
|
||||
private static final String CENSOR_MESSAGE = "Hey, everyone, I just tried to say something very silly!";
|
||||
@VisibleForTesting
|
||||
static final String CENSOR_MESSAGE = "Hey, everyone, I just tried to say something very silly!";
|
||||
|
||||
private final CharMatcher jagexPrintableCharMatcher = Text.JAGEX_PRINTABLE_CHAR_MATCHER;
|
||||
private final List<Pattern> filteredPatterns = new ArrayList<>();
|
||||
private final List<Pattern> filteredNamePatterns = new ArrayList<>();
|
||||
|
||||
@Inject
|
||||
private Client client;
|
||||
@@ -143,7 +146,7 @@ public class ChatFilterPlugin extends Plugin
|
||||
int stringStackSize = client.getStringStackSize();
|
||||
|
||||
String message = stringStack[stringStackSize - 1];
|
||||
String censoredMessage = censorMessage(message);
|
||||
String censoredMessage = censorMessage(name, message);
|
||||
|
||||
if (censoredMessage == null)
|
||||
{
|
||||
@@ -165,7 +168,7 @@ public class ChatFilterPlugin extends Plugin
|
||||
return;
|
||||
}
|
||||
|
||||
String message = censorMessage(event.getOverheadText());
|
||||
String message = censorMessage(event.getActor().getName(), event.getOverheadText());
|
||||
|
||||
if (message == null)
|
||||
{
|
||||
@@ -183,10 +186,23 @@ public class ChatFilterPlugin extends Plugin
|
||||
(config.filterClan() || !clanManager.isClanMember(playerName));
|
||||
}
|
||||
|
||||
String censorMessage(final String message)
|
||||
String censorMessage(final String username, final String message)
|
||||
{
|
||||
String strippedMessage = jagexPrintableCharMatcher.retainFrom(message)
|
||||
.replace('\u00A0', ' ');
|
||||
if (shouldFilterByName(username))
|
||||
{
|
||||
switch (config.filterType())
|
||||
{
|
||||
case CENSOR_WORDS:
|
||||
return StringUtils.repeat('*', strippedMessage.length());
|
||||
case CENSOR_MESSAGE:
|
||||
return CENSOR_MESSAGE;
|
||||
case REMOVE_MESSAGE:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
boolean filtered = false;
|
||||
for (Pattern pattern : filteredPatterns)
|
||||
{
|
||||
@@ -199,7 +215,7 @@ public class ChatFilterPlugin extends Plugin
|
||||
switch (config.filterType())
|
||||
{
|
||||
case CENSOR_WORDS:
|
||||
m.appendReplacement(sb, StringUtils.repeat("*", m.group(0).length()));
|
||||
m.appendReplacement(sb, StringUtils.repeat('*', m.group(0).length()));
|
||||
filtered = true;
|
||||
break;
|
||||
case CENSOR_MESSAGE:
|
||||
@@ -219,25 +235,33 @@ public class ChatFilterPlugin extends Plugin
|
||||
void updateFilteredPatterns()
|
||||
{
|
||||
filteredPatterns.clear();
|
||||
filteredNamePatterns.clear();
|
||||
|
||||
Text.fromCSV(config.filteredWords()).stream()
|
||||
.map(s -> Pattern.compile(Pattern.quote(s), Pattern.CASE_INSENSITIVE))
|
||||
.forEach(filteredPatterns::add);
|
||||
|
||||
NEWLINE_SPLITTER.splitToList(config.filteredRegex()).stream()
|
||||
.map(s ->
|
||||
{
|
||||
try
|
||||
{
|
||||
return Pattern.compile(s, Pattern.CASE_INSENSITIVE);
|
||||
}
|
||||
catch (PatternSyntaxException ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.map(ChatFilterPlugin::compilePattern)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(filteredPatterns::add);
|
||||
|
||||
NEWLINE_SPLITTER.splitToList(config.filteredNames()).stream()
|
||||
.map(ChatFilterPlugin::compilePattern)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(filteredNamePatterns::add);
|
||||
}
|
||||
|
||||
private static Pattern compilePattern(String pattern)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
|
||||
}
|
||||
catch (PatternSyntaxException ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
@@ -253,4 +277,19 @@ public class ChatFilterPlugin extends Plugin
|
||||
//Refresh chat after config change to reflect current rules
|
||||
client.refreshChat();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
boolean shouldFilterByName(final String playerName)
|
||||
{
|
||||
String sanitizedName = Text.standardize(playerName);
|
||||
for (Pattern pattern : filteredNamePatterns)
|
||||
{
|
||||
Matcher m = pattern.matcher(sanitizedName);
|
||||
if (m.find())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user