github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/pipes/mail/config.go (about) 1 package mail 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/smtp" 7 "strings" 8 9 "github.com/lmorg/murex/config" 10 "github.com/lmorg/murex/lang" 11 "github.com/lmorg/murex/lang/types" 12 ) 13 14 func init() { 15 config.InitConf.Define("mail", "sender", config.Properties{ 16 Description: "Sender email address used when sending an email via the 'mail' pipe (ie FROM). This is used both with authenticated mail and MX sends", 17 Default: fmt.Sprintf("%s@%s", username(), hostname()), 18 DataType: types.String, 19 Global: true, 20 }) 21 22 config.InitConf.Define("mail", "allow-plain-text", config.Properties{ 23 Description: "Allow plain text connections when sending direct via MX records (not recommended)", 24 Default: false, 25 DataType: types.Boolean, 26 Global: true, 27 }) 28 29 config.InitConf.Define("mail", "ignore-verify-certs", config.Properties{ 30 Description: "Don't verify the server name when making TLS connections when sending direct via MX records (not recommended)", 31 Default: false, 32 DataType: types.Boolean, 33 Global: true, 34 }) 35 36 config.InitConf.Define("mail", "ports", config.Properties{ 37 Description: "Port numbers to try, in order of precedence when sending direct via MX records", 38 Default: ports, 39 DataType: types.Json, 40 Global: true, 41 GoFunc: config.GoFuncProperties{ 42 Read: getPorts, 43 Write: setPorts, 44 }, 45 }) 46 47 config.InitConf.Define("mail", "smtp-auth-host", config.Properties{ 48 Description: "SMTP hostname (eg 'smtp.google.com') to send authenticated mail from (if unset, murex will attempt to send directly from via MX records and not use auth)", 49 Default: "", 50 DataType: types.String, 51 Global: true, 52 }) 53 54 config.InitConf.Define("mail", "smtp-auth-port", config.Properties{ 55 Description: "SMTP port number (eg 587) to send authenticated mail (if unset, murex will attempt to send directly from via MX records and not use auth)", 56 Default: 587, 57 DataType: types.Integer, 58 Global: true, 59 }) 60 61 config.InitConf.Define("mail", "smtp-auth-user", config.Properties{ 62 Description: "User name when using SMTP user auth (requires 'smtp-host' set too)", 63 Default: "", 64 DataType: types.String, 65 Global: true, 66 }) 67 68 config.InitConf.Define("mail", "smtp-auth-pass", config.Properties{ 69 Description: "Password when using SMTP user auth (requires 'smtp-host' set too)", 70 Default: "", 71 DataType: types.String, 72 Global: true, 73 GoFunc: config.GoFuncProperties{ 74 Read: getPass, 75 Write: setPass, 76 }, 77 }) 78 79 config.InitConf.Define("mail", "smtp-auth-enabled", config.Properties{ 80 Description: "Enable or disable SMTP auth", 81 Default: false, 82 DataType: types.Boolean, 83 Global: true, 84 }) 85 } 86 87 var ports = []int{ 88 587, 89 2525, 90 465, 91 25, 92 } 93 94 func getPorts() (interface{}, error) { 95 return ports, nil 96 } 97 98 func setPorts(v interface{}) error { 99 switch v.(type) { 100 case string: 101 return json.Unmarshal([]byte(v.(string)), &ports) 102 103 default: 104 return fmt.Errorf("Invalid data-type. Expecting a %s encoded string", types.Json) 105 } 106 } 107 108 func allowPlainText() bool { 109 v, err := lang.ShellProcess.Config.Get("mail", "allow-plain-text", types.Boolean) 110 if err != nil { 111 return false 112 } 113 114 return v.(bool) 115 } 116 117 func allowInsecure() bool { 118 v, err := lang.ShellProcess.Config.Get("mail", "ignore-verify-certs", types.Boolean) 119 if err != nil { 120 return false 121 } 122 123 return v.(bool) 124 } 125 126 func senderAddr() string { 127 v, err := lang.ShellProcess.Config.Get("mail", "sender", types.String) 128 if err != nil { 129 return fmt.Sprintf("%s@%s", username(), hostname()) 130 } 131 132 s := strings.TrimSpace(v.(string)) 133 if len(s) == 0 { 134 return fmt.Sprintf("%s@%s", username(), hostname()) 135 } 136 137 return s 138 } 139 140 func smtpAuthHost() string { 141 v, err := lang.ShellProcess.Config.Get("mail", "smtp-auth-host", types.String) 142 if err != nil { 143 return "" 144 } 145 146 return v.(string) 147 } 148 149 func smtpAuthPort() int { 150 v, err := lang.ShellProcess.Config.Get("mail", "smtp-auth-port", types.Integer) 151 if err != nil { 152 return 587 153 } 154 155 return v.(int) 156 } 157 158 var ( 159 smtpPassword string 160 ) 161 162 func getPass() (interface{}, error) { 163 if smtpPassword == "" { 164 return "unset", nil 165 } 166 return "redacted", nil 167 } 168 169 func setPass(v interface{}) error { 170 switch v.(type) { 171 case string: 172 smtpPassword = v.(string) 173 return nil 174 175 default: 176 return fmt.Errorf("Invalid data-type. Expecting a %s", types.String) 177 } 178 } 179 180 func getSmtpAuth() (smtpAuth smtp.Auth, enabled bool) { 181 var ( 182 user string 183 pass string 184 host string 185 ) 186 187 v, err := lang.ShellProcess.Config.Get("mail", "smtp-auth-user", types.String) 188 if err != nil { 189 user = "" 190 } else { 191 user = v.(string) 192 } 193 194 /*v, err = lang.ShellProcess.Config.Get("mail", "smtp-auth-pass", types.String) 195 if err != nil { 196 pass = "" 197 } else { 198 pass = v.(string) 199 }*/ 200 pass = smtpPassword 201 202 v, err = lang.ShellProcess.Config.Get("mail", "smtp-auth-enabled", types.Boolean) 203 if err != nil { 204 enabled = false 205 } else { 206 enabled = v.(bool) 207 } 208 209 host = smtpAuthHost() 210 if host == "" { 211 enabled = false 212 } 213 214 if enabled { 215 smtpAuth = smtp.PlainAuth("", user, pass, host) 216 } 217 218 return 219 }