aboutsummaryrefslogtreecommitdiff
path: root/deliver_dir.go
blob: bba9eb44b74f48cbed2b68227d32682a6679da59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
}