aboutsummaryrefslogtreecommitdiff
path: root/deliver_local.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-01-13 12:02:49 +0800
committerRunxi Yu <me@runxiyu.org>2025-01-13 12:02:49 +0800
commit42c5f39700c6ba95a6b924be807e8cddd69c3bdd (patch)
treea61dc937793b00684a03a14b2ec4adfca5f632da /deliver_local.go
parentAdd addresses (diff)
downloadmaild-42c5f39700c6ba95a6b924be807e8cddd69c3bdd.tar.gz
maild-42c5f39700c6ba95a6b924be807e8cddd69c3bdd.tar.zst
maild-42c5f39700c6ba95a6b924be807e8cddd69c3bdd.zip
Add PostgreSQL mail store support
Diffstat (limited to 'deliver_local.go')
-rw-r--r--deliver_local.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/deliver_local.go b/deliver_local.go
new file mode 100644
index 0000000..9a29459
--- /dev/null
+++ b/deliver_local.go
@@ -0,0 +1,27 @@
+package main
+
+import (
+ "fmt"
+ "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 {
+ fmt.Printf("%#v\n", err)
+ return err
+ }
+ }
+ err = tx.Commit(ctx)
+ if err != nil {
+ return err
+ }
+ return nil
+}