aboutsummaryrefslogblamecommitdiff
path: root/deliver_local.go
blob: 7e98aa6e9de5495cd2aad190bec3d347b068dfda (plain) (tree)
1
2
3
4
5
6
7
8
9
10


            
                 
                
 
                                 


                                         
                                                                                                                                                            



                                






                                                    


                                                                                                                                                                                                         








                                  
package main

import (
	"context"
	"errors"

	"github.com/jackc/pgx/v5"
	"github.com/jackc/pgx/v5/pgxpool"
)

func deliver_local(ctx context.Context, db *pgxpool.Pool, envelope_from string, envelope_recipients []string, data []byte, addresses []string) (err error) {
	tx, err := db.Begin(ctx)
	if err != nil {
		return err
	}
	defer func() {
		err = tx.Rollback(ctx)
		if errors.Is(err, pgx.ErrTxClosed) {
			err = nil
			return
		}
	}()
	for _, address := range addresses {
		_, err = tx.Exec(ctx, "INSERT INTO mail (mailbox_id, data) SELECT a.mailbox_id, $2::bytea FROM addresses a JOIN mailboxes m ON a.mailbox_id = m.id WHERE a.address = $1;", address, data)
		if err != nil {
			return err
		}
	}
	err = tx.Commit(ctx)
	if err != nil {
		return err
	}
	return nil
}