| 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 | #lang racket |
| 39 | |
| 40 | (require openssl/sha1) |
| 41 | (require racket/date) |
| 42 | |
| 43 | (require http-client) |
| 44 | (require rfc3339-old) |
| 45 | |
| 46 | (struct msg (ts_epoch ts_rfc3339 nick uri text)) |
| 47 | (struct feed (nick uri)) |
| 48 | |
| 49 | (define (concurrent-filter-map num_workers f xs) |
| 50 | ; TODO switch from mailboxes to channels |
| 51 | (define (make-worker id f) |
| 52 | (define parent (current-thread)) |
| 53 | (λ () |
| 54 | (define self (current-thread)) |
| 55 | (define (work) |
| 56 | (thread-send parent (cons 'next self)) |
| 57 | (match (thread-receive) |
| 58 | ['done (thread-send parent (cons 'exit id))] |
| 59 | [(cons 'unit x) (begin |
| 60 | (define y (f x)) |
| 61 | (when y (thread-send parent (cons 'result y))) |
| 62 | (work))])) |
| 63 | (work))) |
| 64 | (define (dispatch ws xs ys) |
| 65 | (if (empty? ws) |
| 66 | ys |
| 67 | (match (thread-receive) |
| 68 | [(cons 'exit w) (dispatch (remove w ws =) xs ys)] |
| 69 | [(cons 'result y) (dispatch ws xs (cons y ys))] |
| 70 | [(cons 'next thd) (match xs |
| 71 | ['() (begin |
| 72 | (thread-send thd 'done) |
| 73 | (dispatch ws xs ys))] |
| 74 | [(cons x xs) (begin |
| 75 | (thread-send thd (cons 'unit x)) |
| 76 | (dispatch ws xs ys))])]))) |
| 77 | (define workers (range num_workers)) |
| 78 | (define threads (map (λ (id) (thread (make-worker id f))) workers)) |
| 79 | (define results (dispatch workers xs '())) |
| 80 | (for-each thread-wait threads) |
| 81 | results) |
| 82 | |
| 83 | (define (msg-print out-format odd msg) |
| 84 | (printf |
| 85 | (match out-format |
| 86 | ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"] |
| 87 | ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"] |
| 88 | [_ (raise (format "Invalid output format: ~a" out-format))]) |
| 89 | (date->string (seconds->date [msg-ts_epoch msg]) #t) |
| 90 | (msg-nick msg) |
| 91 | (msg-uri msg) |
| 92 | (if odd 36 33) |
| 93 | (msg-text msg))) |
| 94 | |
| 95 | (define re-msg-begin |
| 96 | ; TODO Zulu offset. Maybe in several formats. Which ones? |
| 97 | (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}")) |
| 98 | |
| 99 | (define (str->msg nick uri str) |
| 100 | (if (not (regexp-match? re-msg-begin str)) |
| 101 | (begin |
| 102 | (log-debug "Non-msg line from nick:~a, line:~a" nick str) |
| 103 | #f) |
| 104 | (let ([toks (string-split str (regexp "\t+"))]) |
| 105 | (if (not (= 2 (length toks))) |
| 106 | (begin |
| 107 | (log-warning "Invalid msg line from nick:~a, msg:~a" nick str) |
| 108 | #f) |
| 109 | (let* |
| 110 | ([ts_rfc3339 (first toks)] |
| 111 | [text (second toks)] |
| 112 | [t (string->rfc3339-record ts_rfc3339)] |
| 113 | ; TODO handle tz offset |
| 114 | [ts_epoch (find-seconds [rfc3339-record:second t] |
| 115 | [rfc3339-record:minute t] |
| 116 | [rfc3339-record:hour t] |
| 117 | [rfc3339-record:mday t] |
| 118 | [rfc3339-record:month t] |
| 119 | [rfc3339-record:year t])]) |
| 120 | (msg ts_epoch ts_rfc3339 nick uri text)))))) |
| 121 | |
| 122 | (define (str->lines str) |
| 123 | (string-split str (regexp "[\r\n]+"))) |
| 124 | |
| 125 | (define (str->msgs nick uri str) |
| 126 | (filter-map (λ (line) (str->msg nick uri line)) (str->lines str))) |
| 127 | |
| 128 | (define (hash-sha1 str) |
| 129 | (define in (open-input-string str)) |
| 130 | (define digest (sha1 in)) |
| 131 | (close-input-port in) |
| 132 | digest) |
| 133 | |
| 134 | (define (uri-fetch uri) |
| 135 | (log-info "GET ~a" uri) |
| 136 | (define resp (http-get uri)) |
| 137 | (define status (http-response-code resp)) |
| 138 | (define body (http-response-body resp)) |
| 139 | (log-debug "finished GET ~a status:~a body length:~a" |
| 140 | uri status (string-length body)) |
| 141 | ; TODO Handle redirects |
| 142 | (if (= status 200) |
| 143 | (let* |
| 144 | ([url-digest |
| 145 | (hash-sha1 uri)] |
| 146 | [cache-file-path |
| 147 | (expand-user-path (string-append "~/.tt/cache/" url-digest))]) |
| 148 | (display-to-file |
| 149 | body cache-file-path |
| 150 | #:exists 'replace) |
| 151 | body) |
| 152 | ; TODO A more-informative exception |
| 153 | (raise status))) |
| 154 | |
| 155 | (define (timeline-print out-format timeline) |
| 156 | (for ([msg timeline] |
| 157 | [i (in-naturals)]) |
| 158 | (msg-print out-format (odd? i) msg))) |
| 159 | |
| 160 | (define (feed->msgs feed) |
| 161 | (log-info "downloading feed nick:~a uri:~a" |
| 162 | (feed-nick feed) |
| 163 | (feed-uri feed)) |
| 164 | (with-handlers |
| 165 | ([exn:fail:network? |
| 166 | (λ (e) |
| 167 | (log-error "network error nick:~a uri:~a exn:~a" |
| 168 | (feed-nick feed) |
| 169 | (feed-uri feed) |
| 170 | e) |
| 171 | #f)] |
| 172 | [integer? |
| 173 | (λ (status) |
| 174 | (log-error "http error nick:~a uri:~a status:~a" |
| 175 | (feed-nick feed) |
| 176 | (feed-uri feed) |
| 177 | status) |
| 178 | #f)]) |
| 179 | (define uri (feed-uri feed)) |
| 180 | (str->msgs [feed-nick feed] uri [uri-fetch uri]))) |
| 181 | |
| 182 | ; TODO timeline contract : time-sorted list of messages |
| 183 | (define (timeline num_workers feeds) |
| 184 | (sort (append* (concurrent-filter-map num_workers feed->msgs feeds)) |
| 185 | (λ (a b) [< (msg-ts_epoch a) (msg-ts_epoch b)]))) |
| 186 | |
| 187 | (define (str->feed str) |
| 188 | ; TODO validation |
| 189 | (define toks (string-split str)) |
| 190 | (apply feed toks)) |
| 191 | |
| 192 | (define (str->feeds str) |
| 193 | (map str->feed (str->lines str))) |
| 194 | |
| 195 | (define (file->feeds filename) |
| 196 | (str->feeds (file->string filename))) |
| 197 | |
| 198 | (define (we-are-twtxt) |
| 199 | (define uri |
| 200 | "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt") |
| 201 | (str->feeds (uri-fetch uri))) |
| 202 | |
| 203 | (define user-agent |
| 204 | (let* |
| 205 | ([prog-name "tt"] |
| 206 | [prog-version "0.3.4"] |
| 207 | [prog-uri "https://github.com/xandkar/tt"] |
| 208 | [user-feed-file (expand-user-path "~/twtxt-me.txt")] |
| 209 | [user |
| 210 | (if (file-exists? user-feed-file) |
| 211 | (let ([user (first (file->feeds user-feed-file))]) |
| 212 | (format "+~a; @~a" (feed-uri user) (feed-nick user))) |
| 213 | (format "+~a" prog-uri))] |
| 214 | ) |
| 215 | (format "~a/~a (~a)" prog-name prog-version user))) |
| 216 | |
| 217 | (module+ main |
| 218 | (define (setup-logging) |
| 219 | (define logger (make-logger #f #f 'debug #f)) |
| 220 | (define log-chan (make-log-receiver logger 'debug)) |
| 221 | (void (thread (λ () |
| 222 | [date-display-format 'iso-8601] |
| 223 | [let loop () |
| 224 | (define data (sync log-chan)) |
| 225 | (define level (vector-ref data 0)) |
| 226 | (define msg (vector-ref data 1)) |
| 227 | (define ts (date->string (current-date) #t)) |
| 228 | (eprintf "~a [~a] ~a~n" ts level msg) |
| 229 | (loop)]))) |
| 230 | (current-logger logger)) |
| 231 | |
| 232 | (setup-logging) |
| 233 | (current-http-response-auto #f) |
| 234 | (current-http-user-agent user-agent) |
| 235 | (date-display-format 'rfc2822) |
| 236 | |
| 237 | (define args (current-command-line-arguments)) |
| 238 | (define feeds |
| 239 | (if (vector-empty? args) |
| 240 | (we-are-twtxt) |
| 241 | (file->feeds (vector-ref args 0)))) |
| 242 | (define out-format 'multi-line) |
| 243 | (define num_workers 15) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20. |
| 244 | (timeline-print out-format (timeline num_workers feeds))) |