mail.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "net/http"
  30. )
  31. func (s *Server) handleMailRequest(w http.ResponseWriter, r *http.Request) {
  32. user, token := s.extractAuth(w, r)
  33. if !s.authenticator.Verify(user, token) {
  34. s.error(http.StatusUnauthorized, "You are not allowed to access this function", w)
  35. return
  36. }
  37. mailId := r.FormValue("mailId")
  38. if mailId == "" {
  39. s.error(http.StatusBadRequest, "Invalid mail id requested", w)
  40. return
  41. }
  42. switch r.URL.Path {
  43. case "/mail":
  44. s.handleMailDetails(w, user, mailId)
  45. case "/setRead":
  46. s.handleSetRead(w, r, user, mailId)
  47. case "/remove":
  48. s.handleRemove(w, user, mailId)
  49. }
  50. }
  51. func (s *Server) handleMailDetails(w http.ResponseWriter, user, mailId string) {
  52. mail, err := s.storage.GetMail(user, mailId)
  53. if err != nil {
  54. s.error(http.StatusInternalServerError, "Unable to read mail", w)
  55. return
  56. }
  57. s.storage.SetRead(user, mailId, true)
  58. fmt.Fprint(w, s.templater.ExecuteDetails(&struct {
  59. From string
  60. To string
  61. Subject string
  62. Text template.HTML
  63. MailId string
  64. Read bool
  65. }{
  66. From: mail.Header.From,
  67. To: mail.Header.To,
  68. Subject: mail.Header.Subject,
  69. Text: template.HTML(mail.Body.RichText),
  70. MailId: mailId,
  71. }))
  72. }
  73. func (s *Server) handleSetRead(w http.ResponseWriter, r *http.Request, user, mailId string) {
  74. read := r.FormValue("read") == "true"
  75. s.storage.SetRead(user, mailId, read)
  76. w.WriteHeader(http.StatusOK)
  77. w.Write([]byte{})
  78. }
  79. func (s *Server) handleRemove(w http.ResponseWriter, user, mailId string) {
  80. s.storage.RemoveMail(user, mailId)
  81. }