f7731c470b0cceba6ae5e6df43d332c99670d7f1
[tt.git] / tt.rkt
1 #lang racket
2
3 (require openssl/sha1)
4 (require racket/date)
5 (require (prefix-in info: setup/getinfo))
6 (require
7 net/http-client
8 net/url-string
9 net/url-structs)
10
11 (module+ test
12 (require rackunit))
13
14 (struct msg
15 (
16 ts_epoch ; Integer
17 ts_rfc3339 ; String
18 nick ; String
19 uri ; net/url-structs:url
20 text ; String
21 ))
22
23 (struct feed
24 (
25 nick ; String
26 uri ; net/url-structs:url
27 ))
28
29 (define (concurrent-filter-map num_workers f xs)
30 ; TODO preserve order of elements OR communicate that reorder is expected
31 ; TODO switch from mailboxes to channels
32 (define (make-worker id f)
33 (define parent (current-thread))
34 (λ ()
35 (define self (current-thread))
36 (define (work)
37 (thread-send parent (cons 'next self))
38 (match (thread-receive)
39 ['done (thread-send parent (cons 'exit id))]
40 [(cons 'unit x) (begin
41 (define y (f x))
42 (when y (thread-send parent (cons 'result y)))
43 (work))]))
44 (work)))
45 (define (dispatch ws xs ys)
46 (if (empty? ws)
47 ys
48 (match (thread-receive)
49 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
50 [(cons 'result y) (dispatch ws xs (cons y ys))]
51 [(cons 'next thd) (match xs
52 ['() (begin
53 (thread-send thd 'done)
54 (dispatch ws xs ys))]
55 [(cons x xs) (begin
56 (thread-send thd (cons 'unit x))
57 (dispatch ws xs ys))])])))
58 (define workers (range num_workers))
59 (define threads (map (λ (id) (thread (make-worker id f))) workers))
60 (define results (dispatch workers xs '()))
61 (for-each thread-wait threads)
62 results)
63
64 (module+ test
65 (let* ([f (λ (x) (if (even? x) x #f))]
66 [xs (range 11)]
67 [actual (sort (concurrent-filter-map 10 f xs) <)]
68 [expected (sort ( filter-map f xs) <)])
69 (check-equal? actual expected "concurrent-filter-map")))
70
71 (define msg-print
72 (let* ([colors (vector 36 33)]
73 [n (vector-length colors)])
74 (λ (out-format color-i msg)
75 (let ([color (vector-ref colors (modulo color-i n))]
76 [nick (msg-nick msg)]
77 [uri (url->string (msg-uri msg))]
78 [text (msg-text msg)])
79 (match out-format
80 ['single-line
81 (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
82 (parameterize ([date-display-format 'iso-8601])
83 (date->string (seconds->date [msg-ts_epoch msg]) #t))
84 nick color text)]
85 ['multi-line
86 (printf "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"
87 (parameterize ([date-display-format 'rfc2822])
88 (date->string (seconds->date [msg-ts_epoch msg]) #t))
89 nick uri color text)])))))
90
91 (define str->msg
92 ; TODO Split parsing into 2 stages: 1) line->list; 2) rfc3339->epoch.
93 (let ([re (pregexp "^(([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})(:([0-9]{2}))?)(\\.[0-9]+)?([^\\s\t]*)[\\s\t]+(.*)$")])
94 (λ (nick uri str)
95 (with-handlers*
96 ([exn:fail?
97 (λ (e)
98 (log-error
99 "Failed to parse msg: ~v, from: ~v, at: ~v, because: ~v"
100 str nick (url->string uri) e)
101 #f)])
102 (match (regexp-match re str)
103 [(list _wholething ts yyyy mm dd HH MM _:SS SS _f tz text)
104 (let*
105 ; TODO handle tz offset
106 ([ts_rfc3339 (string-append ts (if SS "" ":00") (if tz tz ""))]
107 [ts_epoch (find-seconds (if SS (string->number SS) 0)
108 (string->number MM)
109 (string->number HH)
110 (string->number dd)
111 (string->number mm)
112 (string->number yyyy))])
113 (msg ts_epoch ts_rfc3339 nick uri text))]
114 [_
115 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
116 #f])))))
117
118 (module+ test
119 (let* ([tzs (for*/list ([d '("-" "+")]
120 [h '("5" "05")]
121 [m '("00" ":00" "57" ":57")])
122 (string-append d h m))]
123 [tzs (list* "" "Z" tzs)])
124 (for* ([n '("fake-nick")]
125 [u '("fake-uri")]
126 [s '("" ":10")]
127 [f '("" ".1337")]
128 [z tzs]
129 [sep (list "\t" " ")]
130 [txt '("foo bar baz" "'jaz poop bear giraffe / tea" "@*\"``")])
131 (let* ([ts (string-append "2020-11-18T22:22"
132 (if (non-empty-string? s) s ":00")
133 z)]
134 [m (str->msg n u (string-append ts sep txt))])
135 (check-not-false m)
136 (check-equal? (msg-nick m) n)
137 (check-equal? (msg-uri m) u)
138 (check-equal? (msg-text m) txt)
139 (check-equal? (msg-ts_rfc3339 m) ts (format "Given: ~v" ts))
140 )))
141
142 (let* ([ts "2020-11-18T22:22:09-0500"]
143 [tab " "]
144 [text "Lorem ipsum"]
145 [nick "foo"]
146 [uri "bar"]
147 [actual (str->msg nick uri (string-append ts tab text))]
148 [expected (msg 1605756129 ts nick uri text)])
149 ; FIXME re-enable after handling tz offset
150 ;(check-equal?
151 ; (msg-ts_epoch actual)
152 ; (msg-ts_epoch expected)
153 ; "str->msg ts_epoch")
154 (check-equal?
155 (msg-ts_rfc3339 actual)
156 (msg-ts_rfc3339 expected)
157 "str->msg ts_rfc3339")
158 (check-equal?
159 (msg-nick actual)
160 (msg-nick expected)
161 "str->msg nick")
162 (check-equal?
163 (msg-uri actual)
164 (msg-uri expected)
165 "str->msg uri")
166 (check-equal?
167 (msg-text actual)
168 (msg-text expected)
169 "str->msg text")))
170
171 (define (str->lines str)
172 (string-split str (regexp "[\r\n]+")))
173
174 (module+ test
175 (check-equal? (str->lines "abc\ndef\n\nghi") '("abc" "def" "ghi")))
176
177 (define (str->msgs nick uri str)
178 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
179
180 (define (hash-sha1 str)
181 (define in (open-input-string str))
182 (define digest (sha1 in))
183 (close-input-port in)
184 digest)
185
186 (define (url->cache-file-path uri)
187 ; TODO Replace hashing with encoding
188 (expand-user-path (string-append "~/.tt/cache/" (hash-sha1 (url->string uri)))))
189
190 (define (uri-read-cached uri)
191 (define path (url->cache-file-path uri))
192 (if (file-exists? path)
193 (file->string path)
194 (begin
195 (log-warning "Cache file not found for URI: ~a" (url->string uri))
196 "")))
197
198 (define (str->feed str)
199 (log-debug "Parsing feed string: ~v" str)
200 (match (string-split str)
201 [(list nick u)
202 (with-handlers*
203 ([exn:fail?
204 (λ (e)
205 (log-error "Invalid URI: ~v, exn: ~v" u e)
206 #f)])
207 (feed nick (string->url u)))]
208 [_
209 (log-error "Invalid feed string: ~v" str)
210 #f]))
211
212 (define (filter-comments lines)
213 (filter-not (λ (line) (string-prefix? line "#")) lines))
214
215 (define (str->feeds str)
216 (filter-map str->feed (filter-comments (str->lines str))))
217
218 (define (file->feeds filename)
219 (str->feeds (file->string filename)))
220
221 (define user-agent
222 (let*
223 ([prog-name "tt"]
224 [prog-version ((info:get-info (list prog-name)) 'version)]
225 [prog-uri "https://github.com/xandkar/tt"]
226 [user-feed-file (expand-user-path "~/twtxt-me.txt")]
227 [user
228 (if (file-exists? user-feed-file)
229 (let ([user (first (file->feeds user-feed-file))])
230 (format "+~a; @~a" (url->string (feed-uri user)) (feed-nick user)))
231 (format "+~a" prog-uri))])
232 (format "~a/~a (~a)" prog-name prog-version user)))
233
234 ; uri-download : net/url-structs:url -> Void
235 (define (uri-download u)
236 (define cache-file-path (url->cache-file-path u))
237 (log-debug "uri-download ~v into ~v" u cache-file-path)
238 (match* ((url-scheme u) (url-host u) (url-port u))
239 [(s h p)
240 #:when (and s h)
241 (define ssl? (string=? s "https"))
242 (define-values (status-line headers body-input)
243 ; TODO Timeout. Currently hangs on slow connections.
244 (http-sendrecv
245 h
246 (url->string (struct-copy url u [scheme #f] [host #f]))
247 #:ssl? ssl?
248 #:port (cond [p p] [ssl? 443] [else 80])
249 #:headers (list (format "User-Agent: ~a" user-agent))
250 ))
251 (log-debug "headers: ~v" headers)
252 (log-debug "status-line: ~v" status-line)
253 (define status
254 (string->number (second (string-split (bytes->string/utf-8 status-line)))))
255 (log-debug "status: ~v" status)
256 ; TODO Handle redirects
257 (if (= 200 status)
258 (call-with-output-file cache-file-path
259 (λ (cache-output)
260 (copy-port body-input cache-output))
261 #:exists 'replace)
262 (raise status))]
263 [(_ _ _)
264 (log-error "Invalid URI: ~v" u)]))
265
266 (define (timeline-print out-format timeline)
267 (void (foldl (match-lambda**
268 [((and m (msg _ _ nick _ _)) (cons prev-nick i))
269 (let ([i (if (string=? prev-nick nick) i (+ 1 i))])
270 (msg-print out-format i m)
271 (cons nick i))])
272 (cons "" 0)
273 timeline)))
274
275 ; feed->msgs : Feed -> (Listof Msg)
276 (define (feed->msgs f)
277 (match-define (feed nick uri) f)
278 (log-info "Reading feed nick:~a uri:~v" nick uri)
279 (str->msgs nick uri (uri-read-cached uri)))
280
281 ; feed-download : Feed -> Void
282 (define (feed-download f)
283 (match-define (feed nick uri) f)
284 (log-info "Downloading feed nick:~a uri:~a" nick (url->string uri))
285 (with-handlers
286 ([exn:fail?
287 (λ (e)
288 (log-error "Network error nick:~a uri:~v exn:~v" nick uri e)
289 #f)]
290 [integer?
291 (λ (status)
292 (log-error "HTTP error nick:~a uri:~a status:~a" nick uri status)
293 #f)])
294 (uri-download uri)))
295
296 ; timeline-download : Integer -> (Listof Feed) -> Void
297 (define (timeline-download num_workers feeds)
298 ; TODO No need for map - can just iter
299 (void (concurrent-filter-map num_workers feed-download feeds)))
300
301 ; TODO timeline contract : time-sorted list of messages
302 ; timeline-read : (U 'old->new 'new->old) -> (Listof Feeds) -> (Listof Msg)
303 (define (timeline-read order feeds)
304 (define cmp (match order
305 ['old->new <]
306 ['new->old >]))
307 (sort (append* (filter-map feed->msgs feeds))
308 (λ (a b) (cmp (msg-ts_epoch a) (msg-ts_epoch b)))))
309
310 (define (start-logger level)
311 (let* ([logger (make-logger #f #f level #f)]
312 [log-receiver (make-log-receiver logger level)])
313 (void (thread (λ ()
314 (parameterize
315 ([date-display-format 'iso-8601])
316 (let loop ()
317 (define data (sync log-receiver))
318 (define level (vector-ref data 0))
319 (define msg (vector-ref data 1))
320 (define ts (date->string (current-date) #t))
321 (eprintf "~a [~a] ~a~n" ts level msg)
322 (loop))))))
323 (current-logger logger)))
324
325 (module+ main
326 (let ([log-level 'info])
327 (command-line
328 #:program
329 "tt"
330 #:once-each
331 [("-d" "--debug")
332 "Enable debug log level."
333 (set! log-level 'debug)]
334 #:help-labels
335 ""
336 "and <command> is one of"
337 "r, read i : Read the timeline."
338 "d, download : Download the timeline."
339 "u, upload : Upload your twtxt file (alias to execute ~/.tt/upload)."
340 ""
341 #:args (command . args)
342 (start-logger log-level)
343 (current-command-line-arguments (list->vector args))
344 (match command
345 [(or "d" "download")
346 (let ([num_workers 15]) ; 15 was fastest out of the tried: 1, 5, 10, 20.
347 (command-line
348 #:program
349 "tt download"
350 #:once-each
351 [("-j" "--jobs")
352 njobs "Number of concurrent jobs."
353 (set! num_workers (string->number njobs))]
354 #:args (filename)
355 (timeline-download num_workers (file->feeds filename))))]
356 [(or "u" "upload")
357 (command-line
358 #:program
359 "tt upload"
360 #:args ()
361 (if (system (path->string (expand-user-path "~/.tt/upload")))
362 (exit 0)
363 (exit 1)))]
364 [(or "r" "read")
365 (let ([out-format 'multi-line]
366 [order 'old->new])
367 (command-line
368 #:program
369 "tt read"
370 #:once-each
371 [("-r" "--rev")
372 "Reverse displayed timeline order."
373 (set! order 'new->old)]
374 #:once-any
375 [("-s" "--short")
376 "Short output format"
377 (set! out-format 'single-line)]
378 [("-l" "--long")
379 "Long output format"
380 (set! out-format 'multi-line)]
381 #:args (filename)
382 (timeline-print out-format (timeline-read order (file->feeds filename)))))]
383 ))))
This page took 0.079247 seconds and 3 git commands to generate.