messages: Remove bug-id->mbox-file.
[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 bugs) #:prefix db:)
37 #:use-module (web client)
38 #:export (search-bugs
39 fetch-bug
40 recent-bugs
41 easy-bugs
42 wishlist-bugs
43 bugs-by-severity
44
45 bug-id->log-file
46
47 multipart-message?
48 extract-attachment))
49
50 (define (status-with-cache ids)
51 "Invoke GET-STATUS, but only on those IDS that have not been cached
52 yet. Return new results alongside cached results."
53 (let* ((cached (filter-map cached? ids))
54 (uncached-ids (lset-difference eq?
55 ids
56 (map bug-num cached)))
57 (new (soap-invoke* (%config 'debbugs) get-status uncached-ids)))
58 ;; Cache new things
59 (map (lambda (bug) (cache! (bug-num bug) bug)) new)
60 ;; Return everything from cache
61 (sort (append cached new) (lambda (a b) (< (bug-num a) (bug-num b))))))
62
63 (define-public (extract-name address)
64 (or (assoc-ref address 'name)
65 (and=> (assoc-ref address 'address)
66 (lambda (address)
67 (string-take address (string-index address #\@))))
68 "Somebody"))
69
70 (define-public (extract-email address)
71 (assoc-ref address 'address))
72
73 (define (header message key)
74 (assoc-ref (or (email-headers message) '()) key))
75
76 (define-public (sender message)
77 (or (and=> (header message 'from) first)
78 "unknown sender"))
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 (patch-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 (define* (bug-id->log-file bug-id #:key archived?)
157 (format #f "~a/spool/~a/~a/~a.log"
158 (%config 'data-dir)
159 (if archived? "archive" "db-h")
160 (string-take-right (if (string? bug-id)
161 bug-id
162 (number->string bug-id)) 2)
163 bug-id))
164
165 ;; This is a modified version of fetch-mbox from guile-debbugs: it
166 ;; supports downloading with an offset. It also doesn't bother with
167 ;; msg-nums.
168 (define* (fetch-mbox* instance bug-number #:optional mbox-type
169 #:key offset mdate)
170 "Download the mbox containing messages of bug BUG-NUMBER from the
171 Debbugs server INSTANCE (a procedure returning a string when given the
172 symbol 'email)."
173 (let* ((options
174 `((bug . ,(number->string bug-number))
175 ,@(cond
176 ((member mbox-type '(mboxmaint mboxstat mboxstatus))
177 `((mbox-type . "yes")))
178 (else '()))
179 (mbox . "yes")))
180 (uri (string-append (instance 'email) "?"
181 (string-join (map (match-lambda
182 ((key . value)
183 (format #f "~a=~a" key value)))
184 options)
185 ";")))
186 (headers `(,@(if offset
187 `((range . (bytes (,offset . #f))))
188 '())
189 ,@(if mdate
190 `((if-modified-since . ,mdate))
191 '()))))
192 (http-get uri #:decode-body? #f #:headers headers)))
193
194 ;; We would like to use get-bug-log here, but it often returns
195 ;; truncated messages. This is a known bug upstream.
196 (define-public (patch-messages bug-id)
197 "Return list of messages relating to the bug BUG-ID. Cache the
198 result for a while."
199 (define archived-log
200 (bug-id->log-file bug-id #:archived? #t))
201 (define active-log
202 (bug-id->log-file bug-id))
203 (define file
204 (or (and (file-exists? archived-log) archived-log)
205 (and (file-exists? active-log) active-log)))
206 (if file
207 (let ((key (list 'patch-messages bug-id)))
208 (or (cached? key)
209 (cache! key
210 (call-with-input-file file
211 read-emails-from-bug-log))))
212 '()))
213
214 (define* (search-bugs query #:key (sets '()) (max 400))
215 "Return a list of all bugs matching the given QUERY string.
216 Intersect the result with the id sets in the list SETS."
217 (let* ((ids (delete-duplicates
218 (map (compose string->number mu:bugid)
219 (mu:message-list query))))
220 (filtered (match sets
221 (() ids)
222 (_ (apply lset-intersection eq? ids sets)))))
223 (status-with-cache (if (> (length filtered) max)
224 (take filtered max) filtered))))
225
226 ;; TODO: This returns *any* matching debbugs bug, even if it is not
227 ;; part of the default packages.
228 (define (fetch-bug id)
229 "Return the bug matching ID or #F."
230 (match (soap-invoke* (%config 'debbugs) get-status (list id))
231 (() #f)
232 ((bug) bug)))
233
234 (define (recent-bugs amount)
235 "Return up to AMOUNT bugs with most recent activity."
236 (let* ((recent-ids
237 (sort
238 (delete-duplicates
239 (map (compose string->number mu:bugid)
240 (mu:message-list "date:1m..")))
241 <))
242 (ids (take (reverse recent-ids)
243 (min amount (length recent-ids)))))
244 (status-with-cache ids)))
245
246 (define (easy-bugs)
247 "Return all bugs that have been tagged \"easy\"."
248 (let ((ids (db:bugs-by-tag "easy")))
249 (status-with-cache ids)))
250
251 (define (wishlist-bugs)
252 "Return all bugs that have been tagged \"wishlist\"."
253 (let ((ids (db:bugs-by-tag "wishlist")))
254 (status-with-cache ids)))
255
256 (define* (bugs-by-severity severity #:optional status)
257 "Return severe bugs."
258 (let* ((severity-ids (db:bugs-by-severity severity))
259 (ids (if status
260 (let ((status-ids (db:bugs-by-status status)))
261 (lset-intersection eq? severity-ids status-ids))
262 severity-ids)))
263 (status-with-cache ids)))
264
265 (define punctuation?
266 (cut char-set-contains? char-set:punctuation <>))
267
268 (define-public (process-query query)
269 "Process the QUERY string and return a list of query terms and
270 sets that need to overlap the result set."
271 ;; Mu doesn't like punctuation. Replace with spaces.
272 (define (clean-term term)
273 (string-map (match-lambda
274 ((? punctuation? c) #\space)
275 (c c))
276 term))
277 (fold (lambda (term acc)
278 (match acc
279 ((#:terms terms
280 #:sets fs)
281 (match (string-split term #\:)
282 ;; This is not supported by the Debbugs SOAP service,
283 ;; so we filter locally.
284 (("is" (or "done" "closed"))
285 `(#:terms ,terms
286 #:sets ,(cons (db:bugs-by-status "done") fs)))
287 (("is" (or "open" "pending"))
288 `(#:terms ,terms
289 #:sets ,(cons (db:bugs-by-status "open") fs)))
290 (("date" when)
291 `(#:terms ,(cons (string-append "date:" when) terms)
292 #:sets ,fs))
293 ;; TODO: this should only be the title of the bug, not
294 ;; the subject.
295 (("title" title)
296 `(#:terms
297 ,(cons (string-append "subject:" (clean-term title))
298 terms)
299 #:sets ,fs))
300 (("subject" subject)
301 `(#:terms
302 ,(cons (string-append "subject:" (clean-term subject))
303 terms)
304 #:sets ,fs))
305 (("tag" tag)
306 `(#:terms ,terms
307 #:sets
308 ,(cons (db:bugs-by-tag (clean-term tag)) fs)))
309 (("author" who)
310 `(#:terms
311 ,(cons (string-append "from:" (clean-term who)) terms)
312 #:sets ,fs))
313 (("owner" who)
314 `(#:terms ,terms
315 #:sets ,(cons (db:bugs-by-owner who) fs)))
316 (("submitter" who)
317 `(#:terms ,terms
318 #:sets ,(cons (db:bugs-by-submitter who) fs)))
319 (("severity" level)
320 `(#:terms ,terms
321 #:sets ,(cons (db:bugs-by-severity level) fs)))
322 ((whatever term)
323 `(#:terms ,(cons (clean-term term) terms)
324 #:sets ,fs))
325 (_
326 `(#:terms ,(cons (clean-term term) terms)
327 #:sets ,fs))))))
328 '(#:terms () #:sets ())
329 (string-tokenize query)))