| 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 | type encMethod int |
| 9 | |
| 10 | type encMethodVals struct { |
| 11 | // None means no encryption |
| 12 | None encMethod |
| 13 | // ExplicitTLS means that TLS needs to be initiated by using StartTLS |
| 14 | ExplicitTLS encMethod |
| 15 | // ImplicitTLS means that TLS is used for the whole session |
| 16 | ImplicitTLS encMethod |
| 17 | // Auto means that TLS will be implicitly used for port 465, otherwise explicit TLS will be used if supported |
| 18 | Auto encMethod |
| 19 | |
| 20 | // Enum is the EnumFormatter instance for EncMethods |
| 21 | Enum types.EnumFormatter |
| 22 | } |
| 23 | |
| 24 | const ( |
| 25 | // EncNone represents no encryption. |
| 26 | EncNone encMethod = iota // 0 |
| 27 | // EncExplicitTLS represents explicit TLS initiated with StartTLS. |
| 28 | EncExplicitTLS // 1 |
| 29 | // EncImplicitTLS represents implicit TLS used throughout the session. |
| 30 | EncImplicitTLS // 2 |
| 31 | // EncAuto represents automatic TLS selection based on port. |
| 32 | EncAuto // 3 |
| 33 | ) |
| 34 | |
| 35 | // ImplicitTLSPort is the de facto standard SMTPS port for implicit TLS. |
| 36 | const ImplicitTLSPort = 465 |
| 37 | |
| 38 | // EncMethods is the enum helper for populating the Encryption field. |
| 39 | var EncMethods = &encMethodVals{ |
| 40 | None: EncNone, |
| 41 | ExplicitTLS: EncExplicitTLS, |
| 42 | ImplicitTLS: EncImplicitTLS, |
| 43 | Auto: EncAuto, |
| 44 | |
| 45 | Enum: format.CreateEnumFormatter( |
| 46 | []string{ |
| 47 | "None", |
| 48 | "ExplicitTLS", |
| 49 | "ImplicitTLS", |
| 50 | "Auto", |
| 51 | }, |
| 52 | ), |
| 53 | } |
| 54 | |
| 55 | func (at encMethod) String() string { |
| 56 | return EncMethods.Enum.Print(int(at)) |
| 57 | } |
| 58 | |
| 59 | // useImplicitTLS determines if implicit TLS should be used based on encryption method and port. |
| 60 | func useImplicitTLS(encryption encMethod, port uint16) bool { |
| 61 | switch encryption { |
| 62 | case EncNone: |
| 63 | return false |
| 64 | case EncExplicitTLS: |
| 65 | return false |
| 66 | case EncImplicitTLS: |
| 67 | return true |
| 68 | case EncAuto: |
| 69 | return port == ImplicitTLSPort |
| 70 | default: |
| 71 | return false // Unreachable due to enum constraints, but included for safety |
| 72 | } |
| 73 | } |