Commit | Line | Data |
---|---|---|
57737e24 TTN |
1 | ;;; read-rfc822 --- Validate RFC822 file by displaying it to stdout |
2 | ||
a1a2ed53 | 3 | ;; Copyright (C) 2002, 2004, 2006, 2011 Free Software Foundation, Inc. |
57737e24 TTN |
4 | ;; |
5 | ;; This program is free software; you can redistribute it and/or | |
83ba2d37 NJ |
6 | ;; modify it under the terms of the GNU Lesser General Public License |
7 | ;; as published by the Free Software Foundation; either version 3, or | |
57737e24 TTN |
8 | ;; (at your option) any later version. |
9 | ;; | |
10 | ;; This program is distributed in the hope that it will be useful, | |
11 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
83ba2d37 | 13 | ;; Lesser General Public License for more details. |
57737e24 | 14 | ;; |
83ba2d37 NJ |
15 | ;; You should have received a copy of the GNU Lesser General Public |
16 | ;; License along with this software; see the file COPYING.LESSER. If | |
17 | ;; not, write to the Free Software Foundation, Inc., 51 Franklin | |
18 | ;; Street, Fifth Floor, Boston, MA 02110-1301 USA | |
57737e24 TTN |
19 | |
20 | ;;; Author: Thien-Thi Nguyen <ttn@gnu.org> | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
24 | ;; Usage: read-rfc822 FILE | |
25 | ;; | |
26 | ;; Read FILE, assumed to be in RFC822 format, and display it to stdout. | |
27 | ;; This is not very interesting, admittedly. | |
28 | ;; | |
29 | ;; For Scheme programming, this module exports two procs: | |
30 | ;; (read-rfc822 . args) ; only first arg used | |
31 | ;; (read-rfc822-silently port) | |
32 | ;; | |
33 | ;; Parse FILE (a string) or PORT, respectively, and return a query proc that | |
34 | ;; takes a symbol COMP, and returns the message component COMP. Supported | |
35 | ;; values for COMP (and the associated query return values) are: | |
36 | ;; from -- #f (reserved for future mbox support) | |
37 | ;; headers -- alist of (HEADER-SYMBOL . "VALUE-STRING") pairs, in order | |
38 | ;; body -- rest of the mail message, a string | |
39 | ;; body-lines -- rest of the mail message, as a list of lines | |
40 | ;; Any other query results in a "bad component" error. | |
41 | ;; | |
42 | ;; TODO: Add "-m" option (mbox support). | |
43 | ||
44 | ;;; Code: | |
45 | ||
46 | (define-module (scripts read-rfc822) | |
47 | :use-module (ice-9 regex) | |
48 | :use-module (ice-9 rdelim) | |
49 | :autoload (srfi srfi-13) (string-join) | |
50 | :export (read-rfc822 read-rfc822-silently)) | |
51 | ||
a1a2ed53 AW |
52 | (define %include-in-guild-list #f) |
53 | (define %summary "Validate an RFC822-style file.") | |
54 | ||
57737e24 TTN |
55 | (define from-line-rx (make-regexp "^From ")) |
56 | (define header-name-rx (make-regexp "^([^:]+):[ \t]*")) | |
57 | (define header-cont-rx (make-regexp "^[ \t]+")) | |
58 | ||
59 | (define option #f) ; for future "-m" | |
60 | ||
61 | (define (drain-message port) | |
62 | (let loop ((line (read-line port)) (acc '())) | |
63 | (cond ((eof-object? line) | |
64 | (reverse acc)) | |
65 | ((and option (regexp-exec from-line-rx line)) | |
66 | (for-each (lambda (c) | |
67 | (unread-char c port)) | |
68 | (cons #\newline | |
69 | (reverse (string->list line)))) | |
70 | (reverse acc)) | |
71 | (else | |
72 | (loop (read-line port) (cons line acc)))))) | |
73 | ||
74 | (define (parse-message port) | |
75 | (let* ((from (and option | |
76 | (match:suffix (regexp-exec from-line-rx | |
77 | (read-line port))))) | |
78 | (body-lines #f) | |
79 | (body #f) | |
80 | (headers '()) | |
81 | (add-header! (lambda (reversed-hlines) | |
82 | (let* ((hlines (reverse reversed-hlines)) | |
83 | (first (car hlines)) | |
84 | (m (regexp-exec header-name-rx first)) | |
85 | (name (string->symbol (match:substring m 1))) | |
86 | (data (string-join | |
87 | (cons (substring first (match:end m)) | |
88 | (cdr hlines)) | |
89 | " "))) | |
90 | (set! headers (acons name data headers)))))) | |
91 | ;; "From " is only one line | |
92 | (let loop ((line (read-line port)) (current-header #f)) | |
93 | (cond ((string-null? line) | |
94 | (and current-header (add-header! current-header)) | |
95 | (set! body-lines (drain-message port))) | |
96 | ((regexp-exec header-cont-rx line) | |
97 | => (lambda (m) | |
6c60ac51 | 98 | (loop (read-line port) |
57737e24 TTN |
99 | (cons (match:suffix m) current-header)))) |
100 | (else | |
101 | (and current-header (add-header! current-header)) | |
102 | (loop (read-line port) (list line))))) | |
103 | (set! headers (reverse headers)) | |
104 | (lambda (component) | |
105 | (case component | |
106 | ((from) from) | |
107 | ((body-lines) body-lines) | |
108 | ((headers) headers) | |
109 | ((body) (or body | |
110 | (begin (set! body (string-join body-lines "\n" 'suffix)) | |
111 | body))) | |
112 | (else (error "bad component:" component)))))) | |
113 | ||
114 | (define (read-rfc822-silently port) | |
115 | (parse-message port)) | |
116 | ||
117 | (define (display-rfc822 parse) | |
118 | (cond ((parse 'from) => (lambda (from) (format #t "From ~A\n" from)))) | |
119 | (for-each (lambda (header) | |
120 | (format #t "~A: ~A\n" (car header) (cdr header))) | |
121 | (parse 'headers)) | |
122 | (format #t "\n~A" (parse 'body))) | |
123 | ||
124 | (define (read-rfc822 . args) | |
125 | (let ((parse (read-rfc822-silently (open-file (car args) OPEN_READ)))) | |
126 | (display-rfc822 parse)) | |
127 | #t) | |
128 | ||
129 | (define main read-rfc822) | |
130 | ||
131 | ;;; read-rfc822 ends here |