Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit cd1c8e3

Browse files
authored
feat: add regex matching feature for restricted and default servers (#247)
* Add regex matching feature for restricted and default servers * Fix tests
1 parent e6887d0 commit cd1c8e3

File tree

5 files changed

+98
-11
lines changed

5 files changed

+98
-11
lines changed

common/src/main/java/net/william278/huskchat/channel/Channel.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package net.william278.huskchat.channel;
2121

2222
import de.exlll.configlib.Configuration;
23+
import java.util.regex.Pattern;
2324
import lombok.*;
2425
import net.william278.huskchat.config.Settings;
2526
import net.william278.huskchat.user.OnlineUser;
@@ -136,7 +137,9 @@ public List<String> getShortcutCommands() {
136137
}
137138

138139
public boolean isServerRestricted(@NotNull String server) {
139-
return restrictedServers.stream().anyMatch(server::equalsIgnoreCase);
140+
return restrictedServers.stream().anyMatch(
141+
restrictedServer -> Pattern.compile(restrictedServer, Pattern.CASE_INSENSITIVE)
142+
.matcher(server).matches());
140143
}
141144

142145
public boolean canUserSend(@NotNull OnlineUser user) {

common/src/main/java/net/william278/huskchat/config/Channels.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import de.exlll.configlib.Comment;
2323
import de.exlll.configlib.Configuration;
24+
import java.util.Map.Entry;
25+
import java.util.regex.Pattern;
2426
import lombok.AccessLevel;
2527
import lombok.Getter;
2628
import lombok.NoArgsConstructor;
@@ -113,6 +115,18 @@ public Optional<Channel> getChannel(@Nullable String channelId) {
113115
return channels.stream().filter(channel -> channel.getId().equalsIgnoreCase(channelId)).findFirst();
114116
}
115117

118+
/**
119+
* Gets the default channel for the given server. Falls back to the global default is a server-specific default does not exist.
120+
* @param server The server name
121+
* @return The default channel for the given server, if any
122+
*/
123+
public Optional<String> getServerDefaultChannel(String server) {
124+
return getServerDefaultChannels().entrySet().stream().filter(
125+
defaultChannelEntry -> Pattern.compile(defaultChannelEntry.getKey(),
126+
Pattern.CASE_INSENSITIVE).matcher(server).matches()).map(Entry::getValue)
127+
.findFirst();
128+
}
129+
116130
@NotNull
117131
public List<String> getChannelCommandAliases() {
118132
return Settings.formatCommands(channelCommandAliases);

common/src/main/java/net/william278/huskchat/listener/PlayerListener.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public abstract class PlayerListener {
3636
// Handle server switches
3737
public final void handlePlayerSwitchServer(@NotNull OnlineUser player, @NotNull String newServer) {
3838
// Switch to the default channel for the server if there is one
39-
final Map<String, String> defaultChannels = plugin.getChannels().getServerDefaultChannels();
40-
if (defaultChannels.containsKey(newServer)) {
41-
plugin.editUserCache(c -> c.switchPlayerChannel(player, defaultChannels.get(newServer), plugin));
39+
final Optional<String> defaultChannel = plugin.getChannels().getServerDefaultChannel(newServer);
40+
if (defaultChannel.isPresent()) {
41+
plugin.editUserCache(c -> c.switchPlayerChannel(player, defaultChannel.get(), plugin));
4242
return;
4343
}
4444

@@ -52,8 +52,7 @@ public final void handlePlayerSwitchServer(@NotNull OnlineUser player, @NotNull
5252
// Switch the player's channel away if their current channel is now restricted
5353
plugin.getChannels().getChannels().stream()
5454
.filter(channel -> channel.getId().equalsIgnoreCase(currentChannel.get()))
55-
.findFirst().flatMap(channel -> channel.getRestrictedServers().stream()
56-
.filter(restrictedServer -> restrictedServer.equalsIgnoreCase(newServer)).findFirst())
55+
.findFirst().filter(channel -> channel.isServerRestricted(newServer))
5756
.ifPresent(restricted -> plugin.editUserCache(c -> c
5857
.switchPlayerChannel(player, plugin.getChannels().getDefaultChannel(), plugin)));
5958
}

common/src/main/java/net/william278/huskchat/message/ChatMessage.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,9 @@ public boolean dispatch() {
7272
}
7373

7474
// Verify that the player is not sending a message from a server where channel access is restricted
75-
for (String restrictedServer : channel.get().getRestrictedServers()) {
76-
if (restrictedServer.equalsIgnoreCase(getSender().getServerName())) {
77-
getPlugin().getLocales().sendMessage(getSender(), "error_channel_restricted_server", channel.get().getId());
78-
return true;
79-
}
75+
if (channel.get().isServerRestricted(getSender().getServerName())) {
76+
getPlugin().getLocales().sendMessage(getSender(), "error_channel_restricted_server", channel.get().getId());
77+
return true;
8078
}
8179

8280
// Determine the players who will receive the message;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This file is part of HuskChat, licensed under the Apache License 2.0.
3+
*
4+
* Copyright (c) William278 <will27528@gmail.com>
5+
* Copyright (c) contributors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package net.william278.huskchat.channel;
21+
22+
import java.util.List;
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.Test;
25+
26+
public class ChannelTests {
27+
Channel plainTextChannel = Channel
28+
.builder().id("plaintext").restrictedServers(List.of("plain", "text", "restrictions")).build();
29+
Channel regexTextChannel = Channel
30+
.builder().id("regex").restrictedServers(List.of(".*regex.*", "matcher.*", ".*channel")).build();
31+
32+
@Test
33+
public void testPlaintextUnrestrictedServer() {
34+
Assertions.assertFalse(plainTextChannel.isServerRestricted("nota"));
35+
Assertions.assertFalse(plainTextChannel.isServerRestricted("plaintext"));
36+
Assertions.assertFalse(plainTextChannel.isServerRestricted("restricted server"));
37+
}
38+
39+
@Test
40+
public void testPlaintextRestrictedServer() {
41+
Assertions.assertTrue(plainTextChannel.isServerRestricted("plain"));
42+
Assertions.assertTrue(plainTextChannel.isServerRestricted("text"));
43+
Assertions.assertTrue(plainTextChannel.isServerRestricted("restrictions"));
44+
}
45+
46+
@Test
47+
public void testPlaintextRestrictedServerIgnoreCase() {
48+
Assertions.assertTrue(plainTextChannel.isServerRestricted("PLAIN"));
49+
Assertions.assertTrue(plainTextChannel.isServerRestricted("tExT"));
50+
Assertions.assertTrue(plainTextChannel.isServerRestricted("resTriCTioNs"));
51+
}
52+
53+
@Test
54+
public void testRegexUnrestrictedServer() {
55+
Assertions.assertFalse(regexTextChannel.isServerRestricted("does"));
56+
Assertions.assertFalse(regexTextChannel.isServerRestricted("not"));
57+
Assertions.assertFalse(regexTextChannel.isServerRestricted("match"));
58+
}
59+
60+
@Test
61+
public void testRegexRestrictedServer() {
62+
Assertions.assertTrue(regexTextChannel.isServerRestricted("xxx-regex-1234"));
63+
Assertions.assertTrue(regexTextChannel.isServerRestricted("matcher-funtime"));
64+
Assertions.assertTrue(regexTextChannel.isServerRestricted("super-channel"));
65+
}
66+
67+
@Test
68+
public void testRegexRestrictedServerIgnoreCase() {
69+
Assertions.assertTrue(regexTextChannel.isServerRestricted("xXx-REGEX-1234"));
70+
Assertions.assertTrue(regexTextChannel.isServerRestricted("maTCher-funtime"));
71+
Assertions.assertTrue(regexTextChannel.isServerRestricted("sUPEr-chANnel"));
72+
}
73+
}

0 commit comments

Comments
 (0)