web_push.go 2.6 KB

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