messages: Keep hyphens and quotes when preprocessing query string.
[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 (debbugs cache)
30 #:use-module (debbugs soap)
31 #:use-module (debbugs operations)
32 #:use-module (debbugs bug)
33 #:use-module (email email)
34 #:use-module (mumi config)
35 #:use-module (mumi debbugs)
36 #:use-module (mumi xapian)
37 #:use-module ((mumi bugs) #:prefix db:)
38 #:use-module (web client)
39 #:export (search-bugs
40 fetch-bug
41 recent-bugs
42 easy-bugs
43 wishlist-bugs
44 bugs-by-severity
45
46 multipart-message?
47 extract-attachment))
48
49 (define (status-with-cache ids)
50 "Invoke GET-STATUS, but only on those IDS that have not been cached
51 yet. Return new results alongside cached results."
52 (let* ((cached (filter-map cached? ids))
53 (uncached-ids (lset-difference eq?
54 ids
55 (map bug-num cached)))
56 (new (soap-invoke* (%config 'debbugs) get-status uncached-ids)))
57 ;; Cache new things
58 (map (lambda (bug) (cache! (bug-num bug) bug)) new)
59 ;; Return everything from cache
60 (sort (append cached new) (lambda (a b) (< (bug-num a) (bug-num b))))))
61
62 (define-public (extract-name address)
63 (or (assoc-ref address 'name)
64 (and=> (assoc-ref address 'address)
65 (lambda (address)
66 (string-take address (string-index address #\@))))
67 "Somebody"))
68
69 (define-public (extract-email address)
70 (assoc-ref address 'address))
71
72 (define (header message key)
73 (assoc-ref (or (email-headers message) '()) key))
74
75 (define-public (sender message)
76 (or (and=> (header message 'from) first)
77 '((name . "Unknown sender")
78 (address . "unknown"))))
79
80 (define-public sender-email
81 (compose extract-email sender))
82
83 (define-public (sender-name message)
84 (extract-name (sender message)))
85
86 (define-public (date message)
87 (or (header message 'date) "unknown date"))
88
89 (define-public (subject message)
90 (or (header message 'subject) "(no subject)"))
91
92 (define-public (message-id message)
93 (header message 'message-id))
94
95 (define-public (participants messages)
96 "Return a list of unique senders in the conversion."
97 (apply lset-adjoin (lambda (a b)
98 (string= (extract-email a)
99 (extract-email b)))
100 '() (map sender messages)))
101
102 (define-public (recipients message)
103 "Return a list of recipient email addresses for the given MESSAGE."
104 (let ((headers (or (email-headers message) '())))
105 (filter-map (match-lambda
106 (((or 'cc 'bcc 'to) val) val)
107 (_ #f)) headers)))
108
109 (define-public (closing? message id)
110 "Is this MESSAGE closing this bug ID?"
111 (let ((done (string-append (number->string id)
112 "-done")))
113 (string= (header message 'x-debbugs-envelope-to) done)))
114
115 (define-public (bot? address)
116 (string= "help-debbugs@gnu.org" address))
117
118 (define-public (internal-message? message)
119 (bot? (sender-email message)))
120
121 \f
122 (define (multipart-message? message)
123 (eq? (assoc-ref (header message 'content-type)
124 'type)
125 'multipart))
126
127 (define (extract-attachment id msg-num path)
128 "Extract attachment from message number MSG-NUM in the thread for
129 the bug with the given ID. Follow PATH to get to the correct
130 multipart chunk containing the attachment. This is absolutely
131 horrible because Debbugs does not let us access messages directly, so
132 we have to do this in a very convoluted way."
133 (define (nth n lst)
134 (and (< n (length lst))
135 (list-ref lst n)))
136 (define (traverse path parts)
137 (let loop ((path path)
138 (parts parts))
139 (match path
140 ((pos) (nth pos parts))
141 ((pos . rest)
142 (loop rest
143 (and=> (nth pos parts)
144 mime-entity-body))))))
145 (and=> (nth msg-num (issue-messages id))
146 (lambda (msg)
147 (cond
148 ((multipart-message? msg)
149 (traverse path (email-body msg)))
150 (else
151 (match path
152 (() msg)
153 (_ #f)))))))
154
155 \f
156 ;; We would like to use get-bug-log here, but it often returns
157 ;; truncated messages. This is a known bug upstream.
158 (define-public (issue-messages bug-id)
159 "Return list of messages relating to the issue BUG-ID. Cache the
160 result for a while."
161 (define archived-log
162 (bug-id->log-file bug-id #:archived? #t))
163 (define active-log
164 (bug-id->log-file bug-id))
165 (define file
166 (or (and (file-exists? archived-log) archived-log)
167 (and (file-exists? active-log) active-log)))
168 (if file
169 (let ((key (list 'issue-messages bug-id)))
170 (or (cached? key)
171 (cache! key
172 (call-with-input-file file
173 read-emails-from-bug-log))))
174 '()))
175
176 (define* (search-bugs query #:key (sets '()) (max 400))
177 "Return a list of all bugs matching the given QUERY string.
178 Intersect the result with the id sets in the list SETS."
179 (let* ((ids (delete-duplicates
180 (map string->number
181 (search query))))
182 (filtered (match sets
183 (() ids)
184 (_ (apply lset-intersection eq? ids sets)))))
185 (status-with-cache (if (> (length filtered) max)
186 (take filtered max) filtered))))
187
188 ;; TODO: This returns *any* matching debbugs bug, even if it is not
189 ;; part of the default packages.
190 (define (fetch-bug id)
191 "Return the bug matching ID or #F."
192 (match (soap-invoke* (%config 'debbugs) get-status (list id))
193 (() #f)
194 ((bug) bug)))
195
196 (define (recent-bugs amount)
197 "Return up to AMOUNT bugs with most recent activity."
198 (let* ((recent-ids
199 (sort
200 (delete-duplicates
201 (map string->number
202 (search "mdate:1m..")))
203 <))
204 (ids (take (reverse recent-ids)
205 (min amount (length recent-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 ;; Mu doesn't like punctuation. Replace with spaces. Leave hyphens
234 ;; 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 ;; This is not supported by the Debbugs SOAP service,
247 ;; so we filter locally.
248 (("is" (or "done" "closed"))
249 `(#:terms ,terms
250 #:sets ,(cons (db:bugs-by-status "done") fs)))
251 (("is" (or "open" "pending"))
252 `(#:terms ,terms
253 #:sets ,(cons (db:bugs-by-status "open") fs)))
254 (("date" when)
255 `(#:terms ,(cons (string-append "date:" when) terms)
256 #:sets ,fs))
257 ;; TODO: this should only be the title of the bug, not
258 ;; the subject.
259 (("title" title)
260 `(#:terms
261 ,(cons (string-append "subject:" (clean-term title))
262 terms)
263 #:sets ,fs))
264 (("subject" subject)
265 `(#:terms
266 ,(cons (string-append "subject:" (clean-term subject))
267 terms)
268 #:sets ,fs))
269 (("tag" tag)
270 `(#:terms ,terms
271 #:sets
272 ,(cons (db:bugs-by-tag (clean-term tag)) fs)))
273 (("author" who)
274 `(#:terms
275 ,(cons (string-append "from:" (clean-term who)) terms)
276 #:sets ,fs))
277 (("owner" who)
278 `(#:terms ,terms
279 #:sets ,(cons (db:bugs-by-owner who) fs)))
280 (("submitter" who)
281 `(#:terms ,terms
282 #:sets ,(cons (db:bugs-by-submitter who) fs)))
283 (("severity" level)
284 `(#:terms ,terms
285 #:sets ,(cons (db:bugs-by-severity level) fs)))
286 ((whatever term)
287 `(#:terms ,(cons (clean-term term) terms)
288 #:sets ,fs))
289 (_
290 `(#:terms ,(cons (clean-term term) terms)
291 #:sets ,fs))))))
292 '(#:terms () #:sets ())
293 (string-tokenize query)))