mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-03 00:51:20 +03:00
Make the user's name colored in all messages
This commit is contained in:
parent
1b73035495
commit
d22e80d2c1
22
client.go
22
client.go
@ -55,6 +55,10 @@ func NewClient(server *Server, conn *ssh.ServerConn) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) ColoredName() string {
|
||||
return ColorString(c.Color, c.Name)
|
||||
}
|
||||
|
||||
func (c *Client) Write(msg string) {
|
||||
c.term.Write([]byte(msg + "\r\n"))
|
||||
}
|
||||
@ -85,7 +89,7 @@ func (c *Client) Resize(width int, height int) error {
|
||||
|
||||
func (c *Client) Rename(name string) {
|
||||
c.Name = name
|
||||
c.term.SetPrompt(fmt.Sprintf("[%s] ", ColorString(c.Color, c.Name)))
|
||||
c.term.SetPrompt(fmt.Sprintf("[%s] ", c.ColoredName()))
|
||||
}
|
||||
|
||||
func (c *Client) Fingerprint() string {
|
||||
@ -135,7 +139,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
||||
if me == "" {
|
||||
me = " is at a loss for words."
|
||||
}
|
||||
msg := fmt.Sprintf("** %s%s", c.Name, me)
|
||||
msg := fmt.Sprintf("** %s%s", c.ColoredName(), me)
|
||||
if c.IsSilenced() || len(msg) > 1000 {
|
||||
c.Msg <- fmt.Sprintf("-> Message rejected.")
|
||||
} else {
|
||||
@ -155,7 +159,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
||||
if len(version) > 100 {
|
||||
version = "Evil Jerk with a superlong string"
|
||||
}
|
||||
c.Msg <- fmt.Sprintf("-> %s is %s via %s", client.Name, client.Fingerprint(), version)
|
||||
c.Msg <- fmt.Sprintf("-> %s is %s via %s", client.ColoredName(), client.Fingerprint(), version)
|
||||
} else {
|
||||
c.Msg <- fmt.Sprintf("-> No such name: %s", parts[1])
|
||||
}
|
||||
@ -176,10 +180,10 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
||||
c.Msg <- fmt.Sprintf("-> No such name: %s", parts[1])
|
||||
} else {
|
||||
fingerprint := client.Fingerprint()
|
||||
client.Write(fmt.Sprintf("-> Banned by %s.", c.Name))
|
||||
client.Write(fmt.Sprintf("-> Banned by %s.", c.ColoredName()))
|
||||
c.Server.Ban(fingerprint, nil)
|
||||
client.Conn.Close()
|
||||
c.Server.Broadcast(fmt.Sprintf("* %s was banned by %s", parts[1], c.Name), nil)
|
||||
c.Server.Broadcast(fmt.Sprintf("* %s was banned by %s", parts[1], c.ColoredName()), nil)
|
||||
}
|
||||
}
|
||||
case "/op":
|
||||
@ -193,7 +197,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
||||
c.Msg <- fmt.Sprintf("-> No such name: %s", parts[1])
|
||||
} else {
|
||||
fingerprint := client.Fingerprint()
|
||||
client.Write(fmt.Sprintf("-> Made op by %s.", c.Name))
|
||||
client.Write(fmt.Sprintf("-> Made op by %s.", c.ColoredName()))
|
||||
c.Server.Op(fingerprint)
|
||||
}
|
||||
}
|
||||
@ -215,7 +219,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
||||
c.Msg <- fmt.Sprintf("-> No such name: %s", parts[1])
|
||||
} else {
|
||||
client.Silence(duration)
|
||||
client.Write(fmt.Sprintf("-> Silenced for %s by %s.", duration, c.Name))
|
||||
client.Write(fmt.Sprintf("-> Silenced for %s by %s.", duration, c.ColoredName()))
|
||||
}
|
||||
}
|
||||
default:
|
||||
@ -224,7 +228,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
||||
continue
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("%s: %s", ColorString(c.Color, c.Name), line)
|
||||
msg := fmt.Sprintf("%s: %s", c.ColoredName(), line)
|
||||
if c.IsSilenced() || len(msg) > 1000 {
|
||||
c.Msg <- fmt.Sprintf("-> Message rejected.")
|
||||
continue
|
||||
@ -235,7 +239,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
||||
}
|
||||
|
||||
func (c *Client) handleChannels(channels <-chan ssh.NewChannel) {
|
||||
prompt := fmt.Sprintf("[%s] ", ColorString(c.Color, c.Name))
|
||||
prompt := fmt.Sprintf("[%s] ", c.ColoredName())
|
||||
|
||||
hasShell := false
|
||||
|
||||
|
@ -91,7 +91,7 @@ func (s *Server) Add(client *Client) {
|
||||
|
||||
newName, err := s.proposeName(client.Name)
|
||||
if err != nil {
|
||||
client.Msg <- fmt.Sprintf("-> Your name '%s' is not available, renamed to '%s'. Use /nick <name> to change it.", client.Name, newName)
|
||||
client.Msg <- fmt.Sprintf("-> Your name '%s' is not available, renamed to '%s'. Use /nick <name> to change it.", client.ColoredName(), ColorString(client.Color, newName))
|
||||
}
|
||||
|
||||
client.Rename(newName)
|
||||
@ -99,7 +99,7 @@ func (s *Server) Add(client *Client) {
|
||||
num := len(s.clients)
|
||||
s.lock.Unlock()
|
||||
|
||||
s.Broadcast(fmt.Sprintf("* %s joined. (Total connected: %d)", client.Name, num), client)
|
||||
s.Broadcast(fmt.Sprintf("* %s joined. (Total connected: %d)", client.ColoredName(), num), client)
|
||||
}
|
||||
|
||||
func (s *Server) Remove(client *Client) {
|
||||
@ -107,7 +107,7 @@ func (s *Server) Remove(client *Client) {
|
||||
delete(s.clients, client.Name)
|
||||
s.lock.Unlock()
|
||||
|
||||
s.Broadcast(fmt.Sprintf("* %s left.", client.Name), nil)
|
||||
s.Broadcast(fmt.Sprintf("* %s left.", client.ColoredName()), nil)
|
||||
}
|
||||
|
||||
func (s *Server) proposeName(name string) (string, error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user