config.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 config
  26. import (
  27. "log"
  28. "strings"
  29. "sync"
  30. utils "git.semlanik.org/semlanik/gostfix/utils"
  31. ini "gopkg.in/go-ini/ini.v1"
  32. )
  33. const configPath = "data/main.ini"
  34. const (
  35. KeyPostfixConfig = "postfix_config"
  36. KeyMongoAddress = "mongo_address"
  37. KeyMongoUser = "mongo_user"
  38. KeyMongoPassword = "mongo_password"
  39. KeyAttachmentsPath = "attachments_path"
  40. KeyAttachmentsUser = "attachments_user"
  41. KeyAttachmentsPassword = "attachments_password"
  42. )
  43. const (
  44. PostfixKeyVirtualMailboxMaps = "virtual_mailbox_maps"
  45. PostfixKeyVirtualMailboxBase = "virtual_mailbox_base"
  46. PostfixKeyVirtualMailboxDomains = "virtual_mailbox_domains"
  47. )
  48. type GostfixConfig gostfixConfig
  49. var (
  50. once sync.Once
  51. instance *gostfixConfig
  52. )
  53. func ConfigInstance() *GostfixConfig {
  54. once.Do(func() {
  55. instance, _ = newConfig()
  56. })
  57. return (*GostfixConfig)(instance)
  58. }
  59. type gostfixConfig struct {
  60. VMailboxMaps string
  61. VMailboxBase string
  62. VMailboxDomains []string
  63. MongoUser string
  64. MongoPassword string
  65. MongoAddress string
  66. AttachmentsPath string
  67. }
  68. func newConfig() (config *gostfixConfig, err error) {
  69. cfg, err := ini.Load(configPath)
  70. if err != nil {
  71. log.Fatalf("Unable to load %s\n", configPath)
  72. return
  73. }
  74. postfixConfigPath := cfg.Section("").Key(KeyPostfixConfig).String()
  75. if !utils.FileExists(postfixConfigPath) {
  76. log.Fatalf("Unable to find postfix config %s\n", postfixConfigPath)
  77. return
  78. }
  79. postfixCfg, err := ini.Load(postfixConfigPath)
  80. if err != nil {
  81. log.Fatalf("Unable to load %s: %s\n", postfixConfigPath, err)
  82. return
  83. }
  84. baseDir := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxBase).String()
  85. if !utils.DirectoryExists(baseDir) {
  86. log.Fatalf("Base dir %s doesn't exist, postfix is not configured proper way, check %s in %s\n", baseDir, PostfixKeyVirtualMailboxBase, postfixConfigPath)
  87. return
  88. }
  89. maps := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxMaps).String()
  90. mapsList := strings.Split(maps, ":")
  91. if len(mapsList) != 2 || mapsList[0] != "hash" {
  92. log.Fatalf("%s is not set proper way in %s. Should be hash:<path/to/virtualmailbox/map>, but %s provided\n", PostfixKeyVirtualMailboxMaps, postfixConfigPath, maps)
  93. return
  94. }
  95. if !utils.FileExists(mapsList[1]) {
  96. log.Fatalf("Virtual mailbox map %s doesn't exist, postfix is not configured proper way, check %s in %s\n", mapsList[1], PostfixKeyVirtualMailboxMaps, postfixConfigPath)
  97. return
  98. }
  99. domains := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxDomains).String()
  100. domainsList := strings.Split(domains, " ")
  101. var validDomains []string
  102. for _, domain := range domainsList {
  103. if utils.RegExpUtilsInstance().DomainChecker.MatchString(domain) {
  104. validDomains = append(validDomains, domain)
  105. }
  106. }
  107. if len(validDomains) < 0 {
  108. log.Fatalf("Virtual mailbox domains %s are not configured proper way, check %s in %s\n", domains, PostfixKeyVirtualMailboxDomains, postfixConfigPath)
  109. return
  110. }
  111. mongoUser := cfg.Section("").Key(KeyMongoUser).String()
  112. mongoPassword := cfg.Section("").Key(KeyMongoPassword).String()
  113. mongoAddress := cfg.Section("").Key(KeyMongoAddress).String()
  114. if mongoAddress == "" {
  115. mongoAddress = "localhost:27017"
  116. }
  117. attachmentsPath := cfg.Section("").Key(KeyAttachmentsPath).String()
  118. if attachmentsPath == "" {
  119. attachmentsPath = "attachments"
  120. }
  121. config = &gostfixConfig{
  122. VMailboxBase: baseDir,
  123. VMailboxMaps: mapsList[1],
  124. VMailboxDomains: validDomains,
  125. MongoUser: mongoUser,
  126. MongoPassword: mongoPassword,
  127. MongoAddress: mongoAddress,
  128. AttachmentsPath: attachmentsPath,
  129. }
  130. return
  131. }