File: match.go

package info (click to toggle)
golang-github-protonmail-gluon 0.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,020 kB
  • sloc: sh: 55; makefile: 5
file content (195 lines) | stat: -rw-r--r-- 4,404 bytes parent folder | download
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package state

import (
	"context"
	"fmt"
	"regexp"
	"strings"

	"github.com/ProtonMail/gluon/db"
	"github.com/ProtonMail/gluon/imap"
	"github.com/bradenaw/juniper/xslices"
)

type Match struct {
	Name      string
	Delimiter string
	Atts      imap.FlagSet
}

type matchMailbox struct {
	Name       string
	Subscribed bool
	// EntMBox should be set to nil if there is no such value.
	EntMBox *db.MailboxWithAttr
}

func getMatches(
	ctx context.Context,
	client db.ReadOnly,
	allMailboxes []matchMailbox,
	ref, pattern, delimiter string,
	subscribed bool,
) (map[string]Match, error) {
	matches := make(map[string]Match)

	mailboxes := make(map[string]matchMailbox)
	for _, mbox := range allMailboxes {
		mailboxes[mbox.Name] = mbox
	}

	for mboxName := range mailboxes {
		select {
		case <-ctx.Done():
			return nil, ctx.Err()

		default: // fallthrough
		}

		for _, superior := range append(listSuperiors(mboxName, delimiter), mboxName) {
			matchedName, isMatched := match(ref, pattern, delimiter, superior)
			if !isMatched {
				continue
			}

			if _, alreadyMatched := matches[matchedName]; alreadyMatched {
				continue
			}

			mbox, mailboxExists := mailboxes[matchedName]

			match, isMatch, err := prepareMatch(
				ctx, client, matchedName, &mbox,
				pattern, delimiter,
				mboxName == matchedName, mailboxExists, subscribed,
			)
			if err != nil {
				return nil, err
			}

			if isMatch {
				matches[match.Name] = match
			}
		}
	}

	return matches, nil
}

func prepareMatch(
	ctx context.Context,
	client db.ReadOnly,
	matchedName string,
	mbox *matchMailbox,
	pattern, delimiter string,
	isNotSuperior, mailboxExists, onlySubscribed bool,
) (Match, bool, error) {
	// not match when:
	if onlySubscribed && (mbox == nil || !mbox.Subscribed) && // should be subscribed and it's not
		(isNotSuperior || !strings.HasSuffix(pattern, "%")) { // is not superior or percent wildcard not used
		return Match{}, false, nil
	}

	// add match as NoSelect when:
	if !mailboxExists || // is deleted superior
		matchedName == "" || // is empty request for delimiter response
		onlySubscribed && !mbox.Subscribed { // is unsubscribed superior
		return Match{
			Name:      matchedName,
			Delimiter: delimiter,
			Atts:      imap.NewFlagSet(imap.AttrNoSelect),
		}, true, nil
	}

	var (
		atts imap.FlagSet
	)

	if mbox.EntMBox != nil {
		atts = mbox.EntMBox.Attributes.Clone()

		recent, err := client.GetMailboxRecentCount(ctx, mbox.EntMBox.ID)
		if err != nil {
			return Match{}, false, err
		}

		if recent > 0 {
			atts.AddToSelf(imap.AttrMarked)
		} else {
			atts.AddToSelf(imap.AttrUnmarked)
		}
	} else {
		atts = imap.NewFlagSet(imap.AttrNoSelect)
	}

	return Match{
		Name:      mbox.Name,
		Delimiter: delimiter,
		Atts:      atts,
	}, true, nil
}

// GOMSRV-100: validate this implementation.
func match(ref, pattern, del, mailboxName string) (string, bool) {
	if pattern == "" {
		return matchRoot(ref, del)
	}

	rx := fmt.Sprintf("^%v", regexp.QuoteMeta(canon(ref+pattern, del)))

	// If the "%" wildcard is the last character of a mailbox name argument,
	// matching levels of hierarchy are also returned.
	if !strings.HasSuffix(pattern, "%") {
		rx += "$"
	}

	// The character "*" is a wildcard, and matches zero or more characters at this position.
	rx = strings.ReplaceAll(rx, `\*`, ".*")

	// The character "%" is similar to "*", but it does not match a hierarchy delimiter.
	rx = strings.ReplaceAll(rx, "%", fmt.Sprintf("[^%v]*", del))

	if res := regexp.MustCompile(rx).FindAllString(mailboxName, 1); len(res) > 0 {
		return res[0], true
	}

	return "", false
}

// An empty ("" string) mailbox name argument is a special request to
// return the hierarchy delimiter and the root name of the name given
// in the reference. The value returned as the root MAY be the empty
// string if the reference is non-rooted or is an empty string.
func matchRoot(ref, del string) (string, bool) {
	if !strings.Contains(ref, del) {
		return "", true
	}

	var res string

	if strings.HasPrefix(ref, del) {
		res += del
	}

	split := strings.Split(ref, del)

	if len(split) > 0 {
		res += split[0]
	}

	if res != "" && res != del {
		res += del
	}

	return res, true
}

func canon(name, del string) string {
	return strings.Join(xslices.Map(strings.Split(name, del), func(name string) string {
		if strings.EqualFold(name, imap.Inbox) {
			return imap.Inbox
		}

		return name
	}), del)
}