refactor: Privatize sshchat.GetPrompt and sshchat.Identity

This commit is contained in:
Andrey Petrov 2016-09-01 16:42:12 -04:00
parent a838ee2cad
commit b45efc0dae
3 changed files with 22 additions and 22 deletions

20
host.go
View File

@ -18,8 +18,8 @@ import (
const maxInputLength int = 1024 const maxInputLength int = 1024
// GetPrompt will render the terminal prompt string based on the user. // getPrompt will render the terminal prompt string based on the user.
func GetPrompt(user *message.User) string { func getPrompt(user *message.User) string {
name := user.Name() name := user.Name()
cfg := user.Config() cfg := user.Config()
if cfg.Theme != nil { 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. // Connect a specific Terminal to this host and its room.
func (h *Host) Connect(term *sshd.Terminal) { func (h *Host) Connect(term *sshd.Terminal) {
id := NewIdentity(term.Conn) ident := toIdentity(term.Conn)
user := message.NewUserScreen(id, term) user := message.NewUserScreen(ident, term)
cfg := user.Config() cfg := user.Config()
cfg.Theme = &h.theme cfg.Theme = &h.theme
user.SetConfig(cfg) user.SetConfig(cfg)
@ -115,7 +115,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
member, err := h.Join(user) member, err := h.Join(user)
if err != nil { if err != nil {
// Try again... // Try again...
id.SetName(fmt.Sprintf("Guest%d", count)) ident.SetName(fmt.Sprintf("Guest%d", count))
member, err = h.Join(user) member, err = h.Join(user)
} }
if err != nil { if err != nil {
@ -124,7 +124,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
} }
// Successfully joined. // Successfully joined.
term.SetPrompt(GetPrompt(user)) term.SetPrompt(getPrompt(user))
term.AutoCompleteCallback = h.AutoCompleteFunction(user) term.AutoCompleteCallback = h.AutoCompleteFunction(user)
user.SetHighlight(user.Name()) 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 // FIXME: This is hacky, how do we improve the API to allow for
// this? Chat module shouldn't know about terminals. // this? Chat module shouldn't know about terminals.
term.SetPrompt(GetPrompt(user)) term.SetPrompt(getPrompt(user))
user.SetHighlight(user.Name()) user.SetHighlight(user.Name())
} }
} }
@ -356,7 +356,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
return errors.New("user not found") return errors.New("user not found")
} }
id := target.Identifier.(*Identity) id := target.Identifier.(*identity)
var whois string var whois string
switch room.IsOp(msg.From()) { switch room.IsOp(msg.From()) {
case true: case true:
@ -442,7 +442,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
until, _ = time.ParseDuration(args[1]) until, _ = time.ParseDuration(args[1])
} }
id := target.Identifier.(*Identity) id := target.Identifier.(*identity)
h.auth.Ban(id.PublicKey(), until) h.auth.Ban(id.PublicKey(), until)
h.auth.BanAddr(id.RemoteAddr(), 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())) 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()) body := fmt.Sprintf("Made op by %s.", msg.From().Name())
room.Send(message.NewSystemMsg(body, user)) room.Send(message.NewSystemMsg(body, user))

View File

@ -27,9 +27,9 @@ func stripPrompt(s string) string {
func TestHostGetPrompt(t *testing.T) { func TestHostGetPrompt(t *testing.T) {
var expected, actual string var expected, actual string
u := message.NewUser(&Identity{id: "foo"}) u := message.NewUser(&identity{id: "foo"})
actual = GetPrompt(u) actual = getPrompt(u)
expected = "[foo] " expected = "[foo] "
if actual != expected { if actual != expected {
t.Errorf("Got: %q; Expected: %q", actual, expected) t.Errorf("Got: %q; Expected: %q", actual, expected)
@ -38,7 +38,7 @@ func TestHostGetPrompt(t *testing.T) {
u.SetConfig(message.UserConfig{ u.SetConfig(message.UserConfig{
Theme: &message.Themes[0], Theme: &message.Themes[0],
}) })
actual = GetPrompt(u) actual = getPrompt(u)
expected = "[\033[38;05;88mfoo\033[0m] " expected = "[\033[38;05;88mfoo\033[0m] "
if actual != expected { if actual != expected {
t.Errorf("Got: %q; Expected: %q", actual, expected) t.Errorf("Got: %q; Expected: %q", actual, expected)

View File

@ -11,35 +11,35 @@ import (
) )
// Identity is a container for everything that identifies a client. // Identity is a container for everything that identifies a client.
type Identity struct { type identity struct {
sshd.Connection sshd.Connection
id string id string
created time.Time created time.Time
} }
// NewIdentity returns a new identity object from an sshd.Connection. // Converts an sshd.Connection to an identity.
func NewIdentity(conn sshd.Connection) *Identity { func toIdentity(conn sshd.Connection) *identity {
return &Identity{ return &identity{
Connection: conn, Connection: conn,
id: chat.SanitizeName(conn.Name()), id: chat.SanitizeName(conn.Name()),
created: time.Now(), created: time.Now(),
} }
} }
func (i Identity) ID() string { func (i identity) ID() string {
return i.id return i.id
} }
func (i *Identity) SetName(name string) { func (i *identity) SetName(name string) {
i.id = chat.SanitizeName(name) i.id = chat.SanitizeName(name)
} }
func (i Identity) Name() string { func (i identity) Name() string {
return i.id return i.id
} }
// Whois returns a whois description for non-admin users. // Whois returns a whois description for non-admin users.
func (i Identity) Whois() string { func (i identity) Whois() string {
fingerprint := "(no public key)" fingerprint := "(no public key)"
if i.PublicKey() != nil { if i.PublicKey() != nil {
fingerprint = sshd.Fingerprint(i.PublicKey()) fingerprint = sshd.Fingerprint(i.PublicKey())
@ -51,7 +51,7 @@ func (i Identity) Whois() string {
} }
// WhoisAdmin returns a whois description for admin users. // WhoisAdmin returns a whois description for admin users.
func (i Identity) WhoisAdmin() string { func (i identity) WhoisAdmin() string {
ip, _, _ := net.SplitHostPort(i.RemoteAddr().String()) ip, _, _ := net.SplitHostPort(i.RemoteAddr().String())
fingerprint := "(no public key)" fingerprint := "(no public key)"
if i.PublicKey() != nil { if i.PublicKey() != nil {