initial commit
This commit is contained in:
commit
168381aa12
3 changed files with 110 additions and 0 deletions
32
cmd/mail2text/text.go
Normal file
32
cmd/mail2text/text.go
Normal file
|
@ -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)
|
||||
}
|
48
cmd/mailparse/cli.go
Normal file
48
cmd/mailparse/cli.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
30
text.go
Normal file
30
text.go
Normal file
|
@ -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)
|
||||
}
|
Loading…
Reference in a new issue