get matrix messages for all clients because commands, refresh config and matrix recv on boot, assert broc works for never recv

This commit is contained in:
Bel LaPointe
2022-01-11 23:27:45 -05:00
parent 8f681c7927
commit f90ec3917b
3 changed files with 83 additions and 13 deletions

63
main.go
View File

@@ -11,6 +11,7 @@ import (
"log"
"regexp"
"sort"
"strings"
"sync"
"time"
)
@@ -18,6 +19,12 @@ import (
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
func main() {
if err := config.Refresh(); err != nil {
panic(err)
}
if err := matrixrecv(); err != nil {
panic(err)
}
lock := &sync.Mutex{}
go func() {
for {
@@ -50,6 +57,7 @@ func matrixrecv() error {
return err
}
func() {
log.Printf("looking for states")
states := map[string]map[config.State]struct{}{}
for _, msg := range messages {
if _, ok := states[msg.Sender]; ok {
@@ -62,9 +70,58 @@ func matrixrecv() error {
}
setNewStates(states)
}()
func() {
log.Printf("looking for pauses")
pauses := map[string]time.Time{}
for _, msg := range messages {
if _, ok := pauses[msg.Sender]; ok {
continue
}
if !strings.HasPrefix(msg.Content, "pause until") {
continue
}
t, err := time.ParseInLocation(
"2006-01-02",
strings.TrimSpace(strings.TrimPrefix(msg.Content, "pause until")),
time.Local,
)
if err == nil {
pauses[msg.Sender] = t
}
}
setNewPauses(pauses)
}()
return nil
}
func setNewPauses(pauses map[string]time.Time) {
log.Printf("set new pauses: %+v", pauses)
if len(pauses) == 0 {
return
}
conf := *config.Get()
changed := map[string]time.Time{}
for client, pause := range pauses {
clientconf := conf.Clients[client]
if clientconf.PauseUntil.Get().Unix() == pause.Unix() {
continue
}
clientconf.PauseUntil = config.Time(pause)
conf.Clients[client] = clientconf
changed[client] = pause
}
if len(changed) == 0 {
return
}
log.Printf("updating config new pauses: %+v", conf)
config.Set(conf)
for client, pause := range changed {
if err := sendNewPause(client, pause); err != nil {
log.Printf("failed to send new pause %s/%+v: %v", client, pause, err)
}
}
}
func setNewStates(states map[string]map[config.State]struct{}) {
if len(states) == 0 {
return
@@ -124,6 +181,7 @@ func _main() error {
log.Println(err)
}
if config.Get().Once {
time.Sleep(time.Second)
return err
}
if err != nil {
@@ -218,3 +276,8 @@ func sendNewStates(client string, states []config.State) error {
sender := message.NewMatrix()
return sender.Send(fmt.Sprintf("%s: now searching for loads from: %+v", client, states))
}
func sendNewPause(client string, pause time.Time) error {
sender := message.NewMatrix()
return sender.Send(fmt.Sprintf("%s: not searching for loads until %s", client, pause.Format("2006-01-02")))
}