summaryrefslogtreecommitdiff
path: root/chat
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2014-03-27 14:58:45 +0100
committerKevin Chabowski <kevin@kch42.de>2014-03-27 23:51:07 +0100
commitaddb75b981a2044a47adb7ff26850d6ff12b6144 (patch)
tree33d9653f6e206bd688484a00dcb6734520f3280b /chat
parent85371239a7e86d6e5f5dd13196abf0e923b49b4e (diff)
downloadsimplechat-addb75b981a2044a47adb7ff26850d6ff12b6144.tar.gz
simplechat-addb75b981a2044a47adb7ff26850d6ff12b6144.tar.bz2
simplechat-addb75b981a2044a47adb7ff26850d6ff12b6144.zip
Nickname and message length limits implemented
Diffstat (limited to 'chat')
-rw-r--r--chat/chat_test.go4
-rw-r--r--chat/rooms.go16
2 files changed, 20 insertions, 0 deletions
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
}