1 parent
6697d2b229
commit
5af46b32f6
82 files changed
+4149
-345
No files matched your search
@@ -5,11 +5,13 @@ import (
|
||||
"github.com/Jinnrry/pmail/db"
|
||||
"github.com/Jinnrry/pmail/models"
|
||||
"github.com/Jinnrry/pmail/utils/context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cast"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
import . "xorm.io/builder"
|
||||
|
||||
func DelEmail(ctx *context.Context, ids []int64, forcedDel bool) error {
|
||||
func DelEmail(ctx *context.Context, ids []int, forcedDel bool) error {
|
||||
session := db.Instance.NewSession()
|
||||
defer session.Close()
|
||||
if err := session.Begin(); err != nil {
|
||||
@@ -31,7 +33,10 @@ type num struct {
|
||||
|
||||
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})
|
||||
_, err := session.Table(&models.UserEmail{}).Where("email_id=? and user_id=?", id, ctx.UserID).Update(map[string]interface{}{
|
||||
"status": consts.EmailStatusDel,
|
||||
"group_id": 0,
|
||||
})
|
||||
return err
|
||||
}
|
||||
// 先删除关联关系
|
||||
@@ -53,3 +58,38 @@ func deleteOne(ctx *context.Context, session *xorm.Session, id int64, forcedDel
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func DelByUID(ctx *context.Context, ids []int) error {
|
||||
session := db.Instance.NewSession()
|
||||
defer session.Close()
|
||||
for _, id := range ids {
|
||||
var ue models.UserEmail
|
||||
session.Table("user_email").Where(Eq{"id": ids, "user_id": ctx.UserID}).Get(&ue)
|
||||
if ue.ID == 0 {
|
||||
log.WithContext(ctx).Warn("no user email found")
|
||||
return nil
|
||||
}
|
||||
emailId := ue.EmailID
|
||||
|
||||
// 先删除关联关系
|
||||
_, err := session.Table(&models.UserEmail{}).Where("id=? and user_id=?", id, ctx.UserID).Delete(&ue)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
// 检查email是否还有人有权限
|
||||
var Num num
|
||||
_, err = session.Table(&models.UserEmail{}).Select("count(1) as num").Where("email_id=? ", emailId).Get(&Num)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if Num.Num == 0 {
|
||||
var email models.Email
|
||||
_, err = session.Table(&email).Where("id=?", id).Delete(&email)
|
||||
|
||||
}
|
||||
}
|
||||
session.Commit()
|
||||
return nil
|
||||
}
|
||||
@@ -8,11 +8,14 @@ import (
|
||||
"github.com/Jinnrry/pmail/dto/parsemail"
|
||||
"github.com/Jinnrry/pmail/dto/response"
|
||||
"github.com/Jinnrry/pmail/models"
|
||||
"github.com/Jinnrry/pmail/services/list"
|
||||
"github.com/Jinnrry/pmail/utils/array"
|
||||
"github.com/Jinnrry/pmail/utils/context"
|
||||
"github.com/Jinnrry/pmail/utils/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
)
|
||||
import . "xorm.io/builder"
|
||||
|
||||
func GetEmailDetail(ctx *context.Context, id int, markRead bool) (*response.EmailResponseData, error) {
|
||||
// 先查是否是本人的邮件
|
||||
@@ -56,3 +59,61 @@ func GetEmailDetail(ctx *context.Context, id int, markRead bool) (*response.Emai
|
||||
|
||||
return &email, nil
|
||||
}
|
||||
|
||||
func MakeRead(ctx *context.Context, emailId int, hadRead bool) {
|
||||
ue := models.UserEmail{
|
||||
UserID: ctx.UserID,
|
||||
IsRead: 1,
|
||||
EmailID: emailId,
|
||||
}
|
||||
if !hadRead {
|
||||
ue.IsRead = 0
|
||||
}
|
||||
|
||||
db.Instance.Where("email_id = ? and user_id=?", emailId, ctx.UserID).Cols("is_read").Update(&ue)
|
||||
}
|
||||
|
||||
func FindUE(ctx *context.Context, groupName string, req list.ImapListReq, uid bool) []models.UserEmail {
|
||||
var ue []models.UserEmail
|
||||
if uid {
|
||||
err := db.Instance.Where(Eq{"id": req.UidList}).Find(&ue)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL error:%+v", err)
|
||||
}
|
||||
return ue
|
||||
} else {
|
||||
sql := fmt.Sprintf("SELECT id,email_id, is_read from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and status = ?)) a WHERE serial_number in (%s)",
|
||||
array.Join(req.UidList, ","),
|
||||
)
|
||||
switch groupName {
|
||||
case "INBOX":
|
||||
db.Instance.SQL(sql, ctx.UserID, 0).Find(&ue)
|
||||
case "Sent Messages":
|
||||
db.Instance.SQL(sql, ctx.UserID, 1).Find(&ue)
|
||||
case "Drafts":
|
||||
db.Instance.SQL(sql, ctx.UserID, 4).Find(&ue)
|
||||
case "Deleted Messages":
|
||||
db.Instance.SQL(sql, ctx.UserID, 3).Find(&ue)
|
||||
case "Junk":
|
||||
db.Instance.SQL(sql, ctx.UserID, 5).Find(&ue)
|
||||
default:
|
||||
groupNames := strings.Split(groupName, "/")
|
||||
groupName = groupNames[len(groupNames)-1]
|
||||
|
||||
var group models.Group
|
||||
db.Instance.Table("group").Where("user_id=? and name=?", ctx.UserID, groupName).Get(&group)
|
||||
if group.ID == 0 {
|
||||
return nil
|
||||
}
|
||||
db.Instance.
|
||||
SQL(fmt.Sprintf(
|
||||
"SELECT * from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and group_id = ?)) a WHERE serial_number in (%s)",
|
||||
array.Join(req.UidList, ","),
|
||||
)).
|
||||
Find(&ue, ctx.UserID, group.ID)
|
||||
}
|
||||
|
||||
return ue
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,19 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
errors2 "errors"
|
||||
"fmt"
|
||||
"github.com/Jinnrry/pmail/consts"
|
||||
"github.com/Jinnrry/pmail/db"
|
||||
"github.com/Jinnrry/pmail/dto"
|
||||
"github.com/Jinnrry/pmail/models"
|
||||
"github.com/Jinnrry/pmail/services/del_email"
|
||||
"github.com/Jinnrry/pmail/utils/array"
|
||||
"github.com/Jinnrry/pmail/utils/context"
|
||||
"github.com/Jinnrry/pmail/utils/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
type GroupItem struct {
|
||||
@@ -18,6 +23,50 @@ type GroupItem struct {
|
||||
Children []*GroupItem `json:"children"`
|
||||
}
|
||||
|
||||
func CreateGroup(ctx *context.Context, name string, parentId int) (*models.Group, error) {
|
||||
// 先查询是否存在
|
||||
var group models.Group
|
||||
db.Instance.Table("group").Where("name = ? and user_id = ?", name, ctx.UserID).Get(&group)
|
||||
if group.ID > 0 {
|
||||
return &group, nil
|
||||
}
|
||||
group.Name = name
|
||||
group.ParentId = parentId
|
||||
group.UserId = ctx.UserID
|
||||
group.FullPath = getLayerName(ctx, &group, true)
|
||||
|
||||
_, err := db.Instance.Insert(&group)
|
||||
return &group, err
|
||||
}
|
||||
|
||||
func Rename(ctx *context.Context, oldName, newName string) error {
|
||||
oldGroupInfo, err := GetGroupByName(ctx, oldName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if oldGroupInfo == nil || oldGroupInfo.ID == 0 {
|
||||
return errors2.New("group not found")
|
||||
}
|
||||
oldGroupInfo.Name = newName
|
||||
oldGroupInfo.FullPath = getLayerName(ctx, oldGroupInfo, true)
|
||||
_, err = db.Instance.ID(oldGroupInfo.ID).Update(oldGroupInfo)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetGroupByName(ctx *context.Context, name string) (*models.Group, error) {
|
||||
var group models.Group
|
||||
db.Instance.Table("group").Where("name = ? and user_id = ?", name, ctx.UserID).Get(&group)
|
||||
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
func GetGroupByFullPath(ctx *context.Context, fullPath string) (*models.Group, error) {
|
||||
var group models.Group
|
||||
_, err := db.Instance.Table("group").Where("full_path = ? and user_id = ?", fullPath, ctx.UserID).Get(&group)
|
||||
|
||||
return &group, err
|
||||
}
|
||||
|
||||
func DelGroup(ctx *context.Context, groupId int) (bool, error) {
|
||||
allGroupIds := getAllChildId(ctx, groupId)
|
||||
allGroupIds = append(allGroupIds, groupId)
|
||||
@@ -36,7 +85,7 @@ func DelGroup(ctx *context.Context, groupId int) (bool, error) {
|
||||
return false, errors.Wrap(err)
|
||||
}
|
||||
|
||||
_, err = trans.Exec(db.WithContext(ctx, fmt.Sprintf("update email set group_id=0 where group_id in (%s)", array.Join(allGroupIds, ","))))
|
||||
_, err = trans.Exec(db.WithContext(ctx, fmt.Sprintf("update user_email set group_id=0 where group_id in (%s)", array.Join(allGroupIds, ","))))
|
||||
if err != nil {
|
||||
trans.Rollback()
|
||||
return false, errors.Wrap(err)
|
||||
@@ -72,7 +121,9 @@ func GetGroupInfoList(ctx *context.Context) []*GroupItem {
|
||||
|
||||
// MoveMailToGroup 将某封邮件移动到某个分组中
|
||||
func MoveMailToGroup(ctx *context.Context, mailId []int, groupId int) bool {
|
||||
res, err := db.Instance.Exec(db.WithContext(ctx, fmt.Sprintf("update email set group_id=? where id in (%s)", array.Join(mailId, ","))), groupId)
|
||||
res, err := db.Instance.Exec(db.WithContext(ctx,
|
||||
fmt.Sprintf("update user_email set group_id=? where email_id in (%s) and user_id =?", array.Join(mailId, ","))),
|
||||
groupId, ctx.UserID)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL Error:%+v", err)
|
||||
return false
|
||||
@@ -113,3 +164,182 @@ func GetGroupList(ctx *context.Context) []*models.Group {
|
||||
db.Instance.Table("group").Where("user_id=?", ctx.UserID).Find(&ret)
|
||||
return ret
|
||||
}
|
||||
|
||||
func hasChildren(ctx *context.Context, id int) bool {
|
||||
var parent []*models.Group
|
||||
db.Instance.Table("group").Where("parent_id=?", id).Find(&parent)
|
||||
return len(parent) > 0
|
||||
}
|
||||
|
||||
func getLayerName(ctx *context.Context, item *models.Group, allPath bool) string {
|
||||
if item.ParentId == 0 {
|
||||
return item.Name
|
||||
}
|
||||
var parent models.Group
|
||||
_, _ = db.Instance.Table("group").Where("id=?", item.ParentId).Get(&parent)
|
||||
if allPath {
|
||||
return getLayerName(ctx, &parent, allPath) + "/" + item.Name
|
||||
}
|
||||
return getLayerName(ctx, &parent, allPath)
|
||||
}
|
||||
|
||||
func IsDefaultBox(box string) bool {
|
||||
return array.InArray(box, []string{"INBOX", "Sent Messages", "Drafts", "Deleted Messages", "Junk"})
|
||||
}
|
||||
|
||||
func GetGroupStatus(ctx *context.Context, groupName string, params []string) (string, map[string]int) {
|
||||
retMap := map[string]int{}
|
||||
|
||||
if !IsDefaultBox(groupName) {
|
||||
groupNames := strings.Split(groupName, "/")
|
||||
groupName = groupNames[len(groupNames)-1]
|
||||
|
||||
var group models.Group
|
||||
db.Instance.Table("group").Where("user_id=? and name=?", ctx.UserID, groupName).Get(&group)
|
||||
if group.ID == 0 {
|
||||
ret := ""
|
||||
for _, param := range params {
|
||||
if ret != "" {
|
||||
ret += " "
|
||||
}
|
||||
retMap[param] = 0
|
||||
ret += fmt.Sprintf("%s %d", param, 0)
|
||||
}
|
||||
return fmt.Sprintf("(%s)", ret), retMap
|
||||
}
|
||||
ret := ""
|
||||
for _, param := range params {
|
||||
if ret != "" {
|
||||
ret += " "
|
||||
}
|
||||
var value int
|
||||
|
||||
switch param {
|
||||
case "MESSAGES":
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("group_id=?", group.ID).Get(&value)
|
||||
case "UIDNEXT":
|
||||
db.Instance.Table("email").Select("count(1)").Get(&value)
|
||||
case "UIDVALIDITY":
|
||||
value = group.ID
|
||||
case "UNSEEN":
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("group_id=? and is_read=0", group.ID).Get(&value)
|
||||
}
|
||||
retMap[param] = value
|
||||
ret += fmt.Sprintf("%s %d", param, value)
|
||||
}
|
||||
return fmt.Sprintf("(%s)", ret), retMap
|
||||
}
|
||||
|
||||
ret := ""
|
||||
for _, param := range params {
|
||||
if ret != "" {
|
||||
ret += " "
|
||||
}
|
||||
var value int
|
||||
|
||||
switch param {
|
||||
case "MESSAGES":
|
||||
value = getGroupNum(ctx, groupName, false)
|
||||
case "UIDNEXT":
|
||||
db.Instance.Table("email").Select("count(1)").Get(&value)
|
||||
case "UIDVALIDITY":
|
||||
value = models.GroupNameToCode[groupName]
|
||||
case "UNSEEN":
|
||||
value = getGroupNum(ctx, groupName, true)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
retMap[param] = value
|
||||
ret += fmt.Sprintf("%s %d", param, value)
|
||||
}
|
||||
if ret == "" {
|
||||
return "", retMap
|
||||
}
|
||||
|
||||
return fmt.Sprintf("(%s)", ret), retMap
|
||||
|
||||
}
|
||||
|
||||
func getGroupNum(ctx *context.Context, groupName string, mustUnread bool) int {
|
||||
var count int
|
||||
switch groupName {
|
||||
case "INBOX":
|
||||
if mustUnread {
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("user_id=? and status=0 and is_read=0", ctx.UserID).Get(&count)
|
||||
} else {
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("user_id=? and status=0", ctx.UserID).Get(&count)
|
||||
}
|
||||
case "Sent Messages":
|
||||
if mustUnread {
|
||||
count = 0
|
||||
} else {
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("user_id=? and status=1", ctx.UserID).Get(&count)
|
||||
}
|
||||
case "Drafts":
|
||||
if mustUnread {
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("user_id=? and status=4 and is_read=0", ctx.UserID).Get(&count)
|
||||
} else {
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("user_id=? and status=4", ctx.UserID).Get(&count)
|
||||
}
|
||||
case "Deleted Messages":
|
||||
if mustUnread {
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("user_id=? and status=3 and is_read=0", ctx.UserID).Get(&count)
|
||||
} else {
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("user_id=? and status=3", ctx.UserID).Get(&count)
|
||||
}
|
||||
case "Junk":
|
||||
if mustUnread {
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("user_id=? and status=5 and is_read=0", ctx.UserID).Get(&count)
|
||||
} else {
|
||||
db.Instance.Table("user_email").Select("count(1)").Where("user_id=? and status=5", ctx.UserID).Get(&count)
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func Move2DefaultBox(ctx *context.Context, mailIds []int, groupName string) error {
|
||||
switch groupName {
|
||||
case "Deleted Messages":
|
||||
err := del_email.DelEmail(ctx, mailIds, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case "INBOX":
|
||||
_, err := db.Instance.Table(&models.UserEmail{}).Where(builder.Eq{
|
||||
"user_id": ctx.UserID,
|
||||
"email_id": mailIds,
|
||||
}).Update(map[string]interface{}{
|
||||
"status": consts.EmailTypeReceive,
|
||||
"group_id": 0,
|
||||
})
|
||||
return err
|
||||
case "Sent Messages":
|
||||
_, err := db.Instance.Table(&models.UserEmail{}).Where(builder.Eq{
|
||||
"user_id": ctx.UserID,
|
||||
"email_id": mailIds,
|
||||
}).Update(map[string]interface{}{
|
||||
"status": consts.EmailStatusSent,
|
||||
"group_id": 0,
|
||||
})
|
||||
return err
|
||||
case "Drafts":
|
||||
_, err := db.Instance.Table(&models.UserEmail{}).Where(builder.Eq{
|
||||
"user_id": ctx.UserID,
|
||||
"email_id": mailIds,
|
||||
}).Update(map[string]interface{}{
|
||||
"status": consts.EmailStatusDrafts,
|
||||
"group_id": 0,
|
||||
})
|
||||
return err
|
||||
case "Junk":
|
||||
_, err := db.Instance.Table(&models.UserEmail{}).Where(builder.Eq{
|
||||
"user_id": ctx.UserID,
|
||||
"email_id": mailIds,
|
||||
}).Update(map[string]interface{}{
|
||||
"status": consts.EmailStatusJunk,
|
||||
"group_id": 0,
|
||||
})
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Jinnrry/pmail/config"
|
||||
"github.com/Jinnrry/pmail/db"
|
||||
"github.com/Jinnrry/pmail/utils/context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetGroupStatus(t *testing.T) {
|
||||
config.Init()
|
||||
db.Init("")
|
||||
db.Instance.ShowSQL(true)
|
||||
ctx := &context.Context{
|
||||
UserID: 1,
|
||||
UserName: "admin",
|
||||
UserAccount: "admin",
|
||||
}
|
||||
ret, _ := GetGroupStatus(ctx, "INBOX", []string{"MESSAGES", "UIDNEXT", "UIDVALIDITY", "UNSEEN"})
|
||||
fmt.Println(ret)
|
||||
}
|
||||
@@ -5,9 +5,13 @@ import (
|
||||
"github.com/Jinnrry/pmail/db"
|
||||
"github.com/Jinnrry/pmail/dto"
|
||||
"github.com/Jinnrry/pmail/dto/response"
|
||||
"github.com/Jinnrry/pmail/models"
|
||||
"github.com/Jinnrry/pmail/utils/array"
|
||||
"github.com/Jinnrry/pmail/utils/context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
)
|
||||
import . "xorm.io/builder"
|
||||
|
||||
func GetEmailList(ctx *context.Context, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (emailList []*response.EmailResponseData, total int64) {
|
||||
return getList(ctx, tagInfo, keyword, pop3List, offset, limit)
|
||||
@@ -46,7 +50,7 @@ func genSQL(ctx *context.Context, count bool, tagInfo dto.SearchTag, keyword str
|
||||
if tagInfo.Status != -1 {
|
||||
sql += " and ue.status =? "
|
||||
sqlParams = append(sqlParams, tagInfo.Status)
|
||||
} else {
|
||||
} else if tagInfo.GroupId == -1 {
|
||||
sql += " and ue.status != 3"
|
||||
}
|
||||
|
||||
@@ -94,3 +98,157 @@ func Stat(ctx *context.Context) (int64, int64) {
|
||||
}
|
||||
return ret.Total, ret.Size
|
||||
}
|
||||
|
||||
type ImapListReq struct {
|
||||
UidList []int
|
||||
Star int
|
||||
End int
|
||||
}
|
||||
|
||||
func GetUEListByUID(ctx *context.Context, groupName string, star, end int, uidList []int) []*response.UserEmailUIDData {
|
||||
var ue []*response.UserEmailUIDData
|
||||
sql := "SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE user_id = ? "
|
||||
|
||||
params := []any{ctx.UserID}
|
||||
|
||||
if len(uidList) > 0 {
|
||||
sql += fmt.Sprintf(" and id in (%s)", array.Join(uidList, ","))
|
||||
}
|
||||
if star > 0 {
|
||||
sql += " and id >=?"
|
||||
params = append(params, star)
|
||||
}
|
||||
if end > 0 {
|
||||
sql += " and id <=?"
|
||||
params = append(params, end)
|
||||
}
|
||||
|
||||
switch groupName {
|
||||
case "INBOX":
|
||||
sql += " and status =?"
|
||||
params = append(params, 0)
|
||||
case "Sent Messages":
|
||||
sql += " and status =?"
|
||||
params = append(params, 1)
|
||||
case "Drafts":
|
||||
sql += " and status =?"
|
||||
params = append(params, 4)
|
||||
case "Deleted Messages":
|
||||
sql += " and status =?"
|
||||
params = append(params, 3)
|
||||
case "Junk":
|
||||
sql += " and status =?"
|
||||
params = append(params, 5)
|
||||
default:
|
||||
groupNames := strings.Split(groupName, "/")
|
||||
groupName = groupNames[len(groupNames)-1]
|
||||
|
||||
var group models.Group
|
||||
db.Instance.Table("group").Where("user_id=? and name=?", ctx.UserID, groupName).Get(&group)
|
||||
if group.ID == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
sql += " and group_id = ?"
|
||||
params = append(params, group.ID)
|
||||
}
|
||||
|
||||
db.Instance.SQL(sql, params...).Find(&ue)
|
||||
return ue
|
||||
}
|
||||
|
||||
func getEmailListByUidList(ctx *context.Context, groupName string, req ImapListReq, uid bool) []*response.EmailResponseData {
|
||||
var ret []*response.EmailResponseData
|
||||
var ue []*response.UserEmailUIDData
|
||||
sql := fmt.Sprintf("SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and id in (%s))", array.Join(req.UidList, ","))
|
||||
if req.Star > 0 && req.End != 0 {
|
||||
sql = fmt.Sprintf("SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and id >=%d and id <= %d)", req.Star, req.End)
|
||||
}
|
||||
if req.Star > 0 && req.End == 0 {
|
||||
sql = fmt.Sprintf("SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and id >=%d )", req.Star)
|
||||
}
|
||||
|
||||
err := db.Instance.SQL(sql, ctx.UserID).Find(&ue)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", sql, err)
|
||||
}
|
||||
ueMap := map[int]*response.UserEmailUIDData{}
|
||||
var emailIds []int
|
||||
for _, email := range ue {
|
||||
ueMap[email.EmailID] = email
|
||||
emailIds = append(emailIds, email.EmailID)
|
||||
}
|
||||
|
||||
_ = db.Instance.Table("email").Select("*").Where(Eq{"id": emailIds}).Find(&ret)
|
||||
for i, data := range ret {
|
||||
ret[i].IsRead = ueMap[data.Id].IsRead
|
||||
ret[i].SerialNumber = ueMap[data.Id].SerialNumber
|
||||
ret[i].UeId = ueMap[data.Id].ID
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func GetEmailListByGroup(ctx *context.Context, groupName string, req ImapListReq, uid bool) []*response.EmailResponseData {
|
||||
if len(req.UidList) == 0 && req.Star == 0 && req.End == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if uid {
|
||||
return getEmailListByUidList(ctx, groupName, req, uid)
|
||||
}
|
||||
|
||||
var ret []*response.EmailResponseData
|
||||
var ue []*response.UserEmailUIDData
|
||||
|
||||
sql := fmt.Sprintf("SELECT * from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and status = ? and group_id=0 )) a WHERE serial_number in (%s)", array.Join(req.UidList, ","))
|
||||
if req.Star > 0 && req.End == 0 {
|
||||
sql = fmt.Sprintf("SELECT * from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and status = ? and group_id=0 )) a WHERE serial_number >= %d", req.Star)
|
||||
}
|
||||
if req.Star > 0 && req.End > 0 {
|
||||
sql = fmt.Sprintf("SELECT * from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and status = ? and group_id=0 )) a WHERE serial_number >= %d and serial_number <=%d", req.Star, req.End)
|
||||
}
|
||||
|
||||
switch groupName {
|
||||
case "INBOX":
|
||||
db.Instance.SQL(sql, ctx.UserID, 0).Find(&ue)
|
||||
case "Sent Messages":
|
||||
db.Instance.SQL(sql, ctx.UserID, 1).Find(&ue)
|
||||
case "Drafts":
|
||||
db.Instance.SQL(sql, ctx.UserID, 4).Find(&ue)
|
||||
case "Deleted Messages":
|
||||
db.Instance.SQL(sql, ctx.UserID, 3).Find(&ue)
|
||||
case "Junk":
|
||||
db.Instance.SQL(sql, ctx.UserID, 5).Find(&ue)
|
||||
default:
|
||||
groupNames := strings.Split(groupName, "/")
|
||||
groupName = groupNames[len(groupNames)-1]
|
||||
|
||||
var group models.Group
|
||||
db.Instance.Table("group").Where("user_id=? and name=?", ctx.UserID, groupName).Get(&group)
|
||||
if group.ID == 0 {
|
||||
return ret
|
||||
}
|
||||
db.Instance.
|
||||
SQL(fmt.Sprintf(
|
||||
"SELECT * from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and group_id = ?)) a WHERE serial_number in (%s)",
|
||||
array.Join(req.UidList, ","))).
|
||||
Find(&ue, ctx.UserID, group.ID)
|
||||
}
|
||||
|
||||
ueMap := map[int]*response.UserEmailUIDData{}
|
||||
var emailIds []int
|
||||
for _, email := range ue {
|
||||
ueMap[email.EmailID] = email
|
||||
emailIds = append(emailIds, email.EmailID)
|
||||
}
|
||||
|
||||
_ = db.Instance.Table("email").Select("*").Where(Eq{"id": emailIds}).Find(&ret)
|
||||
for i, data := range ret {
|
||||
ret[i].IsRead = ueMap[data.Id].IsRead
|
||||
ret[i].SerialNumber = ueMap[data.Id].SerialNumber
|
||||
ret[i].UeId = ueMap[data.Id].ID
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
@@ -1,26 +1,23 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/Jinnrry/pmail/config"
|
||||
"github.com/Jinnrry/pmail/db"
|
||||
"github.com/Jinnrry/pmail/models"
|
||||
"github.com/Jinnrry/pmail/utils/array"
|
||||
"github.com/Jinnrry/pmail/utils/context"
|
||||
"github.com/Jinnrry/pmail/utils/errors"
|
||||
"github.com/Jinnrry/pmail/utils/file"
|
||||
"github.com/Jinnrry/pmail/utils/password"
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetDatabaseSettings(ctx *context.Context) (string, string, error) {
|
||||
configData, err := ReadConfig()
|
||||
configData, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err)
|
||||
}
|
||||
|
||||
if configData.DbType == "" && configData.DbDSN == "" {
|
||||
return config.DBTypeSQLite, "./config/pmail.db", nil
|
||||
return config.DBTypeSQLite, config.ROOT_PATH + "./config/pmail.db", nil
|
||||
}
|
||||
|
||||
return configData.DbType, configData.DbDSN, nil
|
||||
@@ -59,7 +56,7 @@ func SetAdminPassword(ctx *context.Context, account, pwd string) error {
|
||||
}
|
||||
|
||||
func SetDatabaseSettings(ctx *context.Context, dbType, dbDSN string) error {
|
||||
configData, err := ReadConfig()
|
||||
configData, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
@@ -75,7 +72,7 @@ func SetDatabaseSettings(ctx *context.Context, dbType, dbDSN string) error {
|
||||
configData.DbType = dbType
|
||||
configData.DbDSN = dbDSN
|
||||
|
||||
err = WriteConfig(configData)
|
||||
err = config.WriteConfig(configData)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
@@ -87,38 +84,3 @@ func SetDatabaseSettings(ctx *context.Context, dbType, dbDSN string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteConfig(cfg *config.Config) error {
|
||||
bytes, _ := json.Marshal(cfg)
|
||||
err := os.WriteFile("./config/config.json", bytes, 0666)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReadConfig() (*config.Config, error) {
|
||||
configData := config.Config{
|
||||
DkimPrivateKeyPath: "config/dkim/dkim.priv",
|
||||
SSLPrivateKeyPath: "config/ssl/private.key",
|
||||
SSLPublicKeyPath: "config/ssl/public.crt",
|
||||
}
|
||||
if !file.PathExist("./config/config.json") {
|
||||
bytes, _ := json.Marshal(configData)
|
||||
err := os.WriteFile("./config/config.json", bytes, 0666)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
} else {
|
||||
cfgData, err := os.ReadFile("./config/config.json")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(cfgData, &configData)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
}
|
||||
return &configData, nil
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package setup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Jinnrry/pmail/config"
|
||||
"strings"
|
||||
|
||||
"github.com/Jinnrry/pmail/i18n"
|
||||
@@ -20,7 +21,7 @@ type DNSItem struct {
|
||||
}
|
||||
|
||||
func GetDNSSettings(ctx *context.Context) (map[string][]*DNSItem, error) {
|
||||
configData, err := ReadConfig()
|
||||
configData, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
@@ -31,6 +32,7 @@ func GetDNSSettings(ctx *context.Context) (map[string][]*DNSItem, error) {
|
||||
ret[domain] = []*DNSItem{
|
||||
{Type: "A", Host: strings.ReplaceAll(configData.WebDomain, "."+configData.Domain, ""), Value: ip.GetIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
|
||||
{Type: "A", Host: "smtp", Value: ip.GetIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
|
||||
{Type: "A", Host: "imap", 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},
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"github.com/Jinnrry/pmail/config"
|
||||
"github.com/Jinnrry/pmail/utils/array"
|
||||
"github.com/Jinnrry/pmail/utils/errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetDomainSettings() (string, string, []string, error) {
|
||||
configData, err := ReadConfig()
|
||||
configData, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
return "", "", []string{}, errors.Wrap(err)
|
||||
}
|
||||
@@ -16,7 +17,7 @@ func GetDomainSettings() (string, string, []string, error) {
|
||||
}
|
||||
|
||||
func SetDomainSettings(smtpDomain, webDomain, multiDomains string) error {
|
||||
configData, err := ReadConfig()
|
||||
configData, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
@@ -45,7 +46,7 @@ func SetDomainSettings(smtpDomain, webDomain, multiDomains string) error {
|
||||
|
||||
// 检查域名是否指向本机 todo
|
||||
|
||||
err = WriteConfig(configData)
|
||||
err = config.WriteConfig(configData)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"github.com/Jinnrry/pmail/config"
|
||||
"github.com/Jinnrry/pmail/signal"
|
||||
"github.com/Jinnrry/pmail/utils/errors"
|
||||
)
|
||||
|
||||
// Finish 标记初始化完成
|
||||
func Finish() error {
|
||||
cfg, err := ReadConfig()
|
||||
cfg, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
cfg.IsInit = true
|
||||
|
||||
err = WriteConfig(cfg)
|
||||
err = config.WriteConfig(cfg)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package ssl
|
||||
|
||||
//
|
||||
//import (
|
||||
// "reflect"
|
||||
// "testing"
|
||||
//)
|
||||
//
|
||||
//func TestGetServerParamsList(t *testing.T) {
|
||||
// type args struct {
|
||||
// serverName string
|
||||
// }
|
||||
// tests := []struct {
|
||||
// name string
|
||||
// args args
|
||||
// want []string
|
||||
// wantErr bool
|
||||
// }{
|
||||
// {name: "namesilo", args: args{serverName: "namesilo"}, want: []string{"NAMESILO_API_KEY"}, wantErr: false},
|
||||
// {name: "namesiloAgain", args: args{serverName: "namesilo"}, want: []string{"NAMESILO_API_KEY"}, wantErr: false},
|
||||
// {name: "auroradns", args: args{serverName: "auroradns"}, want: []string{"AURORA_API_KEY", "AURORA_SECRET"}, wantErr: false},
|
||||
// {name: "alidns", args: args{serverName: "alidns"}, want: []string{"ALICLOUD_ACCESS_KEY", "ALICLOUD_SECRET_KEY"}, wantErr: false},
|
||||
// {name: "null", args: args{serverName: "null"}, want: nil, wantErr: true},
|
||||
// }
|
||||
// for _, tt := range tests {
|
||||
// t.Run(tt.name, func(t *testing.T) {
|
||||
// got, err := GetServerParamsList(tt.args.serverName)
|
||||
// if (err != nil) != tt.wantErr {
|
||||
// t.Errorf("GetServerParamsList() error = %v, wantErr %v", err, tt.wantErr)
|
||||
// return
|
||||
// }
|
||||
// if !reflect.DeepEqual(got, tt.want) {
|
||||
// t.Errorf("GetServerParamsList() got = %v, want %v", got, tt.want)
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
//}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cast"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-acme/lego/v4/certcrypto"
|
||||
@@ -39,7 +40,7 @@ func (u *MyUser) GetPrivateKey() crypto.PrivateKey {
|
||||
}
|
||||
|
||||
func GetSSL() string {
|
||||
cfg, err := setup.ReadConfig()
|
||||
cfg, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -51,7 +52,7 @@ func GetSSL() string {
|
||||
}
|
||||
|
||||
func SetSSL(sslType, priKey, crtKey string) error {
|
||||
cfg, err := setup.ReadConfig()
|
||||
cfg, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -68,7 +69,7 @@ func SetSSL(sslType, priKey, crtKey string) error {
|
||||
cfg.HttpsEnabled = 2
|
||||
}
|
||||
|
||||
err = setup.WriteConfig(cfg)
|
||||
err = config.WriteConfig(cfg)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
@@ -124,6 +125,7 @@ func renewCertificate(privateKey *ecdsa.PrivateKey, cfg *config.Config) error {
|
||||
for _, domain := range cfg.Domains {
|
||||
domains = append(domains, "smtp."+domain)
|
||||
domains = append(domains, "pop."+domain)
|
||||
domains = append(domains, "imap."+domain)
|
||||
}
|
||||
|
||||
request := certificate.ObtainRequest{
|
||||
@@ -203,6 +205,7 @@ func generateCertificate(privateKey *ecdsa.PrivateKey, cfg *config.Config, newAc
|
||||
for _, domain := range cfg.Domains {
|
||||
domains = append(domains, "smtp."+domain)
|
||||
domains = append(domains, "pop."+domain)
|
||||
domains = append(domains, "imap."+domain)
|
||||
}
|
||||
|
||||
request := certificate.ObtainRequest{
|
||||
@@ -243,7 +246,7 @@ func generateCertificate(privateKey *ecdsa.PrivateKey, cfg *config.Config, newAc
|
||||
|
||||
func GenSSL(update bool) error {
|
||||
|
||||
cfg, err := setup.ReadConfig()
|
||||
cfg, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -267,38 +270,47 @@ func GenSSL(update bool) error {
|
||||
}
|
||||
|
||||
// CheckSSLCrtInfo 返回证书过期剩余天数
|
||||
func CheckSSLCrtInfo() (int, time.Time, error) {
|
||||
func CheckSSLCrtInfo() (int, time.Time, bool, error) {
|
||||
|
||||
cfg, err := setup.ReadConfig()
|
||||
cfg, err := config.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// load cert and key by tls.LoadX509KeyPair
|
||||
tlsCert, err := tls.LoadX509KeyPair(cfg.SSLPublicKeyPath, cfg.SSLPrivateKeyPath)
|
||||
if err != nil {
|
||||
return -1, time.Now(), errors.Wrap(err)
|
||||
return -1, time.Now(), true, errors.Wrap(err)
|
||||
}
|
||||
|
||||
cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
|
||||
|
||||
if err != nil {
|
||||
return -1, time.Now(), errors.Wrap(err)
|
||||
return -1, time.Now(), true, errors.Wrap(err)
|
||||
}
|
||||
|
||||
nameMatchFail := true
|
||||
for _, name := range cert.DNSNames {
|
||||
if strings.Contains("imap", name) {
|
||||
nameMatchFail = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 检查过期时间
|
||||
hours := cert.NotAfter.Sub(time.Now()).Hours()
|
||||
|
||||
if hours <= 0 {
|
||||
return -1, time.Now(), errors.New("Certificate has expired")
|
||||
return -1, time.Now(), nameMatchFail, errors.New("Certificate has expired")
|
||||
}
|
||||
|
||||
return cast.ToInt(hours / 24), cert.NotAfter, nil
|
||||
return cast.ToInt(hours / 24), cert.NotAfter, nameMatchFail, nil
|
||||
}
|
||||
|
||||
func Update(needRestart bool) {
|
||||
if config.Instance != nil && config.Instance.IsInit && (config.Instance.SSLType == config.SSLTypeAutoHTTP || config.Instance.SSLType == config.SSLTypeAutoDNS) {
|
||||
days, _, err := CheckSSLCrtInfo()
|
||||
if days < 30 || err != nil {
|
||||
days, _, nameMatchFail, err := CheckSSLCrtInfo()
|
||||
|
||||
if days < 30 || err != nil || nameMatchFail {
|
||||
if err != nil {
|
||||
log.Errorf("SSL Check Error, Update SSL Certificate. Error Info :%+v", err)
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package ssl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Jinnrry/pmail/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheckSSLCrtInfo(t *testing.T) {
|
||||
config.Init()
|
||||
|
||||
got, got1, _, err := CheckSSLCrtInfo()
|
||||
|
||||
fmt.Println(got, got1, err)
|
||||
}
|
||||
Reference in new issue
Block a user