1
0
mirror of https://github.com/shazow/ssh-chat.git synced 2025-04-23 12:10:30 +03:00

Implement private messages

This commit is contained in:
J. Stuart McMurray 2014-12-13 05:25:54 -05:00
parent 46d3d7575a
commit ee23c1617a
2 changed files with 28 additions and 0 deletions

@ -18,6 +18,7 @@ const HELP_TEXT string = `-> Available commands:
/list
/nick $NAME
/whois $NAME
/msg $NAME $MESSAGE
`
const ABOUT_TEXT string = `-> ssh-chat is made by @shazow.
@ -226,6 +227,20 @@ func (c *Client) handleShell(channel ssh.Channel) {
client.Write(fmt.Sprintf("-> Silenced for %s by %s.", duration, c.ColoredName()))
}
}
case "/msg": /* Send a PM */
/* Make sure we have a recipient and a message */
if len(parts) < 2 {
c.Msg <- fmt.Sprintf("-> Missing $NAME from: /msg $NAME $MESSAGE")
break
} else if len(parts) < 3 {
c.Msg <- fmt.Sprintf("-> Missing $MESSAGE from: /msg $NAME $MESSAGE")
break
}
/* Ask the server to send the message */
if err := c.Server.Privmsg(parts[1], parts[2], c); nil != err {
c.Msg <- fmt.Sprintf("Unable to send message to %v: %v", parts[1], err)
}
default:
c.Msg <- fmt.Sprintf("-> Invalid command: %s", line)
}

@ -93,6 +93,19 @@ func (s *Server) Broadcast(msg string, except *Client) {
}
}
/* Send a message to a particular nick, if it exists */
func (s *Server) Privmsg(nick, message string, sender *Client) error {
/* Get the recipient */
target, ok := s.clients[nick]
if !ok {
return fmt.Errorf("no client with that nick")
}
/* Send the message */
target.Msg <- fmt.Sprintf("\007[PM from %v] %v", sender.Name, message)
logger.Debugf("PM from %v to %v: %v", sender.Name, nick, message)
return nil
}
func (s *Server) Add(client *Client) {
go func() {
client.WriteLines(s.history.Get(10))