package main import ( "os" "os/signal" "strconv" "strings" "sync" "github.com/charmbracelet/log" mbl "git.saintnet.tech/stryan/matrixbotlib" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) // 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 { log.Info("no local config found, checking /etc/nunbot") conf, err = mbl.LoadMatrixClientConfig("/etc/nunbot/config.yaml") if err != nil { panic(err) } } 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) fID, err := matrixClient.CreateFilter(dataFilter) if err != nil { // don't want to continue if we can't keep state panic(err) } var nun *nunWatch uid := id.NewUserID(strings.ToLower(conf.Username), strings.ToLower(conf.Domain)) store.SaveFilterID(uid, fID.FilterID) matrixClient.Store = store syncer := matrixClient.Syncer.(*mautrix.DefaultSyncer) mbl.AcceptAllRoomInvites(matrixClient) syncer.OnEventType(event.EventMessage, func(_ 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[0] { case "version": // print version if GitTag != "" { _, err := matrixClient.SendText(evt.RoomID, "NunBot version "+GitTag) if err != nil { log.Warn(err) } } else { _, err := matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit) if err != nil { log.Warn(err) } } case "timer": if len(cmd) <= 1 { _, err := matrixClient.SendText(evt.RoomID, "!nun timer [time in minutes to wait between tries]") if err != nil { log.Warn(err) } return } time, err := strconv.Atoi(cmd[1]) if err != nil { _, err = matrixClient.SendText(evt.RoomID, "!nun timer needs integer timer") if err != nil { log.Warn(err) } return } nun.SetTimer(time) _, err = matrixClient.SendText(evt.RoomID, "timer set") if err != nil { log.Warn(err) } case "help": _, err := matrixClient.SendText(evt.RoomID, "Supported commands: version, help,timer") if err != nil { log.Warn(err) } default: // command not found _, err := matrixClient.SendText(evt.RoomID, "command not recognized") if err != nil { log.Warn(err) } } }) var wg sync.WaitGroup stop := make(chan bool) c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { <-c log.Info("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.Warn(err) } wg.Done() log.Info("matrix client shutdown") }() wg.Add(1) go func() { nun.Main() wg.Done() log.Info("nun watch shutdown") }() log.Info("nunbot running") wg.Wait() log.Info("shutting down") }