86f72b783f1c8a1c9aa96fafff6cf47710815302
[tt.git] / tt.rkt
1 #lang typed/racket/no-check
2
3 (require openssl/sha1)
4 (require racket/date)
5 (require
6 net/head
7 net/uri-codec
8 net/url)
9
10 (require (prefix-in info: "info.rkt"))
11
12 (module+ test
13 (require rackunit))
14
15 (define-type Url
16 net/url-structs:url)
17
18 (define-type Out-Format
19 (U 'single-line
20 'multi-line))
21
22 (define-type Timeline-Order
23 (U 'old->new
24 'new->old))
25
26 (struct Msg
27 ([ts-epoch : Integer]
28 [ts-orig : String]
29 [nick : (Option String)]
30 [uri : Url]
31 [text : String]
32 [mentions : (Listof Peer)]))
33
34 (struct Peer
35 ([nick : (Option String)]
36 [uri : Url])
37 #:transparent)
38
39 (: tt-home-dir Path-String)
40 (define tt-home-dir (build-path (expand-user-path "~") ".tt"))
41
42 (: concurrent-filter-map (∀ (α β) (-> Natural (-> α β) (Listof α) (Listof β))))
43 (define (concurrent-filter-map num-workers f xs)
44 ; TODO preserve order of elements OR communicate that reorder is expected
45 ; TODO switch from mailboxes to channels
46 (define (make-worker id f)
47 (define parent (current-thread))
48 (λ ()
49 (define self : Thread (current-thread))
50 (: work (∀ (α) (-> α)))
51 (define (work)
52 (thread-send parent (cons 'next self))
53 (match (thread-receive)
54 ['done (thread-send parent (cons 'exit id))]
55 [(cons 'unit x) (begin
56 (define y (f x))
57 (when y (thread-send parent (cons 'result y)))
58 (work))]))
59 (work)))
60 (: dispatch (∀ (α β) (-> (Listof Nonnegative-Integer) (Listof α) (Listof β))))
61 (define (dispatch ws xs ys)
62 (if (empty? ws)
63 ys
64 (match (thread-receive)
65 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
66 [(cons 'result y) (dispatch ws xs (cons y ys))]
67 [(cons 'next thd) (match xs
68 ['() (begin
69 (thread-send thd 'done)
70 (dispatch ws xs ys))]
71 [(cons x xs) (begin
72 (thread-send thd (cons 'unit x))
73 (dispatch ws xs ys))])])))
74 (define workers (range num-workers))
75 (define threads (map (λ (id) (thread (make-worker id f))) workers))
76 (define results (dispatch workers xs '()))
77 (for-each thread-wait threads)
78 results)
79
80 (module+ test
81 (let* ([f (λ (x) (if (even? x) x #f))]
82 [xs (range 11)]
83 [actual (sort (concurrent-filter-map 10 f xs) <)]
84 [expected (sort ( filter-map f xs) <)])
85 (check-equal? actual expected "concurrent-filter-map")))
86
87 (: msg-print (-> Out-Format Integer Msg Void))
88 (define msg-print
89 (let* ([colors (vector 36 33)]
90 [n (vector-length colors)])
91 (λ (out-format color-i msg)
92 (let ([color (vector-ref colors (modulo color-i n))]
93 [nick (Msg-nick msg)]
94 [uri (url->string (Msg-uri msg))]
95 [text (Msg-text msg)]
96 [mentions (Msg-mentions msg)])
97 (match out-format
98 ['single-line
99 (let ([nick (if nick nick uri)])
100 (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
101 (parameterize
102 ([date-display-format 'iso-8601])
103 (date->string (seconds->date (Msg-ts-epoch msg)) #t))
104 nick color text))]
105 ['multi-line
106 (let ([nick (if nick (string-append nick " ") "")])
107 (printf "~a (~a)~n\033[1;37m<~a~a>\033[0m~n\033[0;~am~a\033[0m~n~n"
108 (parameterize
109 ([date-display-format 'rfc2822])
110 (date->string (seconds->date (Msg-ts-epoch msg)) #t))
111 (Msg-ts-orig msg)
112 nick uri color text))])))))
113
114 (: rfc3339->epoch (-> String (Option Nonnegative-Integer)))
115 (define rfc3339->epoch
116 (let ([re (pregexp "^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})(:([0-9]{2}))?(\\.[0-9]+)?(Z|([+-])([0-9]{1,2}):?([0-9]{2}))?$")])
117 (λ (ts)
118 (match (regexp-match re ts)
119 [(list _wholething yyyy mm dd HH MM _:SS SS _fractional tz-whole tz-sign tz-HH tz-MM)
120 (let*
121 ([tz-offset
122 (match* (tz-whole tz-sign tz-HH tz-MM)
123 [("Z" #f #f #f)
124 0]
125 [(_ (or "-" "+") (? identity h) (? identity m))
126 (let ([h (string->number h)]
127 [m (string->number m)]
128 ; Reverse to get back to UTC:
129 [op (match tz-sign ["+" -] ["-" +])])
130 (op 0 (+ (* 60 m) (* 60 (* 60 h)))))]
131 [(a b c d)
132 (log-warning "Impossible TZ string: ~v, components: ~v ~v ~v ~v" tz-whole a b c d)
133 0])]
134 [ts-orig ts]
135 [local-time? #f]
136 [ts-epoch (find-seconds (if SS (string->number SS) 0)
137 (string->number MM)
138 (string->number HH)
139 (string->number dd)
140 (string->number mm)
141 (string->number yyyy)
142 local-time?)])
143 (+ ts-epoch tz-offset))]
144 [_
145 (log-error "Invalid timestamp: ~v" ts)
146 #f]))))
147
148 (: str->msg (-> (Option String) Url String (Option Msg)))
149 (define str->msg
150 (let ([re (pregexp "^([^\\s\t]+)[\\s\t]+(.*)$")])
151 (λ (nick uri str)
152 (define str-head (substring str 0 (min 100 (string-length str))))
153 (with-handlers*
154 ([exn:fail?
155 (λ (e)
156 (log-error
157 "Failed to parse msg: ~v, from: ~v, at: ~v, because: ~v"
158 str-head nick (url->string uri) e)
159 #f)])
160 (match (regexp-match re str)
161 [(list _wholething ts-orig text)
162 (let ([ts-epoch (rfc3339->epoch ts-orig)])
163 (if ts-epoch
164 (let ([mentions
165 (filter-map
166 (λ (m) (match (regexp-match #px"@<([^>]+)>" m)
167 [(list _wholething nick-uri)
168 (str->peer nick-uri)]))
169 (regexp-match* #px"@<[^\\s]+([\\s]+)?[^>]+>" text))])
170 (Msg ts-epoch ts-orig nick uri text mentions))
171 (begin
172 (log-error
173 "Msg rejected due to invalid timestamp: ~v, nick:~v, uri:~v"
174 str-head nick (url->string uri))
175 #f)))]
176 [_
177 (log-debug "Non-msg line from nick:~v, line:~a" nick str-head)
178 #f])))))
179
180 (module+ test
181 ; TODO Test for when missing-nick case
182 (let* ([tzs (for*/list ([d '("-" "+")]
183 [h '("5" "05")]
184 [m '("00" ":00" "57" ":57")])
185 (string-append d h m))]
186 [tzs (list* "" "Z" tzs)])
187 (for* ([n '("fake-nick")]
188 [u '("fake-uri")]
189 [s '("" ":10")]
190 [f '("" ".1337")]
191 [z tzs]
192 [sep (list "\t" " ")]
193 [txt '("foo bar baz" "'jaz poop bear giraffe / tea" "@*\"``")])
194 (let* ([ts (string-append "2020-11-18T22:22"
195 (if (non-empty-string? s) s ":00")
196 z)]
197 [m (str->msg n u (string-append ts sep txt))])
198 (check-not-false m)
199 (check-equal? (Msg-nick m) n)
200 (check-equal? (Msg-uri m) u)
201 (check-equal? (Msg-text m) txt)
202 (check-equal? (Msg-ts-orig m) ts (format "Given: ~v" ts))
203 )))
204
205 (let* ([ts "2020-11-18T22:22:09-0500"]
206 [tab " "]
207 [text "Lorem ipsum"]
208 [nick "foo"]
209 [uri "bar"]
210 [actual (str->msg nick uri (string-append ts tab text))]
211 [expected (Msg 1605756129 ts nick uri text '())])
212 (check-equal?
213 (Msg-ts-epoch actual)
214 (Msg-ts-epoch expected)
215 "str->msg ts-epoch")
216 (check-equal?
217 (Msg-ts-orig actual)
218 (Msg-ts-orig expected)
219 "str->msg ts-orig")
220 (check-equal?
221 (Msg-nick actual)
222 (Msg-nick expected)
223 "str->msg nick")
224 (check-equal?
225 (Msg-uri actual)
226 (Msg-uri expected)
227 "str->msg uri")
228 (check-equal?
229 (Msg-text actual)
230 (Msg-text expected)
231 "str->msg text")))
232
233 (: str->lines (-> String (Listof String)))
234 (define (str->lines str)
235 (string-split str (regexp "[\r\n]+")))
236
237 (module+ test
238 (check-equal? (str->lines "abc\ndef\n\nghi") '("abc" "def" "ghi")))
239
240 (: str->msgs (-> (Option String) Url String (Listof Msg)))
241 (define (str->msgs nick uri str)
242 (filter-map (λ (line) (str->msg nick uri line)) (filter-comments (str->lines str))))
243
244 (: cache-dir Path-String)
245 (define cache-dir (build-path tt-home-dir "cache"))
246
247 (define cache-object-dir (build-path cache-dir "objects"))
248
249 (: url->cache-file-path-v1 (-> Url Path-String))
250 (define (url->cache-file-path-v1 uri)
251 (define (hash-sha1 str) : (-> String String)
252 (define in (open-input-string str))
253 (define digest (sha1 in))
254 (close-input-port in)
255 digest)
256 (build-path cache-object-dir (hash-sha1 (url->string uri))))
257
258 (: url->cache-file-path-v2 (-> Url Path-String))
259 (define (url->cache-file-path-v2 uri)
260 (build-path cache-object-dir (uri-encode (url->string uri))))
261
262 (define url->cache-object-path url->cache-file-path-v2)
263
264 (: cache-object-filename->url (-> Path-String Url))
265 (define (cache-object-filename->url name)
266 (string->url (uri-decode (path->string name))))
267
268 (define (url->cache-etag-path uri)
269 (build-path cache-dir "etags" (uri-encode (url->string uri))))
270
271 (define (url->cache-lmod-path uri)
272 (build-path cache-dir "lmods" (uri-encode (url->string uri))))
273
274 ; TODO Return Option
275 (: uri-read-cached (-> Url String))
276 (define (uri-read-cached uri)
277 (define path-v1 (url->cache-file-path-v1 uri))
278 (define path-v2 (url->cache-file-path-v2 uri))
279 (when (file-exists? path-v1)
280 (rename-file-or-directory path-v1 path-v2 #t))
281 (if (file-exists? path-v2)
282 (file->string path-v2)
283 (begin
284 (log-warning "Cache file not found for URI: ~a" (url->string uri))
285 "")))
286
287 (: uri? (-> String Boolean))
288 (define (uri? str)
289 (regexp-match? #rx"^[a-z]+://.*" (string-downcase str)))
290
291 (: str->peer (String (Option Peer)))
292 (define (str->peer str)
293 (log-debug "Parsing peer string: ~v" str)
294 (with-handlers*
295 ([exn:fail?
296 (λ (e)
297 (log-error "Invalid URI in string: ~v, exn: ~v" str e)
298 #f)])
299 (match (string-split str)
300 [(list u) #:when (uri? u) (Peer #f (string->url u))]
301 [(list n u) #:when (uri? u) (Peer n (string->url u))]
302 [_
303 (log-error "Invalid peer string: ~v" str)
304 #f])))
305
306
307 (: filter-comments (-> (Listof String) (Listof String)))
308 (define (filter-comments lines)
309 (filter-not (λ (line) (string-prefix? line "#")) lines))
310
311 (: str->peers (-> String (Listof Peer)))
312 (define (str->peers str)
313 (filter-map str->peer (filter-comments (str->lines str))))
314
315 (: peers->file (-> (Listof Peers) Path-String Void))
316 (define (peers->file peers path)
317 (display-lines-to-file
318 (map (match-lambda
319 [(Peer n u)
320 (format "~a~a" (if n (format "~a " n) "") (url->string u))])
321 peers)
322 path
323 #:exists 'replace))
324
325 (: file->peers (-> Path-String (Listof Peer)))
326 (define (file->peers file-path)
327 (if (file-exists? file-path)
328 (str->peers (file->string file-path))
329 (begin
330 (log-warning "File does not exist: ~v" (path->string file-path))
331 '())))
332
333 (define re-rfc2822
334 #px"^(Mon|Tue|Wed|Thu|Fri|Sat|Sun), ([0-9]{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-9]{4}) ([0-2][0-9]):([0-6][0-9]):([0-6][0-9]) GMT")
335
336 (: b->n (-> Bytes (Option Number)))
337 (define (b->n b)
338 (string->number (bytes->string/utf-8 b)))
339
340 (: mon->num (-> Bytes Natural))
341 (define/match (mon->num mon)
342 [(#"Jan") 1]
343 [(#"Feb") 2]
344 [(#"Mar") 3]
345 [(#"Apr") 4]
346 [(#"May") 5]
347 [(#"Jun") 6]
348 [(#"Jul") 7]
349 [(#"Aug") 8]
350 [(#"Sep") 9]
351 [(#"Oct") 10]
352 [(#"Nov") 11]
353 [(#"Dec") 12])
354
355 (: rfc2822->epoch (-> Bytes (Option Nonnegative-Integer)))
356 (define (rfc2822->epoch timestamp)
357 (match (regexp-match re-rfc2822 timestamp)
358 [(list _ _ dd mo yyyy HH MM SS)
359 #:when (and dd mo yyyy HH MM SS)
360 (find-seconds (b->n SS)
361 (b->n MM)
362 (b->n HH)
363 (b->n dd)
364 (mon->num mo)
365 (b->n yyyy)
366 #f)]
367 [_
368 #f]))
369
370 (: user-agent String)
371 (define user-agent
372 (let*
373 ([prog-name "tt"]
374 [prog-version (info:#%info-lookup 'version)]
375 [prog-uri "https://github.com/xandkar/tt"]
376 [user-peer-file (build-path tt-home-dir "me")]
377 [user
378 (if (file-exists? user-peer-file)
379 (match (first (file->peers user-peer-file))
380 [(Peer #f u) (format "+~a" (url->string u) )]
381 [(Peer n u) (format "+~a; @~a" (url->string u) n)])
382 (format "+~a" prog-uri))])
383 (format "~a/~a (~a)" prog-name prog-version user)))
384
385 (: header-get (-> (Listof Bytes) Bytes (Option Bytes)))
386 (define (header-get headers name)
387 (match (filter-map (curry extract-field name) headers)
388 [(list val) val]
389 [_ #f]))
390
391 (: uri-download (-> Url Void))
392 (define (uri-download u)
393 (define cached-object-path (url->cache-object-path u))
394 (define cached-etag-path (url->cache-etag-path u))
395 (define cached-lmod-path (url->cache-lmod-path u))
396 (log-debug "uri-download ~v into ~v" u cached-object-path)
397 (define-values (status-line headers body-input)
398 ; TODO Timeout. Currently hangs on slow connections.
399 (http-sendrecv/url u #:headers (list (format "User-Agent: ~a" user-agent))))
400 (log-debug "headers: ~v" headers)
401 (log-debug "status-line: ~v" status-line)
402 (define status
403 (string->number (second (string-split (bytes->string/utf-8 status-line)))))
404 (log-debug "status: ~v" status)
405 ; TODO Handle redirects
406 (match status
407 [200
408 (let* ([etag (header-get headers #"ETag")]
409 [lmod (header-get headers #"Last-Modified")]
410 [lmod-curr (if lmod (rfc2822->epoch lmod) #f)]
411 [lmod-prev (if (file-exists? cached-lmod-path)
412 (rfc2822->epoch (file->bytes cached-lmod-path))
413 #f)])
414 (log-debug "lmod-curr:~v lmod-prev:~v" lmod-curr lmod-prev)
415 (unless (or (and etag
416 (file-exists? cached-etag-path)
417 (bytes=? etag (file->bytes cached-etag-path))
418 (begin
419 (log-info "ETags match, skipping the rest of ~v" (url->string u))
420 #t))
421 (and lmod-curr
422 lmod-prev
423 (<= lmod-curr lmod-prev)
424 (begin
425 (log-info "Last-Modified <= current skipping the rest of ~v" (url->string u))
426 #t)))
427 (begin
428 (log-info
429 "Downloading the rest of ~v. ETag: ~a, Last-Modified: ~v"
430 (url->string u) etag lmod)
431 (make-parent-directory* cached-object-path)
432 (make-parent-directory* cached-etag-path)
433 (make-parent-directory* cached-lmod-path)
434 (call-with-output-file cached-object-path
435 (curry copy-port body-input)
436 #:exists 'replace)
437 (when etag
438 (display-to-file etag cached-etag-path #:exists 'replace))
439 (when lmod
440 (display-to-file lmod cached-lmod-path #:exists 'replace))))
441 (close-input-port body-input))]
442 [_
443 (raise status)]))
444
445 (: timeline-print (-> Out-Format (Listof Msg) Void))
446 (define (timeline-print out-format timeline)
447 (void (foldl (match-lambda**
448 [((and m (Msg _ _ nick _ _ _)) (cons prev-nick i))
449 (let ([i (if (equal? prev-nick nick) i (+ 1 i))])
450 (msg-print out-format i m)
451 (cons nick i))])
452 (cons "" 0)
453 timeline)))
454
455 (: peer->msgs (-> Peer (Listof Msg)))
456 (define (peer->msgs f)
457 (match-define (Peer nick uri) f)
458 (log-info "Reading peer nick:~v uri:~v" nick (url->string uri))
459 (str->msgs nick uri (uri-read-cached uri)))
460
461 (: peer-download (-> Peer Void))
462 (define (peer-download f)
463 (match-define (Peer nick uri) f)
464 (define u (url->string uri))
465 (log-info "Downloading peer uri:~a" u)
466 (with-handlers
467 ([exn:fail?
468 (λ (e)
469 (log-error "Network error nick:~v uri:~v exn:~v" nick u e)
470 #f)]
471 [integer?
472 (λ (status)
473 (log-error "HTTP error nick:~v uri:~a status:~a" nick u status)
474 #f)])
475 (define-values (_result _tm-cpu-ms tm-real-ms _tm-gc-ms)
476 (time-apply uri-download (list uri)))
477 (log-info "Peer downloaded in ~a seconds, uri: ~a" (/ tm-real-ms 1000.0) u)))
478
479 (: timeline-download (-> Integer (Listof Peer) Void))
480 (define (timeline-download num-workers peers)
481 ; TODO No need for map - can just iter
482 (void (concurrent-filter-map num-workers peer-download peers)))
483
484 (: uniq (∀ (α) (-> (Listof α) (Listof α))))
485 (define (uniq xs)
486 (set->list (list->set xs)))
487
488 (: peers->timeline (-> (listof Peer) (listof Msg)))
489 (define (peers->timeline peers)
490 (append* (filter-map peer->msgs peers)))
491
492 (: timeline-sort (-> (listof Msg) timeline-order (Listof Msgs)))
493 (define (timeline-sort msgs order)
494 (define cmp (match order
495 ['old->new <]
496 ['new->old >]))
497 (sort msgs (λ (a b) (cmp (Msg-ts-epoch a)
498 (Msg-ts-epoch b)))))
499
500 (: paths->peers (-> (Listof String) (Listof Peer)))
501 (define (paths->peers paths)
502 (let* ([paths (match paths
503 ['()
504 (let ([peer-refs-file (build-path tt-home-dir "peers")])
505 (log-debug
506 "No peer ref file paths provided, defaulting to ~v"
507 (path->string peer-refs-file))
508 (list peer-refs-file))]
509 [paths
510 (log-debug "Peer ref file paths provided: ~v" paths)
511 (map string->path paths)])]
512 [peers (append* (map file->peers paths))])
513 (log-info "Read-in ~a peers." (length peers))
514 (uniq peers)))
515
516 (: mentioned-peers-in-cache (-> (Listof Peer)))
517 (define (mentioned-peers-in-cache)
518 (define msgs
519 (append* (map (λ (filename)
520 (define path (build-path cache-object-dir filename))
521 (define size (/ (file-size path) 1000000.0))
522 (log-info "BEGIN parsing ~a MB from file: ~v"
523 size
524 (path->string path))
525 (define t0 (current-inexact-milliseconds))
526 (define m (filter-map
527 (λ (line)
528 (str->msg #f (cache-object-filename->url filename) line))
529 (filter-comments
530 (file->lines path))))
531 (define t1 (current-inexact-milliseconds))
532 (log-info "END parsing ~a MB in ~a seconds from file: ~v."
533 size
534 (* 0.001 (- t1 t0))
535 (path->string path))
536 (when (empty? m)
537 (log-warning "No messages found in ~a" (path->string path)))
538 m)
539 (directory-list cache-object-dir))))
540 (uniq (append* (map Msg-mentions msgs))))
541
542 (: log-writer-stop (-> Thread Void))
543 (define (log-writer-stop log-writer)
544 (log-message (current-logger) 'fatal 'stop "Exiting." #f)
545 (thread-wait log-writer))
546
547 (: log-writer-start (-> Log-Level Thread))
548 (define (log-writer-start level)
549 (let* ([logger
550 (make-logger #f #f level #f)]
551 [log-receiver
552 (make-log-receiver logger level)]
553 [log-writer
554 (thread
555 (λ ()
556 (parameterize
557 ([date-display-format 'iso-8601])
558 (let loop ()
559 (match-define (vector level msg _ topic) (sync log-receiver))
560 (unless (equal? topic 'stop)
561 (eprintf "~a [~a] ~a~n" (date->string (current-date) #t) level msg)
562 (loop))))))])
563 (current-logger logger)
564 log-writer))
565
566 (module+ main
567 (let ([log-level 'info])
568 (command-line
569 #:program
570 "tt"
571 #:once-each
572 [("-d" "--debug")
573 "Enable debug log level."
574 (set! log-level 'debug)]
575 #:help-labels
576 ""
577 "and <command> is one of"
578 "r, read : Read the timeline (offline operation)."
579 "d, download : Download the timeline."
580 ; TODO Add path dynamically
581 "u, upload : Upload your twtxt file (alias to execute ~/.tt/upload)."
582 "c, crawl : Discover new peers mentioned by known peers (offline operation)."
583 ""
584 #:args (command . args)
585 (define log-writer (log-writer-start log-level))
586 (current-command-line-arguments (list->vector args))
587 (match command
588 [(or "d" "download")
589 ; Initially, 15 was fastest out of the tried: 1, 5, 10, 20. Then I
590 ; started noticing significant slowdowns. Reducing to 5 seems to help.
591 (let ([num-workers 5])
592 (command-line
593 #:program
594 "tt download"
595 #:once-each
596 [("-j" "--jobs")
597 njobs "Number of concurrent jobs."
598 (set! num-workers (string->number njobs))]
599 #:args file-paths
600 (let ([peers (paths->peers file-paths)])
601 (define-values (_res _cpu real-ms _gc)
602 (time-apply timeline-download (list num-workers peers)))
603 (log-info "Downloaded timelines from ~a peers in ~a seconds."
604 (length peers)
605 (/ real-ms 1000.0)))))]
606 [(or "u" "upload")
607 (command-line
608 #:program
609 "tt upload"
610 #:args ()
611 (if (system (path->string (build-path tt-home-dir "upload")))
612 (exit 0)
613 (exit 1)))]
614 [(or "r" "read")
615 (let ([out-format 'multi-line]
616 [order 'old->new]
617 [ts-min #f]
618 [ts-max #f])
619 (command-line
620 #:program
621 "tt read"
622 #:once-each
623 [("-r" "--rev")
624 "Reverse displayed timeline order."
625 (set! order 'new->old)]
626 [("-m" "--min")
627 m "Earliest time to display (ignore anything before it)."
628 (set! ts-min (rfc3339->epoch m))]
629 [("-x" "--max")
630 x "Latest time to display (ignore anything after it)."
631 (set! ts-max (rfc3339->epoch x))]
632 #:once-any
633 [("-s" "--short")
634 "Short output format"
635 (set! out-format 'single-line)]
636 [("-l" "--long")
637 "Long output format"
638 (set! out-format 'multi-line)]
639 #:args file-paths
640 (let* ([peers
641 (paths->peers file-paths)]
642 [timeline
643 (timeline-sort (peers->timeline peers) order)]
644 [timeline
645 (filter (λ (m) (and (if ts-min (>= (Msg-ts-epoch m)
646 ts-min)
647 #t)
648 (if ts-max (<= (Msg-ts-epoch m)
649 ts-max)
650 #t)))
651 timeline)])
652 (timeline-print out-format timeline))))]
653 [(or "c" "crawl")
654 (command-line
655 #:program
656 "tt crawl"
657 #:args ()
658 (let* ([peers-sort
659 (λ (peers) (sort peers (match-lambda**
660 [((Peer n1 _) (Peer n2 _))
661 (string<? (if n1 n1 "")
662 (if n2 n2 ""))])))]
663 [peers-all-file
664 (build-path tt-home-dir "peers-all")]
665 [peers-mentioned-file
666 (build-path tt-home-dir "peers-mentioned")]
667 [peers-mentioned-curr
668 (mentioned-peers-in-cache)]
669 [peers-mentioned-prev
670 (file->peers peers-mentioned-file)]
671 [peers-mentioned
672 (peers-sort (uniq (append peers-mentioned-prev
673 peers-mentioned-curr)))]
674 [peers-all-prev
675 (file->peers peers-all-file)]
676 [peers-all
677 (list->set (append peers-mentioned
678 peers-all-prev))]
679 [peers-discovered
680 (set-subtract peers-all (list->set peers-all-prev))]
681 [peers-all
682 (peers-sort (set->list peers-all))])
683 (log-info "Known peers mentioned: ~a" (length peers-mentioned))
684 (log-info "Known peers total: ~a" (length peers-all))
685 (log-info "Discovered ~a new peers:~n~a"
686 (set-count peers-discovered)
687 (pretty-format (map
688 (λ (p) (cons (Peer-nick p)
689 (url->string (Peer-uri p))))
690 (set->list peers-discovered))))
691 (peers->file peers-mentioned
692 peers-mentioned-file)
693 (peers->file peers-all
694 peers-all-file)))]
695 [command
696 (eprintf "Error: invalid command: ~v\n" command)
697 (eprintf "Please use the \"--help\" option to see a list of available commands.\n")
698 (exit 1)])
699 (log-writer-stop log-writer))))
This page took 0.110105 seconds and 3 git commands to generate.