Skip to content

Commit

Permalink
fix(smtp): add date header and make enums public (#118)
Browse files Browse the repository at this point in the history
* fix(smtp): add date header and make enums public
* fix(smtp): add comments for enum helpers
  • Loading branch information
piksel committed Jan 17, 2021
1 parent 94fef61 commit c7b9fb9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
9 changes: 4 additions & 5 deletions pkg/generators/xouath2/xoauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type Generator struct{}
func (g *Generator) Generate(_ types.Service, props map[string]string, args []string) (types.ServiceConfig, error) {

if provider, found := props["provider"]; found {
if provider == "gmail" {
return oauth2GeneratorGmail(args[0])
}
if provider == "gmail" {
return oauth2GeneratorGmail(args[0])
}
}

if len(args) > 0 {
Expand Down Expand Up @@ -179,11 +179,10 @@ func generateOauth2Config(conf *oauth2.Config, host string) (*smtp.Config, error
FromAddress: sender,
FromName: "Shoutrrr",
ToAddresses: []string{sender},
Auth: smtp.OAuth2,
Auth: smtp.AuthTypes.OAuth2,
UseStartTLS: true,
UseHTML: true,
}

return svcConf, nil
}

20 changes: 11 additions & 9 deletions pkg/services/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"net/smtp"
"net/url"
"time"

"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
Expand Down Expand Up @@ -37,10 +38,10 @@ func (service *Service) Initialize(configURL *url.URL, logger *log.Logger) error
Port: 25,
ToAddresses: nil,
Subject: "",
Auth: authTypes.Unknown,
Auth: AuthTypes.Unknown,
UseStartTLS: true,
UseHTML: false,
Encryption: encMethods.Auto,
Encryption: EncMethods.Auto,
}

pkr := format.NewPropKeyResolver(service.config)
Expand All @@ -49,11 +50,11 @@ func (service *Service) Initialize(configURL *url.URL, logger *log.Logger) error
return err
}

if service.config.Auth == authTypes.Unknown {
if service.config.Auth == AuthTypes.Unknown {
if service.config.Username != "" {
service.config.Auth = authTypes.Plain
service.config.Auth = AuthTypes.Plain
} else {
service.config.Auth = authTypes.None
service.config.Auth = AuthTypes.None
}
}

Expand Down Expand Up @@ -152,13 +153,13 @@ func (service *Service) doSend(client *smtp.Client, message string, config *Conf
func (service *Service) getAuth(config *Config) (smtp.Auth, failure) {

switch config.Auth {
case authTypes.None:
case AuthTypes.None:
return nil, nil
case authTypes.Plain:
case AuthTypes.Plain:
return smtp.PlainAuth("", config.Username, config.Password, config.Host), nil
case authTypes.CRAMMD5:
case AuthTypes.CRAMMD5:
return smtp.CRAMMD5Auth(config.Username, config.Password), nil
case authTypes.OAuth2:
case AuthTypes.OAuth2:
return OAuth2Auth(config.Username, config.Password), nil
default:
return nil, fail(FailAuthType, nil, config.Auth.String())
Expand Down Expand Up @@ -216,6 +217,7 @@ func (service *Service) getHeaders(toAddress string, subject string) map[string]

return map[string]string{
"Subject": subject,
"Date": time.Now().Format(time.RFC1123Z),
"To": toAddress,
"From": fmt.Sprintf("%s <%s>", conf.FromName, conf.FromAddress),
"MIME-version": "1.0;",
Expand Down
11 changes: 3 additions & 8 deletions pkg/services/smtp/smtp_authtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type authTypeVals struct {
Enum types.EnumFormatter
}

var authTypes = &authTypeVals{
// AuthTypes is the enum helper for populating the Auth field
var AuthTypes = &authTypeVals{
None: 0,
Plain: 1,
CRAMMD5: 2,
Expand All @@ -33,11 +34,5 @@ var authTypes = &authTypeVals{
}

func (at authType) String() string {
return authTypes.Enum.Print(int(at))
return AuthTypes.Enum.Print(int(at))
}

func parseAuth(s string) authType {
return authType(authTypes.Enum.Parse(s))
}

var OAuth2 = authTypes.OAuth2
4 changes: 2 additions & 2 deletions pkg/services/smtp/smtp_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (config *Config) Clone() Config {
// Enums returns the fields that should use a corresponding EnumFormatter to Print/Parse their values
func (config Config) Enums() map[string]types.EnumFormatter {
return map[string]types.EnumFormatter{
"Auth": authTypes.Enum,
"Encryption": encMethods.Enum,
"Auth": AuthTypes.Enum,
"Encryption": EncMethods.Enum,
}
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/services/smtp/smtp_encmethod.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type encMethodVals struct {
Enum types.EnumFormatter
}

var encMethods = &encMethodVals{
// EncMethods is the enum helper for populating the Encryption field
var EncMethods = &encMethodVals{
None: 0,
ExplicitTLS: 1,
ImplicitTLS: 2,
Expand All @@ -32,18 +33,14 @@ var encMethods = &encMethodVals{
}

func (at encMethod) String() string {
return encMethods.Enum.Print(int(at))
}

func parseEncryption(s string) encMethod {
return encMethod(encMethods.Enum.Parse(s))
return EncMethods.Enum.Print(int(at))
}

func useImplicitTLS(encryption encMethod, port uint16) bool {
switch encryption {
case encMethods.ImplicitTLS:
case EncMethods.ImplicitTLS:
return true
case encMethods.Auto:
case EncMethods.Auto:
return port == ImplicitTLSPort
default:
return false
Expand Down

0 comments on commit c7b9fb9

Please sign in to comment.