diff options
-rw-r--r-- | deliver_local.go | 12 | ||||
-rw-r--r-- | serve_imap.go | 10 |
2 files changed, 15 insertions, 7 deletions
diff --git a/deliver_local.go b/deliver_local.go index 47af7f9..7e98aa6 100644 --- a/deliver_local.go +++ b/deliver_local.go @@ -2,16 +2,24 @@ 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) error { +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 tx.Rollback(ctx) // BUG: Potential missed error here, but this will always fail if commit succeeded + 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 { diff --git a/serve_imap.go b/serve_imap.go index 64a6ad0..2789ca3 100644 --- a/serve_imap.go +++ b/serve_imap.go @@ -2,9 +2,9 @@ package main import ( "bufio" - "fmt" "context" "crypto/tls" + "fmt" "io" "net" "strings" @@ -32,8 +32,8 @@ type imap_recv_session struct { func (session *imap_recv_session) handle(ctx context.Context) error { session.buf_conn = bufio.NewReadWriter(bufio.NewReader(session.net_conn), bufio.NewWriter(session.net_conn)) - session.buf_conn.WriteString("* OK [CAPABILITY IMAP4rev2 AUTH=PLAIN] " + VERSION + "\r\n") - session.buf_conn.Flush() + _, _ = session.buf_conn.WriteString("* OK [CAPABILITY IMAP4rev2 AUTH=PLAIN] " + VERSION + "\r\n") + _ = session.buf_conn.Flush() for { var tag, cmd, param string @@ -49,8 +49,8 @@ func (session *imap_recv_session) handle(ctx context.Context) error { line = strings.TrimSuffix(line, "\r") tag_end := strings.IndexByte(line, ' ') if tag_end == -1 { - session.buf_conn.WriteString(line+" BAD Error in IMAP command received by server.\r\n") - session.buf_conn.Flush() + _, _ = session.buf_conn.WriteString(line + " BAD Error in IMAP command received by server.\r\n") + _ = session.buf_conn.Flush() continue } tag = line[:tag_end] |