backend.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package backends
  2. import (
  3. "fmt"
  4. "github.com/flashmob/go-guerrilla/log"
  5. "github.com/flashmob/go-guerrilla/mail"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "sync/atomic"
  11. )
  12. var (
  13. Svc *service
  14. // Store the constructor for making an new processor decorator.
  15. processors map[string]ProcessorConstructor
  16. b Backend
  17. )
  18. func init() {
  19. Svc = &service{}
  20. processors = make(map[string]ProcessorConstructor)
  21. }
  22. type ProcessorConstructor func() Decorator
  23. // Backends process received mail. Depending on the implementation, they can store mail in the database,
  24. // write to a file, check for spam, re-transmit to another server, etc.
  25. // Must return an SMTP message (i.e. "250 OK") and a boolean indicating
  26. // whether the message was processed successfully.
  27. type Backend interface {
  28. // Process processes then saves the mail envelope
  29. Process(*mail.Envelope) Result
  30. // ValidateRcpt validates the last recipient that was pushed to the mail envelope
  31. ValidateRcpt(e *mail.Envelope) RcptError
  32. // Initializes the backend, eg. creates folders, sets-up database connections
  33. Initialize(BackendConfig) error
  34. // Initializes the backend after it was Shutdown()
  35. Reinitialize() error
  36. // Shutdown frees / closes anything created during initializations
  37. Shutdown() error
  38. // Start Starts a backend that has been initialized
  39. Start() error
  40. }
  41. type BackendConfig map[string]interface{}
  42. // All config structs extend from this
  43. type BaseConfig interface{}
  44. type notifyMsg struct {
  45. err error
  46. queuedID string
  47. }
  48. // Result represents a response to an SMTP client after receiving DATA.
  49. // The String method should return an SMTP message ready to send back to the
  50. // client, for example `250 OK: Message received`.
  51. type Result interface {
  52. fmt.Stringer
  53. // Code should return the SMTP code associated with this response, ie. `250`
  54. Code() int
  55. }
  56. // Internal implementation of BackendResult for use by backend implementations.
  57. type result string
  58. func (br result) String() string {
  59. return string(br)
  60. }
  61. // Parses the SMTP code from the first 3 characters of the SMTP message.
  62. // Returns 554 if code cannot be parsed.
  63. func (br result) Code() int {
  64. trimmed := strings.TrimSpace(string(br))
  65. if len(trimmed) < 3 {
  66. return 554
  67. }
  68. code, err := strconv.Atoi(trimmed[:3])
  69. if err != nil {
  70. return 554
  71. }
  72. return code
  73. }
  74. func NewResult(message string) Result {
  75. return result(message)
  76. }
  77. type processorInitializer interface {
  78. Initialize(backendConfig BackendConfig) error
  79. }
  80. type processorShutdowner interface {
  81. Shutdown() error
  82. }
  83. type InitializeWith func(backendConfig BackendConfig) error
  84. type ShutdownWith func() error
  85. // Satisfy ProcessorInitializer interface
  86. // So we can now pass an anonymous function that implements ProcessorInitializer
  87. func (i InitializeWith) Initialize(backendConfig BackendConfig) error {
  88. // delegate to the anonymous function
  89. return i(backendConfig)
  90. }
  91. // satisfy ProcessorShutdowner interface, same concept as InitializeWith type
  92. func (s ShutdownWith) Shutdown() error {
  93. // delegate
  94. return s()
  95. }
  96. type Errors []error
  97. // implement the Error interface
  98. func (e Errors) Error() string {
  99. if len(e) == 1 {
  100. return e[0].Error()
  101. }
  102. // multiple errors
  103. msg := ""
  104. for _, err := range e {
  105. msg += "\n" + err.Error()
  106. }
  107. return msg
  108. }
  109. func convertError(name string) error {
  110. return fmt.Errorf("failed to load backend config (%s)", name)
  111. }
  112. type service struct {
  113. initializers []processorInitializer
  114. shutdowners []processorShutdowner
  115. sync.Mutex
  116. mainlog atomic.Value
  117. }
  118. // Get loads the log.logger in an atomic operation. Returns a stderr logger if not able to load
  119. func Log() log.Logger {
  120. if v, ok := Svc.mainlog.Load().(log.Logger); ok {
  121. return v
  122. }
  123. l, _ := log.GetLogger(log.OutputStderr.String(), log.InfoLevel.String())
  124. return l
  125. }
  126. func (s *service) SetMainlog(l log.Logger) {
  127. s.mainlog.Store(l)
  128. }
  129. // AddInitializer adds a function that implements ProcessorShutdowner to be called when initializing
  130. func (s *service) AddInitializer(i processorInitializer) {
  131. s.Lock()
  132. defer s.Unlock()
  133. s.initializers = append(s.initializers, i)
  134. }
  135. // AddShutdowner adds a function that implements ProcessorShutdowner to be called when shutting down
  136. func (s *service) AddShutdowner(sh processorShutdowner) {
  137. s.Lock()
  138. defer s.Unlock()
  139. s.shutdowners = append(s.shutdowners, sh)
  140. }
  141. // reset clears the initializers and Shutdowners
  142. func (s *service) reset() {
  143. s.shutdowners = make([]processorShutdowner, 0)
  144. s.initializers = make([]processorInitializer, 0)
  145. }
  146. // Initialize initializes all the processors one-by-one and returns any errors.
  147. // Subsequent calls to Initialize will not call the initializer again unless it failed on the previous call
  148. // so Initialize may be called again to retry after getting errors
  149. func (s *service) initialize(backend BackendConfig) Errors {
  150. s.Lock()
  151. defer s.Unlock()
  152. var errors Errors
  153. failed := make([]processorInitializer, 0)
  154. for i := range s.initializers {
  155. if err := s.initializers[i].Initialize(backend); err != nil {
  156. errors = append(errors, err)
  157. failed = append(failed, s.initializers[i])
  158. }
  159. }
  160. // keep only the failed initializers
  161. s.initializers = failed
  162. return errors
  163. }
  164. // Shutdown shuts down all the processors by calling their shutdowners (if any)
  165. // Subsequent calls to Shutdown will not call the shutdowners again unless it failed on the previous call
  166. // so Shutdown may be called again to retry after getting errors
  167. func (s *service) shutdown() Errors {
  168. s.Lock()
  169. defer s.Unlock()
  170. var errors Errors
  171. failed := make([]processorShutdowner, 0)
  172. for i := range s.shutdowners {
  173. if err := s.shutdowners[i].Shutdown(); err != nil {
  174. errors = append(errors, err)
  175. failed = append(failed, s.shutdowners[i])
  176. }
  177. }
  178. s.shutdowners = failed
  179. return errors
  180. }
  181. // AddProcessor adds a new processor, which becomes available to the backend_config.save_process option
  182. // and also the backend_config.validate_process option
  183. // Use to add your own custom processor when using backends as a package, or after importing an external
  184. // processor.
  185. func (s *service) AddProcessor(name string, p ProcessorConstructor) {
  186. // wrap in a constructor since we want to defer calling it
  187. var c ProcessorConstructor
  188. c = func() Decorator {
  189. return p()
  190. }
  191. // add to our processors list
  192. processors[strings.ToLower(name)] = c
  193. }
  194. // extractConfig loads the backend config. It has already been unmarshalled
  195. // configData contains data from the main config file's "backend_config" value
  196. // configType is a Processor's specific config value.
  197. // The reason why using reflection is because we'll get a nice error message if the field is missing
  198. // the alternative solution would be to json.Marshal() and json.Unmarshal() however that will not give us any
  199. // error messages
  200. func (s *service) ExtractConfig(configData BackendConfig, configType BaseConfig) (interface{}, error) {
  201. // Use reflection so that we can provide a nice error message
  202. v := reflect.ValueOf(configType).Elem() // so that we can set the values
  203. //m := reflect.ValueOf(configType).Elem()
  204. t := reflect.TypeOf(configType).Elem()
  205. typeOfT := v.Type()
  206. for i := 0; i < v.NumField(); i++ {
  207. f := v.Field(i)
  208. // read the tags of the config struct
  209. field_name := t.Field(i).Tag.Get("json")
  210. omitempty := false
  211. if len(field_name) > 0 {
  212. // parse the tag to
  213. // get the field name from struct tag
  214. split := strings.Split(field_name, ",")
  215. field_name = split[0]
  216. if len(split) > 1 {
  217. if split[1] == "omitempty" {
  218. omitempty = true
  219. }
  220. }
  221. } else {
  222. // could have no tag
  223. // so use the reflected field name
  224. field_name = typeOfT.Field(i).Name
  225. }
  226. if f.Type().Name() == "int" {
  227. // in json, there is no int, only floats...
  228. if intVal, converted := configData[field_name].(float64); converted {
  229. v.Field(i).SetInt(int64(intVal))
  230. } else if intVal, converted := configData[field_name].(int); converted {
  231. v.Field(i).SetInt(int64(intVal))
  232. } else if !omitempty {
  233. return configType, convertError("property missing/invalid: '" + field_name + "' of expected type: " + f.Type().Name())
  234. }
  235. }
  236. if f.Type().Name() == "string" {
  237. if stringVal, converted := configData[field_name].(string); converted {
  238. v.Field(i).SetString(stringVal)
  239. } else if !omitempty {
  240. return configType, convertError("missing/invalid: '" + field_name + "' of type: " + f.Type().Name())
  241. }
  242. }
  243. if f.Type().Name() == "bool" {
  244. if boolVal, converted := configData[field_name].(bool); converted {
  245. v.Field(i).SetBool(boolVal)
  246. } else if !omitempty {
  247. return configType, convertError("missing/invalid: '" + field_name + "' of type: " + f.Type().Name())
  248. }
  249. }
  250. }
  251. return configType, nil
  252. }