1、支持多域名
2、支持PostgreSQL数据库
3、更新全部依赖
4、支持垃圾箱邮件删除

Co-authored-by: jinnrry <i@jinnrry.com>
This commit is contained in:
Jinnrryandjinnrry authored and GitHub committed 2024-07-14 09:42:43 +08:00
1 parent 402e00150d
commit 9ffbdf41c3
34 files changed
+394 -222

No files matched your search

+39 -34
View File
@@ -1,50 +1,55 @@
package del_email
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
"pmail/consts"
"pmail/db"
"pmail/models"
"pmail/utils/context"
"xorm.io/xorm"
)
import . "xorm.io/builder"
func DelEmail(ctx *context.Context, ids []int) error {
if len(ids) == 0 {
return nil
}
where, params, err := ToSQL(Eq{"user_id": ctx.UserID}.And(Eq{"email_id": ids}))
if err != nil {
log.Errorf("del email err: %v", err)
func DelEmail(ctx *context.Context, ids []int64, forcedDel bool) error {
session := db.Instance.NewSession()
defer session.Close()
if err := session.Begin(); err != nil {
return err
}
for _, id := range ids {
err := deleteOne(ctx, session, cast.ToInt64(id), forcedDel)
if err != nil {
session.Rollback()
return err
}
}
return session.Commit()
}
_, err = db.Instance.Table(&models.UserEmail{}).Where(where, params...).Update(map[string]interface{}{"status": consts.EmailStatusDel})
type num struct {
Num int `xorm:"num"`
}
func deleteOne(ctx *context.Context, session *xorm.Session, id int64, forcedDel bool) error {
if !forcedDel {
_, err := session.Table(&models.UserEmail{}).Where("email_id=? and user_id=?", id, ctx.UserID).Update(map[string]interface{}{"status": consts.EmailStatusDel})
return err
}
// 先删除关联关系
var ue models.UserEmail
_, err := session.Table(&models.UserEmail{}).Where("email_id=? and user_id=?", id, ctx.UserID).Delete(&ue)
if err != nil {
log.Errorf("del email err: %v", err)
}
return err
}
func DelEmailI64(ctx *context.Context, ids []int64) error {
if len(ids) == 0 {
return nil
}
where, params, err := ToSQL(Eq{"user_id": ctx.UserID}.And(Eq{"email_id": ids}))
if err != nil {
log.Errorf("del email err: %v", err)
return err
}
_, err = db.Instance.Table(&models.UserEmail{}).Where(where, params...).Update(map[string]interface{}{"status": consts.EmailStatusDel})
if err != nil {
log.Errorf("del email err: %v", err)
return err
}
// 检查email是否还有人有权限
var Num num
_, err = session.Table(&models.UserEmail{}).Select("count(1) as num").Where("email_id=? ", id).Get(&Num)
if err != nil {
return err
}
if Num.Num == 0 {
var email models.Email
_, err = session.Table(&email).Where("id=?", id).Delete(&email)
}
return err
}
+1 -1
View File
@@ -72,7 +72,7 @@ func genSQL(ctx *context.Context, count bool, tagInfo dto.SearchTag, keyword str
sql += " order by e.id desc"
if limit < 10000 {
sql += fmt.Sprintf(" limit %d,%d ", offset, limit)
sql += fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, offset)
}
return sql, sqlParams
-53
View File
@@ -1,54 +1 @@
package list
import (
"pmail/dto"
"pmail/utils/context"
"reflect"
"testing"
)
func Test_genSQL(t *testing.T) {
type args struct {
ctx *context.Context
count bool
tagInfo dto.SearchTag
keyword string
pop3List bool
offset int
limit int
}
tests := []struct {
name string
args args
want string
want1 []any
}{
{
name: "Group搜索",
args: args{
ctx: &context.Context{
UserID: 1,
},
count: false,
tagInfo: dto.SearchTag{-1, -1, 2},
keyword: "",
pop3List: false,
offset: 0,
limit: 0,
},
want: "select e.*,ue.is_read from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? and ue.status != 3 and ue.group_id=? order by e.id desc limit 0,10 ",
want1: []any{1, 2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := genSQL(tt.args.ctx, tt.args.count, tt.args.tagInfo, tt.args.keyword, tt.args.pop3List, tt.args.offset, tt.args.limit)
if got != tt.want {
t.Errorf("genSQL() got = \n%v, want \n%v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("genSQL() got1 = \n%v, want \n%v", got1, tt.want1)
}
})
}
}
+8 -1
View File
@@ -43,7 +43,14 @@ func GetAdminPassword(ctx *context.Context) (string, error) {
func SetAdminPassword(ctx *context.Context, account, pwd string) error {
encodePwd := password.Encode(pwd)
_, err := db.Instance.Exec(db.WithContext(ctx, "INSERT INTO user (account, name, password,is_admin) VALUES (?, 'admin',?,1)"), account, encodePwd)
var user models.User = models.User{
Account: account,
Name: "admin",
Password: encodePwd,
IsAdmin: 1,
}
_, err := db.Instance.Insert(&user)
if err != nil {
return errors.Wrap(err)
}
+13 -8
View File
@@ -17,19 +17,24 @@ type DNSItem struct {
Tips string `json:"tips"`
}
func GetDNSSettings(ctx *context.Context) ([]*DNSItem, error) {
func GetDNSSettings(ctx *context.Context) (map[string][]*DNSItem, error) {
configData, err := ReadConfig()
if err != nil {
return nil, errors.Wrap(err)
}
ret := []*DNSItem{
{Type: "A", Host: "smtp", Value: ip.GetIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
{Type: "A", Host: "pop", Value: ip.GetIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
{Type: "A", Host: "", Value: ip.GetIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
{Type: "MX", Host: "", Value: fmt.Sprintf("smtp.%s", configData.Domain), TTL: 3600},
{Type: "TXT", Host: "", Value: "v=spf1 a mx ~all", TTL: 3600},
{Type: "TXT", Host: "default._domainkey", Value: auth.DkimGen(), TTL: 3600},
ret := make(map[string][]*DNSItem)
for _, domain := range configData.Domains {
ret[domain] = []*DNSItem{
{Type: "A", Host: "smtp", Value: ip.GetIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
{Type: "A", Host: "pop", Value: ip.GetIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
{Type: "A", Host: "@", Value: ip.GetIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
{Type: "MX", Host: "@", Value: fmt.Sprintf("smtp.%s", domain), TTL: 3600},
{Type: "TXT", Host: "@", Value: "v=spf1 a mx ~all", TTL: 3600},
{Type: "TXT", Host: "default._domainkey", Value: auth.DkimGen(), TTL: 3600},
}
}
return ret, nil
}
+17 -4
View File
@@ -1,19 +1,21 @@
package setup
import (
"pmail/utils/array"
"pmail/utils/errors"
"strings"
)
func GetDomainSettings() (string, string, error) {
func GetDomainSettings() (string, string, []string, error) {
configData, err := ReadConfig()
if err != nil {
return "", "", errors.Wrap(err)
return "", "", []string{}, errors.Wrap(err)
}
return configData.Domain, configData.WebDomain, nil
return configData.Domain, configData.WebDomain, array.Difference(configData.Domains, []string{configData.Domain}), nil
}
func SetDomainSettings(smtpDomain, webDomain string) error {
func SetDomainSettings(smtpDomain, webDomain, multiDomains string) error {
configData, err := ReadConfig()
if err != nil {
return errors.Wrap(err)
@@ -27,6 +29,17 @@ func SetDomainSettings(smtpDomain, webDomain string) error {
return errors.New("web domain must not empty!")
}
configData.Domains = []string{}
if multiDomains != "" {
domains := strings.Split(multiDomains, ",")
configData.Domains = domains
}
if !array.InArray(smtpDomain, configData.Domains) {
configData.Domains = append(configData.Domains, smtpDomain)
}
configData.Domain = smtpDomain
configData.WebDomain = webDomain
+14 -2
View File
@@ -102,8 +102,14 @@ func renewCertificate(privateKey *ecdsa.PrivateKey, cfg *config.Config) error {
myUser.Registration = reg
domains := []string{cfg.WebDomain}
for _, domain := range cfg.Domains {
domains = append(domains, "smtp."+domain)
domains = append(domains, "pop."+domain)
}
request := certificate.ObtainRequest{
Domains: []string{"smtp." + cfg.Domain, cfg.WebDomain, "pop." + cfg.Domain},
Domains: domains,
Bundle: true,
}
@@ -175,8 +181,14 @@ func generateCertificate(privateKey *ecdsa.PrivateKey, cfg *config.Config, newAc
myUser.Registration = reg
domains := []string{cfg.WebDomain}
for _, domain := range cfg.Domains {
domains = append(domains, "smtp."+domain)
domains = append(domains, "pop."+domain)
}
request := certificate.ObtainRequest{
Domains: []string{"smtp." + cfg.Domain, cfg.WebDomain, "pop." + cfg.Domain},
Domains: domains,
Bundle: true,
}