aboutsummaryrefslogtreecommitdiff
path: root/config/config.go
blob: 153d59fb6afbb008aced266755d72bfa8c6fd0f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package config

import (
	"os"
	"path/filepath"
	"strings"

	"dove/utils/logger"
	"dove/utils/toml"
)

type Http struct {
	Debug    bool   `toml:"debug" default:"false"`
	Username string `toml:"username"`
	Password string `toml:"password"`
}

type Smtp struct {
	Domain         string `toml:"domain" default:"localhost"`
	MaxMessageSize int    `toml:"max_message_size" default:"26214400"`
	ReadTimeout    int    `toml:"read_timeout" default:"30"`
	WriteTimeout   int    `toml:"write_timeout" default:"30"`
	AuthRequired   bool   `toml:"auth_required" default:"false"`
	Username       string `toml:"username"`
	Password       string `toml:"password"`
	TLSEnabled     bool   `toml:"tls_enabled" default:"false"`
	TLSCertPath    string `toml:"tls_cert"`
	TLSKeyPath     string `toml:"tls_key"`
}

type Imap struct {
	AuthRequired bool   `toml:"auth_required" default:"false"`
	Username     string `toml:"username"`
	Password     string `toml:"password"`
	TLSEnabled   bool   `toml:"tls_enabled" default:"false"`
	TLSCertPath  string `toml:"tls_cert"`
	TLSKeyPath   string `toml:"tls_key"`
}

type Pop3 struct {
	AuthRequired bool   `toml:"auth_required" default:"false"`
	Username     string `toml:"username"`
	Password     string `toml:"password"`
	TLSEnabled   bool   `toml:"tls_enabled" default:"false"`
	TLSCertPath  string `toml:"tls_cert"`
	TLSKeyPath   string `toml:"tls_key"`
}

type Storage struct {
	DataDir       string `toml:"data_dir" default:"storage"`
	MaxObjectSize int64  `toml:"max_object_size" default:"536870912"`
}

var (
	HTTP Http
	SMTP Smtp
	IMAP Imap
	POP3 Pop3
	S3   Storage
)

var (
	AuthEnabled bool
	DataDir     string
	DevMode     bool
)

func init() {
	DevMode = isDevelopmentMode()
	osConfigDirectory := resolveOSConfigDirectory()
	DataDir = resolveDataDirectory(DevMode, osConfigDirectory)

	configFilePath := resolveConfigFilePath(DevMode, osConfigDirectory)

	if createError := ensureConfigFileExists(configFilePath); createError != nil {
		logger.Fatalf(LogPrefix, ConfigCreateFailed, createError)
	}

	if loadError := loadConfigFile(configFilePath); loadError != nil {
		logger.Fatalf(LogPrefix, ConfigFileLoadFailed, loadError)
	}

	for _, section := range []any{&HTTP, &SMTP, &IMAP, &POP3, &S3} {
		if parseError := parseSection(section); parseError != nil {
			logger.Fatalf(LogPrefix, ConfigSectionFailed, parseError)
		}
	}

	AuthEnabled = isAuthEnabled()

	logger.Successf(LogPrefix, ConfigLoaded)
}

func isDevelopmentMode() bool {
	executablePath, pathError := os.Executable()
	if pathError != nil {
		return false
	}

	return strings.Contains(executablePath, GoBuildPathIndicator) ||
		strings.Contains(executablePath, GoBuildAltIndicator)
}

func resolveOSConfigDirectory() string {
	configDirectory, directoryError := os.UserConfigDir()
	if directoryError != nil {
		return CurrentDirectory
	}

	return filepath.Join(configDirectory, ApplicationDirectory)
}

func resolveConfigFilePath(developmentMode bool, osConfigDirectory string) string {
	switch developmentMode {
	case true:
		return filepath.Join(CurrentDirectory, ConfigFileName)
	default:
		return filepath.Join(osConfigDirectory, ConfigFileName)
	}
}

func resolveDataDirectory(developmentMode bool, osConfigDirectory string) string {
	switch developmentMode {
	case true:
		return CurrentDirectory
	default:
		return osConfigDirectory
	}
}

func ensureConfigFileExists(configFilePath string) error {
	if _, statError := os.Stat(configFilePath); statError == nil {
		return nil
	}

	configDirectory := filepath.Dir(configFilePath)
	if mkdirError := os.MkdirAll(configDirectory, ConfigDirectoryPermissions); mkdirError != nil {
		return mkdirError
	}

	return os.WriteFile(configFilePath, defaultConfigContent, ConfigFilePermissions)
}

func loadConfigFile(configFilePath string) error {
	if _, statError := os.Stat(configFilePath); statError != nil {
		return nil
	}

	return toml.LoadFile(configFilePath)
}

func parseSection(target any) error {
	return toml.Parse(target)
}

func isAuthEnabled() bool {
	return HTTP.Username != "" && HTTP.Password != ""
}