-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
130 lines (114 loc) · 3.16 KB
/
backend.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"os"
"path"
"time"
"github.com/emersion/go-smtp"
"github.com/restsend/carrot"
"gorm.io/gorm"
)
type Attachment struct {
Name string `json:"name,omitempty"`
Path string `json:"path"`
Size int `json:"size"`
}
// Mail
type Mail struct {
ID string `json:"id" gorm:"size:20;primarykey"`
CreatedAt time.Time `json:"createdAt" gorm:"index"`
UpdatedAt time.Time `json:"updatedAt" gorm:"index"`
Opened bool `json:"opened" gorm:"index"`
OpenAt sql.NullTime `json:"openAt" gorm:"index"`
Score int `json:"score" gorm:"index"`
From string `json:"from" gorm:"size:100;index"`
To string `json:"to"`
Size int `json:"size"`
Subject string `json:"subject,omitempty"`
Attachments string `json:"attachments,omitempty"`
EmbeddedFiles string `json:"embeddedFiles,omitempty"`
RemoteAddr string `json:"remoteAddr,omitempty" gorm:"size:100"`
AuthName string `json:"authName,omitempty" gorm:"size:200"`
TextBody string `json:"textBody,omitempty"`
RawBody []byte `json:"-" gorm:"-"`
}
func (m *Mail) IsValid() bool {
return m.From != "" && m.To != ""
}
func (m *Mail) Flush(db *gorm.DB, mailDir string, perm os.FileMode) (string, error) {
name := path.Join(mailDir, m.ID+".eml")
err := os.WriteFile(name, m.RawBody, perm)
if err != nil {
return name, err
}
return name, db.Save(m).Error
}
func (m *Mail) AfterDelete(tx *gorm.DB) (err error) {
mailDir := carrot.GetValue(tx, key_MAIL_DIR)
name := path.Join(mailDir, m.ID+".eml")
err = os.Remove(name)
if err != nil {
log.Println("remove file fail", name, err)
return err
}
var attachments []Attachment
json.Unmarshal([]byte(m.Attachments), &attachments)
for _, att := range attachments {
name = path.Join(mailDir, att.Path)
os.Remove(name)
}
var embeddedFiles []Attachment
json.Unmarshal([]byte(m.EmbeddedFiles), &embeddedFiles)
for _, efile := range embeddedFiles {
name = path.Join(mailDir, efile.Path)
os.Remove(name)
}
return
}
// The Backend implements SMTP server methods.
type Backend struct {
db *gorm.DB
FilePerm os.FileMode
MailDir string
AuthStrict bool
Logout io.Writer
}
func (b *Backend) Prepare() (err error) {
err = carrot.MakeMigrates(b.db, []any{
&Mail{},
})
if err != nil {
return
}
st, err := os.Stat(b.MailDir)
if err != nil {
log.Println("Prepare maildir:", b.MailDir)
return os.MkdirAll(b.MailDir, fs.ModePerm)
}
if !st.IsDir() {
return fmt.Errorf("maildir: %s is not directory", b.MailDir)
}
return
}
// NewSession is called after client greeting (EHLO, HELO).
func (b *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) {
s := &Session{
b: b,
c: c,
Logger: log.New(b.Logout, "", logFlags),
}
s.Logger.Println(s.ID(), "Incoming session")
return s, nil
}
func (bkd *Backend) AuthUser(username, password string) (bool, error) {
u, err := carrot.GetUserByEmail(bkd.db, username)
if err != nil {
return false, err
}
return carrot.CheckPassword(u, password), nil
}