module-{ref,define}-submodule use the submodules table
[bpt/guile.git] / module / scripts / read-rfc822.scm
1 ;;; read-rfc822 --- Validate RFC822 file by displaying it to stdout
2
3 ;; Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
4 ;;
5 ;; This program is free software; you can redistribute it and/or
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
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
13 ;; Lesser General Public License for more details.
14 ;;
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
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
52 (define from-line-rx (make-regexp "^From "))
53 (define header-name-rx (make-regexp "^([^:]+):[ \t]*"))
54 (define header-cont-rx (make-regexp "^[ \t]+"))
55
56 (define option #f) ; for future "-m"
57
58 (define (drain-message port)
59 (let loop ((line (read-line port)) (acc '()))
60 (cond ((eof-object? line)
61 (reverse acc))
62 ((and option (regexp-exec from-line-rx line))
63 (for-each (lambda (c)
64 (unread-char c port))
65 (cons #\newline
66 (reverse (string->list line))))
67 (reverse acc))
68 (else
69 (loop (read-line port) (cons line acc))))))
70
71 (define (parse-message port)
72 (let* ((from (and option
73 (match:suffix (regexp-exec from-line-rx
74 (read-line port)))))
75 (body-lines #f)
76 (body #f)
77 (headers '())
78 (add-header! (lambda (reversed-hlines)
79 (let* ((hlines (reverse reversed-hlines))
80 (first (car hlines))
81 (m (regexp-exec header-name-rx first))
82 (name (string->symbol (match:substring m 1)))
83 (data (string-join
84 (cons (substring first (match:end m))
85 (cdr hlines))
86 " ")))
87 (set! headers (acons name data headers))))))
88 ;; "From " is only one line
89 (let loop ((line (read-line port)) (current-header #f))
90 (cond ((string-null? line)
91 (and current-header (add-header! current-header))
92 (set! body-lines (drain-message port)))
93 ((regexp-exec header-cont-rx line)
94 => (lambda (m)
95 (loop (read-line port)
96 (cons (match:suffix m) current-header))))
97 (else
98 (and current-header (add-header! current-header))
99 (loop (read-line port) (list line)))))
100 (set! headers (reverse headers))
101 (lambda (component)
102 (case component
103 ((from) from)
104 ((body-lines) body-lines)
105 ((headers) headers)
106 ((body) (or body
107 (begin (set! body (string-join body-lines "\n" 'suffix))
108 body)))
109 (else (error "bad component:" component))))))
110
111 (define (read-rfc822-silently port)
112 (parse-message port))
113
114 (define (display-rfc822 parse)
115 (cond ((parse 'from) => (lambda (from) (format #t "From ~A\n" from))))
116 (for-each (lambda (header)
117 (format #t "~A: ~A\n" (car header) (cdr header)))
118 (parse 'headers))
119 (format #t "\n~A" (parse 'body)))
120
121 (define (read-rfc822 . args)
122 (let ((parse (read-rfc822-silently (open-file (car args) OPEN_READ))))
123 (display-rfc822 parse))
124 #t)
125
126 (define main read-rfc822)
127
128 ;;; read-rfc822 ends here