From ea3f6ec5cb994ec8d4ed89afe42b20cff91df4c2 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 25 Mar 2023 13:38:49 +0200 Subject: [PATCH] Client - Just import `birchwood.config` package rather - Call `setDefaults(this.connInfo)` to assign default `db` values - `nick(string)` will now fetch the maximum nickname length and ensure the provided nickname is in said bounds, if not then a `BirchwoodException` with `ErrorType` `NICKNAME_TOO_LONG` is thrown Unit tests - Updated unit test to test the updated value of `MAXNICKLEN` (which on BonoboNET is 30) --- source/birchwood/client/client.d | 40 ++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/source/birchwood/client/client.d b/source/birchwood/client/client.d index b464570..0a5c0e9 100644 --- a/source/birchwood/client/client.d +++ b/source/birchwood/client/client.d @@ -8,7 +8,7 @@ import core.sync.mutex : Mutex; import core.thread : Thread, dur; import std.string; import eventy : EventyEvent = Event, Engine, EventType, Signal; -import birchwood.config : ConnectionInfo; +import birchwood.config; import birchwood.client.exceptions : BirchwoodException, ErrorType; import birchwood.protocol.messages : Message, encodeMessage, decodeMessage, isValidText; @@ -80,6 +80,11 @@ public class Client : Thread */ this.receiver = new ReceiverThread(this); this.sender = new SenderThread(this); + + /** + * Set defaults in db + */ + setDefaults(this.connInfo); } /** @@ -212,9 +217,21 @@ public class Client : Thread /* Ensure no illegal characters in nick name */ if(isValidText(nickname)) { - /* Set the nick */ - Message nickMessage = new Message("", "NICK", nickname); - sendMessage(nickMessage); + // TODO: We could investigate this later if we want to be safer + ulong maxNickLen = connInfo.getDB!(ulong)("MAXNICKLEN"); + + /* If the username's lenght is within the allowed bounds */ + if(nickname.length <= maxNickLen) + { + /* Set the nick */ + Message nickMessage = new Message("", "NICK", nickname); + sendMessage(nickMessage); + } + /* If not */ + else + { + throw new BirchwoodException(ErrorType.NICKNAME_TOO_LONG); + } } else { @@ -1176,6 +1193,21 @@ public class Client : Thread */ client.leaveChannel(["#birchwoodLeave2"]); + + /** + * Definately by now we would have learnt the new MAXNICLEN + * which on BonoboNET is 30, hence the below should work + */ + try + { + client.nick("birchwood123456789123456789123"); + assert(true); + } + catch(BirchwoodException e) + { + assert(false); + } + // TODO: Don't forget to re-enable this when done testing! Thread.sleep(dur!("seconds")(15)); client.quit();