fix and test email parsing, add mock matrix
parent
ec2b514a9f
commit
392578bb54
|
|
@ -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": {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ type Config struct {
|
|||
Client string
|
||||
Message struct {
|
||||
Matrix struct {
|
||||
Mock bool
|
||||
Homeserver string
|
||||
Username string
|
||||
Token string
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
|
|||
33
main.go
33
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,16 +57,10 @@ 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 {
|
||||
for _, state := range parseOutStates(b) {
|
||||
states[state] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(states) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -2,11 +2,13 @@ package message
|
|||
|
||||
import (
|
||||
"local/truckstop/config"
|
||||
"log"
|
||||
|
||||
"github.com/matrix-org/gomatrix"
|
||||
)
|
||||
|
||||
type Matrix struct {
|
||||
mock bool
|
||||
homeserver string
|
||||
username string
|
||||
token string
|
||||
|
|
@ -20,6 +22,7 @@ func NewMatrix() Matrix {
|
|||
username: conf.Username,
|
||||
token: conf.Token,
|
||||
room: conf.Room,
|
||||
mock: conf.Mock,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -28,6 +31,10 @@ func (m Matrix) client() (*gomatrix.Client, error) {
|
|||
}
|
||||
|
||||
func (m Matrix) Send(text string) error {
|
||||
if m.mock {
|
||||
log.Printf("matrix.Send(%s)", text)
|
||||
return nil
|
||||
}
|
||||
c, err := m.client()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
Loading…
Reference in New Issue