diff --git a/client.go b/client.go index 3941716..b433262 100644 --- a/client.go +++ b/client.go @@ -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) } diff --git a/server.go b/server.go index ce4d4dc..6343148 100644 --- a/server.go +++ b/server.go @@ -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))