Skip to content

Commit 279bcf3

Browse files
committed
tests: All passing again
1 parent a15c255 commit 279bcf3

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
@@ -295,13 +295,12 @@ func (h *Host) AutoCompleteFunction(u *message.User) func(line string, pos int,
295295
}
296296

297297
// GetUser returns a message.User based on a name.
298-
func (h *Host) GetUser(name string) (*message.User, bool) {
298+
func (h *Host) GetUser(name string) (chat.Member, bool) {
299299
m, ok := h.MemberByID(name)
300300
if !ok {
301301
return nil, false
302302
}
303-
u, ok := m.Member.(*message.User)
304-
return u, ok
303+
return m.Member, true
305304
}
306305

307306
// InitCommands adds host-specific commands to a Commands container. These will
@@ -331,7 +330,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
331330
txt := fmt.Sprintf("[Sent PM to %s]", target.Name())
332331
ms := message.NewSystemMsg(txt, msg.From())
333332
room.Send(ms)
334-
target.SetReplyTo(msg.From())
333+
target.(Replier).SetReplyTo(msg.From())
335334
return nil
336335
},
337336
})
@@ -347,7 +346,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
347346
return errors.New("must specify message")
348347
}
349348

350-
target := msg.From().ReplyTo()
349+
target := msg.From().(Replier).ReplyTo()
351350
if target == nil {
352351
return errors.New("no message to reply to")
353352
}
@@ -380,7 +379,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
380379
// FIXME: Handle many clients
381380
clients := h.findClients(target)
382381
var whois string
383-
switch room.IsOp(msg.From()) {
382+
switch room.IsOp(msg.From().(chat.Member)) {
384383
case true:
385384
whois = whoisAdmin(clients)
386385
case false:
@@ -416,7 +415,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
416415
PrefixHelp: "USER [DURATION]",
417416
Help: "Set USER as admin.",
418417
Handler: func(room *chat.Room, msg message.CommandMsg) error {
419-
if !room.IsOp(msg.From()) {
418+
if !room.IsOp(msg.From().(chat.Member)) {
420419
return errors.New("must be op")
421420
}
422421

@@ -459,7 +458,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
459458
Help: "Ban USER from the server.",
460459
Handler: func(room *chat.Room, msg message.CommandMsg) error {
461460
// TODO: Would be nice to specify what to ban. Key? Ip? etc.
462-
if !room.IsOp(msg.From()) {
461+
if !room.IsOp(msg.From().(chat.Member)) {
463462
return errors.New("must be op")
464463
}
465464

@@ -486,7 +485,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
486485

487486
body := fmt.Sprintf("%s was banned by %s.", target.Name(), msg.From().Name())
488487
room.Send(message.NewAnnounceMsg(body))
489-
target.Close()
488+
target.(io.Closer).Close()
490489

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

@@ -500,7 +499,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
500499
PrefixHelp: "USER",
501500
Help: "Kick USER from the server.",
502501
Handler: func(room *chat.Room, msg message.CommandMsg) error {
503-
if !room.IsOp(msg.From()) {
502+
if !room.IsOp(msg.From().(chat.Member)) {
504503
return errors.New("must be op")
505504
}
506505

@@ -516,7 +515,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
516515

517516
body := fmt.Sprintf("%s was kicked by %s.", target.Name(), msg.From().Name())
518517
room.Send(message.NewAnnounceMsg(body))
519-
target.Close()
518+
target.(io.Closer).Close()
520519
return nil
521520
},
522521
})
@@ -538,7 +537,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
538537
room.Send(message.NewSystemMsg(motd, user))
539538
return nil
540539
}
541-
if !room.IsOp(user) {
540+
if !room.IsOp(user.(chat.Member)) {
542541
return errors.New("must be OP to modify the MOTD")
543542
}
544543

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)