mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-12 15:17:16 +03:00
/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 != "" {
|
if options.Motd != "" {
|
||||||
motd, err := ioutil.ReadFile(options.Motd)
|
host.GetMOTD = func() (string, error) {
|
||||||
if err != nil {
|
motd, err := ioutil.ReadFile(options.Motd)
|
||||||
fail(7, "Failed to load MOTD file: %v\n", err)
|
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 == "-" {
|
if options.Log == "-" {
|
||||||
|
23
host.go
23
host.go
@ -46,6 +46,9 @@ type Host struct {
|
|||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
motd string
|
motd string
|
||||||
count int
|
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.
|
// 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.
|
// SetMotd sets the host's message of the day.
|
||||||
|
// TODO: Change to SetMOTD
|
||||||
func (h *Host) SetMotd(motd string) {
|
func (h *Host) SetMotd(motd string) {
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
h.motd = motd
|
h.motd = motd
|
||||||
@ -544,7 +548,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
|
|||||||
Op: true,
|
Op: true,
|
||||||
Prefix: "/motd",
|
Prefix: "/motd",
|
||||||
PrefixHelp: "[MESSAGE]",
|
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 {
|
Handler: func(room *chat.Room, msg message.CommandMsg) error {
|
||||||
args := msg.Args()
|
args := msg.Args()
|
||||||
user := msg.From()
|
user := msg.From()
|
||||||
@ -561,10 +565,21 @@ func (h *Host) InitCommands(c *chat.Commands) {
|
|||||||
return errors.New("must be OP to modify the MOTD")
|
return errors.New("must be OP to modify the MOTD")
|
||||||
}
|
}
|
||||||
|
|
||||||
motd = strings.Join(args, " ")
|
var err error
|
||||||
h.SetMotd(motd)
|
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())
|
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
|
return nil
|
||||||
},
|
},
|
||||||
|
6
motd.txt
6
motd.txt
@ -1,4 +1,4 @@
|
|||||||
[31;1mWelcome to ssh-chat, enter [0m/help[31;1m for more.
|
[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
|
[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
|
[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
|
[34;1m😌 Be nice and follow our Code of Conduct:[0m https://ssh.chat/conduct
|
||||||
|
Loading…
x
Reference in New Issue
Block a user