|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (scripts read-rfc822) |
|
:use-module (ice-9 regex) |
|
:use-module (ice-9 rdelim) |
|
:autoload (srfi srfi-13) (string-join) |
|
:export (read-rfc822 read-rfc822-silently)) |
|
|
|
(define %include-in-guild-list #f) |
|
(define %summary "Validate an RFC822-style file.") |
|
|
|
(define from-line-rx (make-regexp "^From ")) |
|
(define header-name-rx (make-regexp "^([^:]+):[ \t]*")) |
|
(define header-cont-rx (make-regexp "^[ \t]+")) |
|
|
|
(define option #f) |
|
|
|
(define (drain-message port) |
|
(let loop ((line (read-line port)) (acc '())) |
|
(cond ((eof-object? line) |
|
(reverse acc)) |
|
((and option (regexp-exec from-line-rx line)) |
|
(for-each (lambda (c) |
|
(unread-char c port)) |
|
(cons #\newline |
|
(reverse (string->list line)))) |
|
(reverse acc)) |
|
(else |
|
(loop (read-line port) (cons line acc)))))) |
|
|
|
(define (parse-message port) |
|
(let* ((from (and option |
|
(match:suffix (regexp-exec from-line-rx |
|
(read-line port))))) |
|
(body-lines #f) |
|
(body #f) |
|
(headers '()) |
|
(add-header! (lambda (reversed-hlines) |
|
(let* ((hlines (reverse reversed-hlines)) |
|
(first (car hlines)) |
|
(m (regexp-exec header-name-rx first)) |
|
(name (string->symbol (match:substring m 1))) |
|
(data (string-join |
|
(cons (substring first (match:end m)) |
|
(cdr hlines)) |
|
" "))) |
|
(set! headers (acons name data headers)))))) |
|
|
|
(let loop ((line (read-line port)) (current-header #f)) |
|
(cond ((string-null? line) |
|
(and current-header (add-header! current-header)) |
|
(set! body-lines (drain-message port))) |
|
((regexp-exec header-cont-rx line) |
|
=> (lambda (m) |
|
(loop (read-line port) |
|
(cons (match:suffix m) current-header)))) |
|
(else |
|
(and current-header (add-header! current-header)) |
|
(loop (read-line port) (list line))))) |
|
(set! headers (reverse headers)) |
|
(lambda (component) |
|
(case component |
|
((from) from) |
|
((body-lines) body-lines) |
|
((headers) headers) |
|
((body) (or body |
|
(begin (set! body (string-join body-lines "\n" 'suffix)) |
|
body))) |
|
(else (error "bad component:" component)))))) |
|
|
|
(define (read-rfc822-silently port) |
|
(parse-message port)) |
|
|
|
(define (display-rfc822 parse) |
|
(cond ((parse 'from) => (lambda (from) (format #t "From ~A\n" from)))) |
|
(for-each (lambda (header) |
|
(format #t "~A: ~A\n" (car header) (cdr header))) |
|
(parse 'headers)) |
|
(format #t "\n~A" (parse 'body))) |
|
|
|
(define (read-rfc822 . args) |
|
(let ((parse (read-rfc822-silently (open-file (car args) OPEN_READ)))) |
|
(display-rfc822 parse)) |
|
#t) |
|
|
|
(define main read-rfc822) |
|
|
|
|
|
|