diff --git a/config.json b/config.json index a73b98c..c52df25 100644 --- a/config.json +++ b/config.json @@ -1,13 +1,15 @@ { - "Interval": "4h", + "Interval": "6h0m0s", "States": [ "GA" ], "Storage": [ "map" ], + "Client": "breellocaldev@gmail.com", "Message": { "Matrix": { + "Mock": true, "Homeserver": "https://matrix-client.matrix.org", "Username": "@breellocaldev:matrix.org", "Token": "syt_YnJlZWxsb2NhbGRldg_HTewKMMePdEcLvceAKEz_2fHsHa", @@ -15,7 +17,6 @@ "Room": "!vVwjXhWXMxZtOwexKa:matrix.org" } }, - "Client": "breellocaldev@gmail.com", "Once": true, "Brokers": { "NTG": { diff --git a/config/config.go b/config/config.go index a321db0..29a2b50 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ type Config struct { Client string Message struct { Matrix struct { + Mock bool Homeserver string Username string Token string diff --git a/config/duration.go b/config/duration.go index ececb9a..3094dad 100644 --- a/config/duration.go +++ b/config/duration.go @@ -11,7 +11,7 @@ func (d Duration) Get() time.Duration { return time.Duration(d) } -func (d *Duration) MarshalJSON() ([]byte, error) { +func (d Duration) MarshalJSON() ([]byte, error) { return json.Marshal(d.Get().String()) } diff --git a/main.go b/main.go index 224e056..81c773f 100644 --- a/main.go +++ b/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 { diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..11dcf2d --- /dev/null +++ b/main_test.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "local/truckstop/config" + "testing" +) + +func TestParseOutStates(t *testing.T) { + input := `--0000000000006704ef05d5335639 +Content-Type: text/plain; charset="UTF-8" + +NC the states should be ok GA SC + +--0000000000006704ef05d5335639 +Content-Type: text/html; charset="UTF-8" + +