123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package config
- import (
- "log"
- "strings"
- "sync"
- utils "git.semlanik.org/semlanik/gostfix/utils"
- ini "gopkg.in/go-ini/ini.v1"
- )
- const configPath = "data/main.ini"
- const (
- KeyPostfixConfig = "postfix_config"
- KeyMongoAddress = "mongo_address"
- KeyMongoUser = "mongo_user"
- KeyMongoPassword = "mongo_password"
- KeyAttachmentsPath = "attachments_path"
- KeyAttachmentsUser = "attachments_user"
- KeyAttachmentsPassword = "attachments_password"
- )
- const (
- PostfixKeyVirtualMailboxMaps = "virtual_mailbox_maps"
- PostfixKeyVirtualMailboxBase = "virtual_mailbox_base"
- PostfixKeyVirtualMailboxDomains = "virtual_mailbox_domains"
- )
- type GostfixConfig gostfixConfig
- var (
- once sync.Once
- instance *gostfixConfig
- )
- func ConfigInstance() *GostfixConfig {
- once.Do(func() {
- instance, _ = newConfig()
- })
- return (*GostfixConfig)(instance)
- }
- type gostfixConfig struct {
- VMailboxMaps string
- VMailboxBase string
- VMailboxDomains []string
- MongoUser string
- MongoPassword string
- MongoAddress string
- AttachmentsPath string
- }
- func newConfig() (config *gostfixConfig, err error) {
- cfg, err := ini.Load(configPath)
- if err != nil {
- log.Fatalf("Unable to load %s\n", configPath)
- return
- }
- postfixConfigPath := cfg.Section("").Key(KeyPostfixConfig).String()
- if !utils.FileExists(postfixConfigPath) {
- log.Fatalf("Unable to find postfix config %s\n", postfixConfigPath)
- return
- }
- postfixCfg, err := ini.Load(postfixConfigPath)
- if err != nil {
- log.Fatalf("Unable to load %s: %s\n", postfixConfigPath, err)
- return
- }
- baseDir := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxBase).String()
- if !utils.DirectoryExists(baseDir) {
- log.Fatalf("Base dir %s doesn't exist, postfix is not configured proper way, check %s in %s\n", baseDir, PostfixKeyVirtualMailboxBase, postfixConfigPath)
- return
- }
- maps := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxMaps).String()
- mapsList := strings.Split(maps, ":")
- if len(mapsList) != 2 || mapsList[0] != "hash" {
- log.Fatalf("%s is not set proper way in %s. Should be hash:<path/to/virtualmailbox/map>, but %s provided\n", PostfixKeyVirtualMailboxMaps, postfixConfigPath, maps)
- return
- }
- if !utils.FileExists(mapsList[1]) {
- log.Fatalf("Virtual mailbox map %s doesn't exist, postfix is not configured proper way, check %s in %s\n", mapsList[1], PostfixKeyVirtualMailboxMaps, postfixConfigPath)
- return
- }
- domains := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxDomains).String()
- domainsList := strings.Split(domains, " ")
- var validDomains []string
- for _, domain := range domainsList {
- if utils.RegExpUtilsInstance().DomainChecker.MatchString(domain) {
- validDomains = append(validDomains, domain)
- }
- }
- if len(validDomains) < 0 {
- log.Fatalf("Virtual mailbox domains %s are not configured proper way, check %s in %s\n", domains, PostfixKeyVirtualMailboxDomains, postfixConfigPath)
- return
- }
- mongoUser := cfg.Section("").Key(KeyMongoUser).String()
- mongoPassword := cfg.Section("").Key(KeyMongoPassword).String()
- mongoAddress := cfg.Section("").Key(KeyMongoAddress).String()
- if mongoAddress == "" {
- mongoAddress = "localhost:27017"
- }
- attachmentsPath := cfg.Section("").Key(KeyAttachmentsPath).String()
- if attachmentsPath == "" {
- attachmentsPath = "attachments"
- }
- config = &gostfixConfig{
- VMailboxBase: baseDir,
- VMailboxMaps: mapsList[1],
- VMailboxDomains: validDomains,
- MongoUser: mongoUser,
- MongoPassword: mongoPassword,
- MongoAddress: mongoAddress,
- AttachmentsPath: attachmentsPath,
- }
- return
- }
|