spam_block.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/Jinnrry/pmail/dto/parsemail"
  6. "github.com/Jinnrry/pmail/hooks/framework"
  7. "github.com/Jinnrry/pmail/hooks/spam_block/tools"
  8. "github.com/Jinnrry/pmail/models"
  9. "github.com/Jinnrry/pmail/utils/context"
  10. log "github.com/sirupsen/logrus"
  11. "io"
  12. "net/http"
  13. "os"
  14. "strings"
  15. "time"
  16. )
  17. type SpamBlock struct {
  18. cfg SpamBlockConfig
  19. hc *http.Client
  20. }
  21. func (s SpamBlock) SendBefore(ctx *context.Context, email *parsemail.Email) {
  22. }
  23. func (s SpamBlock) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {
  24. }
  25. func (s SpamBlock) ReceiveParseBefore(ctx *context.Context, email *[]byte) {
  26. }
  27. type ModelResponse struct {
  28. Predictions [][]float64 `json:"predictions"`
  29. }
  30. type ApiRequest struct {
  31. Instances []InstanceItem `json:"instances"`
  32. }
  33. type InstanceItem struct {
  34. Token []string `json:"token"`
  35. }
  36. func (s SpamBlock) ReceiveParseAfter(ctx *context.Context, email *parsemail.Email) {
  37. reqData := ApiRequest{
  38. Instances: []InstanceItem{
  39. {
  40. Token: []string{
  41. fmt.Sprintf("%s %s", email.Subject, tools.Trim(tools.TrimHtml(string(email.HTML)))),
  42. },
  43. },
  44. },
  45. }
  46. str, _ := json.Marshal(reqData)
  47. resp, err := s.hc.Post(s.cfg.ApiURL, "application/json", strings.NewReader(string(str)))
  48. if err != nil {
  49. log.Errorf("API Error: %v", err)
  50. return
  51. }
  52. body, _ := io.ReadAll(resp.Body)
  53. modelResponse := ModelResponse{}
  54. err = json.Unmarshal(body, &modelResponse)
  55. if err != nil {
  56. log.WithContext(ctx).Errorf("API Error: %v", err)
  57. return
  58. }
  59. if len(modelResponse.Predictions) == 0 {
  60. log.WithContext(ctx).Errorf("API Response Error: %v", string(body))
  61. return
  62. }
  63. classes := modelResponse.Predictions[0]
  64. if len(classes) != 3 {
  65. return
  66. }
  67. var maxScore float64
  68. var maxClass int
  69. for i, score := range classes {
  70. if score > maxScore {
  71. maxScore = score
  72. maxClass = i
  73. }
  74. }
  75. switch maxClass {
  76. case 0:
  77. log.WithContext(ctx).Infof("[Spam Check Result: Normal] %s", email.Subject)
  78. case 1:
  79. log.WithContext(ctx).Infof("[Spam Check Result: Spam ] %s", email.Subject)
  80. case 2:
  81. log.WithContext(ctx).Infof("[Spam Check Result: Blackmail ] %s", email.Subject)
  82. }
  83. if maxClass != 0 {
  84. email.Status = 3
  85. }
  86. }
  87. func (s SpamBlock) ReceiveSaveAfter(ctx *context.Context, email *parsemail.Email, ue []*models.UserEmail) {
  88. }
  89. type SpamBlockConfig struct {
  90. ApiURL string `json:"apiURL"`
  91. ApiTimeout int `json:"apiTimeout"` // 单位毫秒
  92. }
  93. func NewSpamBlockHook() *SpamBlock {
  94. var pluginConfig SpamBlockConfig
  95. if _, err := os.Stat("./plugins/spam_block_config.json"); err == nil {
  96. cfgData, err := os.ReadFile("./plugins/spam_block_config.json")
  97. if err == nil {
  98. json.Unmarshal(cfgData, &pluginConfig)
  99. }
  100. } else {
  101. log.Infof("No Config file found")
  102. return nil
  103. }
  104. log.Infof("Config: %+v", pluginConfig)
  105. if pluginConfig.ApiURL == "" {
  106. pluginConfig.ApiURL = "http://localhost:8501/v1/models/emotion_model:predict"
  107. }
  108. if pluginConfig.ApiTimeout == 0 {
  109. pluginConfig.ApiTimeout = 3000
  110. }
  111. hc := &http.Client{
  112. Timeout: time.Duration(pluginConfig.ApiTimeout) * time.Millisecond,
  113. }
  114. return &SpamBlock{
  115. cfg: pluginConfig,
  116. hc: hc,
  117. }
  118. }
  119. func main() {
  120. log.Infof("SpamBlockPlug Star Success")
  121. instance := NewSpamBlockHook()
  122. if instance == nil {
  123. return
  124. }
  125. framework.CreatePlugin("spam_block", instance).Run()
  126. }