aboutsummaryrefslogtreecommitdiffstats
path: root/lib/send/parse.go
blob: d1088ee22adb22191c715bf8138fd5573c531fb4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package send

import (
	"fmt"
	"math/rand"
	"net/url"
	"os"
	"strconv"
	"strings"

	"github.com/emersion/go-message/mail"
)

func parseScheme(uri *url.URL) (protocol string, auth string, err error) {
	protocol = ""
	auth = "plain"
	if uri.Scheme != "" {
		parts := strings.Split(uri.Scheme, "+")
		switch len(parts) {
		case 1:
			protocol = parts[0]
		case 2:
			if parts[1] == "insecure" {
				protocol = uri.Scheme
			} else {
				protocol = parts[0]
				auth = parts[1]
			}
		case 3:
			protocol = parts[0] + "+" + parts[1]
			auth = parts[2]
		default:
			return "", "", fmt.Errorf("Unknown scheme %s", uri.Scheme)
		}
	}
	return protocol, auth, nil
}

func GetMessageIdHostname(sendWithHostname bool, from *mail.Address) (string, error) {
	if sendWithHostname {
		return os.Hostname()
	}
	if from == nil {
		// no from address present, generate a random hostname
		return strings.ToUpper(strconv.FormatInt(rand.Int63(), 36)), nil
	}
	_, domain, found := strings.Cut(from.Address, "@")
	if !found {
		return "", fmt.Errorf("Invalid address %q", from)
	}
	return domain, nil
}