From 92694600c9011fb9495c12a0e0fdaeffac5b37f8 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 12 Jan 2025 05:11:18 +0800 Subject: incoming -> deliver_dir --- deliver_dir.go | 33 +++++++++++++++++++++++++++++++++ incoming.go | 33 --------------------------------- mta_recv.go | 2 +- 3 files changed, 34 insertions(+), 34 deletions(-) create mode 100644 deliver_dir.go delete mode 100644 incoming.go diff --git a/deliver_dir.go b/deliver_dir.go new file mode 100644 index 0000000..bba9eb4 --- /dev/null +++ b/deliver_dir.go @@ -0,0 +1,33 @@ +package main + +import ( + "os" + "time" + + "go.lindenii.runxiyu.org/lindenii-common/clog" + "go.lindenii.runxiyu.org/lindenii-common/misc" +) + +func deliver_to_local_directory(envelope_from string, envelope_recipients []string, data []byte, dir_path string) error { + clog.Debug( + "incoming_mail", + "envelope_from", envelope_from, + "envelope_recipients", envelope_recipients, + "data", string(data), + ) + t := time.Now() + dir, err := misc.Open_directory_readonly(dir_path) + if err != nil { + return misc.Wrap_one_error(err_deliver_write, err) + } + fd, err := misc.Open_file_at(dir, envelope_from+" "+t.Format(time.RFC3339Nano)+".eml", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) + if err != nil { + // TODO: handle fs.ErrExist + return misc.Wrap_one_error(err_deliver_write, err) + } + _, err = fd.Write(data) + if err != nil { + return misc.Wrap_one_error(err_deliver_write, err) + } + return nil +} diff --git a/incoming.go b/incoming.go deleted file mode 100644 index e672587..0000000 --- a/incoming.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "os" - "time" - - "go.lindenii.runxiyu.org/lindenii-common/clog" - "go.lindenii.runxiyu.org/lindenii-common/misc" -) - -func deliver_incoming(envelope_from string, envelope_recipients []string, data []byte, dir_path string) error { - clog.Debug( - "incoming_mail", - "envelope_from", envelope_from, - "envelope_recipients", envelope_recipients, - "data", string(data), - ) - t := time.Now() - dir, err := misc.Open_directory_readonly(dir_path) - if err != nil { - return misc.Wrap_one_error(err_deliver_write, err) - } - fd, err := misc.Open_file_at(dir, envelope_from+" "+t.Format(time.RFC3339Nano)+".eml", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) - if err != nil { - // TODO: handle fs.ErrExist - return misc.Wrap_one_error(err_deliver_write, err) - } - _, err = fd.Write(data) - if err != nil { - return misc.Wrap_one_error(err_deliver_write, err) - } - return nil -} diff --git a/mta_recv.go b/mta_recv.go index d5a14ed..ea1a1ef 100644 --- a/mta_recv.go +++ b/mta_recv.go @@ -155,7 +155,7 @@ func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Write inboxes_to_deliver_to[inbox] = struct{}{} } for inbox := range inboxes_to_deliver_to { - err = deliver_incoming(current_mail_from, current_rcpt_to, current_data, inbox) + err = deliver_to_local_directory(current_mail_from, current_rcpt_to, current_data, inbox) } } if err == nil { -- cgit v1.2.3