read-rfc822.scm 5.0 KB

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