2021-09-05 13:31:06 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-09 23:12:29 +02:00
|
|
|
"flag"
|
2021-09-14 21:37:37 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-09-05 19:20:27 +02:00
|
|
|
|
2021-09-09 23:12:29 +02:00
|
|
|
"dev.sum7.eu/genofire/golang-lib/file"
|
2021-09-05 19:20:27 +02:00
|
|
|
"github.com/bdlm/log"
|
2021-09-15 18:54:23 +02:00
|
|
|
"github.com/bdlm/std/logger"
|
2021-09-09 18:31:26 +02:00
|
|
|
"unifiedpush.org/go/np2p_dbus/distributor"
|
2021-09-14 21:37:37 +02:00
|
|
|
"unifiedpush.org/go/np2p_dbus/storage"
|
2021-09-05 13:31:06 +02:00
|
|
|
)
|
|
|
|
|
2021-09-09 18:31:26 +02:00
|
|
|
var dbus *distributor.DBus
|
2021-09-05 19:20:27 +02:00
|
|
|
|
2021-09-14 21:37:37 +02:00
|
|
|
type configData struct {
|
|
|
|
StoragePath string `toml"storage_path"`
|
|
|
|
XMPP XMPPService `toml:"xmpp"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultPath(given, filename string) string {
|
|
|
|
if given != "" {
|
|
|
|
return given
|
|
|
|
}
|
|
|
|
basedir := os.Getenv("XDG_CONFIG_HOME")
|
|
|
|
if len(basedir) == 0 {
|
|
|
|
basedir = os.Getenv("HOME")
|
|
|
|
if len(basedir) == 0 {
|
|
|
|
basedir = "./" // FIXME: set to cwd if dunno wth is going on
|
|
|
|
}
|
|
|
|
basedir = filepath.Join(basedir, ".config")
|
|
|
|
}
|
|
|
|
basedir = filepath.Join(basedir, "unifiedpushxmpp")
|
|
|
|
os.MkdirAll(basedir, 0o700)
|
|
|
|
return filepath.Join(basedir, filename)
|
|
|
|
}
|
|
|
|
|
2021-09-09 18:31:26 +02:00
|
|
|
func main() {
|
2021-09-14 21:37:37 +02:00
|
|
|
configPath := ""
|
2021-09-15 18:54:23 +02:00
|
|
|
loglevel := 40
|
2021-09-09 23:12:29 +02:00
|
|
|
flag.StringVar(&configPath, "c", configPath, "path to configuration file")
|
2021-09-15 18:54:23 +02:00
|
|
|
flag.IntVar(&loglevel, "l", loglevel, "loglevel")
|
2021-09-09 23:12:29 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2021-09-15 18:54:23 +02:00
|
|
|
log.SetLevel(logger.Level(loglevel))
|
|
|
|
|
2021-09-14 21:37:37 +02:00
|
|
|
config := &configData{}
|
2021-09-15 13:28:16 +02:00
|
|
|
configPath = defaultPath(configPath, "config.toml")
|
|
|
|
if err := file.ReadTOML(configPath, config); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if err := file.SaveTOML(configPath, config); err != nil {
|
|
|
|
log.Panicf("save example config file: %s", err)
|
|
|
|
}
|
|
|
|
log.Panicf("open config file, we generate one here %s", configPath)
|
|
|
|
}
|
2021-09-09 23:12:29 +02:00
|
|
|
log.Panicf("open config file: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-09-14 21:37:37 +02:00
|
|
|
store, err := storage.InitStorage(defaultPath(config.StoragePath, "database.db"))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("open storage: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-09-09 18:31:26 +02:00
|
|
|
dbus = distributor.NewDBus("org.unifiedpush.Distributor.xmpp")
|
2021-09-14 21:37:37 +02:00
|
|
|
dbus.StartHandling(&config.XMPP)
|
2021-09-05 19:20:27 +02:00
|
|
|
|
2021-09-15 18:42:08 +02:00
|
|
|
log.Debug("startup ...")
|
2021-09-14 21:37:37 +02:00
|
|
|
if err := config.XMPP.Run(dbus, store); err != nil {
|
2021-09-09 23:12:29 +02:00
|
|
|
log.Errorf("startup xmpp: %v", err)
|
|
|
|
}
|
2021-09-05 13:31:06 +02:00
|
|
|
}
|