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
45 (struct msg (ts_epoch ts_rfc3339 nick uri text))
46 (struct feed (nick uri))
48 (define (concurrent-filter-map num_workers f xs)
49 ; TODO switch from mailboxes to channels
50 (define (make-worker id f)
51 (define parent (current-thread))
53 (define self (current-thread))
55 (thread-send parent (cons 'next self))
56 (match (thread-receive)
57 ['done (thread-send parent (cons 'exit id))]
58 [(cons 'unit x) (begin
60 (when y (thread-send parent (cons 'result y)))
63 (define (dispatch ws xs ys)
66 (match (thread-receive)
67 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
68 [(cons 'result y) (dispatch ws xs (cons y ys))]
69 [(cons 'next thd) (match xs
71 (thread-send thd 'done)
74 (thread-send thd (cons 'unit x))
75 (dispatch ws xs ys))])])))
76 (define workers (range num_workers))
77 (define threads (map (λ (id) (thread (make-worker id f))) workers))
78 (define results (dispatch workers xs '()))
79 (for-each thread-wait threads)
82 (define (msg-print out-format odd msg)
85 ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"]
86 ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"]
87 [_ (raise (format "Invalid output format: ~a" out-format))])
88 (date->string (seconds->date [msg-ts_epoch msg]) #t)
95 ; TODO Zulu offset. Maybe in several formats. Which ones?
96 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
98 (define (str->msg nick uri str)
99 (if (not (regexp-match? re-msg-begin str))
101 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
103 (let ([toks (string-split str (regexp "\t+"))])
104 (if (not (= 2 (length toks)))
106 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
109 ([ts_rfc3339 (first toks)]
111 [t (string->rfc3339-record ts_rfc3339)]
112 ; TODO handle tz offset
113 [ts_epoch (find-seconds [rfc3339-record:second t]
114 [rfc3339-record:minute t]
115 [rfc3339-record:hour t]
116 [rfc3339-record:mday t]
117 [rfc3339-record:month t]
118 [rfc3339-record:year t])])
119 (msg ts_epoch ts_rfc3339 nick uri text))))))
121 (define (str->lines str)
122 (string-split str (regexp "[\r\n]+")))
124 (define (str->msgs nick uri str)
125 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
127 (define (uri-fetch uri)
128 (log-info "GET ~a" uri)
129 (define resp (http-get uri))
130 (define status (http-response-code resp))
131 (define body (http-response-body resp))
132 (log-debug "finished GET ~a status:~a body length:~a"
133 uri status (string-length body))
134 ; TODO Handle redirects
135 (if (= status 200) body (raise status)))
137 (define (timeline-print out-format timeline)
140 (msg-print out-format (odd? i) msg)))
142 (define (feed->msgs feed)
143 (log-info "downloading feed nick:~a uri:~a"
149 (log-error "network error nick:~a uri:~a exn:~a"
156 (log-error "http error nick:~a uri:~a status:~a"
161 (define uri (feed-uri feed))
162 (str->msgs [feed-nick feed] uri [uri-fetch uri])))
164 ; TODO timeline contract : time-sorted list of messages
165 (define (timeline num_workers feeds)
166 (sort (append* (concurrent-filter-map num_workers feed->msgs feeds))
167 (λ (a b) [< (msg-ts_epoch a) (msg-ts_epoch b)])))
169 (define (str->feed str)
171 (define toks (string-split str))
174 (define (str->feeds str)
175 (map str->feed (str->lines str)))
177 (define (file->feeds filename)
178 (str->feeds (file->string filename)))
180 (define (we-are-twtxt)
182 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt")
183 (str->feeds (uri-fetch uri)))
185 (define (setup-logging)
186 (define logger (make-logger #f #f 'debug #f))
187 (define log-chan (make-log-receiver logger 'debug))
189 [date-display-format 'iso-8601]
191 (define data (sync log-chan))
192 (define level (vector-ref data 0))
193 (define msg (vector-ref data 1))
194 (define ts (date->string (current-date) #t))
195 (eprintf "~a [~a] ~a~n" ts level msg)
197 (current-logger logger))
203 [prog-version "0.3.3"]
204 [prog-uri "https://github.com/xandkar/tt"]
205 [user-feed-file (expand-user-path "~/twtxt-me.txt")]
207 (if (file-exists? user-feed-file)
208 (let ([user (first (file->feeds user-feed-file))])
209 (format "+~a; @~a" (feed-uri user) (feed-nick user)))
210 (format "+~a" prog-uri))]
212 (format "~a/~a (~a)" prog-name prog-version user)))
215 (current-http-response-auto #f)
216 (current-http-user-agent user-agent)
217 (date-display-format 'rfc2822)
219 (define args (current-command-line-arguments))
221 (if (vector-empty? args)
223 (file->feeds (vector-ref args 0))))
224 (define out-format 'multi-line)
225 (define num_workers 15) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20.
226 (timeline-print out-format (timeline num_workers feeds)))