notification.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "log"
  4. "strconv"
  5. "time"
  6. notification "imuslab.com/arozos/mod/notification"
  7. "imuslab.com/arozos/mod/notification/agents/smtpn"
  8. )
  9. var notificationQueue *notification.NotificationQueue
  10. func notificationInit() {
  11. //Create a new notification agent
  12. notificationQueue = notification.NewNotificationQueue()
  13. //Register the notification agents
  14. //SMTP Mail Sender
  15. //Load username and their email from authAgent
  16. userEmailmap := map[string]string{}
  17. allRecords := registerHandler.ListAllUserEmails()
  18. for _, userRercord := range allRecords {
  19. if userRercord[2].(bool) {
  20. userEmailmap[userRercord[0].(string)] = userRercord[1].(string)
  21. }
  22. }
  23. smtpnConfigPath := "./system/smtp_conf.json"
  24. if !fileExists(smtpnConfigPath) {
  25. //Create an empty one
  26. smtpn.GenerateEmptyConfigFile(smtpnConfigPath)
  27. }
  28. smtpAgent, err := smtpn.NewSMTPNotificationAgent(*host_name, smtpnConfigPath,
  29. func(username string) (string, error) {
  30. //Translate username to email
  31. return registerHandler.GetUserEmail(username)
  32. })
  33. if err != nil {
  34. log.Println("[Notification/SMTPN] Unable to start smtpn agent: " + err.Error())
  35. } else {
  36. notificationQueue.RegisterNotificationAgent(smtpAgent)
  37. }
  38. //Create and register other notification agents
  39. go func() {
  40. time.Sleep(10 * time.Second)
  41. notificationQueue.BroadcastNotification(&notification.NotificationPayload{
  42. ID: strconv.Itoa(int(time.Now().Unix())),
  43. Title: "Email Test",
  44. Message: "This is a testing notification for showcasing a sample email when DISK SMART error was scanned and discovered.<br> Please visit <a href='https://blog.teacat.io'>here</a> for more information.",
  45. Receiver: []string{"TC"},
  46. Sender: "SMART Nightly Scanner",
  47. ReciverAgents: []string{"smtpn"},
  48. })
  49. }()
  50. }