summaryrefslogtreecommitdiff
path: root/mumi/debbugs.scm
blob: 1f6d4304419a7026a3ed55682ecc1b62eb98243a (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
;;; mumi -- Mediocre, uh, mail interface
;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (mumi debbugs)
  #:use-module (mumi config)
  #:use-module (mumi cache)
  #:use-module (email email)
  #:use-module (email quoted-printable)
  #:use-module (rnrs bytevectors)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-2)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-19)
  #:use-module (ice-9 match)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 regex)
  #:export (read-emails-from-bug-log
            extract-bug-numbers
            forgotten-bug-numbers
            bug-id->log-file
            bug-id->summary-file

            bug-status
            bug-status?

            bug-num
            bug-archived
            bug-blockedby
            bug-blocks
            bug-date
            bug-done
            bug-mergedwith
            bug-originator
            bug-owner
            bug-severity
            bug-subject
            bug-tags))

(define* (read-emails-from-bug-log port
                                   #:optional (keep '(incoming-recv)))
  "Read the Debbugs bug log from PORT and return a list of parsed
email objects.  According to the documentation of the Perl module
Debbugs::Log there are four record types that are separated with
single control characters on a line of their own.

* autocheck

This is for auto-forwarded messages.  The mail is recorded between a
leading ^A and a trailing ^C.

* recips

This is for emails with an additional list of recipients.  The
recipients list may be just the string \"-t\", which represents the
same sendmail option, indicating that the recipients are taken from
the message headers.  This record starts with ^B, uses ^D as an inline
separator for recipients, and uses ^E to mark the beginning of the
email.  The email ends with ^C.

* html

This is for raw HTML snippets.  They are recorded between ^F and ^C.
They are only used for the Debbugs web interface.

* incoming-recv

This is for received emails.  The raw email is introduced by ^G and
ends with ^C.
"
  (let loop ((msgids (make-hash-table))
             (mails '())
             (lines '())
             (type 'init)
             (skip? #f))
    (let ((line (read-line port)))
      (if (eof-object? line) (reverse mails)
          (match (and (not (string-null? line))
                      (string-ref line 0))
            ;; Ctrl-A, autocheck
            (#\soh
             (loop msgids mails lines 'autocheck #f))
            ;; Ctrl-B, recips: skip until beginning of email
            (#\stx
             (loop msgids mails lines 'recips #t))
            ;; Ctrl-C, end of record
            (#\etx
             (let ((mails*
                    (if (member type keep)
                        ;; TODO: This is very ugly.  The first few
                        ;; lines of the raw messages stored in Debbugs
                        ;; logs seem to confuse the email parser, so we
                        ;; try to strip them off.
                        (let* ((content (string-join
                                         (drop-while (lambda (line)
                                                       (or (string-prefix? "From " line)
                                                           (string-prefix? "Received" line)
                                                           (string-prefix? "\t" line)
                                                           (string-prefix? " " line)))
                                                     (reverse lines)) "\n"))
                               (mail (catch #t
                                       (lambda ()
                                         (parse-email content))
                                       (lambda args
                                         (format (current-error-port)
                                                 "failed to process email~%")
                                         #f))))
                          (let ((id (and mail (assoc-ref (email-headers mail) 'message-id))))
                            (if (and id (not (hash-ref msgids id)))
                                (begin
                                  (hash-set! msgids id #t)
                                  (cons mail mails))
                                mails)))
                        mails)))
               (loop msgids mails* '() 'init #f)))
            ;; Ctrl-E, beginning of email in recips
            (#\enq
             (loop msgids mails lines 'recips #f))
            ;; Ctrl-F, raw HTML snippet: skip
            (#\ack
             (loop msgids mails lines 'html #t))
            ;; Ctrl-G, incoming-recv
            (#\bel
             (loop msgids mails lines 'incoming-recv #f))
            (_
             (if skip?
                 (loop msgids mails lines type skip?)
                 (loop msgids mails (cons line lines) type skip?))))))))

(define* (filter-index pred proc #:key archived?)
  "Open up a Debbugs index file and collect the result of applying
PROC to those lines that match the predicate PRED.  If ARCHIVED? is #T
search the index of archived issues."
  (define index-file
    (format #f "~a/spool/index.~a.realtime" (%config 'data-dir)
            (if archived? "archive" "db")))
  (call-with-input-file index-file
    (lambda (port)
      (let loop ((result '()))
        (match (read-line port)
          ((? eof-object? x) result)
          (line
           (cond
            ((pred line)
             (loop (cons (proc line) result)))
            (else (loop result)))))))))

(define* (extract-bug-numbers packages #:key archived?)
  "Open up a Debbugs index file and return the bug numbers for those
lines that match one of PACKAGES.  If ARCHIVED? is #T search the index
of archived issues."
  (define cache-key
    (list 'extract-bug-numbers packages archived?))
  (define cached (cached? cache-key))
  (or cached
      (let ((result
             (filter-index (lambda (line)
                             (any (lambda (package)
                                    (string-prefix? package line))
                                  packages))
                           (lambda (line)
                             (second (string-split line #\space)))
                           #:archived? archived?)))
        (cache! cache-key result)
        result)))

(define* (forgotten-bug-numbers packages #:key (seconds-ago (* 60 60 24 30)))
  "Return the numbers of issues that are open but haven't seen any
activity for a while.  The duration is given by SECONDS-AGO, which
defaults to 30 days."
  (define threshold
    (number->string
     (time-second
      (subtract-duration (current-time)
                         (make-time time-duration 0 seconds-ago)))))
  (define cache-key
    (list 'forgotten-bug-numbers packages seconds-ago))
  (define cached (cached? cache-key))
  (or cached
      (let ((result
             (filter-index (lambda (line)
                             (any (lambda (package)
                                    (and (string-prefix? package line)
                                         (let ((fields (string-split line #\space)))
                                           (and (string=? "open" (fourth fields))
                                                (string< (third fields) threshold)))))
                                  packages))
                           (lambda (line)
                             (second (string-split line #\space))))))
        (cache! cache-key result)
        result)))

(define* (bug-id->log-file bug-id #:key archived?)
  (format #f "~a/spool/~a/~a/~a.log"
          (%config 'data-dir)
          (if archived? "archive" "db-h")
          (string-take-right (if (string? bug-id)
                                 bug-id
                                 (number->string bug-id)) 2)
          bug-id))

(define (bug-id->summary-file bug-id)
  (let ((candidate (lambda (archived?)
                     (format #f "~a/spool/~a/~a/~a.summary"
                             (%config 'data-dir)
                             (if archived? "archive" "db-h")
                             (string-take-right (if (string? bug-id)
                                                    bug-id
                                                    (number->string bug-id)) 2)
                             bug-id))))
    (find file-exists?
          (list (candidate #f)
                (candidate #t)))))


(define-record-type <bug-status>
  (make-bug-status num
                   archived blockedby blocks date done mergedwith
                   originator owner severity subject tags)
  bug-status?
  (num bug-num)
  (archived bug-archived)
  (blockedby bug-blockedby)
  (blocks bug-blocks)
  (date bug-date)
  (done bug-done)
  (mergedwith bug-mergedwith)
  (originator bug-originator)
  (owner bug-owner)
  (severity bug-severity)
  (subject bug-subject)
  (tags bug-tags))

(define qp-pattern "=\\?UTF-8\\?Q\\?([^?]+)\\?=")
(define (bug-status bug-id)
  (and-let* ((bug-id (if (number? bug-id) bug-id
                         (string->number bug-id)))
             (file (bug-id->summary-file bug-id))
             (properties
              (call-with-input-file file
                (lambda (port)
                  (let loop ((props '()))
                    (let ((line (read-line port)))
                      (if (eof-object? line) props
                          (let* ((split-point (string-index line #\:))
                                 (key (string-take line split-point))
                                 (value (string-drop line (1+ split-point))))
                            (loop (cons (cons key
                                              (string-trim-both value))
                                        props))))))))))
    (make-bug-status bug-id
                     (string-contains "/archive/" file)
                     (assoc-ref properties "Blocked-By")
                     (assoc-ref properties "Blocks")
                     (time-monotonic->date
                      (make-time time-monotonic
                                 0
                                 (string->number (assoc-ref properties "Date"))))
                     (assoc-ref properties "Done")
                     (assoc-ref properties "Merged-With")
                     (assoc-ref properties "Submitter")
                     (assoc-ref properties "Owner")
                     (assoc-ref properties "Severity")
                     (let ((subject (assoc-ref properties "Subject")))
                       (if (string-contains subject "=?UTF-8?Q?")
                           (or (false-if-exception
                                (utf8->string
                                 (quoted-printable-decode
                                  (regexp-substitute/global #f qp-pattern
                                                            subject 'pre 1 'post))))
                               subject)
                           subject))
                     (assoc-ref properties "Tags"))))