fix and test email parsing, add mock matrix
This commit is contained in:
35
main.go
35
main.go
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user