mailscanner.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. "fmt"
  28. "log"
  29. "os"
  30. "git.semlanik.org/semlanik/gostfix/common"
  31. config "git.semlanik.org/semlanik/gostfix/config"
  32. db "git.semlanik.org/semlanik/gostfix/db"
  33. utils "git.semlanik.org/semlanik/gostfix/utils"
  34. fsnotify "github.com/fsnotify/fsnotify"
  35. )
  36. const (
  37. SignalReconfigure = iota
  38. )
  39. type MailScanner struct {
  40. watcher *fsnotify.Watcher
  41. emailMaps map[string]string
  42. storage *db.Storage
  43. signalChannel chan int
  44. }
  45. func NewMailScanner() (ms *MailScanner) {
  46. log.Print("test2")
  47. watcher, err := fsnotify.NewWatcher()
  48. if err != nil {
  49. log.Fatal(err)
  50. return
  51. }
  52. log.Print("test2")
  53. storage, err := db.NewStorage()
  54. log.Print("test3")
  55. if err != nil {
  56. log.Fatal(err)
  57. return
  58. }
  59. if !utils.DirectoryExists(config.ConfigInstance().AttachmentsPath) {
  60. err = os.Mkdir(config.ConfigInstance().AttachmentsPath, 0755)
  61. if err != nil {
  62. log.Fatal(err)
  63. return
  64. }
  65. }
  66. ms = &MailScanner{
  67. watcher: watcher,
  68. storage: storage,
  69. signalChannel: make(chan int),
  70. }
  71. return
  72. }
  73. func (ms *MailScanner) Reconfigure() {
  74. ms.signalChannel <- SignalReconfigure
  75. }
  76. func (ms *MailScanner) checkEmailRegistred(email string) bool {
  77. emails, err := ms.storage.GetAllEmails()
  78. if err != nil {
  79. return false
  80. }
  81. for _, e := range emails {
  82. if email == e {
  83. return true
  84. }
  85. }
  86. return false
  87. }
  88. func (ms *MailScanner) reconfigure() {
  89. var err error
  90. ms.emailMaps, err = ms.storage.ReadEmailMaps()
  91. if err != nil {
  92. log.Fatal(err.Error())
  93. }
  94. for mailbox, mailPath := range ms.emailMaps {
  95. if !utils.FileExists(mailPath) {
  96. file, err := os.Create(mailPath)
  97. if err != nil {
  98. fmt.Printf("Unable to create mailbox for watching %s\n", err)
  99. continue
  100. }
  101. file.Close()
  102. }
  103. mails := ms.readMailFile(mailPath)
  104. for _, mail := range mails {
  105. ms.storage.SaveMail(mailbox, common.Inbox, mail, false)
  106. }
  107. log.Printf("New email for %s, emails read %d", mailPath, len(mails))
  108. err := ms.watcher.Add(mailPath)
  109. if err != nil {
  110. fmt.Printf("Unable to add mailbox for watching\n")
  111. } else {
  112. fmt.Printf("Add mail file %s for watching\n", mailPath)
  113. }
  114. }
  115. }
  116. func (ms *MailScanner) Run() {
  117. go func() {
  118. ms.reconfigure()
  119. for {
  120. select {
  121. case signal := <-ms.signalChannel:
  122. switch signal {
  123. case SignalReconfigure:
  124. ms.reconfigure()
  125. }
  126. case event, ok := <-ms.watcher.Events:
  127. if !ok {
  128. return
  129. }
  130. if event.Op&fsnotify.Write == fsnotify.Write {
  131. log.Println("iNotify write")
  132. mailPath := event.Name
  133. mailbox := ""
  134. for k, v := range ms.emailMaps {
  135. if v == mailPath {
  136. mailbox = k
  137. }
  138. }
  139. if mailbox != "" {
  140. mails := ms.readMailFile(mailPath)
  141. for _, mail := range mails {
  142. ms.storage.SaveMail(mailbox, common.Inbox, mail, false)
  143. }
  144. log.Printf("New email for %s, emails read %d", mailPath, len(mails))
  145. } else {
  146. log.Printf("Invalid path update triggered: %s", mailPath)
  147. }
  148. }
  149. case err, ok := <-ms.watcher.Errors:
  150. if !ok {
  151. return
  152. }
  153. log.Println("error:", err)
  154. }
  155. }
  156. }()
  157. }
  158. func (ms *MailScanner) Stop() {
  159. defer ms.watcher.Close()
  160. }
  161. func (ms *MailScanner) readMailFile(mailPath string) (mails []*common.Mail) {
  162. log.Println("Read mail file")
  163. defer log.Println("Exit read mail file")
  164. if !utils.FileExists(mailPath) {
  165. return nil
  166. }
  167. file, err := utils.OpenAndLockWait(mailPath)
  168. if err != nil {
  169. return nil
  170. }
  171. defer file.CloseAndUnlock()
  172. mails = parseFile(file)
  173. if len(mails) > 0 {
  174. file.Truncate(0)
  175. }
  176. return mails
  177. }