Use a greyish color for system messages

This color is easily adjusted
This commit is contained in:
Andreas Renberg (IQAndreas) 2014-12-13 03:08:33 -06:00
parent 857dcd0a14
commit ffe23fa91f
2 changed files with 46 additions and 31 deletions

View File

@ -11,16 +11,18 @@ import (
const MSG_BUFFER int = 10 const MSG_BUFFER int = 10
const HELP_TEXT string = `-> Available commands: const HELP_TEXT string = SYSTEM_MESSAGE_FORMAT + `
-> Available commands:
/about /about
/exit /exit
/help /help
/list /list
/nick $NAME /nick $NAME
/whois $NAME /whois $NAME
` ` + RESET
const ABOUT_TEXT string = `-> ssh-chat is made by @shazow. const ABOUT_TEXT string = SYSTEM_MESSAGE_FORMAT + `
-> ssh-chat is made by @shazow.
It is a custom ssh server built in Go to serve a chat experience It is a custom ssh server built in Go to serve a chat experience
instead of a shell. instead of a shell.
@ -28,7 +30,7 @@ const ABOUT_TEXT string = `-> ssh-chat is made by @shazow.
Source: https://github.com/shazow/ssh-chat Source: https://github.com/shazow/ssh-chat
For more, visit shazow.net or follow at twitter.com/shazow For more, visit shazow.net or follow at twitter.com/shazow
` ` + RESET
type Client struct { type Client struct {
Server *Server Server *Server
@ -59,6 +61,14 @@ func (c *Client) ColoredName() string {
return ColorString(c.Color, c.Name) return ColorString(c.Color, c.Name)
} }
func (c *Client) SysMsg(msg string, args ...interface{}) {
c.Msg <- SYSTEM_MESSAGE_FORMAT + "-> " + fmt.Sprintf(msg, args...) + RESET
}
func (c *Client) SysMsg2(msg string, args ...interface{}) {
c.Write(SYSTEM_MESSAGE_FORMAT + "-> " + fmt.Sprintf(msg, args...) + RESET)
}
func (c *Client) Write(msg string) { func (c *Client) Write(msg string) {
c.term.Write([]byte(msg + "\r\n")) c.term.Write([]byte(msg + "\r\n"))
} }
@ -141,7 +151,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
} }
msg := fmt.Sprintf("** %s%s", c.ColoredName(), me) msg := fmt.Sprintf("** %s%s", c.ColoredName(), me)
if c.IsSilenced() || len(msg) > 1000 { if c.IsSilenced() || len(msg) > 1000 {
c.Msg <- fmt.Sprintf("-> Message rejected.") c.SysMsg("Message rejected.")
} else { } else {
c.Server.Broadcast(msg, nil) c.Server.Broadcast(msg, nil)
} }
@ -149,7 +159,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
if len(parts) == 2 { if len(parts) == 2 {
c.Server.Rename(c, parts[1]) c.Server.Rename(c, parts[1])
} else { } else {
c.Msg <- fmt.Sprintf("-> Missing $NAME from: /nick $NAME") c.SysMsg("Missing $NAME from: /nick $NAME")
} }
case "/whois": case "/whois":
if len(parts) == 2 { if len(parts) == 2 {
@ -159,28 +169,28 @@ func (c *Client) handleShell(channel ssh.Channel) {
if len(version) > 100 { if len(version) > 100 {
version = "Evil Jerk with a superlong string" version = "Evil Jerk with a superlong string"
} }
c.Msg <- fmt.Sprintf("-> %s is %s via %s", client.ColoredName(), client.Fingerprint(), version) c.SysMsg("%s is %s via %s", client.ColoredName(), client.Fingerprint(), version)
} else { } else {
c.Msg <- fmt.Sprintf("-> No such name: %s", parts[1]) c.SysMsg("No such name: %s", parts[1])
} }
} else { } else {
c.Msg <- fmt.Sprintf("-> Missing $NAME from: /whois $NAME") c.SysMsg("Missing $NAME from: /whois $NAME")
} }
case "/list": case "/list":
names := c.Server.List(nil) names := c.Server.List(nil)
c.Msg <- fmt.Sprintf("-> %d connected: %s", len(names), strings.Join(names, ", ")) c.SysMsg("%d connected: %s", len(names), strings.Join(names, ", "))
case "/ban": case "/ban":
if !c.Server.IsOp(c) { if !c.Server.IsOp(c) {
c.Msg <- fmt.Sprintf("-> You're not an admin.") c.SysMsg("You're not an admin.")
} else if len(parts) != 2 { } else if len(parts) != 2 {
c.Msg <- fmt.Sprintf("-> Missing $NAME from: /ban $NAME") c.SysMsg("Missing $NAME from: /ban $NAME")
} else { } else {
client := c.Server.Who(parts[1]) client := c.Server.Who(parts[1])
if client == nil { if client == nil {
c.Msg <- fmt.Sprintf("-> No such name: %s", parts[1]) c.SysMsg("No such name: %s", parts[1])
} else { } else {
fingerprint := client.Fingerprint() fingerprint := client.Fingerprint()
client.Write(fmt.Sprintf("-> Banned by %s.", c.ColoredName())) client.SysMsg2("Banned by %s.", c.ColoredName())
c.Server.Ban(fingerprint, nil) c.Server.Ban(fingerprint, nil)
client.Conn.Close() client.Conn.Close()
c.Server.Broadcast(fmt.Sprintf("* %s was banned by %s", parts[1], c.ColoredName()), nil) c.Server.Broadcast(fmt.Sprintf("* %s was banned by %s", parts[1], c.ColoredName()), nil)
@ -188,24 +198,24 @@ func (c *Client) handleShell(channel ssh.Channel) {
} }
case "/op": case "/op":
if !c.Server.IsOp(c) { if !c.Server.IsOp(c) {
c.Msg <- fmt.Sprintf("-> You're not an admin.") c.SysMsg("You're not an admin.")
} else if len(parts) != 2 { } else if len(parts) != 2 {
c.Msg <- fmt.Sprintf("-> Missing $NAME from: /op $NAME") c.SysMsg("Missing $NAME from: /op $NAME")
} else { } else {
client := c.Server.Who(parts[1]) client := c.Server.Who(parts[1])
if client == nil { if client == nil {
c.Msg <- fmt.Sprintf("-> No such name: %s", parts[1]) c.SysMsg("No such name: %s", parts[1])
} else { } else {
fingerprint := client.Fingerprint() fingerprint := client.Fingerprint()
client.Write(fmt.Sprintf("-> Made op by %s.", c.ColoredName())) client.SysMsg2("Made op by %s.", c.ColoredName())
c.Server.Op(fingerprint) c.Server.Op(fingerprint)
} }
} }
case "/silence": case "/silence":
if !c.Server.IsOp(c) { if !c.Server.IsOp(c) {
c.Msg <- fmt.Sprintf("-> You're not an admin.") c.SysMsg("You're not an admin.")
} else if len(parts) < 2 { } else if len(parts) < 2 {
c.Msg <- fmt.Sprintf("-> Missing $NAME from: /silence $NAME") c.SysMsg("Missing $NAME from: /silence $NAME")
} else { } else {
duration := time.Duration(5) * time.Minute duration := time.Duration(5) * time.Minute
if len(parts) >= 3 { if len(parts) >= 3 {
@ -216,21 +226,21 @@ func (c *Client) handleShell(channel ssh.Channel) {
} }
client := c.Server.Who(parts[1]) client := c.Server.Who(parts[1])
if client == nil { if client == nil {
c.Msg <- fmt.Sprintf("-> No such name: %s", parts[1]) c.SysMsg("No such name: %s", parts[1])
} else { } else {
client.Silence(duration) client.Silence(duration)
client.Write(fmt.Sprintf("-> Silenced for %s by %s.", duration, c.ColoredName())) client.SysMsg2("Silenced for %s by %s.", duration, c.ColoredName())
} }
} }
default: default:
c.Msg <- fmt.Sprintf("-> Invalid command: %s", line) c.SysMsg("Invalid command: %s", line)
} }
continue continue
} }
msg := fmt.Sprintf("%s: %s", c.ColoredName(), line) msg := fmt.Sprintf("%s: %s", c.ColoredName(), line)
if c.IsSilenced() || len(msg) > 1000 { if c.IsSilenced() || len(msg) > 1000 {
c.Msg <- fmt.Sprintf("-> Message rejected.") c.SysMsg("Message rejected.")
continue continue
} }
c.Server.Broadcast(msg, c) c.Server.Broadcast(msg, c)

View File

@ -76,6 +76,11 @@ func (s *Server) Len() int {
return len(s.clients) return len(s.clients)
} }
const SYSTEM_MESSAGE_FORMAT string = "\033[1;3;90m"
func (s *Server) SysMsg(msg string, args ...interface{}) {
s.Broadcast(SYSTEM_MESSAGE_FORMAT + " * " + fmt.Sprintf(msg, args...) + RESET, nil)
}
func (s *Server) Broadcast(msg string, except *Client) { func (s *Server) Broadcast(msg string, except *Client) {
logger.Debugf("Broadcast to %d: %s", s.Len(), msg) logger.Debugf("Broadcast to %d: %s", s.Len(), msg)
s.history.Add(msg) s.history.Add(msg)
@ -91,7 +96,7 @@ func (s *Server) Broadcast(msg string, except *Client) {
func (s *Server) Add(client *Client) { func (s *Server) Add(client *Client) {
go func() { go func() {
client.WriteLines(s.history.Get(10)) client.WriteLines(s.history.Get(10))
client.Write(fmt.Sprintf("-> Welcome to ssh-chat. Enter /help for more.")) client.SysMsg2("Welcome to ssh-chat. Enter /help for more.")
}() }()
s.lock.Lock() s.lock.Lock()
@ -99,15 +104,15 @@ func (s *Server) Add(client *Client) {
newName, err := s.proposeName(client.Name) newName, err := s.proposeName(client.Name)
if err != nil { if err != nil {
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.SysMsg("Your name '%s' is not available, renamed to '%s'. Use /nick <name> to change it.", client.ColoredName(), ColorString(client.Color, newName))
} }
client.Rename(newName) client.Rename(newName)
s.clients[client.Name] = client s.clients[client.Name] = client
num := len(s.clients) num := len(s.clients)
s.lock.Unlock() s.lock.Unlock()
s.Broadcast(fmt.Sprintf("* %s joined. (Total connected: %d)", client.ColoredName(), num), client) s.Broadcast(fmt.Sprintf("%s * %s joined. (Total connected: %d)%s", SYSTEM_MESSAGE_FORMAT, client.ColoredName(), num, RESET), client)
} }
func (s *Server) Remove(client *Client) { func (s *Server) Remove(client *Client) {
@ -115,7 +120,7 @@ func (s *Server) Remove(client *Client) {
delete(s.clients, client.Name) delete(s.clients, client.Name)
s.lock.Unlock() s.lock.Unlock()
s.Broadcast(fmt.Sprintf("* %s left.", client.ColoredName()), nil) s.SysMsg("%s left.", client.ColoredName())
} }
func (s *Server) proposeName(name string) (string, error) { func (s *Server) proposeName(name string) (string, error) {
@ -143,7 +148,7 @@ func (s *Server) Rename(client *Client, newName string) {
newName, err := s.proposeName(newName) newName, err := s.proposeName(newName)
if err != nil { if err != nil {
client.Msg <- fmt.Sprintf("-> %s", err) client.SysMsg("%s", err)
s.lock.Unlock() s.lock.Unlock()
return return
} }
@ -155,7 +160,7 @@ func (s *Server) Rename(client *Client, newName string) {
s.clients[client.Name] = client s.clients[client.Name] = client
s.lock.Unlock() s.lock.Unlock()
s.Broadcast(fmt.Sprintf("* %s is now known as %s.", oldName, newName), nil) s.SysMsg("%s is now known as %s.", oldName, newName)
} }
func (s *Server) List(prefix *string) []string { func (s *Server) List(prefix *string) []string {