action.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package pop3_server
  2. import (
  3. "database/sql"
  4. "github.com/Jinnrry/gopop"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/spf13/cast"
  7. "pmail/db"
  8. "pmail/models"
  9. "pmail/services/detail"
  10. "pmail/utils/array"
  11. "pmail/utils/context"
  12. "pmail/utils/errors"
  13. "pmail/utils/id"
  14. "pmail/utils/password"
  15. )
  16. type action struct {
  17. }
  18. func (a action) User(ctx *gopop.Data, username string) error {
  19. if ctx.Ctx == nil {
  20. tc := &context.Context{}
  21. tc.SetValue(context.LogID, id.GenLogID())
  22. ctx.Ctx = tc
  23. }
  24. log.WithContext(ctx.Ctx).Debugf("POP3 User %s", username)
  25. ctx.User = username
  26. return nil
  27. }
  28. func (a action) Pass(ctx *gopop.Data, pwd string) error {
  29. if ctx.Ctx == nil {
  30. tc := &context.Context{}
  31. tc.SetValue(context.LogID, id.GenLogID())
  32. ctx.Ctx = tc
  33. }
  34. log.WithContext(ctx.Ctx).Debugf("POP3 PASS %s", pwd)
  35. var user models.User
  36. encodePwd := password.Encode(pwd)
  37. err := db.Instance.Get(&user, db.WithContext(ctx.Ctx.(*context.Context), "select * from user where account =? and password =?"), ctx.User, encodePwd)
  38. if err != nil && !errors.Is(err, sql.ErrNoRows) {
  39. log.WithContext(ctx.Ctx.(*context.Context)).Errorf("%+v", err)
  40. }
  41. if user.ID > 0 {
  42. ctx.Status = gopop.TRANSACTION
  43. ctx.Ctx.(*context.Context).UserID = user.ID
  44. ctx.Ctx.(*context.Context).UserName = user.Name
  45. ctx.Ctx.(*context.Context).UserAccount = user.Account
  46. return nil
  47. }
  48. return errors.New("password error")
  49. }
  50. func (a action) Apop(ctx *gopop.Data, username, digest string) error {
  51. if ctx.Ctx == nil {
  52. tc := &context.Context{}
  53. tc.SetValue(context.LogID, id.GenLogID())
  54. ctx.Ctx = tc
  55. }
  56. log.WithContext(ctx.Ctx).Debugf("POP3 APOP %s %s", username, digest)
  57. var user models.User
  58. err := db.Instance.Get(&user, db.WithContext(ctx.Ctx.(*context.Context), "select * from user where account =? "), username)
  59. if err != nil && !errors.Is(err, sql.ErrNoRows) {
  60. log.WithContext(ctx.Ctx.(*context.Context)).Errorf("%+v", err)
  61. }
  62. if user.ID > 0 && digest == password.Md5Encode(user.Password) {
  63. ctx.User = username
  64. ctx.Status = gopop.TRANSACTION
  65. ctx.Ctx.(*context.Context).UserID = user.ID
  66. ctx.Ctx.(*context.Context).UserName = user.Name
  67. ctx.Ctx.(*context.Context).UserAccount = user.Account
  68. return nil
  69. }
  70. return errors.New("password error")
  71. }
  72. type statInfo struct {
  73. Num int64 `json:"num"`
  74. Size int64 `json:"size"`
  75. }
  76. func (a action) Stat(ctx *gopop.Data) (msgNum, msgSize int64, err error) {
  77. var si statInfo
  78. err = db.Instance.Get(&si, db.WithContext(ctx.Ctx.(*context.Context), "select count(1) as `num`, sum(length(text)+length(html)) as `size` from email"))
  79. if err != nil && !errors.Is(err, sql.ErrNoRows) {
  80. log.WithContext(ctx.Ctx.(*context.Context)).Errorf("%+v", err)
  81. err = nil
  82. log.WithContext(ctx.Ctx).Debugf("POP3 STAT RETURT :0,0")
  83. return 0, 0, nil
  84. }
  85. log.WithContext(ctx.Ctx).Debugf("POP3 STAT RETURT : %d,%d", si.Num, si.Size)
  86. return si.Num, si.Size, nil
  87. }
  88. func (a action) Uidl(ctx *gopop.Data, id int64) (string, error) {
  89. log.WithContext(ctx.Ctx).Debugf("POP3 Uidl RETURT : %d", id)
  90. return cast.ToString(id), nil
  91. }
  92. type listItem struct {
  93. Id int64 `json:"id"`
  94. Size int64 `json:"size"`
  95. }
  96. func (a action) List(ctx *gopop.Data, msg string) ([]gopop.MailInfo, error) {
  97. var res []listItem
  98. var id int64
  99. if msg != "" {
  100. id = cast.ToInt64(msg)
  101. if id == 0 {
  102. return nil, errors.New("params error")
  103. }
  104. }
  105. var err error
  106. if id != 0 {
  107. err = db.Instance.Select(&res, db.WithContext(ctx.Ctx.(*context.Context), "select id, length(text)+length(html) as `size` from email where id =?"), id)
  108. } else {
  109. err = db.Instance.Select(&res, db.WithContext(ctx.Ctx.(*context.Context), "select id, length(text)+length(html) as `size` from email"))
  110. }
  111. if err != nil && !errors.Is(err, sql.ErrNoRows) {
  112. log.WithContext(ctx.Ctx.(*context.Context)).Errorf("%+v", err)
  113. err = nil
  114. return []gopop.MailInfo{}, nil
  115. }
  116. ret := []gopop.MailInfo{}
  117. for _, re := range res {
  118. ret = append(ret, gopop.MailInfo{
  119. Id: re.Id,
  120. Size: re.Size,
  121. })
  122. }
  123. return ret, nil
  124. }
  125. func (a action) Retr(ctx *gopop.Data, id int64) (string, int64, error) {
  126. email, err := detail.GetEmailDetail(ctx.Ctx.(*context.Context), cast.ToInt(id), false)
  127. if err != nil {
  128. log.WithContext(ctx.Ctx.(*context.Context)).Errorf("%+v", err)
  129. return "", 0, errors.New("server error")
  130. }
  131. ret := email.ToTransObj().BuildBytes(ctx.Ctx.(*context.Context), false)
  132. return string(ret), cast.ToInt64(len(ret)), nil
  133. }
  134. func (a action) Delete(ctx *gopop.Data, id int64) error {
  135. ctx.DeleteIds = append(ctx.DeleteIds, id)
  136. ctx.DeleteIds = array.Unique(ctx.DeleteIds)
  137. return nil
  138. }
  139. func (a action) Rest(ctx *gopop.Data) error {
  140. ctx.DeleteIds = []int64{}
  141. return nil
  142. }
  143. func (a action) Top(ctx *gopop.Data, id int64, n int) (string, error) {
  144. //email, err := detail.GetEmailDetail(ctx.Ctx.(*context.Context), cast.ToInt(id), false)
  145. //if err != nil {
  146. // log.WithContext(ctx.Ctx.(*context.Context)).Errorf("%+v", err)
  147. // return "", errors.New("server error")
  148. //}
  149. //
  150. //ret := email.ToTransObj().BuilderHeaders(ctx.Ctx.(*context.Context))
  151. //return string(ret), nil
  152. return "", errors.New("not supported")
  153. }
  154. func (a action) Noop(ctx *gopop.Data) error {
  155. return nil
  156. }
  157. func (a action) Quit(ctx *gopop.Data) error {
  158. if len(ctx.DeleteIds) > 0 {
  159. _, err := db.Instance.Exec(db.WithContext(ctx.Ctx.(*context.Context), "DELETE FROM email WHERE id in ?"), ctx.DeleteIds)
  160. if err != nil {
  161. log.WithContext(ctx.Ctx.(*context.Context)).Errorf("%+v", err)
  162. }
  163. }
  164. return nil
  165. }