2025-01-24 11:26:48 +07:00
|
|
|
// Copyright (c) 2025 H0llyW00dzZ All rights reserved.
|
|
|
|
//
|
|
|
|
// By accessing or using this software, you agree to be bound by the terms
|
|
|
|
// of the License Agreement, which you can find at LICENSE files.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/emersion/go-imap"
|
|
|
|
"github.com/valyala/bytebufferpool"
|
|
|
|
)
|
|
|
|
|
2025-01-24 07:08:59 +00:00
|
|
|
const (
|
|
|
|
// KeyID is the key for the message ID
|
|
|
|
KeyID = "id"
|
|
|
|
// KeyFrom is the key for the sender's address
|
|
|
|
KeyFrom = "from"
|
|
|
|
// KeySubject is the key for the message subject
|
|
|
|
KeySubject = "subject"
|
|
|
|
// KeyBody is the key for the message body
|
|
|
|
KeyBody = "body"
|
|
|
|
)
|
2025-01-24 11:26:48 +07:00
|
|
|
|
|
|
|
// ListMessages lists the messages in the specified mailbox based on the MessageConfig
|
2025-01-24 07:08:59 +00:00
|
|
|
func (c *IMAPClient) ListMessages(mailbox string, numMessages uint32, config MessageConfig) ([]map[string]any, error) {
|
2025-01-24 11:26:48 +07:00
|
|
|
if c.client == nil {
|
|
|
|
return nil, fmt.Errorf("client is not connected")
|
|
|
|
}
|
|
|
|
|
2025-01-24 08:10:45 +00:00
|
|
|
mbox, err := c.selectMailbox(mailbox)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
seqset := c.createSeqSet(mbox.Messages, numMessages)
|
|
|
|
items := c.getFetchItems(config)
|
|
|
|
|
|
|
|
messages := make(chan *imap.Message, numMessages)
|
|
|
|
done := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
done <- c.client.Fetch(seqset, items, messages)
|
|
|
|
}()
|
|
|
|
|
|
|
|
results, err := c.processMessages(messages, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := <-done; err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch messages: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectMailbox selects the specified mailbox and returns its status.
|
|
|
|
// It returns an error if the mailbox cannot be selected.
|
|
|
|
func (c *IMAPClient) selectMailbox(mailbox string) (*imap.MailboxStatus, error) {
|
2025-01-24 11:26:48 +07:00
|
|
|
mbox, err := c.client.Select(mailbox, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to select %s: %v", mailbox, err)
|
|
|
|
}
|
2025-01-24 08:10:45 +00:00
|
|
|
return mbox, nil
|
|
|
|
}
|
2025-01-24 11:26:48 +07:00
|
|
|
|
2025-01-24 08:10:45 +00:00
|
|
|
// createSeqSet creates a sequence set for fetching messages based on the total and desired number of messages.
|
|
|
|
func (c *IMAPClient) createSeqSet(totalMessages, numMessages uint32) *imap.SeqSet {
|
2025-01-24 11:26:48 +07:00
|
|
|
from := uint32(1)
|
2025-01-24 08:10:45 +00:00
|
|
|
to := totalMessages
|
|
|
|
if totalMessages > numMessages {
|
|
|
|
from = totalMessages - numMessages + 1
|
2025-01-24 11:26:48 +07:00
|
|
|
}
|
|
|
|
seqset := new(imap.SeqSet)
|
|
|
|
seqset.AddRange(from, to)
|
2025-01-24 08:10:45 +00:00
|
|
|
return seqset
|
|
|
|
}
|
2025-01-24 11:26:48 +07:00
|
|
|
|
2025-01-24 08:10:45 +00:00
|
|
|
// getFetchItems determines which parts of the message to fetch based on the MessageConfig.
|
|
|
|
func (c *IMAPClient) getFetchItems(config MessageConfig) []imap.FetchItem {
|
2025-01-24 11:26:48 +07:00
|
|
|
items := []imap.FetchItem{imap.FetchEnvelope}
|
|
|
|
if config.GrabBody {
|
|
|
|
items = append(items, imap.FetchItem("BODY.PEEK[]"))
|
|
|
|
}
|
2025-01-24 08:10:45 +00:00
|
|
|
return items
|
|
|
|
}
|
2025-01-24 11:26:48 +07:00
|
|
|
|
2025-01-24 08:10:45 +00:00
|
|
|
// processMessages processes fetched messages and extracts details based on the MessageConfig.
|
|
|
|
func (c *IMAPClient) processMessages(messages chan *imap.Message, config MessageConfig) ([]map[string]any, error) {
|
2025-01-24 07:08:59 +00:00
|
|
|
var results []map[string]any
|
2025-01-24 11:26:48 +07:00
|
|
|
for msg := range messages {
|
2025-01-24 07:08:59 +00:00
|
|
|
details := make(map[string]any)
|
2025-01-24 11:26:48 +07:00
|
|
|
|
2025-01-24 07:08:59 +00:00
|
|
|
if config.GrabID && msg.Envelope.MessageId != "" {
|
|
|
|
details[KeyID] = msg.Envelope.MessageId
|
2025-01-24 11:26:48 +07:00
|
|
|
}
|
|
|
|
if config.GrabFrom {
|
2025-01-24 08:10:45 +00:00
|
|
|
details[KeyFrom] = c.extractAddresses(msg.Envelope.From)
|
2025-01-24 11:26:48 +07:00
|
|
|
}
|
2025-01-24 07:08:59 +00:00
|
|
|
if config.GrabSubject && msg.Envelope.Subject != "" {
|
|
|
|
details[KeySubject] = msg.Envelope.Subject
|
2025-01-24 11:26:48 +07:00
|
|
|
}
|
|
|
|
if config.GrabBody {
|
2025-01-24 08:10:45 +00:00
|
|
|
body, err := c.extractBody(msg.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2025-01-24 11:26:48 +07:00
|
|
|
}
|
2025-01-24 08:10:45 +00:00
|
|
|
details[KeyBody] = body
|
2025-01-24 11:26:48 +07:00
|
|
|
}
|
|
|
|
results = append(results, details)
|
|
|
|
}
|
2025-01-24 08:10:45 +00:00
|
|
|
return results, nil
|
|
|
|
}
|
2025-01-24 11:26:48 +07:00
|
|
|
|
2025-01-24 08:10:45 +00:00
|
|
|
// extractAddresses extracts email addresses from a list of IMAP addresses.
|
|
|
|
func (c *IMAPClient) extractAddresses(addresses []*imap.Address) []string {
|
|
|
|
var from []string
|
|
|
|
for _, addr := range addresses {
|
|
|
|
from = append(from, addr.Address())
|
2025-01-24 11:26:48 +07:00
|
|
|
}
|
2025-01-24 08:10:45 +00:00
|
|
|
return from
|
|
|
|
}
|
2025-01-24 11:26:48 +07:00
|
|
|
|
2025-01-24 08:10:45 +00:00
|
|
|
// extractBody reads and returns the body content from the message body literals.
|
|
|
|
func (c *IMAPClient) extractBody(body map[*imap.BodySectionName]imap.Literal) (string, error) {
|
|
|
|
for _, literal := range body {
|
|
|
|
buf := bytebufferpool.Get()
|
|
|
|
if _, err := buf.ReadFrom(literal); err == nil {
|
|
|
|
result := buf.String()
|
|
|
|
buf.Reset() // Reset the buffer before returning it to the pool.
|
|
|
|
bytebufferpool.Put(buf)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
bytebufferpool.Put(buf)
|
|
|
|
return "", fmt.Errorf("failed to read body")
|
|
|
|
}
|
|
|
|
return "", nil
|
2025-01-24 11:26:48 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListUserMessages lists messages for a specific user based on the MessageConfig
|
2025-01-24 07:08:59 +00:00
|
|
|
func (m *MultiUserIMAP) ListUserMessages(username, mailbox string, numMessages uint32, config MessageConfig) ([]map[string]any, error) {
|
2025-01-24 11:26:48 +07:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
client, exists := m.clients[username]
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("user not found: %s", username)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.ListMessages(mailbox, numMessages, config)
|
|
|
|
}
|