config.go 3.8 KB

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