diff --git a/core/src/main/java/nl/andrewl/concord_core/msg/Serializer.java b/core/src/main/java/nl/andrewl/concord_core/msg/Serializer.java index 11c1f93..9e9c924 100644 --- a/core/src/main/java/nl/andrewl/concord_core/msg/Serializer.java +++ b/core/src/main/java/nl/andrewl/concord_core/msg/Serializer.java @@ -1,5 +1,6 @@ package nl.andrewl.concord_core.msg; +import nl.andrewl.concord_core.msg.types.Error; import nl.andrewl.concord_core.msg.types.*; import java.io.*; @@ -22,6 +23,7 @@ public class Serializer { registerType(6, ChannelUsersRequest.class); registerType(7, ChannelUsersResponse.class); registerType(8, ServerMetaData.class); + registerType(9, Error.class); } private static void registerType(int id, Class clazz) { diff --git a/core/src/main/java/nl/andrewl/concord_core/msg/types/Error.java b/core/src/main/java/nl/andrewl/concord_core/msg/types/Error.java new file mode 100644 index 0000000..052b335 --- /dev/null +++ b/core/src/main/java/nl/andrewl/concord_core/msg/types/Error.java @@ -0,0 +1,51 @@ +package nl.andrewl.concord_core.msg.types; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import nl.andrewl.concord_core.msg.Message; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import static nl.andrewl.concord_core.msg.MessageUtils.*; + +/** + * Error message which can be sent between either the server or client to + * indicate an unsavory situation. + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Error implements Message { + public enum Level {WARNING, ERROR} + + private Level level; + private String message; + + public static Error warning(String message) { + return new Error(Level.WARNING, message); + } + + public static Error error(String message) { + return new Error(Level.ERROR, message); + } + + @Override + public int getByteCount() { + return Integer.BYTES + getByteSize(this.message); + } + + @Override + public void write(DataOutputStream o) throws IOException { + writeEnum(this.level, o); + writeString(this.message, o); + } + + @Override + public void read(DataInputStream i) throws IOException { + this.level = readEnum(Level.class, i); + this.message = readString(i); + } +} diff --git a/server/src/main/java/nl/andrewl/concord_server/cli/command/AddChannelCommand.java b/server/src/main/java/nl/andrewl/concord_server/cli/command/AddChannelCommand.java index 5619eac..b6ab208 100644 --- a/server/src/main/java/nl/andrewl/concord_server/cli/command/AddChannelCommand.java +++ b/server/src/main/java/nl/andrewl/concord_server/cli/command/AddChannelCommand.java @@ -12,13 +12,16 @@ public class AddChannelCommand implements ServerCliCommand { public void handle(ConcordServer server, String[] args) throws Exception { if (args.length < 1) { System.err.println("Missing required name argument."); + return; } String name = args[0].trim().toLowerCase().replaceAll("\\s+", "-"); if (name.isBlank()) { System.err.println("Cannot create channel with blank name."); + return; } if (server.getChannelManager().getChannelByName(name).isPresent()) { System.err.println("Channel with that name already exists."); + return; } String description = null; if (args.length > 1) { diff --git a/server/src/main/java/nl/andrewl/concord_server/event/ChatHandler.java b/server/src/main/java/nl/andrewl/concord_server/event/ChatHandler.java index bae8f1e..dd42219 100644 --- a/server/src/main/java/nl/andrewl/concord_server/event/ChatHandler.java +++ b/server/src/main/java/nl/andrewl/concord_server/event/ChatHandler.java @@ -1,6 +1,7 @@ package nl.andrewl.concord_server.event; import nl.andrewl.concord_core.msg.types.Chat; +import nl.andrewl.concord_core.msg.types.Error; import nl.andrewl.concord_server.ClientThread; import nl.andrewl.concord_server.ConcordServer; import org.dizitart.no2.Document; @@ -11,6 +12,10 @@ import java.util.Map; public class ChatHandler implements MessageHandler { @Override public void handle(Chat msg, ClientThread client, ConcordServer server) throws IOException { + if (msg.getMessage().length() > server.getConfig().getMaxMessageLength()) { + client.getCurrentChannel().sendMessage(Error.warning("Message is too long.")); + return; + } server.getExecutorService().submit(() -> { var collection = client.getCurrentChannel().getMessageCollection(); Document doc = new Document(Map.of(