2026-09-09 16:19:41 -06:00
|
|
|
package settings
|
|
|
|
|
|
|
|
|
|
import (
|
2026-09-10 22:31:23 -06:00
|
|
|
"bytes"
|
2026-10-02 18:00:29 -06:00
|
|
|
"fmt"
|
2026-10-05 15:43:05 -06:00
|
|
|
"io"
|
2026-10-02 18:00:29 -06:00
|
|
|
"net"
|
2026-09-09 22:14:55 -06:00
|
|
|
"os"
|
|
|
|
|
|
2026-09-09 16:19:41 -06:00
|
|
|
"github.com/pelletier/go-toml"
|
2026-09-10 22:31:23 -06:00
|
|
|
|
2026-10-05 15:43:05 -06:00
|
|
|
"go.destrealm.net/cwrap"
|
|
|
|
|
"go.destrealm.net/vfs"
|
2026-09-09 16:19:41 -06:00
|
|
|
)
|
|
|
|
|
|
2026-09-09 22:14:55 -06:00
|
|
|
type Settings struct {
|
2026-10-05 15:43:05 -06:00
|
|
|
file string `toml:"-"`
|
2026-10-07 18:57:26 -06:00
|
|
|
FS vfs.VFS `toml:"-"`
|
2026-09-12 15:41:20 -06:00
|
|
|
API *API `toml:"api"`
|
|
|
|
|
DailyScripture *DailyScripture `toml:"daily_scripture"`
|
|
|
|
|
Translations *Translations `toml:"translations"`
|
2026-10-23 23:27:24 -06:00
|
|
|
Scheduler *Scheduler `toml:"scheduler"`
|
2026-09-12 15:41:20 -06:00
|
|
|
System *System `toml:"system"`
|
|
|
|
|
Service *Service `toml:"service"`
|
|
|
|
|
Chat *Chat `toml:"chat"`
|
2026-10-16 00:14:24 -06:00
|
|
|
Debug *Debug `toml:"debug"`
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 15:43:05 -06:00
|
|
|
// Reload previously loaded configuration.
|
|
|
|
|
func (s *Settings) Reload() error {
|
2026-10-07 18:57:26 -06:00
|
|
|
st, err := LoadSettings(s.file, s.FS)
|
2026-10-05 15:43:05 -06:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.API = st.API
|
|
|
|
|
s.DailyScripture = st.DailyScripture
|
|
|
|
|
s.Translations = st.Translations
|
|
|
|
|
s.System = st.System
|
|
|
|
|
s.Service = st.Service
|
|
|
|
|
s.Chat = st.Chat
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Settings) Save() error {
|
|
|
|
|
buf, err := s.Marshal()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-10-07 18:57:26 -06:00
|
|
|
fp, err := s.FS.Mutable().OpenFile(s.file, os.O_TRUNC|os.O_CREATE, 0644)
|
2026-10-05 15:43:05 -06:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer fp.Close()
|
|
|
|
|
_, err = fp.Write(buf)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-09 16:19:41 -06:00
|
|
|
type API struct {
|
2026-09-12 15:41:20 -06:00
|
|
|
BaseURL string `toml:"base_url"`
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
2026-09-10 22:31:23 -06:00
|
|
|
type Chat struct {
|
2026-09-11 15:20:32 -06:00
|
|
|
Channels []int64 `toml:"channels"`
|
2026-09-10 22:31:23 -06:00
|
|
|
}
|
|
|
|
|
|
2026-09-09 16:19:41 -06:00
|
|
|
type DailyScripture struct {
|
2026-09-12 15:41:20 -06:00
|
|
|
UserSubmitted bool `toml:"user_submitted"`
|
|
|
|
|
Schedule string `toml:"schedule"`
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type System struct {
|
2026-10-16 00:14:24 -06:00
|
|
|
TimeZone string `toml:"timezone"`
|
|
|
|
|
DSN string `toml:"dsn"`
|
|
|
|
|
Logging *Logging `toml:"logging"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Logging struct {
|
|
|
|
|
Level string `toml:"level"`
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-23 23:27:24 -06:00
|
|
|
type Scheduler struct {
|
|
|
|
|
Announcements struct {
|
|
|
|
|
Weekly string `toml:"weekly"`
|
|
|
|
|
Daily string `toml:"daily"`
|
|
|
|
|
} `toml:"announcements"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 19:57:32 -06:00
|
|
|
type Service struct {
|
2026-10-16 00:14:24 -06:00
|
|
|
BotToken string `toml:"secret"`
|
|
|
|
|
Method string `toml:"method"`
|
|
|
|
|
Webhook *Webhook `toml:"webhook"`
|
2026-09-11 19:57:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Webhook struct {
|
2026-09-12 15:41:20 -06:00
|
|
|
URI string `toml:"uri"`
|
|
|
|
|
Port int `toml:"port"`
|
|
|
|
|
Host string `toml:"host"`
|
2026-09-11 19:57:32 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-02 18:00:29 -06:00
|
|
|
func (w *Webhook) ListenAddr() string {
|
|
|
|
|
return net.JoinHostPort(w.Host, fmt.Sprintf("%d", w.Port))
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-09 16:19:41 -06:00
|
|
|
type Translations struct {
|
2026-09-09 22:14:55 -06:00
|
|
|
BaseDir string `toml:"basedir"`
|
2026-09-25 23:16:55 -06:00
|
|
|
Current string `toml:"current"`
|
2026-09-09 22:14:55 -06:00
|
|
|
Active []string `toml:"active"`
|
2026-10-23 11:09:01 -06:00
|
|
|
Default string `toml:"default"`
|
2026-09-09 22:14:55 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-16 00:14:24 -06:00
|
|
|
type Debug struct {
|
|
|
|
|
Commands struct {
|
2026-10-21 19:44:38 -06:00
|
|
|
// Register bot commands with Telegram. If false and `Reset` is true,
|
|
|
|
|
// commands will be removed and not re-added.
|
|
|
|
|
Register bool `toml:"register"`
|
2026-10-16 00:14:24 -06:00
|
|
|
// Resets the command list by deleting all existing commands.
|
|
|
|
|
Reset bool `toml:"reset"`
|
|
|
|
|
} `toml:"commands"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 22:31:23 -06:00
|
|
|
func (c *Settings) Marshal() ([]byte, error) {
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
|
|
|
|
|
enc := toml.NewEncoder(cwrap.New(buf))
|
|
|
|
|
enc.ArraysWithOneElementPerLine(true)
|
|
|
|
|
enc.Indentation(" ")
|
|
|
|
|
enc.Order(toml.OrderPreserve)
|
|
|
|
|
enc.PromoteAnonymous(false)
|
|
|
|
|
|
|
|
|
|
if err := enc.Encode(c); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 15:43:05 -06:00
|
|
|
func LoadSettings(name string, fs vfs.VFS) (*Settings, error) {
|
2026-10-23 09:07:39 -06:00
|
|
|
conf := &Settings{
|
|
|
|
|
API: &API{},
|
|
|
|
|
Chat: &Chat{
|
|
|
|
|
Channels: make([]int64, 0),
|
|
|
|
|
},
|
|
|
|
|
DailyScripture: &DailyScripture{
|
|
|
|
|
UserSubmitted: false,
|
|
|
|
|
Schedule: "0 8 * * *",
|
|
|
|
|
},
|
|
|
|
|
System: &System{
|
|
|
|
|
TimeZone: "UTC",
|
|
|
|
|
DSN: "",
|
|
|
|
|
Logging: &Logging{
|
|
|
|
|
Level: "info",
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-10-23 23:27:24 -06:00
|
|
|
Scheduler: &Scheduler{
|
|
|
|
|
Announcements: struct {
|
|
|
|
|
Weekly string `toml:"weekly"`
|
|
|
|
|
Daily string `toml:"daily"`
|
|
|
|
|
}{
|
|
|
|
|
Weekly: "Sunday",
|
|
|
|
|
Daily: "30 7 * * *",
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-10-23 09:07:39 -06:00
|
|
|
Service: &Service{
|
|
|
|
|
BotToken: "",
|
|
|
|
|
Method: "polling",
|
|
|
|
|
Webhook: &Webhook{
|
|
|
|
|
URI: "",
|
|
|
|
|
Port: 0,
|
|
|
|
|
Host: "localhost",
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-10-23 11:09:01 -06:00
|
|
|
Translations: &Translations{
|
|
|
|
|
BaseDir: "dat/translations",
|
|
|
|
|
Current: "bsb",
|
|
|
|
|
Active: []string{"bsb", "kjv"},
|
|
|
|
|
Default: "bsb",
|
|
|
|
|
},
|
2026-10-23 23:27:24 -06:00
|
|
|
Debug: &Debug{
|
|
|
|
|
Commands: struct {
|
|
|
|
|
Register bool `toml:"register"`
|
|
|
|
|
Reset bool `toml:"reset"`
|
|
|
|
|
}{
|
|
|
|
|
Register: true,
|
|
|
|
|
Reset: false,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-10-23 09:07:39 -06:00
|
|
|
}
|
2026-10-05 15:43:05 -06:00
|
|
|
file, err := fs.Open(name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
2026-09-09 22:14:55 -06:00
|
|
|
|
2026-10-05 15:43:05 -06:00
|
|
|
buf, err := io.ReadAll(file)
|
2026-09-09 22:14:55 -06:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, ErrReadingSettings.Do(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = toml.Unmarshal(buf, conf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, ErrReadingSettings.Do(err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 15:43:05 -06:00
|
|
|
conf.file = name
|
2026-09-09 22:14:55 -06:00
|
|
|
return conf, nil
|
|
|
|
|
}
|
2026-09-10 22:31:23 -06:00
|
|
|
|
|
|
|
|
func SaveSettings(name string, settings *Settings) error {
|
2026-10-05 15:43:05 -06:00
|
|
|
return settings.Save()
|
2026-09-10 22:31:23 -06:00
|
|
|
}
|