mail.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package web
  26. import (
  27. "fmt"
  28. template "html/template"
  29. "log"
  30. "net/http"
  31. "strings"
  32. "git.semlanik.org/semlanik/gostfix/common"
  33. "git.semlanik.org/semlanik/gostfix/utils"
  34. )
  35. func (s *Server) handleMailRequest(w http.ResponseWriter, r *http.Request) {
  36. user, token := s.extractAuth(w, r)
  37. if !s.authenticator.Verify(user, token) {
  38. s.error(http.StatusUnauthorized, "You are not allowed to access this function", w)
  39. return
  40. }
  41. mailId := r.FormValue("mailId")
  42. if mailId == "" {
  43. s.error(http.StatusBadRequest, "Invalid mail id requested", w)
  44. return
  45. }
  46. switch r.URL.Path {
  47. case "/mail":
  48. s.handleMailDetails(w, user, mailId)
  49. case "/setRead":
  50. s.handleSetRead(w, r, user, mailId)
  51. case "/remove":
  52. s.handleRemove(w, user, mailId)
  53. case "/restore":
  54. s.handleRestore(w, user, mailId)
  55. case "/delete":
  56. s.handleDelete(w, user, mailId)
  57. }
  58. }
  59. func (s *Server) handleMailDetails(w http.ResponseWriter, user, mailId string) {
  60. mail, err := s.storage.GetMail(user, mailId)
  61. if err != nil {
  62. s.error(http.StatusInternalServerError, "Unable to read mail", w)
  63. return
  64. }
  65. text := mail.Mail.Body.RichText
  66. if text == "" {
  67. text = strings.Replace(mail.Mail.Body.PlainText, "\n", "</br>", -1)
  68. } else {
  69. utils.SanitizeTags(&text)
  70. }
  71. s.storage.SetRead(user, mailId, true)
  72. fmt.Fprint(w, s.templater.ExecuteDetails(&struct {
  73. From string
  74. To string
  75. Subject string
  76. Text template.HTML
  77. MailId string
  78. Read bool
  79. Trash bool
  80. }{
  81. From: mail.Mail.Header.From,
  82. To: mail.Mail.Header.To,
  83. Subject: mail.Mail.Header.Subject,
  84. Text: template.HTML(text),
  85. MailId: mailId,
  86. Read: false,
  87. Trash: mail.Trash ||
  88. mail.Folder == common.Trash, //TODO: Legacy for old databases remove soon
  89. }))
  90. }
  91. func (s *Server) handleSetRead(w http.ResponseWriter, r *http.Request, user, mailId string) {
  92. read := r.FormValue("read") == "true"
  93. s.storage.SetRead(user, mailId, read)
  94. w.WriteHeader(http.StatusOK)
  95. w.Write([]byte{})
  96. }
  97. func (s *Server) handleRemove(w http.ResponseWriter, user, mailId string) {
  98. err := s.storage.MoveMail(user, mailId, common.Trash)
  99. if err != nil {
  100. s.error(http.StatusInternalServerError, "Could not move email to trash", w)
  101. }
  102. }
  103. func (s *Server) handleRestore(w http.ResponseWriter, user, mailId string) {
  104. err := s.storage.RestoreMail(user, mailId)
  105. if err != nil {
  106. s.error(http.StatusInternalServerError, "Could not move email to trash", w)
  107. }
  108. }
  109. func (s *Server) handleDelete(w http.ResponseWriter, user, mailId string) {
  110. log.Printf("Delete mail")
  111. err := s.storage.DeleteMail(user, mailId)
  112. if err != nil {
  113. s.error(http.StatusInternalServerError, "Could not delete email", w)
  114. }
  115. }