service.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "log"
  28. "git.semlanik.org/semlanik/gostfix/common"
  29. "github.com/fsnotify/fsnotify"
  30. )
  31. func (ms *MailScanner) ServiceName() string {
  32. return "Mail Scanner"
  33. }
  34. func (ms *MailScanner) Run() {
  35. ms.reconfigure()
  36. defer ms.watcher.Close()
  37. for {
  38. select {
  39. case signal := <-ms.signalChannel:
  40. if signal == SignalStop {
  41. return
  42. }
  43. ms.handleSignal(signal)
  44. case event, ok := <-ms.watcher.Events:
  45. if !ok {
  46. log.Printf("Unable to read mail watcher event. Mail scanner restart is needed.")
  47. return
  48. }
  49. if event.Op&fsnotify.Write == fsnotify.Write {
  50. log.Println("iNotify write")
  51. mailPath := event.Name
  52. mailbox := ""
  53. for k, v := range ms.emailMaps {
  54. if v == mailPath {
  55. mailbox = k
  56. }
  57. }
  58. if mailbox != "" {
  59. mails := ms.readMailFile(mailPath)
  60. for _, mail := range mails {
  61. ms.storage.SaveMail(mailbox, common.Inbox, mail, false)
  62. }
  63. log.Printf("New email for %s, emails read %d", mailPath, len(mails))
  64. } else {
  65. log.Printf("Invalid path update triggered: %s", mailPath)
  66. }
  67. }
  68. case err, ok := <-ms.watcher.Errors:
  69. if !ok {
  70. log.Printf("Unable to read mail watcher errors. Mail scanner restart is needed.")
  71. return
  72. }
  73. log.Println("Mail watcher error:", err)
  74. }
  75. }
  76. }
  77. func (ms *MailScanner) Stop() {
  78. ms.signalChannel <- SignalStop
  79. }
  80. func (ms *MailScanner) Error() string {
  81. return ""
  82. }