From 53ae43fb1bd8fd1a353ce61e2cc6def9228c3f35 Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Mon, 3 Aug 2020 13:26:12 -0400 Subject: [PATCH] /motd: Add reload functionality when msg is @ --- cmd/ssh-chat/cmd.go | 21 ++++++++++++++------- host.go | 23 +++++++++++++++++++---- motd.txt | 6 +++--- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/cmd/ssh-chat/cmd.go b/cmd/ssh-chat/cmd.go index bd89b190..d5dca7cc 100644 --- a/cmd/ssh-chat/cmd.go +++ b/cmd/ssh-chat/cmd.go @@ -205,15 +205,22 @@ func main() { } if options.Motd != "" { - motd, err := ioutil.ReadFile(options.Motd) - if err != nil { + host.GetMOTD = func() (string, error) { + motd, err := ioutil.ReadFile(options.Motd) + if err != nil { + return "", err + } + motdString := 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) + return motdString, nil + } + if motdString, err := host.GetMOTD(); err != nil { fail(7, "Failed to load MOTD file: %v\n", err) + } else { + host.SetMotd(motdString) } - motdString := 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) - host.SetMotd(motdString) } if options.Log == "-" { diff --git a/host.go b/host.go index 2a67e393..e9ae8d0c 100644 --- a/host.go +++ b/host.go @@ -46,6 +46,9 @@ type Host struct { mu sync.Mutex motd string count int + + // GetMOTD is used to reload the motd from an external source + GetMOTD func() (string, error) } // NewHost creates a Host on top of an existing listener. @@ -75,6 +78,7 @@ func (h *Host) SetTheme(theme message.Theme) { } // SetMotd sets the host's message of the day. +// TODO: Change to SetMOTD func (h *Host) SetMotd(motd string) { h.mu.Lock() h.motd = motd @@ -544,7 +548,7 @@ func (h *Host) InitCommands(c *chat.Commands) { Op: true, Prefix: "/motd", PrefixHelp: "[MESSAGE]", - Help: "Set a new MESSAGE of the day, print the current motd without parameters.", + Help: "Set a new MESSAGE of the day, or print the motd if no MESSAGE.", Handler: func(room *chat.Room, msg message.CommandMsg) error { args := msg.Args() user := msg.From() @@ -561,10 +565,21 @@ func (h *Host) InitCommands(c *chat.Commands) { return errors.New("must be OP to modify the MOTD") } - motd = strings.Join(args, " ") - h.SetMotd(motd) + var err error + var s string = strings.Join(args, " ") + + if s == "@" { + if h.GetMOTD == nil { + return errors.New("motd reload not set") + } + if s, err = h.GetMOTD(); err != nil { + return err + } + } + + h.SetMotd(s) fromMsg := fmt.Sprintf("New message of the day set by %s:", msg.From().Name()) - room.Send(message.NewAnnounceMsg(fromMsg + message.Newline + "-> " + motd)) + room.Send(message.NewAnnounceMsg(fromMsg + message.Newline + "-> " + s)) return nil }, diff --git a/motd.txt b/motd.txt index f8fdcc3e..8a874453 100644 --- a/motd.txt +++ b/motd.txt @@ -1,4 +1,4 @@ Welcome to ssh-chat, enter /help for more. - 🐛 Please enjoy our selection of bugs, but run your own server if you want to crash it: https://ssh.chat/issues - 🍮 Sponsors get an emoji prefix: https://ssh.chat/sponsor - 😌 Be nice and follow our Code of Conduct: https://ssh.chat/conduct +🐛 Please enjoy our selection of bugs, but run your own server if you want to crash it: https://ssh.chat/issues +🍮 Sponsors get an emoji prefix: https://ssh.chat/sponsor +😌 Be nice and follow our Code of Conduct: https://ssh.chat/conduct