diff --git a/cmd/channels/email/main.go b/cmd/channels/email/main.go index f1339a08..a42a90eb 100644 --- a/cmd/channels/email/main.go +++ b/cmd/channels/email/main.go @@ -16,6 +16,10 @@ import ( "net/mail" ) +func main() { + plugin.RunPlugin(&Email{}) +} + const ( EncryptionNone = "none" EncryptionStartTLS = "starttls" @@ -32,86 +36,6 @@ type Email struct { Encryption string `json:"encryption"` } -func main() { - plugin.RunPlugin(&Email{}) -} - -func (ch *Email) SendNotification(req *plugin.NotificationRequest) error { - var to []mail.Address - for _, address := range req.Contact.Addresses { - if address.Type == "email" { - to = append(to, mail.Address{Name: req.Contact.FullName, Address: address.Address}) - } - } - - if len(to) == 0 { - return fmt.Errorf("contact user %s does not have an e-mail address", req.Contact.FullName) - } - - var msg bytes.Buffer - plugin.FormatMessage(&msg, req) - - return enmime.Builder(). - ToAddrs(to). - From(ch.SenderName, ch.SenderMail). - Subject(plugin.FormatSubject(req)). - Header("Message-Id", fmt.Sprintf("<%s-%s>", uuid.New().String(), ch.SenderMail)). - Text(msg.Bytes()). - Send(ch) -} - -func (ch *Email) Send(reversePath string, recipients []string, msg []byte) error { - var ( - client *smtp.Client - err error - ) - - switch ch.Encryption { - case EncryptionStartTLS: - client, err = smtp.DialStartTLS(ch.GetServer(), nil) - case EncryptionTLS: - client, err = smtp.DialTLS(ch.GetServer(), nil) - case EncryptionNone: - client, err = smtp.Dial(ch.GetServer()) - default: - return fmt.Errorf("unsupported mail encryption type %q", ch.Encryption) - } - if err != nil { - return err - } - defer func() { _ = client.Close() }() - - if ch.Password != "" { - if err = client.Auth(sasl.NewPlainClient("", ch.User, ch.Password)); err != nil { - return err - } - } - - if err := client.SendMail(reversePath, recipients, bytes.NewReader(msg)); err != nil { - return err - } - - return client.Quit() -} - -func (ch *Email) SetConfig(jsonStr json.RawMessage) error { - err := plugin.PopulateDefaults(ch) - if err != nil { - return err - } - - err = json.Unmarshal(jsonStr, ch) - if err != nil { - return fmt.Errorf("failed to load config: %s %w", jsonStr, err) - } - - if (ch.User == "") != (ch.Password == "") { - return fmt.Errorf("user and password fields must both be set or empty") - } - - return nil -} - func (ch *Email) GetInfo() *plugin.Info { configAttrs := plugin.ConfigOptions{ { @@ -197,6 +121,81 @@ func (ch *Email) GetInfo() *plugin.Info { } } -func (ch *Email) GetServer() string { - return net.JoinHostPort(ch.Host, ch.Port) +func (ch *Email) SetConfig(jsonStr json.RawMessage) error { + err := plugin.PopulateDefaults(ch) + if err != nil { + return err + } + + err = json.Unmarshal(jsonStr, ch) + if err != nil { + return fmt.Errorf("failed to load config: %s %w", jsonStr, err) + } + + if (ch.User == "") != (ch.Password == "") { + return fmt.Errorf("user and password fields must both be set or empty") + } + + return nil +} + +func (ch *Email) SendNotification(req *plugin.NotificationRequest) error { + var to []mail.Address + for _, address := range req.Contact.Addresses { + if address.Type == "email" { + to = append(to, mail.Address{Name: req.Contact.FullName, Address: address.Address}) + } + } + + if len(to) == 0 { + return fmt.Errorf("contact user %s does not have an e-mail address", req.Contact.FullName) + } + + var msg bytes.Buffer + plugin.FormatMessage(&msg, req) + + return enmime.Builder(). + ToAddrs(to). + From(ch.SenderName, ch.SenderMail). + Subject(plugin.FormatSubject(req)). + Header("Message-Id", fmt.Sprintf("<%s-%s>", uuid.New().String(), ch.SenderMail)). + Text(msg.Bytes()). + Send(ch) +} + +// Send implements the enmime.Sender interface. +func (ch *Email) Send(reversePath string, recipients []string, msg []byte) error { + var ( + client *smtp.Client + err error + ) + + serverAddr := net.JoinHostPort(ch.Host, ch.Port) + + switch ch.Encryption { + case EncryptionStartTLS: + client, err = smtp.DialStartTLS(serverAddr, nil) + case EncryptionTLS: + client, err = smtp.DialTLS(serverAddr, nil) + case EncryptionNone: + client, err = smtp.Dial(serverAddr) + default: + return fmt.Errorf("unsupported mail encryption type %q", ch.Encryption) + } + if err != nil { + return err + } + defer func() { _ = client.Close() }() + + if ch.Password != "" { + if err = client.Auth(sasl.NewPlainClient("", ch.User, ch.Password)); err != nil { + return err + } + } + + if err := client.SendMail(reversePath, recipients, bytes.NewReader(msg)); err != nil { + return err + } + + return client.Quit() } diff --git a/cmd/channels/rocketchat/main.go b/cmd/channels/rocketchat/main.go index 49afb0b2..199d9d72 100644 --- a/cmd/channels/rocketchat/main.go +++ b/cmd/channels/rocketchat/main.go @@ -11,14 +11,62 @@ import ( "time" ) +func main() { + plugin.RunPlugin(&RocketChat{}) +} + type RocketChat struct { URL string `json:"url"` UserID string `json:"user_id"` Token string `json:"token"` } -func main() { - plugin.RunPlugin(&RocketChat{}) +func (ch *RocketChat) GetInfo() *plugin.Info { + configAttrs := plugin.ConfigOptions{ + { + Name: "url", + Type: "string", + Label: map[string]string{ + "en_US": "Rocket.Chat URL", + "de_DE": "Rocket.Chat URL", + }, + Required: true, + }, + { + Name: "user_id", + Type: "string", + Label: map[string]string{ + "en_US": "User ID", + "de_DE": "Benutzer ID", + }, + Required: true, + }, + { + Name: "token", + Type: "secret", + Label: map[string]string{ + "en_US": "Personal Access Token", + "de_DE": "Persönliches Zugangstoken", + }, + Required: true, + }, + } + + return &plugin.Info{ + Name: "Rocket.Chat", + Version: internal.Version.Version, + Author: "Icinga GmbH", + ConfigAttributes: configAttrs, + } +} + +func (ch *RocketChat) SetConfig(jsonStr json.RawMessage) error { + err := plugin.PopulateDefaults(ch) + if err != nil { + return err + } + + return json.Unmarshal(jsonStr, ch) } func (ch *RocketChat) SendNotification(req *plugin.NotificationRequest) error { @@ -75,51 +123,3 @@ func (ch *RocketChat) SendNotification(req *plugin.NotificationRequest) error { return nil } - -func (ch *RocketChat) SetConfig(jsonStr json.RawMessage) error { - err := plugin.PopulateDefaults(ch) - if err != nil { - return err - } - - return json.Unmarshal(jsonStr, ch) -} - -func (ch *RocketChat) GetInfo() *plugin.Info { - configAttrs := plugin.ConfigOptions{ - { - Name: "url", - Type: "string", - Label: map[string]string{ - "en_US": "Rocket.Chat URL", - "de_DE": "Rocket.Chat URL", - }, - Required: true, - }, - { - Name: "user_id", - Type: "string", - Label: map[string]string{ - "en_US": "User ID", - "de_DE": "Benutzer ID", - }, - Required: true, - }, - { - Name: "token", - Type: "secret", - Label: map[string]string{ - "en_US": "Personal Access Token", - "de_DE": "Persönliches Zugangstoken", - }, - Required: true, - }, - } - - return &plugin.Info{ - Name: "Rocket.Chat", - Version: internal.Version.Version, - Author: "Icinga GmbH", - ConfigAttributes: configAttrs, - } -} diff --git a/cmd/channels/webhook/main.go b/cmd/channels/webhook/main.go index 5407b12f..90a72701 100644 --- a/cmd/channels/webhook/main.go +++ b/cmd/channels/webhook/main.go @@ -14,6 +14,10 @@ import ( "text/template" ) +func main() { + plugin.RunPlugin(&Webhook{}) +} + type Webhook struct { Method string `json:"method"` URLTemplate string `json:"url_template"` @@ -164,7 +168,3 @@ func (ch *Webhook) SendNotification(req *plugin.NotificationRequest) error { return nil } - -func main() { - plugin.RunPlugin(&Webhook{}) -}