aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/search.go
blob: 4d8cef80740e9092ceb2634c4f4dfd2bc0a7da89 (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
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
package alpsbase

import (
	"bufio"
	"bytes"
	"net/textproto"
	"strings"

	"github.com/emersion/go-imap"
)

func searchCriteriaHeader(k, v string) *imap.SearchCriteria {
	return &imap.SearchCriteria{
		Header: map[string][]string{
			k: []string{v},
		},
	}
}

func searchCriteriaOr(criteria ...*imap.SearchCriteria) *imap.SearchCriteria {
	if criteria[0] == nil {
		criteria = criteria[1:]
	}
	or := criteria[0]
	for _, c := range criteria[1:] {
		or = &imap.SearchCriteria{
			Or: [][2]*imap.SearchCriteria{{or, c}},
		}
	}
	return or
}

func searchCriteriaAnd(criteria ...*imap.SearchCriteria) *imap.SearchCriteria {
	if criteria[0] == nil {
		criteria = criteria[1:]
	}
	and := criteria[0]
	for _, c := range criteria[1:] {
		// TODO: Maybe pitch the AND and OR functions to go-imap upstream
		if c.Header != nil {
			if and.Header == nil {
				and.Header = make(textproto.MIMEHeader)
			}

			for key, value := range c.Header {
				if _, ok := and.Header[key]; !ok {
					and.Header[key] = nil
				}
				and.Header[key] = append(and.Header[key], value...)
			}
		}
		and.Body = append(and.Body, c.Body...)
		and.Text = append(and.Text, c.Text...)
		and.WithFlags = append(and.WithFlags, c.WithFlags...)
		and.WithoutFlags = append(and.WithoutFlags, c.WithoutFlags...)
		// TODO: Merge more things
	}
	return and
}

// Splits search up into the longest string of non-functional parts and
// functional parts
//
// Input: hello world foo:bar baz trains:"are cool"
// Output: ["hello world", "foo:bar", "baz", "trains:are cool"]
func splitSearchTokens(buf []byte, eof bool) (int, []byte, error) {
	if len(buf) == 0 {
		return 0, nil, nil
	}

	if buf[0] == ' ' {
		return 1, nil, nil
	}

	colon := bytes.IndexByte(buf, byte(':'))
	if colon == -1 && eof {
		return len(buf), buf, nil
	} else if colon == -1 {
		return 0, nil, nil
	} else {
		space := bytes.LastIndexByte(buf[:colon], byte(' '))
		if space != -1 {
			return space, buf[:space], nil
		}

		var (
			terminator int
			quoted     bool
		)
		if colon + 1 < len(buf) && buf[colon+1] == byte('"') {
			terminator = bytes.IndexByte(buf[colon+2:], byte('"'))
			terminator += colon + 3
			quoted = true
		} else {
			terminator = bytes.IndexByte(buf[colon:], byte(' '))
			terminator += colon
		}

		if terminator == -1 {
			return 0, nil, nil
		} else if terminator == -1 && eof {
			terminator = len(buf)
		}

		if quoted {
			trimmed := append(buf[:colon+1], buf[colon+2:terminator-1]...)
			return terminator, trimmed, nil
		}

		return terminator, buf[:terminator], nil
	}
}

// TODO: Document search functionality somewhere
func PrepareSearch(terms string) *imap.SearchCriteria {
	// XXX: If Migadu's IMAP servers can learn a better Full-Text Search then
	// we can probably start matching on the message bodies by default (gated
	// behind some kind of flag, perhaps)
	var criteria *imap.SearchCriteria

	scanner := bufio.NewScanner(strings.NewReader(terms))
	scanner.Split(splitSearchTokens)

	for scanner.Scan() {
		term := scanner.Text()
		if !strings.ContainsRune(term, ':') {
			criteria = searchCriteriaAnd(
				criteria,
				searchCriteriaOr(
					searchCriteriaHeader("From", term),
					searchCriteriaHeader("To", term),
					searchCriteriaHeader("Cc", term),
					searchCriteriaHeader("Subject", term),
				),
			)
		} else {
			parts := strings.SplitN(term, ":", 2)
			key, value := parts[0], parts[1]
			switch strings.ToLower(key) {
			case "from":
				criteria = searchCriteriaAnd(
					criteria, searchCriteriaHeader("From", value))
			case "to":
				criteria = searchCriteriaAnd(
					criteria, searchCriteriaHeader("To", value))
			case "cc":
				criteria = searchCriteriaAnd(
					criteria, searchCriteriaHeader("Cc", value))
			case "subject":
				criteria = searchCriteriaAnd(
					criteria, searchCriteriaHeader("Subject", value))
			case "body":
				criteria = searchCriteriaAnd(
					criteria, &imap.SearchCriteria{Body: []string{value}})
			default:
				continue
			}
		}
	}

	return criteria
}