config.go 4.4 KB

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