wid-notifier/main.go

113 lines
3.3 KiB
Go
Raw Permalink Normal View History

// Copyright (c) 2023 Julian Müller (ChaoticByte)
2023-10-11 22:14:01 +02:00
package main
import (
"fmt"
"net/smtp"
"os"
"time"
)
var logger Logger
2023-10-11 22:14:01 +02:00
func main() {
// get cli arguments
args := os.Args
if len(args) < 2 {
fmt.Printf("Usage: %v <configfile>\nIf the config file doesn't exist, a incomplete configuration with default values is created.\n", args[0])
os.Exit(1)
}
configFilePath := os.Args[1]
// create logger
logger = NewLogger(2)
2023-10-11 22:14:01 +02:00
// init
logger.info("Initializing ...")
defer logger.info("Exiting ...")
2023-10-11 22:14:01 +02:00
config := NewDataStore(
configFilePath,
NewConfig(),
true,
0600,
).data.(Config)
persistent := NewDataStore(
config.PersistentDataFilePath,
NewPersistentData(config),
false,
0640)
logger.LogLevel = config.LogLevel
logger.debug("Checking configuration file ...")
2023-10-11 22:14:01 +02:00
checkConfig(config)
// create mail template from mail template config
logger.debug("Parsing mail template ...")
2023-10-11 22:14:01 +02:00
if config.Template.SubjectTemplate == "" {
logger.debug("Using default template for mail subject")
2023-10-11 22:14:01 +02:00
config.Template.SubjectTemplate = DEFAULT_SUBJECT_TEMPLATE
}
if config.Template.BodyTemplate == "" {
logger.debug("Using default template for mail body")
2023-10-11 22:14:01 +02:00
config.Template.BodyTemplate = DEFAULT_BODY_TEMPLATE
}
mailTemplate := NewTemplateFromTemplateConfig(config.Template)
// mail authentication from config
mailAuth := smtp.PlainAuth(
"",
config.SmtpConfiguration.User,
config.SmtpConfiguration.Password,
config.SmtpConfiguration.ServerHost,
)
// filter out disabled api endpoints
enabledApiEndpoints := []ApiEndpoint{}
for _, a := range apiEndpoints {
for _, b := range config.EnabledApiEndpoints {
if a.Id == b {
logger.debug("Endpoint '" + b + "' is enabled")
2023-10-11 22:14:01 +02:00
enabledApiEndpoints = append(enabledApiEndpoints, a)
}
}
}
// main loop
logger.debug("Entering main loop ...")
2023-10-11 22:14:01 +02:00
for {
t1 := time.Now().UnixMilli()
2023-10-11 22:14:01 +02:00
newNotices := []WidNotice{}
cache := map[string][]byte{}
2023-10-11 22:14:01 +02:00
for _, a := range enabledApiEndpoints {
logger.info("Querying endpoint '" + a.Id + "' for new notices ...")
2023-10-11 22:14:01 +02:00
n, t, err := a.getNotices(persistent.data.(PersistentData).LastPublished[a.Id])
if err != nil {
// retry
logger.warn("Couldn't query notices from API endpoint '" + a.Id + "'. Retrying ...")
logger.warn(err)
2023-10-11 22:14:01 +02:00
n, t, err = a.getNotices(persistent.data.(PersistentData).LastPublished[a.Id])
}
if err != nil {
// ok then...
logger.error("Couldn't query notices from API endpoint '" + a.Id + "'")
logger.error(err)
} else if len(n) > 0 {
2023-10-11 22:14:01 +02:00
newNotices = append(newNotices, n...)
persistent.data.(PersistentData).LastPublished[a.Id] = t
persistent.save()
}
}
logger.debug(fmt.Sprintf("Got %v new notices", len(newNotices)))
if len(newNotices) > 0 {
logger.info("Sending email notifications ...")
recipientsNotified := 0
for _, r := range config.Recipients {
err := r.filterAndSendNotices(newNotices, mailTemplate, mailAuth, config.SmtpConfiguration, &cache)
if err != nil {
logger.error(err)
} else {
recipientsNotified++
}
}
logger.info(fmt.Sprintf("Email notifications sent to %v of %v recipients", recipientsNotified, len(config.Recipients)))
2023-10-11 22:14:01 +02:00
}
t2 := time.Now().UnixMilli()
dt := int(t2 - t1)
time.Sleep(time.Millisecond * time.Duration((config.ApiFetchInterval * 1000) - dt))
2023-10-11 22:14:01 +02:00
}
}