24bd4956b782591e51827a00af7e7804afe3b25f
[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 multipart-message?
46 extract-attachment))
47
48 (define (status-with-cache ids)
49 "Invoke GET-STATUS, but only on those IDS that have not been cached
50 yet. Return new results alongside cached results."
51 (let* ((cached (filter-map cached? ids))
52 (uncached-ids (lset-difference eq?
53 ids
54 (map bug-num cached)))
55 (new (soap-invoke* (%config 'debbugs) get-status uncached-ids)))
56 ;; Cache new things
57 (map (lambda (bug) (cache! (bug-num bug) bug)) new)
58 ;; Return everything from cache
59 (sort (append cached new) (lambda (a b) (< (bug-num a) (bug-num b))))))
60
61 (define-public (extract-name address)
62 (or (assoc-ref address 'name)
63 (and=> (assoc-ref address 'address)
64 (lambda (address)
65 (string-take address (string-index address #\@))))
66 "Somebody"))
67
68 (define-public (extract-email address)
69 (assoc-ref address 'address))
70
71 (define (header message key)
72 (assoc-ref (or (email-headers message) '()) key))
73
74 (define-public (sender message)
75 (or (and=> (header message 'from) first)
76 "unknown sender"))
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 (patch-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 (patch-messages bug-id)
157 "Return list of messages relating to the bug 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 'patch-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 (compose string->number mu:bugid)
179 (mu:message-list 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 ;; TODO: This returns *any* matching debbugs bug, even if it is not
187 ;; part of the default packages.
188 (define (fetch-bug id)
189 "Return the bug matching ID or #F."
190 (match (soap-invoke* (%config 'debbugs) get-status (list id))
191 (() #f)
192 ((bug) bug)))
193
194 (define (recent-bugs amount)
195 "Return up to AMOUNT bugs with most recent activity."
196 (let* ((recent-ids
197 (sort
198 (delete-duplicates
199 (map (compose string->number mu:bugid)
200 (mu:message-list "date:1m..")))
201 <))
202 (ids (take (reverse recent-ids)
203 (min amount (length recent-ids)))))
204 (status-with-cache ids)))
205
206 (define (easy-bugs)
207 "Return all bugs that have been tagged \"easy\"."
208 (let ((ids (db:bugs-by-tag "easy")))
209 (status-with-cache ids)))
210
211 (define (wishlist-bugs)
212 "Return all bugs that have been tagged \"wishlist\"."
213 (let ((ids (db:bugs-by-tag "wishlist")))
214 (status-with-cache ids)))
215
216 (define* (bugs-by-severity severity #:optional status)
217 "Return severe bugs."
218 (let* ((severity-ids (db:bugs-by-severity severity))
219 (ids (if status
220 (let ((status-ids (db:bugs-by-status status)))
221 (lset-intersection eq? severity-ids status-ids))
222 severity-ids)))
223 (status-with-cache ids)))
224
225 (define punctuation?
226 (cut char-set-contains? char-set:punctuation <>))
227
228 (define-public (process-query query)
229 "Process the QUERY string and return a list of query terms and
230 sets that need to overlap the result set."
231 ;; Mu doesn't like punctuation. Replace with spaces.
232 (define (clean-term term)
233 (string-map (match-lambda
234 ((? punctuation? c) #\space)
235 (c c))
236 term))
237 (fold (lambda (term acc)
238 (match acc
239 ((#:terms terms
240 #:sets fs)
241 (match (string-split term #\:)
242 ;; This is not supported by the Debbugs SOAP service,
243 ;; so we filter locally.
244 (("is" (or "done" "closed"))
245 `(#:terms ,terms
246 #:sets ,(cons (db:bugs-by-status "done") fs)))
247 (("is" (or "open" "pending"))
248 `(#:terms ,terms
249 #:sets ,(cons (db:bugs-by-status "open") fs)))
250 (("date" when)
251 `(#:terms ,(cons (string-append "date:" when) terms)
252 #:sets ,fs))
253 ;; TODO: this should only be the title of the bug, not
254 ;; the subject.
255 (("title" title)
256 `(#:terms
257 ,(cons (string-append "subject:" (clean-term title))
258 terms)
259 #:sets ,fs))
260 (("subject" subject)
261 `(#:terms
262 ,(cons (string-append "subject:" (clean-term subject))
263 terms)
264 #:sets ,fs))
265 (("tag" tag)
266 `(#:terms ,terms
267 #:sets
268 ,(cons (db:bugs-by-tag (clean-term tag)) fs)))
269 (("author" who)
270 `(#:terms
271 ,(cons (string-append "from:" (clean-term who)) terms)
272 #:sets ,fs))
273 (("owner" who)
274 `(#:terms ,terms
275 #:sets ,(cons (db:bugs-by-owner who) fs)))
276 (("submitter" who)
277 `(#:terms ,terms
278 #:sets ,(cons (db:bugs-by-submitter who) fs)))
279 (("severity" level)
280 `(#:terms ,terms
281 #:sets ,(cons (db:bugs-by-severity level) fs)))
282 ((whatever term)
283 `(#:terms ,(cons (clean-term term) terms)
284 #:sets ,fs))
285 (_
286 `(#:terms ,(cons (clean-term term) terms)
287 #:sets ,fs))))))
288 '(#:terms () #:sets ())
289 (string-tokenize query)))