Giving each controller a Reload method worked for the notification pipes, which only hold values, but not for the two bot channels: the NapCat client is stopped through a sync.Once and the Telegram polling context is created with the controller, so neither can be re-pointed once running. It also meant five bespoke implementations of the same idea. Replace the whole set instead. Manager.ReplaceAll swaps in a freshly built set under the registry lock, stops the outgoing controllers outside it, and starts the incoming ones off the calling goroutine so a slow handshake does not hold the settings request open. ReplaceAll is now the only way controllers are installed: Register is gone, because a lone registration would slip a controller in without recording the settings it was built from, which is what NeedsRebuild compares against. The rebuild runs from the reload hook and only fires when the controllerMethod section actually changed, so saving a message template or a webhook endpoint leaves the channels alone. NeedsRebuild compares the whole section by value, and a test pins that reloading a file reproduces it exactly — a default applied inconsistently would make every save look like a controller change and tear down every channel. With controllers immutable after construction and replaced wholesale, the atomic pointers and Reload methods have no writers left, so they are gone and the pipes return to plain fields. notifyReload now serializes hook execution for the same reason: a hook mutates process-wide state, and two overlapping settings updates would otherwise each decide to rebuild from their own view of what was applied. Kept from that work: applyEmailProxy restores gomail's default dialer when networkUseProxy is turned off. The old constructor only ever installed the proxy dialer, so switching the flag back left SMTP tunnelled through a proxy with no way to undo it short of a restart. Documents the resulting behaviour in the README under "What applies without a restart".
153 lines
4.4 KiB
Go
153 lines
4.4 KiB
Go
package pipes
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
|
|
gomail "gopkg.in/mail.v2"
|
|
|
|
"nukumizu-backend/config"
|
|
"nukumizu-backend/internal/controller"
|
|
"nukumizu-backend/internal/netproxy"
|
|
"nukumizu-backend/internal/node"
|
|
"nukumizu-backend/internal/template"
|
|
"nukumizu-backend/postLog"
|
|
)
|
|
|
|
// EmailController handles email notifications via SMTP.
|
|
//
|
|
// cfg is written once, by the constructor, and never again: a controller that
|
|
// needs different settings is replaced wholesale by the manager rather than
|
|
// reconfigured in place, so the send methods can read it without locking.
|
|
type EmailController struct {
|
|
cfg config.EmailConfig
|
|
}
|
|
|
|
// NewEmailController creates a new Email controller.
|
|
func NewEmailController(cfg config.EmailConfig) *EmailController {
|
|
applyEmailProxy(cfg.NetworkUseProxy)
|
|
return &EmailController{cfg: cfg}
|
|
}
|
|
|
|
// applyEmailProxy routes SMTP through the HTTP CONNECT proxy, or restores a
|
|
// direct dial. gomail exposes the dial path as the package-level
|
|
// NetDialTimeout, whose own default is net.DialTimeout, so turning the proxy
|
|
// off has to put that back rather than leave the hook in place. There is a
|
|
// single global email channel, so setting a package-level hook here is
|
|
// unambiguous.
|
|
func applyEmailProxy(useProxy bool) {
|
|
if useProxy {
|
|
gomail.NetDialTimeout = netproxy.DialWithTimeout(true)
|
|
return
|
|
}
|
|
gomail.NetDialTimeout = net.DialTimeout
|
|
}
|
|
|
|
// Name returns the controller name.
|
|
func (e *EmailController) Name() string {
|
|
return "email"
|
|
}
|
|
|
|
// Start initializes the Email controller.
|
|
func (e *EmailController) Start() error {
|
|
if !e.cfg.Enabled {
|
|
postLog.Info("Email controller is disabled")
|
|
return nil
|
|
}
|
|
postLog.Info("Email controller started")
|
|
return nil
|
|
}
|
|
|
|
// Stop shuts down the Email controller.
|
|
func (e *EmailController) Stop() {
|
|
postLog.Info("Email controller stopped")
|
|
}
|
|
|
|
// IsEnabled returns whether the controller is enabled.
|
|
func (e *EmailController) IsEnabled() bool {
|
|
return e.cfg.Enabled
|
|
}
|
|
|
|
// IsMarkdown returns whether the channel renders Markdown, per its markdown
|
|
// setting in config.json.
|
|
func (e *EmailController) IsMarkdown() bool {
|
|
return e.cfg.Markdown
|
|
}
|
|
|
|
// SendStatusChange sends a status change notification via Email.
|
|
func (e *EmailController) SendStatusChange(change node.StatusChange) error {
|
|
if !e.cfg.Enabled {
|
|
return nil
|
|
}
|
|
if len(e.cfg.To) == 0 {
|
|
postLog.Debug("Email controller has no recipients configured")
|
|
return nil
|
|
}
|
|
|
|
cfg := config.Current()
|
|
params := template.BuildParamsFromStatusChange(change)
|
|
body := template.Render(cfg.ControllerMessage.ServerStatusChanged, params, e.cfg.Markdown)
|
|
|
|
subject := fmt.Sprintf("Server Status Change: %s - %s", change.Name, change.Event)
|
|
return e.sendEmail(subject, body)
|
|
}
|
|
|
|
// SendServerList sends the server list via Email.
|
|
func (e *EmailController) SendServerList(onlineServers, offlineServers string) error {
|
|
if !e.cfg.Enabled || len(e.cfg.To) == 0 {
|
|
return nil
|
|
}
|
|
|
|
cfg := config.Current()
|
|
params := template.BuildParamsFromServerList()
|
|
body := template.Render(cfg.ControllerMessage.ServerList, params, e.cfg.Markdown)
|
|
|
|
return e.sendEmail("Server List", body)
|
|
}
|
|
|
|
// SendExecuteResult sends a command execution result via Email.
|
|
func (e *EmailController) SendExecuteResult(serverName, serverUUID, command, result string) error {
|
|
if !e.cfg.Enabled || len(e.cfg.To) == 0 {
|
|
return nil
|
|
}
|
|
|
|
cfg := config.Current()
|
|
params := template.BuildParamsFromExecResult(serverName, serverUUID, command, result)
|
|
body := template.Render(cfg.ControllerMessage.ServerExecuteResult, params, e.cfg.Markdown)
|
|
|
|
subject := fmt.Sprintf("Command Result: %s on %s", command, serverName)
|
|
return e.sendEmail(subject, body)
|
|
}
|
|
|
|
// SendAlert sends an alert submitted through the incoming webhook API to the
|
|
// configured recipients.
|
|
func (e *EmailController) SendAlert(alert controller.Alert) error {
|
|
if !e.cfg.Enabled {
|
|
return nil
|
|
}
|
|
if len(e.cfg.To) == 0 {
|
|
postLog.Debug("Email controller has no recipients configured")
|
|
return nil
|
|
}
|
|
|
|
return e.sendEmail(alert.Subject, alert.Render(e.cfg.Markdown))
|
|
}
|
|
|
|
func (e *EmailController) sendEmail(subject, body string) error {
|
|
m := gomail.NewMessage()
|
|
m.SetHeader("From", e.cfg.From)
|
|
m.SetHeader("To", e.cfg.To...)
|
|
m.SetHeader("Subject", subject)
|
|
m.SetBody("text/plain", body)
|
|
|
|
d := gomail.NewDialer(e.cfg.SMTPHost, e.cfg.SMTPPort, e.cfg.Username, e.cfg.Password)
|
|
|
|
if err := d.DialAndSend(m); err != nil {
|
|
postLog.Warning("Failed to send email: " + err.Error())
|
|
return err
|
|
}
|
|
|
|
postLog.Debug("Email sent successfully to " + fmt.Sprintf("%v", e.cfg.To))
|
|
return nil
|
|
}
|