Test msg constructor and line splitter
[tt.git] / tt.rkt
1 ; TODO optional text wrap
2 ; TODO write
3 ; TODO caching (use cache by default, unless explicitly asked for update)
4 ; - [x] value --> cache
5 ; - [ ] value <-- cache
6 ; requires: commands
7 ; TODO timeline limits
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
11 ; TODO CLI params
12 ; TODO config files
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?
22 ; TODO query language
23 ; TODO console logger colors by level ('error)
24 ; TODO file logger ('debug)
25 ; TODO commands:
26 ; - r | read
27 ; - see timeline ops above
28 ; - w | write
29 ; - arg or stdin
30 ; - nick expand to URI
31 ; - q | query
32 ; - see timeline ops above
33 ; - see hashtag and channels above
34 ; - d | download
35 ; - u | upload
36 ; - calls user-configured command to upload user's own feed file to their server
37 ;
38 ; Looks like a better CLI parser than "racket/cmdline":
39 ; https://docs.racket-lang.org/natural-cli/
40
41 #lang racket
42
43 (require openssl/sha1)
44 (require racket/date)
45
46 (require http-client)
47 (require rfc3339-old)
48
49 (module+ test
50 (require rackunit))
51
52 (struct msg (ts_epoch ts_rfc3339 nick uri text))
53 (struct feed (nick uri))
54
55 (define (concurrent-filter-map num_workers f xs)
56 ; TODO preserve order of elements OR communicate that reorder is expected
57 ; TODO switch from mailboxes to channels
58 (define (make-worker id f)
59 (define parent (current-thread))
60 (λ ()
61 (define self (current-thread))
62 (define (work)
63 (thread-send parent (cons 'next self))
64 (match (thread-receive)
65 ['done (thread-send parent (cons 'exit id))]
66 [(cons 'unit x) (begin
67 (define y (f x))
68 (when y (thread-send parent (cons 'result y)))
69 (work))]))
70 (work)))
71 (define (dispatch ws xs ys)
72 (if (empty? ws)
73 ys
74 (match (thread-receive)
75 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
76 [(cons 'result y) (dispatch ws xs (cons y ys))]
77 [(cons 'next thd) (match xs
78 ['() (begin
79 (thread-send thd 'done)
80 (dispatch ws xs ys))]
81 [(cons x xs) (begin
82 (thread-send thd (cons 'unit x))
83 (dispatch ws xs ys))])])))
84 (define workers (range num_workers))
85 (define threads (map (λ (id) (thread (make-worker id f))) workers))
86 (define results (dispatch workers xs '()))
87 (for-each thread-wait threads)
88 results)
89
90 (module+ test
91 (let* ([f (λ (x) (if (even? x) x #f))]
92 [xs (range 11)]
93 [actual (sort (concurrent-filter-map 10 f xs) <)]
94 [expected (sort ( filter-map f xs) <)])
95 (check-equal? actual expected "concurrent-filter-map")))
96
97 (define (msg-print out-format odd msg)
98 (printf
99 (match out-format
100 ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"]
101 ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"]
102 [_ (raise (format "Invalid output format: ~a" out-format))])
103 (date->string (seconds->date [msg-ts_epoch msg]) #t)
104 (msg-nick msg)
105 (msg-uri msg)
106 (if odd 36 33)
107 (msg-text msg)))
108
109 (define re-msg-begin
110 ; TODO Zulu offset. Maybe in several formats. Which ones?
111 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
112
113 (define (str->msg nick uri str)
114 (if (not (regexp-match? re-msg-begin str))
115 (begin
116 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
117 #f)
118 (let ([toks (string-split str (regexp "\t+"))])
119 (if (not (= 2 (length toks)))
120 (begin
121 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
122 #f)
123 (let*
124 ([ts_rfc3339 (first toks)]
125 [text (second toks)]
126 [t (string->rfc3339-record ts_rfc3339)]
127 ; TODO handle tz offset
128 [ts_epoch (find-seconds [rfc3339-record:second t]
129 [rfc3339-record:minute t]
130 [rfc3339-record:hour t]
131 [rfc3339-record:mday t]
132 [rfc3339-record:month t]
133 [rfc3339-record:year t])])
134 (msg ts_epoch ts_rfc3339 nick uri text))))))
135
136 (module+ test
137 (let* ([ts "2020-11-18T22:22:09-0500"]
138 [tab " "]
139 [text "Lorem ipsum"]
140 [nick "foo"]
141 [uri "bar"]
142 [actual (str->msg nick uri (string-join (list ts text) tab))]
143 [expected (msg 1605756129 ts nick uri text)])
144 (check-equal?
145 (msg-ts_epoch actual)
146 (msg-ts_epoch expected)
147 "str->msg ts_epoch")
148 (check-equal?
149 (msg-ts_rfc3339 actual)
150 (msg-ts_rfc3339 expected)
151 "str->msg ts_rfc3339")
152 (check-equal?
153 (msg-nick actual)
154 (msg-nick expected)
155 "str->msg nick")
156 (check-equal?
157 (msg-uri actual)
158 (msg-uri expected)
159 "str->msg uri")
160 (check-equal?
161 (msg-text actual)
162 (msg-text expected)
163 "str->msg text")))
164
165 (define (str->lines str)
166 (string-split str (regexp "[\r\n]+")))
167
168 (module+ test
169 (check-equal? (str->lines "abc\ndef\n\nghi") '("abc" "def" "ghi")))
170
171 (define (str->msgs nick uri str)
172 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
173
174 (define (hash-sha1 str)
175 (define in (open-input-string str))
176 (define digest (sha1 in))
177 (close-input-port in)
178 digest)
179
180 (define (uri-fetch uri)
181 (log-info "GET ~a" uri)
182 (define resp (http-get uri))
183 (define status (http-response-code resp))
184 (define body (http-response-body resp))
185 (log-debug "finished GET ~a status:~a body length:~a"
186 uri status (string-length body))
187 ; TODO Handle redirects
188 (if (= status 200)
189 (let*
190 ([url-digest
191 (hash-sha1 uri)]
192 [cache-file-path
193 (expand-user-path (string-append "~/.tt/cache/" url-digest))])
194 (display-to-file
195 body cache-file-path
196 #:exists 'replace)
197 body)
198 ; TODO A more-informative exception
199 (raise status)))
200
201 (define (timeline-print out-format timeline)
202 (for ([msg timeline]
203 [i (in-naturals)])
204 (msg-print out-format (odd? i) msg)))
205
206 (define (feed->msgs feed)
207 (log-info "downloading feed nick:~a uri:~a"
208 (feed-nick feed)
209 (feed-uri feed))
210 (with-handlers
211 ([exn:fail:network?
212 (λ (e)
213 (log-error "network error nick:~a uri:~a exn:~a"
214 (feed-nick feed)
215 (feed-uri feed)
216 e)
217 #f)]
218 [integer?
219 (λ (status)
220 (log-error "http error nick:~a uri:~a status:~a"
221 (feed-nick feed)
222 (feed-uri feed)
223 status)
224 #f)])
225 (define uri (feed-uri feed))
226 (str->msgs [feed-nick feed] uri [uri-fetch uri])))
227
228 ; TODO timeline contract : time-sorted list of messages
229 (define (timeline num_workers feeds)
230 (sort (append* (concurrent-filter-map num_workers feed->msgs feeds))
231 (λ (a b) [< (msg-ts_epoch a) (msg-ts_epoch b)])))
232
233 (define (str->feed str)
234 ; TODO validation
235 (define toks (string-split str))
236 (apply feed toks))
237
238 (define (str->feeds str)
239 (map str->feed (str->lines str)))
240
241 (define (file->feeds filename)
242 (str->feeds (file->string filename)))
243
244 (define (we-are-twtxt)
245 (define uri
246 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt")
247 (str->feeds (uri-fetch uri)))
248
249 (define (user-agent prog-name prog-version)
250 (let*
251 ([prog-uri "https://github.com/xandkar/tt"]
252 [user-feed-file (expand-user-path "~/twtxt-me.txt")]
253 [user
254 (if (file-exists? user-feed-file)
255 (let ([user (first (file->feeds user-feed-file))])
256 (format "+~a; @~a" (feed-uri user) (feed-nick user)))
257 (format "+~a" prog-uri))]
258 )
259 (format "~a/~a (~a)" prog-name prog-version user)))
260
261 (module+ main
262 (require setup/getinfo)
263
264 (let* ([logger (make-logger #f #f 'debug #f)]
265 [log-receiver (make-log-receiver logger 'debug)])
266 (void (thread (λ ()
267 [date-display-format 'iso-8601]
268 [let loop ()
269 (define data (sync log-receiver))
270 (define level (vector-ref data 0))
271 (define msg (vector-ref data 1))
272 (define ts (date->string (current-date) #t))
273 (eprintf "~a [~a] ~a~n" ts level msg)
274 (loop)])))
275 (current-logger logger))
276 (current-http-response-auto #f)
277 (let* ([prog-name "tt"]
278 [prog-version ((get-info (list prog-name)) 'version)]
279 [user-agent (user-agent prog-name prog-version)])
280 (current-http-user-agent user-agent))
281 (date-display-format 'rfc2822)
282 (let ([feeds
283 (let ([args (current-command-line-arguments)])
284 (if (= 0 (vector-length args))
285 (we-are-twtxt)
286 (file->feeds (vector-ref args 0))))]
287 [out-format
288 'multi-line]
289 [num_workers
290 15]) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20.
291 (timeline-print out-format (timeline num_workers feeds))))
This page took 0.055734 seconds and 5 git commands to generate.