From 4ae360c5adc25051136ce638f85c7c10f856d9d3 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 25 Jan 2024 00:20:53 +0700 Subject: cmd/emaildecode: CLI to decode email body to plain text The emaildecode accept file as input. If the email header contains content-transfer-encoding with value quoted-printable or base64, it will decode the message body and print it to stdout as plain text. --- .gitignore | 1 + cmd/emaildecode/main.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 cmd/emaildecode/main.go diff --git a/.gitignore b/.gitignore index 7faf3c6e..14402872 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ *.zst /_bin/ansua /_bin/bcrypt +/_bin/emaildecode /_bin/epoch /_bin/gofmtcomment /_bin/hexo diff --git a/cmd/emaildecode/main.go b/cmd/emaildecode/main.go new file mode 100644 index 00000000..970af753 --- /dev/null +++ b/cmd/emaildecode/main.go @@ -0,0 +1,91 @@ +// Copyright 2023, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Program emaildecode convert the email body from quoted-printable to plain +// text. +// Usage, +// +// emaildecode +// +// The emaildecode accept single file as input, read its content, decode the +// body based on Content-Transfer-Encoding, and then print each body to +// standard output. +package main + +import ( + "flag" + "fmt" + "log" + "os" + + "git.sr.ht/~shulhan/pakakeh.go" + "git.sr.ht/~shulhan/pakakeh.go/lib/email" +) + +var usage = `= emaildecode - CLI to decode email body + +== USAGE + + emaildecode + +List of accepted COMMANDS, + + help - print the usage. + +FILE + The file that contains email to be decoded. + +== INFO + +Version: ` + pakakeh.Version + ` +Website: https://sr.ht/~shulhan/pakakeh.go/ +` + +const ( + cmdHelp = `help` +) + +func main() { + flag.Parse() + + var fileInput = flag.Arg(0) + if len(fileInput) == 0 { + log.Fatalf(`missing file input`) + } + + switch fileInput { + case cmdHelp: + fmt.Println(usage) + os.Exit(0) + } + + var ( + msg *email.Message + err error + ) + + msg, _, err = email.ParseFile(fileInput) + if err != nil { + log.Fatal(err) + } + + var ( + mime *email.MIME + field *email.Field + x int + ) + for _, field = range msg.Header.Fields { + fmt.Printf(`%s: %s`, field.Name, field.Value) + } + for x, mime = range msg.Body.Parts { + fmt.Printf("\n-- mime #%d\n\n", x) + if mime.Header != nil { + for _, field = range mime.Header.Fields { + fmt.Printf(`%s: %s`, field.Name, field.Value) + } + fmt.Println(``) + } + fmt.Println(string(mime.Content)) + } +} -- cgit v1.3