notification.go 1.9 KB

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