web_push.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. log "github.com/sirupsen/logrus"
  6. "net/http"
  7. "os"
  8. "pmail/config"
  9. "pmail/dto/parsemail"
  10. "pmail/hooks/framework"
  11. "pmail/models"
  12. "pmail/utils/context"
  13. )
  14. type WebPushHook struct {
  15. url string
  16. token string
  17. }
  18. func (w *WebPushHook) ReceiveSaveAfter(ctx *context.Context, email *parsemail.Email, ue []*models.UserEmail) {
  19. if w.url == "" {
  20. return
  21. }
  22. content := string(email.Text)
  23. if content == "" {
  24. content = email.Subject
  25. }
  26. webhookURL := w.url // 替换为您的 Webhook URL
  27. to := make([]string, len(email.To))
  28. for i, user := range email.To {
  29. to[i] = user.EmailAddress
  30. }
  31. data := EmailData{
  32. From: email.From.EmailAddress,
  33. To: to,
  34. Subject: email.Subject,
  35. Body: content,
  36. Token: w.token,
  37. }
  38. jsonData, err := json.Marshal(data)
  39. if err != nil {
  40. log.WithContext(ctx).Errorf("web push error %+v", err)
  41. }
  42. resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData))
  43. if err != nil {
  44. log.WithContext(ctx).Errorf("web push error %+v", err)
  45. }
  46. defer resp.Body.Close()
  47. }
  48. // EmailData 用于存储解析后的邮件数据
  49. type EmailData struct {
  50. From string `json:"from"`
  51. To []string `json:"to"`
  52. Subject string `json:"subject"`
  53. Body string `json:"body"`
  54. Token string `json:"token"`
  55. }
  56. func (w *WebPushHook) SendBefore(ctx *context.Context, email *parsemail.Email) {
  57. }
  58. func (w *WebPushHook) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {
  59. }
  60. func (w *WebPushHook) ReceiveParseBefore(ctx *context.Context, email *[]byte) {
  61. }
  62. func (w *WebPushHook) ReceiveParseAfter(ctx *context.Context, email *parsemail.Email) {
  63. }
  64. type Config struct {
  65. WebPushUrl string `json:"webPushUrl"`
  66. WebPushToken string `json:"webPushToken"`
  67. }
  68. func NewWebPushHook() *WebPushHook {
  69. var cfgData []byte
  70. var err error
  71. cfgData, err = os.ReadFile("./config/config.json")
  72. if err != nil {
  73. panic(err)
  74. }
  75. var mainConfig *config.Config
  76. err = json.Unmarshal(cfgData, &mainConfig)
  77. if err != nil {
  78. panic(err)
  79. }
  80. var pluginConfig *Config
  81. if _, err := os.Stat("./plugins/web_push_config.json"); err == nil {
  82. cfgData, err = os.ReadFile("./plugins/web_push_config.json")
  83. if err != nil {
  84. panic(err)
  85. }
  86. err = json.Unmarshal(cfgData, &pluginConfig)
  87. if err != nil {
  88. panic(err)
  89. }
  90. }
  91. token := ""
  92. pushURL := ""
  93. if pluginConfig != nil {
  94. pushURL = pluginConfig.WebPushUrl
  95. token = pluginConfig.WebPushToken
  96. } else {
  97. pushURL = mainConfig.WebPushUrl
  98. token = mainConfig.WebPushToken
  99. }
  100. ret := &WebPushHook{
  101. url: pushURL,
  102. token: token,
  103. }
  104. return ret
  105. }
  106. func main() {
  107. framework.CreatePlugin("web_push", NewWebPushHook()).Run()
  108. }