mailbox.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. common "git.semlanik.org/semlanik/gostfix/common"
  32. )
  33. func (s *Server) handleMailbox(w http.ResponseWriter, user, email string) {
  34. mailList, err := s.storage.MailList(user, email, "Inbox", common.Frame{Skip: 0, Limit: 0})
  35. if err != nil {
  36. s.error(http.StatusInternalServerError, "Couldn't read email database", w)
  37. return
  38. }
  39. fmt.Fprint(w, s.templater.ExecuteIndex(&struct {
  40. Folders template.HTML
  41. MailList template.HTML
  42. Version template.HTML
  43. }{
  44. MailList: template.HTML(s.templater.ExecuteMailList(mailList)),
  45. Folders: "Folders",
  46. Version: common.Version,
  47. }))
  48. }
  49. func (s *Server) handleMailboxRequest(path, user string, mailbox int, w http.ResponseWriter, r *http.Request) {
  50. log.Printf("Handle mailbox %s", path)
  51. emails, err := s.storage.GetEmails(user)
  52. if err != nil || len(emails) <= 0 {
  53. s.error(http.StatusInternalServerError, "Unable to access mailbox", w)
  54. return
  55. }
  56. if len(emails) <= mailbox {
  57. if path == "" {
  58. http.Redirect(w, r, "/m0", http.StatusTemporaryRedirect)
  59. } else {
  60. s.error(http.StatusInternalServerError, "Unable to access mailbox", w)
  61. }
  62. return
  63. }
  64. switch path {
  65. case "":
  66. s.handleMailbox(w, user, emails[mailbox])
  67. case "folders":
  68. s.handleFolders(w, user, emails[mailbox])
  69. case "statusLine":
  70. s.handleStatusLine(w, user, emails[mailbox])
  71. case "mailList":
  72. s.handleMailList(w, user, emails[mailbox])
  73. default:
  74. http.Redirect(w, r, "/m0", http.StatusTemporaryRedirect)
  75. }
  76. }
  77. func (s *Server) handleFolders(w http.ResponseWriter, user, email string) {
  78. folders := []string{"Inbox", "Trash"}
  79. fmt.Fprintf(w, s.templater.ExecuteFolders(folders))
  80. }
  81. func (s *Server) handleMailList(w http.ResponseWriter, user, email string) {
  82. mailList, err := s.storage.MailList(user, email, "Inbox", common.Frame{Skip: 0, Limit: 0})
  83. if err != nil {
  84. s.error(http.StatusInternalServerError, "Couldn't read email database", w)
  85. return
  86. }
  87. fmt.Fprint(w, s.templater.ExecuteMailList(mailList))
  88. }
  89. func (s *Server) handleStatusLine(w http.ResponseWriter, user, email string) {
  90. info, err := s.storage.GetUserInfo(user)
  91. if err != nil {
  92. s.error(http.StatusInternalServerError, "Could not read user info", w)
  93. return
  94. }
  95. unread, total, err := s.storage.GetEmailStats(user, email)
  96. if err != nil {
  97. s.error(http.StatusInternalServerError, "Could not read user stats", w)
  98. return
  99. }
  100. fmt.Fprint(w, s.templater.ExecuteStatusLine(&struct {
  101. Name string
  102. Unread int
  103. Total int
  104. }{
  105. Name: info.FullName,
  106. Unread: unread,
  107. Total: total,
  108. }))
  109. }