(U 'old->new
'new->old))
-(struct msg
+(struct Msg
([ts-epoch : Integer]
[ts-orig : String]
[nick : (Option String)]
[uri : Url]
[text : String]
- [mentions : (Listof Peer)])
- #:type-name Msg)
+ [mentions : (Listof Peer)]))
(struct Peer
([nick : (Option String)]
- [uri : Url]))
+ [uri : Url])
+ #:transparent)
(: tt-home-dir Path-String)
(define tt-home-dir (build-path (expand-user-path "~") ".tt"))
-(: concurrent-filter-map (∀ (α β) (-> Natural (-> α β) (Listof α))))
+(: concurrent-filter-map (∀ (α β) (-> Natural (-> α β) (Listof α) (Listof β))))
(define (concurrent-filter-map num-workers f xs)
; TODO preserve order of elements OR communicate that reorder is expected
; TODO switch from mailboxes to channels
[n (vector-length colors)])
(λ (out-format color-i msg)
(let ([color (vector-ref colors (modulo color-i n))]
- [nick (msg-nick msg)]
- [uri (url->string (msg-uri msg))]
- [text (msg-text msg)]
- [mentions (msg-mentions msg)])
+ [nick (Msg-nick msg)]
+ [uri (url->string (Msg-uri msg))]
+ [text (Msg-text msg)]
+ [mentions (Msg-mentions msg)])
(match out-format
['single-line
(let ([nick (if nick nick uri)])
(printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
(parameterize
([date-display-format 'iso-8601])
- (date->string (seconds->date [msg-ts-epoch msg]) #t))
+ (date->string (seconds->date (Msg-ts-epoch msg)) #t))
nick color text))]
['multi-line
(let ([nick (if nick (string-append nick " ") "")])
(printf "~a (~a)~n\033[1;37m<~a~a>\033[0m~n\033[0;~am~a\033[0m~n~n"
(parameterize
([date-display-format 'rfc2822])
- (date->string (seconds->date [msg-ts-epoch msg]) #t))
- (msg-ts-orig msg)
+ (date->string (seconds->date (Msg-ts-epoch msg)) #t))
+ (Msg-ts-orig msg)
nick uri color text))])))))
(: rfc3339->epoch (-> String (Option Nonnegative-Integer)))
[(list _wholething nick-uri)
(str->peer nick-uri)]))
(regexp-match* #px"@<[^\\s]+([\\s]+)?[^>]+>" text))])
- (msg ts-epoch ts-orig nick uri text mentions))
+ (Msg ts-epoch ts-orig nick uri text mentions))
(begin
(log-error
"Msg rejected due to invalid timestamp: ~v, nick:~v, uri:~v"
z)]
[m (str->msg n u (string-append ts sep txt))])
(check-not-false m)
- (check-equal? (msg-nick m) n)
- (check-equal? (msg-uri m) u)
- (check-equal? (msg-text m) txt)
- (check-equal? (msg-ts-orig m) ts (format "Given: ~v" ts))
+ (check-equal? (Msg-nick m) n)
+ (check-equal? (Msg-uri m) u)
+ (check-equal? (Msg-text m) txt)
+ (check-equal? (Msg-ts-orig m) ts (format "Given: ~v" ts))
)))
(let* ([ts "2020-11-18T22:22:09-0500"]
[nick "foo"]
[uri "bar"]
[actual (str->msg nick uri (string-append ts tab text))]
- [expected (msg 1605756129 ts nick uri text)])
+ [expected (Msg 1605756129 ts nick uri text)])
(check-equal?
- (msg-ts-epoch actual)
- (msg-ts-epoch expected)
+ (Msg-ts-epoch actual)
+ (Msg-ts-epoch expected)
"str->msg ts-epoch")
(check-equal?
- (msg-ts-orig actual)
- (msg-ts-orig expected)
+ (Msg-ts-orig actual)
+ (Msg-ts-orig expected)
"str->msg ts-orig")
(check-equal?
- (msg-nick actual)
- (msg-nick expected)
+ (Msg-nick actual)
+ (Msg-nick expected)
"str->msg nick")
(check-equal?
- (msg-uri actual)
- (msg-uri expected)
+ (Msg-uri actual)
+ (Msg-uri expected)
"str->msg uri")
(check-equal?
- (msg-text actual)
- (msg-text expected)
+ (Msg-text actual)
+ (Msg-text expected)
"str->msg text")))
(: str->lines (-> String (Listof String)))
(filter-map str->peer (filter-comments (str->lines str))))
(: file->peers (-> Path-String (Listof Peer)))
-(define (file->peers filename)
- (str->peers (file->string filename)))
+(define (file->peers file-path)
+ (if (file-exists? file-path)
+ (str->peers (file->string file-path))
+ (begin
+ (log-error "File does not exist: ~v" (path->string file-path))
+ '())))
(: user-agent String)
(define user-agent
(: timeline-print (-> Out-Format (Listof Msg) Void))
(define (timeline-print out-format timeline)
(void (foldl (match-lambda**
- [((and m (msg _ _ nick _ _ _)) (cons prev-nick i))
+ [((and m (Msg _ _ nick _ _ _)) (cons prev-nick i))
(let ([i (if (equal? prev-nick nick) i (+ 1 i))])
(msg-print out-format i m)
(cons nick i))])
['old->new <]
['new->old >]))
(sort (append* (filter-map peer->msgs peers))
- (λ (a b) (cmp (msg-ts-epoch a) (msg-ts-epoch b)))))
+ (λ (a b) (cmp (Msg-ts-epoch a) (Msg-ts-epoch b)))))
+
+(: paths->peers (-> (Listof String) (Listof Peer)))
+(define (paths->peers paths)
+ (let* ([paths (match paths
+ ['()
+ (let ([peer-refs-file (build-path tt-home-dir "peers")])
+ (log-debug
+ "No peer ref file paths provided, defaulting to ~v"
+ (path->string peer-refs-file))
+ (list peer-refs-file))]
+ [paths
+ (log-debug "Peer ref file paths provided: ~v" paths)
+ (map string->path paths)])]
+ [peers (append* (map file->peers paths))])
+ (log-info "Read-in ~a peers." (length peers))
+ peers))
(: log-writer-stop (-> Thread Void))
(define (log-writer-stop log-writer)
(log-message (current-logger) 'fatal 'stop "Exiting." #f)
(thread-wait log-writer))
-(: logger-start (-> Log-Level Thread))
-(define (logger-start level)
+(: log-writer-start (-> Log-Level Thread))
+(define (log-writer-start level)
(let* ([logger
(make-logger #f #f level #f)]
[log-receiver
"u, upload : Upload your twtxt file (alias to execute ~/.tt/upload)."
""
#:args (command . args)
- (define log-writer (logger-start log-level))
+ (define log-writer (log-writer-start log-level))
(current-command-line-arguments (list->vector args))
(match command
[(or "d" "download")
[("-j" "--jobs")
njobs "Number of concurrent jobs."
(set! num-workers (string->number njobs))]
- #:args (filename)
+ #:args file-paths
(define-values (_res _cpu real-ms _gc)
- (time-apply timeline-download (list num-workers (file->peers filename))))
- (log-info "Timeline downloaded in ~a seconds." (/ real-ms 1000.0))
- (log-writer-stop log-writer)))]
+ (time-apply timeline-download (list num-workers (paths->peers file-paths))))
+ (log-info "Timeline downloaded in ~a seconds." (/ real-ms 1000.0))))]
[(or "u" "upload")
(command-line
#:program
[("-l" "--long")
"Long output format"
(set! out-format 'multi-line)]
- #:args (filename)
- (timeline-print out-format (timeline-read order (file->peers filename)))))]
- ))))
+ #:args file-paths
+ (timeline-print out-format (timeline-read order (paths->peers file-paths)))))])
+ (log-writer-stop log-writer))))