Debian Codegraph @

cmd/utils.go

raw
1 package cmd
2
3 import (
4 "bufio"
5 "fmt"
6 "io"
7 "net/mail"
8 "sort"
9 "strings"
10
11 "github.com/jhillyerd/enmime"
12 )
13
14 // AddressHeaders enumerates SMTP headers that contain email addresses
15 var addressHeaders = []string{"From", "To", "Delivered-To", "Cc", "Bcc", "Reply-To"}
16
17 // markdown adds some simple HTML tag like methods to a bufio.Writer
18 type markdown struct {
19 *bufio.Writer
20 }
21
22 func (md *markdown) H1(content string) {
23 bar := strings.Repeat("=", len(content))
24 fmt.Fprintf(md, "%v\n%v\n\n", content, bar)
25 }
26
27 func (md *markdown) H2(content string) {
28 fmt.Fprintf(md, "## %v\n", content)
29 }
30
31 func (md *markdown) H3(content string) {
32 fmt.Fprintf(md, "### %v\n", content)
33 }
34
35 // Printf implements fmt.Printf for markdown
36 func (md *markdown) Printf(format string, args ...interface{}) {
37 fmt.Fprintf(md, format, args...)
38 }
39
40 // Println implements fmt.Println for markdown
41 func (md *markdown) Println(args ...interface{}) {
42 fmt.Fprintln(md, args...)
43 }
44
45 // EnvelopeToMarkdown renders the contents of an enmime.Envelope in Markdown format. Used by
46 // mime-dump and mime-extractor commands.
47 func EnvelopeToMarkdown(w io.Writer, e *enmime.Envelope, name string) error {
48 md := &markdown{bufio.NewWriter(w)}
49
50 md.H1(name)
51
52 // Output a sorted list of headers, minus the ones displayed later
53 md.H2("Header")
54 if e.Root != nil && e.Root.Header != nil {
55 keys := make([]string, 0, len(e.Root.Header))
56 for k := range e.Root.Header {
57 switch strings.ToLower(k) {
58 case "from", "to", "cc", "bcc", "reply-to", "subject":
59 continue
60 }
61 keys = append(keys, k)
62 }
63 sort.Strings(keys)
64 for _, k := range keys {
65 md.Printf(" %v: %v\n", k, e.GetHeader(k))
66 }
67 }
68 md.Println()
69
70 md.H2("Envelope")
71 for _, hkey := range addressHeaders {
72 addrlist, err := e.AddressList(hkey)
73 if err != nil {
74 if err == mail.ErrHeaderNotPresent {
75 continue
76 }
77 return err
78 }
79 md.H3(hkey)
80 for _, addr := range addrlist {
81 md.Printf("- %v `<%v>`\n", addr.Name, addr.Address)
82 }
83 md.Println()
84 }
85 md.H3("Subject")
86 md.Println(e.GetHeader("Subject"))
87 md.Println()
88
89 md.H2("Body Text")
90 md.Println(e.Text)
91 md.Println()
92
93 md.H2("Body HTML")
94 md.Println(e.HTML)
95 md.Println()
96
97 md.H2("Attachment List")
98 for _, a := range e.Attachments {
99 md.Printf("- %v (%v)\n", a.FileName, a.ContentType)
100 if a.ContentID != "" {
101 md.Printf(" Content-ID: %s\n", a.ContentID)
102 }
103 }
104 md.Println()
105
106 md.H2("Inline List")
107 for _, a := range e.Inlines {
108 md.Printf("- %v (%v)\n", a.FileName, a.ContentType)
109 if a.ContentID != "" {
110 md.Printf(" Content-ID: %s\n", a.ContentID)
111 }
112 }
113 md.Println()
114
115 md.H2("Other Part List")
116 for _, a := range e.OtherParts {
117 md.Printf("- %v (%v)\n", a.FileName, a.ContentType)
118 if a.ContentID != "" {
119 md.Printf(" Content-ID: %s\n", a.ContentID)
120 }
121 }
122 md.Println()
123
124 md.H2("MIME Part Tree")
125 if e.Root == nil {
126 md.Println("Message was not MIME encoded")
127 } else {
128 FormatPart(md, e.Root, " ")
129 }
130
131 if len(e.Errors) > 0 {
132 md.Println()
133 md.H2("Errors")
134 for _, perr := range e.Errors {
135 md.Println("-", perr)
136 }
137 }
138
139 return md.Flush()
140 }
141
142 // FormatPart pretty prints the Part tree
143 func FormatPart(w io.Writer, p *enmime.Part, indent string) {
144 if p == nil {
145 return
146 }
147
148 sibling := p.NextSibling
149 child := p.FirstChild
150
151 // Compute indent strings
152 myindent := indent + "`-- "
153 childindent := indent + " "
154 if sibling != nil {
155 myindent = indent + "|-- "
156 childindent = indent + "| "
157 }
158 if p.Parent == nil {
159 // Root shouldn't be decorated, has no siblings
160 myindent = indent
161 childindent = indent
162 }
163
164 // Format and print this node
165 ctype := "MISSING TYPE"
166 if p.ContentType != "" {
167 ctype = p.ContentType
168 }
169 disposition := ""
170 if p.Disposition != "" {
171 disposition = fmt.Sprintf(", disposition: %s", p.Disposition)
172 }
173 filename := ""
174 if p.FileName != "" {
175 filename = fmt.Sprintf(", filename: %q", p.FileName)
176 }
177 errors := ""
178 if len(p.Errors) > 0 {
179 errors = fmt.Sprintf(" (errors: %v)", len(p.Errors))
180 }
181 fmt.Fprintf(w, "%s%s%s%s%s\n", myindent, ctype, disposition, filename, errors)
182
183 // Recurse
184 FormatPart(w, child, childindent)
185 FormatPart(w, sibling, indent)
186 }