processor.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package backends
  2. import (
  3. "github.com/flashmob/go-guerrilla/mail"
  4. "io"
  5. )
  6. type SelectTask int
  7. const (
  8. TaskSaveMail SelectTask = iota
  9. TaskValidateRcpt
  10. TaskSaveMailStream
  11. )
  12. func (o SelectTask) String() string {
  13. switch o {
  14. case TaskSaveMail:
  15. return "save mail"
  16. case TaskValidateRcpt:
  17. return "validate recipient"
  18. case TaskSaveMailStream:
  19. return "save mail stream"
  20. }
  21. return "[unnamed task]"
  22. }
  23. var BackendResultOK = NewResult("200 OK")
  24. // Our processor is defined as something that processes the envelope and returns a result and error
  25. type Processor interface {
  26. Process(*mail.Envelope, SelectTask) (Result, error)
  27. }
  28. // Signature of Processor
  29. type ProcessWith func(*mail.Envelope, SelectTask) (Result, error)
  30. // Make ProcessWith will satisfy the Processor interface
  31. func (f ProcessWith) Process(e *mail.Envelope, task SelectTask) (Result, error) {
  32. // delegate to the anonymous function
  33. return f(e, task)
  34. }
  35. // DefaultProcessor is a undecorated worker that does nothing
  36. // Notice DefaultProcessor has no knowledge of the other decorators that have orthogonal concerns.
  37. type DefaultProcessor struct{}
  38. // do nothing except return the result
  39. // (this is the last call in the decorator stack, if it got here, then all is good)
  40. func (w DefaultProcessor) Process(e *mail.Envelope, task SelectTask) (Result, error) {
  41. return BackendResultOK, nil
  42. }
  43. // if no processors specified, skip operation
  44. type NoopProcessor struct{ DefaultProcessor }
  45. type StreamProcessor interface {
  46. io.Writer
  47. }
  48. type StreamProcessWith func(p []byte) (n int, err error)
  49. func (f StreamProcessWith) Write(p []byte) (n int, err error) {
  50. // delegate to the anonymous function
  51. return f(p)
  52. }
  53. type DefaultStreamProcessor struct{}
  54. func (w DefaultStreamProcessor) Write(p []byte) (n int, err error) {
  55. return len(p), nil
  56. }
  57. // NoopStreamProcessor does nothing, it's a placeholder when no stream processors have been configured
  58. type NoopStreamProcessor struct{ DefaultStreamProcessor }
  59. type ValidatingProcessor interface {
  60. Processor
  61. }