This commit is contained in:
木木的木头 authored and jinnrry committed 2023-09-08 02:11:00 +08:00
1 parent 5603f26ed6
commit f4689d073c
72 files changed
+1914 -2860

No files matched your search

+3 -3
View File
@@ -4,13 +4,13 @@ import (
"fmt"
"github.com/spf13/cast"
"net/http"
"pmail/dto"
"pmail/dto/response"
"pmail/services/attachments"
"pmail/utils/context"
"strings"
)
func GetAttachments(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func GetAttachments(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
urlInfos := strings.Split(req.RequestURI, "/")
if len(urlInfos) != 4 {
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
@@ -29,7 +29,7 @@ func GetAttachments(ctx *dto.Context, w http.ResponseWriter, req *http.Request)
w.Write(content)
}
func Download(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func Download(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
urlInfos := strings.Split(req.RequestURI, "/")
if len(urlInfos) != 5 {
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
+2 -2
View File
@@ -2,7 +2,7 @@ package controllers
import (
"net/http"
"pmail/dto"
"pmail/utils/context"
)
type HandlerFunc func(*dto.Context, http.ResponseWriter, *http.Request)
type HandlerFunc func(*context.Context, http.ResponseWriter, *http.Request)
+2 -2
View File
@@ -5,16 +5,16 @@ import (
log "github.com/sirupsen/logrus"
"io"
"net/http"
"pmail/dto"
"pmail/dto/response"
"pmail/services/del_email"
"pmail/utils/context"
)
type emailDeleteRequest struct {
IDs []int `json:"ids"`
}
func EmailDelete(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func EmailDelete(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
log.WithContext(ctx).Errorf("%+v", err)
+2 -2
View File
@@ -5,17 +5,17 @@ import (
log "github.com/sirupsen/logrus"
"io"
"net/http"
"pmail/dto"
"pmail/dto/response"
"pmail/services/auth"
"pmail/services/detail"
"pmail/utils/context"
)
type emailDetailRequest struct {
ID int `json:"id"`
}
func EmailDetail(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func EmailDetail(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
log.WithContext(ctx).Errorf("%+v", err)
+16 -14
View File
@@ -7,9 +7,9 @@ import (
"io"
"math"
"net/http"
"pmail/dto"
"pmail/dto/response"
"pmail/services/list"
"pmail/utils/context"
)
type emailListResponse struct {
@@ -19,12 +19,13 @@ type emailListResponse struct {
}
type emilItem struct {
ID int `json:"id"`
Title string `json:"title"`
Desc string `json:"desc"`
Datetime string `json:"datetime"`
IsRead bool `json:"is_read"`
Sender User `json:"sender"`
ID int `json:"id"`
Title string `json:"title"`
Desc string `json:"desc"`
Datetime string `json:"datetime"`
IsRead bool `json:"is_read"`
Sender User `json:"sender"`
Dangerous bool `json:"dangerous"`
}
type User struct {
@@ -39,7 +40,7 @@ type emailRequest struct {
PageSize int `json:"page_size"`
}
func EmailList(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func EmailList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
var lst []*emilItem
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
@@ -67,12 +68,13 @@ func EmailList(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
_ = json.Unmarshal([]byte(email.Sender), &sender)
lst = append(lst, &emilItem{
ID: email.Id,
Title: email.Subject,
Desc: email.Text.String,
Datetime: email.SendDate.Format("2006-01-02 15:04:05"),
IsRead: email.IsRead == 1,
Sender: sender,
ID: email.Id,
Title: email.Subject,
Desc: email.Text.String,
Datetime: email.SendDate.Format("2006-01-02 15:04:05"),
IsRead: email.IsRead == 1,
Sender: sender,
Dangerous: email.SPFCheck == 0 && email.DKIMCheck == 0,
})
}
+2 -2
View File
@@ -5,9 +5,9 @@ import (
log "github.com/sirupsen/logrus"
"io"
"net/http"
"pmail/dto"
"pmail/dto/response"
"pmail/services/group"
"pmail/utils/context"
)
type moveRequest struct {
@@ -15,7 +15,7 @@ type moveRequest struct {
IDs []int `json:"ids"`
}
func Move(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func Move(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
log.WithContext(ctx).Errorf("%+v", err)
+2 -2
View File
@@ -5,16 +5,16 @@ import (
log "github.com/sirupsen/logrus"
"io"
"net/http"
"pmail/dto"
"pmail/dto/response"
"pmail/services/detail"
"pmail/utils/context"
)
type markReadRequest struct {
IDs []int `json:"ids"`
}
func MarkRead(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func MarkRead(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
log.WithContext(ctx).Errorf("%+v", err)
+5 -5
View File
@@ -8,13 +8,13 @@ import (
"net/http"
"pmail/config"
"pmail/db"
"pmail/dto"
"pmail/dto/parsemail"
"pmail/dto/response"
"pmail/hooks"
"pmail/i18n"
"pmail/smtp_server"
"pmail/utils/async"
"pmail/utils/context"
"pmail/utils/send"
"strings"
"time"
)
@@ -43,7 +43,7 @@ type attachment struct {
Data string `json:"data"`
}
func Send(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func Send(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
log.WithContext(ctx).Errorf("%+v", err)
@@ -157,7 +157,7 @@ func Send(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
1,
1,
time.Now(),
ctx.UserInfo.ID,
ctx.UserID,
"",
)
emailId, _ := sqlRes.LastInsertId()
@@ -170,7 +170,7 @@ func Send(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
async.New(ctx).Process(func(p any) {
errMsg := ""
err, sendErr := smtp_server.Send(ctx, e)
err, sendErr := send.Send(ctx, e)
as2 := async.New(ctx)
for _, hook := range hooks.HookList {
+6 -5
View File
@@ -11,14 +11,15 @@ import (
"pmail/i18n"
"pmail/services/group"
"pmail/utils/array"
"pmail/utils/context"
)
func GetUserGroupList(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func GetUserGroupList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
infos := group.GetGroupList(ctx)
response.NewSuccessResponse(infos).FPrint(w)
}
func GetUserGroup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func GetUserGroup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
retData := []*group.GroupItem{
{
@@ -50,7 +51,7 @@ type addGroupRequest struct {
ParentId int `json:"parent_id"`
}
func AddGroup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func AddGroup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
var reqData *addGroupRequest
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
@@ -61,7 +62,7 @@ func AddGroup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
log.WithContext(ctx).Errorf("%+v", err)
}
res, err := db.Instance.Exec(db.WithContext(ctx, "insert into `group` (name,parent_id,user_id) values (?,?,?)"), reqData.Name, reqData.ParentId, ctx.UserInfo.ID)
res, err := db.Instance.Exec(db.WithContext(ctx, "insert into `group` (name,parent_id,user_id) values (?,?,?)"), reqData.Name, reqData.ParentId, ctx.UserID)
if err != nil {
response.NewErrorResponse(response.ServerError, "DBError", err.Error()).FPrint(w)
return
@@ -78,7 +79,7 @@ type delGroupRequest struct {
Id int `json:"id"`
}
func DelGroup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func DelGroup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
var reqData *delGroupRequest
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
+2 -2
View File
@@ -7,11 +7,11 @@ import (
"io"
"net/http"
"pmail/db"
"pmail/dto"
"pmail/dto/response"
"pmail/i18n"
"pmail/models"
"pmail/session"
"pmail/utils/context"
"pmail/utils/password"
)
@@ -20,7 +20,7 @@ type loginRequest struct {
Password string `json:"password"`
}
func Login(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func Login(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
+2 -2
View File
@@ -3,11 +3,11 @@ package controllers
import (
log "github.com/sirupsen/logrus"
"net/http"
"pmail/dto"
"pmail/dto/response"
"pmail/utils/context"
)
func Ping(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func Ping(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
response.NewSuccessResponse("pong").FPrint(w)
log.WithContext(ctx).Info("pong")
}
+89
View File
@@ -0,0 +1,89 @@
package controllers
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"pmail/db"
"pmail/dto"
"pmail/dto/response"
"pmail/i18n"
"pmail/services/rule"
"pmail/utils/address"
"pmail/utils/array"
"pmail/utils/context"
)
func GetRule(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
res := rule.GetAllRules(ctx)
response.NewSuccessResponse(res).FPrint(w)
}
func UpsertRule(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
requestBody, err := io.ReadAll(req.Body)
if err != nil {
log.WithContext(ctx).Errorf("ReadError:%v", err)
return
}
var data *dto.Rule
err = json.Unmarshal(requestBody, &data)
if err != nil {
response.NewErrorResponse(response.ParamsError, "params error", err).FPrint(w)
return
}
if data.Action == dto.FORWARD && !address.IsValidEmailAddress(data.Params) {
response.NewErrorResponse(response.ParamsError, "ParamsError error", i18n.GetText(ctx.Lang, "invalid_email_address")).FPrint(w)
return
}
for _, r := range data.Rules {
if !array.InArray(r.Field, []string{"From", "Subject", "To", "Cc", "Text", "Html", "Content"}) {
response.NewErrorResponse(response.ParamsError, "ParamsError error", "params error! Rule Field Error!").FPrint(w)
return
}
}
err = data.Encode().Save(ctx)
if err != nil {
response.NewErrorResponse(response.ServerError, "server error", err).FPrint(w)
return
}
response.NewSuccessResponse("succ").FPrint(w)
}
type delRuleReq struct {
Id int `json:"id"`
}
func DelRule(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
requestBody, err := io.ReadAll(req.Body)
if err != nil {
log.WithContext(ctx).Errorf("ReadError:%v", err)
return
}
var data delRuleReq
err = json.Unmarshal(requestBody, &data)
if err != nil {
response.NewErrorResponse(response.ParamsError, "params error", err).FPrint(w)
return
}
if data.Id <= 0 {
response.NewErrorResponse(response.ParamsError, "params error", "id is empty").FPrint(w)
return
}
_, err = db.Instance.Exec(db.WithContext(ctx, "delete from rule where id =? and user_id =?"), data.Id, ctx.UserID)
if err != nil {
response.NewErrorResponse(response.ServerError, "unknown error", err).FPrint(w)
return
}
response.NewSuccessResponse("succ").FPrint(w)
}
+3 -3
View File
@@ -6,9 +6,9 @@ import (
"io"
"net/http"
"pmail/db"
"pmail/dto"
"pmail/dto/response"
"pmail/i18n"
"pmail/utils/context"
"pmail/utils/password"
)
@@ -16,7 +16,7 @@ type modifyPasswordRequest struct {
Password string `json:"password"`
}
func ModifyPassword(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func ModifyPassword(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
log.Errorf("%+v", err)
@@ -30,7 +30,7 @@ func ModifyPassword(ctx *dto.Context, w http.ResponseWriter, req *http.Request)
if retData.Password != "" {
encodePwd := password.Encode(retData.Password)
_, err := db.Instance.Exec(db.WithContext(ctx, "update user set password = ? where id =?"), encodePwd, ctx.UserInfo.ID)
_, err := db.Instance.Exec(db.WithContext(ctx, "update user set password = ? where id =?"), encodePwd, ctx.UserID)
if err != nil {
response.NewErrorResponse(response.ServerError, i18n.GetText(ctx.Lang, "unknowError"), "").FPrint(w)
return
+2 -2
View File
@@ -5,10 +5,10 @@ import (
"io"
"net/http"
"pmail/config"
"pmail/dto"
"pmail/dto/response"
"pmail/services/setup"
"pmail/services/setup/ssl"
"pmail/utils/context"
"strings"
)
@@ -23,7 +23,7 @@ func AcmeChallenge(w http.ResponseWriter, r *http.Request) {
}
}
func Setup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
func Setup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
reqBytes, err := io.ReadAll(req.Body)
if err != nil {
response.NewSuccessResponse("").FPrint(w)