mailscanner.go 4.6 KB

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