code.gitea.io/gitea@v1.19.3/modules/setting/incoming_email.go (about)

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