95 lines
2.0 KiB
Go
95 lines
2.0 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 {
|
|
conf, err = mbl.LoadMatrixClientConfig("/etc/nunbot/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, 2)
|
|
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")
|
|
}
|