From c98c58e03fd3703f5d500424e886e5f06fe0768d Mon Sep 17 00:00:00 2001 From: Maxime Bouillot Date: Fri, 26 Jul 2024 19:50:11 +0200 Subject: [PATCH] Add rmdir command. (#226) --- commands.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/commands.go b/commands.go index ff7f68ec..fac5a0b1 100644 --- a/commands.go +++ b/commands.go @@ -70,6 +70,7 @@ var ( {"uname", unameCMD, "", "Show build info"}, {"uptime", uptimeCMD, "", "Show server uptime"}, {"8ball", eightBallCMD, "`question`", "Always tells the truth."}, + {"rmdir", rmdirCMD, "#`room`", "Remove an empty room"}, } SecretCMDs = []CMD{ {"ls", lsCMD, "???", "???"}, @@ -938,3 +939,18 @@ func eightBallCMD(_ string, u *User) { u.room.broadcast("8ball", responses[rand.Intn(len(responses))]+u.Name) }() } + +func rmdirCMD(rest string, u *User) { + if rest == "#main" { + u.room.broadcast("", "rmdir: failed to remove '"+rest+"': Operation not permitted") + } else if room, ok := Rooms[rest]; ok { + if len(room.users) == 0 { + delete(Rooms, rest) + u.room.broadcast("", "rmdir: removing directory, '"+rest+"'") + } else { + u.room.broadcast("", "rmdir: failed to remove '"+rest+"': Room not empty") + } + } else { + u.room.broadcast("", "rmdir: failed to remove '"+rest+"': No such room") + } +}