mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-14 16:17:17 +03:00
refactor: Privatize sshchat.GetPrompt and sshchat.Identity
This commit is contained in:
parent
a838ee2cad
commit
b45efc0dae
20
host.go
20
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))
|
||||
|
@ -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)
|
||||
|
18
identity.go
18
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user