/motd: Add reload functionality when msg is @
This commit is contained in:
parent
0c7f325499
commit
53ae43fb1b
|
@ -205,15 +205,22 @@ func main() {
|
|||
}
|
||||
|
||||
if options.Motd != "" {
|
||||
motd, err := ioutil.ReadFile(options.Motd)
|
||||
if err != nil {
|
||||
fail(7, "Failed to load MOTD file: %v\n", err)
|
||||
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 == "-" {
|
||||
|
|
23
host.go
23
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
|
||||
},
|
||||
|
|
6
motd.txt
6
motd.txt
|
@ -1,4 +1,4 @@
|
|||
[31;1mWelcome to ssh-chat, enter [0m/help[31;1m for more.
|
||||
[32;1m🐛 Please enjoy our selection of bugs, but run your own server if you want to crash it:[0m https://ssh.chat/issues
|
||||
[33;1m🍮 Sponsors get an emoji prefix:[0m https://ssh.chat/sponsor
|
||||
[34;1m😌 Be nice and follow our Code of Conduct:[0m https://ssh.chat/conduct
|
||||
[32;1m🐛 Please enjoy our selection of bugs, but run your own server if you want to crash it:[0m https://ssh.chat/issues
|
||||
[33;1m🍮 Sponsors get an emoji prefix:[0m https://ssh.chat/sponsor
|
||||
[34;1m😌 Be nice and follow our Code of Conduct:[0m https://ssh.chat/conduct
|
||||
|
|
Loading…
Reference in New Issue