mail.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "git.semlanik.org/semlanik/gostfix/common"
  32. )
  33. func (s *Server) handleMailRequest(w http.ResponseWriter, r *http.Request) {
  34. user, token := s.extractAuth(w, r)
  35. if !s.authenticator.Verify(user, token) {
  36. s.error(http.StatusUnauthorized, "You are not allowed to access this function", w)
  37. return
  38. }
  39. mailId := r.FormValue("mailId")
  40. if mailId == "" {
  41. s.error(http.StatusBadRequest, "Invalid mail id requested", w)
  42. return
  43. }
  44. switch r.URL.Path {
  45. case "/mail":
  46. s.handleMailDetails(w, user, mailId)
  47. case "/setRead":
  48. s.handleSetRead(w, r, user, mailId)
  49. case "/remove":
  50. s.handleRemove(w, user, mailId)
  51. case "/delete":
  52. s.handleDelete(w, user, mailId)
  53. }
  54. }
  55. func (s *Server) handleMailDetails(w http.ResponseWriter, user, mailId string) {
  56. mail, err := s.storage.GetMail(user, mailId)
  57. if err != nil {
  58. s.error(http.StatusInternalServerError, "Unable to read mail", w)
  59. return
  60. }
  61. s.storage.SetRead(user, mailId, true)
  62. fmt.Fprint(w, s.templater.ExecuteDetails(&struct {
  63. From string
  64. To string
  65. Subject string
  66. Text template.HTML
  67. MailId string
  68. Read bool
  69. }{
  70. From: mail.Header.From,
  71. To: mail.Header.To,
  72. Subject: mail.Header.Subject,
  73. Text: template.HTML(mail.Body.RichText),
  74. MailId: mailId,
  75. }))
  76. }
  77. func (s *Server) handleSetRead(w http.ResponseWriter, r *http.Request, user, mailId string) {
  78. read := r.FormValue("read") == "true"
  79. s.storage.SetRead(user, mailId, read)
  80. w.WriteHeader(http.StatusOK)
  81. w.Write([]byte{})
  82. }
  83. func (s *Server) handleRemove(w http.ResponseWriter, user, mailId string) {
  84. err := s.storage.MoveMail(user, mailId, common.Trash)
  85. if err != nil {
  86. s.error(http.StatusInternalServerError, "Could not move email to trash", w)
  87. }
  88. }
  89. func (s *Server) handleDelete(w http.ResponseWriter, user, mailId string) {
  90. log.Printf("Delete mail")
  91. err := s.storage.DeleteMail(user, mailId)
  92. if err != nil {
  93. s.error(http.StatusInternalServerError, "Could not delete email", w)
  94. }
  95. }