2abfd513fb175244a58bd1696edb675dd6460dba
[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 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-26)
26 (srfi srfi-37)
27 (system repl server)
28 (ice-9 match)
29 (ice-9 threads)
30 (mumi config)
31 (mumi messages)
32 (mumi web server)
33 (debbugs))
34
35 (define %default-repl-server-port
36 ;; Default port to run REPL server on, if --listen-repl is provided
37 ;; but no port is mentioned
38 37146)
39
40 (define %options
41 ;; Specifications of the command-line options
42 (list (option '("listen-repl") #f #t
43 (lambda (opt name arg result)
44 (let ((port (cond (arg => string->number)
45 (else %default-repl-server-port))))
46 (if port
47 (alist-cons 'listen-repl port
48 (alist-delete 'listen-repl result))
49 (error "invalid REPL server port" arg)))))
50 (option '("fetch") #f #f
51 (lambda (opt name arg result)
52 (alist-cons 'fetch #t result)))))
53
54 (define %default-options
55 ;; Alist of default option values
56 `((listen-repl . #f)
57 (fetch . #f)))
58
59 (define (parse-options args)
60 (args-fold
61 args %options
62 (lambda (opt name arg result)
63 (error "unrecognized option" name))
64 (lambda (arg result)
65 (error "extraneous argument" arg))
66 %default-options))
67
68 (let ((opts (parse-options (cdr (program-arguments)))))
69 (cond
70 ((assoc-ref opts 'fetch)
71 (let ((bug-nums
72 (sort (append-map (lambda (package)
73 (soap-invoke (%config 'debbugs)
74 get-bugs
75 `((package . ,package))))
76 (%config 'packages)) <)))
77 (for-each (lambda (bug-num i)
78 (let ((msg-nums
79 (match (soap-invoke (%config 'debbugs)
80 get-bug-message-numbers
81 bug-num)
82 ((nums . ids) nums))))
83 (format (current-error-port)
84 "[~a/~a] Downloading ~a messages of bug ~a...~%"
85 i (length bug-nums)
86 (length msg-nums) bug-num)
87 (parameterize ((current-error-port (%make-void-port OPEN_WRITE)))
88 (par-for-each (cut download-message bug-num <>)
89 msg-nums))))
90 bug-nums
91 (iota (length bug-nums) 1))))
92 (else
93 (let ((repl-port (assoc-ref opts 'listen-repl)))
94 (when repl-port
95 (spawn-server (make-tcp-server-socket #:port repl-port)))))))
96
97 (start-mumi-web-server 1234)