2017-10-29 23:30:44 -07:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
2018-02-16 11:24:43 +02:00
|
|
|
"time"
|
2017-10-29 23:30:44 -07:00
|
|
|
|
2021-01-06 20:06:56 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
shoutrrrSmtp "github.com/containrrr/shoutrrr/pkg/services/smtp"
|
2023-10-04 12:17:38 +02:00
|
|
|
t "github.com/nicholas-fedor/watchtower/pkg/types"
|
2017-11-27 12:03:00 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-10-29 23:30:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
emailType = "email"
|
|
|
|
)
|
|
|
|
|
|
|
|
type emailTypeNotifier struct {
|
2022-11-01 00:00:00 +01:00
|
|
|
From, To string
|
|
|
|
Server, User, Password string
|
|
|
|
Port int
|
|
|
|
tlsSkipVerify bool
|
|
|
|
entries []*log.Entry
|
|
|
|
delay time.Duration
|
2017-10-29 23:30:44 -07:00
|
|
|
}
|
|
|
|
|
2022-11-01 00:00:00 +01:00
|
|
|
func newEmailNotifier(c *cobra.Command) t.ConvertibleNotifier {
|
|
|
|
flags := c.Flags()
|
2019-06-22 22:04:36 +02:00
|
|
|
|
2020-12-21 23:08:23 +01:00
|
|
|
from, _ := flags.GetString("notification-email-from")
|
|
|
|
to, _ := flags.GetString("notification-email-to")
|
|
|
|
server, _ := flags.GetString("notification-email-server")
|
|
|
|
user, _ := flags.GetString("notification-email-server-user")
|
|
|
|
password, _ := flags.GetString("notification-email-server-password")
|
|
|
|
port, _ := flags.GetInt("notification-email-server-port")
|
|
|
|
tlsSkipVerify, _ := flags.GetBool("notification-email-server-tls-skip-verify")
|
|
|
|
delay, _ := flags.GetInt("notification-email-delay")
|
2019-06-22 22:04:36 +02:00
|
|
|
|
2017-10-29 23:30:44 -07:00
|
|
|
n := &emailTypeNotifier{
|
2021-01-06 20:06:56 +01:00
|
|
|
entries: []*log.Entry{},
|
2019-06-22 22:04:36 +02:00
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Server: server,
|
|
|
|
User: user,
|
|
|
|
Password: password,
|
|
|
|
Port: port,
|
|
|
|
tlsSkipVerify: tlsSkipVerify,
|
2019-08-25 13:14:02 +02:00
|
|
|
delay: time.Duration(delay) * time.Second,
|
2017-10-29 23:30:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2022-11-01 00:00:00 +01:00
|
|
|
func (e *emailTypeNotifier) GetURL(c *cobra.Command) (string, error) {
|
2021-01-06 20:06:56 +01:00
|
|
|
conf := &shoutrrrSmtp.Config{
|
|
|
|
FromAddress: e.From,
|
|
|
|
FromName: "Watchtower",
|
|
|
|
ToAddresses: []string{e.To},
|
|
|
|
Port: uint16(e.Port),
|
|
|
|
Host: e.Server,
|
|
|
|
Username: e.User,
|
|
|
|
Password: e.Password,
|
2021-03-13 08:58:11 +01:00
|
|
|
UseStartTLS: !e.tlsSkipVerify,
|
2021-01-06 20:06:56 +01:00
|
|
|
UseHTML: false,
|
2021-03-13 08:58:11 +01:00
|
|
|
Encryption: shoutrrrSmtp.EncMethods.Auto,
|
|
|
|
Auth: shoutrrrSmtp.AuthTypes.None,
|
2023-01-29 17:30:08 +01:00
|
|
|
ClientHost: "localhost",
|
2021-01-06 20:06:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(e.User) > 0 {
|
2021-03-13 08:58:11 +01:00
|
|
|
conf.Auth = shoutrrrSmtp.AuthTypes.Plain
|
2021-01-10 11:12:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 08:58:11 +01:00
|
|
|
if e.tlsSkipVerify {
|
|
|
|
conf.Encryption = shoutrrrSmtp.EncMethods.None
|
2021-01-06 20:06:56 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 08:58:11 +01:00
|
|
|
return conf.GetURL().String(), nil
|
2021-01-06 20:06:56 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 09:31:01 +01:00
|
|
|
func (e *emailTypeNotifier) GetDelay() time.Duration {
|
|
|
|
return e.delay
|
|
|
|
}
|