Skip to content

Commit 10cc44c

Browse files
committed
refactor: Privatize sshchat.GetPrompt and sshchat.Identity
1 parent 26676d0 commit 10cc44c

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

host.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818

1919
const maxInputLength int = 1024
2020

21-
// GetPrompt will render the terminal prompt string based on the user.
22-
func GetPrompt(user *message.User) string {
21+
// getPrompt will render the terminal prompt string based on the user.
22+
func getPrompt(user *message.User) string {
2323
name := user.Name()
2424
cfg := user.Config()
2525
if cfg.Theme != nil {
@@ -90,8 +90,8 @@ func (h *Host) isOp(conn sshd.Connection) bool {
9090

9191
// Connect a specific Terminal to this host and its room.
9292
func (h *Host) Connect(term *sshd.Terminal) {
93-
id := NewIdentity(term.Conn)
94-
user := message.NewUserScreen(id, term)
93+
ident := toIdentity(term.Conn)
94+
user := message.NewUserScreen(ident, term)
9595
cfg := user.Config()
9696
cfg.Theme = &h.theme
9797
user.SetConfig(cfg)
@@ -115,7 +115,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
115115
member, err := h.Join(user)
116116
if err != nil {
117117
// Try again...
118-
id.SetName(fmt.Sprintf("Guest%d", count))
118+
ident.SetName(fmt.Sprintf("Guest%d", count))
119119
member, err = h.Join(user)
120120
}
121121
if err != nil {
@@ -124,7 +124,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
124124
}
125125

126126
// Successfully joined.
127-
term.SetPrompt(GetPrompt(user))
127+
term.SetPrompt(getPrompt(user))
128128
term.AutoCompleteCallback = h.AutoCompleteFunction(user)
129129
user.SetHighlight(user.Name())
130130

@@ -172,7 +172,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
172172
//
173173
// FIXME: This is hacky, how do we improve the API to allow for
174174
// this? Chat module shouldn't know about terminals.
175-
term.SetPrompt(GetPrompt(user))
175+
term.SetPrompt(getPrompt(user))
176176
user.SetHighlight(user.Name())
177177
}
178178
}
@@ -344,7 +344,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
344344
return errors.New("user not found")
345345
}
346346

347-
id := target.Identifier.(*Identity)
347+
id := target.Identifier.(*identity)
348348
var whois string
349349
switch room.IsOp(msg.From()) {
350350
case true:
@@ -430,7 +430,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
430430
until, _ = time.ParseDuration(args[1])
431431
}
432432

433-
id := target.Identifier.(*Identity)
433+
id := target.Identifier.(*identity)
434434
h.auth.Ban(id.PublicKey(), until)
435435
h.auth.BanAddr(id.RemoteAddr(), until)
436436

@@ -500,7 +500,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
500500
}
501501
room.Ops.Add(set.Keyize(user.ID()))
502502

503-
h.auth.Op(user.Identifier.(*Identity).PublicKey(), until)
503+
h.auth.Op(user.Identifier.(*identity).PublicKey(), until)
504504

505505
body := fmt.Sprintf("Made op by %s.", msg.From().Name())
506506
room.Send(message.NewSystemMsg(body, user))

host_test.go

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

30-
u := message.NewUser(&Identity{id: "foo"})
30+
u := message.NewUser(&identity{id: "foo"})
3131

32-
actual = GetPrompt(u)
32+
actual = getPrompt(u)
3333
expected = "[foo] "
3434
if actual != expected {
3535
t.Errorf("Got: %q; Expected: %q", actual, expected)
@@ -38,7 +38,7 @@ func TestHostGetPrompt(t *testing.T) {
3838
u.SetConfig(message.UserConfig{
3939
Theme: &message.Themes[0],
4040
})
41-
actual = GetPrompt(u)
41+
actual = getPrompt(u)
4242
expected = "[\033[38;05;88mfoo\033[0m] "
4343
if actual != expected {
4444
t.Errorf("Got: %q; Expected: %q", actual, expected)

identity.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ import (
1111
)
1212

1313
// Identity is a container for everything that identifies a client.
14-
type Identity struct {
14+
type identity struct {
1515
sshd.Connection
1616
id string
1717
created time.Time
1818
}
1919

20-
// NewIdentity returns a new identity object from an sshd.Connection.
21-
func NewIdentity(conn sshd.Connection) *Identity {
22-
return &Identity{
20+
// Converts an sshd.Connection to an identity.
21+
func toIdentity(conn sshd.Connection) *identity {
22+
return &identity{
2323
Connection: conn,
2424
id: chat.SanitizeName(conn.Name()),
2525
created: time.Now(),
2626
}
2727
}
2828

29-
func (i Identity) ID() string {
29+
func (i identity) ID() string {
3030
return i.id
3131
}
3232

33-
func (i *Identity) SetName(name string) {
33+
func (i *identity) SetName(name string) {
3434
i.id = chat.SanitizeName(name)
3535
}
3636

37-
func (i Identity) Name() string {
37+
func (i identity) Name() string {
3838
return i.id
3939
}
4040

4141
// Whois returns a whois description for non-admin users.
42-
func (i Identity) Whois() string {
42+
func (i identity) Whois() string {
4343
fingerprint := "(no public key)"
4444
if i.PublicKey() != nil {
4545
fingerprint = sshd.Fingerprint(i.PublicKey())
@@ -51,7 +51,7 @@ func (i Identity) Whois() string {
5151
}
5252

5353
// WhoisAdmin returns a whois description for admin users.
54-
func (i Identity) WhoisAdmin() string {
54+
func (i identity) WhoisAdmin() string {
5555
ip, _, _ := net.SplitHostPort(i.RemoteAddr().String())
5656
fingerprint := "(no public key)"
5757
if i.PublicKey() != nil {

0 commit comments

Comments
 (0)