Improve [Client] [List Messages] Grab MessageConfig #13

Merged
H0llyW00dzZ merged 1 commits from improve into master 2025-01-24 11:51:16 +00:00
2 changed files with 90 additions and 22 deletions

View File

@ -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

View File

@ -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 != "<nil>" {
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