2025-07-29 17:18:20 +02:00
|
|
|
package couch
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-08-01 16:12:17 +02:00
|
|
|
"io"
|
2025-07-29 17:18:20 +02:00
|
|
|
"net/url"
|
2025-08-01 16:12:17 +02:00
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
2025-07-29 17:18:20 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/go-kivik/kivik/v4"
|
|
|
|
|
_ "github.com/go-kivik/kivik/v4/couchdb" // The CouchDB driver
|
|
|
|
|
"mail2couch/config"
|
|
|
|
|
"mail2couch/mail"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Client wraps the Kivik client
|
|
|
|
|
type Client struct {
|
|
|
|
|
*kivik.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MailDocument represents an email message stored in CouchDB
|
|
|
|
|
type MailDocument struct {
|
2025-08-01 16:12:17 +02:00
|
|
|
ID string `json:"_id,omitempty"`
|
|
|
|
|
Rev string `json:"_rev,omitempty"`
|
|
|
|
|
Attachments map[string]AttachmentStub `json:"_attachments,omitempty"` // CouchDB attachments
|
|
|
|
|
SourceUID string `json:"sourceUid"` // Unique ID from the mail source (e.g., IMAP UID)
|
|
|
|
|
Mailbox string `json:"mailbox"` // Source mailbox name
|
|
|
|
|
From []string `json:"from"`
|
|
|
|
|
To []string `json:"to"`
|
|
|
|
|
Subject string `json:"subject"`
|
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
|
Body string `json:"body"`
|
|
|
|
|
Headers map[string][]string `json:"headers"`
|
|
|
|
|
StoredAt time.Time `json:"storedAt"` // When the document was stored
|
|
|
|
|
DocType string `json:"docType"` // Always "mail"
|
|
|
|
|
HasAttachments bool `json:"hasAttachments"` // Indicates if message has attachments
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AttachmentStub represents metadata for a CouchDB attachment
|
|
|
|
|
type AttachmentStub struct {
|
|
|
|
|
ContentType string `json:"content_type"`
|
|
|
|
|
Length int64 `json:"length,omitempty"`
|
|
|
|
|
Stub bool `json:"stub,omitempty"`
|
2025-07-29 17:18:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewClient creates a new CouchDB client from the configuration
|
|
|
|
|
func NewClient(cfg *config.CouchDbConfig) (*Client, error) {
|
|
|
|
|
parsedURL, err := url.Parse(cfg.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("invalid couchdb url: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parsedURL.User = url.UserPassword(cfg.User, cfg.Password)
|
|
|
|
|
dsn := parsedURL.String()
|
|
|
|
|
|
|
|
|
|
client, err := kivik.New("couch", dsn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Client{client}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EnsureDB ensures that the configured database exists.
|
|
|
|
|
func (c *Client) EnsureDB(ctx context.Context, dbName string) error {
|
|
|
|
|
exists, err := c.DBExists(ctx, dbName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !exists {
|
|
|
|
|
return c.CreateDB(ctx, dbName)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 16:12:17 +02:00
|
|
|
// GenerateAccountDBName creates a CouchDB-compatible database name from account info
|
|
|
|
|
func GenerateAccountDBName(accountName, userEmail string) string {
|
|
|
|
|
// Use account name if available, otherwise fall back to email
|
|
|
|
|
name := accountName
|
|
|
|
|
if name == "" {
|
|
|
|
|
name = userEmail
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert to lowercase and replace invalid characters with underscores
|
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
|
// CouchDB database names must match: ^[a-z][a-z0-9_$()+/-]*$
|
|
|
|
|
validName := regexp.MustCompile(`[^a-z0-9_$()+/-]`).ReplaceAllString(name, "_")
|
|
|
|
|
|
|
|
|
|
// Ensure it starts with a letter
|
|
|
|
|
if len(validName) > 0 && (validName[0] < 'a' || validName[0] > 'z') {
|
|
|
|
|
validName = "mail_" + validName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return validName
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 17:18:20 +02:00
|
|
|
// ConvertMessage converts an IMAP message to a MailDocument
|
|
|
|
|
func ConvertMessage(msg *mail.Message, mailbox string) *MailDocument {
|
|
|
|
|
docID := fmt.Sprintf("%s_%d", mailbox, msg.UID)
|
|
|
|
|
|
2025-08-01 16:12:17 +02:00
|
|
|
doc := &MailDocument{
|
|
|
|
|
ID: docID,
|
|
|
|
|
SourceUID: fmt.Sprintf("%d", msg.UID),
|
|
|
|
|
Mailbox: mailbox,
|
|
|
|
|
From: msg.From,
|
|
|
|
|
To: msg.To,
|
|
|
|
|
Subject: msg.Subject,
|
|
|
|
|
Date: msg.Date,
|
|
|
|
|
Body: msg.Body,
|
|
|
|
|
Headers: msg.Headers,
|
|
|
|
|
StoredAt: time.Now(),
|
|
|
|
|
DocType: "mail",
|
|
|
|
|
HasAttachments: len(msg.Attachments) > 0,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare attachment metadata if attachments exist
|
|
|
|
|
if len(msg.Attachments) > 0 {
|
|
|
|
|
doc.Attachments = make(map[string]AttachmentStub)
|
|
|
|
|
for _, att := range msg.Attachments {
|
|
|
|
|
doc.Attachments[att.Filename] = AttachmentStub{
|
|
|
|
|
ContentType: att.ContentType,
|
|
|
|
|
Length: int64(len(att.Content)),
|
|
|
|
|
Stub: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-29 17:18:20 +02:00
|
|
|
}
|
2025-08-01 16:12:17 +02:00
|
|
|
|
|
|
|
|
return doc
|
2025-07-29 17:18:20 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-01 16:12:17 +02:00
|
|
|
// StoreMessage stores a mail message in CouchDB with attachments
|
|
|
|
|
func (c *Client) StoreMessage(ctx context.Context, dbName string, doc *MailDocument, msg *mail.Message) error {
|
2025-07-29 17:18:20 +02:00
|
|
|
db := c.DB(dbName)
|
|
|
|
|
if db.Err() != nil {
|
|
|
|
|
return db.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if document already exists
|
|
|
|
|
exists, err := c.DocumentExists(ctx, dbName, doc.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to check if document exists: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
|
return nil // Document already exists, skip
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 16:12:17 +02:00
|
|
|
// Store the document first (without attachments)
|
|
|
|
|
rev, err := db.Put(ctx, doc.ID, doc)
|
2025-07-29 17:18:20 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to store document: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 16:12:17 +02:00
|
|
|
// If there are attachments, store them as CouchDB attachments
|
|
|
|
|
if msg != nil && len(msg.Attachments) > 0 {
|
|
|
|
|
for _, att := range msg.Attachments {
|
|
|
|
|
err := c.StoreAttachment(ctx, dbName, doc.ID, rev, att.Filename, att.ContentType, att.Content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to store attachment %s: %w", att.Filename, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 17:18:20 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 16:12:17 +02:00
|
|
|
// StoreAttachment stores an attachment to an existing CouchDB document
|
|
|
|
|
func (c *Client) StoreAttachment(ctx context.Context, dbName, docID, rev, filename, contentType string, content []byte) error {
|
|
|
|
|
db := c.DB(dbName)
|
|
|
|
|
if db.Err() != nil {
|
|
|
|
|
return db.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
att := &kivik.Attachment{
|
|
|
|
|
Filename: filename,
|
|
|
|
|
ContentType: contentType,
|
|
|
|
|
Content: io.NopCloser(strings.NewReader(string(content))),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := db.PutAttachment(ctx, docID, att, kivik.Rev(rev))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to store attachment: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StoreMessages stores multiple mail messages in CouchDB with their corresponding attachments
|
|
|
|
|
func (c *Client) StoreMessages(ctx context.Context, dbName string, docs []*MailDocument, messages []*mail.Message) error {
|
|
|
|
|
for i, doc := range docs {
|
|
|
|
|
var msg *mail.Message
|
|
|
|
|
if i < len(messages) {
|
|
|
|
|
msg = messages[i]
|
|
|
|
|
}
|
|
|
|
|
if err := c.StoreMessage(ctx, dbName, doc, msg); err != nil {
|
2025-07-29 17:18:20 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DocumentExists checks if a document with the given ID already exists.
|
|
|
|
|
func (c *Client) DocumentExists(ctx context.Context, dbName, docID string) (bool, error) {
|
|
|
|
|
db := c.DB(dbName)
|
|
|
|
|
if db.Err() != nil {
|
|
|
|
|
return false, db.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
row := db.Get(ctx, docID)
|
|
|
|
|
return row.Err() == nil, nil
|
|
|
|
|
}
|