-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
171 lines (148 loc) · 4.9 KB
/
settings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2015-2017 John Weldon. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE.md file.
package mqd // import "jw4.us/mqd"
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/mail"
gosmtp "net/smtp"
"os"
"strings"
"jw4.us/mqd/smtp"
)
// SMTPAuthType names smtp authentication methods
type SMTPAuthType string
// SMTPAuthTypes
const (
LoginAuth SMTPAuthType = "LOGIN"
PlainAuth SMTPAuthType = "PLAIN"
)
// Settings holds the configuration parameters used by the mail queue
// dispatcher.
type Settings struct {
C map[string]ConnectionDetails `json:"connections"`
MailQueue string `json:"mailqueue"`
BadMail string `json:"badmail"`
SentMail string `json:"sentmail"`
Interval int `json:"interval"`
}
// NewSettings generates a new Settings configuration, initializing it
// with the supplied mailqueue and badmail folders, and an empty map
// of connection details.
func NewSettings(mailqueue string, badmail string) *Settings {
return &Settings{C: map[string]ConnectionDetails{}, MailQueue: mailqueue, BadMail: badmail, Interval: 30}
}
// String fulfills the fmt.Stringer interface
func (s *Settings) String() string {
return fmt.Sprintf(
"mailqueue: %s, badmail: %s, interval: %d seconds, details: %s",
s.MailQueue, s.BadMail, s.Interval, s.C)
}
// ConnectionForSender uses the supplied sender and tries to find
// ConnectionDetails that match the email address.
func (s *Settings) ConnectionForSender(sender string) (ConnectionDetails, error) {
if details, ok := s.C[sender]; ok {
return details, nil
}
addr, err := mail.ParseAddress(sender)
if err != nil {
return ConnectionDetails{}, err
}
if details, ok := s.C[addr.Address]; ok {
return details, nil
}
if details, ok := s.C[strings.ToLower(addr.Address)]; ok {
return details, nil
}
return ConnectionDetails{}, fmt.Errorf("connection details not found for %q", sender)
}
// ReadSettingsFrom uses an io.Reader to read in JSON that is parsed
// into a Settings configuration object.
func ReadSettingsFrom(r io.Reader) (*Settings, error) {
raw, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return UnmarshalSettings(raw)
}
// UnmarshalSettings converts raw bytes that are expected to be in JSON
// format into the Settings configuration object.
func UnmarshalSettings(raw []byte) (*Settings, error) {
s := &Settings{}
err := unmarshalSettings(raw, s)
if err != nil {
return s, err
}
if s.Interval < 5 || s.Interval > 3600 {
s.Interval = 30
}
return s, nil
}
// WriteSettingsTo marshals the Settings config struct and sends it to
// the io.Writer, returning any errors encountered along the way.
func WriteSettingsTo(w io.Writer, s *Settings) error {
bytes, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
_, err = w.Write(bytes)
if err != nil {
return err
}
return nil
}
// WriteSettings is a helper function to open a file and send it to the
// WriteSettingsTo function.
func WriteSettings(path string, s *Settings) error {
fi, err := os.Create(path)
if err != nil {
return err
}
defer func() { _ = fi.Close() }()
return WriteSettingsTo(fi, s)
}
// ConnectionDetails describe the metadata needed to complete an SMTP
// connection for a given sender.
type ConnectionDetails struct {
// Sender should be the plain, lowercase email address of the
// account that the email will originate from.
Sender string `json:"sender,omitempty"`
// Server is the string representing the smtp host and port joined
// by a colon. e.g: smtp.example.com:25
Server string `json:"server,omitempty"`
// Host is just the host portion. e.g: smtp.example.com
Host string `json:"host,omitempty"`
// AuthType shows which authentication mechanism should be used
// when connecting to this Server.
AuthType SMTPAuthType `json:"authtype"`
// Username of the Sender account.
Username string `json:"username,omitempty"`
// Password of the Sender account.
Password string `json:"password,omitempty"`
}
// Auth returns an implementation of the smtp.Auth interface that can
// be used to perform the smtp authentication for this ConnectionDetails
func (d *ConnectionDetails) Auth() (gosmtp.Auth, error) {
if d == nil {
return nil, fmt.Errorf("invalid connection info; nil")
}
switch d.AuthType {
case LoginAuth:
return smtp.LoginAuth(d.Username, d.Password), nil
case PlainAuth:
return gosmtp.PlainAuth("", d.Username, d.Password, d.Host), nil
}
return nil, fmt.Errorf("unknown auth type %q", string(d.AuthType))
}
// String implements the fmt.Stringer interface
func (d *ConnectionDetails) String() string {
return fmt.Sprintf(
"sender: %s, authtype: %s, server: %s, host: %s, username: %s, password: ******",
d.Sender, d.AuthType, d.Server, d.Host, d.Username)
}
func unmarshalSettings(data []byte, s *Settings) error {
return json.Unmarshal(data, s)
}