mailscanner.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. SignalStop
  39. )
  40. type MailScanner struct {
  41. watcher *fsnotify.Watcher
  42. emailMaps map[string]string
  43. storage *db.Storage
  44. signalChannel chan int
  45. }
  46. func NewMailScanner() (ms *MailScanner) {
  47. watcher, err := fsnotify.NewWatcher()
  48. if err != nil {
  49. log.Fatal(err)
  50. return
  51. }
  52. storage, err := db.NewStorage()
  53. if err != nil {
  54. log.Fatal(err)
  55. return
  56. }
  57. if !utils.DirectoryExists(config.ConfigInstance().AttachmentsPath) {
  58. err = os.Mkdir(config.ConfigInstance().AttachmentsPath, 0755)
  59. if err != nil {
  60. log.Fatal(err)
  61. return
  62. }
  63. }
  64. ms = &MailScanner{
  65. watcher: watcher,
  66. storage: storage,
  67. signalChannel: make(chan int),
  68. }
  69. return
  70. }
  71. func (ms *MailScanner) Reconfigure() {
  72. ms.signalChannel <- SignalReconfigure
  73. }
  74. func (ms *MailScanner) checkEmailRegistred(email string) bool {
  75. emails, err := ms.storage.GetAllEmails()
  76. if err != nil {
  77. return false
  78. }
  79. for _, e := range emails {
  80. if email == e {
  81. return true
  82. }
  83. }
  84. return false
  85. }
  86. func (ms *MailScanner) reconfigure() {
  87. log.Printf("Reconfiguring mail scanner")
  88. var err error
  89. ms.emailMaps, err = ms.storage.ReadEmailMaps()
  90. if err != nil {
  91. log.Fatal(err.Error())
  92. }
  93. for mailbox, mailPath := range ms.emailMaps {
  94. if !utils.FileExists(mailPath) {
  95. file, err := os.Create(mailPath)
  96. if err != nil {
  97. fmt.Printf("Unable to create mailbox for watching %s\n", err)
  98. continue
  99. }
  100. file.Close()
  101. }
  102. mails := ms.readMailFile(mailPath)
  103. for _, mail := range mails {
  104. ms.storage.SaveMail(mailbox, common.Inbox, mail, false)
  105. }
  106. log.Printf("New email for %s, emails read %d", mailPath, len(mails))
  107. err := ms.watcher.Add(mailPath)
  108. if err != nil {
  109. fmt.Printf("Unable to add mailbox for watching\n")
  110. } else {
  111. fmt.Printf("Add mail file %s for watching\n", mailPath)
  112. }
  113. }
  114. }
  115. func (ms *MailScanner) handleSignal(signal int) {
  116. switch signal {
  117. case SignalReconfigure:
  118. ms.reconfigure()
  119. }
  120. }
  121. func (ms *MailScanner) readMailFile(mailPath string) (mails []*common.Mail) {
  122. log.Println("Read mail file")
  123. defer log.Println("Exit read mail file")
  124. if !utils.FileExists(mailPath) {
  125. return nil
  126. }
  127. file, err := utils.OpenAndLockWait(mailPath)
  128. if err != nil {
  129. return nil
  130. }
  131. defer file.CloseAndUnlock()
  132. mails = parseFile(file)
  133. if len(mails) > 0 {
  134. file.Truncate(0)
  135. }
  136. return mails
  137. }