mailscanner.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 scanner
  26. import (
  27. "bufio"
  28. "fmt"
  29. "log"
  30. "os"
  31. "strings"
  32. "git.semlanik.org/semlanik/gostfix/common"
  33. config "git.semlanik.org/semlanik/gostfix/config"
  34. db "git.semlanik.org/semlanik/gostfix/db"
  35. utils "git.semlanik.org/semlanik/gostfix/utils"
  36. fsnotify "github.com/fsnotify/fsnotify"
  37. )
  38. func NewEmail() *common.Mail {
  39. return &common.Mail{
  40. Header: &common.MailHeader{},
  41. Body: &common.MailBody{},
  42. }
  43. }
  44. type MailScanner struct {
  45. watcher *fsnotify.Watcher
  46. emailMaps map[string]string
  47. storage *db.Storage
  48. }
  49. func NewMailScanner() (ms *MailScanner) {
  50. watcher, err := fsnotify.NewWatcher()
  51. if err != nil {
  52. log.Fatal(err)
  53. return
  54. }
  55. storage, err := db.NewStorage()
  56. if err != nil {
  57. log.Fatal(err)
  58. return
  59. }
  60. ms = &MailScanner{
  61. watcher: watcher,
  62. storage: storage,
  63. }
  64. return
  65. }
  66. func (ms *MailScanner) checkEmailRegistred(email string) bool {
  67. emails, err := ms.storage.GetAllEmails()
  68. if err != nil {
  69. return false
  70. }
  71. for _, e := range emails {
  72. if email == e {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. func (ms *MailScanner) readEmailMaps() {
  79. registredEmails, err := ms.storage.GetAllEmails()
  80. if err != nil {
  81. log.Fatal(err)
  82. return
  83. }
  84. mailPath := config.ConfigInstance().VMailboxBase
  85. emailMaps := make(map[string]string)
  86. mapsFile := config.ConfigInstance().VMailboxMaps
  87. if !utils.FileExists(mapsFile) {
  88. log.Fatal("Could not read virtual mailbox maps")
  89. return
  90. }
  91. file, err := os.Open(mapsFile)
  92. if err != nil {
  93. log.Fatalf("Unable to open virtual mailbox maps %s\n", mapsFile)
  94. }
  95. scanner := bufio.NewScanner(file)
  96. for scanner.Scan() {
  97. emailMapPair := strings.Split(scanner.Text(), " ")
  98. if len(emailMapPair) != 2 {
  99. log.Printf("Invalid record in virtual mailbox maps %s\n", scanner.Text())
  100. continue
  101. }
  102. found := false
  103. email := emailMapPair[0]
  104. for _, registredEmail := range registredEmails {
  105. if email == registredEmail {
  106. found = true
  107. }
  108. }
  109. if !found {
  110. log.Fatalf("Found non-registred mailbox <%s> in mail maps. Database has inconsistancy.\n", email)
  111. return
  112. }
  113. emailMaps[email] = mailPath + "/" + emailMapPair[1]
  114. }
  115. for _, registredEmail := range registredEmails {
  116. if _, exists := emailMaps[registredEmail]; !exists {
  117. log.Fatalf("Found existing mailbox <%s> in database. Mail maps has inconsistancy.\n", registredEmail)
  118. }
  119. }
  120. ms.emailMaps = emailMaps
  121. }
  122. func (ms *MailScanner) Run() {
  123. go func() {
  124. ms.readEmailMaps()
  125. for mailbox, mailPath := range ms.emailMaps {
  126. if !utils.FileExists(mailPath) {
  127. file, err := os.Create(mailPath)
  128. if err != nil {
  129. fmt.Printf("Unable to create mailbox for watching %s\n", err)
  130. continue
  131. }
  132. file.Close()
  133. }
  134. mails := ms.readMailFile(mailPath)
  135. for _, mail := range mails {
  136. ms.storage.SaveMail(mailbox, "Inbox", mail)
  137. }
  138. log.Printf("New email for %s, emails read %d", mailPath, len(mails))
  139. err := ms.watcher.Add(mailPath)
  140. if err != nil {
  141. fmt.Printf("Unable to add mailbox for watching\n")
  142. } else {
  143. fmt.Printf("Add mail file %s for watching\n", mailPath)
  144. }
  145. }
  146. for {
  147. select {
  148. case event, ok := <-ms.watcher.Events:
  149. if !ok {
  150. return
  151. }
  152. if event.Op&fsnotify.Write == fsnotify.Write {
  153. mailPath := event.Name
  154. mailbox := ""
  155. for k, v := range ms.emailMaps {
  156. if v == mailPath {
  157. mailbox = k
  158. }
  159. }
  160. if mailbox != "" {
  161. mails := ms.readMailFile(mailPath)
  162. for _, mail := range mails {
  163. ms.storage.SaveMail(mailbox, "Inbox", mail)
  164. }
  165. log.Printf("New email for %s, emails read %d", mailPath, len(mails))
  166. } else {
  167. log.Printf("Invalid path update triggered: %s", mailPath)
  168. }
  169. }
  170. case err, ok := <-ms.watcher.Errors:
  171. if !ok {
  172. return
  173. }
  174. log.Println("error:", err)
  175. }
  176. }
  177. }()
  178. }
  179. func (ms *MailScanner) Stop() {
  180. defer ms.watcher.Close()
  181. }
  182. func (ms *MailScanner) readMailFile(mailPath string) (mails []*common.Mail) {
  183. if !utils.FileExists(mailPath) {
  184. return nil
  185. }
  186. file, err := utils.OpenAndLockWait(mailPath)
  187. if err != nil {
  188. return nil
  189. }
  190. defer file.CloseAndUnlock()
  191. mails = parseFile(file)
  192. if len(mails) > 0 {
  193. file.Truncate(0)
  194. }
  195. return mails
  196. }