read-rfc822 5.1 KB

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