chat filter: add options to filter friends and clan members

Also no longer ever filter messages from the local player

Co-authored-by: Adam <Adam@sigterm.info>
This commit is contained in:
osrs-music-map
2019-05-26 10:51:56 -04:00
committed by Adam
parent 0211149ce2
commit da260fb8b2
5 changed files with 108 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018, Magic fTail * Copyright (c) 2018, Magic fTail
* Copyright (c) 2019, osrs-music-map <osrs-music-map@users.noreply.github.com>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -63,4 +64,26 @@ public interface ChatFilterConfig extends Config
{ {
return ""; return "";
} }
@ConfigItem(
keyName = "filterFriends",
name = "Filter Friends",
description = "Filter your friends' messages",
position = 4
)
default boolean filterFriends()
{
return false;
}
@ConfigItem(
keyName = "filterClan",
name = "Filter Clan Chat Members",
description = "Filter your clan chat members' messages",
position = 5
)
default boolean filterClan()
{
return false;
}
} }

View File

@@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018, Magic fTail * Copyright (c) 2018, Magic fTail
* Copyright (c) 2019, osrs-music-map <osrs-music-map@users.noreply.github.com>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -35,6 +36,7 @@ import java.util.regex.PatternSyntaxException;
import javax.inject.Inject; import javax.inject.Inject;
import net.runelite.api.ChatMessageType; import net.runelite.api.ChatMessageType;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.api.MessageNode;
import net.runelite.api.Player; import net.runelite.api.Player;
import net.runelite.api.events.ConfigChanged; import net.runelite.api.events.ConfigChanged;
import net.runelite.api.events.OverheadTextChanged; import net.runelite.api.events.OverheadTextChanged;
@@ -97,7 +99,10 @@ public class ChatFilterPlugin extends Plugin
int[] intStack = client.getIntStack(); int[] intStack = client.getIntStack();
int intStackSize = client.getIntStackSize(); int intStackSize = client.getIntStackSize();
ChatMessageType chatMessageType = ChatMessageType.of(intStack[intStackSize - 1]); int messageType = intStack[intStackSize - 2];
int messageId = intStack[intStackSize - 1];
ChatMessageType chatMessageType = ChatMessageType.of(messageType);
// Only filter public chat and private messages // Only filter public chat and private messages
switch (chatMessageType) switch (chatMessageType)
@@ -113,6 +118,13 @@ public class ChatFilterPlugin extends Plugin
return; return;
} }
MessageNode messageNode = (MessageNode) client.getMessages().get(messageId);
String name = messageNode.getName();
if (!shouldFilterPlayerMessage(name))
{
return;
}
String[] stringStack = client.getStringStack(); String[] stringStack = client.getStringStack();
int stringStackSize = client.getStringStackSize(); int stringStackSize = client.getStringStackSize();
@@ -122,7 +134,7 @@ public class ChatFilterPlugin extends Plugin
if (censoredMessage == null) if (censoredMessage == null)
{ {
// Block the message // Block the message
intStack[intStackSize - 2] = 0; intStack[intStackSize - 3] = 0;
} }
else else
{ {
@@ -134,7 +146,7 @@ public class ChatFilterPlugin extends Plugin
@Subscribe @Subscribe
public void onOverheadTextChanged(OverheadTextChanged event) public void onOverheadTextChanged(OverheadTextChanged event)
{ {
if (!(event.getActor() instanceof Player)) if (!(event.getActor() instanceof Player) || !shouldFilterPlayerMessage(event.getActor().getName()))
{ {
return; return;
} }
@@ -149,6 +161,14 @@ public class ChatFilterPlugin extends Plugin
event.getActor().setOverheadText(message); event.getActor().setOverheadText(message);
} }
boolean shouldFilterPlayerMessage(String playerName)
{
boolean isMessageFromSelf = playerName.equals(client.getLocalPlayer().getName());
return !isMessageFromSelf &&
(config.filterFriends() || !client.isFriended(playerName, false)) &&
(config.filterClan() || !client.isClanMember(playerName));
}
String censorMessage(final String message) String censorMessage(final String message)
{ {
String strippedMessage = jagexPrintableCharMatcher.retainFrom(message) String strippedMessage = jagexPrintableCharMatcher.retainFrom(message)

View File

@@ -189,8 +189,10 @@ CHAT_FILTER:
sload 11 ; Load the message sload 11 ; Load the message
iconst 1 ; Gets changed to 0 if message is blocked iconst 1 ; Gets changed to 0 if message is blocked
iload 10 ; Load the messageType iload 10 ; Load the messageType
iload 9 ; Load the id of the messageNode
sconst "chatFilterCheck" sconst "chatFilterCheck"
runelite_callback runelite_callback
pop_int ; Pop the id of the messageNode
pop_int ; Pop the messageType pop_int ; Pop the messageType
iconst 1 ; 2nd half of conditional iconst 1 ; 2nd half of conditional
sstore 11 ; Override the message with our filtered message sstore 11 ; Override the message with our filtered message

View File

@@ -359,9 +359,11 @@ CHAT_FILTER:
sload 0 ; Load the message sload 0 ; Load the message
iconst 1 ; Gets changed to 0 if message is blocked iconst 1 ; Gets changed to 0 if message is blocked
iload 15 ; Load the messageType iload 15 ; Load the messageType
iload 12 ; Load the id of the messageNode
sconst "chatFilterCheck" sconst "chatFilterCheck"
runelite_callback runelite_callback
pop_int ; Pop the messageType pop_int ; Pop the id of the messageNode
pop_int ; Pop the messageType
iconst 1 ; 2nd half of conditional iconst 1 ; 2nd half of conditional
sstore 0 ; Override the message with our filtered message sstore 0 ; Override the message with our filtered message
if_icmpeq LABEL327 ; Check if we are building this message if_icmpeq LABEL327 ; Check if we are building this message

View File

@@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2019, Adam <Adam@sigterm.info> * Copyright (c) 2019, Adam <Adam@sigterm.info>
* Copyright (c) 2019, osrs-music-map <osrs-music-map@users.noreply.github.com>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -29,8 +30,11 @@ import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule; import com.google.inject.testing.fieldbinder.BoundFieldModule;
import javax.inject.Inject; import javax.inject.Inject;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.api.Player;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -49,6 +53,10 @@ public class ChatFilterPluginTest
@Bind @Bind
private ChatFilterConfig chatFilterConfig; private ChatFilterConfig chatFilterConfig;
@Mock
@Bind
private Player localPlayer;
@Inject @Inject
private ChatFilterPlugin chatFilterPlugin; private ChatFilterPlugin chatFilterPlugin;
@@ -60,6 +68,7 @@ public class ChatFilterPluginTest
when(chatFilterConfig.filterType()).thenReturn(ChatFilterType.CENSOR_WORDS); when(chatFilterConfig.filterType()).thenReturn(ChatFilterType.CENSOR_WORDS);
when(chatFilterConfig.filteredWords()).thenReturn(""); when(chatFilterConfig.filteredWords()).thenReturn("");
when(chatFilterConfig.filteredRegex()).thenReturn(""); when(chatFilterConfig.filteredRegex()).thenReturn("");
when(client.getLocalPlayer()).thenReturn(localPlayer);
} }
@Test @Test
@@ -110,4 +119,51 @@ public class ChatFilterPluginTest
chatFilterPlugin.updateFilteredPatterns(); chatFilterPlugin.updateFilteredPatterns();
assertNull(chatFilterPlugin.censorMessage("te\u008Cst")); assertNull(chatFilterPlugin.censorMessage("te\u008Cst"));
} }
@Test
public void testMessageFromFriendIsFiltered()
{
when(client.isFriended("Iron Mammal", false)).thenReturn(true);
when(chatFilterConfig.filterFriends()).thenReturn(true);
assertTrue(chatFilterPlugin.shouldFilterPlayerMessage("Iron Mammal"));
}
@Test
public void testMessageFromFriendIsNotFiltered()
{
when(client.isFriended("Iron Mammal", false)).thenReturn(true);
when(chatFilterConfig.filterFriends()).thenReturn(false);
assertFalse(chatFilterPlugin.shouldFilterPlayerMessage("Iron Mammal"));
}
@Test
public void testMessageFromClanIsFiltered()
{
when(client.isClanMember("B0aty")).thenReturn(true);
when(chatFilterConfig.filterClan()).thenReturn(true);
assertTrue(chatFilterPlugin.shouldFilterPlayerMessage("B0aty"));
}
@Test
public void testMessageFromClanIsNotFiltered()
{
when(client.isClanMember("B0aty")).thenReturn(true);
when(chatFilterConfig.filterClan()).thenReturn(false);
assertFalse(chatFilterPlugin.shouldFilterPlayerMessage("B0aty"));
}
@Test
public void testMessageFromSelfIsNotFiltered()
{
when(localPlayer.getName()).thenReturn("Swampletics");
assertFalse(chatFilterPlugin.shouldFilterPlayerMessage("Swampletics"));
}
@Test
public void testMessageFromNonFriendNonClanIsFiltered()
{
when(client.isFriended("Woox", false)).thenReturn(false);
when(client.isClanMember("Woox")).thenReturn(false);
assertTrue(chatFilterPlugin.shouldFilterPlayerMessage("Woox"));
}
} }