| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 5 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 6 |
) |
| 7 |
|
| 8 |
// encMethod is an SMTP transport encryption method. |
| 9 |
type encMethod int |
| 10 |
|
| 11 |
// encMethodVals holds named encryption methods and their enum formatter. |
| 12 |
type encMethodVals struct { |
| 13 |
// None means no encryption. |
| 14 |
None encMethod |
| 15 |
// ExplicitTLS means that TLS is initiated with STARTTLS. |
| 16 |
ExplicitTLS encMethod |
| 17 |
// ImplicitTLS means that TLS is used for the whole session. |
| 18 |
ImplicitTLS encMethod |
| 19 |
// Auto means implicit TLS on port [ImplicitTLSPort], otherwise explicit TLS when supported. |
| 20 |
Auto encMethod |
| 21 |
// Enum formats and parses encryption method values. |
| 22 |
Enum types.EnumFormatter |
| 23 |
} |
| 24 |
|
| 25 |
const ( |
| 26 |
// EncNone represents no encryption. |
| 27 |
EncNone encMethod = iota |
| 28 |
// EncExplicitTLS represents explicit TLS initiated with STARTTLS. |
| 29 |
EncExplicitTLS |
| 30 |
// EncImplicitTLS represents implicit TLS used throughout the session. |
| 31 |
EncImplicitTLS |
| 32 |
// EncAuto represents automatic TLS selection based on port. |
| 33 |
EncAuto |
| 34 |
) |
| 35 |
|
| 36 |
// ImplicitTLSPort is the de facto standard SMTPS port for implicit TLS. |
| 37 |
const ImplicitTLSPort = 465 |
| 38 |
|
| 39 |
// EncMethods is the enum helper for populating the [Config.Encryption] field. |
| 40 |
var EncMethods = &encMethodVals{ |
| 41 |
None: EncNone, |
| 42 |
ExplicitTLS: EncExplicitTLS, |
| 43 |
ImplicitTLS: EncImplicitTLS, |
| 44 |
Auto: EncAuto, |
| 45 |
|
| 46 |
Enum: format.CreateEnumFormatter( |
| 47 |
[]string{ |
| 48 |
"None", |
| 49 |
"ExplicitTLS", |
| 50 |
"ImplicitTLS", |
| 51 |
"Auto", |
| 52 |
}, |
| 53 |
), |
| 54 |
} |
| 55 |
|
| 56 |
// String returns the encryption method name. |
| 57 |
// |
| 58 |
// Returns: |
| 59 |
// - The canonical name of the encryption method, such as "Auto" or "ImplicitTLS". |
| 60 |
func (em encMethod) String() string { |
| 61 |
return EncMethods.Enum.Print(int(em)) |
| 62 |
} |
| 63 |
|
| 64 |
// useImplicitTLS reports whether implicit TLS should be used. |
| 65 |
// |
| 66 |
// Parameters: |
| 67 |
// - encryption: The configured encryption method. |
| 68 |
// - port: The SMTP server port. |
| 69 |
// |
| 70 |
// Returns: |
| 71 |
// - true when [EncImplicitTLS] is selected, or when [EncAuto] is selected and port is [ImplicitTLSPort]. |
| 72 |
func useImplicitTLS(encryption encMethod, port uint16) bool { |
| 73 |
switch encryption { |
| 74 |
case EncNone: |
| 75 |
return false |
| 76 |
case EncExplicitTLS: |
| 77 |
return false |
| 78 |
case EncImplicitTLS: |
| 79 |
return true |
| 80 |
case EncAuto: |
| 81 |
return port == ImplicitTLSPort |
| 82 |
default: |
| 83 |
// Unreachable due to enum constraints, but included for safety. |
| 84 |
return false |
| 85 |
} |
| 86 |
} |