diff --git a/config.json b/config.json index d88812c..76c51e9 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,12 @@ { - "Interval": "1s", - "States": ["OH", "CA"], + "Interval": "1m", + "States": [ + "GA" + ], + "Storage": [ + "map" + ], + "Client": "breellocaldev@gmail.com", "Once": true, "Brokers": { "NTG": { @@ -10,8 +16,10 @@ }, "Emailer": { "From": "breellocaldev@gmail.com", - "noIMAP": "imap.gmail.com:993", + "SMTP": "", + "POP3": "", + "IMAP": "imap.gmail.com:993", "Password": "gojfkkfrkmtxzyro", - "Limit": 10 + "Limit": 0 } -} +} \ No newline at end of file diff --git a/config/config.go b/config/config.go index 9414800..daaa08e 100644 --- a/config/config.go +++ b/config/config.go @@ -13,6 +13,7 @@ type Config struct { Interval Duration States []State Storage []string + Client string Once bool Brokers struct { NTG struct { @@ -57,7 +58,7 @@ func Get() *Config { } func Set(other Config) { - b, err := json.Marshal(other) + b, err := json.MarshalIndent(other, "", " ") if err != nil { return } diff --git a/config/duration.go b/config/duration.go index c2d211b..ececb9a 100644 --- a/config/duration.go +++ b/config/duration.go @@ -11,6 +11,10 @@ func (d Duration) Get() time.Duration { return time.Duration(d) } +func (d *Duration) MarshalJSON() ([]byte, error) { + return json.Marshal(d.Get().String()) +} + func (d *Duration) UnmarshalJSON(b []byte) error { var s string if err := json.Unmarshal(b, &s); err != nil { diff --git a/main.go b/main.go index 79946e8..1fd1bc4 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,21 @@ package main import ( - "errors" + "bytes" + "encoding/json" + "fmt" + "io/ioutil" "local/storage" "local/truckstop/broker" "local/truckstop/config" "log" + "regexp" + "strings" "time" ) +var stateFinder = regexp.MustCompile(`[ ^>][A-Z][A-Z][ $<]`) + func main() { go func() { email() @@ -22,6 +29,7 @@ func main() { if err := _main(); err != nil { panic(err) } + time.Sleep(time.Minute) } func email() error { @@ -29,10 +37,37 @@ func email() error { if err != nil { return err } + states := map[config.State]struct{}{} + var state config.State for email := range ch { - log.Printf("%+v", email) + if len(states) > 0 { + continue + } + if !strings.Contains(email.Header.Get("From"), config.Get().Client) { + 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{}{} + } + } } - return errors.New("not impl") + 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 _main() error { diff --git a/todo.yaml b/todo.yaml index dda20e5..e71f6f7 100644 --- a/todo.yaml +++ b/todo.yaml @@ -1,5 +1,5 @@ todo: -- read email to state file +- read email to state file from local/sandbox/contact/contact - upload cookies - download cookies - encrypt cookies