outbox.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package email
  2. /*
  3. outbox.go
  4. Delayed and scheduled delivery.
  5. "Undo send" and "Send later" both park the rendered message here. The raw
  6. message is spooled to a 0600 file under the mail data folder and a record
  7. describes who sends it, when, and what to do afterwards. A background
  8. ticker delivers due items from the server, so closing the browser never
  9. loses or blocks a message.
  10. Failed deliveries are retried with a growing delay; after the last attempt
  11. the item stays in the outbox as failed and the user is notified.
  12. */
  13. import (
  14. "context"
  15. "encoding/json"
  16. "errors"
  17. "os"
  18. "path/filepath"
  19. "sort"
  20. "strings"
  21. "sync"
  22. "time"
  23. "github.com/emersion/go-imap/v2/imapclient"
  24. "imuslab.com/arozos/mod/info/logger"
  25. )
  26. var retryDelays = []time.Duration{time.Minute, 5 * time.Minute, 15 * time.Minute, time.Hour}
  27. // OutboxItem is a queued message.
  28. type OutboxItem struct {
  29. ID string `json:"id"`
  30. Owner string `json:"owner"`
  31. Admin bool `json:"admin"` //Network policy of the user who queued it
  32. AccountID string `json:"accountId"`
  33. Subject string `json:"subject"`
  34. To []string `json:"to"`
  35. From string `json:"from"`
  36. Recipients []string `json:"recipients"`
  37. MessageID string `json:"messageId"`
  38. Created int64 `json:"created"`
  39. SendAt int64 `json:"sendAt"` //Unix ms
  40. Status string `json:"status"` //queued | sending | failed
  41. Attempts int `json:"attempts"`
  42. Error string `json:"error,omitempty"`
  43. Post postSend `json:"post"`
  44. Date int64 `json:"date"`
  45. }
  46. // OutboxView is what the browser sees.
  47. type OutboxView struct {
  48. ID string `json:"id"`
  49. AccountID string `json:"accountId"`
  50. Subject string `json:"subject"`
  51. To []string `json:"to"`
  52. SendAt int64 `json:"sendAt"`
  53. Status string `json:"status"`
  54. Attempts int `json:"attempts"`
  55. Error string `json:"error,omitempty"`
  56. Created int64 `json:"created"`
  57. }
  58. type outbox struct {
  59. manager *Manager
  60. spoolDir string
  61. mutex sync.Mutex
  62. busy bool
  63. }
  64. func newOutbox(manager *Manager, spoolDir string) *outbox {
  65. box := &outbox{manager: manager, spoolDir: spoolDir}
  66. //Items interrupted mid-delivery by a restart are retried
  67. items, err := box.all()
  68. if err == nil {
  69. for _, item := range items {
  70. if item.Status == "sending" {
  71. item.Status = "queued"
  72. box.save(item)
  73. }
  74. }
  75. }
  76. return box
  77. }
  78. func (o *outbox) spoolPath(id string) string {
  79. return filepath.Join(o.spoolDir, id+".eml")
  80. }
  81. func (o *outbox) save(item *OutboxItem) error {
  82. return o.manager.store.db.Write(tableOutbox, ownerKey(item.Owner, item.ID), item)
  83. }
  84. func (o *outbox) all() ([]*OutboxItem, error) {
  85. entries, err := o.manager.store.db.ListTable(tableOutbox)
  86. if err != nil {
  87. return nil, err
  88. }
  89. items := []*OutboxItem{}
  90. for _, entry := range entries {
  91. if len(entry) < 2 {
  92. continue
  93. }
  94. item := &OutboxItem{}
  95. if err := json.Unmarshal(entry[1], item); err != nil || item.ID == "" {
  96. continue
  97. }
  98. items = append(items, item)
  99. }
  100. return items, nil
  101. }
  102. func (o *outbox) get(owner string, id string) (*OutboxItem, error) {
  103. key := ownerKey(owner, id)
  104. if !o.manager.store.db.KeyExists(tableOutbox, key) {
  105. return nil, errors.New("this message is no longer in the outbox")
  106. }
  107. item := &OutboxItem{}
  108. if err := o.manager.store.db.Read(tableOutbox, key, item); err != nil {
  109. return nil, err
  110. }
  111. if item.Owner != owner {
  112. return nil, errors.New("this message is no longer in the outbox")
  113. }
  114. return item, nil
  115. }
  116. func (o *outbox) remove(item *OutboxItem) {
  117. o.manager.store.db.Delete(tableOutbox, ownerKey(item.Owner, item.ID))
  118. os.Remove(o.spoolPath(item.ID))
  119. }
  120. func (o *outbox) enqueue(p Principal, account *Account, built *builtMessage, post postSend, sendAt time.Time, subject string, to []string) (*OutboxItem, error) {
  121. item := &OutboxItem{
  122. ID: randomID(10),
  123. Owner: p.Username,
  124. Admin: p.Admin,
  125. AccountID: account.ID,
  126. Subject: strings.TrimSpace(subject),
  127. To: to,
  128. From: built.from,
  129. Recipients: built.recipients,
  130. MessageID: built.messageID,
  131. Created: o.manager.now().UnixMilli(),
  132. SendAt: sendAt.UnixMilli(),
  133. Status: "queued",
  134. Post: post,
  135. Date: built.date.Unix(),
  136. }
  137. if err := os.WriteFile(o.spoolPath(item.ID), built.raw, 0600); err != nil {
  138. return nil, err
  139. }
  140. if err := o.save(item); err != nil {
  141. os.Remove(o.spoolPath(item.ID))
  142. return nil, err
  143. }
  144. return item, nil
  145. }
  146. // processDue delivers every item whose time has come.
  147. func (o *outbox) processDue(ctx context.Context) {
  148. o.mutex.Lock()
  149. if o.busy {
  150. o.mutex.Unlock()
  151. return
  152. }
  153. o.busy = true
  154. o.mutex.Unlock()
  155. defer func() {
  156. o.mutex.Lock()
  157. o.busy = false
  158. o.mutex.Unlock()
  159. }()
  160. items, err := o.all()
  161. if err != nil {
  162. return
  163. }
  164. now := o.manager.now().UnixMilli()
  165. for _, item := range items {
  166. if item.Status != "queued" || item.SendAt > now {
  167. continue
  168. }
  169. o.deliver(ctx, item)
  170. }
  171. }
  172. func (o *outbox) deliver(ctx context.Context, item *OutboxItem) {
  173. m := o.manager
  174. p := Principal{Username: item.Owner, Admin: item.Admin}
  175. //Claim the item under the lock OutboxCancel takes, so a cancel either
  176. //wins outright or sees "sending" and refuses
  177. o.mutex.Lock()
  178. current, err := o.get(item.Owner, item.ID)
  179. if err != nil || current.Status != "queued" {
  180. o.mutex.Unlock()
  181. return
  182. }
  183. item = current
  184. item.Status = "sending"
  185. o.save(item)
  186. o.mutex.Unlock()
  187. fail := func(err error) {
  188. item.Attempts++
  189. item.Error = err.Error()
  190. if item.Attempts > len(retryDelays) || IsAuthError(err) || errors.Is(err, ErrAccountNotFound) || errors.Is(err, ErrTooLarge) {
  191. item.Status = "failed"
  192. o.save(item)
  193. logger.PrintAndLog("Email", "Scheduled message to "+strings.Join(item.Recipients, ", ")+" failed permanently", err)
  194. if m.options.Notify != nil {
  195. m.options.Notify(item.Owner, "Message not sent", "\""+item.Subject+"\" could not be delivered: "+err.Error())
  196. }
  197. return
  198. }
  199. item.Status = "queued"
  200. item.SendAt = m.now().Add(retryDelays[item.Attempts-1]).UnixMilli()
  201. o.save(item)
  202. }
  203. account, err := m.account(p, item.AccountID)
  204. if err != nil {
  205. fail(err)
  206. return
  207. }
  208. raw, err := os.ReadFile(o.spoolPath(item.ID))
  209. if err != nil {
  210. fail(errors.New("the queued message file is missing"))
  211. return
  212. }
  213. sendCtx, cancel := context.WithTimeout(ctx, 10*time.Minute)
  214. defer cancel()
  215. if err := m.sendRaw(sendCtx, p, account, item.From, item.Recipients, raw); err != nil {
  216. m.noteAuthError(p, account, err)
  217. fail(err)
  218. return
  219. }
  220. o.remove(item)
  221. m.afterDelivery(sendCtx, p, account, raw, item.MessageID, time.Unix(item.Date, 0), item.Post)
  222. }
  223. // dropAccount removes the queued mail of a deleted account.
  224. func (o *outbox) dropAccount(owner string, accountID string) {
  225. items, err := o.all()
  226. if err != nil {
  227. return
  228. }
  229. for _, item := range items {
  230. if item.Owner == owner && item.AccountID == accountID {
  231. o.remove(item)
  232. }
  233. }
  234. }
  235. // Outbox lists a user's queued and failed messages.
  236. func (m *Manager) Outbox(p Principal) ([]OutboxView, error) {
  237. items, err := m.outbox.all()
  238. if err != nil {
  239. return nil, err
  240. }
  241. views := []OutboxView{}
  242. for _, item := range items {
  243. if item.Owner != p.Username {
  244. continue
  245. }
  246. views = append(views, OutboxView{
  247. ID: item.ID, AccountID: item.AccountID, Subject: item.Subject, To: item.To,
  248. SendAt: item.SendAt, Status: item.Status, Attempts: item.Attempts, Error: item.Error, Created: item.Created,
  249. })
  250. }
  251. sort.Slice(views, func(i, j int) bool { return views[i].SendAt < views[j].SendAt })
  252. return views, nil
  253. }
  254. // OutboxCancel stops a queued message. toDrafts saves it to the Drafts folder
  255. // so a scheduled message can be edited.
  256. func (m *Manager) OutboxCancel(ctx context.Context, p Principal, id string, toDrafts bool) (*DraftResult, error) {
  257. m.outbox.mutex.Lock()
  258. item, err := m.outbox.get(p.Username, id)
  259. if err != nil {
  260. m.outbox.mutex.Unlock()
  261. return nil, err
  262. }
  263. if item.Status == "sending" {
  264. m.outbox.mutex.Unlock()
  265. return nil, errors.New("the message is already being sent")
  266. }
  267. raw, readErr := os.ReadFile(m.outbox.spoolPath(item.ID))
  268. m.outbox.remove(item)
  269. m.outbox.mutex.Unlock()
  270. if !toDrafts || readErr != nil {
  271. return nil, nil
  272. }
  273. account, err := m.account(p, item.AccountID)
  274. if err != nil {
  275. return nil, err
  276. }
  277. if item.Post.BccHeader != "" {
  278. raw = append([]byte(item.Post.BccHeader), raw...)
  279. }
  280. result := &DraftResult{}
  281. key := poolKey(p.Username, account.ID)
  282. err = m.withIMAP(ctx, p, account, func(c *imapclient.Client) error {
  283. drafts, err := m.ensureRoleFolder(c, key, RoleDrafts)
  284. if err != nil {
  285. return err
  286. }
  287. uid, err := appendMessage(c, drafts, raw, draftFlags, time.Unix(item.Date, 0), item.MessageID)
  288. result.Folder = drafts
  289. result.UID = uid
  290. return err
  291. })
  292. if err != nil {
  293. return nil, err
  294. }
  295. return result, nil
  296. }
  297. // OutboxSendNow delivers a queued or failed message at the next tick.
  298. func (m *Manager) OutboxSendNow(p Principal, id string) error {
  299. m.outbox.mutex.Lock()
  300. defer m.outbox.mutex.Unlock()
  301. item, err := m.outbox.get(p.Username, id)
  302. if err != nil {
  303. return err
  304. }
  305. if item.Status == "sending" {
  306. return nil
  307. }
  308. item.Status = "queued"
  309. item.SendAt = m.now().UnixMilli()
  310. if item.Attempts > len(retryDelays) {
  311. item.Attempts = 0
  312. }
  313. return m.outbox.save(item)
  314. }