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 text))
48 (struct feed (nick uri))
50 (define (msg-print odd m)
51 (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
52 (date->string (seconds->date [msg-tm_epoch m]) #t)
58 ; TODO Zulu offset. Maybe in several formats. Which ones?
59 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
61 (define (str->msg nick str)
62 (if (not (regexp-match? re-msg-begin str))
64 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
66 (let ([toks (string-split str (regexp "\t+"))])
67 (if (not (= 2 (length toks)))
69 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
72 ([tm_rfc3339 (list-ref toks 0)]
73 [tok_text (list-ref toks 1)]
74 [t (string->rfc3339-record tm_rfc3339)]
75 ; TODO handle tz offset
76 [tm_epoch (find-seconds [rfc3339-record:second t]
77 [rfc3339-record:minute t]
78 [rfc3339-record:hour t]
79 [rfc3339-record:mday t]
80 [rfc3339-record:month t]
81 [rfc3339-record:year t])])
82 (msg tm_epoch tm_rfc3339 nick tok_text))))))
84 (define (str->lines str)
85 (string-split str (regexp "[\r\n]+")))
87 (define (str->msgs nick str)
88 (filter-map (λ (line) (str->msg nick line)) (str->lines str)))
90 (define (uri-fetch uri)
91 (log-info "GET ~a" uri)
92 (define resp (http-get uri))
93 (define status (http-response-code resp))
94 (define body (http-response-body resp))
95 (log-debug "finished GET ~a status:~a body length:~a"
96 uri status (string-length body))
97 ; TODO Handle redirects
98 (if (= status 200) body (raise status)))
100 (define (timeline-print timeline)
103 (msg-print (odd? i) msg)))
105 (define (feed->msgs feed)
106 (log-info "downloading feed nick:~a uri:~a"
112 (log-error "network error nick:~a uri:~a exn:~a"
119 (log-error "http error nick:~a uri:~a status:~a"
124 (str->msgs [feed-nick feed] [uri-fetch (feed-uri feed)])))
126 ; TODO timeline contract : time-sorted list of messages
127 (define (timeline feeds)
128 (sort (append* (filter-map feed->msgs feeds))
129 (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)])))
131 (define (we-are-twtxt)
133 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt"]
137 (str->lines payload)]
141 (define toks (string-split line))
148 (define (setup-logging)
149 (define logger (make-logger #f #f 'debug #f))
150 (define log-chan (make-log-receiver logger 'debug))
152 [date-display-format 'iso-8601]
154 (define data (sync log-chan))
155 (define level (vector-ref data 0))
156 (define msg (vector-ref data 1))
157 (define ts (date->string (current-date) #t))
158 (eprintf "~a [~a] ~a~n" ts level msg)
160 (current-logger logger))
164 (current-http-response-auto #f)
165 (current-http-user-agent "xandkar/tt 0.0.0")
166 (date-display-format 'rfc2822)
168 (define feeds (we-are-twtxt))
169 (timeline-print (timeline feeds)))