nunbot/main.go
Steve 45b50e4733
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
switch to botlib, waitgroups
2022-07-17 18:15:29 -04:00

92 lines
1.9 KiB
Go

package main
import (
"log"
"os"
"os/signal"
"sync"
mbl "git.saintnet.tech/stryan/matrixbotlib"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
)
//GitTag is current git tag
var GitTag string
//GitCommit is current git commit
var GitCommit string
func main() {
conf, err := mbl.LoadMatrixClientConfig("config.yaml")
if err != nil {
panic(err)
}
store := mbl.NewLazyMemStore(conf.Statefile)
matrixClient, err := mbl.NewMatrixClient(conf, store)
if err != nil {
panic(err)
}
syncer := matrixClient.Syncer.(*mautrix.DefaultSyncer)
mbl.AcceptAllRoomInvites(matrixClient)
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
if evt.Sender == matrixClient.UserID {
return //ignore events from self
}
cmd, err := mbl.ParseCommand(evt, "nun")
if err != nil {
if err != mbl.ErrCmdParseNoPrefix {
log.Printf("invalid command: %v", err)
}
return
}
switch cmd[1] {
case "version":
// print version
if GitTag != "" {
matrixClient.SendText(evt.RoomID, "NunBot version "+GitTag)
} else {
matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit)
}
case "help":
matrixClient.SendText(evt.RoomID, "Supported commands: version, help")
default:
//command not found
matrixClient.SendText(evt.RoomID, "command not recognized")
}
})
var wg sync.WaitGroup
stop := make(chan bool)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
log.Println("trying to shutdown cleanly")
matrixClient.StopSync()
stop <- true
}()
nun := newNunWatch(stop, matrixClient)
wg.Add(1)
go func() {
err = matrixClient.Sync()
if err != nil {
log.Println(err)
}
wg.Done()
log.Println("matrix client shutdown")
}()
wg.Add(1)
go func() {
nun.Main()
wg.Done()
log.Println("nun watch shutdown")
}()
log.Println("nunbot running")
wg.Wait()
log.Println("shutting down")
}