From 88d50b3e1fd3e0ceedf688262aa81f91033c889f Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Tue, 10 Nov 2020 05:51:00 -0500 Subject: [PATCH] Consolidate msg-validation in the single-msg parser --- tt | 66 ++++++++++++++++++++++++++------------------------------------ 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/tt b/tt index e8f1010..0b590e3 100755 --- a/tt +++ b/tt @@ -11,18 +11,34 @@ (struct msg (tm_epoch tm_rfc3339 nick text)) (struct feed (nick uri)) -(define (msg<-toks nick toks) - (define tm_rfc3339 (list-ref toks 0)) - (define tok_text (list-ref toks 1)) - (define t (string->rfc3339-record tm_rfc3339)) - ; TODO handle tz offset - (define tm_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 tm_epoch tm_rfc3339 nick tok_text)) +(define (str->lines str) + (string-split str (regexp "[\r\n]+"))) + +(define (str->msg nick 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* + ([tm_rfc3339 (list-ref toks 0)] + [tok_text (list-ref toks 1)] + [t (string->rfc3339-record tm_rfc3339)] + ; TODO handle tz offset + [tm_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 tm_epoch tm_rfc3339 nick tok_text)))))) + +(define (str->msgs nick str) + (filter-map (λ (line) (str->msg nick line)) (str->lines str))) (define (uri-fetch uri) (log-info "GET ~a" uri) @@ -51,32 +67,6 @@ [i (in-naturals)]) (msg-print (odd? i) msg))) -(define (str->lines str) - (string-split str (regexp "[\r\n]+"))) - -(define (str->msgs nick str) - (let* ([lines - (str->lines str)] - [msg_lines - (filter (λ (l) (regexp-match? re-msg-begin l)) lines)] - [msgs - (begin - (log-debug "nick:~a lines:~a msg_lines:~a" - nick - (length lines) - (length msg_lines)) - (filter-map - (λ (line) - (define toks (string-split line (regexp "\t+"))) - (if (= 2 (length toks)) - (msg<-toks nick toks) - (begin - (log-warning "Invalid msg from nick:~a, msg:~a" nick line) - #f)) - ) - msg_lines))]) - msgs)) - (define (timeline feeds) (let* ([timelines (filter-map -- 2.20.1