Debian Codegraph @

libsrc/rfc/822.scm

raw
1 ;;;
2 ;;; 822.scm - parsing RFC2822 style message
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; Parser and constructor of the message defined in
35 ;; RFC2822 Internet Message Format
36 ;; http://www.ietf.org/rfc/rfc2822.txt
37
38 (define-module rfc.822
39 (use scheme.list)
40 (use srfi.13)
41 (use srfi.19)
42 (use text.parse)
43 (use gauche.regexp)
44 (use util.match)
45 (export <rfc822-parse-error> rfc822-parse-errorf
46 rfc822-read-headers rfc822-header->list
47 rfc822-header-ref rfc822-header-ref* rfc822-header-put
48 rfc822-skip-cfws
49 *rfc822-atext-chars* *rfc822-standard-tokenizers*
50 rfc822-atom rfc822-dot-atom rfc822-quoted-string
51 rfc822-next-token rfc822-field->tokens
52 rfc822-parse-date rfc822-date->date date->rfc822-date
53
54 rfc822-invalid-header-field rfc822-write-headers
55 )
56 )
57 (select-module rfc.822)
58
59 ;;=================================================================
60 ;; Parsers
61 ;;
62
63 ;;-----------------------------------------------------------------
64 ;; A condition reported when rfc822 parser finds an error.
65 ;;
66 ;; For most practical applications, it is useful to handle
67 ;; rfc822 syntax errors gracefully. So the rfc822 and mime procedures
68 ;; tries to return a reasonable value even if the input is not strictly
69 ;; conforming the RFCs. Some routines have an option to be more strict,
70 ;; and they raise <rfc822-parse-error> when the option is given and
71 ;; they find a violation.
72 ;;
73 ;; TODO: We need some consistent way to switch these 'graceful' and
74 ;; 'strict' modes throughout the libraries; otherwise the API would be
75 ;; pretty confusing. Something to consider as we add more high-level
76 ;; parsers.
77
78 (define-condition-type <rfc822-parse-error> <error> #f
79 (header-field-name)
80 (header-field-body))
81
82 (define (rfc822-parse-errorf name body fmt . args)
83 (apply errorf <rfc822-parse-error>
84 :header-field-name name
85 :header-field-body body
86 fmt args))
87
88 ;;-----------------------------------------------------------------
89 ;; Generic header parser. Returns ((name body) ...)
90 ;; Does process unfolding.
91 ;; May throw <rfc822-parse-error> if :strict? is true.
92 (define (rfc822-read-headers iport :key (strict? #f)
93 (reader (cut read-line <> #t)))
94
95 (define (accum name bodies r)
96 (cons (list name (string-concatenate-reverse bodies)) r))
97
98 (define (drop-leading-fws body)
99 (if (string-incomplete? body)
100 body ;; this message is not RFC2822 compliant anyway
101 (string-trim body)))
102
103 (let loop ([r '()]
104 [line (reader iport)])
105 (cond
106 [(eof-object? line) (reverse! r)]
107 [(string-null? line) (reverse! r)]
108 [else
109 (receive (n body) (string-scan line #\: 'both)
110 (let1 name (and-let* ([ (string? n) ]
111 [name (string-incomplete->complete n)]
112 [name (string-trim-both name)]
113 [ (string-every #[\u0021-\u0039\u003b-\u007e] name) ])
114 (string-downcase name))
115 (cond
116 [name
117 (let loop2 ([nline (reader iport)]
118 [bodies (list (drop-leading-fws body))])
119 (cond [(eof-object? nline)
120 ;; maybe premature end of the message
121 (if strict?
122 (rfc822-parse-errorf
123 #f #f "premature end of message header")
124 (reverse! (accum name bodies r)))]
125 [(string-null? nline) ;; end of the header
126 (reverse! (accum name bodies r))]
127 [(memv (string-byte-ref nline 0) '(9 32))
128 ;; careful for byte strings
129 (loop2 (reader iport) (cons nline bodies))]
130 [else
131 (loop (accum name bodies r) nline)]))]
132 [strict? (rfc822-parse-errorf #f #f "bad header line: ~s" line)]
133 [else (loop r (reader iport))])))])
134 ))
135
136 (define (rfc822-header-ref headers field-name :optional (default #f))
137 (assume-type field-name <string>)
138 (cond [(assoc field-name headers) => cadr]
139 [else default]))
140
141 (define (rfc822-header-ref* headers field-name)
142 (assume-type field-name <string>)
143 (filter-map (^e (and (equal? (car e) field-name) (cadr e))) headers))
144
145 (define (rfc822-header-put headers field-name value)
146 (assume-type field-name <string>)
147 (assume-type value <string>)
148 (let1 canon-name (string-downcase field-name)
149 (cons `(,canon-name ,value)
150 (alist-delete canon-name headers equal?))))
151
152 ;; backward compatibility
153 (define rfc822-header->list rfc822-read-headers)
154
155 ;;------------------------------------------------------------------
156 ;; Comments, quoted pairs, atoms and quoted string. Section 3.2
157 ;;
158
159 ;; skip comments and white spaces, then returns the head char.
160 (define (rfc822-skip-cfws input)
161 (define (scan c)
162 (cond [(eof-object? c) c]
163 [(char=? c #\( ) (in-comment (peek-next-char input))]
164 [(char-whitespace? c) (scan (peek-next-char input))]
165 [else c]))
166 (define (in-comment c)
167 (cond [(eof-object? c) c]
168 [(char=? c #\) ) (scan (peek-next-char input))]
169 [(char=? c #\\ ) (read-char input) (in-comment (peek-next-char input))]
170 [(char=? c #\( ) (in-comment (in-comment (peek-next-char input)))]
171 [else (in-comment (peek-next-char input))]))
172 (scan (peek-char input)))
173
174 ;; Basic tokenizers. Supposed to be used for higher-level parsers.
175
176 (define-constant *rfc822-atext-chars* #[A-Za-z0-9!#$%&'*+/=?^_`{|}~-])
177
178 (define (rfc822-atom input)
179 (next-token-of *rfc822-atext-chars* input))
180
181 ;; NB: this is loose, but usually OK.
182 (define (rfc822-dot-atom input)
183 (next-token-of `(,*rfc822-atext-chars* #\.) input))
184
185 ;; Assuming the first char in input is DQUOTE
186 (define (rfc822-quoted-string input)
187 (let1 r (open-output-string :private? #t)
188 (define (finish) (get-output-string r))
189 (let loop ([c (peek-next-char input)])
190 (cond [(eof-object? c) (finish)];; tolerate missing closing DQUOTE
191 [(char=? c #\") (read-char input) (finish)] ;; discard DQUOTE
192 [(char=? c #\\)
193 (let1 c (peek-next-char input)
194 (cond [(eof-object? c) (finish)] ;; tolerate stray backslash
195 [else (write-char c r) (loop (peek-next-char input))]))]
196 [else (write-char c r) (loop (peek-next-char input))]))))
197
198 ;; Default tokenizer table
199 (define *rfc822-standard-tokenizers*
200 `((#[\"] . ,rfc822-quoted-string)
201 (,*rfc822-atext-chars* . ,rfc822-dot-atom)))
202
203 ;; Returns the next token or EOF
204 (define (rfc822-next-token input :optional (table *rfc822-standard-tokenizers*))
205 (let ([toktab (map (^e (if (char-set? e)
206 (cons e (cut next-token-of e <>))
207 e))
208 table)]
209 [c (rfc822-skip-cfws input)])
210 (cond [(eof-object? c) c]
211 [(find (^e (char-set-contains? (car e) c)) toktab)
212 => (^e ((cdr e) input))]
213 [else (read-char input)])))
214
215 ;; returns a list of tokens, for convenience
216 (define (rfc822-field->tokens field . opts)
217 (call-with-input-string field
218 (cut port->list (cut apply rfc822-next-token <> opts) <>)))
219
220 ;;------------------------------------------------------------------
221 ;; Date and time, section 3.3
222 ;;
223
224 ;; Takes RFC-822 type date string, and returns eight values:
225 ;; year, month, day-of-month, hour, minutes, seconds, timezone, day-of-week.
226 ;; Timezone is an offset from UT in minutes. Day-of-week is a day from
227 ;; sunday, and may be #f if that information is not available.
228 ;; If the string is not parsable, all the elements are #f.
229
230 ;; NB: This function follows the new definition of date format in RFC2822,
231 ;; but may fail to recognize "obsolete" format, which allows arbitrary
232 ;; comments appear between words.
233
234 (define (rfc822-parse-date string)
235 (define (dow->number dow)
236 (list-index (cut string=? <> dow)
237 '("Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat")))
238 (define (mon->number mon)
239 (+ 1 (list-index (cut string=? <> mon)
240 '("Jan" "Feb" "Mar" "Apr" "May" "Jun"
241 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))))
242 (define (year->number year) ;; see obs-year definition of RFC2822
243 (and-let* ([y (string->number year)])
244 (cond [(< y 50) (+ y 2000)]
245 [(< y 100) (+ y 1900)]
246 [else y])))
247 (define (tz->number tz)
248 (cond [(equal? tz "-0000") #f] ;;no effective TZ info; see 3.3 of RFC2822
249 [(string->number tz)]
250 [(assoc-ref '(("UT" . 0) ("GMT" . 0) ("EDT" . -400) ("EST" . -500)
251 ("CDT" . -500) ("CST" . -600) ("MDT" . -600)
252 ("MST" . -700) ("PDT" . -700) ("PST" . -800)) tz)]
253 [else #f]))
254
255 (rxmatch-case string
256 (#/((Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s*,)?\s*(\d+)\s*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s*(\d\d(\d\d)?)\s+(\d\d)\s*:\s*(\d\d)(\s*:\s*(\d\d))?(\s+([+-]\d\d\d\d|[A-Z][A-Z][A-Z]?))?/
257 (#f bebe dow dom mon yr #f hour min #f sec #f tz)
258 (values (year->number yr)
259 (mon->number mon)
260 (string->number dom)
261 (string->number hour)
262 (string->number min)
263 (and sec (string->number sec))
264 (and tz (tz->number tz))
265 (and dow (dow->number dow))))
266 (else (values #f #f #f #f #f #f #f #f))))
267
268 ;; returns it by SRFI-19 date
269 (define (rfc822-date->date string)
270 (receive (year month day hour min sec tz . rest)
271 (rfc822-parse-date string)
272 (and year
273 (make-date 0 sec min hour day month year
274 (receive (quot rem) (quotient&remainder tz 100)
275 (+ (* quot 3600) (* rem 60)))))))
276
277 ;; inverse of rfc822-date->date. Take SRFI-19 date and returns
278 ;; formatted string.
279 (define (date->rfc822-date date)
280 (let1 tz (date-zone-offset date)
281 (format "~a, ~2d ~a ~4d ~2,'0d:~2,'0d:~2,'0d ~a~2,'0d~2,'0d"
282 (~ '#("Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat")
283 (date-week-day date))
284 (date-day date)
285 (~ '#("" "Jan" "Feb" "Mar" "Apr" "May" "Jun"
286 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
287 (date-month date))
288 (date-year date)
289 (date-hour date) (date-minute date) (date-second date)
290 (if (>= tz 0) "+" "-")
291 (quotient (abs tz) 3600)
292 (modulo (quotient (abs tz) 60) 60))))
293
294 ;;------------------------------------------------------------------
295 ;; Address specification (Section 3.4)
296 ;;
297
298 ;; The EBNF syntax in RFC2822 requires arbitrary lookahead,
299 ;; so straight recursive-descent parser won't work.
300 ;;
301
302 ;; to be written
303
304 ;;=================================================================
305 ;; Constructors
306 ;;
307
308 ;; Writes out the header fields specified by HEADERS, which is
309 ;; ((name body) ...).
310 (define (rfc822-write-headers headers :key
311 (output (current-output-port))
312 (check :error) ; #f, :ignore, :error or proc
313 (continue #f))
314 (define (process headers)
315 (dolist [field headers]
316 (display (car field) output)
317 (display ": " output)
318 (display (cadr field) output)
319 (unless (string-suffix? "\r\n" (cadr field))
320 (display "\r\n" output)))
321 (unless continue (display "\r\n" output)))
322 (define (bad name body reason)
323 (errorf "Illegal RFC2822 header field data (~a): ~a: ~,,,,80:a" reason name body))
324 (if (memv check '(#f :ignore))
325 (process headers)
326 (let loop ([hs headers]
327 [hs2 '()])
328 (match hs
329 [() (process (reverse hs2))]
330 [((name body) . rest)
331 (cond [(rfc822-invalid-header-field (string-append name ": " body))
332 => (^[reason]
333 (if (eq? check :error)
334 (bad name body reason)
335 (receive (name2 body2) (check name body reason)
336 (if (and (equal? name name2) (equal? body body2))
337 (bad name body reason)
338 (loop `((,name2 ,body2) . ,rest) hs2)))))]
339 [else (loop rest `((,name ,body) . ,hs2))])]
340 [else
341 (error "Invalid header data:" headers)]))))
342
343 (define (rfc822-invalid-header-field body)
344 (cond [(string-incomplete? body) 'incomplete-string]
345 [(string-index body #[^\u0001-\u007f]) 'bad-character]
346 [else
347 (let1 lines (string-split body "\r\n ")
348 (cond [(any (^s (> (string-size s) 998)) lines) 'line-too-long]
349 [(any (^s (string-index s #[\u000d\u000a])) lines) 'stray-crlf]
350 [else #f]))]))