something
sophuwu sophie@skisiel.com
Sat, 20 Jul 2024 04:30:24 +0200
4 files changed,
104 insertions(+),
0 deletions(-)
A
go.mod
@@ -0,0 +1,8 @@
+module sophuwu.site/mailbox + +go 1.22.5 + +require ( + github.com/DusanKasan/parsemail v1.2.0 // indirect + github.com/andybalholm/brotli v1.1.0 // indirect +)
A
go.sum
@@ -0,0 +1,4 @@
+github.com/DusanKasan/parsemail v1.2.0 h1:CrzTL1nuPLxB41aO4zE/Tzc9GVD8jjifUftlbTKQQl4= +github.com/DusanKasan/parsemail v1.2.0/go.mod h1:B9lfMbpVe4DMqPImAOCGti7KEwasnRTrKKn66iQefVs= +github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= +github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
A
main.go
@@ -0,0 +1,90 @@
+package main + +import ( + "bytes" + "crypto/sha1" + "encoding/json" + "fmt" + "github.com/DusanKasan/parsemail" + "github.com/andybalholm/brotli" + "os" + "time" +) + +func br(buf *bytes.Buffer) error { + data := buf.Bytes() + buf.Reset() + // Create a new buffer to hold the compressed data + writer := brotli.NewWriter(buf) + _, err := writer.Write(data) + if err != nil { + return err + } + // Close the writer to flush any remaining data + return writer.Close() +} + +func main() { + // if len(os.Args)>0 && filepath.Base(os.Args[0])== "mailbox-parse"{ + var email bytes.Buffer + if _, err := email.ReadFrom(os.Stdin); err != nil { + os.Exit(1) + } + meta, err := GenerateMeta(email) + if err != nil { + os.Exit(1) + } + js, _ := json.Marshal(meta) + fmt.Println(string(js)) + _ = br(&email) + + // } +} + +// 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 = fmt.Sprintf("%X", sha1.Sum(email.Bytes())) + + e, err := parsemail.Parse(&email) + + if err != nil { + return em, err + } + if e.Header["X-Original-To"] != nil { + em.To = e.Header["X-Original-To"][0] + } else if len(e.To) > 0 { + em.To = e.To[0].String() + } else { + em.To = "Unknown" + } + if len(e.From) > 0 { + em.From = e.From[0].String() + } else if e.Header["Return-Path"] != nil { + em.From = e.Header["Return-Path"][0] + } else { + em.From = "Unknown" + } + if len(e.Subject) > 0 { + em.Subject = e.Subject + } else { + em.Subject = "No Subject" + } + if !e.Date.IsZero() { + em.Date = e.Date.Local().Unix() + } else { + em.Date = time.Now().Local().Unix() + } + + return em, nil +} + +// EmailMeta contains the fields that will be searchable in the database +type EmailMeta struct { + From string `storm:"index" json:"from"` + To string `storm:"index" json:"to"` + Subject string `storm:"index" json:"subject"` + Date int64 `storm:"index" json:"date"` + Id string `storm:"id" json:"id"` +}