123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- package scanner
- import (
- "bufio"
- "bytes"
- "encoding/hex"
- "log"
- "os"
- "strings"
- "time"
- "git.semlanik.org/semlanik/gostfix/common"
- "git.semlanik.org/semlanik/gostfix/config"
- utils "git.semlanik.org/semlanik/gostfix/utils"
- "github.com/google/uuid"
- enmime "github.com/jhillyerd/enmime"
- )
- const (
- StateHeaderScan = iota
- StateBodyScan
- )
- const (
- AtLeastOneHeaderMask = 1 << iota
- FromHeaderMask
- DateHeaderMask
- ToHeaderMask
- AllHeaderMask = 15
- )
- type parseData struct {
- state int
- mandatoryHeaders int
- previousHeader *string
- email *common.Mail
- bodyContentType string
- bodyData string
- activeBoundary string
- }
- func (pd *parseData) reset() {
- *pd = parseData{
- state: StateHeaderScan,
- previousHeader: nil,
- mandatoryHeaders: 0,
- email: NewEmail(),
- bodyContentType: "plain/text",
- bodyData: "",
- activeBoundary: "",
- }
- }
- func parseFile(file *utils.LockedFile) []*common.Mail {
- var emails []*common.Mail
- pd := &parseData{}
- pd.reset()
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- switch pd.state {
- case StateHeaderScan:
- if scanner.Text() == "" {
- if pd.mandatoryHeaders&AtLeastOneHeaderMask == AtLeastOneHeaderMask {
- pd.previousHeader = nil
- boundaryCapture := utils.RegExpUtilsInstance().BoundaryFinder.FindStringSubmatch(pd.bodyContentType)
- if len(boundaryCapture) == 2 {
- pd.activeBoundary = boundaryCapture[1]
- } else {
- pd.activeBoundary = ""
- }
- pd.state = StateBodyScan
- }
- } else {
- pd.parseHeader(scanner.Text())
- }
- case StateBodyScan:
- if scanner.Text() == "" {
- if pd.state == StateBodyScan && pd.activeBoundary == "" {
- if pd.mandatoryHeaders == AllHeaderMask {
- pd.parseBody()
- emails = append(emails, pd.email)
- }
- pd.reset()
- continue
- }
- }
- if pd.activeBoundary != "" {
- pd.bodyData += scanner.Text() + "\n"
- capture := utils.RegExpUtilsInstance().BoundaryEndFinder.FindStringSubmatch(scanner.Text())
- if len(capture) == 2 && pd.activeBoundary == capture[1] {
- pd.state = StateBodyScan
- pd.activeBoundary = ""
- }
- }
- }
- }
- if pd.state == StateBodyScan {
- if pd.mandatoryHeaders == AllHeaderMask {
- pd.parseBody()
- emails = append(emails, pd.email)
- }
- pd.reset()
- }
- return emails
- }
- func (pd *parseData) parseHeader(headerRaw string) {
- capture := utils.RegExpUtilsInstance().HeaderFinder.FindStringSubmatch(headerRaw)
-
- if len(capture) == 3 {
-
- header := strings.ToLower(capture[1])
- pd.mandatoryHeaders |= AtLeastOneHeaderMask
- switch header {
- case "from":
- pd.previousHeader = &pd.email.Header.From
- pd.mandatoryHeaders |= FromHeaderMask
- case "to":
- pd.previousHeader = &pd.email.Header.To
- pd.mandatoryHeaders |= ToHeaderMask
- case "cc":
- pd.previousHeader = &pd.email.Header.Cc
- case "bcc":
- pd.previousHeader = &pd.email.Header.Bcc
- pd.mandatoryHeaders |= ToHeaderMask
- case "subject":
- pd.previousHeader = &pd.email.Header.Subject
- case "date":
- pd.previousHeader = nil
- time, err := time.Parse(time.RFC1123Z, strings.Trim(capture[2], " \t"))
- if err == nil {
- pd.email.Header.Date = time.Unix()
- pd.mandatoryHeaders |= DateHeaderMask
- } else {
- log.Printf("Invalid date format %s, %s", strings.Trim(capture[2], " \t"), err)
- }
- case "content-type":
- pd.previousHeader = &pd.bodyContentType
- default:
- pd.previousHeader = nil
- }
- if pd.previousHeader != nil {
- *pd.previousHeader = strings.Trim(capture[2], " \t")
- }
- return
- }
-
- capture = utils.RegExpUtilsInstance().FoldingFinder.FindStringSubmatch(headerRaw)
- if len(capture) == 2 && pd.previousHeader != nil {
- *pd.previousHeader += capture[1]
- }
- }
- func (pd *parseData) parseBody() {
- buffer := bytes.NewBufferString("content-type:" + pd.bodyContentType + "\n\n" + pd.bodyData)
- en, err := enmime.ReadEnvelope(buffer)
- if err != nil {
- log.Printf("Unable to read mail body %s\n\nBody content: %s\n\n", err, pd.bodyData)
- return
- }
- pd.email.Body = &common.MailBody{}
- pd.email.Body.PlainText = en.Text
- pd.email.Body.RichText = en.HTML
- for _, attachment := range en.Attachments {
- uuid := uuid.New()
- fileName := hex.EncodeToString(uuid[:])
- attachmentFile, err := os.Create(config.ConfigInstance().AttachmentsPath + "/" + fileName)
- log.Printf("Attachment found %s\n", fileName)
- if err != nil {
- log.Printf("Unable to save attachment %s %s\n", fileName, err)
- continue
- }
- pd.email.Body.Attachments = append(pd.email.Body.Attachments, &common.AttachmentHeader{
- Id: fileName,
- FileName: attachment.FileName,
- ContentType: attachment.ContentType,
- })
- attachmentFile.Write(attachment.Content)
- }
- }
|