~shironeko/lotte

lotte/lotte-web.scm -rwxr-xr-x 7.0 KiB
788e47dbshironeko add mock web interface 1 year, 4 days ago
                                                                                
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
#! /usr/bin/env -S guile -L . -s
!#

; lotte: The Fair Lottery Program
; Copyright 2020 shironeko
;
; This file is part of lotte.
;
; lotte 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.
;
; lotte 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 <https://www.gnu.org/licenses/>.

(use-modules (ice-9 rdelim)
             (web server)
             (web request)
             (web uri)
             (lotte main)
             (lotte utils))


(define lucky-num-max "10000")
(define winner-num-max "100")


(define (get-users p)
  (define (iter users)
    (let ((user (read-line p)))
      (if (eof-object? user)
          users
          (iter (cons user users)))))
  (iter '()))

(define (format-lotte lotte-name lucky-num-str winners-num-str mac-key users winners)
  (define (format-winners)
    (define (iter i acc)
      (if (< i 0)
          acc
          (let* ((winner (vector-ref winners i))
                 (name (car winner))
                 (score (cdr winner)))
            (iter (1- i) (cons `(tr (td ,name) (td ,score)) acc)))))
    `(table (thead (tr (th "Winner") (th "Score")))
            (tbody ,@(iter (1- (vector-length winners)) '()))))
  (define (format-users)
    `(table (thead (tr (th "User")))
            (tbody ,@(reverse (map (lambda (user) `(tr (td ,user))) users)))))
  (if (null? winners)
      `((p "The info you need to post:")
        (p ,lotte-name)
        (p "Lucky number's MAC: " ,(tag-msg mac-key lucky-num-str))
        (p "Number of Winners: " ,winners-num-str)
        (form (@ (method "post"))
              (label (@ (for "users")) "Enter the list of users: ")
              (br)
              (textarea (@ (id "users") (name "users") (rows "25") (cols "80")) "")
              (br)
              (input (@ (type "submit") (value "Draw Lottery")))))
      `((p ,lotte-name)
        (p "The lucky number was: " ,lucky-num-str)
        (p "The MAC key was: " ,mac-key)
        ,(format-winners)
        ,(if (not (null? users)) (format-users) '()))))

(define (get-new-lotte)
  (html-response "en" "New Lotte"
                 '((style "*{box-sizing:border-box}input,textarea{width:75%;margin-top:5px}label{float:left;width:25%;margin-top:5px}input[type=submit]{cursor:pointer;float:right;width:auto}@media screen and (max-width:80ch){input,label,textarea{width:100% !important}}"))
                 `((form (@ (method "post"))
                    (label (@ (for "lotte-name")) "Lottery Name: ")
                    (input (@ (type "text") (id "lotte-name") (name "lotte-name")
                              (required) (placeholder "this needs to be unique each time")))
                    (label (@ (for "lucky-num")) "Lucky Number: ")
                    (input (@ (type "number") (id "lucky-num") (name "lucky-num")
                              (min "1") (max ,lucky-num-max) (required)))
                    (label (@ (for "winner-num")) "Number of Winners: ")
                    (input (@ (type "number") (id "winner-num") (name "winner-num")
                              (min "1") (max ,winner-num-max) (required)))
                    (input (@ (type "submit") (value "Create Lottery")))))))
(define (post-new-lotte body)
  (let* ((mac-key (gen-mac-key))
         (mac-part (substring mac-key 0 12))
         (lotte-info (parse-post body))
         (lotte-name (assoc-ref lotte-info "lotte-name"))
         (lucky-num (assoc-ref lotte-info "lucky-num"))
         (winner-num (assoc-ref lotte-info "winner-num")))
    (call-with-output-file (string-append "lotte-" mac-part ".txt")
      (lambda (p) (format p "~A\n~A\n~A\n~A\n"
                          lotte-name lucky-num winner-num mac-key)))
    (redirect-to (string-append "/priv/" mac-part))))

(define (get-existing-lotte mac-part)
  (call-with-input-file (string-append "lotte-" mac-part ".txt")
    (lambda (p)
      (let* ((lotte-name (read-line p))
             (lucky-num-str (read-line p))
             (lucky-num (string->number lucky-num-str))
             (winners-num-str (read-line p))
             (winners-num (string->number winners-num-str))
             (mac-key (read-line p))
             (users (get-users p)))
        (if (null? users)
            (html-response "en" "Public Lotte" '()
                           (format-lotte lotte-name lucky-num-str winners-num-str
                                         mac-key '() '()))
            (html-response "en" "Public Lotte" '()
                           (format-lotte lotte-name lucky-num-str winners-num-str
                                         mac-key users
                                         (lotte lotte-name lucky-num
                                                winners-num users))))))))
(define (post-pub-lotte mac-part path body)
  (let* ((parsed (parse-post body))
         (users-str (assoc-ref parsed "users"))
         (file (open-file (string-append "lotte-" mac-part ".txt") "a")))
    (display (string-delete #\return users-str) file)
    (newline file)
    (close-port file)
    (redirect-to path)))
(define (get-priv-lotte mac-part)
  (values '((content-type . (text/plain)))
          (string-append "This is a public lottery: " mac-part)))
(define (post-pub-lotte mac-part path body)
  (let* ((parsed (parse-post body))
         (users-str (assoc-ref parsed "users"))
         (file (open-file (string-append "lotte-" mac-part ".txt") "a")))
    (display (string-delete #\return users-str) file)
    (newline file)
    (close-port file)
    (redirect-to path)))

(define (lotte-handler request body)
  (let* ((method (request-method request))
         (path (uri-path (request-uri request)))
         (pathl (split-and-decode-uri-path path)))
    (cond ((and (eq? method 'GET) (null? pathl))
           (get-new-lotte))
          ((and (eq? method 'POST) (null? pathl))
           (post-new-lotte body))
          ((and (eq? method 'GET)
                (not (null? (cdr pathl))) (string=? (car pathl) "pub")
                (string-every base64url-charset (cadr pathl)))
           (get-pub-lotte (cadr pathl)))
          ((and (eq? method 'POST)
                (not (null? (cdr pathl))) (string=? (car pathl) "pub")
                (string-every base64url-charset (cadr pathl)))
           (post-pub-lotte (cadr pathl) path body))
          ((and (eq? method 'GET)
                (not (null? (cdr pathl))) (string=? (car pathl) "priv")
                (string-every base64url-charset (cadr pathl)))
           (get-priv-lotte (cadr pathl)))
          ((string=? (car pathl) "favicon.ico") (null? (cdr pathl)) (not-found))
          (else (redirect-to "/")))))

(run-server lotte-handler 'http '(#:port 41488))