From 22b8e0fb70da2e4b8ac92cf8f00c389e21af5f44 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Tue, 11 Jan 2022 09:42:42 -0500 Subject: [PATCH] receive from enabled --- config.json | 13 +++++++----- config/config.go | 15 +++++++------- main.go | 51 +++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 62 insertions(+), 17 deletions(-) diff --git a/config.json b/config.json index c7b4a6a..888b4b1 100644 --- a/config.json +++ b/config.json @@ -1,12 +1,14 @@ { "Name": "pa", "Interval": { - "OK": "6h0m0s", - "Error": "6h", - "Email": "15m" + "Email": "15m0s..15m0s", + "OK": "6h0m0s..6h0m0s", + "Error": "6h0m0s..6h0m0s" }, "States": [ - "GA" + "FL", + "GA", + "NC" ], "Storage": [ "map" @@ -14,8 +16,9 @@ "Client": "breellocaldev@gmail.com", "Message": { "Matrix": { - "Mock": true, + "ReceiveEnabled": true, "Client": "@belandbroc:matrix.org", + "Mock": true, "Homeserver": "https://matrix-client.matrix.org", "Username": "@breellocaldev:matrix.org", "Token": "syt_YnJlZWxsb2NhbGRldg_HTewKMMePdEcLvceAKEz_2fHsHa", diff --git a/config/config.go b/config/config.go index 5ea8568..a339b06 100644 --- a/config/config.go +++ b/config/config.go @@ -21,13 +21,14 @@ type Config struct { Client string Message struct { Matrix struct { - Client string - Mock bool - Homeserver string - Username string - Token string - Device string - Room string + ReceiveEnabled bool + Client string + Mock bool + Homeserver string + Username string + Token string + Device string + Room string } } Once bool diff --git a/main.go b/main.go index 87eb186..cebd918 100644 --- a/main.go +++ b/main.go @@ -23,12 +23,23 @@ func main() { go func() { for { time.Sleep(config.Get().Interval.Email.Get()) - if config.Get().EmailerEnabled { - lock.Lock() - if err := email(); err != nil { - log.Print(err) + if err := config.Refresh(); err != nil { + log.Println(err) + } else { + if config.Get().EmailerEnabled { + lock.Lock() + if err := email(); err != nil { + log.Print(err) + } + lock.Unlock() + } + if config.Get().Message.Matrix.ReceiveEnabled { + lock.Lock() + if err := matrixrecv(); err != nil { + log.Print(err) + } + lock.Unlock() } - lock.Unlock() } } }() @@ -38,6 +49,36 @@ func main() { lock.Lock() } +func matrixrecv() error { + log.Printf("checking matrix...") + defer log.Printf("/checking matrix...") + sender := message.NewMatrix() + messages, err := sender.Receive() + if err != nil { + return err + } + states := map[config.State]struct{}{} + for _, msg := range messages { + if len(states) > 0 { + continue + } + for _, state := range parseOutStates([]byte(msg)) { + states[state] = struct{}{} + } + } + if len(states) == 0 { + return nil + } + conf := *config.Get() + conf.States = []config.State{} + for k := range states { + conf.States = append(conf.States, k) + } + log.Printf("%+v, %+v", states, conf) + config.Set(conf) + return nil +} + func email() error { log.Printf("checking email...") ch, err := config.Get().Emailer.ReadIMAP()