99 lines
1.6 KiB
Go
99 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"net/url"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
|
|
mbl "git.saintnet.tech/stryan/matrixbotlib"
|
|
|
|
"github.com/charmbracelet/log"
|
|
)
|
|
|
|
//GitTag is current git tag
|
|
var GitTag string
|
|
|
|
//GitCommit is current git commit
|
|
var GitCommit string
|
|
|
|
func main() {
|
|
log.Info("loading matrix client")
|
|
cnffile := "config.yaml"
|
|
conf, err := mbl.LoadMatrixClientConfig(cnffile)
|
|
if err != nil {
|
|
log.Info("no local config found, checking /etc/simpbot")
|
|
cnffile = "/etc/simpbot/config.yaml"
|
|
conf, err = mbl.LoadMatrixClientConfig(cnffile)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
matrixClient, err := mbl.NewMatrixClient(conf, nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
log.Info("loading simpbot")
|
|
simp, err := newSimp(cnffile, matrixClient)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
err = simp.SetupMatrix()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
stop := make(chan bool)
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt)
|
|
go func() {
|
|
<-c
|
|
log.Info("trying to shutdown cleanly")
|
|
simp.Stop()
|
|
stop <- true
|
|
}()
|
|
|
|
wg.Add(1)
|
|
go func() {
|
|
log.Debug("starting matrix client")
|
|
err = matrixClient.Sync()
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
wg.Done()
|
|
log.Info("matrix shutdown")
|
|
}()
|
|
|
|
wg.Add(1)
|
|
go func() {
|
|
log.Debug("starting simpbot sync")
|
|
err = simp.Sync()
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
wg.Done()
|
|
log.Info("simpbot shutdown")
|
|
}()
|
|
log.Info("simpbot running")
|
|
wg.Wait()
|
|
log.Info("shutting down")
|
|
|
|
}
|
|
|
|
func isValidURL(toTest string) bool {
|
|
_, err := url.ParseRequestURI(toTest)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
u, err := url.Parse(toTest)
|
|
if err != nil || u.Scheme == "" || u.Host == "" {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|