2020-02-10 21:48:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-05-22 16:10:21 +02:00
|
|
|
"os"
|
2020-02-10 21:48:41 +01:00
|
|
|
|
|
|
|
toml "github.com/pelletier/go-toml"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Validation struct {
|
|
|
|
Param string
|
|
|
|
CmdFlag string
|
|
|
|
ConfFlag string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerConfig struct {
|
|
|
|
Address string `toml:address,omitempty`
|
|
|
|
Port int64 `toml:port,omitempty`
|
|
|
|
Encryption bool `toml:encryption,omitempty`
|
|
|
|
User string `toml:user,omitempty`
|
|
|
|
Password string `toml:password,omitempty`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ServerConfig) String() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"\tAddress: %s\n\tPort: %d\n\tEncryption: %t\n\tUser: %s\n\tPassword: %s\n",
|
|
|
|
s.Address,
|
|
|
|
s.Port,
|
|
|
|
s.Encryption,
|
|
|
|
s.User,
|
|
|
|
s.Password,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2020-05-22 16:10:21 +02:00
|
|
|
Server *ServerConfig `toml:server,omitempty`
|
|
|
|
From string `toml:from,omitempty`
|
|
|
|
To []string `toml:to,omitempty`
|
|
|
|
Cc []string `toml:cc,omitempty`
|
|
|
|
Bcc []string `toml:bcc,omitempty`
|
|
|
|
Subject string `toml:subject,omitempty`
|
|
|
|
Text string `toml:text,omitempty`
|
|
|
|
Attachments []string `toml:attachments,omitempty`
|
2020-02-10 21:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) String() string {
|
2020-05-22 16:10:21 +02:00
|
|
|
msg := fmt.Sprintf(
|
|
|
|
"From: %s\nTo: %s\nCc: %s\nBcc: %s\nSubject: %s\nText:\n%s\nServer:\n%s\n",
|
2020-02-10 21:48:41 +01:00
|
|
|
c.From,
|
|
|
|
c.To,
|
|
|
|
c.Cc,
|
|
|
|
c.Bcc,
|
|
|
|
c.Subject,
|
|
|
|
c.Text,
|
|
|
|
c.Server,
|
|
|
|
)
|
2020-05-22 16:10:21 +02:00
|
|
|
msg += "Attachments:"
|
|
|
|
for _, attachment := range c.Attachments {
|
|
|
|
msg += fmt.Sprintf(" - %s\n", attachment)
|
|
|
|
}
|
|
|
|
return msg
|
2020-02-10 21:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig() *Config {
|
|
|
|
server := &ServerConfig{}
|
|
|
|
config := &Config{}
|
|
|
|
config.Server = server
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerConfig) Merge(key string, value interface{}) {
|
|
|
|
Merge(key, value, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Merge(key string, value interface{}) {
|
|
|
|
Merge(key, value, c)
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:57:25 +01:00
|
|
|
func readConfig(configPath, section string) (*Config, error) {
|
2020-02-10 21:48:41 +01:00
|
|
|
config := NewConfig()
|
|
|
|
tree, err := toml.LoadFile(configPath)
|
|
|
|
if err != nil {
|
2020-02-17 16:57:25 +01:00
|
|
|
return config, err
|
2020-02-10 21:48:41 +01:00
|
|
|
}
|
|
|
|
keys := tree.Keys()
|
|
|
|
|
|
|
|
if !IsPresent(keys, section) {
|
2020-02-17 16:57:25 +01:00
|
|
|
return config, errors.New("missing section")
|
2020-02-10 21:48:41 +01:00
|
|
|
}
|
|
|
|
sub := tree.Get(section).(*toml.Tree)
|
|
|
|
err = sub.Unmarshal(config)
|
2020-02-17 16:57:25 +01:00
|
|
|
return config, err
|
2020-02-10 21:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkValidity(validations *[]Validation, obj interface{}, name, cmd, param string) {
|
|
|
|
if IsEmpty(obj) {
|
|
|
|
*validations = append(*validations, Validation{name, cmd, param})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Validate() error {
|
|
|
|
var validations = []Validation{}
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
checkValidity(&validations, c.Server.Address, "server address", "server-address", "server.address")
|
|
|
|
checkValidity(&validations, c.Server.Port, "server port", "server-port", "server.port")
|
|
|
|
if !c.Server.Encryption {
|
|
|
|
Warning.Ln("Warning: not using encryption!")
|
|
|
|
}
|
|
|
|
checkValidity(&validations, c.Server.User, "username", "user", "server.user")
|
|
|
|
checkValidity(&validations, c.Server.Password, "password", "password", "server.password")
|
|
|
|
checkValidity(&validations, c.From, "from", "from", "from")
|
|
|
|
checkValidity(&validations, c.To, "to", "to", "to")
|
|
|
|
checkValidity(&validations, c.Subject, "subject", "sub", "subject")
|
|
|
|
checkValidity(&validations, c.Text, "body", "", "text")
|
|
|
|
|
|
|
|
Debug.F("Lengths:\n\tTo: %d\n\tCc: %d\n\tBcc: %d", len(c.To), len(c.Cc), len(c.Bcc))
|
|
|
|
|
|
|
|
if len(validations) > 0 {
|
|
|
|
for _, v := range validations {
|
|
|
|
if v.CmdFlag == "" {
|
|
|
|
msg += fmt.Sprintf("%s: pass a value either via stdin or in configuration file section (%s)\n", v.Param, v.ConfFlag)
|
|
|
|
} else {
|
|
|
|
msg += fmt.Sprintf("%s: pass a value either via command line (-%s) or in configuration file section (%s)\n", v.Param, v.CmdFlag, v.ConfFlag)
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 16:10:21 +02:00
|
|
|
}
|
|
|
|
if len(c.Attachments) > 0 {
|
|
|
|
for _, file := range c.Attachments {
|
|
|
|
if _, err := os.Lstat(file); err != nil {
|
|
|
|
msg += fmt.Sprintf("Error with attachment: %s -> %s\n", file, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if msg != "" {
|
2020-02-10 21:48:41 +01:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|