From addb75b981a2044a47adb7ff26850d6ff12b6144 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Thu, 27 Mar 2014 14:58:45 +0100 Subject: Nickname and message length limits implemented --- chat/chat_test.go | 4 ++++ chat/rooms.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) (limited to 'chat') diff --git a/chat/chat_test.go b/chat/chat_test.go index c92b8e7..65a50dd 100644 --- a/chat/chat_test.go +++ b/chat/chat_test.go @@ -22,6 +22,10 @@ func TestJoining(t *testing.T) { t.Fatalf("Got error \"%s\", expected \"%s\"", err, EmptyNick) } + if _, _, err = Join("test", "abcdefghijklmnopqrstuvwxyz"); err != NickTooLong { + t.Fatalf("Got error \"%s\", expected \"%s\"", err, NickTooLong) + } + 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 793f027..849aeb8 100644 --- a/chat/rooms.go +++ b/chat/rooms.go @@ -8,6 +8,12 @@ var ( NickAlreadyInUse = errors.New("Nickname is already in use") RoomIsFull = errors.New("Room is full") EmptyNick = errors.New("Nickname must not be empty") + NickTooLong = errors.New("NIckname is too long") +) + +const ( + nickLenLimit = 16 + msgLenLimit = 2000 ) // Room represents a chatroom. @@ -46,6 +52,12 @@ func (r *Room) leave(nick string) { func (r *Room) broadcast() { for m := range r.messages { + if len(m.Text) > msgLenLimit { + text := []rune(m.Text) + text = text[:msgLenLimit] + m.Text = string(text) + " (message truncated. was too long)" + } + for _, buddy := range r.buddies { buddy.Push(m) } @@ -87,6 +99,10 @@ func Join(room, nick string) (*Buddy, *Room, error) { return nil, nil, EmptyNick } + if len(nick) > nickLenLimit { + return nil, nil, NickTooLong + } + if len(r.buddies) >= perroom { return nil, nil, RoomIsFull } -- cgit v1.2.3-70-g09d2