messages: Pass submitter query term down to Xapian.
[software/mumi.git] / mumi / messages.scm
1 ;;; mumi -- Mediocre, uh, mail interface
2 ;;; Copyright © 2017, 2018, 2019, 2020 Ricardo Wurmus <rekado@elephly.net>
3 ;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net>
4 ;;;
5 ;;; This program is free software: you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Affero General Public License
7 ;;; as published by the Free Software Foundation, either version 3 of
8 ;;; the License, or (at your option) any later version.
9 ;;;
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Affero General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU Affero General Public
16 ;;; License along with this program. If not, see
17 ;;; <http://www.gnu.org/licenses/>.
18
19 (define-module (mumi messages)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-19)
22 #:use-module (srfi srfi-26)
23 #:use-module (ice-9 optargs)
24 #:use-module (ice-9 regex)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 textual-ports)
27 #:use-module (ice-9 binary-ports)
28 #:use-module (ice-9 threads)
29 #:use-module (email email)
30 #:use-module (mumi cache)
31 #:use-module (mumi config)
32 #:use-module (mumi debbugs)
33 #:use-module (mumi xapian)
34 #:use-module ((mumi bugs) #:prefix db:)
35 #:use-module (web client)
36 #:export (search-bugs
37 fetch-bug
38 recent-bugs
39 forgotten-issues
40 easy-bugs
41 wishlist-bugs
42 bugs-by-severity
43
44 multipart-message?
45 extract-attachment))
46
47 (define (status-with-cache ids)
48 "Invoke GET-STATUS, but only on those IDS that have not been cached
49 yet. Return new results alongside cached results."
50 (let* ((cached (filter-map cached? ids))
51 (uncached-ids (lset-difference eq?
52 ids
53 (map bug-num cached)))
54 (new (filter-map bug-status uncached-ids )))
55 ;; Cache new things
56 (map (lambda (bug) (cache! (bug-num bug) bug)) new)
57 ;; Return everything from cache
58 (sort (append cached new) (lambda (a b) (< (bug-num a) (bug-num b))))))
59
60 (define-public (extract-name address)
61 (or (assoc-ref address 'name)
62 (and=> (assoc-ref address 'address)
63 (lambda (address)
64 (string-take address (string-index address #\@))))
65 "Somebody"))
66
67 (define-public (extract-email address)
68 (assoc-ref address 'address))
69
70 (define (header message key)
71 (assoc-ref (or (email-headers message) '()) key))
72
73 (define-public (sender message)
74 (or (and=> (header message 'from) first)
75 '((name . "Unknown sender")
76 (address . "unknown"))))
77
78 (define-public sender-email
79 (compose extract-email sender))
80
81 (define-public (sender-name message)
82 (extract-name (sender message)))
83
84 (define-public (date message)
85 (or (header message 'date) "unknown date"))
86
87 (define-public (subject message)
88 (or (header message 'subject) "(no subject)"))
89
90 (define-public (message-id message)
91 (header message 'message-id))
92
93 (define-public (participants messages)
94 "Return a list of unique senders in the conversion."
95 (apply lset-adjoin (lambda (a b)
96 (string= (extract-email a)
97 (extract-email b)))
98 '() (map sender messages)))
99
100 (define-public (recipients message)
101 "Return a list of recipient email addresses for the given MESSAGE."
102 (let ((headers (or (email-headers message) '())))
103 (filter-map (match-lambda
104 (((or 'cc 'bcc 'to) val) val)
105 (_ #f)) headers)))
106
107 (define-public (closing? message id)
108 "Is this MESSAGE closing this bug ID?"
109 (let ((done (string-append (number->string id)
110 "-done")))
111 (string= (header message 'x-debbugs-envelope-to) done)))
112
113 (define-public (bot? address)
114 (string= "help-debbugs@gnu.org" address))
115
116 (define-public (internal-message? message)
117 (bot? (sender-email message)))
118
119 \f
120 (define (multipart-message? message)
121 (eq? (assoc-ref (header message 'content-type)
122 'type)
123 'multipart))
124
125 (define (extract-attachment id msg-num path)
126 "Extract attachment from message number MSG-NUM in the thread for
127 the bug with the given ID. Follow PATH to get to the correct
128 multipart chunk containing the attachment. This is absolutely
129 horrible because Debbugs does not let us access messages directly, so
130 we have to do this in a very convoluted way."
131 (define (nth n lst)
132 (and (< n (length lst))
133 (list-ref lst n)))
134 (define (traverse path parts)
135 (let loop ((path path)
136 (parts parts))
137 (match path
138 ((pos) (nth pos parts))
139 ((pos . rest)
140 (loop rest
141 (and=> (nth pos parts)
142 mime-entity-body))))))
143 (and=> (nth msg-num (issue-messages id))
144 (lambda (msg)
145 (cond
146 ((multipart-message? msg)
147 (traverse path (email-body msg)))
148 (else
149 (match path
150 (() msg)
151 (_ #f)))))))
152
153 \f
154 ;; We would like to use get-bug-log here, but it often returns
155 ;; truncated messages. This is a known bug upstream.
156 (define-public (issue-messages bug-id)
157 "Return list of messages relating to the issue BUG-ID. Cache the
158 result for a while."
159 (define archived-log
160 (bug-id->log-file bug-id #:archived? #t))
161 (define active-log
162 (bug-id->log-file bug-id))
163 (define file
164 (or (and (file-exists? archived-log) archived-log)
165 (and (file-exists? active-log) active-log)))
166 (if file
167 (let ((key (list 'issue-messages bug-id)))
168 (or (cached? key)
169 (cache! key
170 (call-with-input-file file
171 read-emails-from-bug-log))))
172 '()))
173
174 (define* (search-bugs query #:key (sets '()) (max 400))
175 "Return a list of all bugs matching the given QUERY string.
176 Intersect the result with the id sets in the list SETS."
177 (let* ((ids (delete-duplicates
178 (map string->number
179 (search query))))
180 (filtered (match sets
181 (() ids)
182 (_ (apply lset-intersection eq? ids sets)))))
183 (status-with-cache (if (> (length filtered) max)
184 (take filtered max) filtered))))
185
186 (define fetch-bug bug-status)
187
188 (define (recent-bugs amount)
189 "Return up to AMOUNT bugs with most recent activity."
190 (let* ((recent-ids
191 (sort
192 (delete-duplicates
193 (map string->number
194 (search "mdate:1m..")))
195 <))
196 (ids (take (reverse recent-ids)
197 (min amount (length recent-ids)))))
198 (status-with-cache ids)))
199
200 (define (forgotten-issues amount)
201 "Return up to AMOUNT issues that appear to have been forgotten
202 about."
203 (let* ((forgotten-ids (forgotten-bug-numbers (%config 'packages)))
204 (ids (take (reverse forgotten-ids)
205 (min amount (length forgotten-ids)))))
206 (status-with-cache ids)))
207
208 (define (easy-bugs)
209 "Return all bugs that have been tagged \"easy\"."
210 (let ((ids (db:bugs-by-tag "easy")))
211 (status-with-cache ids)))
212
213 (define (wishlist-bugs)
214 "Return all bugs that have been tagged \"wishlist\"."
215 (let ((ids (db:bugs-by-tag "wishlist")))
216 (status-with-cache ids)))
217
218 (define* (bugs-by-severity severity #:optional status)
219 "Return severe bugs."
220 (let* ((severity-ids (db:bugs-by-severity severity))
221 (ids (if status
222 (let ((status-ids (db:bugs-by-status status)))
223 (lset-intersection eq? severity-ids status-ids))
224 severity-ids)))
225 (status-with-cache ids)))
226
227 (define punctuation?
228 (cut char-set-contains? char-set:punctuation <>))
229
230 (define-public (process-query query)
231 "Process the QUERY string and return a list of query terms and
232 sets that need to overlap the result set."
233 ;; Xapian doesn't like punctuation. Replace with spaces. Leave
234 ;; hyphens and quotes.
235 (define (clean-term term)
236 (string-map (match-lambda
237 ((and (or #\" #\-) c) c)
238 ((? punctuation? c) #\space)
239 (c c))
240 term))
241 (fold (lambda (term acc)
242 (match acc
243 ((#:terms terms
244 #:sets fs)
245 (match (string-split term #\:)
246 (("is" (or "done" "closed"))
247 `(#:terms ,terms
248 #:sets ,(cons (db:bugs-by-status "done") fs)))
249 (("is" (or "open" "pending"))
250 `(#:terms ,terms
251 #:sets ,(cons (db:bugs-by-status "open") fs)))
252 (("date" when)
253 `(#:terms ,(cons (string-append "date:" when) terms)
254 #:sets ,fs))
255 ;; TODO: this should only be the title of the bug, not
256 ;; the subject.
257 (("title" title)
258 `(#:terms
259 ,(cons (string-append "subject:" (clean-term title))
260 terms)
261 #:sets ,fs))
262 (("subject" subject)
263 `(#:terms
264 ,(cons (string-append "subject:" (clean-term subject))
265 terms)
266 #:sets ,fs))
267 (("tag" tag)
268 `(#:terms ,terms
269 #:sets
270 ,(cons (db:bugs-by-tag (clean-term tag)) fs)))
271 (("author" who)
272 `(#:terms
273 ,(cons (string-append "from:" (clean-term who)) terms)
274 #:sets ,fs))
275 (("owner" who)
276 `(#:terms ,terms
277 #:sets ,(cons (db:bugs-by-owner who) fs)))
278 (("submitter" who)
279 `(#:terms ,(cons (string-append "submitter:" who) terms)
280 #:sets ,fs))
281 (("severity" level)
282 `(#:terms ,terms
283 #:sets ,(cons (db:bugs-by-severity level) fs)))
284 ((whatever term)
285 `(#:terms ,(cons (clean-term term) terms)
286 #:sets ,fs))
287 (_
288 `(#:terms ,(cons (clean-term term) terms)
289 #:sets ,fs))))))
290 '(#:terms () #:sets ())
291 (tokenize query)))