2024-07-22 18:41:22 +02:00
|
|
|
// Copyright (c) 2024 Julian Müller (ChaoticByte)
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
ApiFetchInterval int `json:"api_fetch_interval"` // in seconds
|
|
|
|
EnabledApiEndpoints []string `json:"enabled_api_endpoints"`
|
|
|
|
PersistentDataFilePath string `json:"datafile"`
|
|
|
|
LogLevel int `json:"loglevel"`
|
2025-06-12 00:00:52 +02:00
|
|
|
Lists *[]NotifyList `json:"lists"`
|
2024-07-22 18:41:22 +02:00
|
|
|
SmtpConfiguration SmtpSettings `json:"smtp"`
|
|
|
|
Template MailTemplateConfig `json:"template"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig() Config {
|
|
|
|
// Initial config
|
|
|
|
c := Config{
|
|
|
|
ApiFetchInterval: 60 * 10, // every 10 minutes,
|
|
|
|
EnabledApiEndpoints: []string{"bay", "bund"},
|
|
|
|
PersistentDataFilePath: "data.json",
|
|
|
|
LogLevel: 2,
|
2025-06-12 00:00:52 +02:00
|
|
|
Lists: &[]NotifyList{
|
|
|
|
{ Name: "Example List",
|
|
|
|
Recipients: []string{},
|
|
|
|
Filter: []Filter{},},
|
|
|
|
},
|
2024-07-22 18:41:22 +02:00
|
|
|
SmtpConfiguration: SmtpSettings{
|
2025-06-12 00:00:52 +02:00
|
|
|
From: "user@localhost",
|
|
|
|
User: "user@localhost",
|
|
|
|
Password: "change me :)",
|
|
|
|
ServerHost: "127.0.0.1",
|
2024-07-22 18:41:22 +02:00
|
|
|
ServerPort: 587},
|
|
|
|
Template: MailTemplateConfig{
|
|
|
|
SubjectTemplate: "",
|
|
|
|
BodyTemplate: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkConfig(config Config) {
|
2025-06-12 00:00:52 +02:00
|
|
|
if len(*config.Lists) < 1 {
|
2024-07-22 18:41:22 +02:00
|
|
|
logger.error("Configuration is incomplete")
|
2025-06-12 00:00:52 +02:00
|
|
|
panic(errors.New("no lists are configured"))
|
2024-07-22 18:41:22 +02:00
|
|
|
}
|
2025-06-12 00:00:52 +02:00
|
|
|
for _, l := range *config.Lists {
|
|
|
|
if len(l.Filter) < 1 {
|
2024-07-22 18:41:22 +02:00
|
|
|
logger.error("Configuration is incomplete")
|
2025-06-12 00:00:52 +02:00
|
|
|
panic(errors.New("list " + l.Name + " has no filter defined - at least [{'any': true/false}] should be configured"))
|
|
|
|
}
|
|
|
|
for _, r := range l.Recipients {
|
|
|
|
if !mailAddressIsValid(r) {
|
|
|
|
logger.error("Configuration includes invalid data")
|
|
|
|
panic(errors.New("'" + r + "' is not a valid e-mail address"))
|
|
|
|
}
|
2024-07-22 18:41:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !mailAddressIsValid(config.SmtpConfiguration.From) {
|
|
|
|
logger.error("Configuration includes invalid data")
|
|
|
|
panic(errors.New("'" + config.SmtpConfiguration.From + "' is not a valid e-mail address"))
|
|
|
|
}
|
|
|
|
}
|