2021-07-18 23:03:28 +01:00
|
|
|
/*
|
2026-02-14 02:34:09 +05:00
|
|
|
* Original Copyright (c) 2021 Neil Alexander)
|
2021-07-18 23:03:28 +01:00
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-07 18:15:07 +01:00
|
|
|
package smtpsender
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-14 02:34:09 +05:00
|
|
|
"crypto/ed25519"
|
2025-12-02 22:34:08 +00:00
|
|
|
"database/sql"
|
2021-07-09 00:26:43 +01:00
|
|
|
"encoding/hex"
|
2025-12-02 22:34:08 +00:00
|
|
|
"errors"
|
2021-07-07 18:15:07 +01:00
|
|
|
"fmt"
|
2026-02-14 02:34:09 +05:00
|
|
|
"io"
|
2021-07-07 18:15:07 +01:00
|
|
|
"log"
|
2021-07-09 23:43:09 +01:00
|
|
|
"net/mail"
|
2021-07-07 18:15:07 +01:00
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/neilalexander/yggmail/internal/config"
|
2026-02-14 02:34:09 +05:00
|
|
|
"github.com/neilalexander/yggmail/internal/notify"
|
|
|
|
|
"github.com/neilalexander/yggmail/internal/remote"
|
|
|
|
|
pb "github.com/neilalexander/yggmail/internal/remote/types"
|
2021-07-09 23:43:09 +01:00
|
|
|
"github.com/neilalexander/yggmail/internal/storage"
|
2026-02-14 02:34:09 +05:00
|
|
|
"github.com/neilalexander/yggmail/internal/storage/types"
|
2021-07-07 18:15:07 +01:00
|
|
|
"github.com/neilalexander/yggmail/internal/transport"
|
2021-07-09 23:43:09 +01:00
|
|
|
"github.com/neilalexander/yggmail/internal/utils"
|
2021-07-07 18:15:07 +01:00
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Queues struct {
|
|
|
|
|
Config *config.Config
|
|
|
|
|
Log *log.Logger
|
|
|
|
|
Transport transport.Transport
|
2021-07-09 23:43:09 +01:00
|
|
|
Storage storage.Storage
|
2026-02-14 02:34:09 +05:00
|
|
|
RPCClient *remote.Client
|
|
|
|
|
Notify *notify.Notify
|
|
|
|
|
queues sync.Map
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
func NewQueues(config *config.Config, log *log.Logger, transport transport.Transport, storage storage.Storage, rpcClient *remote.Client, notify *notify.Notify) *Queues {
|
2021-07-09 23:43:09 +01:00
|
|
|
qs := &Queues{
|
2021-07-07 18:15:07 +01:00
|
|
|
Config: config,
|
|
|
|
|
Log: log,
|
|
|
|
|
Transport: transport,
|
2021-07-09 23:43:09 +01:00
|
|
|
Storage: storage,
|
2026-02-14 02:34:09 +05:00
|
|
|
RPCClient: rpcClient,
|
|
|
|
|
Notify: notify,
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
2023-11-03 14:03:41 +00:00
|
|
|
time.AfterFunc(time.Second*5, qs.manager)
|
2021-07-09 23:43:09 +01:00
|
|
|
return qs
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-09 23:43:09 +01:00
|
|
|
func (qs *Queues) manager() {
|
|
|
|
|
destinations, err := qs.Storage.QueueListDestinations()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, destination := range destinations {
|
2026-02-14 02:34:09 +05:00
|
|
|
_ = qs.QueueForServer(destination)
|
2021-07-09 23:43:09 +01:00
|
|
|
}
|
2023-11-03 14:03:41 +00:00
|
|
|
time.AfterFunc(time.Minute, qs.manager)
|
2021-07-09 23:43:09 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
func (qs *Queues) QueueFor(storage storage.Storage, senderPK string, from string, rcpts []string, m io.Reader, size int) error {
|
|
|
|
|
pid, err := storage.MailCreate(senderPK, "Outbox", m, size)
|
2021-07-09 23:43:09 +01:00
|
|
|
if err != nil {
|
2026-02-14 02:34:09 +05:00
|
|
|
return fmt.Errorf("storage.MailCreate(Outbox): %w", err)
|
2021-07-09 23:43:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, rcpt := range rcpts {
|
|
|
|
|
addr, err := mail.ParseAddress(rcpt)
|
|
|
|
|
if err != nil {
|
2026-02-14 02:34:09 +05:00
|
|
|
continue
|
2021-07-09 23:43:09 +01:00
|
|
|
}
|
2026-02-14 02:34:09 +05:00
|
|
|
|
|
|
|
|
dPk, _, err := utils.ParseAddress(addr.Address)
|
2021-07-09 23:43:09 +01:00
|
|
|
if err != nil {
|
2025-11-27 00:09:06 +00:00
|
|
|
continue
|
|
|
|
|
}
|
2021-07-09 23:43:09 +01:00
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
hostHex := hex.EncodeToString(dPk)
|
|
|
|
|
err = storage.QueueInsertDestinationForID(senderPK, hostHex, pid, from, rcpt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
qs.Log.Printf("Failed to insert queue for %s: %v", hostHex, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if storage == qs.Storage {
|
|
|
|
|
_ = qs.QueueForServer(hostHex)
|
2021-07-09 23:43:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
func (qs *Queues) QueueForServer(server string) error {
|
2021-07-07 18:15:07 +01:00
|
|
|
v, _ := qs.queues.LoadOrStore(server, &Queue{
|
|
|
|
|
queues: qs,
|
|
|
|
|
destination: server,
|
|
|
|
|
})
|
|
|
|
|
q, ok := v.(*Queue)
|
|
|
|
|
if !ok {
|
2026-02-14 02:34:09 +05:00
|
|
|
return fmt.Errorf("type assertion error")
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
2023-11-03 14:03:41 +00:00
|
|
|
if q.running.CompareAndSwap(false, true) {
|
2021-07-07 18:15:07 +01:00
|
|
|
go q.run()
|
|
|
|
|
}
|
2026-02-14 02:34:09 +05:00
|
|
|
return nil
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Queue struct {
|
2026-02-14 02:34:09 +05:00
|
|
|
queues *Queues
|
|
|
|
|
// hex16
|
2021-07-07 18:15:07 +01:00
|
|
|
destination string
|
|
|
|
|
running atomic.Bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queue) run() {
|
|
|
|
|
defer q.running.Store(false)
|
2026-02-14 02:34:09 +05:00
|
|
|
for {
|
|
|
|
|
refs, err := q.queues.Storage.QueueMailIDsForDestination(q.destination)
|
|
|
|
|
if err != nil || len(refs) == 0 {
|
|
|
|
|
return
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
2021-07-09 23:43:09 +01:00
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
for _, ref := range refs {
|
|
|
|
|
_, mail, err := q.queues.Storage.MailSelect(ref.AccountPubKey, "Outbox", ref.ID)
|
2021-07-07 18:15:07 +01:00
|
|
|
if err != nil {
|
2026-02-14 02:34:09 +05:00
|
|
|
q.handleError(ref, err)
|
|
|
|
|
continue
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
q.queues.Log.Printf("Sending mail from %s (Acc: %s) to %s", ref.From, ref.AccountPubKey, q.destination)
|
2021-07-07 18:15:07 +01:00
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
deliver := q.deliverRemote
|
|
|
|
|
if q.destination == hex.EncodeToString(q.queues.Config.PublicKey) {
|
|
|
|
|
deliver = q.deliverLocal
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
if err := deliver(ref, mail.Body, mail.Size); err != nil {
|
|
|
|
|
q.queues.Log.Printf("Delivery to %s failed: %v. Will retry later.", q.destination, err)
|
|
|
|
|
return
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
q.finalize(ref)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-07 18:15:07 +01:00
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
func (q *Queue) deliverLocal(ref types.QueuedMail, content io.ReadCloser, size uint32) error {
|
|
|
|
|
q.queues.Log.Printf("Local delivery for %s", ref.Rcpt)
|
2021-07-07 18:15:07 +01:00
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
_, toAcc, err := utils.ParseAddress(ref.Rcpt)
|
|
|
|
|
toAccPk := utils.EncodeString(toAcc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("local delivery: invalid rcpt address: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if acc, err := q.queues.Storage.AccountGet(toAccPk); acc == nil && err != nil {
|
|
|
|
|
return fmt.Errorf("local delivery: account not found")
|
|
|
|
|
}
|
2021-07-07 18:15:07 +01:00
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
_ = q.queues.Storage.MailboxCreate(toAccPk, "INBOX")
|
|
|
|
|
id, err := q.queues.Storage.MailCreate(toAccPk, "INBOX", content, int(size))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("local delivery failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if count, err := q.queues.Storage.MailCount(toAccPk, "INBOX"); err == nil {
|
|
|
|
|
q.queues.Notify.NotifyNew(toAccPk, notify.NewEmailEvent(toAccPk, "INBOX", int64(id), int64(count)))
|
|
|
|
|
}
|
2021-07-09 23:43:09 +01:00
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
return nil
|
|
|
|
|
}
|
2021-07-09 23:43:09 +01:00
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
// Deliver incapsulate SMTP-Transaction
|
|
|
|
|
func (q *Queue) deliverRemote(ref types.QueuedMail, content io.ReadCloser, size uint32) error {
|
|
|
|
|
dPkb, err := hex.DecodeString(q.destination)
|
|
|
|
|
dPk := ed25519.PublicKey(dPkb)
|
|
|
|
|
|
|
|
|
|
resp, _, err := q.queues.RPCClient.Call(dPk, &pb.Request{
|
|
|
|
|
Call: &pb.Request_MailPush{
|
|
|
|
|
MailPush: &pb.MailPushRequest{
|
|
|
|
|
From: ref.From,
|
|
|
|
|
To: ref.Rcpt,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}, content, size)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("rpc call failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !resp.Success {
|
|
|
|
|
return fmt.Errorf("remote server returned error: %s", resp.Error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
q.queues.Log.Printf("Sender: RPC delivery successful for ID %d to node %s", ref.ID, q.destination)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queue) finalize(ref types.QueuedMail) {
|
|
|
|
|
// Delete id from Queue
|
|
|
|
|
_ = q.queues.Storage.QueueDeleteDestinationForID(ref.AccountPubKey, q.destination, ref.ID)
|
|
|
|
|
|
|
|
|
|
remaining, err := q.queues.Storage.QueueSelectIsMessagePendingSend(ref.AccountPubKey, "Outbox", ref.ID)
|
|
|
|
|
if err == nil && !remaining {
|
|
|
|
|
q.queues.Log.Printf("Moving mail %d to Sent for %s", ref.ID, ref.AccountPubKey)
|
|
|
|
|
_, _ = q.queues.Storage.MailMove(ref.AccountPubKey, "Outbox", ref.ID, "Sent")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ = q.queues.Storage.MailExpunge(ref.AccountPubKey, "Outbox")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queue) handleError(ref types.QueuedMail, err error) {
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
q.queues.Log.Printf("Cleaning up orphaned queue ref %d for %s", ref.ID, ref.AccountPubKey)
|
|
|
|
|
_ = q.queues.Storage.QueueDeleteDestinationForID(ref.AccountPubKey, q.destination, ref.ID)
|
|
|
|
|
} else {
|
|
|
|
|
q.queues.Log.Printf("Storage error for %s: %v", ref.AccountPubKey, err)
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
}
|