2022-07-13 15:07:01 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-07-17 18:15:29 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-08-03 13:35:27 -04:00
|
|
|
"strconv"
|
2022-08-10 14:14:52 -04:00
|
|
|
"strings"
|
2022-07-17 18:15:29 -04:00
|
|
|
"sync"
|
2022-07-13 15:07:01 -04:00
|
|
|
|
2023-10-10 21:01:01 -04:00
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
|
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"
|
2022-08-10 14:14:52 -04:00
|
|
|
"maunium.net/go/mautrix/id"
|
2022-07-13 15:07:01 -04:00
|
|
|
)
|
|
|
|
|
2023-10-10 21:01:01 -04:00
|
|
|
// GitTag is current git tag
|
2022-07-13 15:07:01 -04:00
|
|
|
var GitTag string
|
|
|
|
|
2023-10-10 21:01:01 -04:00
|
|
|
// GitCommit is current git commit
|
2022-07-13 15:07:01 -04:00
|
|
|
var GitCommit string
|
|
|
|
|
|
|
|
func main() {
|
2022-07-17 18:15:29 -04:00
|
|
|
conf, err := mbl.LoadMatrixClientConfig("config.yaml")
|
|
|
|
if err != nil {
|
2023-10-10 21:01:01 -04:00
|
|
|
log.Info("no local config found, checking /etc/nunbot")
|
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)
|
2022-08-10 14:14:52 -04:00
|
|
|
fID, err := matrixClient.CreateFilter(dataFilter)
|
2022-07-17 18:15:29 -04:00
|
|
|
if err != nil {
|
2023-10-10 21:01:01 -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-10 14:14:52 -04:00
|
|
|
uid := id.NewUserID(strings.ToLower(conf.Username), strings.ToLower(conf.Domain))
|
|
|
|
store.SaveFilterID(uid, fID.FilterID)
|
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)
|
2023-10-10 21:01:01 -04:00
|
|
|
syncer.OnEventType(event.EventMessage, func(_ mautrix.EventSource, evt *event.Event) {
|
2022-07-13 15:07:01 -04:00
|
|
|
if evt.Sender == matrixClient.UserID {
|
2023-10-10 21:01:01 -04:00
|
|
|
return // ignore events from self
|
2022-07-13 15:07:01 -04:00
|
|
|
}
|
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 != "" {
|
2023-10-10 21:01:01 -04:00
|
|
|
_, err := matrixClient.SendText(evt.RoomID, "NunBot version "+GitTag)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
2022-07-13 15:07:01 -04:00
|
|
|
} else {
|
2023-10-10 21:01:01 -04:00
|
|
|
_, err := matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
2022-07-13 15:07:01 -04:00
|
|
|
}
|
2022-08-03 13:35:27 -04:00
|
|
|
case "timer":
|
|
|
|
if len(cmd) <= 1 {
|
2023-10-10 21:01:01 -04:00
|
|
|
_, err := matrixClient.SendText(evt.RoomID, "!nun timer [time in minutes to wait between tries]")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
2022-08-03 13:35:27 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
time, err := strconv.Atoi(cmd[1])
|
|
|
|
if err != nil {
|
2023-10-10 21:01:01 -04:00
|
|
|
_, err = matrixClient.SendText(evt.RoomID, "!nun timer needs integer timer")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
2022-08-03 13:35:27 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
nun.SetTimer(time)
|
2023-10-10 21:01:01 -04:00
|
|
|
_, err = matrixClient.SendText(evt.RoomID, "timer set")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
2022-07-13 15:07:01 -04:00
|
|
|
case "help":
|
2023-10-10 21:01:01 -04:00
|
|
|
_, err := matrixClient.SendText(evt.RoomID, "Supported commands: version, help,timer")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
2022-07-13 15:07:01 -04:00
|
|
|
default:
|
2023-10-10 21:01:01 -04:00
|
|
|
// command not found
|
|
|
|
_, err := matrixClient.SendText(evt.RoomID, "command not recognized")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
2022-07-13 15:07:01 -04:00
|
|
|
}
|
|
|
|
})
|
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
|
2023-10-10 21:01:01 -04:00
|
|
|
log.Info("trying to shutdown cleanly")
|
2022-07-17 18:15:29 -04:00
|
|
|
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 {
|
2023-10-10 21:01:01 -04:00
|
|
|
log.Warn(err)
|
2022-07-13 15:07:01 -04:00
|
|
|
}
|
2022-07-17 18:15:29 -04:00
|
|
|
wg.Done()
|
2023-10-10 21:01:01 -04:00
|
|
|
log.Info("matrix client shutdown")
|
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()
|
2023-10-10 21:01:01 -04:00
|
|
|
log.Info("nun watch shutdown")
|
2022-07-17 18:15:29 -04:00
|
|
|
}()
|
2023-10-10 21:01:01 -04:00
|
|
|
log.Info("nunbot running")
|
2022-07-17 18:15:29 -04:00
|
|
|
wg.Wait()
|
2023-10-10 21:01:01 -04:00
|
|
|
log.Info("shutting down")
|
2022-07-13 15:07:01 -04:00
|
|
|
}
|