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

Commit

Permalink
feat: add regex matching feature for restricted and default servers (#…
Browse files Browse the repository at this point in the history
…247)

* Add regex matching feature for restricted and default servers

* Fix tests
  • Loading branch information
SirPrimrose authored Aug 14, 2024
1 parent e6887d0 commit cd1c8e3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package net.william278.huskchat.channel;

import de.exlll.configlib.Configuration;
import java.util.regex.Pattern;
import lombok.*;
import net.william278.huskchat.config.Settings;
import net.william278.huskchat.user.OnlineUser;
Expand Down Expand Up @@ -136,7 +137,9 @@ public List<String> getShortcutCommands() {
}

public boolean isServerRestricted(@NotNull String server) {
return restrictedServers.stream().anyMatch(server::equalsIgnoreCase);
return restrictedServers.stream().anyMatch(
restrictedServer -> Pattern.compile(restrictedServer, Pattern.CASE_INSENSITIVE)
.matcher(server).matches());
}

public boolean canUserSend(@NotNull OnlineUser user) {
Expand Down
14 changes: 14 additions & 0 deletions common/src/main/java/net/william278/huskchat/config/Channels.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import de.exlll.configlib.Comment;
import de.exlll.configlib.Configuration;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -113,6 +115,18 @@ public Optional<Channel> getChannel(@Nullable String channelId) {
return channels.stream().filter(channel -> channel.getId().equalsIgnoreCase(channelId)).findFirst();
}

/**
* Gets the default channel for the given server. Falls back to the global default is a server-specific default does not exist.
* @param server The server name
* @return The default channel for the given server, if any
*/
public Optional<String> getServerDefaultChannel(String server) {
return getServerDefaultChannels().entrySet().stream().filter(
defaultChannelEntry -> Pattern.compile(defaultChannelEntry.getKey(),
Pattern.CASE_INSENSITIVE).matcher(server).matches()).map(Entry::getValue)
.findFirst();
}

@NotNull
public List<String> getChannelCommandAliases() {
return Settings.formatCommands(channelCommandAliases);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public abstract class PlayerListener {
// Handle server switches
public final void handlePlayerSwitchServer(@NotNull OnlineUser player, @NotNull String newServer) {
// Switch to the default channel for the server if there is one
final Map<String, String> defaultChannels = plugin.getChannels().getServerDefaultChannels();
if (defaultChannels.containsKey(newServer)) {
plugin.editUserCache(c -> c.switchPlayerChannel(player, defaultChannels.get(newServer), plugin));
final Optional<String> defaultChannel = plugin.getChannels().getServerDefaultChannel(newServer);
if (defaultChannel.isPresent()) {
plugin.editUserCache(c -> c.switchPlayerChannel(player, defaultChannel.get(), plugin));
return;
}

Expand All @@ -52,8 +52,7 @@ public final void handlePlayerSwitchServer(@NotNull OnlineUser player, @NotNull
// Switch the player's channel away if their current channel is now restricted
plugin.getChannels().getChannels().stream()
.filter(channel -> channel.getId().equalsIgnoreCase(currentChannel.get()))
.findFirst().flatMap(channel -> channel.getRestrictedServers().stream()
.filter(restrictedServer -> restrictedServer.equalsIgnoreCase(newServer)).findFirst())
.findFirst().filter(channel -> channel.isServerRestricted(newServer))
.ifPresent(restricted -> plugin.editUserCache(c -> c
.switchPlayerChannel(player, plugin.getChannels().getDefaultChannel(), plugin)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ public boolean dispatch() {
}

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

// Determine the players who will receive the message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of HuskChat, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <will27528@gmail.com>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.william278.huskchat.channel;

import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ChannelTests {
Channel plainTextChannel = Channel
.builder().id("plaintext").restrictedServers(List.of("plain", "text", "restrictions")).build();
Channel regexTextChannel = Channel
.builder().id("regex").restrictedServers(List.of(".*regex.*", "matcher.*", ".*channel")).build();

@Test
public void testPlaintextUnrestrictedServer() {
Assertions.assertFalse(plainTextChannel.isServerRestricted("nota"));
Assertions.assertFalse(plainTextChannel.isServerRestricted("plaintext"));
Assertions.assertFalse(plainTextChannel.isServerRestricted("restricted server"));
}

@Test
public void testPlaintextRestrictedServer() {
Assertions.assertTrue(plainTextChannel.isServerRestricted("plain"));
Assertions.assertTrue(plainTextChannel.isServerRestricted("text"));
Assertions.assertTrue(plainTextChannel.isServerRestricted("restrictions"));
}

@Test
public void testPlaintextRestrictedServerIgnoreCase() {
Assertions.assertTrue(plainTextChannel.isServerRestricted("PLAIN"));
Assertions.assertTrue(plainTextChannel.isServerRestricted("tExT"));
Assertions.assertTrue(plainTextChannel.isServerRestricted("resTriCTioNs"));
}

@Test
public void testRegexUnrestrictedServer() {
Assertions.assertFalse(regexTextChannel.isServerRestricted("does"));
Assertions.assertFalse(regexTextChannel.isServerRestricted("not"));
Assertions.assertFalse(regexTextChannel.isServerRestricted("match"));
}

@Test
public void testRegexRestrictedServer() {
Assertions.assertTrue(regexTextChannel.isServerRestricted("xxx-regex-1234"));
Assertions.assertTrue(regexTextChannel.isServerRestricted("matcher-funtime"));
Assertions.assertTrue(regexTextChannel.isServerRestricted("super-channel"));
}

@Test
public void testRegexRestrictedServerIgnoreCase() {
Assertions.assertTrue(regexTextChannel.isServerRestricted("xXx-REGEX-1234"));
Assertions.assertTrue(regexTextChannel.isServerRestricted("maTCher-funtime"));
Assertions.assertTrue(regexTextChannel.isServerRestricted("sUPEr-chANnel"));
}
}

0 comments on commit cd1c8e3

Please sign in to comment.