backend.go 9.3 KB

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