优化收发件判断逻辑,优化SMTP邮件列表

This commit is contained in:
jinnrry 2024-01-14 19:21:00 +08:00
parent 89f3e32ae4
commit bdee80efc6
3 changed files with 15 additions and 7 deletions

View File

@ -39,7 +39,7 @@ type Config struct {
//go:embed tables/* //go:embed tables/*
var tableConfig embed.FS var tableConfig embed.FS
const Version = "2.3.4" const Version = "2.3.6"
const DBTypeMySQL = "mysql" const DBTypeMySQL = "mysql"
const DBTypeSQLite = "sqlite" const DBTypeSQLite = "sqlite"

View File

@ -19,6 +19,7 @@ import (
type action struct { type action struct {
} }
// Custom 非标准命令
func (a action) Custom(session *gopop.Session, cmd string, args []string) ([]string, error) { func (a action) Custom(session *gopop.Session, cmd string, args []string) ([]string, error) {
if session.Ctx == nil { if session.Ctx == nil {
tc := &context.Context{} tc := &context.Context{}
@ -30,6 +31,7 @@ func (a action) Custom(session *gopop.Session, cmd string, args []string) ([]str
return nil, nil return nil, nil
} }
// Capa 说明服务端支持的命令列表
func (a action) Capa(session *gopop.Session) ([]string, error) { func (a action) Capa(session *gopop.Session) ([]string, error) {
if session.Ctx == nil { if session.Ctx == nil {
tc := &context.Context{} tc := &context.Context{}
@ -64,6 +66,7 @@ func (a action) Capa(session *gopop.Session) ([]string, error) {
return ret, nil return ret, nil
} }
// User 提交登陆的用户名
func (a action) User(session *gopop.Session, username string) error { func (a action) User(session *gopop.Session, username string) error {
if session.Ctx == nil { if session.Ctx == nil {
tc := &context.Context{} tc := &context.Context{}
@ -83,6 +86,7 @@ func (a action) User(session *gopop.Session, username string) error {
return nil return nil
} }
// Pass 提交密码验证
func (a action) Pass(session *gopop.Session, pwd string) error { func (a action) Pass(session *gopop.Session, pwd string) error {
if session.Ctx == nil { if session.Ctx == nil {
tc := &context.Context{} tc := &context.Context{}
@ -114,6 +118,7 @@ func (a action) Pass(session *gopop.Session, pwd string) error {
return errors.New("password error") return errors.New("password error")
} }
// Apop APOP登陆命令
func (a action) Apop(session *gopop.Session, username, digest string) error { func (a action) Apop(session *gopop.Session, username, digest string) error {
if session.Ctx == nil { if session.Ctx == nil {
tc := &context.Context{} tc := &context.Context{}
@ -156,11 +161,12 @@ type statInfo struct {
Size int64 `json:"size"` Size int64 `json:"size"`
} }
// Stat 查询邮件数量
func (a action) Stat(session *gopop.Session) (msgNum, msgSize int64, err error) { func (a action) Stat(session *gopop.Session) (msgNum, msgSize int64, err error) {
log.WithContext(session.Ctx).Debugf("POP3 CMD: STAT") log.WithContext(session.Ctx).Debugf("POP3 CMD: STAT")
var si statInfo 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) { if err != nil && !errors.Is(err, sql.ErrNoRows) {
log.WithContext(session.Ctx.(*context.Context)).Errorf("%+v", err) log.WithContext(session.Ctx.(*context.Context)).Errorf("%+v", err)
err = nil err = nil
@ -213,6 +219,7 @@ type listItem struct {
Size int64 `json:"size"` Size int64 `json:"size"`
} }
// List 邮件列表
func (a action) List(session *gopop.Session, msg string) ([]gopop.MailInfo, error) { func (a action) List(session *gopop.Session, msg string) ([]gopop.MailInfo, error) {
log.WithContext(session.Ctx).Debugf("POP3 CMD: LIST ,Args:%s", msg) log.WithContext(session.Ctx).Debugf("POP3 CMD: LIST ,Args:%s", msg)
var res []listItem var res []listItem
@ -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 =?") 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) err = db.Instance.Select(&res, ssql, listId)
} else { } 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) err = db.Instance.Select(&res, ssql)
} }
@ -249,6 +256,7 @@ func (a action) List(session *gopop.Session, msg string) ([]gopop.MailInfo, erro
return ret, nil return ret, nil
} }
// Retr 获取邮件详情
func (a action) Retr(session *gopop.Session, id int64) (string, int64, error) { func (a action) Retr(session *gopop.Session, id int64) (string, int64, error) {
log.WithContext(session.Ctx).Debugf("POP3 CMD: RETR ,Args:%d", id) log.WithContext(session.Ctx).Debugf("POP3 CMD: RETR ,Args:%d", id)
email, err := detail.GetEmailDetail(session.Ctx.(*context.Context), cast.ToInt(id), false) email, err := detail.GetEmailDetail(session.Ctx.(*context.Context), cast.ToInt(id), false)
@ -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 { func (a action) Delete(session *gopop.Session, id int64) error {
log.WithContext(session.Ctx).Debugf("POP3 CMD: DELE ,Args:%d", id) log.WithContext(session.Ctx).Debugf("POP3 CMD: DELE ,Args:%d", id)

View File

@ -13,7 +13,6 @@ import (
"pmail/dto/parsemail" "pmail/dto/parsemail"
"pmail/hooks" "pmail/hooks"
"pmail/services/rule" "pmail/services/rule"
"pmail/utils/array"
"pmail/utils/async" "pmail/utils/async"
"pmail/utils/context" "pmail/utils/context"
"pmail/utils/send" "pmail/utils/send"
@ -60,9 +59,9 @@ func (s *Session) Data(r io.Reader) error {
} }
} }
// 判断是收信还是转发 // 判断是收信还是转发,只要是登陆了,都当成转发处理
account, domain := email.From.GetDomainAccount() //account, domain := email.From.GetDomainAccount()
if array.InArray(domain, config.Instance.Domains) && s.Ctx.UserName == account { if s.Ctx.UserID > 0 {
// 转发 // 转发
err := saveEmail(ctx, email, 1, true, true) err := saveEmail(ctx, email, 1, true, true)
if err != nil { if err != nil {