mail.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "/delete":
  53. s.handleDelete(w, user, mailId)
  54. }
  55. }
  56. func (s *Server) handleMailDetails(w http.ResponseWriter, user, mailId string) {
  57. mail, err := s.storage.GetMail(user, mailId)
  58. if err != nil {
  59. s.error(http.StatusInternalServerError, "Unable to read mail", w)
  60. return
  61. }
  62. text := mail.Body.RichText
  63. if text == "" {
  64. text = strings.Replace(mail.Body.PlainText, "\n", "</br>", -1)
  65. }
  66. s.storage.SetRead(user, mailId, true)
  67. fmt.Fprint(w, s.templater.ExecuteDetails(&struct {
  68. From string
  69. To string
  70. Subject string
  71. Text template.HTML
  72. MailId string
  73. Read bool
  74. }{
  75. From: mail.Header.From,
  76. To: mail.Header.To,
  77. Subject: mail.Header.Subject,
  78. Text: template.HTML(text),
  79. MailId: mailId,
  80. }))
  81. }
  82. func (s *Server) handleSetRead(w http.ResponseWriter, r *http.Request, user, mailId string) {
  83. read := r.FormValue("read") == "true"
  84. s.storage.SetRead(user, mailId, read)
  85. w.WriteHeader(http.StatusOK)
  86. w.Write([]byte{})
  87. }
  88. func (s *Server) handleRemove(w http.ResponseWriter, user, mailId string) {
  89. err := s.storage.MoveMail(user, mailId, common.Trash)
  90. if err != nil {
  91. s.error(http.StatusInternalServerError, "Could not move email to trash", w)
  92. }
  93. }
  94. func (s *Server) handleDelete(w http.ResponseWriter, user, mailId string) {
  95. log.Printf("Delete mail")
  96. err := s.storage.DeleteMail(user, mailId)
  97. if err != nil {
  98. s.error(http.StatusInternalServerError, "Could not delete email", w)
  99. }
  100. }