simpbot/main.go

97 lines
1.6 KiB
Go
Raw Normal View History

2021-05-21 12:45:24 -04:00
package main
import (
2021-10-26 14:14:26 -04:00
"net/url"
2021-08-07 13:02:50 -04:00
"os"
"os/signal"
2023-05-14 18:45:55 -04:00
"sync"
2021-05-21 12:45:24 -04:00
2023-05-30 20:12:38 -04:00
mbl "git.saintnet.tech/stryan/matrixbotlib"
2023-05-14 18:45:55 -04:00
"github.com/charmbracelet/log"
2021-05-21 12:45:24 -04:00
)
2023-05-30 20:12:38 -04:00
//GitTag is current git tag
var GitTag string
//GitCommit is current git commit
var GitCommit string
2021-05-21 12:45:24 -04:00
func main() {
2023-05-30 20:12:38 -04:00
log.Info("loading matrix client")
conf, err := mbl.LoadMatrixClientConfig("config.yaml")
if err != nil {
log.Info("no local config found, checking /etc/simpbot")
conf, err = mbl.LoadMatrixClientConfig("/etc/simpbot/config.yaml")
if err != nil {
panic(err)
}
}
matrixClient, err := mbl.NewMatrixClient(conf, nil)
2021-05-21 13:39:54 -04:00
if err != nil {
2023-05-30 20:12:38 -04:00
panic(err)
2021-05-21 12:45:24 -04:00
}
2022-08-10 14:03:35 -04:00
2023-05-30 20:12:38 -04:00
log.Info("loading simpbot")
simp, err := newSimp("config.yaml", matrixClient)
2023-05-14 18:45:55 -04:00
if err != nil {
log.Fatal(err)
2021-05-21 12:45:24 -04:00
}
2023-05-30 20:12:38 -04:00
err = simp.SetupMatrix()
if err != nil {
log.Fatal(err)
2023-04-24 18:01:40 -04:00
}
2023-05-30 20:12:38 -04:00
2023-05-14 18:45:55 -04:00
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
}()
2021-05-21 12:45:24 -04:00
2023-05-14 18:45:55 -04:00
wg.Add(1)
2021-05-21 12:45:24 -04:00
go func() {
2023-05-30 20:12:38 -04:00
log.Debug("starting matrix client")
err = matrixClient.Sync()
2023-05-14 18:45:55 -04:00
if err != nil {
2023-05-14 19:09:49 -04:00
log.Error(err)
2021-05-21 12:45:24 -04:00
}
2023-05-14 18:45:55 -04:00
wg.Done()
2023-05-30 20:12:38 -04:00
log.Info("matrix shutdown")
2021-05-21 12:45:24 -04:00
}()
2023-05-14 18:45:55 -04:00
wg.Add(1)
go func() {
2023-05-30 20:12:38 -04:00
log.Debug("starting simpbot sync")
err = simp.Sync()
if err != nil {
log.Error(err)
}
2023-05-14 18:45:55 -04:00
wg.Done()
log.Info("simpbot shutdown")
}()
log.Info("simpbot running")
wg.Wait()
log.Info("shutting down")
2023-05-30 20:12:38 -04:00
2021-05-21 12:45:24 -04:00
}
2021-10-26 14:14:26 -04:00
2023-05-14 18:45:55 -04:00
func isValidURL(toTest string) bool {
2021-10-26 14:14:26 -04:00
_, 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
}