2022-12-11 04:08:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2023-01-10 00:10:05 +00:00
|
|
|
"strconv"
|
|
|
|
"text/template"
|
2022-12-11 04:08:29 +00:00
|
|
|
|
2023-01-10 00:10:05 +00:00
|
|
|
"github.com/Masterminds/sprig"
|
2022-12-11 04:08:29 +00:00
|
|
|
"github.com/pelletier/go-toml/v2"
|
2023-04-21 03:04:46 +00:00
|
|
|
"go.elara.ws/logger/log"
|
|
|
|
"go.elara.ws/pcre"
|
2022-12-11 04:08:29 +00:00
|
|
|
)
|
|
|
|
|
2023-08-19 13:56:42 +00:00
|
|
|
type ConfigFile struct {
|
2022-12-11 04:08:29 +00:00
|
|
|
Lemmy struct {
|
|
|
|
InstanceURL string `toml:"instanceURL"`
|
|
|
|
Account struct {
|
|
|
|
UserOrEmail string `toml:"userOrEmail"`
|
|
|
|
Password string `toml:"password"`
|
|
|
|
} `toml:"account"`
|
|
|
|
} `toml:"lemmy"`
|
|
|
|
Replies []Reply `toml:"reply"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Reply struct {
|
|
|
|
Regex string `toml:"regex"`
|
|
|
|
Msg string `toml:"msg"`
|
|
|
|
}
|
|
|
|
|
2023-08-19 13:56:42 +00:00
|
|
|
type Config struct {
|
|
|
|
ConfigFile *ConfigFile
|
|
|
|
Regexes map[string]*pcre.Regexp
|
|
|
|
Tmpls map[string]*template.Template
|
|
|
|
}
|
2022-12-11 04:08:29 +00:00
|
|
|
|
2023-08-19 13:56:42 +00:00
|
|
|
func loadConfig(path string) (Config, error) {
|
2022-12-11 04:08:29 +00:00
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2023-08-19 13:56:42 +00:00
|
|
|
return Config{}, err
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Mode().Perm() != 0o600 {
|
|
|
|
log.Fatal("Your config file's permissions are insecure. Please use chmod to set them to 600. Refusing to start.").Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
fl, err := os.Open(path)
|
|
|
|
if err != nil {
|
2023-08-19 13:56:42 +00:00
|
|
|
return Config{}, err
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 13:56:42 +00:00
|
|
|
cfgFile := &ConfigFile{}
|
|
|
|
err = toml.NewDecoder(fl).Decode(cfgFile)
|
2022-12-11 04:08:29 +00:00
|
|
|
if err != nil {
|
2023-08-19 13:56:42 +00:00
|
|
|
return Config{}, err
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 13:56:42 +00:00
|
|
|
compiledRegexes, compiledTmpls, err := compileReplies(cfgFile.Replies)
|
2022-12-11 04:08:29 +00:00
|
|
|
if err != nil {
|
2023-08-19 13:56:42 +00:00
|
|
|
return Config{}, err
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 13:56:42 +00:00
|
|
|
cfg := Config{cfgFile, compiledRegexes, compiledTmpls}
|
|
|
|
validateConfig(cfg)
|
|
|
|
return cfg, nil
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 13:56:42 +00:00
|
|
|
func compileReplies(replies []Reply) (map[string]*pcre.Regexp, map[string]*template.Template, error) {
|
|
|
|
compiledRegexes := map[string]*pcre.Regexp{}
|
|
|
|
compiledTmpls := map[string]*template.Template{}
|
|
|
|
|
2023-01-10 00:10:05 +00:00
|
|
|
for i, reply := range replies {
|
2022-12-11 04:08:29 +00:00
|
|
|
if _, ok := compiledRegexes[reply.Regex]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
re, err := pcre.Compile(reply.Regex)
|
|
|
|
if err != nil {
|
2023-08-19 13:56:42 +00:00
|
|
|
return nil, nil, err
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
|
|
|
compiledRegexes[reply.Regex] = re
|
2023-01-10 00:10:05 +00:00
|
|
|
|
|
|
|
tmpl, err := template.
|
|
|
|
New(strconv.Itoa(i)).
|
|
|
|
Funcs(sprig.TxtFuncMap()).
|
|
|
|
Parse(reply.Msg)
|
|
|
|
if err != nil {
|
2023-08-19 13:56:42 +00:00
|
|
|
return nil, nil, err
|
2023-01-10 00:10:05 +00:00
|
|
|
}
|
|
|
|
compiledTmpls[reply.Regex] = tmpl
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
2023-08-19 13:56:42 +00:00
|
|
|
|
|
|
|
return compiledRegexes, compiledTmpls, nil
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 13:56:42 +00:00
|
|
|
func validateConfig(cfg Config) {
|
|
|
|
_, err := url.Parse(cfg.ConfigFile.Lemmy.InstanceURL)
|
2022-12-11 04:08:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Lemmy instance URL is not valid").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
2023-08-19 13:56:42 +00:00
|
|
|
for i, reply := range cfg.ConfigFile.Replies {
|
|
|
|
re := cfg.Regexes[reply.Regex]
|
2022-12-11 04:08:29 +00:00
|
|
|
|
|
|
|
if re.MatchString(reply.Msg) {
|
|
|
|
log.Fatal("Regular expression matches message. This may create an infinite loop. Refusing to start.").Int("reply-index", i).Send()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|