store.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package chunk
  2. import (
  3. "github.com/flashmob/go-guerrilla/backends"
  4. "github.com/flashmob/go-guerrilla/mail"
  5. "github.com/flashmob/go-guerrilla/mail/smtp"
  6. "io"
  7. "net"
  8. "time"
  9. )
  10. func init() {
  11. StorageEngines = make(map[string]StorageEngineConstructor)
  12. }
  13. // Storage defines an interface to the storage layer (the database)
  14. type Storage interface {
  15. // OpenMessage is used to begin saving an email. An email id is returned and used to call CloseMessage later
  16. OpenMessage(
  17. queuedID mail.Hash128,
  18. from string,
  19. helo string,
  20. recipient string,
  21. ipAddress IPAddr,
  22. returnPath string,
  23. protocol mail.Protocol,
  24. transport smtp.TransportType) (mailID uint64, err error)
  25. // CloseMessage finalizes the writing of an email. Additional data collected while parsing the email is saved
  26. CloseMessage(
  27. mailID uint64,
  28. size int64,
  29. partsInfo *PartsInfo,
  30. subject string,
  31. to string,
  32. from string) error
  33. // AddChunk saves a chunk of bytes to a given hash key
  34. AddChunk(data []byte, hash []byte) error
  35. // GetEmail returns an email that's been saved
  36. GetMessage(mailID uint64) (*Email, error)
  37. // GetChunks loads in the specified chunks of bytes from storage
  38. GetChunks(hash ...HashKey) ([]*Chunk, error)
  39. // Initialize is called when the backend is started
  40. Initialize(cfg backends.ConfigGroup) error
  41. // Shutdown is called when the backend gets shutdown.
  42. Shutdown() (err error)
  43. }
  44. // StorageEngines contains the constructors for creating instances that implement Storage
  45. // To add your own Storage, create your own Storage struct, then add your constructor to
  46. // this `StorageEngines` map. Enable it via the configuration (the `storage_engine` setting)
  47. var StorageEngines map[string]StorageEngineConstructor
  48. type StorageEngineConstructor func() Storage
  49. // Email represents an email
  50. type Email struct {
  51. mailID uint64
  52. createdAt time.Time
  53. size int64
  54. from string // from stores the email address found in the "From" header field
  55. to string // to stores the email address found in the "From" header field
  56. partsInfo PartsInfo
  57. helo string // helo message given by the client when the message was transmitted
  58. subject string // subject stores the value from the first "Subject" header field
  59. queuedID string
  60. recipient string // recipient is the email address that the server received from the RCPT TO command
  61. ipv4 IPAddr // set to a value if client connected via ipv4
  62. ipv6 IPAddr // set to a value if client connected via ipv6
  63. returnPath string // returnPath is the email address that the server received from the MAIL FROM command
  64. protocol mail.Protocol // protocol such as SMTP, ESTMP, ESMTPS
  65. transport smtp.TransportType // transport what type of transport the message uses, eg 8bitmime
  66. }
  67. type Chunk struct {
  68. modifiedAt time.Time
  69. referenceCount uint // referenceCount counts how many emails reference this chunk
  70. data io.Reader
  71. }
  72. type IPAddr struct {
  73. net.IPAddr
  74. }