fix and test email parsing, add mock matrix

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

View File

@ -1,13 +1,15 @@
{ {
"Interval": "4h", "Interval": "6h0m0s",
"States": [ "States": [
"GA" "GA"
], ],
"Storage": [ "Storage": [
"map" "map"
], ],
"Client": "breellocaldev@gmail.com",
"Message": { "Message": {
"Matrix": { "Matrix": {
"Mock": true,
"Homeserver": "https://matrix-client.matrix.org", "Homeserver": "https://matrix-client.matrix.org",
"Username": "@breellocaldev:matrix.org", "Username": "@breellocaldev:matrix.org",
"Token": "syt_YnJlZWxsb2NhbGRldg_HTewKMMePdEcLvceAKEz_2fHsHa", "Token": "syt_YnJlZWxsb2NhbGRldg_HTewKMMePdEcLvceAKEz_2fHsHa",
@ -15,7 +17,6 @@
"Room": "!vVwjXhWXMxZtOwexKa:matrix.org" "Room": "!vVwjXhWXMxZtOwexKa:matrix.org"
} }
}, },
"Client": "breellocaldev@gmail.com",
"Once": true, "Once": true,
"Brokers": { "Brokers": {
"NTG": { "NTG": {

View File

@ -16,6 +16,7 @@ type Config struct {
Client string Client string
Message struct { Message struct {
Matrix struct { Matrix struct {
Mock bool
Homeserver string Homeserver string
Username string Username string
Token string Token string

View File

@ -11,7 +11,7 @@ func (d Duration) Get() time.Duration {
return time.Duration(d) return time.Duration(d)
} }
func (d *Duration) MarshalJSON() ([]byte, error) { func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Get().String()) return json.Marshal(d.Get().String())
} }

35
main.go
View File

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

35
main_test.go Normal file
View File

@ -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"
<div dir="ltr">NC the states should be ok GA SC<br></div>
--0000000000006704ef05d5335639--`
want := []config.State{
config.State("NC"),
config.State("OK"),
config.State("GA"),
config.State("SC"),
config.State("NC"),
config.State("OK"),
config.State("GA"),
config.State("SC"),
}
got := parseOutStates([]byte(input))
if fmt.Sprint(want) != fmt.Sprint(got) {
t.Fatal(want, got)
}
}

View File

@ -2,11 +2,13 @@ package message
import ( import (
"local/truckstop/config" "local/truckstop/config"
"log"
"github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrix"
) )
type Matrix struct { type Matrix struct {
mock bool
homeserver string homeserver string
username string username string
token string token string
@ -20,6 +22,7 @@ func NewMatrix() Matrix {
username: conf.Username, username: conf.Username,
token: conf.Token, token: conf.Token,
room: conf.Room, room: conf.Room,
mock: conf.Mock,
} }
} }
@ -28,6 +31,10 @@ func (m Matrix) client() (*gomatrix.Client, error) {
} }
func (m Matrix) Send(text string) error { func (m Matrix) Send(text string) error {
if m.mock {
log.Printf("matrix.Send(%s)", text)
return nil
}
c, err := m.client() c, err := m.client()
if err != nil { if err != nil {
return err return err