mailbox.go 6.0 KB

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