|
@@ -9,6 +9,8 @@ package smtpn
|
|
|
*/
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
"io/ioutil"
|
|
|
"log"
|
|
|
"net/smtp"
|
|
@@ -20,13 +22,48 @@ import (
|
|
|
)
|
|
|
|
|
|
type Agent struct {
|
|
|
- Hostname string
|
|
|
+ Hostname string `json:"-"`
|
|
|
SMTPSenderDisplayName string
|
|
|
SMTPSender string
|
|
|
SMTPPassword string
|
|
|
SMTPDomain string
|
|
|
SMTPPort int
|
|
|
- UsernameToEmailMap map[string]string
|
|
|
+ UsernameToEmail func(string) (string, error) `json:"-"`
|
|
|
+}
|
|
|
+
|
|
|
+func NewSMTPNotificationAgent(hostname string, configFile string, usernameToEmailFunction func(string) (string, error)) (*Agent, error) {
|
|
|
+ config, err := ioutil.ReadFile(configFile)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New("Unable to load config from file: " + err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ //Pasre the json file to agent object
|
|
|
+ newAgent := Agent{}
|
|
|
+ err = json.Unmarshal(config, &newAgent)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New("Unable to parse config file for SMTP authentication")
|
|
|
+ }
|
|
|
+
|
|
|
+ newAgent.Hostname = hostname
|
|
|
+ newAgent.UsernameToEmail = usernameToEmailFunction
|
|
|
+ return &newAgent, nil
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//Generate an empty config filepath
|
|
|
+func GenerateEmptyConfigFile(configFilepath string) error {
|
|
|
+ demoConfig := Agent{}
|
|
|
+ //Stringify the empty struct
|
|
|
+ js, err := json.MarshalIndent(demoConfig, "", " ")
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ //Write to file
|
|
|
+ err = ioutil.WriteFile(configFilepath, js, 0775)
|
|
|
+ return err
|
|
|
+
|
|
|
}
|
|
|
|
|
|
func (a Agent) Name() string {
|
|
@@ -52,8 +89,9 @@ func (a Agent) ConsumerNotification(incomingNotification *notification.Notificat
|
|
|
userEmails := [][]string{}
|
|
|
|
|
|
for _, username := range incomingNotification.Receiver {
|
|
|
- userEmail, ok := a.UsernameToEmailMap[username]
|
|
|
- if ok {
|
|
|
+
|
|
|
+ userEmail, err := a.UsernameToEmail(username)
|
|
|
+ if err == nil {
|
|
|
userEmails = append(userEmails, []string{username, userEmail})
|
|
|
} else {
|
|
|
log.Println("[SMTP Notification] Unable to notify " + username + ": Email not set")
|