summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2014-03-27 12:28:20 +0100
committerKevin Chabowski <kevin@kch42.de>2014-03-27 23:51:07 +0100
commit7206812e266df8d57cd6628e22b9049b6517ebb8 (patch)
tree205b05c9b8def160bbe3a7fd07e363abb64195af
parentdfd11c94c1ad5261caa43128a39fcce550edf61d (diff)
downloadsimplechat-7206812e266df8d57cd6628e22b9049b6517ebb8.tar.gz
simplechat-7206812e266df8d57cd6628e22b9049b6517ebb8.tar.bz2
simplechat-7206812e266df8d57cd6628e22b9049b6517ebb8.zip
Forbid empty nicknames
-rw-r--r--chat/chat_test.go4
-rw-r--r--chat/rooms.go6
2 files changed, 10 insertions, 0 deletions
diff --git a/chat/chat_test.go b/chat/chat_test.go
index 03cc948..c92b8e7 100644
--- a/chat/chat_test.go
+++ b/chat/chat_test.go
@@ -18,6 +18,10 @@ func TestJoining(t *testing.T) {
t.Fatalf("Got error \"%s\", expected \"%s\"", err, NickAlreadyInUse)
}
+ if _, _, err = Join("test", ""); err != EmptyNick {
+ t.Fatalf("Got error \"%s\", expected \"%s\"", err, EmptyNick)
+ }
+
b2, r2, err := Join("test", "Bar")
if err != nil {
t.Fatalf("Could not join room a second time: %s", err)
diff --git a/chat/rooms.go b/chat/rooms.go
index 1991d77..793f027 100644
--- a/chat/rooms.go
+++ b/chat/rooms.go
@@ -7,6 +7,7 @@ import (
var (
NickAlreadyInUse = errors.New("Nickname is already in use")
RoomIsFull = errors.New("Room is full")
+ EmptyNick = errors.New("Nickname must not be empty")
)
// Room represents a chatroom.
@@ -31,6 +32,7 @@ func (r *Room) leave(nick string) {
}
delete(r.buddies, nick)
+
if len(r.buddies) == 0 {
close(r.messages)
delete(rooms, r.name)
@@ -81,6 +83,10 @@ func Join(room, nick string) (*Buddy, *Room, error) {
return nil, nil, NickAlreadyInUse
}
+ if nick == "" {
+ return nil, nil, EmptyNick
+ }
+
if len(r.buddies) >= perroom {
return nil, nil, RoomIsFull
}