2022-07-13 15:07:01 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2022-07-17 18:15:29 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
2022-07-13 15:07:01 -04:00
|
|
|
|
2022-07-17 18:15:29 -04:00
|
|
|
mbl "git.saintnet.tech/stryan/matrixbotlib"
|
2022-07-13 15:07:01 -04:00
|
|
|
"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() {
|
2022-07-17 18:15:29 -04:00
|
|
|
conf, err := mbl.LoadMatrixClientConfig("config.yaml")
|
|
|
|
if err != nil {
|
2022-07-17 18:26:35 -04:00
|
|
|
conf, err = mbl.LoadMatrixClientConfig("/etc/nunbot/config.yaml")
|
2022-07-17 18:21:06 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-17 18:15:29 -04:00
|
|
|
}
|
|
|
|
store := mbl.NewLazyMemStore(conf.Statefile)
|
|
|
|
matrixClient, err := mbl.NewMatrixClient(conf, store)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-13 15:07:01 -04:00
|
|
|
syncer := matrixClient.Syncer.(*mautrix.DefaultSyncer)
|
2022-07-17 18:15:29 -04:00
|
|
|
mbl.AcceptAllRoomInvites(matrixClient)
|
2022-07-13 15:07:01 -04:00
|
|
|
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
|
|
|
if evt.Sender == matrixClient.UserID {
|
|
|
|
return //ignore events from self
|
|
|
|
}
|
2022-07-17 18:15:29 -04:00
|
|
|
cmd, err := mbl.ParseCommand(evt, "nun")
|
|
|
|
if err != nil {
|
|
|
|
if err != mbl.ErrCmdParseNoPrefix {
|
|
|
|
log.Printf("invalid command: %v", err)
|
|
|
|
}
|
2022-07-13 15:07:01 -04:00
|
|
|
return
|
|
|
|
}
|
2022-07-17 18:15:29 -04:00
|
|
|
switch cmd[1] {
|
2022-07-13 15:07:01 -04:00
|
|
|
case "version":
|
|
|
|
// print version
|
|
|
|
if GitTag != "" {
|
|
|
|
matrixClient.SendText(evt.RoomID, "NunBot version "+GitTag)
|
|
|
|
} else {
|
|
|
|
matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit)
|
|
|
|
}
|
|
|
|
case "help":
|
2022-07-17 18:15:29 -04:00
|
|
|
matrixClient.SendText(evt.RoomID, "Supported commands: version, help")
|
2022-07-13 15:07:01 -04:00
|
|
|
default:
|
|
|
|
//command not found
|
|
|
|
matrixClient.SendText(evt.RoomID, "command not recognized")
|
|
|
|
}
|
|
|
|
})
|
2022-07-17 18:15:29 -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.Println("trying to shutdown cleanly")
|
|
|
|
matrixClient.StopSync()
|
|
|
|
stop <- true
|
|
|
|
}()
|
2022-07-13 15:07:01 -04:00
|
|
|
|
2022-07-17 18:15:29 -04:00
|
|
|
nun := newNunWatch(stop, matrixClient)
|
|
|
|
wg.Add(1)
|
2022-07-13 15:07:01 -04:00
|
|
|
go func() {
|
|
|
|
|
2022-07-17 18:15:29 -04:00
|
|
|
err = matrixClient.Sync()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2022-07-13 15:07:01 -04:00
|
|
|
}
|
2022-07-17 18:15:29 -04:00
|
|
|
wg.Done()
|
|
|
|
log.Println("matrix client shutdown")
|
2022-07-13 15:07:01 -04:00
|
|
|
|
2022-07-17 18:15:29 -04:00
|
|
|
}()
|
2022-07-13 15:07:01 -04:00
|
|
|
|
2022-07-17 18:15:29 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
nun.Main()
|
|
|
|
wg.Done()
|
|
|
|
log.Println("nun watch shutdown")
|
|
|
|
}()
|
|
|
|
log.Println("nunbot running")
|
|
|
|
wg.Wait()
|
|
|
|
log.Println("shutting down")
|
2022-07-13 15:07:01 -04:00
|
|
|
}
|