Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions application/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"scamBlocker": {
"mode": "AUTO_DELETE_BUT_APPROVE_QUARANTINE",
"reportChannelPattern": "commands",
"suspiciousKeywords": ["nitro", "boob", "sexy", "sexi", "esex"],
"hostWhitelist": ["discord.com", "discord.gg", "discord.media", "discordapp.com", "discordapp.net", "discordstatus.com"],
"hostBlacklist": ["bit.ly"],
"suspiciousHostKeywords": ["discord", "nitro", "premium"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.togetherjava.tjbot.config.ScamBlockerConfig;

import java.net.URI;
import java.util.Locale;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -39,19 +40,23 @@ public boolean isScam(CharSequence message) {
}

private boolean isScam(AnalyseResults results) {
if (results.pingsEveryone && results.containsNitroKeyword && results.hasUrl) {
if (results.pingsEveryone && results.containsSuspiciousKeyword && results.hasUrl) {
return true;
}
return results.containsNitroKeyword && results.hasSuspiciousUrl;
return results.containsSuspiciousKeyword && results.hasSuspiciousUrl;
}

private void analyzeToken(String token, AnalyseResults results) {
if ("@everyone".equalsIgnoreCase(token)) {
if (token.isBlank()) {
return;
}

if (!results.pingsEveryone && "@everyone".equalsIgnoreCase(token)) {
results.pingsEveryone = true;
}

if ("nitro".equalsIgnoreCase(token)) {
results.containsNitroKeyword = true;
if (!results.containsSuspiciousKeyword && containsSuspiciousKeyword(token)) {
results.containsSuspiciousKeyword = true;
}

if (token.startsWith("http")) {
Expand Down Expand Up @@ -91,6 +96,15 @@ private void analyzeUrl(String url, AnalyseResults results) {
}
}

private boolean containsSuspiciousKeyword(String token) {
String preparedToken = token.toLowerCase(Locale.US);

return config.getSuspiciousKeywords()
.stream()
.map(keyword -> keyword.toLowerCase(Locale.US))
.anyMatch(preparedToken::contains);
}

private boolean isHostSimilarToKeyword(String host, String keyword) {
// NOTE This algorithm is far from optimal.
// It is good enough for our purpose though and not that complex.
Expand All @@ -116,7 +130,7 @@ private boolean isHostSimilarToKeyword(String host, String keyword) {

private static class AnalyseResults {
private boolean pingsEveryone;
private boolean containsNitroKeyword;
private boolean containsSuspiciousKeyword;
private boolean hasUrl;
private boolean hasSuspiciousUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public final class ScamBlockerConfig {
private final Mode mode;
private final String reportChannelPattern;
private final Set<String> suspiciousKeywords;
private final Set<String> hostWhitelist;
private final Set<String> hostBlacklist;
private final Set<String> suspiciousHostKeywords;
Expand All @@ -26,6 +27,8 @@ public final class ScamBlockerConfig {
private ScamBlockerConfig(@JsonProperty(value = "mode", required = true) Mode mode,
@JsonProperty(value = "reportChannelPattern",
required = true) String reportChannelPattern,
@JsonProperty(value = "suspiciousKeywords",
required = true) Set<String> suspiciousKeywords,
@JsonProperty(value = "hostWhitelist", required = true) Set<String> hostWhitelist,
@JsonProperty(value = "hostBlacklist", required = true) Set<String> hostBlacklist,
@JsonProperty(value = "suspiciousHostKeywords",
Expand All @@ -34,6 +37,7 @@ private ScamBlockerConfig(@JsonProperty(value = "mode", required = true) Mode mo
required = true) int isHostSimilarToKeywordDistanceThreshold) {
this.mode = Objects.requireNonNull(mode);
this.reportChannelPattern = Objects.requireNonNull(reportChannelPattern);
this.suspiciousKeywords = new HashSet<>(Objects.requireNonNull(suspiciousKeywords));
this.hostWhitelist = new HashSet<>(Objects.requireNonNull(hostWhitelist));
this.hostBlacklist = new HashSet<>(Objects.requireNonNull(hostBlacklist));
this.suspiciousHostKeywords = new HashSet<>(Objects.requireNonNull(suspiciousHostKeywords));
Expand All @@ -59,6 +63,15 @@ public String getReportChannelPattern() {
return reportChannelPattern;
}

/**
* Gets the set of keywords that are considered suspicious if they appear in a message.
*
* @return the set of suspicious keywords
*/
public Set<String> getSuspiciousKeywords() {
return Collections.unmodifiableSet(suspiciousKeywords);
}

/**
* Gets the set of trusted hosts. Urls using those hosts are not considered scam.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void setUp() {
ScamBlockerConfig scamConfig = mock(ScamBlockerConfig.class);
when(config.getScamBlocker()).thenReturn(scamConfig);

when(scamConfig.getSuspiciousKeywords())
.thenReturn(Set.of("nitro", "boob", "sexi", "esex"));
when(scamConfig.getHostWhitelist()).thenReturn(Set.of("discord.com", "discord.gg",
"discord.media", "discordapp.com", "discordapp.net", "discordstatus.com"));
when(scamConfig.getHostBlacklist()).thenReturn(Set.of("bit.ly"));
Expand Down Expand Up @@ -139,6 +141,9 @@ private static List<String> provideRealScamMessages() {
Steam is giving away free discord nitro, have time to pick up at my link https://bit.ly/3nlzmUa before the action is over.""",
"""
@everyone, take nitro faster, it's already running out
https://discordu.gift/u1CHEX2sjpDuR3T5""");
https://discordu.gift/u1CHEX2sjpDuR3T5""",
"@everyone join now https://discord.gg/boobise",
"@everyone join now https://discord.gg/esexiest",
"@everyone Join Now | Free All 12-18 y.o. https://discord.gg/eesexe");
}
}