Add handler for downloading patch sets.
[software/mumi.git] / scripts / mumi.in
1 #!@GUILE@ --no-auto-compile
2 -*- scheme -*-
3 -*- geiser-scheme-implementation: guile -*-
4 !#
5 ;;; mumi -- Mediocre, uh, mail interface
6 ;;; Copyright © 2016, 2017, 2019, 2020 Ricardo Wurmus <rekado@elephly.net>
7 ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
8 ;;;
9 ;;; This file is part of mumi.
10 ;;;
11 ;;; mumi is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or
14 ;;; (at your option) any later version.
15 ;;;
16 ;;; mumi is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;;; General Public License for more details.
20 ;;;
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with mumi. If not, see <http://www.gnu.org/licenses/>.
23
24 (use-modules (srfi srfi-1)
25 (srfi srfi-37)
26 (system repl server)
27 (ice-9 match)
28 (ice-9 format)
29 (mumi config)
30 ((mumi debbugs)
31 #:select (extract-bug-numbers))
32 ((mumi jobs)
33 #:select (worker-loop))
34 ((mumi web server)
35 #:select (start-mumi-web-server))
36 ((mumi xapian)
37 #:select (index!)))
38
39 (define %default-repl-server-port
40 ;; Default port to run REPL server on, if --listen-repl is provided
41 ;; but no port is mentioned
42 37146)
43
44 ;; Keep indexing the mail directory
45 (define %update-interval 30)
46
47 (define update-state!
48 (let ((count -1))
49 (lambda* (#:key loop?)
50 (set! count (remainder (1+ count) 10))
51 (catch #t
52 (lambda ()
53 (when (zero? count)
54 (display "Starting full indexing." (current-error-port))
55 (newline (current-error-port)))
56 (index! #:full? (zero? count))
57 (and loop?
58 (begin
59 (format (current-error-port)
60 "Sleeping for ~a seconds." %update-interval)
61 (sleep %update-interval)
62 (update-state!))))
63 (lambda args
64 (format (current-error-port) "worker error: ~a~%" args)
65 (sleep %update-interval)
66 (update-state!))))))
67
68 (define %options
69 ;; Specifications of the command-line options
70 (list (option '("listen-repl") #f #t
71 (lambda (opt name arg result)
72 (let ((port (cond (arg => string->number)
73 (else %default-repl-server-port))))
74 (if port
75 (alist-cons 'listen-repl port
76 (alist-delete 'listen-repl result))
77 (error "invalid REPL server port" arg)))))
78 (option '("disable-mailer") #f #f
79 (lambda (opt name arg result)
80 (alist-cons 'disable-mailer #t result)))
81 (option '("sender") #t #f
82 (lambda (opt name arg result)
83 (alist-cons 'sender arg result)))
84 (option '("smtp") #t #f
85 (lambda (opt name arg result)
86 (alist-cons 'smtp arg result)))))
87
88 (define %default-options
89 ;; Alist of default option values
90 `((listen-repl . #f)
91 (smtp . #f)
92 (sender . #f)
93 (disable-mailer . #f)))
94
95 (define (parse-options args)
96 (args-fold
97 args %options
98 (lambda (opt name arg result)
99 (error "unrecognized option" name))
100 (lambda (arg result)
101 (error "extraneous argument" arg))
102 %default-options))
103
104 (define (show-mumi-usage)
105 (format (current-error-port)
106 "
107 `mumi web [--listen-repl[=port]] [--disable-mailer]':
108 start the application web server.
109
110 `mumi mailer --sender=SENDER --smtp=SMTP:
111 start a mailer process (requires Redis).
112
113 `mumi worker':
114 run an update loop to refresh issue information from Debbugs.
115
116 `mumi fetch':
117 index all Debbugs bug logs and update bug statuses once.
118
119 ~%")
120 (exit 1))
121
122 (match (cdr (program-arguments))
123 (("mailer" . rest)
124 (let* ((opts (parse-options rest))
125 (sender (assoc-ref opts 'sender))
126 (smtp (assoc-ref opts 'smtp)))
127 (if (and sender smtp)
128 (worker-loop opts)
129 (error "Both sender and smtp options must be provided!"))))
130 (("fetch")
131 (update-state! #:loop? #f))
132 (("worker")
133 (update-state! #:loop? #t))
134 (("web" . rest)
135 (let ((opts (parse-options rest)))
136 (parameterize ((mailer-enabled? (not (assoc-ref opts 'disable-mailer))))
137 (let ((repl-port (assoc-ref opts 'listen-repl)))
138 (when repl-port
139 (spawn-server (make-tcp-server-socket #:port repl-port))))
140 (start-mumi-web-server 1234))))
141 (_ (show-mumi-usage)))