mailbox.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. "encoding/json"
  28. "fmt"
  29. template "html/template"
  30. "log"
  31. "net/http"
  32. "strconv"
  33. common "git.semlanik.org/semlanik/gostfix/common"
  34. )
  35. func (s *Server) handleMailbox(w http.ResponseWriter, user, email string) {
  36. mailList, err := s.storage.MailList(user, email, common.Inbox, common.Frame{Skip: 0, Limit: 50})
  37. if err != nil {
  38. s.error(http.StatusInternalServerError, "Couldn't read email database", w)
  39. return
  40. }
  41. fmt.Fprint(w, s.templater.ExecuteIndex(&struct {
  42. Folders template.HTML
  43. MailList template.HTML
  44. Version template.HTML
  45. }{
  46. MailList: template.HTML(s.templater.ExecuteMailList(mailList)),
  47. Folders: "Folders",
  48. Version: common.Version,
  49. }))
  50. }
  51. func (s *Server) handleMailboxRequest(path, user string, mailbox int, w http.ResponseWriter, r *http.Request) {
  52. log.Printf("Handle mailbox %s", path)
  53. emails, err := s.storage.GetEmails(user)
  54. if err != nil || len(emails) <= 0 {
  55. s.error(http.StatusInternalServerError, "Unable to access mailbox", w)
  56. return
  57. }
  58. if len(emails) <= mailbox {
  59. if path == "" {
  60. http.Redirect(w, r, "/m0", http.StatusTemporaryRedirect)
  61. } else {
  62. s.error(http.StatusInternalServerError, "Unable to access mailbox", w)
  63. }
  64. return
  65. }
  66. switch path {
  67. case "":
  68. s.handleMailbox(w, user, emails[mailbox])
  69. case "folders":
  70. s.handleFolders(w, user, emails[mailbox])
  71. case "folderStat":
  72. s.handleFolderStat(w, r, user, emails[mailbox])
  73. case "statusLine":
  74. s.handleStatusLine(w, user, emails[mailbox])
  75. case "mailList":
  76. s.handleMailList(w, r, user, emails[mailbox])
  77. default:
  78. http.Redirect(w, r, "/m0", http.StatusTemporaryRedirect)
  79. }
  80. }
  81. func (s *Server) handleFolders(w http.ResponseWriter, user, email string) {
  82. folders := s.storage.GetFolders(email)
  83. out, err := json.Marshal(&struct {
  84. Folders []*common.Folder `json:"folders"`
  85. Html string `json:"html"`
  86. }{
  87. Folders: folders,
  88. Html: s.templater.ExecuteFolders(s.storage.GetFolders(email)),
  89. })
  90. if err != nil {
  91. s.error(http.StatusInternalServerError, "Could not fetch folder list", w)
  92. }
  93. w.Write(out)
  94. }
  95. func (s *Server) handleFolderStat(w http.ResponseWriter, r *http.Request, user, email string) {
  96. unread, total, err := s.storage.GetEmailStats(user, email, s.extractFolder(email, r))
  97. if err != nil {
  98. s.error(http.StatusInternalServerError, "Couldn't read mailbox stat", w)
  99. return
  100. }
  101. out, err := json.Marshal(&struct {
  102. Total int `json:"total"`
  103. Unread int `json:"unread"`
  104. }{
  105. Total: total,
  106. Unread: unread,
  107. })
  108. if err != nil {
  109. s.error(http.StatusInternalServerError, "Couldn't parse mailbox stat", w)
  110. return
  111. }
  112. w.Write(out)
  113. }
  114. func (s *Server) handleMailList(w http.ResponseWriter, r *http.Request, user, email string) {
  115. folder := s.extractFolder(email, r)
  116. page, err := strconv.Atoi(r.FormValue("page"))
  117. if err != nil {
  118. page = 0
  119. }
  120. _, total, err := s.storage.GetEmailStats(user, email, folder)
  121. if err != nil {
  122. s.error(http.StatusInternalServerError, "Couldn't read email database", w)
  123. return
  124. }
  125. mailList, err := s.storage.MailList(user, email, folder, common.Frame{Skip: int32(50 * page), Limit: 50})
  126. if err != nil {
  127. s.error(http.StatusInternalServerError, "Couldn't read email database", w)
  128. return
  129. }
  130. out, err := json.Marshal(&struct {
  131. Total int `json:"total"`
  132. Html string `json:"html"`
  133. }{
  134. Total: total,
  135. Html: s.templater.ExecuteMailList(mailList),
  136. })
  137. if err != nil {
  138. s.error(http.StatusInternalServerError, "Could not perform maillist", w)
  139. return
  140. }
  141. w.Write(out)
  142. }
  143. func (s *Server) handleStatusLine(w http.ResponseWriter, user, email string) {
  144. info, err := s.storage.GetUserInfo(user)
  145. if err != nil {
  146. s.error(http.StatusInternalServerError, "Could not read user info", w)
  147. return
  148. }
  149. fmt.Fprint(w, s.templater.ExecuteStatusLine(&struct {
  150. Name string
  151. Email string
  152. }{
  153. Name: info.FullName,
  154. Email: email,
  155. }))
  156. }
  157. func (s *Server) extractFolder(email string, r *http.Request) string {
  158. folder := r.FormValue("folder")
  159. folders := s.storage.GetFolders(email)
  160. ok := false
  161. for _, existFolder := range folders {
  162. if folder == existFolder.Name {
  163. ok = true
  164. break
  165. }
  166. }
  167. if !ok {
  168. folder = common.Inbox
  169. }
  170. return folder
  171. }