From e129c63b37959a65c7e763a28f1f5e7ac922395d Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Fri, 24 Jan 2025 11:51:16 +0000 Subject: [PATCH] Improve [Client] [List Messages] Grab MessageConfig (#13) - [+] feat(config.go): add additional fields to MessageConfig for enhanced message retrieval - [+] feat(list_messages.go): refactor message processing to use extractDetails method for cleaner code Reviewed-on: https://git.b0zal.io/H0llyW00dzZ/imap/pulls/13 Co-authored-by: H0llyW00dzZ Co-committed-by: H0llyW00dzZ --- client/config.go | 15 +++++-- client/list_messages.go | 97 +++++++++++++++++++++++++++++++++-------- 2 files changed, 90 insertions(+), 22 deletions(-) diff --git a/client/config.go b/client/config.go index 3bd52cd..e61b781 100644 --- a/client/config.go +++ b/client/config.go @@ -17,10 +17,17 @@ type Config struct { // MessageConfig defines what parts of the message to retrieve type MessageConfig struct { - GrabID bool - GrabFrom bool - GrabSubject bool - GrabBody bool + GrabID bool + GrabFrom bool + GrabSubject bool + GrabBody bool + GrabSender bool + GrabReplyTo bool + GrabTo bool + GrabCc bool + GrabBcc bool + GrabInReplyTo bool + GrabDate bool } // IMAPClient represents an IMAP client diff --git a/client/list_messages.go b/client/list_messages.go index 4ab34cc..74d456a 100644 --- a/client/list_messages.go +++ b/client/list_messages.go @@ -7,6 +7,7 @@ package client import ( "fmt" + "time" "github.com/emersion/go-imap" "github.com/valyala/bytebufferpool" @@ -21,6 +22,20 @@ const ( KeySubject = "subject" // KeyBody is the key for the message body KeyBody = "body" + // KeySender is the key for the sender's address in the envelope + KeySender = "sender" + // KeyReplyTo is the key for the reply-to addresses + KeyReplyTo = "replyTo" + // KeyTo is the key for the 'To' addresses + KeyTo = "to" + // KeyCc is the key for the 'Cc' addresses + KeyCc = "cc" + // KeyBcc is the key for the 'Bcc' addresses + KeyBcc = "bcc" + // KeyInReplyTo is the key for the in-reply-to identifier + KeyInReplyTo = "inReplyTo" + // KeyDate is the key for the message date + KeyDate = "date" ) // ListMessages lists the messages in the specified mailbox based on the MessageConfig @@ -90,29 +105,75 @@ func (c *IMAPClient) getFetchItems(config MessageConfig) []imap.FetchItem { func (c *IMAPClient) processMessages(messages chan *imap.Message, config MessageConfig) ([]map[string]any, error) { var results []map[string]any for msg := range messages { - details := make(map[string]any) - - if config.GrabID && msg.Envelope.MessageId != "" { - details[KeyID] = msg.Envelope.MessageId + details := c.extractDetails(msg, config) + if details != nil { + results = append(results, details) } - if config.GrabFrom { - details[KeyFrom] = c.extractAddresses(msg.Envelope.From) - } - if config.GrabSubject && msg.Envelope.Subject != "" { - details[KeySubject] = msg.Envelope.Subject - } - if config.GrabBody { - body, err := c.extractBody(msg.Body) - if err != nil { - return nil, err - } - details[KeyBody] = body - } - results = append(results, details) } return results, nil } +// extractDetails extracts message details based on the MessageConfig. +func (c *IMAPClient) extractDetails(msg *imap.Message, config MessageConfig) map[string]any { + details := make(map[string]any) + + // Add message ID if configured + c.addIfNotEmpty(details, KeyID, config.GrabID, msg.Envelope.MessageId) + // Add 'From' addresses if configured + c.addAddresses(details, KeyFrom, config.GrabFrom, msg.Envelope.From) + // Add subject if configured + c.addIfNotEmpty(details, KeySubject, config.GrabSubject, msg.Envelope.Subject) + // Add body if configured + c.addBody(details, KeyBody, config.GrabBody, msg.Body) + // Add sender if configured + c.addAddresses(details, KeySender, config.GrabSender, msg.Envelope.Sender) + // Add reply-to addresses if configured + c.addAddresses(details, KeyReplyTo, config.GrabReplyTo, msg.Envelope.ReplyTo) + // Add 'To' addresses if configured + c.addAddresses(details, KeyTo, config.GrabTo, msg.Envelope.To) + // Add 'Cc' addresses if configured + c.addAddresses(details, KeyCc, config.GrabCc, msg.Envelope.Cc) + // Add 'Bcc' addresses if configured + c.addAddresses(details, KeyBcc, config.GrabBcc, msg.Envelope.Bcc) + // Add in-reply-to ID if configured + c.addIfNotEmpty(details, KeyInReplyTo, config.GrabInReplyTo, msg.Envelope.InReplyTo) + // Add date if configured + c.addIfNotZero(details, KeyDate, config.GrabDate, msg.Envelope.Date) + + return details +} + +// addIfNotEmpty adds a value to details if it's not empty or nil and grabbing is enabled. +func (c *IMAPClient) addIfNotEmpty(details map[string]any, key string, grab bool, value string) { + if grab && value != "" && value != "" { + details[key] = value + } +} + +// addAddresses adds email addresses to details if grabbing is enabled. +func (c *IMAPClient) addAddresses(details map[string]any, key string, grab bool, addresses []*imap.Address) { + if grab && len(addresses) > 0 { + details[key] = c.extractAddresses(addresses) + } +} + +// addBody adds the message body to details if grabbing is enabled. +func (c *IMAPClient) addBody(details map[string]any, key string, grab bool, body map[*imap.BodySectionName]imap.Literal) { + if grab { + content, err := c.extractBody(body) + if err == nil && content != "" { + details[key] = content + } + } +} + +// addIfNotZero adds a date to details if it's not zero and grabbing is enabled. +func (c *IMAPClient) addIfNotZero(details map[string]any, key string, grab bool, date time.Time) { + if grab && !date.IsZero() { + details[key] = date + } +} + // extractAddresses extracts email addresses from a list of IMAP addresses. func (c *IMAPClient) extractAddresses(addresses []*imap.Address) []string { var from []string