From a27ced0c0696d5e27ca44e29a95c366f7c02664b Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Fri, 12 Dec 2014 00:48:06 -0800 Subject: [PATCH] Redid all the newline mess, fixed renaming. --- client.go | 16 ++++++++-------- server.go | 17 +++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index 705efc6..caa1e81 100644 --- a/client.go +++ b/client.go @@ -52,12 +52,12 @@ func NewClient(server *Server, conn *ssh.ServerConn) *Client { } func (c *Client) Write(msg string) { - c.term.Write([]byte(msg)) + c.term.Write([]byte(msg + "\r\n")) } func (c *Client) WriteLines(msg []string) { for _, line := range msg { - c.Write(line + "\r\n") + c.Write(line) } } @@ -114,25 +114,25 @@ func (c *Client) handleShell(channel ssh.Channel) { if len(parts) == 2 { c.Server.Rename(c, parts[1]) } else { - c.Msg <- fmt.Sprintf("-> Missing $NAME from: /nick $NAME\r\n") + c.Msg <- fmt.Sprintf("-> Missing $NAME from: /nick $NAME") } case "/whois": if len(parts) == 2 { client := c.Server.Who(parts[1]) - c.Msg <- fmt.Sprintf("-> %s is %s via %s\r\n", client.Name, client.Conn.RemoteAddr(), client.Conn.ClientVersion()) + c.Msg <- fmt.Sprintf("-> %s is %s via %s", client.Name, client.Conn.RemoteAddr(), client.Conn.ClientVersion()) } else { - c.Msg <- fmt.Sprintf("-> Missing $NAME from: /whois $NAME\r\n") + c.Msg <- fmt.Sprintf("-> Missing $NAME from: /whois $NAME") } case "/list": names := c.Server.List(nil) - c.Msg <- fmt.Sprintf("-> %d connected: %s\r\n", len(names), strings.Join(names, ",")) + c.Msg <- fmt.Sprintf("-> %d connected: %s", len(names), strings.Join(names, ",")) default: - c.Msg <- fmt.Sprintf("-> Invalid command: %s\r\n", line) + c.Msg <- fmt.Sprintf("-> Invalid command: %s", line) } continue } - msg := fmt.Sprintf("%s: %s\r\n", c.Name, line) + msg := fmt.Sprintf("%s: %s", c.Name, line) if c.IsSilenced() { c.Msg <- fmt.Sprintf("-> Message rejected, silenced.") continue diff --git a/server.go b/server.go index 2b23b69..c7ba3b1 100644 --- a/server.go +++ b/server.go @@ -62,7 +62,7 @@ func (s *Server) Len() int { } func (s *Server) Broadcast(msg string, except *Client) { - logger.Debugf("Broadcast to %d: %s", s.Len(), strings.TrimRight(msg, "\r\n")) + logger.Debugf("Broadcast to %d: %s", s.Len(), msg) s.history.Add(msg) for _, client := range s.clients { @@ -76,7 +76,7 @@ func (s *Server) Broadcast(msg string, except *Client) { func (s *Server) Add(client *Client) { go func() { client.WriteLines(s.history.Get(10)) - client.Write(fmt.Sprintf("-> Welcome to ssh-chat. Enter /help for more.\r\n")) + client.Write(fmt.Sprintf("-> Welcome to ssh-chat. Enter /help for more.")) }() s.lock.Lock() @@ -84,15 +84,15 @@ 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 to change it.\r\n", client.Name, newName) + client.Msg <- fmt.Sprintf("-> Your name '%s' is not available, renamed to '%s'. Use /nick to change it.", client.Name, newName) } - client.Name = newName + client.Rename(newName) s.clients[client.Name] = client num := len(s.clients) s.lock.Unlock() - s.Broadcast(fmt.Sprintf("* %s joined. (Total connected: %d)\r\n", client.Name, num), nil) + s.Broadcast(fmt.Sprintf("* %s joined. (Total connected: %d)", client.Name, num), client) } func (s *Server) Remove(client *Client) { @@ -100,7 +100,7 @@ func (s *Server) Remove(client *Client) { delete(s.clients, client.Name) s.lock.Unlock() - s.Broadcast(fmt.Sprintf("* %s left.\r\n", client.Name), nil) + s.Broadcast(fmt.Sprintf("* %s left.", client.Name), nil) } func (s *Server) proposeName(name string) (string, error) { @@ -128,7 +128,7 @@ func (s *Server) Rename(client *Client, newName string) { newName, err := s.proposeName(newName) if err != nil { - client.Msg <- fmt.Sprintf("-> %s\r\n", err) + client.Msg <- fmt.Sprintf("-> %s", err) s.lock.Unlock() return } @@ -139,7 +139,7 @@ func (s *Server) Rename(client *Client, newName string) { s.clients[client.Name] = client s.lock.Unlock() - s.Broadcast(fmt.Sprintf("* %s is now known as %s.\r\n", oldName, newName), nil) + s.Broadcast(fmt.Sprintf("* %s is now known as %s.", oldName, newName), nil) } func (s *Server) List(prefix *string) []string { @@ -195,6 +195,7 @@ func (s *Server) Start(laddr string) error { client := NewClient(s, sshConn) client.handleChannels(channels) s.Add(client) + go func() { // Block until done, then remove. sshConn.Wait()