mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-07-02 03:34:29 +02:00
Redid all the newline mess, fixed renaming.
This commit is contained in:
parent
ed74e12b6d
commit
a27ced0c06
16
client.go
16
client.go
@ -52,12 +52,12 @@ func NewClient(server *Server, conn *ssh.ServerConn) *Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Write(msg string) {
|
func (c *Client) Write(msg string) {
|
||||||
c.term.Write([]byte(msg))
|
c.term.Write([]byte(msg + "\r\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) WriteLines(msg []string) {
|
func (c *Client) WriteLines(msg []string) {
|
||||||
for _, line := range msg {
|
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 {
|
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\r\n")
|
c.Msg <- fmt.Sprintf("-> Missing $NAME from: /nick $NAME")
|
||||||
}
|
}
|
||||||
case "/whois":
|
case "/whois":
|
||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
client := c.Server.Who(parts[1])
|
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 {
|
} else {
|
||||||
c.Msg <- fmt.Sprintf("-> Missing $NAME from: /whois $NAME\r\n")
|
c.Msg <- fmt.Sprintf("-> 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\r\n", len(names), strings.Join(names, ","))
|
c.Msg <- fmt.Sprintf("-> %d connected: %s", len(names), strings.Join(names, ","))
|
||||||
default:
|
default:
|
||||||
c.Msg <- fmt.Sprintf("-> Invalid command: %s\r\n", line)
|
c.Msg <- fmt.Sprintf("-> Invalid command: %s", line)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := fmt.Sprintf("%s: %s\r\n", c.Name, line)
|
msg := fmt.Sprintf("%s: %s", c.Name, line)
|
||||||
if c.IsSilenced() {
|
if c.IsSilenced() {
|
||||||
c.Msg <- fmt.Sprintf("-> Message rejected, silenced.")
|
c.Msg <- fmt.Sprintf("-> Message rejected, silenced.")
|
||||||
continue
|
continue
|
||||||
|
17
server.go
17
server.go
@ -62,7 +62,7 @@ func (s *Server) Len() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Broadcast(msg string, except *Client) {
|
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)
|
s.history.Add(msg)
|
||||||
|
|
||||||
for _, client := range s.clients {
|
for _, client := range s.clients {
|
||||||
@ -76,7 +76,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.\r\n"))
|
client.Write(fmt.Sprintf("-> Welcome to ssh-chat. Enter /help for more."))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
@ -84,15 +84,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.\r\n", client.Name, newName)
|
client.Msg <- fmt.Sprintf("-> Your name '%s' is not available, renamed to '%s'. Use /nick <name> to change it.", client.Name, newName)
|
||||||
}
|
}
|
||||||
|
|
||||||
client.Name = 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)\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) {
|
func (s *Server) Remove(client *Client) {
|
||||||
@ -100,7 +100,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.\r\n", client.Name), nil)
|
s.Broadcast(fmt.Sprintf("* %s left.", client.Name), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) proposeName(name string) (string, error) {
|
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)
|
newName, err := s.proposeName(newName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
client.Msg <- fmt.Sprintf("-> %s\r\n", err)
|
client.Msg <- fmt.Sprintf("-> %s", err)
|
||||||
s.lock.Unlock()
|
s.lock.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -139,7 +139,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.\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 {
|
func (s *Server) List(prefix *string) []string {
|
||||||
@ -195,6 +195,7 @@ func (s *Server) Start(laddr string) error {
|
|||||||
client := NewClient(s, sshConn)
|
client := NewClient(s, sshConn)
|
||||||
client.handleChannels(channels)
|
client.handleChannels(channels)
|
||||||
s.Add(client)
|
s.Add(client)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// Block until done, then remove.
|
// Block until done, then remove.
|
||||||
sshConn.Wait()
|
sshConn.Wait()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user