nunbot/main.go

125 lines
2.7 KiB
Go
Raw Normal View History

2022-07-13 15:07:01 -04:00
package main
import (
"log"
2022-07-17 18:15:29 -04:00
"os"
"os/signal"
2022-08-03 13:35:27 -04:00
"strconv"
2022-07-17 18:15:29 -04:00
"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
}
2022-08-03 13:06:54 -04:00
dataFilter := &mautrix.Filter{
AccountData: mautrix.FilterPart{
Limit: 20,
NotTypes: []event.Type{
event.NewEventType("nun.batch"),
},
},
}
matrixClient, err := mbl.NewMatrixClient(conf, nil)
if err != nil {
panic(err)
}
store := mautrix.NewAccountDataStore("nun.batch", matrixClient)
_, err = matrixClient.CreateFilter(dataFilter)
2022-07-17 18:15:29 -04:00
if err != nil {
2022-08-03 13:06:54 -04:00
//don't want to continue if we can't keep state
2022-07-17 18:15:29 -04:00
panic(err)
}
2022-08-03 13:35:27 -04:00
var nun *nunWatch
2022-08-03 13:06:54 -04:00
matrixClient.Store = store
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-08-03 13:06:54 -04:00
switch cmd[0] {
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)
}
2022-08-03 13:35:27 -04:00
case "timer":
if len(cmd) <= 1 {
matrixClient.SendText(evt.RoomID, "!nun timer [time in minutes to wait between tries]")
return
}
time, err := strconv.Atoi(cmd[1])
if err != nil {
matrixClient.SendText(evt.RoomID, "!nun timer needs integer timer")
return
}
nun.SetTimer(time)
matrixClient.SendText(evt.RoomID, "timer set")
2022-07-13 15:07:01 -04:00
case "help":
2022-08-03 13:35:27 -04:00
matrixClient.SendText(evt.RoomID, "Supported commands: version, help,timer")
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-08-03 13:35:27 -04:00
nun = newNunWatch(stop, matrixClient, 2)
2022-07-17 18:15:29 -04:00
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
}