-
Notifications
You must be signed in to change notification settings - Fork 0
/
spool.go
64 lines (54 loc) · 1.15 KB
/
spool.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"syscall"
"time"
)
type Spool struct {
Dir string
}
func (be *Backend) NewSpoolIncoming() (*Spool, error) {
c := be.Config
if !isDir(c.QueueIncoming) {
return nil, errors.New("No directory")
}
return &Spool{Dir: c.QueueIncoming}, nil
}
func (be *Backend) NewSpoolBounce() (*Spool, error) {
c := be.Config
if !isDir(c.QueueBounce) {
return nil, errors.New("No directory")
}
return &Spool{Dir: c.QueueBounce}, nil
}
func (spool *Spool) Store(l *List, m *Message) error {
var a string
if l.Type == "return_path" {
if l.Name == "" {
a = l.Backend.Config.Email + "@" + l.Domain
} else {
a = l.Name + "@" + l.Domain
}
} else {
a = l.String()
}
fn := fmt.Sprintf("%s.%d.%s", a, time.Now().Unix(), m.SessionId)
tmppath := filepath.Join(spool.Dir, "T."+fn)
path := filepath.Join(spool.Dir, fn)
file, err :=
os.OpenFile(tmppath, syscall.O_CREAT|syscall.O_WRONLY, 0600)
if err != nil {
return err
}
defer file.Close()
if _, err := file.Write(m.Serialized()); err != nil {
return err
}
if err := os.Rename(tmppath, path); err != nil {
return err
}
return nil
}