notification.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package main
  2. import (
  3. "log"
  4. "strconv"
  5. "time"
  6. fs "imuslab.com/arozos/mod/filesystem"
  7. notification "imuslab.com/arozos/mod/notification"
  8. "imuslab.com/arozos/mod/notification/agents/smtpn"
  9. )
  10. var notificationQueue *notification.NotificationQueue
  11. func notificationInit() {
  12. //Create a new notification agent
  13. notificationQueue = notification.NewNotificationQueue()
  14. //Register the notification agents
  15. /*
  16. SMTP Notification Agent
  17. For handling notification sending via Mail
  18. */
  19. //Load username and their email from authAgent
  20. userEmailmap := map[string]string{}
  21. allRecords := registerHandler.ListAllUserEmails()
  22. for _, userRercord := range allRecords {
  23. if userRercord[2].(bool) {
  24. userEmailmap[userRercord[0].(string)] = userRercord[1].(string)
  25. }
  26. }
  27. smtpnConfigPath := "./system/smtp_conf.json"
  28. if !fs.FileExists(smtpnConfigPath) {
  29. //Create an empty one
  30. smtpn.GenerateEmptyConfigFile(smtpnConfigPath)
  31. }
  32. smtpAgent, err := smtpn.NewSMTPNotificationAgent(*host_name, smtpnConfigPath,
  33. func(username string) (string, error) {
  34. //Translate username to email
  35. return registerHandler.GetUserEmail(username)
  36. })
  37. if err != nil {
  38. log.Println("[Notification/SMTPN] Unable to start smtpn agent: " + err.Error())
  39. } else {
  40. notificationQueue.RegisterNotificationAgent(smtpAgent)
  41. }
  42. //Create and register other notification agents
  43. go func() {
  44. time.Sleep(10 * time.Second)
  45. return
  46. notificationQueue.BroadcastNotification(&notification.NotificationPayload{
  47. ID: strconv.Itoa(int(time.Now().Unix())),
  48. Title: "Email Test",
  49. 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.",
  50. Receiver: []string{"TC"},
  51. Sender: "SMART Nightly Scanner",
  52. ReciverAgents: []string{"smtpn"},
  53. })
  54. }()
  55. }