aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-01-04 21:01:08 +0800
committerRunxi Yu <me@runxiyu.org>2025-01-04 21:18:00 +0800
commitc4b96658a0883d94186ce2b64305dcafaf567922 (patch)
treef7826c4b4629e489578c57e5941ba8b53fb78e85
parentReformat files (diff)
downloadmaild-c4b96658a0883d94186ce2b64305dcafaf567922.tar.gz
maild-c4b96658a0883d94186ce2b64305dcafaf567922.tar.zst
maild-c4b96658a0883d94186ce2b64305dcafaf567922.zip
Use Open_file_at instead of Open
-rw-r--r--config.go17
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--incoming.go8
-rw-r--r--main.go5
-rw-r--r--mta_recv.go2
6 files changed, 30 insertions, 8 deletions
diff --git a/config.go b/config.go
index f965c24..1ec7207 100644
--- a/config.go
+++ b/config.go
@@ -5,6 +5,7 @@ import (
"os"
"sync"
+ "go.lindenii.runxiyu.org/lindenii-common/misc"
"go.lindenii.runxiyu.org/lindenii-common/scfg"
)
@@ -33,3 +34,19 @@ func load_config(path string) error {
}
return nil
}
+
+var directories struct {
+ inbox misc.Dir_t
+}
+var directories_mutex sync.RWMutex
+
+func prepare_dirs() error {
+ directories_mutex.Lock()
+ defer directories_mutex.Unlock()
+ var err error
+ directories.inbox, err = misc.Open_directory_readonly(config.Inbox_path)
+ if err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/go.mod b/go.mod
index cbbf3f2..ad6be5f 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,4 @@ module go.lindenii.runxiyu.org/maild
go 1.23.4
-require go.lindenii.runxiyu.org/lindenii-common v0.0.0-20241229193139-603ef6227aca
+require go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104131426-7a480ad159b9
diff --git a/go.sum b/go.sum
index dbcd25c..bccd500 100644
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,2 @@
-go.lindenii.runxiyu.org/lindenii-common v0.0.0-20241229193139-603ef6227aca h1:iyxIKtyBNe5RlIMVu+whX2r2JGK9JCo6RotHrHstb0U=
-go.lindenii.runxiyu.org/lindenii-common v0.0.0-20241229193139-603ef6227aca/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA=
+go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104131426-7a480ad159b9 h1:9H+rOwTeOYBoi11NSXn4IN4j0/3zo5nT8HA2Ktrdur0=
+go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104131426-7a480ad159b9/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA=
diff --git a/incoming.go b/incoming.go
index e264262..35aa006 100644
--- a/incoming.go
+++ b/incoming.go
@@ -2,10 +2,10 @@ package main
import (
"os"
- "path"
"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) error {
@@ -16,14 +16,14 @@ func deliver_incoming(envelope_from string, envelope_recipients []string, data [
"data", string(data),
)
t := time.Now()
- fd, err := os.OpenFile(path.Join(config.Inbox_path, envelope_from+" "+t.Format(time.RFC3339Nano)+".eml"), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o660) // TODO: vulnerability
+ fd, err := misc.Open_file_at(directories.inbox, 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 err_deliver_write
+ return misc.Wrap_one_error(err_deliver_write, err)
}
_, err = fd.Write(data)
if err != nil {
- return err_deliver_write
+ return misc.Wrap_one_error(err_deliver_write, err)
}
return nil
}
diff --git a/main.go b/main.go
index 94f195e..2d6b706 100644
--- a/main.go
+++ b/main.go
@@ -19,6 +19,11 @@ func main() {
panic(err)
}
+ err = prepare_dirs()
+ if err != nil {
+ panic(err)
+ }
+
listener, err := net.Listen("tcp", ":25")
if err != nil {
panic(err)
diff --git a/mta_recv.go b/mta_recv.go
index 1d2c4f3..fe6139f 100644
--- a/mta_recv.go
+++ b/mta_recv.go
@@ -133,7 +133,7 @@ func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Write
if err == nil {
_, _ = writer.WriteString("250 2.0.0 Ok: Accepted\r\n")
} else {
- _, _ = writer.WriteString("500 2.0.0 Funderscore\r\n")
+ _, _ = writer.WriteString("500 2.0.0 Error: "+err.Error()+"\r\n")
}
_ = writer.Flush()
server_state = server_state_helo