mailscanner.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. config "../config"
  33. utils "../utils"
  34. fsnotify "github.com/fsnotify/fsnotify"
  35. )
  36. type MailScanner struct {
  37. watcher *fsnotify.Watcher
  38. mailMaps map[string]string
  39. }
  40. func NewMailScanner() (ms *MailScanner) {
  41. mailPath := config.ConfigInstance().VMailboxBase
  42. fmt.Printf("Add mail folder %s for watching\n", mailPath)
  43. watcher, err := fsnotify.NewWatcher()
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. ms = &MailScanner{
  48. watcher: watcher,
  49. mailMaps: readMailMaps(),
  50. }
  51. for _, f := range ms.mailMaps {
  52. fullPath := mailPath + "/" + f
  53. if utils.FileExists(fullPath) {
  54. fmt.Printf("Add mail file %s for watching\n", fullPath)
  55. watcher.Add(fullPath)
  56. }
  57. }
  58. return
  59. }
  60. func readMailMaps() map[string]string {
  61. mailMaps := make(map[string]string)
  62. mapsFile := config.ConfigInstance().VMailboxMaps
  63. if !utils.FileExists(mapsFile) {
  64. return mailMaps
  65. }
  66. file, err := os.Open(mapsFile)
  67. if err != nil {
  68. log.Fatalf("Unable to open virtual mailbox maps %s\n", mapsFile)
  69. }
  70. scanner := bufio.NewScanner(file)
  71. for scanner.Scan() {
  72. mailPathPair := strings.Split(scanner.Text(), " ")
  73. if len(mailPathPair) != 2 {
  74. log.Printf("Invalid record in virtual mailbox maps %s", scanner.Text())
  75. continue
  76. }
  77. mailMaps[mailPathPair[0]] = mailPathPair[1]
  78. }
  79. return mailMaps
  80. }
  81. func (ms *MailScanner) Run() {
  82. go func() {
  83. for {
  84. select {
  85. case event, ok := <-ms.watcher.Events:
  86. if !ok {
  87. return
  88. }
  89. if event.Op&fsnotify.Write == fsnotify.Write {
  90. log.Println("New email for", event.Name)
  91. }
  92. case err, ok := <-ms.watcher.Errors:
  93. if !ok {
  94. return
  95. }
  96. log.Println("error:", err)
  97. }
  98. }
  99. }()
  100. }
  101. func (ms *MailScanner) Stop() {
  102. defer ms.watcher.Close()
  103. }