config.go 5.0 KB

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