fix and test email parsing, add mock matrix

This commit is contained in:
Bel LaPointe
2022-01-11 00:02:00 -05:00
parent ec2b514a9f
commit 392578bb54
6 changed files with 72 additions and 13 deletions

35
main.go
View File

@@ -16,7 +16,7 @@ import (
"time"
)
var stateFinder = regexp.MustCompile(`[ ^>][A-Z][A-Z][ $<]`)
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
func main() {
lock := &sync.Mutex{}
@@ -40,12 +40,15 @@ func main() {
}
func email() error {
log.Printf("checking email...")
ch, err := config.Get().Emailer.ReadIMAP()
if err != nil {
ch, err = config.Get().Emailer.ReadIMAP()
}
if err != nil {
return err
}
states := map[config.State]struct{}{}
var state config.State
for email := range ch {
if len(states) > 0 {
continue
@@ -54,14 +57,8 @@ func email() error {
continue
}
b, _ := ioutil.ReadAll(email.Body)
candidates := stateFinder.FindAll(b, -1)
for _, candidate := range candidates {
s := fmt.Sprintf(`"%s"`, bytes.TrimSpace(candidate))
err := json.Unmarshal([]byte(s), &state)
if err != nil {
} else {
states[state] = struct{}{}
}
for _, state := range parseOutStates(b) {
states[state] = struct{}{}
}
}
if len(states) == 0 {
@@ -77,6 +74,24 @@ func email() error {
return nil
}
func parseOutStates(b []byte) []config.State {
var states []config.State
var state config.State
candidates := stateFinder.FindAll(b, -1)
for _, candidate := range candidates {
if len(candidate) != 2 {
continue
}
s := fmt.Sprintf(`"%s"`, bytes.ToUpper(candidate))
err := json.Unmarshal([]byte(s), &state)
if err != nil {
} else {
states = append(states, state)
}
}
return states
}
func _main() error {
for {
if err := config.Refresh(); err != nil {