mailbox.go 4.2 KB

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