summaryrefslogtreecommitdiff
path: root/mumi/web/controller.scm
blob: 10252dd04ec8eb8d41d593fe7a2ec7fbd6fbfef3 (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
;;; mumi -- Mediocre, uh, mail interface
;;; Copyright © 2016-2022 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2022 Arun Isaac <arunisaac@systemreboot.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 web controller)
  #:use-module (ice-9 match)
  #:use-module (ice-9 format)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (web request)
  #:use-module (web uri)
  #:use-module (webutils sessions)
  #:use-module (gcrypt base64)
  #:use-module (gcrypt mac)
  #:use-module (mumi config)
  #:use-module ((mumi debbugs)
                #:select (bug-status bug-archived bug-subject bug-done))
  #:use-module (mumi jobs)
  #:use-module (mumi messages)
  #:use-module (mumi web render)
  #:use-module (mumi web download)
  #:use-module (mumi web graphql)
  #:use-module (mumi web util)
  #:use-module (mumi web view html)
  #:use-module ((mumi xapian) #:select (search))
  #:export (controller))

(define-syntax-rule (-> target functions ...)
  (fold (lambda (f val) (and=> val f))
        target
        (list functions ...)))

(define (%session-manager)
  (let ((key-file (string-append (%config 'key-dir) "/signing-key")))
    (unless (file-exists? key-file)
      (with-output-to-file key-file
        (lambda () (write (base64-encode (generate-signing-key))))))
    (make-session-manager
     (with-input-from-file key-file read)
     ;; expire session after 30 mins
     #:expire-delta '(0 0 30))))

(define (controller request body)
  (match-lambda
    (('GET)
     (render-html (index)
                  #:extra-headers
                  '((cache-control . ((max-age . 60))))))
    (('GET "easy")
     (render-html (list-of-matching-bugs "tag:easy" (easy-bugs))))
    (('GET "recent")
     (render-html (list-of-recent-issues)))
    (('GET "forgotten")
     (render-html (list-of-forgotten-issues)))
    (('GET "wishlist")
     (render-html
      (list-of-matching-bugs "severity:wishlist is:open"
                             (bugs-by-severity "wishlist" "open"))))
    (((or 'GET 'POST) "graphql")
     (handle-graphql request body))
    (('GET "search")
     (let ((query (-> request
                      request-uri
                      uri-query
                      parse-query-string
                      (cut assoc-ref <> "query"))))
       (cond
        ;; TODO: query should not be empty!
        ((or (not query)
             (string-null? (string-trim query)))
         (redirect '()))

        ;; For convenience
        ((string-prefix? "id:" query) =>
         (lambda _ (redirect (list "issue" (string-drop query (string-length "id:"))))))
        ((string-prefix? "#" query) =>
         (lambda _ (redirect (list "issue" (string-drop query (string-length "#"))))))
        ((string->number query) =>
         (lambda _ (redirect (list "issue" query))))

        ;; Search for matching messages and return list of bug reports
        ;; that belong to them.
        (else
         (render-html
          (list-of-matching-bugs query
                                 (search-bugs (string-join
                                               (process-query query)))))))))
    ((or ('GET "issue" (? string->number id))
         ('GET (? string->number id)))
     (let ((bug (bug-status id))
           (message (match (uri-query (request-uri request))
                      ("comment-ok"
                       '(info . "Your comment has been submitted!"))
                      ("comment-error"
                       '(error . "There was an error submitting your comment!"))
                      (_ #f))))
       (if bug
           ;; Record the current issue id in an encrypted cookie.
           ;; This will be verified when posting a comment.
           (let* ((cookie-header
                   (set-session (%session-manager) `((issue-id . ,id))))
                  (headers
                   (cond
                    ((bug-archived bug)
                     ;; Tell browser to cache this for 12 hours.
                     (cons cookie-header
                           '((cache-control . ((max-age . 43200))))))
                    ((bug-done bug)
                     ;; Tell browser to cache this for 1 hour.
                     (cons cookie-header
                           '((cache-control . ((max-age . 3600))))))
                    (else (list cookie-header))))
                  (page (issue-page bug message)))
             (if page
                 (render-html page #:extra-headers headers)
                 (render-html (unknown id))))
           (render-html (unknown id)))))
    (('GET "msgid" msgid)
     (match (search (format #false "msgid:~a" (string-hash msgid)))
       ((id . rest)
        (redirect (list "issue" id)))
       (_ (render-html (unknown msgid)))))
    (('POST "issue" (? string->number id) "comment")
     (if (mailer-enabled?)
         (let ((headers   (request-headers request))
               (form-data (parse-form-submission request body))
               (cookie    (or (session-data (%session-manager) request)
                              '()))
               (bug       (bug-status id)))
           (if (and
                bug
                ;; The encrypted cookie must be fresh and contain the
                ;; current issue id.
                (and=> (assoc-ref cookie 'issue-id)
                       (cut string=? id <>))
                ;; The honeypot field "validation" must remain empty
                (let ((val (assoc-ref form-data 'validation)))
                  (and val (string-null? (string-trim-both val))))
                ;; Submission may not have happened too quickly
                (let ((time (assoc-ref form-data 'timestamp)))
                  (and time (reasonable-timestamp? time)))
                ;; Message must not be too short
                (and=> (assoc-ref form-data 'text)
                       (lambda (text)
                         (> (string-length (string-trim-both text)) 10)))
                ;; Message must have sender
                (and=> (assoc-ref form-data 'from)
                       (compose (negate string-null?) string-trim-both)))
               (begin
                 ;; Send comment to list
                 (enqueue 'mail
                          `((from . ,(string-trim-both (assoc-ref form-data 'from)))
                            (subject . ,(bug-subject bug))
                            (to . ,(format #f "~a@~a"
                                           id (%config 'debbugs-domain)))
                            (text . ,(assoc-ref form-data 'text))))
                 (redirect (list "issue" id) "comment-ok"))
               (redirect (list "issue" id) "comment-error")))
         (redirect (list "issue" id) "comment-error")))
    (('GET "issue" (? string->number id)
           "attachment" (? string->number msg-num)
           (? string->number path) ...)
     (handle-download (string->number id)
                      (string->number msg-num)
                      (map string->number path)))
    (('GET "issue" (? string->number id)
           "patch-set" . patch-set-num)
     (let* ((patch-set-num* (match patch-set-num
                              (() #false)
                              ((f . rest) f)))
            (content (patch-messages id patch-set-num*)))
       (if content
           (list `((content-type text)
                   (content-disposition
                    text/plain
                    (filename
                     . ,(format #f "~a-patch-set~:[-~a~;~].mbox"
                                id patch-set-num* patch-set-num*))))
                 content)
           (not-found (request-uri request)))))
    (('GET "issue" (? string->number id)
           "raw" (? string->number msg-num))
     (download-raw (string->number id)
                   (string->number msg-num)))
    (('GET "issue" not-an-id)
     (render-html (unknown not-an-id)))
    (('GET "help")
     (render-html (help)
                  ;; Cache for 24 hours.
                  #:extra-headers
                  '((cache-control . ((max-age . 86400))))))
    (('GET path ...)
     (render-static-asset request))))