p_debugger.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(
  32. ConfigProcessors, defaultProcessor, backendConfig, configType)
  33. if err != nil {
  34. return err
  35. }
  36. config = bcfg.(*debuggerConfig)
  37. return nil
  38. })
  39. Svc.AddInitializer(initFunc)
  40. return func(p Processor) Processor {
  41. return ProcessWith(func(e *mail.Envelope, task SelectTask) (Result, error) {
  42. if task == TaskSaveMail {
  43. if config.LogReceivedMails {
  44. Log().Infof("Mail from: %s / to: %v", e.MailFrom.String(), e.RcptTo)
  45. Log().Info("Headers are:", e.Header)
  46. Log().Info("Body:", e.Data.String())
  47. }
  48. if config.SleepSec > 0 {
  49. Log().Infof("sleeping for %d", config.SleepSec)
  50. time.Sleep(time.Second * time.Duration(config.SleepSec))
  51. Log().Infof("woke up")
  52. if config.SleepSec == 1 {
  53. panic("panic on purpose")
  54. }
  55. }
  56. // continue to the next Processor in the decorator stack
  57. return p.Process(e, task)
  58. } else {
  59. return p.Process(e, task)
  60. }
  61. })
  62. }
  63. }