-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
215 lines (176 loc) · 5.46 KB
/
config.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Pieter De Ridder
* 2017
*
* Base64 smtp mail client
* Can be cross compiled for Windows, Linux, Mac OS X and Solaris on Intel x86-64
*
* ------------------------------------------------------------
*
* encoding smtpmail go file
* extra function for Base64 encoding following RFC2047
*
*/
package main
import (
"strings"
"bufio" // files
"os" // os.Args
"strconv"
)
// ----- file I/O ------
/* read a text file line by line to a slice */
func readLines(path string) ([]string, error) {
var lines []string
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
/* file exists check */
func fileExists(path string) bool {
var found bool = false
_, err := os.Stat(path)
if err == nil {
found = true
}
/*if os.IsNotExist(err) {
found false
}*/
return found
}
/* search of the config file */
func fetchConfigFile() string {
var configFile string
// used defined config file with cmdline parameter "-config <configfile>"
if (len(configFileOverride) > 0) {
if (fileExists(configFileOverride)) {
configFile = configFileOverride
debugprint("user config " + configFileOverride + " set.")
} else {
debugprint("user config " + configFileOverride + " not found.")
warning("user defined config not present!", WARNING)
warning("given file: " + configFileOverride, WARNING)
}
} else {
// application auto search
var exeFilename string = stripExeFilenameExt()
var currentPath string = stripExeFilepath()
switch getOSFlavor() {
case "win32":
var win32SearchPath string = currentPath + exeFilename + ".conf"
// windows has a static folder patch "<current folder>\<exefilename>.conf"
// filename : "<current folder>\<exefilename>.conf"
if (fileExists(win32SearchPath)) {
configFile = win32SearchPath
debugprint("win config " + win32SearchPath + " set.")
} else {
debugprint("win config " + win32SearchPath + " not found.")
configFile = "" // return empty
}
case "bsd":
fallthrough
case "darwin":
fallthrough
case "unix":
var unixSearchPath string = "/etc/" + exeFilename + ".conf"
if (fileExists(unixSearchPath)) {
// try /etc folder
// filename : "/etc/<exefilename>.conf"
configFile = unixSearchPath
debugprint("unix config " + unixSearchPath + " set.")
} else {
debugprint("unix config " + unixSearchPath + " not found.")
// fallback to "current" folder
// filename : "<current folder>\<exefilename>.conf"
unixSearchPath = currentPath + exeFilename + ".conf"
if (fileExists(unixSearchPath)) {
configFile = unixSearchPath
debugprint("unix config " + unixSearchPath + " set.")
} else {
configFile = "" // return empty
debugprint("unix config " + unixSearchPath + " not found.")
}
}
default:
configFile = "" // return empty
}
}
return configFile
}
/* read and parse config file */
func readConfigFile() {
if !(skipConfigFileLoad) {
var configFile string = fetchConfigFile()
if (len(configFile) > 0) {
lines, err := readLines(configFile)
debugprint("reading config file")
if err == nil {
print("config file present")
for l := 0; l < len(lines); l++ {
if (len(lines[l]) >= 2 ) {
line := strings.TrimSpace(lines[l]) // rim the whole line first and store it in a var
if !(strings.HasPrefix(line, "//")) { //exclude comments starting with a double slash
codes := strings.Split(line, "=") // try splitting on a '=' char
opcode := strings.TrimSpace(codes[0]) // the "opcode", or command so to say
opvalue := strings.TrimSpace(codes[1]) // the value of the opcode
// parse opcodes
switch (opcode) {
case "server", "smtpserver":
smtpServer = opvalue
case "port", "smtpport":
smtpPort, _ = strconv.Atoi(opvalue)
case "authusername", "authuser", "username":
userName = opvalue
case "authpassword", "authpwd", "password":
userPasswd = opvalue
case "from", "frommail":
fromMailAddress = opvalue
case "body":
mailBody = opvalue
case "subject", "title":
mailSubject = opvalue
case "authlogin", "authmethod", "method":
if (opvalue == "1") {
useAuthLogin = true
} else {
useAuthLogin = false
}
case "verbose":
if (opvalue == "1") {
verbose = true
} else {
verbose = false
}
case "silent":
if (opvalue == "1") {
silent = true
} else {
silent = false
}
/*case "debug":
if (opvalue == "1") {
debug = true
} else {
debug = false
}*/
}
}
}
}
} else {
warning("problem while reading the config file!", WARNING) // throw as warning instead of error, because the application should still work without config file
}
} else {
print("no config file present.")
}
} else {
print("user skipped config file.")
}
}