aboutsummaryrefslogblamecommitdiff
path: root/deliver_local.go
blob: 47af7f93abbad28618cd8965f7286eb5350769c0 (plain) (tree)
1
2
3
4
5


            
                 
 











                                                                                                                                                                                                         








                                  
package main

import (
	"context"

	"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) error {
	tx, err := db.Begin(ctx)
	if err != nil {
		return err
	}
	defer tx.Rollback(ctx) // BUG: Potential missed error here, but this will always fail if commit succeeded
	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
}