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
11 ; TODO alt timeline formats: irc-like | email-like
14 ; <timestamp> <nick> <msg><lf>
24 ; TODO parse mentions:
25 ; - @<source.nick source.url> | @<source.url>
26 ; TODO highlight mentions
27 ; TODO filter on mentions
28 ; TODO highlight hashtags
29 ; TODO filter on hashtags
30 ; TODO hashtags as channels? initial hashtag special?
33 ; TODO console logger colors by level ('error)
34 ; TODO file logger ('debug)
37 ; - see timeline ops above
40 ; - nick expand to URI
42 ; - see timeline ops above
43 ; - see hashtag and channels above
46 ; - calls user-configured command to upload user's own feed file to their server
47 ; TODO user-agent format: <client>/<version> (+<source.url>; @<source.nick>)
48 ; - requires configurability
49 ; - ref: https://twtxt.readthedocs.io/en/latest/user/discoverability.html
58 (struct msg (tm_epoch tm_rfc3339 nick text))
59 (struct feed (nick uri))
61 (define (msg-print odd m)
62 (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
63 (date->string (seconds->date [msg-tm_epoch m]) #t)
69 ; TODO Zulu offset. Maybe in several formats. Which ones?
70 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
72 (define (str->msg nick str)
73 (if (not (regexp-match? re-msg-begin str))
75 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
77 (let ([toks (string-split str (regexp "\t+"))])
78 (if (not (= 2 (length toks)))
80 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
83 ([tm_rfc3339 (list-ref toks 0)]
84 [tok_text (list-ref toks 1)]
85 [t (string->rfc3339-record tm_rfc3339)]
86 ; TODO handle tz offset
87 [tm_epoch (find-seconds [rfc3339-record:second t]
88 [rfc3339-record:minute t]
89 [rfc3339-record:hour t]
90 [rfc3339-record:mday t]
91 [rfc3339-record:month t]
92 [rfc3339-record:year t])])
93 (msg tm_epoch tm_rfc3339 nick tok_text))))))
95 (define (str->lines str)
96 (string-split str (regexp "[\r\n]+")))
98 (define (str->msgs nick str)
99 (filter-map (λ (line) (str->msg nick line)) (str->lines str)))
101 (define (uri-fetch uri)
102 (log-info "GET ~a" uri)
103 (define resp (http-get uri))
104 (define status (http-response-code resp))
105 (define body (http-response-body resp))
106 (log-debug "finished GET ~a status:~a body length:~a"
107 uri status (string-length body))
108 ; TODO Handle redirects
109 (if (= status 200) body (raise status)))
111 (define (timeline-print timeline)
114 (msg-print (odd? i) msg)))
116 (define (feed->msgs feed)
117 (log-info "downloading feed nick:~a uri:~a"
123 (log-error "network error nick:~a uri:~a exn:~a"
130 (log-error "http error nick:~a uri:~a status:~a"
135 (str->msgs [feed-nick feed] [uri-fetch (feed-uri feed)])))
137 ; TODO timeline contract : time-sorted list of messages
138 (define (timeline feeds)
139 (sort (append* (filter-map feed->msgs feeds))
140 (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)])))
142 (define (we-are-twtxt)
144 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt"]
148 (str->lines payload)]
152 (define toks (string-split line))
159 (define (setup-logging)
160 (define logger (make-logger #f #f 'debug #f))
161 (define log-chan (make-log-receiver logger 'debug))
163 [date-display-format 'iso-8601]
165 (define data (sync log-chan))
166 (define level (vector-ref data 0))
167 (define msg (vector-ref data 1))
168 (define ts (date->string (current-date) #t))
169 (eprintf "~a [~a] ~a~n" ts level msg)
171 (current-logger logger))
175 (current-http-response-auto #f)
176 (current-http-user-agent "xandkar/tt 0.0.0")
177 (date-display-format 'rfc2822)
179 (define feeds (we-are-twtxt))
180 (timeline-print (timeline feeds)))