uwuski
sophuwu sophie@skisiel.com
Wed, 24 Jul 2024 10:01:53 +0200
5 files changed,
68 insertions(+),
18 deletions(-)
M
files.go
→
files.go
@@ -3,12 +3,12 @@
import ( "bytes" "encoding/base64" - "fmt" "io" "mime" "mime/multipart" "net/mail" "net/textproto" + "regexp" "strings" )@@ -47,31 +47,52 @@ }
if len(content) == 0 { return } + if mediaType == "text/html" || mediaType == "text/plain" { + content = []byte(func(s string) string { + s = strings.ReplaceAll(s, "=\n", "") + s = strings.ReplaceAll(s, "=3D", "=") + return s + }(string(content))) + } name := "body.txt" if params["name"] != "" { name = params["name"] } if strings.Contains(mediaType, "html") { - name = "index.html" + name = "body.html" } (*files)[name] = append((*files)[name], content...) + if head.Get("Content-ID") != "" { + cid := head.Get("Content-ID") + cid = strings.TrimPrefix(cid, "<") + cid = strings.TrimSuffix(cid, ">") + cid = "cidname: " + cid + " " + name + "\n" + (*files)["header.txt"] = append((*files)["header.txt"], []byte(cid)...) + } } -func EmlFiles(eml *mail.Message) FileList { - s := "" - for k := range eml.Header { - s += fmt.Sprintf("%s: %s\n", k, eml.Header.Get(k)) - } +var cidheader = regexp.MustCompile(`^cidname: [^ ]+ [^ ]+$`) // Content-ID header + +func EmlFiles(eml *mail.Message, head []byte) FileList { + files := make(FileList) - files["header.txt"] = []byte(s) getfiles(&files, eml) + for _, v := range cidheader.FindAll(files["header.txt"], -1) { + v = bytes.TrimSuffix(v, []byte("\n")) + v = bytes.ReplaceAll(v, []byte("cidname: "), []byte("cid:")) + n := bytes.Index(v, []byte(" ")) + files["body.html"] = bytes.ReplaceAll(files["body.html"], v[:n], v[n+1:]) + } + files["header.txt"] = head return files } func GetFiles(b *bytes.Buffer) (FileList, error) { + head := bytes.SplitN(b.Bytes(), []byte{10, 10}, 2)[0] + head = bytes.ReplaceAll(head, []byte{'\t'}, []byte{' '}) e, err := mail.ReadMessage(b) if err != nil { return nil, err } - return EmlFiles(e), nil + return EmlFiles(e, head), nil }
M
mailbox.py
→
mailbox.py
@@ -16,7 +16,8 @@
def showparts(eml): for part in eml.walk(): if part.get_payload(decode=True) is not None: - print(part.get_content_type(), part.get_content_disposition(), part.get_filename(), len(part.get_payload(decode=True))) + #print(part.get_payload(decode=True).decode('utf-8', errors='ignore')) + print(part.get_content_type()) for file in sys.argv[1:]:
M
main.go
→
main.go
@@ -1,6 +1,7 @@
package main import ( + "bytes" "database/sql" "fmt" _ "github.com/glebarez/go-sqlite"@@ -97,6 +98,26 @@ }
for _, v := range os.Args[1:] { if v == "--cli" { CLI(&metas) + return + } + if v == "--" { + var b bytes.Buffer + b.ReadFrom(os.Stdin) + fl, e := GetFiles(&b) + if e != nil { + fmt.Fprintln(os.Stderr, e) + return + } + path := filepath.Dir(DBPATH) + path = filepath.Join(path, "stdin") + _ = os.MkdirAll(path, 0700) + for name, data := range fl { + err = os.WriteFile(filepath.Join(path, name), data, 0600) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return + } + } return } }
M
parse.go
→
parse.go
@@ -110,20 +110,26 @@ h.Write(b)
return fmt.Sprintf("%X", h.Sum(nil)) } +func decodeR(s string) string { + dec := new(mime.WordDecoder) + decoded, _ := dec.DecodeHeader(s) + return decoded +} + +var decode = func(d mime.WordDecoder) func(s *string) { + return func(s *string) { + if ss, ers := d.DecodeHeader(fmt.Sprintf(*s)); ers == nil { + *s = ss + } + } +}(mime.WordDecoder{}) + // GenerateMeta generates the EmailMeta for the EmailData // This is used to index the email in the database func GenerateMeta(email bytes.Buffer) (EmailMeta, error) { var em EmailMeta em.Id = ShaHash(email.Bytes()) em.Subject = "No Subject" - - decode := func(d mime.WordDecoder) func(s *string) { - return func(s *string) { - if ss, ers := d.DecodeHeader(fmt.Sprintf(*s)); ers == nil { - *s = ss - } - } - }(mime.WordDecoder{}) e, err := mail.ReadMessage(bytes.NewReader(email.Bytes())) if err != nil {