p_debugger.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package backends
  2. import (
  3. "github.com/flashmob/go-guerrilla/mail"
  4. "strings"
  5. "time"
  6. )
  7. // ----------------------------------------------------------------------------------
  8. // Processor Name: debugger
  9. // ----------------------------------------------------------------------------------
  10. // Description : Log received emails
  11. // ----------------------------------------------------------------------------------
  12. // Config Options: log_received_mails bool - log if true
  13. // --------------:-------------------------------------------------------------------
  14. // Input : e.MailFrom, e.RcptTo, e.Header
  15. // ----------------------------------------------------------------------------------
  16. // Output : none (only output to the log if enabled)
  17. // ----------------------------------------------------------------------------------
  18. func init() {
  19. processors[strings.ToLower(defaultProcessor)] = func() Decorator {
  20. return Debugger()
  21. }
  22. }
  23. type debuggerConfig struct {
  24. LogReceivedMails bool `json:"log_received_mails"`
  25. SleepSec int `json:"sleep_seconds,omitempty"`
  26. }
  27. func Debugger() Decorator {
  28. var config *debuggerConfig
  29. initFunc := InitializeWith(func(backendConfig BackendConfig) error {
  30. configType := BaseConfig(&debuggerConfig{})
  31. bcfg, err := Svc.ExtractConfig(backendConfig, configType)
  32. if err != nil {
  33. return err
  34. }
  35. config = bcfg.(*debuggerConfig)
  36. return nil
  37. })
  38. Svc.AddInitializer(initFunc)
  39. return func(p Processor) Processor {
  40. return ProcessWith(func(e *mail.Envelope, task SelectTask) (Result, error) {
  41. if task == TaskSaveMail {
  42. if config.LogReceivedMails {
  43. Log().Infof("Mail from: %s / to: %v", e.MailFrom.String(), e.RcptTo)
  44. Log().Info("Headers are:", e.Header)
  45. }
  46. if config.SleepSec > 0 {
  47. Log().Infof("sleeping for %d", config.SleepSec)
  48. time.Sleep(time.Second * time.Duration(config.SleepSec))
  49. Log().Infof("woke up")
  50. if config.SleepSec == 1 {
  51. panic("panic on purpose")
  52. }
  53. }
  54. // continue to the next Processor in the decorator stack
  55. return p.Process(e, task)
  56. } else {
  57. return p.Process(e, task)
  58. }
  59. })
  60. }
  61. }