p_debugger.go 2.5 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. // : sleep_seconds - how many seconds to pause for, useful to force a
  14. // : timeout. If sleep_seconds is 1 then a panic will be induced
  15. // --------------:-------------------------------------------------------------------
  16. // Input : e.MailFrom, e.RcptTo, e.Header
  17. // ----------------------------------------------------------------------------------
  18. // Output : none (only output to the log if enabled)
  19. // ----------------------------------------------------------------------------------
  20. func init() {
  21. processors[strings.ToLower(defaultProcessor)] = func() Decorator {
  22. return Debugger()
  23. }
  24. }
  25. type debuggerConfig struct {
  26. LogReceivedMails bool `json:"log_received_mails"`
  27. SleepSec int `json:"sleep_seconds,omitempty"`
  28. }
  29. func Debugger() Decorator {
  30. var config *debuggerConfig
  31. initFunc := InitializeWith(func(backendConfig BackendConfig) error {
  32. configType := BaseConfig(&debuggerConfig{})
  33. bcfg, err := Svc.ExtractConfig(
  34. ConfigProcessors, defaultProcessor, backendConfig, configType)
  35. if err != nil {
  36. return err
  37. }
  38. config = bcfg.(*debuggerConfig)
  39. return nil
  40. })
  41. Svc.AddInitializer(initFunc)
  42. return func(p Processor) Processor {
  43. return ProcessWith(func(e *mail.Envelope, task SelectTask) (Result, error) {
  44. if task == TaskSaveMail {
  45. if config.LogReceivedMails {
  46. Log().Fields("queuedID", e.QueuedId, "from", e.MailFrom.String(), "to", e.RcptTo).Info("save mail")
  47. Log().Fields("queuedID", e.QueuedId, "headers", e.Header).Info("headers dump")
  48. Log().Fields("queuedID", e.QueuedId, "body", e.Data.String()).Info("body dump")
  49. }
  50. if config.SleepSec > 0 {
  51. Log().Fields("queuedID", e.QueuedId, "sleep", config.SleepSec).Info("sleeping")
  52. time.Sleep(time.Second * time.Duration(config.SleepSec))
  53. Log().Fields("queuedID", e.QueuedId).Info("woke up")
  54. if config.SleepSec == 1 {
  55. panic("panic on purpose")
  56. }
  57. }
  58. // continue to the next Processor in the decorator stack
  59. return p.Process(e, task)
  60. } else {
  61. return p.Process(e, task)
  62. }
  63. })
  64. }
  65. }