diff --git a/chat/channel.go b/chat/channel.go index d6b1393..f26dbf2 100644 --- a/chat/channel.go +++ b/chat/channel.go @@ -30,19 +30,21 @@ func (ch *Channel) Send(m Message) { func (ch *Channel) Join(u *User) error { err := ch.users.Add(u) if err != nil { - s := fmt.Sprintf("%s joined. (Connected: %d)", u.Name(), ch.users.Len()) - ch.Send(*NewMessage(s)) + return err } - return err + s := fmt.Sprintf("%s joined. (Connected: %d)", u.Name(), ch.users.Len()) + ch.Send(*NewMessage(s)) + return nil } func (ch *Channel) Leave(u *User) error { err := ch.users.Remove(u) if err != nil { - s := fmt.Sprintf("%s left.", u.Name()) - ch.Send(*NewMessage(s)) + return err } - return err + s := fmt.Sprintf("%s left.", u.Name()) + ch.Send(*NewMessage(s)) + return nil } func (ch *Channel) Topic() string { diff --git a/chat/channel_test.go b/chat/channel_test.go index 53debc9..b52577c 100644 --- a/chat/channel_test.go +++ b/chat/channel_test.go @@ -8,20 +8,24 @@ import ( func TestChannel(t *testing.T) { s := &MockScreen{} out := make(chan Message) - defer close(out) go func() { for msg := range out { + t.Logf("Broadcasted: %s", msg.String()) s.Write([]byte(msg.Render(nil))) } }() u := NewUser("foo", s) ch := NewChannel("", out) - ch.Join(u) + err := ch.Join(u) + + if err != nil { + t.Error(err) + } expected := []byte(" * foo joined. (Connected: 1)") if !reflect.DeepEqual(s.received, expected) { - t.Errorf("Got: %s, Expected: %s", s.received, expected) + t.Errorf("Got: `%s`, Expected: `%s`", s.received, expected) } } diff --git a/chat/doc.go b/chat/doc.go new file mode 100644 index 0000000..7c80e02 --- /dev/null +++ b/chat/doc.go @@ -0,0 +1,13 @@ +/* +`chat` package is a server-agnostic implementation of a chat interface, built +with the intention of using with the intention of using as the backend for +ssh-chat. + +This package should not know anything about sockets. It should expose io-style +interfaces and channels for communicating with any method of transnport. + +TODO: Add usage examples here. + +*/ + +package chat diff --git a/chat/logger.go b/chat/logger.go new file mode 100644 index 0000000..93b2761 --- /dev/null +++ b/chat/logger.go @@ -0,0 +1,22 @@ +package chat + +import "io" +import stdlog "log" + +var logger *stdlog.Logger + +func SetLogger(w io.Writer) { + flags := stdlog.Flags() + prefix := "[chat] " + logger = stdlog.New(w, prefix, flags) +} + +type nullWriter struct{} + +func (nullWriter) Write(data []byte) (int, error) { + return len(data), nil +} + +func init() { + SetLogger(nullWriter{}) +}