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>
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.
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.
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/>.
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
(debbugs cache
)
29 #:use-module
(debbugs soap
)
30 #:use-module
(debbugs operations
)
31 #:use-module
(debbugs bug
)
32 #:use-module
(email email
)
33 #:use-module
(mumi config
)
34 #:use-module
((mumi bugs
) #:prefix db
:)
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?
53 (map bug-num cached
)))
54 (new (soap-invoke* (%config
'debbugs
) get-status uncached-ids
)))
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
))))))
60 (define-public (extract-name address
)
61 (or (assoc-ref address
'name
)
62 (and=> (assoc-ref address
'address
)
64 (string-take address
(string-index address
#\
@))))
67 (define-public (extract-email address
)
68 (assoc-ref address
'address
))
70 (define (header message key
)
71 (assoc-ref (email-headers message
) key
))
73 (define-public (sender message
)
74 (first (header message
'from
)))
76 (define-public sender-email
77 (compose extract-email sender
))
79 (define-public (sender-name message
)
80 (extract-name (sender message
)))
82 (define-public (date message
)
83 (header message
'date
))
85 (define-public (subject message
)
86 (or (header message
'subject
) "(no subject)"))
88 (define-public (message-id message
)
89 (header message
'message-id
))
91 (define-public (participants messages
)
92 "Return a list of unique senders in the conversion."
93 (apply lset-adjoin
(lambda (a b
)
94 (string= (extract-email a
)
96 '() (map sender messages
)))
98 (define-public (recipients message
)
99 "Return a list of recipient email addresses for the given MESSAGE."
100 (let ((headers (email-headers message
)))
101 (filter-map (match-lambda
102 (((or 'cc
'bcc
'to
) val
) val
)
105 (define-public (closing? message id
)
106 "Is this MESSAGE closing this bug ID?"
107 (let ((done (string-append (number->string id
)
109 (string= (header message
'x-debbugs-envelope-to
) done
)))
111 (define-public (bot? address
)
112 (string= "help-debbugs@gnu.org" address
))
114 (define-public (internal-message? message
)
115 (bot?
(sender-email message
)))
118 (define (multipart-message? message
)
119 (eq?
(assoc-ref (header message
'content-type
)
123 (define (extract-attachment id msg-num path
)
124 "Extract attachment from message number MSG-NUM in the thread for
125 the bug with the given ID. Follow PATH to get to the correct
126 multipart chunk containing the attachment. This is absolutely
127 horrible because Debbugs does not let us access messages directly, so
128 we have to do this in a very convoluted way."
130 (and (< n
(length lst
))
132 (define (traverse path parts
)
133 (let loop
((path path
)
136 ((pos) (nth pos parts
))
139 (and=> (nth pos parts
)
140 mime-entity-body
))))))
141 (and=> (fetch-bug id
)
143 (and=> (nth msg-num
(patch-messages id
))
146 ((multipart-message? msg
)
147 (traverse path
(email-body msg
)))
154 (define (download-message bug-id msg-num
)
155 "Download message number MSG-NUM of bug BUG-ID and store it in the
156 mail directory if it's not already there. Return the name of the
158 (let ((key (list 'download-message bug-id msg-num
)))
161 (let ((file-name (format #f
"~a/cur/~a-~a"
164 (if (file-exists? file-name
) file-name
166 (format (current-error-port)
167 "downloading ~a~%" file-name
)
170 (fetch-mbox (%config
'debbugs
)
171 bug-id msg-num
#:streaming?
#t
))
172 (lambda (response port
)
173 (with-output-to-file file-name
175 (put-bytevector (current-output-port)
176 (get-bytevector-all port
))))
179 ;; Reset mtime, because mu uses it!
180 (with-input-from-file file-name
182 (match (mbox->emails
(current-input-port))
184 (let* ((mail (parse-email email
))
187 (date->time-monotonic
195 ;; We would like to use get-bug-log here, but it often returns
196 ;; truncated messages. This is a known bug upstream.
197 (define-public (patch-messages bug-id
)
198 "Return list of messages relating to the bug BUG-ID. Cache the
200 (let ((key (list 'patch-messages bug-id
)))
204 (soap-invoke* (%config
'debbugs
) get-bug-message-numbers bug-id
)
206 (map (lambda (msg-num)
207 (with-input-from-file (download-message bug-id msg-num
)
209 (match (mbox->emails
(current-input-port))
210 ((email) (parse-email email
))
214 (define* (search-bugs query
#:key
(sets '()) (max 100))
215 "Return a list of all bugs matching the given QUERY string.
216 Interset 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
222 (_ (apply lset-intersection eq? ids sets
)))))
223 (status-with-cache (if (> (length filtered
) max
)
224 (take filtered max
) filtered
))))
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
))
234 (define (recent-bugs amount
)
235 "Return up to AMOUNT bugs with most recent activity."
238 (map (compose string-
>number mu
:bugid
)
239 (mu:message-list
(string-append "date:1m..")))))
240 (ids (take recent-ids
(min amount
(length recent-ids
)))))
241 (status-with-cache ids
)))
244 "Return all bugs that have been tagged \"easy\
"."
245 (let ((ids (db:bugs-by-tag
"easy")))
246 (status-with-cache ids
)))
248 (define* (bugs-by-severity severity
#:optional status
)
249 "Return severe bugs."
250 (let* ((severity-ids (db:bugs-by-severity severity
))
252 (let ((status-ids (db:bugs-by-status status
)))
253 (lset-intersection eq? severity-ids status-ids
))
255 (status-with-cache ids
)))
258 (cut char-set-contains? char-set
:punctuation
<>))
260 (define-public (process-query query
)
261 "Process the QUERY string and return a list of query terms and
262 sets that need to overlap the result set."
263 ;; Mu doesn't like punctuation. Replace with spaces.
264 (define (clean-term term
)
265 (string-map (match-lambda
266 ((? punctuation? c
) #\space
)
269 (fold (lambda (term acc
)
273 (match (string-split term
#\
:)
274 ;; This is not supported by the Debbugs SOAP service,
275 ;; so we filter locally.
276 (("is" (or "done" "closed"))
278 #:sets
,(cons (db:bugs-by-status
"done") fs
)))
279 (("is" (or "open" "pending"))
281 #:sets
,(cons (db:bugs-by-status
"open") fs
)))
283 `(#:terms
,(cons (string-append "date:" when
) terms
)
285 ;; TODO: this should only be the title of the bug, not
289 ,(cons (string-append "subject:" (clean-term title
))
294 ,(cons (string-append "subject:" (clean-term subject
))
300 ,(cons (db:bugs-by-tag
(clean-term tag
)) fs
)))
303 ,(cons (string-append "from:" (clean-term who
)) terms
)
307 #:sets
,(cons (db:bugs-by-owner who
) fs
)))
310 #:sets
,(cons (db:bugs-by-submitter who
) fs
)))
313 #:sets
,(cons (db:bugs-by-severity level
) fs
)))
315 `(#:terms
,(cons (clean-term term
) terms
)
318 `(#:terms
,(cons (clean-term term
) terms
)
320 '(#:terms
() #:sets
())
321 (string-tokenize query
)))