// NewConfig parses the DSN string to a Config.
// nolint: gocognit
NewConfig parses the DSN string to a Config.
nolint: gocognit
// NewConfig parses the DSN string to a Config . // nolint : gocognit
func NewConfig(dsn string) (Config, errors.Error) {
	var (
		smtpcnf = &smtpConfig{
			DSN: dsn,
		}
	)
	if !strings.ContainsRune(dsn, '?') && !strings.ContainsRune(dsn, '/') {
		dsn += "/"
	} else if strings.ContainsRune(dsn, '?') && !strings.ContainsRune(dsn, '/') {
		v := strings.Split(dsn, "?")
		v[len(v)-2] += "/"
		dsn = strings.Join(v, "?")
	}
	foundSlash := false
	for i := len(dsn) - 1; i >= 0; i-- {
		if dsn[i] == '/' {
			foundSlash = true
			var j, k int
			if i > 0 {
				for j = i; j >= 0; j-- {
					if dsn[j] == '@' {
						for k = 0; k < j; k++ {
							if dsn[k] == ':' {
								smtpcnf.Pass = dsn[k+1 : j]
								break
							}
						}
						smtpcnf.User = dsn[:k]
						break
					}
				}
				for k = j + 1; k < i; k++ {
					if dsn[k] == '(' {
						if dsn[i-1] != ')' {
							if strings.ContainsRune(dsn[k+1:i], ')') {
								return nil, ErrorConfigInvalidDSN.Error(nil)
							}
							return nil, ErrorConfigInvalidNetwork.Error(nil)
						}
						if strings.ContainsRune(dsn[k+1:i-1], ':') {
							h, p, e := net.SplitHostPort(dsn[k+1 : i-1])
							if e == nil && p != "" {
								pint, er := strconv.ParseInt(p, 10, 64)
								if er == nil {
									smtpcnf.Port = int(pint)
								}
								smtpcnf.Host = h
							}
						}
						if smtpcnf.Host == "" || smtpcnf.Port == 0 {
							smtpcnf.Host = dsn[k+1 : i-1]
						}
						break
					}
				}
				smtpcnf.Net = parseNETMode(dsn[j+1 : k])
			}
			for j = i + 1; j < len(dsn); j++ {
				if dsn[j] == '?' {
					if val, err := url.ParseQuery(dsn[j+1:]); err != nil {
						return nil, ErrorConfigInvalidParams.ErrorParent(err)
					} else {
						if val.Get("ServerName") != "" {
							smtpcnf.ServerName = val.Get("ServerName")
						}
						if val.Get("SkipVerify") != "" {
							vi, e := strconv.ParseBool(val.Get("SkipVerify"))
							if e == nil {
								smtpcnf.SkipVerify = vi
							}
						}
					}
					break
				}
			}
			smtpcnf.TLS = parseTLSMode(dsn[i+1 : j])
			break
		}
	}
	if !foundSlash && len(dsn) > 0 {
		return nil, ErrorConfigInvalidHost.Error(nil)
	}
	return smtpcnf, nil
}
func NewConfig ( dsn string ) ( Config , errors . Error ) { var ( smtpcnf = & smtpConfig { DSN : dsn , } ) if ! strings . ContainsRune ( dsn , '?' ) && ! strings . ContainsRune ( dsn , '/' ) { dsn += " " } else if strings . ContainsRune ( dsn , '?' ) && ! strings . ContainsRune ( dsn , '/' ) { v := strings . Split ( dsn , " " ) v [ len ( v ) - 2 ] += " " dsn = strings . Join ( v , " " ) } foundSlash := false for i := len ( dsn ) - 1 ; i >= 0 ; i -- { if dsn [ i ] == '/' { foundSlash = true var j , k int if i > 0 { for j = i ; j >= 0 ; j -- { if dsn [ j ] == '@' { for k = 0 ; k < j ; k ++ { if dsn [ k ] == ':' { smtpcnf . Pass = dsn [ k + 1 : j ] break } } smtpcnf . User = dsn [ : k ] break } } for k = j + 1 ; k < i ; k ++ { if dsn [ k ] == '(' { if dsn [ i - 1 ] != ')' { if strings . ContainsRune ( dsn [ k + 1 : i ] , ')' ) { return nil , ErrorConfigInvalidDSN . Error ( nil ) } return nil , ErrorConfigInvalidNetwork . Error ( nil ) } if strings . ContainsRune ( dsn [ k + 1 : i - 1 ] , ':' ) { h , p , e := net . SplitHostPort ( dsn [ k + 1 : i - 1 ] ) if e == nil && p != " " { pint , er := strconv . ParseInt ( p , 10 , 64 ) if er == nil { smtpcnf . Port = int ( pint ) } smtpcnf . Host = h } } if smtpcnf . Host == " " || smtpcnf . Port == 0 { smtpcnf . Host = dsn [ k + 1 : i - 1 ] } break } } smtpcnf . Net = parseNETMode ( dsn [ j + 1 : k ] ) } for j = i + 1 ; j < len ( dsn ) ; j ++ { if dsn [ j ] == '?' { if val , err := url . ParseQuery ( dsn [ j + 1 : ] ) ; err != nil { return nil , ErrorConfigInvalidParams . ErrorParent ( err ) } else { if val . Get ( " " ) != " " { smtpcnf . ServerName = val . Get ( " " ) } if val . Get ( " " ) != " " { vi , e := strconv . ParseBool ( val . Get ( " " ) ) if e == nil { smtpcnf . SkipVerify = vi } } } break } } smtpcnf . TLS = parseTLSMode ( dsn [ i + 1 : j ] ) break } } if ! foundSlash && len ( dsn ) > 0 { return nil , ErrorConfigInvalidHost . Error ( nil ) } return smtpcnf , nil }
NewConfig parses the DSN string to a Config.
NewConfig parses the DSN string to a Config .
// [user[:password]@][net[(addr)]]/tlsmode[?param1=value1&paramN=valueN] // Find the last '/' (since the password or the net addr might contain a '/') // left part is empty if i <= 0 // [username[:password]@][protocol[(address)]] // Find the last '@' in dsn[:i] // username[:password] // Find the first ':' in dsn[:j] // [protocol[(address)]] // Find the first '(' in dsn[j+1:i] // dsn[i-1] must be == ')' if an address is specified // [?param1=value1&...&paramN=valueN] // Find the first '?' in dsn[i+1:]