diff --git a/.gitmodules b/.gitmodules index 3b9d069..37f75fa 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,3 +14,7 @@ path = vendor/github.com/shazow/rateio url = https://github.com/shazow/rateio branch = master +[submodule "vendor/github.com/dustin/go-humanize"] + path = vendor/github.com/dustin/go-humanize + url = https://github.com/dustin/go-humanize + branch = master diff --git a/host.go b/host.go index 41c2f91..21d799b 100644 --- a/host.go +++ b/host.go @@ -330,7 +330,14 @@ func (h *Host) InitCommands(c *chat.Commands) { } id := target.Identifier.(*Identity) - room.Send(message.NewSystemMsg(id.Whois(), msg.From())) + var whois string + switch room.IsOp(msg.From()) { + case true: + whois = id.WhoisAdmin() + case false: + whois = id.Whois() + } + room.Send(message.NewSystemMsg(whois, msg.From())) return nil }, diff --git a/host_test.go b/host_test.go index d29a875..22a75e7 100644 --- a/host_test.go +++ b/host_test.go @@ -26,7 +26,7 @@ func stripPrompt(s string) string { func TestHostGetPrompt(t *testing.T) { var expected, actual string - u := message.NewUser(&Identity{nil, "foo"}) + u := message.NewUser(&Identity{id: "foo"}) u.SetColorIdx(2) actual = GetPrompt(u) diff --git a/identity.go b/identity.go index b4de872..cff8924 100644 --- a/identity.go +++ b/identity.go @@ -1,9 +1,10 @@ package sshchat import ( - "fmt" "net" + "time" + "github.com/dustin/go-humanize" "github.com/shazow/ssh-chat/chat" "github.com/shazow/ssh-chat/chat/message" "github.com/shazow/ssh-chat/sshd" @@ -12,7 +13,8 @@ import ( // Identity is a container for everything that identifies a client. type Identity struct { sshd.Connection - id string + id string + created time.Time } // NewIdentity returns a new identity object from an sshd.Connection. @@ -20,6 +22,7 @@ func NewIdentity(conn sshd.Connection) *Identity { return &Identity{ Connection: conn, id: chat.SanitizeName(conn.Name()), + created: time.Now(), } } @@ -39,14 +42,28 @@ func (i Identity) Name() string { return i.id } +// Whois returns a whois description for non-admin users. func (i Identity) Whois() string { + fingerprint := "(no public key)" + if i.PublicKey() != nil { + fingerprint = sshd.Fingerprint(i.PublicKey()) + } + return "name: " + i.Name() + message.Newline + + " > fingerprint: " + fingerprint + message.Newline + + " > client: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline + + " > joined: " + humanize.Time(i.created) +} + +// WhoisAdmin returns a whois description for admin users. +func (i Identity) WhoisAdmin() string { ip, _, _ := net.SplitHostPort(i.RemoteAddr().String()) fingerprint := "(no public key)" if i.PublicKey() != nil { fingerprint = sshd.Fingerprint(i.PublicKey()) } - // TODO: Include time joined, client, etc. - return fmt.Sprintf("name: %s"+message.Newline+ - " > ip: %s"+message.Newline+ - " > fingerprint: %s", i.Name(), ip, fingerprint) + return "name: " + i.Name() + message.Newline + + " > ip: " + ip + message.Newline + + " > fingerprint: " + fingerprint + message.Newline + + " > client: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline + + " > joined: " + humanize.Time(i.created) } diff --git a/sshd/terminal.go b/sshd/terminal.go index 8d4b725..23f066b 100644 --- a/sshd/terminal.go +++ b/sshd/terminal.go @@ -22,6 +22,7 @@ type Connection interface { PublicKey() ssh.PublicKey RemoteAddr() net.Addr Name() string + ClientVersion() []byte Close() error } diff --git a/vendor/github.com/dustin/go-humanize b/vendor/github.com/dustin/go-humanize new file mode 160000 index 0000000..2fcb520 --- /dev/null +++ b/vendor/github.com/dustin/go-humanize @@ -0,0 +1 @@ +Subproject commit 2fcb5204cdc65b4bec9fd0a87606bb0d0e3c54e8