-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaoy.scm
executable file
·216 lines (182 loc) · 6.23 KB
/
yaoy.scm
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
#!/usr/bin/env gosh
(use rfc.http)
(use rfc.json)
(use srfi-19)
(use file.util)
(use gauche.parseopt)
(load "./yaoytoolkit.scm")
(define *subcommands* '())
(define-macro (aif test consequent alternative)
`(let ((it ,test))
(if it ,consequent ,alternative)))
(define (get-subcommand command)
(aif (assoc command *subcommands*)
(cdr it)
#f))
(define (set-subcommand! command procedure)
(set! *subcommands* (assoc-set! *subcommands* command procedure)))
(define *yaoy-directory* (expand-path "~/.yaoy/"))
(define *yaoy-config* (string-append *yaoy-directory* "config"))
(define (get-yaoy-settings)
(call-with-input-file *yaoy-config*
(lambda (iport)
(if iport
(parse-json iport)
'()))
:if-does-not-exist #f))
(define (get-user-info key)
(let1 result (assoc key (get-yaoy-settings))
(if result
(cdr result) #f)))
(define (set-user-info! key value)
(create-directory* *yaoy-directory*)
(let1 settings (get-yaoy-settings)
(call-with-output-file *yaoy-config*
(lambda (oport)
(construct-json
(assoc-set! (if settings settings '()) key value) oport))
:if-does-not-exist :create)))
(define (send-yo username)
(let1 response
(http-response-jsonbody
(apply openyo-sendyo
(append
(map get-user-info
'("endpoint" "api_ver" "api_token"))
`(,username))))
(cdr (assoc "result" response))))
(define (send-yo-all)
(apply openyo-yoall
(map get-user-info
'("endpoint" "api_ver" "api_token"))))
(define (date->datestring date)
(date->string date "~Y/~m/~d ~H:~M:~S"))
(define (show-history . n)
(let1 result (if (null? n)
(apply openyo-history
(map get-user-info
'("endpoint" "api_ver" "api_token")))
(openyo-history
(get-user-info "endpoint")
(get-user-info "api_ver")
(get-user-info "api_token")
:count (car n)))
(for-each
(lambda (e)
(print (format "~A\t~A"
(date->datestring (cdr e)) (car e))))
result)))
(define (create-user username password)
(receive (result fail?)
(openyo-create-user (get-user-info "endpoint")
(get-user-info "api_ver")
username
password)
(cond
((not fail?)
(set-user-info! "api_token" result)
(print (format "User created: ~A" username)) #t)
((string=? fail? "http-post")
(print "Could not post request to ~A.~%"
(get-user-info "endpoint"))
(format #t "error log:~A~%" (car result)) #f)
(else
(format #t "Could not create user \"~A\": ~A~%"
username
(cadr result)) #f))))
(define (get-friends)
(openyo-list-friends (get-user-info "endpoint")
(get-user-info "api_ver")
(get-user-info "api_token")))
(set-subcommand! "friends"
(lambda (args)
(for-each print (get-friends))))
(define (initialize-yaoy-config endpoint api-ver)
(set-user-info! "endpoint" endpoint)
(set-user-info! "api_ver" api-ver))
(define (print-all . exprs)
(for-each print exprs))
(define (help)
(print-all "usage: yaoy <command> [<args>]"
""
" yo \t Send yo to your friend"
" yoall \t Send yo to all of your friends"
" history \t Show history of when and who yoed you"
" init \t Initialize yaoy configuration file"
" register \t Create new user"))
(define (yo-help)
(print-all "usage: yaoy yo <username>"
" send yo to <username>"))
(define (history-help)
(print-all "usage: yaoy history [n]"
" Show history of when and who yoed yo (at most n)"))
(define (init-help)
(print-all "usage: yaoy init [endpoint] [api_ver]"
(format " Initialize yaoy confiruration file (~A)~%" *yaoy-config*)))
(define (yo args)
(if (null? args)
(yo-help)
(print (send-yo (car args)))))
(set-subcommand! "yo" yo)
(define (yoall args)
(send-yo-all))
(set-subcommand! "yoall" yoall)
(define (history args)
(cond
((null? args) (show-history))
((string->number (car args)) (show-history (car args)))
(else (print (format "error: ~A is not a number" (car args)))
(history-help))))
(set-subcommand! "history" history)
(define (get-prompt prompt)
(display prompt)
(flush)
(let1 input (read-line)
(if (zero? (string-length input))
(get-prompt prompt)
input)))
(define get-pass get-prompt)
(define (length>? x k)
(cond
((null? x) #f)
((zero? k) x)
(else (length>? (cdr x) (- k 1)))))
(define-macro (let-with-list lst binds . body)
`(let ,(map (lambda (bind n)
`(,(car bind) (aif (length>? ,lst ,n)
(car it)
,(cadr bind))))
binds (liota +inf.0))
,@body))
(define (init args)
(let-with-list args ((endpoint (get-prompt "input endpoint> "))
(api-ver (get-prompt "input api_ver> ")))
(if (string->number api-ver)
(initialize-yaoy-config endpoint api-ver)
(begin
(print (format "error: ~A is not a number" api-ver))
(init-help)))))
(set-subcommand! "init" init)
(define (register args)
(let-with-list args ((username (get-prompt "input username> "))
(password (get-pass "input password> ")))
(aif (create-user username password)
(set-user-info! "username" username)
(print "registration failed."))))
(set-subcommand! "register" register)
(define (token args)
(let-with-list args ((password (get-pass "input password> ")))
(let1 response (openyo-new-api-token
(get-user-info "endpoint")
(get-user-info "api_ver")
(get-user-info "username")
password)
(print response))))
(set-subcommand! "token" token)
(define (main args)
(let1 args (cdr args)
(if (null? args)
(help)
(aif (get-subcommand (car args))
(it (cdr args))
(print (class-of (car args)))))))