5 ; TODO caching (use cache by default, unless explicitly asked for update)
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
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?
22 ; TODO console logger colors by level ('error)
23 ; TODO file logger ('debug)
26 ; - see timeline ops above
29 ; - nick expand to URI
31 ; - see timeline ops above
32 ; - see hashtag and channels above
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
47 (struct msg (tm_epoch tm_rfc3339 nick uri text))
48 (struct feed (nick uri))
50 (define (msg-print out-format odd msg)
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)
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}"))
66 (define (str->msg nick uri str)
67 (if (not (regexp-match? re-msg-begin str))
69 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
71 (let ([toks (string-split str (regexp "\t+"))])
72 (if (not (= 2 (length toks)))
74 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
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))))))
89 (define (str->lines str)
90 (string-split str (regexp "[\r\n]+")))
92 (define (str->msgs nick uri str)
93 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
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)))
105 (define (timeline-print out-format timeline)
108 (msg-print out-format (odd? i) msg)))
110 (define (feed->msgs feed)
111 (log-info "downloading feed nick:~a uri:~a"
117 (log-error "network error nick:~a uri:~a exn:~a"
124 (log-error "http error nick:~a uri:~a status:~a"
129 (define uri (feed-uri feed))
130 (str->msgs [feed-nick feed] uri [uri-fetch uri])))
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)])))
137 (define (we-are-twtxt)
139 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt"]
143 (str->lines payload)]
147 (define toks (string-split line))
154 (define (setup-logging)
155 (define logger (make-logger #f #f 'debug #f))
156 (define log-chan (make-log-receiver logger 'debug))
158 [date-display-format 'iso-8601]
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)
166 (current-logger logger))
170 (current-http-response-auto #f)
171 (current-http-user-agent "xandkar/tt 0.0.0")
172 (date-display-format 'rfc2822)
174 (define feeds (we-are-twtxt))
175 (define out-format 'multi-line)
176 (timeline-print out-format (timeline feeds)))