/motd
authorAndrey Petrov <andrey.petrov@shazow.net>
Mon, 19 Jan 2015 03:55:01 +0000 (19:55 -0800)
committerAndrey Petrov <andrey.petrov@shazow.net>
Mon, 19 Jan 2015 03:55:01 +0000 (19:55 -0800)
cmd.go
host.go

diff --git a/cmd.go b/cmd.go
index a763885a649646f8c7c0d75e0a351b761994d5f9..721c597c66b98eaedb1c1627a95ffd0380b593ee 100644 (file)
--- a/cmd.go
+++ b/cmd.go
@@ -144,7 +144,7 @@ func main() {
                        logger.Errorf("Failed to load MOTD file: %v", err)
                        return
                }
-               motdString := string(motd[:])
+               motdString := strings.TrimSpace(string(motd))
                // hack to normalize line endings into \r\n
                motdString = strings.Replace(motdString, "\r\n", "\n", -1)
                motdString = strings.Replace(motdString, "\n", "\r\n", -1)
diff --git a/host.go b/host.go
index 5c8547039636971351f2b4d518f0983b5991760f..3f592332c5932e84cdd6e6c7e1c4b31117ca6ae6 100644 (file)
--- a/host.go
+++ b/host.go
@@ -81,6 +81,11 @@ func (h *Host) Connect(term *sshd.Terminal) {
        }()
        defer user.Close()
 
+       // Send MOTD
+       if h.motd != "" {
+               user.Send(chat.NewAnnounceMsg(h.motd))
+       }
+
        member, err := h.Join(user)
        if err == chat.ErrIdTaken {
                // Try again...
@@ -284,6 +289,28 @@ func (h *Host) InitCommands(c *chat.Commands) {
                },
        })
 
+       c.Add(chat.Command{
+               Prefix:     "/whois",
+               PrefixHelp: "USER",
+               Help:       "Information about USER.",
+               Handler: func(room *chat.Room, msg chat.CommandMsg) error {
+                       args := msg.Args()
+                       if len(args) == 0 {
+                               return errors.New("must specify user")
+                       }
+
+                       target, ok := h.GetUser(args[0])
+                       if !ok {
+                               return errors.New("user not found")
+                       }
+
+                       id := target.Identifier.(*Identity)
+                       room.Send(chat.NewSystemMsg(id.Whois(), msg.From()))
+
+                       return nil
+               },
+       })
+
        // Op commands
        c.Add(chat.Command{
                Op:         true,
@@ -349,28 +376,27 @@ func (h *Host) InitCommands(c *chat.Commands) {
 
        c.Add(chat.Command{
                Op:         true,
-               Prefix:     "/whois",
-               PrefixHelp: "USER",
-               Help:       "Information about USER.",
+               Prefix:     "/motd",
+               PrefixHelp: "MESSAGE",
+               Help:       "Set the MESSAGE of the day.",
                Handler: func(room *chat.Room, msg chat.CommandMsg) error {
-                       // TODO: Would be nice to specify what to ban. Key? Ip? etc.
                        if !room.IsOp(msg.From()) {
                                return errors.New("must be op")
                        }
 
+                       motd := ""
                        args := msg.Args()
-                       if len(args) == 0 {
-                               return errors.New("must specify user")
+                       if len(args) > 0 {
+                               motd = strings.Join(args, " ")
                        }
 
-                       target, ok := h.GetUser(args[0])
-                       if !ok {
-                               return errors.New("user not found")
+                       h.motd = motd
+                       body := fmt.Sprintf("New message of the day set by %s:", msg.From().Name())
+                       room.Send(chat.NewAnnounceMsg(body))
+                       if motd != "" {
+                               room.Send(chat.NewAnnounceMsg(motd))
                        }
 
-                       id := target.Identifier.(*Identity)
-                       room.Send(chat.NewSystemMsg(id.Whois(), msg.From()))
-
                        return nil
                },
        })