Skip to content

Commit 3ef4041

Browse files
committed
tests: All passing again
1 parent 0096404 commit 3ef4041

File tree

5 files changed

+43
-31
lines changed

5 files changed

+43
-31
lines changed

chat/message/screen.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ func BufferedScreen(name string, screen io.WriteCloser) *bufferedScreen {
2525

2626
func PipedScreen(name string, screen io.WriteCloser) *pipedScreen {
2727
return &pipedScreen{
28-
baseScreen: &baseScreen{
29-
User: NewUser(name),
30-
},
28+
baseScreen: Screen(name),
3129
WriteCloser: screen,
3230
}
3331
}
3432

3533
func HandledScreen(name string, handler func(Message) error) *handledScreen {
3634
return &handledScreen{
37-
baseScreen: &baseScreen{
38-
User: NewUser(name),
39-
},
40-
handler: handler,
35+
baseScreen: Screen(name),
36+
handler: handler,
37+
}
38+
}
39+
40+
func Screen(name string) *baseScreen {
41+
return &baseScreen{
42+
User: NewUser(name),
4143
}
4244
}
4345

client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ package sshchat
33
import (
44
"time"
55

6+
"github.com/shazow/ssh-chat/chat"
67
"github.com/shazow/ssh-chat/chat/message"
78
"github.com/shazow/ssh-chat/sshd"
89
)
910

1011
type Client struct {
11-
user *message.User
12+
user chat.Member
1213
conn sshd.Connection
1314

1415
timestamp time.Time
1516
}
17+
18+
type Replier interface {
19+
ReplyTo() message.Author
20+
SetReplyTo(message.Author)
21+
}

host.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Host struct {
3535
mu sync.Mutex
3636
motd string
3737
count int
38-
clients map[*message.User][]Client
38+
clients map[chat.Member][]Client
3939
}
4040

4141
// NewHost creates a Host on top of an existing listener.
@@ -46,7 +46,7 @@ func NewHost(listener *sshd.SSHListener, auth *Auth) *Host {
4646
listener: listener,
4747
commands: chat.Commands{},
4848
auth: auth,
49-
clients: map[*message.User][]Client{},
49+
clients: map[chat.Member][]Client{},
5050
}
5151

5252
// Make our own commands registry instance.
@@ -75,7 +75,7 @@ func (h *Host) SetMotd(motd string) {
7575
// Connect a specific Terminal to this host and its room.
7676
func (h *Host) Connect(term *sshd.Terminal) {
7777
requestedName := term.Conn.Name()
78-
user := message.NewScreen(requestedName, term)
78+
user := message.BufferedScreen(requestedName, term)
7979

8080
client := h.addClient(user, term.Conn)
8181
defer h.removeClient(user, client)
@@ -180,7 +180,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
180180
logger.Debugf("[%s] Leaving: %s", term.Conn.RemoteAddr(), user.Name())
181181
}
182182

183-
func (h *Host) addClient(user *message.User, conn sshd.Connection) *Client {
183+
func (h *Host) addClient(user chat.Member, conn sshd.Connection) *Client {
184184
client := Client{
185185
user: user,
186186
conn: conn,
@@ -195,7 +195,7 @@ func (h *Host) addClient(user *message.User, conn sshd.Connection) *Client {
195195
return &client
196196
}
197197

198-
func (h *Host) removeClient(user *message.User, client *Client) {
198+
func (h *Host) removeClient(user chat.Member, client *Client) {
199199
h.mu.Lock()
200200
defer h.mu.Unlock()
201201

@@ -212,7 +212,7 @@ func (h *Host) removeClient(user *message.User, client *Client) {
212212
}
213213
}
214214

215-
func (h *Host) findClients(user *message.User) []Client {
215+
func (h *Host) findClients(user chat.Member) []Client {
216216
h.mu.Lock()
217217
defer h.mu.Unlock()
218218
return h.clients[user]
@@ -244,7 +244,7 @@ func (h *Host) completeCommand(partial string) string {
244244
}
245245

246246
// AutoCompleteFunction returns a callback for terminal autocompletion
247-
func (h *Host) AutoCompleteFunction(u *message.User) func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
247+
func (h *Host) AutoCompleteFunction(u Replier) func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
248248
return func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
249249
if key != 9 {
250250
return
@@ -301,13 +301,12 @@ func (h *Host) AutoCompleteFunction(u *message.User) func(line string, pos int,
301301
}
302302

303303
// GetUser returns a message.User based on a name.
304-
func (h *Host) GetUser(name string) (*message.User, bool) {
304+
func (h *Host) GetUser(name string) (chat.Member, bool) {
305305
m, ok := h.MemberByID(name)
306306
if !ok {
307307
return nil, false
308308
}
309-
u, ok := m.Member.(*message.User)
310-
return u, ok
309+
return m.Member, true
311310
}
312311

313312
// InitCommands adds host-specific commands to a Commands container. These will
@@ -337,7 +336,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
337336
txt := fmt.Sprintf("[Sent PM to %s]", target.Name())
338337
ms := message.NewSystemMsg(txt, msg.From())
339338
room.Send(ms)
340-
target.SetReplyTo(msg.From())
339+
target.(Replier).SetReplyTo(msg.From())
341340
return nil
342341
},
343342
})
@@ -353,7 +352,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
353352
return errors.New("must specify message")
354353
}
355354

356-
target := msg.From().ReplyTo()
355+
target := msg.From().(Replier).ReplyTo()
357356
if target == nil {
358357
return errors.New("no message to reply to")
359358
}
@@ -392,7 +391,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
392391
// FIXME: Handle many clients
393392
clients := h.findClients(target)
394393
var whois string
395-
switch room.IsOp(msg.From()) {
394+
switch room.IsOp(msg.From().(chat.Member)) {
396395
case true:
397396
whois = whoisAdmin(clients)
398397
case false:
@@ -428,7 +427,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
428427
PrefixHelp: "USER [DURATION]",
429428
Help: "Set USER as admin.",
430429
Handler: func(room *chat.Room, msg message.CommandMsg) error {
431-
if !room.IsOp(msg.From()) {
430+
if !room.IsOp(msg.From().(chat.Member)) {
432431
return errors.New("must be op")
433432
}
434433

@@ -471,7 +470,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
471470
Help: "Ban USER from the server.",
472471
Handler: func(room *chat.Room, msg message.CommandMsg) error {
473472
// TODO: Would be nice to specify what to ban. Key? Ip? etc.
474-
if !room.IsOp(msg.From()) {
473+
if !room.IsOp(msg.From().(chat.Member)) {
475474
return errors.New("must be op")
476475
}
477476

@@ -498,7 +497,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
498497

499498
body := fmt.Sprintf("%s was banned by %s.", target.Name(), msg.From().Name())
500499
room.Send(message.NewAnnounceMsg(body))
501-
target.Close()
500+
target.(io.Closer).Close()
502501

503502
logger.Debugf("Banned: \n-> %s", whoisAdmin(clients))
504503

@@ -512,7 +511,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
512511
PrefixHelp: "USER",
513512
Help: "Kick USER from the server.",
514513
Handler: func(room *chat.Room, msg message.CommandMsg) error {
515-
if !room.IsOp(msg.From()) {
514+
if !room.IsOp(msg.From().(chat.Member)) {
516515
return errors.New("must be op")
517516
}
518517

@@ -528,7 +527,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
528527

529528
body := fmt.Sprintf("%s was kicked by %s.", target.Name(), msg.From().Name())
530529
room.Send(message.NewAnnounceMsg(body))
531-
target.Close()
530+
target.(io.Closer).Close()
532531
return nil
533532
},
534533
})
@@ -550,7 +549,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
550549
room.Send(message.NewSystemMsg(motd, user))
551550
return nil
552551
}
553-
if !room.IsOp(user) {
552+
if !room.IsOp(user.(chat.Member)) {
554553
return errors.New("must be OP to modify the MOTD")
555554
}
556555

host_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func stripPrompt(s string) string {
2828
func TestHostGetPrompt(t *testing.T) {
2929
var expected, actual string
3030

31-
u := message.NewUser("foo")
31+
u := message.Screen("foo")
3232

3333
actual = u.Prompt()
3434
expected = "[foo] "
@@ -40,7 +40,7 @@ func TestHostGetPrompt(t *testing.T) {
4040
Theme: &message.Themes[0],
4141
})
4242
actual = u.Prompt()
43-
expected = "[\033[38;05;88mfoo\033[0m] "
43+
expected = "[\033[38;05;1mfoo\033[0m] "
4444
if actual != expected {
4545
t.Errorf("Got: %q; Expected: %q", actual, expected)
4646
}

whois.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sshchat
22

33
import (
44
"net"
5+
"time"
56

67
humanize "github.com/dustin/go-humanize"
78
"github.com/shazow/ssh-chat/chat/message"
@@ -10,6 +11,10 @@ import (
1011

1112
// Helpers for printing whois messages
1213

14+
type joinTimestamped interface {
15+
Joined() time.Time
16+
}
17+
1318
func whoisPublic(clients []Client) string {
1419
// FIXME: Handle many clients
1520
conn, u := clients[0].conn, clients[0].user
@@ -21,7 +26,7 @@ func whoisPublic(clients []Client) string {
2126
return "name: " + u.Name() + message.Newline +
2227
" > fingerprint: " + fingerprint + message.Newline +
2328
" > client: " + SanitizeData(string(conn.ClientVersion())) + message.Newline +
24-
" > joined: " + humanize.Time(u.Joined())
29+
" > joined: " + humanize.Time(u.(joinTimestamped).Joined())
2530
}
2631

2732
func whoisAdmin(clients []Client) string {
@@ -37,5 +42,5 @@ func whoisAdmin(clients []Client) string {
3742
" > ip: " + ip + message.Newline +
3843
" > fingerprint: " + fingerprint + message.Newline +
3944
" > client: " + SanitizeData(string(conn.ClientVersion())) + message.Newline +
40-
" > joined: " + humanize.Time(u.Joined())
45+
" > joined: " + humanize.Time(u.(joinTimestamped).Joined())
4146
}

0 commit comments

Comments
 (0)