diff options
| author | Shulhan <ms@kilabit.info> | 2024-01-25 00:20:53 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-08-04 11:33:49 +0700 |
| commit | 4ae360c5adc25051136ce638f85c7c10f856d9d3 (patch) | |
| tree | b3bfe2676406012ea7225cbd491ec4fea1a4305c | |
| parent | 9902b65d3feef74a30001b8f150299467693e699 (diff) | |
| download | pakakeh.go-4ae360c5adc25051136ce638f85c7c10f856d9d3.tar.xz | |
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.
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | cmd/emaildecode/main.go | 91 |
2 files changed, 92 insertions, 0 deletions
@@ -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 <ms@kilabit.info>. 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 <file> +// +// 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 <COMMAND | FILE> + +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)) + } +} |
