From b45efc0daef975b40270bf65f1b9e6ca730938bd Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Thu, 1 Sep 2016 16:42:12 -0400 Subject: [PATCH] refactor: Privatize sshchat.GetPrompt and sshchat.Identity --- host.go | 20 ++++++++++---------- host_test.go | 6 +++--- identity.go | 18 +++++++++--------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/host.go b/host.go index 42fb39e..028d757 100644 --- a/host.go +++ b/host.go @@ -18,8 +18,8 @@ import ( const maxInputLength int = 1024 -// GetPrompt will render the terminal prompt string based on the user. -func GetPrompt(user *message.User) string { +// getPrompt will render the terminal prompt string based on the user. +func getPrompt(user *message.User) string { name := user.Name() cfg := user.Config() if cfg.Theme != nil { @@ -90,8 +90,8 @@ func (h *Host) isOp(conn sshd.Connection) bool { // Connect a specific Terminal to this host and its room. func (h *Host) Connect(term *sshd.Terminal) { - id := NewIdentity(term.Conn) - user := message.NewUserScreen(id, term) + ident := toIdentity(term.Conn) + user := message.NewUserScreen(ident, term) cfg := user.Config() cfg.Theme = &h.theme user.SetConfig(cfg) @@ -115,7 +115,7 @@ func (h *Host) Connect(term *sshd.Terminal) { member, err := h.Join(user) if err != nil { // Try again... - id.SetName(fmt.Sprintf("Guest%d", count)) + ident.SetName(fmt.Sprintf("Guest%d", count)) member, err = h.Join(user) } if err != nil { @@ -124,7 +124,7 @@ func (h *Host) Connect(term *sshd.Terminal) { } // Successfully joined. - term.SetPrompt(GetPrompt(user)) + term.SetPrompt(getPrompt(user)) term.AutoCompleteCallback = h.AutoCompleteFunction(user) user.SetHighlight(user.Name()) @@ -172,7 +172,7 @@ func (h *Host) Connect(term *sshd.Terminal) { // // FIXME: This is hacky, how do we improve the API to allow for // this? Chat module shouldn't know about terminals. - term.SetPrompt(GetPrompt(user)) + term.SetPrompt(getPrompt(user)) user.SetHighlight(user.Name()) } } @@ -356,7 +356,7 @@ func (h *Host) InitCommands(c *chat.Commands) { return errors.New("user not found") } - id := target.Identifier.(*Identity) + id := target.Identifier.(*identity) var whois string switch room.IsOp(msg.From()) { case true: @@ -442,7 +442,7 @@ func (h *Host) InitCommands(c *chat.Commands) { until, _ = time.ParseDuration(args[1]) } - id := target.Identifier.(*Identity) + id := target.Identifier.(*identity) h.auth.Ban(id.PublicKey(), until) h.auth.BanAddr(id.RemoteAddr(), until) @@ -512,7 +512,7 @@ func (h *Host) InitCommands(c *chat.Commands) { } room.Ops.Add(set.Keyize(user.ID())) - h.auth.Op(user.Identifier.(*Identity).PublicKey(), until) + h.auth.Op(user.Identifier.(*identity).PublicKey(), until) body := fmt.Sprintf("Made op by %s.", msg.From().Name()) room.Send(message.NewSystemMsg(body, user)) diff --git a/host_test.go b/host_test.go index 7a42b8f..b0ec48a 100644 --- a/host_test.go +++ b/host_test.go @@ -27,9 +27,9 @@ func stripPrompt(s string) string { func TestHostGetPrompt(t *testing.T) { var expected, actual string - u := message.NewUser(&Identity{id: "foo"}) + u := message.NewUser(&identity{id: "foo"}) - actual = GetPrompt(u) + actual = getPrompt(u) expected = "[foo] " if actual != expected { t.Errorf("Got: %q; Expected: %q", actual, expected) @@ -38,7 +38,7 @@ func TestHostGetPrompt(t *testing.T) { u.SetConfig(message.UserConfig{ Theme: &message.Themes[0], }) - actual = GetPrompt(u) + actual = getPrompt(u) expected = "[\033[38;05;88mfoo\033[0m] " if actual != expected { t.Errorf("Got: %q; Expected: %q", actual, expected) diff --git a/identity.go b/identity.go index 6f38aa8..b33cb42 100644 --- a/identity.go +++ b/identity.go @@ -11,35 +11,35 @@ import ( ) // Identity is a container for everything that identifies a client. -type Identity struct { +type identity struct { sshd.Connection id string created time.Time } -// NewIdentity returns a new identity object from an sshd.Connection. -func NewIdentity(conn sshd.Connection) *Identity { - return &Identity{ +// Converts an sshd.Connection to an identity. +func toIdentity(conn sshd.Connection) *identity { + return &identity{ Connection: conn, id: chat.SanitizeName(conn.Name()), created: time.Now(), } } -func (i Identity) ID() string { +func (i identity) ID() string { return i.id } -func (i *Identity) SetName(name string) { +func (i *identity) SetName(name string) { i.id = chat.SanitizeName(name) } -func (i Identity) Name() string { +func (i identity) Name() string { return i.id } // Whois returns a whois description for non-admin users. -func (i Identity) Whois() string { +func (i identity) Whois() string { fingerprint := "(no public key)" if i.PublicKey() != nil { fingerprint = sshd.Fingerprint(i.PublicKey()) @@ -51,7 +51,7 @@ func (i Identity) Whois() string { } // WhoisAdmin returns a whois description for admin users. -func (i Identity) WhoisAdmin() string { +func (i identity) WhoisAdmin() string { ip, _, _ := net.SplitHostPort(i.RemoteAddr().String()) fingerprint := "(no public key)" if i.PublicKey() != nil {