From 168381aa12fff91472f348697638699137ce617c Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 21 Oct 2018 14:48:06 +0200 Subject: [PATCH] initial commit --- cmd/mail2text/text.go | 32 +++++++++++++++++++++++++++++ cmd/mailparse/cli.go | 48 +++++++++++++++++++++++++++++++++++++++++++ text.go | 30 +++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 cmd/mail2text/text.go create mode 100644 cmd/mailparse/cli.go create mode 100644 text.go diff --git a/cmd/mail2text/text.go b/cmd/mail2text/text.go new file mode 100644 index 0000000..198cc2e --- /dev/null +++ b/cmd/mail2text/text.go @@ -0,0 +1,32 @@ +package main + +/* Tries hard to print email text */ + +import ( + "bufio" + "flag" + "fmt" + "os" + + "git.lattuga.net/boyska/mailparse" + "mvdan.cc/xurls" +) + +func main() { + urlsonly := flag.Bool("urls", false, "Only print found URLs") + flag.Parse() + + reader := bufio.NewReader(os.Stdin) + body, err := mailparse.GetText(reader) + if err != nil { + fmt.Fprintln(os.Stderr, "No text parts found") // but we should have walked more! + os.Exit(1) + } + if *urlsonly { + for _, url := range xurls.Strict().FindAllString(body, -1) { + fmt.Println(url) + } + return + } + fmt.Println(body) +} diff --git a/cmd/mailparse/cli.go b/cmd/mailparse/cli.go new file mode 100644 index 0000000..51ccfdd --- /dev/null +++ b/cmd/mailparse/cli.go @@ -0,0 +1,48 @@ +package main + +/* Finds (1st occurrence of) a specific header */ + +import ( + "bufio" + "encoding/json" + "flag" + "fmt" + "net/mail" + "net/textproto" + "os" +) + +func main() { + reader := bufio.NewReader(os.Stdin) + m, err := mail.ReadMessage(reader) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + flag.Parse() + headers := textproto.MIMEHeader(m.Header) + if len(flag.Args()) > 0 { + results := make([]string, 0) + for _, needle := range flag.Args() { + values := headers[textproto.CanonicalMIMEHeaderKey(needle)] + if len(values) == 0 { + fmt.Fprintf(os.Stderr, "No values for field `%s`\n", needle) + } else { + results = append(results, string(values[0])) + } + } + for _, res := range results { + fmt.Println(res) + } + os.Exit(0) + } else { + enc, err := json.Marshal(headers) + if err != nil { + fmt.Fprintln(os.Stderr, "Error serializing mail headers") + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + fmt.Println(string(enc)) + os.Exit(0) + } +} diff --git a/text.go b/text.go new file mode 100644 index 0000000..4ee4708 --- /dev/null +++ b/text.go @@ -0,0 +1,30 @@ +package mailparse + +import ( + "errors" + "fmt" + "io" + "os" + + "github.com/jhillyerd/enmime" +) + +func getBody(envelope *enmime.Envelope) (string, error) { + if envelope.Text != "" { + return envelope.Text, nil + } + if envelope.HTML != "" { + // TODO: jaytaylor.com/html2text + return envelope.HTML, nil + } + return "", errors.New("Can't find any text part inside the email") +} + +func GetText(r io.Reader) (string, error) { + envelope, err := enmime.ReadEnvelope(r) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + return getBody(envelope) +}