smtp_server.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package server
  2. import (
  3. "bytes"
  4. "errors"
  5. "github.com/emersion/go-smtp"
  6. "io"
  7. "mime"
  8. "mime/multipart"
  9. "net/mail"
  10. "strings"
  11. "sync"
  12. )
  13. var (
  14. errInvalidDomain = errors.New("invalid domain")
  15. errInvalidAddress = errors.New("invalid address")
  16. errInvalidTopic = errors.New("invalid topic")
  17. errTooManyRecipients = errors.New("too many recipients")
  18. errUnsupportedContentType = errors.New("unsupported content type")
  19. )
  20. // smtpBackend implements SMTP server methods.
  21. type smtpBackend struct {
  22. config *Config
  23. sub subscriber
  24. success int64
  25. failure int64
  26. mu sync.Mutex
  27. }
  28. func newMailBackend(conf *Config, sub subscriber) *smtpBackend {
  29. return &smtpBackend{
  30. config: conf,
  31. sub: sub,
  32. }
  33. }
  34. func (b *smtpBackend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
  35. return &smtpSession{backend: b}, nil
  36. }
  37. func (b *smtpBackend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
  38. return &smtpSession{backend: b}, nil
  39. }
  40. func (b *smtpBackend) Counts() (success int64, failure int64) {
  41. b.mu.Lock()
  42. defer b.mu.Unlock()
  43. return b.success, b.failure
  44. }
  45. // smtpSession is returned after EHLO.
  46. type smtpSession struct {
  47. backend *smtpBackend
  48. topic string
  49. mu sync.Mutex
  50. }
  51. func (s *smtpSession) AuthPlain(username, password string) error {
  52. return nil
  53. }
  54. func (s *smtpSession) Mail(from string, opts smtp.MailOptions) error {
  55. return nil
  56. }
  57. func (s *smtpSession) Rcpt(to string) error {
  58. return s.withFailCount(func() error {
  59. conf := s.backend.config
  60. addressList, err := mail.ParseAddressList(to)
  61. if err != nil {
  62. return err
  63. } else if len(addressList) != 1 {
  64. return errTooManyRecipients
  65. }
  66. to = addressList[0].Address
  67. if !strings.HasSuffix(to, "@"+conf.SMTPServerDomain) {
  68. return errInvalidDomain
  69. }
  70. to = strings.TrimSuffix(to, "@"+conf.SMTPServerDomain)
  71. if conf.SMTPServerAddrPrefix != "" {
  72. if !strings.HasPrefix(to, conf.SMTPServerAddrPrefix) {
  73. return errInvalidAddress
  74. }
  75. to = strings.TrimPrefix(to, conf.SMTPServerAddrPrefix)
  76. }
  77. if !topicRegex.MatchString(to) {
  78. return errInvalidTopic
  79. }
  80. s.mu.Lock()
  81. s.topic = to
  82. s.mu.Unlock()
  83. return nil
  84. })
  85. }
  86. func (s *smtpSession) Data(r io.Reader) error {
  87. return s.withFailCount(func() error {
  88. conf := s.backend.config
  89. b, err := io.ReadAll(r) // Protected by MaxMessageBytes
  90. if err != nil {
  91. return err
  92. }
  93. msg, err := mail.ReadMessage(bytes.NewReader(b))
  94. if err != nil {
  95. return err
  96. }
  97. body, err := readMailBody(msg)
  98. if err != nil {
  99. return err
  100. }
  101. if len(body) > conf.MessageLimit {
  102. body = body[:conf.MessageLimit]
  103. }
  104. m := newDefaultMessage(s.topic, body)
  105. subject := msg.Header.Get("Subject")
  106. if subject != "" {
  107. dec := mime.WordDecoder{}
  108. subject, err := dec.DecodeHeader(subject)
  109. if err != nil {
  110. return err
  111. }
  112. m.Title = subject
  113. }
  114. if err := s.backend.sub(m); err != nil {
  115. return err
  116. }
  117. s.backend.mu.Lock()
  118. s.backend.success++
  119. s.backend.mu.Unlock()
  120. return nil
  121. })
  122. }
  123. func (s *smtpSession) Reset() {
  124. s.mu.Lock()
  125. s.topic = ""
  126. s.mu.Unlock()
  127. }
  128. func (s *smtpSession) Logout() error {
  129. return nil
  130. }
  131. func (s *smtpSession) withFailCount(fn func() error) error {
  132. err := fn()
  133. s.backend.mu.Lock()
  134. defer s.backend.mu.Unlock()
  135. if err != nil {
  136. s.backend.failure++
  137. }
  138. return err
  139. }
  140. func readMailBody(msg *mail.Message) (string, error) {
  141. contentType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
  142. if err != nil {
  143. return "", err
  144. }
  145. if contentType == "text/plain" {
  146. body, err := io.ReadAll(msg.Body)
  147. if err != nil {
  148. return "", err
  149. }
  150. return string(body), nil
  151. }
  152. if strings.HasPrefix(contentType, "multipart/") {
  153. mr := multipart.NewReader(msg.Body, params["boundary"])
  154. for {
  155. part, err := mr.NextPart()
  156. if err != nil { // may be io.EOF
  157. return "", err
  158. }
  159. partContentType, _, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
  160. if err != nil {
  161. return "", err
  162. }
  163. if partContentType != "text/plain" {
  164. continue
  165. }
  166. body, err := io.ReadAll(part)
  167. if err != nil {
  168. return "", err
  169. }
  170. return string(body), nil
  171. }
  172. }
  173. return "", errUnsupportedContentType
  174. }