Refactor to prevent multiple webhooks Also leave cleaning up HTML to the webhook server.
Anirudh Oppiliappan x@icyphox.sh
Thu, 01 Apr 2021 10:04:38 +0530
M
go.sum
→
go.sum
@@ -1,7 +1,3 @@
-github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= -github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= -github.com/chris-ramon/douceur v0.2.0 h1:IDMEdxlEUUBYBKE4z/mJnFyVXox+MjuEVDJNN27glkU= -github.com/chris-ramon/douceur v0.2.0/go.mod h1:wDW5xjJdeoMm1mRt4sD4c/LbF/mWdEpRXQKjTR8nIBE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=@@ -9,18 +5,12 @@ github.com/emersion/go-message v0.14.1 h1:j3rj9F+7VtXE9c8P5UHBq8FTHLW/AjnmvSRre6AHoYI=
github.com/emersion/go-message v0.14.1/go.mod h1:N1JWdZQ2WRUalmdHAX308CWBq747VJ8oUorFI3VCBwU= github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 h1:IbFBtwoTQyw0fIM5xv1HF+Y+3ZijDR839WMulgxCcUY= github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U= -github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= -github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/martinlindhe/base36 v1.1.0 h1:cIwvvwYse/0+1CkUPYH5ZvVIYG3JrILmQEIbLuar02Y= github.com/martinlindhe/base36 v1.1.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8= -github.com/microcosm-cc/bluemonday v1.0.5 h1:cF59UCKMmmUgqN1baLvqU/B1ZsMori+duLVTLpgiG3w= -github.com/microcosm-cc/bluemonday v1.0.5/go.mod h1:8iwZnFn2CDDNZ0r6UXhF4xawGvzaqzCRa1n3/lO3W2w= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis= -golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/text v0.3.5-0.20201125200606-c27b9fd57aec/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
M
main.go
→
main.go
@@ -4,24 +4,21 @@ import (
"bufio" "bytes" "encoding/json" - "errors" - "fmt" "io" "io/ioutil" "log" "net/http" "os" - "strings" "github.com/emersion/go-message/mail" - "github.com/microcosm-cc/bluemonday" ) +type P map[string]string + type Mail struct { - From string - Date string - Body string - ContentType string + From string + Date string + Parts []P } func makeReq(j []byte) {@@ -35,48 +32,14 @@ }
defer res.Body.Close() } -func cleanHTML(body string) string { - bm := bluemonday.StrictPolicy() - bm.AddSpaceWhenStrippingTag(true) - clean := bm.Sanitize(body) - return clean -} - -func jsonMail(p *mail.Part, mr *mail.Reader) ([]byte, error) { - b, _ := ioutil.ReadAll(p.Body) - body := string(b) - ct := p.Header.Get("Content-Type") - from := mr.Header.Get("From") - date := mr.Header.Get("Date") - - // Prefer plaintext part over html - if strings.Contains(ct, "text/plain") { - m := Mail{from, date, body, "text/plain"} - j, err := json.Marshal(m) - if err != nil { - log.Fatal(err) - } - return j, nil - - // If there's no plaintext, fallback on html - // Clean up HTML junk using bluemonday - } else if strings.Contains(ct, "text/html") { - cleanBody := cleanHTML(body) - m := Mail{from, date, cleanBody, "text/html"} - j, err := json.Marshal(m) - if err != nil { - log.Fatal(err) - } - return j, nil - } else { - return nil, errors.New("no plaintext or html parts") - } -} - func main() { r := bufio.NewReader(os.Stdin) mr, err := mail.CreateReader(r) + newmail := Mail{} + newmail.Date = mr.Header.Get("Date") + newmail.From = mr.Header.Get("From") + if err != nil { log.Fatal(err) }@@ -87,20 +50,23 @@ if err == io.EOF {
break } else if err != nil { log.Fatal(err) - } switch h := p.Header.(type) { case *mail.InlineHeader: - jm, err := jsonMail(p, mr) - fmt.Println(string(jm)) - if err != nil { - log.Fatal(err) - } - makeReq(jm) + ct := p.Header.Get("Content-Type") + b, _ := ioutil.ReadAll(p.Body) + part := P{ct: string(b)} + newmail.Parts = append(newmail.Parts, part) case *mail.AttachmentHeader: filename, _ := h.Filename() log.Printf("Got attachment: %v\n", filename) } } + + j, err := json.Marshal(newmail) + if err != nil { + log.Fatal(err) + } + makeReq(j) }