/
mails_go_web.go
240 lines (201 loc) · 6.23 KB
/
mails_go_web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"bytes"
"crypto/md5"
"fmt"
"html"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"regexp"
"strings"
"text/template"
"github.com/djimenez/iconv-go"
"github.com/jessevdk/go-flags"
"github.com/pbogut/mails-go-web/preview"
"github.com/veqryn/go-email/email"
)
var opts struct {
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
Resolv string `short:"r" long:"query-resolve" description:"Command to resolve a query into file path"`
Port string `short:"p" long:"port" description:"Server port" default:"6245"`
Host string `short:"s" long:"host" description:"Server host" default:"localhost"`
}
func debug(message string, params ...interface{}) {
if opts.Verbose {
fmt.Printf(message+"\n", params...)
}
}
func query_to_file(q string) string {
var file_path string
if opts.Resolv == "" {
return q
}
res := fmt.Sprintf(opts.Resolv, q)
cmd := exec.Command("sh", "-c", res)
out, _ := cmd.Output()
lines := strings.Split(string(out), "\n")
if len(lines) > 0 {
file_path = lines[0]
}
debug("Query \"%s\" resolved as \"%s\" file path.", q, file_path)
return file_path
}
func get_email_body(file_path string, query string) string {
var body string
msg := email_file_to_msg(file_path)
re := regexp.MustCompile("<(img)([^>]*)(src)=\"cid:([^\"]*)\"([^>]*)>")
for _, part := range msg.MessagesAll() {
mediaType, extra, _ := part.Header.ContentType()
switch mediaType {
case "text/html":
// by defaoult use html email
html := string(part.Body)
if len(extra["charset"]) > 0 {
html = convert(html, extra["charset"], "UTF-8")
}
idx := strings.Index(body, "<!doctype")
if idx == -1 || idx > 50 {
html = "<!doctype html>\n<base target=\"_parent\">" + html
}
// open links in parent window
body = strings.Replace(html, "<head>", `<head><base target="_parent">`, 1)
body = re.ReplaceAllString(body, "<$1$2$3=\"/?q="+query+"&file=$4\"$5>")
case "text/plain":
// use plain body only if html not available
if body == "" {
body = string(part.Body)
if len(extra["charset"]) > 0 {
body = convert(body, extra["charset"], "UTF-8")
}
}
}
}
return body
}
func get_email_view(file_path string, query string) string {
msg := email_file_to_msg(file_path)
from := strings.ToLower(extract_from_angle_brackets(msg.Header.From()))
data := []byte(from)
hash := fmt.Sprintf("%x", md5.Sum(data))
date, _ := msg.Header.Date()
preview_html := preview.Template()
var parts []map[string]string
for _, part := range msg.MessagesAll() {
partType, disposition, _ := part.Header.ContentDisposition()
if partType == "attachment" || partType == "inline" {
fileName := html.UnescapeString(q_string_decode(disposition["filename"]))
parts = append(parts, map[string]string{
"Url": "/?q=" + query + "&file=" + url.QueryEscape(fileName),
"Name": fileName,
})
}
}
m := map[string]interface{}{
"EmailHash": hash,
"From": html.EscapeString(q_string_decode(msg.Header.From())),
"To": html.EscapeString(q_string_decode(strings.Join(msg.Header.To(), ", "))),
"Date": date.Format("Mon, 2 Jan [2006-01-02 15:04:05]"),
"Subject": html.EscapeString(q_string_decode(msg.Header.Subject())),
"Query": query,
"Parts": parts,
}
content := new(bytes.Buffer)
templ, _ := template.New("preview").Parse(preview_html)
templ.Execute(content, m)
return content.String()
}
func get_email_attachment(file_path string, attachment_name string) string {
msg := email_file_to_msg(file_path)
for _, part := range msg.MessagesAll() {
_, disposition, _ := part.Header.ContentDisposition()
candidates := []string{
html.UnescapeString(q_string_decode(disposition["filename"])),
part.Header.Get("X-Attachment-Id"),
extract_from_angle_brackets(part.Header.Get("Content-Id")),
}
if contains(candidates, attachment_name) {
return string(part.Body)
}
}
return file_path + attachment_name
}
func view(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Query()["q"]) == 0 {
io.WriteString(w, "Query not provided.")
return
}
query := r.URL.Query()["q"][0]
file_path := query_to_file(query)
if _, err := os.Stat(file_path); os.IsNotExist(err) {
io.WriteString(w, "Email not found.")
return
}
var body string
if len(r.URL.Query()["eml"]) > 0 {
w.Header().Add("Content-Disposition", "attachment; filename=\""+path.Base(file_path)+".eml\"")
raw_content, _ := os.ReadFile(file_path)
body = string(raw_content)
} else if len(r.URL.Query()["file"]) > 0 {
attachment_name, _ := url.QueryUnescape(r.URL.Query()["file"][0])
w.Header().Add("Content-Disposition", "attachment; filename=\""+attachment_name+"\"")
body = get_email_attachment(file_path, attachment_name)
} else if len(r.URL.Query()["raw"]) == 0 {
body = get_email_view(file_path, query)
} else {
body = get_email_body(file_path, query)
}
// render body
io.WriteString(w, body)
}
func email_file_to_msg(file_path string) *email.Message {
file, _ := os.ReadFile(file_path)
reader := strings.NewReader(string(file))
msg, _ := email.ParseMessage(reader)
return msg
}
func q_string_decode(name string) string {
re := regexp.MustCompile("=\\?([a-zA-Z0-9_\\-]*?)\\?Q\\?(.*?)\\?=")
for _, part := range re.FindAllStringSubmatch(name, -1) {
full := part[0]
charset := part[1]
text := part[2]
re = regexp.MustCompile("=([A-F0-9][A-F0-9])")
newValue := re.ReplaceAllString(text, "%$1")
newValue, _ = url.PathUnescape(newValue)
newValue = convert(newValue, charset, "UTF-8")
name = strings.Replace(name, full, newValue, -1)
}
return name
}
func extract_from_angle_brackets(text string) string {
re := regexp.MustCompile("[^<]*<(.*)>")
return strings.ToLower(re.ReplaceAllString(text, "$1"))
}
func contains(arr []string, str string) bool {
for _, a := range arr {
if strings.ToLower(a) == strings.ToLower(str) {
return true
}
}
return false
}
func convert(text string, src string, dest string) string {
result, err := iconv.ConvertString(text, src, dest)
if err != nil {
return text
}
return result
}
func main() {
_, err := flags.Parse(&opts)
if err != nil {
return
}
http.HandleFunc("/", view)
fmt.Println("Starting server on http://" + opts.Host + ":" + opts.Port)
http.ListenAndServe(opts.Host+":"+opts.Port, nil)
}