/
InfractionCheck.java
144 lines (127 loc) · 4.95 KB
/
InfractionCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package me.dreamerzero.chatregulator.modules.checks;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import me.dreamerzero.chatregulator.config.Configuration;
import me.dreamerzero.chatregulator.enums.ControlType;
import me.dreamerzero.chatregulator.enums.InfractionType;
import me.dreamerzero.chatregulator.result.Result;
import net.kyori.adventure.builder.AbstractBuilder;
import me.dreamerzero.chatregulator.result.PatternResult;
import me.dreamerzero.chatregulator.result.ReplaceableResult;
/**
* Utilities for the detection of restringed words
*/
public final class InfractionCheck implements ICheck {
private final Pattern[] blockedWords;
private final boolean blockable;
private InfractionCheck(){
this(
Configuration.getConfig().getInfractionsConfig().isBlockable(),
Configuration.getBlacklist().getBlockedPatterns()
);
}
private InfractionCheck(boolean blockable, Pattern... blockedWords){
this.blockedWords = blockedWords;
this.blockable = blockable;
}
/**
* {@inheritDoc}
* @param string the string
* @return May return a {@link PatternResult} if the result was successful
* and the check was set to only block the message.
* Or a {@link ReplaceableResult} if the check was successful
* and is configured to replace multiple violations.
* Or a {@link Result} if the check was not successful
* @see ICheck
*/
@Override
public CompletableFuture<Result> check(final @NotNull String string){
return CompletableFuture.supplyAsync(() -> {
final List<Pattern> patterns = new ArrayList<>();
boolean detected = false;
for (final Pattern pattern : blockedWords) {
final Matcher match = pattern.matcher(string);
if(match.find()){
detected = true;
if (blockable) {
return new PatternResult(match.group(), blockable, pattern, match);
}
patterns.add(pattern);
}
}
return detected
? new ReplaceableResult(patterns.toString(), true){
@Override
public String replaceInfraction(){
String original = string;
for (final Pattern pattern : patterns) {
original = pattern.matcher(original).replaceAll(InfractionCheck::generateReplacement);
}
return original;
}
}
: new Result(string, false);
});
}
private static String generateReplacement(MatchResult result) {
final int size = result.group().length()/2;
final StringBuilder builder = new StringBuilder(size);
for (int i = 0; i < size; i++) {
builder.append('*');
}
return builder.toString();
}
@Override
public @NotNull InfractionType type() {
return InfractionType.REGULAR;
}
public static @NotNull CompletableFuture<Result> createCheck(String string){
return new InfractionCheck().check(string);
}
public static @NotNull InfractionCheck.Builder builder(){
return new InfractionCheck.Builder();
}
public static class Builder implements AbstractBuilder<InfractionCheck> {
private List<Pattern> blockedWords;
private boolean replaceable;
private boolean edited = false;
Builder(){}
public Builder blockedPattern(Collection<Pattern> patterns){
if(this.blockedWords == null) {
this.blockedWords = new ArrayList<>(patterns);
} else {
this.blockedWords.addAll(patterns);
}
return this;
}
public Builder blockedPatterns(Pattern... patterns){
if(this.blockedWords == null) {
this.blockedWords = new ArrayList<>(List.of(patterns));
} else {
this.blockedWords.addAll(List.of(patterns));
}
return this;
}
public Builder replaceable(boolean replaceable){
this.replaceable = replaceable;
this.edited = true;
return this;
}
@Override
public InfractionCheck build(){
if(this.blockedWords == null){
this.blockedWords = List.of(Configuration.getBlacklist().getBlockedPatterns());
}
if(!edited){
this.replaceable = Configuration.getConfig().getInfractionsConfig().getControlType() == ControlType.REPLACE;
}
return new InfractionCheck(!replaceable, blockedWords.toArray(Pattern[]::new));
}
}
}