From 14f40813b7bb7eeab44e6a5a9476afe395cc7118 Mon Sep 17 00:00:00 2001 From: Florian Charlaix Date: Fri, 7 Jan 2022 10:25:57 +0100 Subject: [PATCH] Add channel check on message broadcast --- .../univ/lyon1/common/exception/NotInChannel.java | 9 +++++++++ src/fr/univ/lyon1/server/ConnectedClient.java | 15 ++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 src/fr/univ/lyon1/common/exception/NotInChannel.java diff --git a/src/fr/univ/lyon1/common/exception/NotInChannel.java b/src/fr/univ/lyon1/common/exception/NotInChannel.java new file mode 100644 index 0000000..5a2473f --- /dev/null +++ b/src/fr/univ/lyon1/common/exception/NotInChannel.java @@ -0,0 +1,9 @@ +package fr.univ.lyon1.common.exception; + +import fr.univ.lyon1.common.Channel; + +public class NotInChannel extends ChatException { + public NotInChannel(Channel channel) { + super("Your not in channel "+channel); + } +} diff --git a/src/fr/univ/lyon1/server/ConnectedClient.java b/src/fr/univ/lyon1/server/ConnectedClient.java index 74ce713..09f98c6 100644 --- a/src/fr/univ/lyon1/server/ConnectedClient.java +++ b/src/fr/univ/lyon1/server/ConnectedClient.java @@ -5,10 +5,7 @@ import fr.univ.lyon1.common.Message; import fr.univ.lyon1.common.User; import fr.univ.lyon1.common.command.Command; import fr.univ.lyon1.common.command.CommandType; -import fr.univ.lyon1.common.exception.ChatException; -import fr.univ.lyon1.common.exception.LoginInvalid; -import fr.univ.lyon1.common.exception.LoginRequired; -import fr.univ.lyon1.common.exception.UnknownCommand; +import fr.univ.lyon1.common.exception.*; import fr.univ.lyon1.server.models.ChannelModel; import fr.univ.lyon1.server.models.UserModel; @@ -80,11 +77,15 @@ public class ConnectedClient implements Runnable { } } - private void commandMessage(Command cmd) { + private void commandMessage(Command cmd) throws NotInChannel { Message msg = (Message) cmd.getArgs().get(0); msg.setSender(this.user); - // ToDo: Check the user channel - server.broadcastMessage(msg, id); + + ChannelModel chan = ChannelModel.get(msg.getChannel().getUUID()); + if (chan == null || !chan.have(this.user)) + throw new NotInChannel(chan); + else + server.broadcastMessage(msg, id); } private void commandList() throws IOException {