Skip to content

Commit

Permalink
优化收发件判断逻辑,优化SMTP邮件列表
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinnrry committed Jan 14, 2024
1 parent 89f3e32 commit bdee80e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Config struct {
//go:embed tables/*
var tableConfig embed.FS

const Version = "2.3.4"
const Version = "2.3.6"

const DBTypeMySQL = "mysql"
const DBTypeSQLite = "sqlite"
Expand Down
13 changes: 11 additions & 2 deletions server/pop3_server/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type action struct {
}

// Custom 非标准命令
func (a action) Custom(session *gopop.Session, cmd string, args []string) ([]string, error) {
if session.Ctx == nil {
tc := &context.Context{}
Expand All @@ -30,6 +31,7 @@ func (a action) Custom(session *gopop.Session, cmd string, args []string) ([]str
return nil, nil
}

// Capa 说明服务端支持的命令列表
func (a action) Capa(session *gopop.Session) ([]string, error) {
if session.Ctx == nil {
tc := &context.Context{}
Expand Down Expand Up @@ -64,6 +66,7 @@ func (a action) Capa(session *gopop.Session) ([]string, error) {
return ret, nil
}

// User 提交登陆的用户名
func (a action) User(session *gopop.Session, username string) error {
if session.Ctx == nil {
tc := &context.Context{}
Expand All @@ -83,6 +86,7 @@ func (a action) User(session *gopop.Session, username string) error {
return nil
}

// Pass 提交密码验证
func (a action) Pass(session *gopop.Session, pwd string) error {
if session.Ctx == nil {
tc := &context.Context{}
Expand Down Expand Up @@ -114,6 +118,7 @@ func (a action) Pass(session *gopop.Session, pwd string) error {
return errors.New("password error")
}

// Apop APOP登陆命令
func (a action) Apop(session *gopop.Session, username, digest string) error {
if session.Ctx == nil {
tc := &context.Context{}
Expand Down Expand Up @@ -156,11 +161,12 @@ type statInfo struct {
Size int64 `json:"size"`
}

// Stat 查询邮件数量
func (a action) Stat(session *gopop.Session) (msgNum, msgSize int64, err error) {
log.WithContext(session.Ctx).Debugf("POP3 CMD: STAT")

var si statInfo
err = db.Instance.Get(&si, db.WithContext(session.Ctx.(*context.Context), "select count(1) as `num`, sum(length(text)+length(html)) as `size` from email"))
err = db.Instance.Get(&si, db.WithContext(session.Ctx.(*context.Context), "select count(1) as `num`, sum(length(text)+length(html)) as `size` from email where type = 0"))
if err != nil && !errors.Is(err, sql.ErrNoRows) {
log.WithContext(session.Ctx.(*context.Context)).Errorf("%+v", err)
err = nil
Expand Down Expand Up @@ -213,6 +219,7 @@ type listItem struct {
Size int64 `json:"size"`
}

// List 邮件列表
func (a action) List(session *gopop.Session, msg string) ([]gopop.MailInfo, error) {
log.WithContext(session.Ctx).Debugf("POP3 CMD: LIST ,Args:%s", msg)
var res []listItem
Expand All @@ -230,7 +237,7 @@ func (a action) List(session *gopop.Session, msg string) ([]gopop.MailInfo, erro
ssql = db.WithContext(session.Ctx.(*context.Context), "SELECT id, ifnull(LENGTH(TEXT) , 0) + ifnull(LENGTH(html) , 0) AS `size` FROM email where id =?")
err = db.Instance.Select(&res, ssql, listId)
} else {
ssql = db.WithContext(session.Ctx.(*context.Context), "SELECT id, ifnull(LENGTH(TEXT) , 0) + ifnull(LENGTH(html) , 0) AS `size` FROM email")
ssql = db.WithContext(session.Ctx.(*context.Context), "SELECT id, ifnull(LENGTH(TEXT) , 0) + ifnull(LENGTH(html) , 0) AS `size` FROM email where type = 0")
err = db.Instance.Select(&res, ssql)
}

Expand All @@ -249,6 +256,7 @@ func (a action) List(session *gopop.Session, msg string) ([]gopop.MailInfo, erro
return ret, nil
}

// Retr 获取邮件详情
func (a action) Retr(session *gopop.Session, id int64) (string, int64, error) {
log.WithContext(session.Ctx).Debugf("POP3 CMD: RETR ,Args:%d", id)
email, err := detail.GetEmailDetail(session.Ctx.(*context.Context), cast.ToInt(id), false)
Expand All @@ -262,6 +270,7 @@ func (a action) Retr(session *gopop.Session, id int64) (string, int64, error) {

}

// Delete 删除邮件
func (a action) Delete(session *gopop.Session, id int64) error {
log.WithContext(session.Ctx).Debugf("POP3 CMD: DELE ,Args:%d", id)

Expand Down
7 changes: 3 additions & 4 deletions server/smtp_server/read_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"pmail/dto/parsemail"
"pmail/hooks"
"pmail/services/rule"
"pmail/utils/array"
"pmail/utils/async"
"pmail/utils/context"
"pmail/utils/send"
Expand Down Expand Up @@ -60,9 +59,9 @@ func (s *Session) Data(r io.Reader) error {
}
}

// 判断是收信还是转发
account, domain := email.From.GetDomainAccount()
if array.InArray(domain, config.Instance.Domains) && s.Ctx.UserName == account {
// 判断是收信还是转发,只要是登陆了,都当成转发处理
//account, domain := email.From.GetDomainAccount()
if s.Ctx.UserID > 0 {
// 转发
err := saveEmail(ctx, email, 1, true, true)
if err != nil {
Expand Down

0 comments on commit bdee80e

Please sign in to comment.