mail.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. )
  34. func (s *Server) handleMailRequest(w http.ResponseWriter, r *http.Request) {
  35. user, token := s.extractAuth(w, r)
  36. if !s.authenticator.Verify(user, token) {
  37. s.error(http.StatusUnauthorized, "You are not allowed to access this function", w)
  38. return
  39. }
  40. mailId := r.FormValue("mailId")
  41. if mailId == "" {
  42. s.error(http.StatusBadRequest, "Invalid mail id requested", w)
  43. return
  44. }
  45. switch r.URL.Path {
  46. case "/mail":
  47. s.handleMailDetails(w, user, mailId)
  48. case "/setRead":
  49. s.handleSetRead(w, r, user, mailId)
  50. case "/remove":
  51. s.handleRemove(w, user, mailId)
  52. case "/restore":
  53. s.handleRestore(w, user, mailId)
  54. case "/delete":
  55. s.handleDelete(w, user, mailId)
  56. }
  57. }
  58. func (s *Server) handleMailDetails(w http.ResponseWriter, user, mailId string) {
  59. mail, err := s.storage.GetMail(user, mailId)
  60. if err != nil {
  61. s.error(http.StatusInternalServerError, "Unable to read mail", w)
  62. return
  63. }
  64. text := mail.Mail.Body.RichText
  65. if text == "" {
  66. text = strings.Replace(mail.Mail.Body.PlainText, "\n", "</br>", -1)
  67. }
  68. s.storage.SetRead(user, mailId, true)
  69. fmt.Fprint(w, s.templater.ExecuteDetails(&struct {
  70. From string
  71. To string
  72. Subject string
  73. Text template.HTML
  74. MailId string
  75. Read bool
  76. Trash bool
  77. }{
  78. From: mail.Mail.Header.From,
  79. To: mail.Mail.Header.To,
  80. Subject: mail.Mail.Header.Subject,
  81. Text: template.HTML(text),
  82. MailId: mailId,
  83. Read: false,
  84. Trash: mail.Trash ||
  85. mail.Folder == common.Trash, //TODO: Legacy for old databases remove soon
  86. }))
  87. }
  88. func (s *Server) handleSetRead(w http.ResponseWriter, r *http.Request, user, mailId string) {
  89. read := r.FormValue("read") == "true"
  90. s.storage.SetRead(user, mailId, read)
  91. w.WriteHeader(http.StatusOK)
  92. w.Write([]byte{})
  93. }
  94. func (s *Server) handleRemove(w http.ResponseWriter, user, mailId string) {
  95. err := s.storage.MoveMail(user, mailId, common.Trash)
  96. if err != nil {
  97. s.error(http.StatusInternalServerError, "Could not move email to trash", w)
  98. }
  99. }
  100. func (s *Server) handleRestore(w http.ResponseWriter, user, mailId string) {
  101. err := s.storage.RestoreMail(user, mailId)
  102. if err != nil {
  103. s.error(http.StatusInternalServerError, "Could not move email to trash", w)
  104. }
  105. }
  106. func (s *Server) handleDelete(w http.ResponseWriter, user, mailId string) {
  107. log.Printf("Delete mail")
  108. err := s.storage.DeleteMail(user, mailId)
  109. if err != nil {
  110. s.error(http.StatusInternalServerError, "Could not delete email", w)
  111. }
  112. }