gitea源码

incoming_email.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/mail"
  8. "strings"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. var IncomingEmail = struct {
  12. Enabled bool
  13. ReplyToAddress string
  14. TokenPlaceholder string `ini:"-"`
  15. Host string
  16. Port int
  17. UseTLS bool `ini:"USE_TLS"`
  18. SkipTLSVerify bool `ini:"SKIP_TLS_VERIFY"`
  19. Username string
  20. Password string
  21. Mailbox string
  22. DeleteHandledMessage bool
  23. MaximumMessageSize uint32
  24. }{
  25. Mailbox: "INBOX",
  26. DeleteHandledMessage: true,
  27. TokenPlaceholder: "%{token}",
  28. MaximumMessageSize: 10485760,
  29. }
  30. func loadIncomingEmailFrom(rootCfg ConfigProvider) {
  31. mustMapSetting(rootCfg, "email.incoming", &IncomingEmail)
  32. if !IncomingEmail.Enabled {
  33. return
  34. }
  35. if err := checkReplyToAddress(); err != nil {
  36. log.Fatal("Invalid incoming_mail.REPLY_TO_ADDRESS (%s): %v", IncomingEmail.ReplyToAddress, err)
  37. }
  38. }
  39. func checkReplyToAddress() error {
  40. parsed, err := mail.ParseAddress(IncomingEmail.ReplyToAddress)
  41. if err != nil {
  42. return err
  43. }
  44. if parsed.Name != "" {
  45. return errors.New("name must not be set")
  46. }
  47. c := strings.Count(IncomingEmail.ReplyToAddress, IncomingEmail.TokenPlaceholder)
  48. switch c {
  49. case 0:
  50. return fmt.Errorf("%s must appear in the user part of the address (before the @)", IncomingEmail.TokenPlaceholder)
  51. case 1:
  52. default:
  53. return fmt.Errorf("%s must appear only once", IncomingEmail.TokenPlaceholder)
  54. }
  55. parts := strings.Split(IncomingEmail.ReplyToAddress, "@")
  56. if !strings.Contains(parts[0], IncomingEmail.TokenPlaceholder) {
  57. return fmt.Errorf("%s must appear in the user part of the address (before the @)", IncomingEmail.TokenPlaceholder)
  58. }
  59. return nil
  60. }