Debian Codegraph @

pkg/services/email/smtp/smtp_encmethod.go

raw
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 func (at encMethod) String() string {
55 return EncMethods.Enum.Print(int(at))
56 }
57
58 // useImplicitTLS determines if implicit TLS should be used based on encryption method and port.
59 func useImplicitTLS(encryption encMethod, port uint16) bool {
60 switch encryption {
61 case EncNone:
62 return false
63 case EncExplicitTLS:
64 return false
65 case EncImplicitTLS:
66 return true
67 case EncAuto:
68 return port == ImplicitTLSPort
69 default:
70 return false // Unreachable due to enum constraints, but included for safety
71 }
72 }