4 ; TODO optional text wrap
6 ; TODO caching (use cache by default, unless explicitly asked for update)
8 ; TODO feed set operations (perhaps better done externally?)
9 ; TODO timeline as a result of a query (feed set op + filter expressions)
10 ; TODO named timelines
13 ; TODO parse "following" from feed
14 ; - following = <nick> <uri>
15 ; TODO parse mentions:
16 ; - @<source.nick source.url> | @<source.url>
17 ; TODO highlight mentions
18 ; TODO filter on mentions
19 ; TODO highlight hashtags
20 ; TODO filter on hashtags
21 ; TODO hashtags as channels? initial hashtag special?
23 ; TODO console logger colors by level ('error)
24 ; TODO file logger ('debug)
27 ; - see timeline ops above
30 ; - nick expand to URI
32 ; - see timeline ops above
33 ; - see hashtag and channels above
36 ; - calls user-configured command to upload user's own feed file to their server
37 ; TODO user-agent format: <client>/<version> (+<source.url>; @<source.nick>)
38 ; - requires configurability
39 ; - ref: https://twtxt.readthedocs.io/en/latest/user/discoverability.html
48 (struct msg (tm_epoch tm_rfc3339 nick uri text))
49 (struct feed (nick uri))
51 (define (concurrent-filter-map num_workers f xs)
52 (define (make-worker id f)
53 (define parent (current-thread))
55 (define self (current-thread))
57 (thread-send parent (cons 'next self))
58 (match (thread-receive)
59 ['done (thread-send parent (cons 'exit id))]
60 [(cons 'unit x) (begin
62 (when y (thread-send parent (cons 'result y)))
65 (define (dispatch ws xs ys)
68 (match (thread-receive)
69 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
70 [(cons 'result y) (dispatch ws xs (cons y ys))]
71 [(cons 'next thd) (match xs
73 (thread-send thd 'done)
76 (thread-send thd (cons 'unit x))
77 (dispatch ws xs ys))])])))
79 (range 1 (add1 num_workers)))
81 (map (λ (id) (thread (make-worker id f))) workers))
83 (dispatch workers xs '()))
84 (for-each thread-wait threads)
87 (define (msg-print out-format odd msg)
90 ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"]
91 ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"]
92 [_ (raise (format "Invalid output format: ~a" out-format))])
93 (date->string (seconds->date [msg-tm_epoch msg]) #t)
100 ; TODO Zulu offset. Maybe in several formats. Which ones?
101 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
103 (define (str->msg nick uri str)
104 (if (not (regexp-match? re-msg-begin str))
106 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
108 (let ([toks (string-split str (regexp "\t+"))])
109 (if (not (= 2 (length toks)))
111 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
114 ([tm_rfc3339 (list-ref toks 0)]
115 [tok_text (list-ref toks 1)]
116 [t (string->rfc3339-record tm_rfc3339)]
117 ; TODO handle tz offset
118 [tm_epoch (find-seconds [rfc3339-record:second t]
119 [rfc3339-record:minute t]
120 [rfc3339-record:hour t]
121 [rfc3339-record:mday t]
122 [rfc3339-record:month t]
123 [rfc3339-record:year t])])
124 (msg tm_epoch tm_rfc3339 nick uri tok_text))))))
126 (define (str->lines str)
127 (string-split str (regexp "[\r\n]+")))
129 (define (str->msgs nick uri str)
130 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
132 (define (uri-fetch uri)
133 (log-info "GET ~a" uri)
134 (define resp (http-get uri))
135 (define status (http-response-code resp))
136 (define body (http-response-body resp))
137 (log-debug "finished GET ~a status:~a body length:~a"
138 uri status (string-length body))
139 ; TODO Handle redirects
140 (if (= status 200) body (raise status)))
142 (define (timeline-print out-format timeline)
145 (msg-print out-format (odd? i) msg)))
147 (define (feed->msgs feed)
148 (log-info "downloading feed nick:~a uri:~a"
154 (log-error "network error nick:~a uri:~a exn:~a"
161 (log-error "http error nick:~a uri:~a status:~a"
166 (define uri (feed-uri feed))
167 (str->msgs [feed-nick feed] uri [uri-fetch uri])))
169 ; TODO timeline contract : time-sorted list of messages
170 (define (timeline num_workers feeds)
171 (sort (append* (concurrent-filter-map num_workers feed->msgs feeds))
172 (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)])))
174 (define (str->feed str)
176 (define toks (string-split str))
181 (define (str->feeds str)
182 (map str->feed (str->lines str)))
184 (define (file->feeds filename)
185 (str->feeds (file->string filename)))
187 (define (we-are-twtxt)
189 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt")
190 (str->feeds (uri-fetch uri)))
192 (define (setup-logging)
193 (define logger (make-logger #f #f 'debug #f))
194 (define log-chan (make-log-receiver logger 'debug))
196 [date-display-format 'iso-8601]
198 (define data (sync log-chan))
199 (define level (vector-ref data 0))
200 (define msg (vector-ref data 1))
201 (define ts (date->string (current-date) #t))
202 (eprintf "~a [~a] ~a~n" ts level msg)
204 (current-logger logger))
208 (current-http-response-auto #f)
209 (current-http-user-agent "xandkar/tt 0.2.0")
210 (date-display-format 'rfc2822)
212 (define args (current-command-line-arguments))
214 (if (vector-empty? args)
216 (file->feeds (vector-ref args 0))))
217 (define out-format 'multi-line)
218 (define num_workers 15) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20.
219 (timeline-print out-format (timeline num_workers feeds)))