7d6c5b9b48773a37c4b027fce90cdd9ddc870b6c
[tt.git] / tt
1 #! /usr/bin/env racket
2 ; vim: filetype=racket
3
4 ; TODO write
5 ; TODO caching (use cache by default, unless explicitly asked for update)
6 ; TODO timeline limits
7 ; TODO user-defined feed sets (a la twitter lists)
8 ; TODO feed set operations
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 mentions:
14 ; - @<source.nick source.url> | @<source.url>
15 ; TODO highlight mentions
16 ; TODO filter on mentions
17 ; TODO highlight hashtags
18 ; TODO filter on hashtags
19 ; TODO hashtags as channels? initial hashtag special?
20 ; TODO query language
21 ; TODO concurrency
22 ; TODO console logger colors by level ('error)
23 ; TODO file logger ('debug)
24 ; TODO commands:
25 ; - r | read
26 ; - see timeline ops above
27 ; - w | write
28 ; - arg or stdin
29 ; - nick expand to URI
30 ; - q | query
31 ; - see timeline ops above
32 ; - see hashtag and channels above
33 ; - d | download
34 ; - u | upload
35 ; - calls user-configured command to upload user's own feed file to their server
36 ; TODO user-agent format: <client>/<version> (+<source.url>; @<source.nick>)
37 ; - requires configurability
38 ; - ref: https://twtxt.readthedocs.io/en/latest/user/discoverability.html
39
40 #lang racket
41
42 (require racket/date)
43
44 (require http-client)
45 (require rfc3339-old)
46
47 (struct msg (tm_epoch tm_rfc3339 nick uri text))
48 (struct feed (nick uri))
49
50 (define (msg-print out-format odd msg)
51 (printf
52 (match out-format
53 ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"]
54 ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"]
55 [_ (raise (format "Invalid output format: ~a" out-format))])
56 (date->string (seconds->date [msg-tm_epoch msg]) #t)
57 (msg-nick msg)
58 (msg-uri msg)
59 (if odd 36 33)
60 (msg-text msg)))
61
62 (define re-msg-begin
63 ; TODO Zulu offset. Maybe in several formats. Which ones?
64 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
65
66 (define (str->msg nick uri str)
67 (if (not (regexp-match? re-msg-begin str))
68 (begin
69 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
70 #f)
71 (let ([toks (string-split str (regexp "\t+"))])
72 (if (not (= 2 (length toks)))
73 (begin
74 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
75 #f)
76 (let*
77 ([tm_rfc3339 (list-ref toks 0)]
78 [tok_text (list-ref toks 1)]
79 [t (string->rfc3339-record tm_rfc3339)]
80 ; TODO handle tz offset
81 [tm_epoch (find-seconds [rfc3339-record:second t]
82 [rfc3339-record:minute t]
83 [rfc3339-record:hour t]
84 [rfc3339-record:mday t]
85 [rfc3339-record:month t]
86 [rfc3339-record:year t])])
87 (msg tm_epoch tm_rfc3339 nick uri tok_text))))))
88
89 (define (str->lines str)
90 (string-split str (regexp "[\r\n]+")))
91
92 (define (str->msgs nick uri str)
93 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
94
95 (define (uri-fetch uri)
96 (log-info "GET ~a" uri)
97 (define resp (http-get uri))
98 (define status (http-response-code resp))
99 (define body (http-response-body resp))
100 (log-debug "finished GET ~a status:~a body length:~a"
101 uri status (string-length body))
102 ; TODO Handle redirects
103 (if (= status 200) body (raise status)))
104
105 (define (timeline-print out-format timeline)
106 (for ([msg timeline]
107 [i (in-naturals)])
108 (msg-print out-format (odd? i) msg)))
109
110 (define (feed->msgs feed)
111 (log-info "downloading feed nick:~a uri:~a"
112 (feed-nick feed)
113 (feed-uri feed))
114 (with-handlers
115 ([exn:fail:network?
116 (λ (e)
117 (log-error "network error nick:~a uri:~a exn:~a"
118 (feed-nick feed)
119 (feed-uri feed)
120 e)
121 #f)]
122 [integer?
123 (λ (status)
124 (log-error "http error nick:~a uri:~a status:~a"
125 (feed-nick feed)
126 (feed-uri feed)
127 status)
128 #f)])
129 (define uri (feed-uri feed))
130 (str->msgs [feed-nick feed] uri [uri-fetch uri])))
131
132 ; TODO timeline contract : time-sorted list of messages
133 (define (timeline feeds)
134 (sort (append* (filter-map feed->msgs feeds))
135 (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)])))
136
137 (define (we-are-twtxt)
138 (let* ([uri
139 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt"]
140 [payload
141 (uri-fetch uri)]
142 [lines
143 (str->lines payload)]
144 [feeds
145 (map (λ (line)
146 ; TODO validation
147 (define toks (string-split line))
148 (feed
149 [list-ref toks 0]
150 [list-ref toks 1]))
151 lines)])
152 feeds))
153
154 (define (setup-logging)
155 (define logger (make-logger #f #f 'debug #f))
156 (define log-chan (make-log-receiver logger 'debug))
157 (void (thread (λ ()
158 [date-display-format 'iso-8601]
159 [let loop ()
160 (define data (sync log-chan))
161 (define level (vector-ref data 0))
162 (define msg (vector-ref data 1))
163 (define ts (date->string (current-date) #t))
164 (eprintf "~a [~a] ~a~n" ts level msg)
165 (loop)])))
166 (current-logger logger))
167
168 (define (main)
169 (setup-logging)
170 (current-http-response-auto #f)
171 (current-http-user-agent "xandkar/tt 0.0.0")
172 (date-display-format 'rfc2822)
173
174 (define feeds (we-are-twtxt))
175 (define out-format 'multi-line)
176 (timeline-print out-format (timeline feeds)))
177
178 (main)
This page took 0.071363 seconds and 3 git commands to generate.