- (let* ([f (λ (x) (if (even? x) x #f))]
- [xs (range 11)]
- [actual (sort (concurrent-filter-map 10 f xs) <)]
- [expected (sort ( filter-map f xs) <)])
- (check-equal? actual expected "concurrent-filter-map")))
-
-(define (msg-print out-format odd msg)
- (printf
- (match out-format
- ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"]
- ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"]
- [_ (raise (format "Invalid output format: ~a" out-format))])
- (date->string (seconds->date [msg-ts_epoch msg]) #t)
- (msg-nick msg)
- (msg-uri msg)
- (if odd 36 33)
- (msg-text msg)))
-
-(define re-msg-begin
- ; TODO Zulu offset. Maybe in several formats. Which ones?
- (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
-
-(define (str->msg nick uri str)
- (if (not (regexp-match? re-msg-begin str))
- (begin
- (log-debug "Non-msg line from nick:~a, line:~a" nick str)
- #f)
- (let ([toks (string-split str (regexp "\t+"))])
- (if (not (= 2 (length toks)))
- (begin
- (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
- #f)
- (let*
- ([ts_rfc3339 (first toks)]
- [text (second toks)]
- [t (string->rfc3339-record ts_rfc3339)]
- ; TODO handle tz offset
- [ts_epoch (find-seconds [rfc3339-record:second t]
- [rfc3339-record:minute t]
- [rfc3339-record:hour t]
- [rfc3339-record:mday t]
- [rfc3339-record:month t]
- [rfc3339-record:year t])])
- (msg ts_epoch ts_rfc3339 nick uri text))))))
+ (let* ([f (λ (x) (if (even? x) x #f))]
+ [xs (range 11)]
+ [actual (sort (concurrent-filter-map 10 f xs) <)]
+ [expected (sort ( filter-map f xs) <)])
+ (check-equal? actual expected "concurrent-filter-map")))
+
+(: msg-print (-> Out-Format Integer Msg Void))
+(define msg-print
+ (let* ([colors (vector 36 33)]
+ [n (vector-length colors)])
+ (λ (out-format color-i msg)
+ (let ([color (vector-ref colors (modulo color-i n))]
+ [nick (Msg-nick msg)]
+ [uri (url->string (Msg-uri msg))]
+ [text (Msg-text msg)]
+ [mentions (Msg-mentions msg)])
+ (match out-format
+ ['single-line
+ (let ([nick (if nick nick uri)])
+ (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
+ (parameterize
+ ([date-display-format 'iso-8601])
+ (date->string (seconds->date (Msg-ts-epoch msg)) #t))
+ nick color text))]
+ ['multi-line
+ (let ([nick (if nick (string-append nick " ") "")])
+ (printf "~a (~a)~n\033[1;37m<~a~a>\033[0m~n\033[0;~am~a\033[0m~n~n"
+ (parameterize
+ ([date-display-format 'rfc2822])
+ (date->string (seconds->date (Msg-ts-epoch msg)) #t))
+ (Msg-ts-orig msg)
+ nick uri color text))])))))
+
+(: rfc3339->epoch (-> String (Option Nonnegative-Integer)))
+(define rfc3339->epoch
+ (let ([re (pregexp "^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})(:([0-9]{2}))?(\\.[0-9]+)?(Z|([+-])([0-9]{1,2}):?([0-9]{2}))?$")])
+ (λ (ts)
+ (match (regexp-match re ts)
+ [(list _wholething yyyy mm dd HH MM _:SS SS _fractional tz-whole tz-sign tz-HH tz-MM)
+ (let*
+ ([tz-offset
+ (match* (tz-whole tz-sign tz-HH tz-MM)
+ [("Z" #f #f #f)
+ 0]
+ [(_ (or "-" "+") (? identity h) (? identity m))
+ (let ([h (string->number h)]
+ [m (string->number m)]
+ ; Reverse to get back to UTC:
+ [op (match tz-sign ["+" -] ["-" +])])
+ (op 0 (+ (* 60 m) (* 60 (* 60 h)))))]
+ [(a b c d)
+ (log-warning "Impossible TZ string: ~v, components: ~v ~v ~v ~v" tz-whole a b c d)
+ 0])]
+ [ts-orig ts]
+ [local-time? #f]
+ [ts-epoch (find-seconds (if SS (string->number SS) 0)
+ (string->number MM)
+ (string->number HH)
+ (string->number dd)
+ (string->number mm)
+ (string->number yyyy)
+ local-time?)])
+ (+ ts-epoch tz-offset))]
+ [_
+ (log-error "Invalid timestamp: ~v" ts)
+ #f]))))
+
+(: str->msg (-> (Option String) Url String (Option Msg)))
+(define str->msg
+ (let ([re (pregexp "^([^\\s\t]+)[\\s\t]+(.*)$")])
+ (λ (nick uri str)
+ (with-handlers*
+ ([exn:fail?
+ (λ (e)
+ (log-error
+ "Failed to parse msg: ~v, from: ~v, at: ~v, because: ~v"
+ str nick (url->string uri) e)
+ #f)])
+ (match (regexp-match re str)
+ [(list _wholething ts-orig text)
+ (let ([ts-epoch (rfc3339->epoch ts-orig)])
+ (if ts-epoch
+ (let ([mentions
+ (filter-map
+ (λ (m) (match (regexp-match #px"@<([^>]+)>" m)
+ [(list _wholething nick-uri)
+ (str->peer nick-uri)]))
+ (regexp-match* #px"@<[^\\s]+([\\s]+)?[^>]+>" text))])
+ (Msg ts-epoch ts-orig nick uri text mentions))
+ (begin
+ (log-error
+ "Msg rejected due to invalid timestamp: ~v, nick:~v, uri:~v"
+ str nick (url->string uri))
+ #f)))]
+ [_
+ (log-debug "Non-msg line from nick:~v, line:~a" nick str)
+ #f])))))