mail.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, user, mailId string) {
  36. if user == "" {
  37. log.Printf("User could not be empty. Invalid usage of handleMailRequest")
  38. panic(nil)
  39. }
  40. if mailId == "" {
  41. s.error(http.StatusBadRequest, "Invalid mail id requested", w)
  42. return
  43. }
  44. switch r.Method {
  45. case "GET":
  46. s.handleMailDetails(w, user, mailId)
  47. case "DELETE":
  48. s.handleMailDelete(w, user, mailId)
  49. case "PATCH":
  50. s.handleMailUpdate(w, r, user, mailId)
  51. }
  52. }
  53. func (s *Server) handleMailDetails(w http.ResponseWriter, user, mailId string) {
  54. mail, err := s.storage.GetMail(user, mailId)
  55. if err != nil {
  56. s.error(http.StatusInternalServerError, "Unable to read mail", w)
  57. return
  58. }
  59. text := mail.Mail.Body.RichText
  60. if text == "" {
  61. text = strings.Replace(mail.Mail.Body.PlainText, "\n", "</br>", -1)
  62. } else {
  63. utils.SanitizeTags(&text)
  64. }
  65. s.storage.SetRead(user, mailId, true)
  66. fmt.Fprint(w, s.templater.ExecuteDetails(&struct {
  67. From string
  68. To string
  69. Subject string
  70. Text template.HTML
  71. MailId string
  72. Read bool
  73. Trash bool
  74. Attachments []*common.AttachmentHeader
  75. }{
  76. From: mail.Mail.Header.From,
  77. To: mail.Mail.Header.To,
  78. Subject: mail.Mail.Header.Subject,
  79. Text: template.HTML(text),
  80. MailId: mailId,
  81. Read: false,
  82. Trash: mail.Trash ||
  83. mail.Folder == common.Trash, //TODO: Legacy for old databases remove soon
  84. Attachments: mail.Mail.Body.Attachments,
  85. }))
  86. }
  87. func (s *Server) handleMailUpdate(w http.ResponseWriter, r *http.Request, user, mailId string) {
  88. updateMap := map[string]interface{}{}
  89. if r.FormValue("read") == "true" {
  90. updateMap["read"] = true
  91. } else if r.FormValue("read") == "false" {
  92. updateMap["read"] = false
  93. }
  94. if r.FormValue("trash") == "true" {
  95. updateMap["trash"] = true
  96. } else if r.FormValue("trash") == "false" {
  97. updateMap["trash"] = false
  98. }
  99. if len(updateMap) == 0 {
  100. s.error(http.StatusBadRequest, "Unable to proccess mail", w)
  101. return
  102. }
  103. err := s.storage.UpdateMail(user, mailId, &updateMap)
  104. if err != nil {
  105. s.error(http.StatusInternalServerError, "Unable to proccess mail", w)
  106. return
  107. }
  108. w.WriteHeader(http.StatusOK)
  109. w.Write([]byte{})
  110. }
  111. func (s *Server) handleMailDelete(w http.ResponseWriter, user, mailId string) {
  112. log.Printf("Delete mail")
  113. err := s.storage.DeleteMail(user, mailId)
  114. if err != nil {
  115. s.error(http.StatusInternalServerError, "Could not delete email", w)
  116. }
  117. }