recv now has -json for pretty print

This commit is contained in:
Bel LaPointe
2023-10-16 08:50:59 -06:00
parent 7d372adb78
commit e3d5a7e221

View File

@@ -1,10 +1,12 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net/mail" "net/mail"
"os"
"strings" "strings"
"time" "time"
@@ -21,6 +23,7 @@ func main() {
as.Append(args.STRING, "u", "username", emailer.From) as.Append(args.STRING, "u", "username", emailer.From)
as.Append(args.STRING, "p", "password", emailer.Password) as.Append(args.STRING, "p", "password", emailer.Password)
as.Append(args.INT, "b", "dont read more than this many characters", 4096) as.Append(args.INT, "b", "dont read more than this many characters", 4096)
as.Append(args.BOOL, "json", "output as json", false)
if err := as.Parse(); err != nil { if err := as.Parse(); err != nil {
panic(err) panic(err)
} }
@@ -42,6 +45,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
emails := make([]map[string]any, 0, emailer.Limit)
for msg := range msgs { for msg := range msgs {
b, _ := ioutil.ReadAll(io.LimitReader(msg.Body, int64(as.GetInt("b")))) b, _ := ioutil.ReadAll(io.LimitReader(msg.Body, int64(as.GetInt("b"))))
s := strings.ReplaceAll(string(b), "\r\n", "\n") s := strings.ReplaceAll(string(b), "\r\n", "\n")
@@ -52,6 +56,14 @@ func main() {
} }
d, _ := msg.Header.Date() d, _ := msg.Header.Date()
d = d.In(time.Local) d = d.In(time.Local)
if as.GetBool("json") {
emails = append(emails, map[string]any{
"t": d.Format("06-01-02T15:04Z07"),
"from": msg.Header.Get("From"),
"subject": msg.Header.Get("Subject"),
"body": s,
})
} else {
fmt.Printf( fmt.Printf(
"@%+v @%+v: \n\t%+v: \n\t%s\n", "@%+v @%+v: \n\t%+v: \n\t%s\n",
d.Format("06-01-02T15:04Z07"), d.Format("06-01-02T15:04Z07"),
@@ -61,3 +73,9 @@ func main() {
) )
} }
} }
if as.GetBool("json") {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(emails)
}
}