2 ;;; tl-822.el --- RFC 822 parser for GNU Emacs
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1995 MORIOKA Tomohiko
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; $Id: tl-822.el,v 2.0 1995-10-05 11:26:43 morioka Exp $
10 ;;; Keywords: mail, news, RFC 822
12 ;;; This file is part of tm (Tools for MIME).
21 (defconst rfc822/field-name-regexp "[!-9;-~]+")
23 (defconst rfc822::next-field-top-regexp
24 (concat "\n" rfc822/field-name-regexp ":"))
26 (defun rfc822/field-end ()
27 (if (re-search-forward rfc822::next-field-top-regexp nil t)
28 (goto-char (match-beginning 0))
29 (if (re-search-forward "^$" nil t)
30 (goto-char (1- (match-beginning 0)))
36 (defun rfc822/get-field-body (name)
37 (let ((case-fold-search t))
41 (goto-char (point-min))
42 (or (and (re-search-forward "^$" nil t) (match-end 0))
45 (goto-char (point-min))
46 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
47 (buffer-substring-no-properties
57 (defun rfc822/strip-quoted-pair (str)
63 (setq chr (elt str i))
64 (if (or flag (not (eq chr ?\\)))
66 (setq dest (concat dest (char-to-string chr)))
75 (defun rfc822/strip-quoted-string (str)
76 (rfc822/strip-quoted-pair
77 (let ((max (- (length str) 1))
79 (if (and (eq (elt str 0) ?\")
80 (eq (elt str max) ?\")
90 (defun rfc822/unfolding-string (str)
92 (while (string-match "\n\\s +" str)
93 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
94 (setq str (substring str (match-end 0)))
100 ;;; @ lexical analyze
103 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
104 (defconst rfc822/space-chars " \t\n")
105 (defconst rfc822/non-atom-chars
106 (concat rfc822/special-chars rfc822/space-chars))
107 (defconst rfc822/non-qtext-chars "\"")
108 (defconst rfc822/non-dtext-chars "[]")
109 (defconst rfc822/non-ctext-chars "()")
111 (defun rfc822/analyze-spaces (str)
112 (let ((i (position-mismatched
115 (find elt rfc822/space-chars)
119 (cons (cons 'spaces (substring str 0 i))
124 (defun rfc822/analyze-special (str)
125 (if (and (> (length str) 0)
126 (find (elt str 0) rfc822/special-chars)
128 (cons (cons 'specials (substring str 0 1))
133 (defun rfc822/analyze-atom (str)
134 (let ((i (position-mismatched
137 (not (find elt rfc822/non-atom-chars))
141 (cons (cons 'atom (substring str 0 i))
146 (defun rfc822/analyze-quoted-pair (str)
147 (if (and (>= (length str) 2)
150 (cons (cons 'quoted-pair (substring str 0 2))
155 (defun rfc822/analyze-quoted-string (str)
156 (if (and (> (length str) 0)
159 (let* ((i (position-mismatched
162 (not (find elt rfc822/non-qtext-chars))
164 (setq str (substring str 1))
166 (rest (substring str i))
170 (eq (elt rest 0) ?\")
172 (cons (cons 'quoted-string (substring str 0 i))
177 (defun rfc822/analyze-domain-literal (str)
178 (if (and (> (length str) 0)
181 (let* ((i (position-mismatched
184 (not (find elt rfc822/non-dtext-chars))
186 (setq str (substring str 1))
188 (rest (substring str i))
192 (eq (elt rest 0) ?\])
194 (cons (cons 'domain-literal (substring str 0 i))
199 (defun rfc822/analyze-comment (str)
200 (if (and (> (length str) 0)
205 (setq str (substring str 1))
207 (while (not (string-equal str ""))
208 (setq p (position-mismatched
211 (not (find elt rfc822/non-ctext-chars))
214 (setq dest (concat dest (substring str 0 p)))
215 (setq str (substring str p))
217 ((setq ret (rfc822/analyze-comment str))
218 (setq dest (concat dest "(" (cdr (car ret)) ")"))
224 (if (and (> (length str) 0)
227 (cons (cons 'comment dest)
232 (defun rfc822/lexical-analyze (str)
234 (i 0)(len (length str))
236 (while (not (string-equal str ""))
238 (or (rfc822/analyze-quoted-string str)
239 (rfc822/analyze-domain-literal str)
240 (rfc822/analyze-comment str)
241 (rfc822/analyze-spaces str)
242 (rfc822/analyze-special str)
243 (rfc822/analyze-atom str)
246 (setq dest (cons (car ret) dest))
256 (defun rfc822/ignored-token-p (token)
257 (let ((type (car token)))
258 (or (eq type 'spaces)(eq type 'comment))
261 (defun rfc822/parse-token (lal)
265 (setq token (car lal))
266 (rfc822/ignored-token-p token)
269 (setq itl (cons token itl))
271 (cons (reverse (cons token itl))
275 (defun rfc822/parse-token-or-comment (lal)
279 (setq token (car lal))
280 (eq (car token) 'spaces)
283 (setq itl (cons token itl))
285 (cons (reverse (cons token itl))
289 (defun rfc822/parse-word (lal)
290 (let ((ret (rfc822/parse-token lal)))
292 (let ((elt (car ret))
295 (if (or (assq 'atom elt)
296 (assq 'quoted-string elt))
297 (cons (cons 'word elt) rest)
300 (defun rfc822/parse-word-or-comment (lal)
301 (let ((ret (rfc822/parse-token-or-comment lal)))
303 (let ((elt (car ret))
306 (cond ((or (assq 'atom elt)
307 (assq 'quoted-string elt))
308 (cons (cons 'word elt) rest)
311 (cons (cons 'comment-word elt) rest)
315 (defun rfc822/parse-phrase (lal)
317 (while (setq ret (rfc822/parse-word-or-comment lal))
318 (setq phrase (append phrase (cdr (car ret))))
322 (cons (cons 'phrase phrase) lal)
325 (defun rfc822/parse-local-part (lal)
326 (let ((ret (rfc822/parse-word lal)))
328 (let ((local-part (cdr (car ret))) dot)
330 (while (and (setq ret (rfc822/parse-token lal))
332 (equal (cdr (assq 'specials dot)) ".")
333 (setq ret (rfc822/parse-word (cdr ret)))
335 (append local-part dot (cdr (car ret)))
339 (cons (cons 'local-part local-part) lal)
342 (defun rfc822/parse-sub-domain (lal)
343 (let ((ret (rfc822/parse-token lal)))
345 (let ((sub-domain (car ret)))
346 (if (or (assq 'atom sub-domain)
347 (assq 'domain-literal sub-domain)
349 (cons (cons 'sub-domain sub-domain)
354 (defun rfc822/parse-domain (lal)
355 (let ((ret (rfc822/parse-sub-domain lal)))
357 (let ((domain (cdr (car ret))) dot)
359 (while (and (setq ret (rfc822/parse-token lal))
361 (equal (cdr (assq 'specials dot)) ".")
362 (setq ret (rfc822/parse-sub-domain (cdr ret)))
364 (append domain dot (cdr (car ret)))
368 (cons (cons 'domain domain) lal)
371 (defun rfc822/parse-at-domain (lal)
372 (let ((ret (rfc822/parse-token lal)) at-sign)
374 (setq at-sign (car ret))
375 (equal (cdr (assq 'specials at-sign)) "@")
376 (setq ret (rfc822/parse-domain (cdr ret)))
378 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
382 (defun rfc822/parse-addr-spec (lal)
383 (let ((ret (rfc822/parse-local-part lal))
387 (setq addr (cdr (car ret)))
389 (and (setq ret (rfc822/parse-at-domain lal))
390 (setq addr (append addr (cdr (car ret))))
393 (cons (cons 'addr-spec addr) lal)
396 (defun rfc822/parse-route (lal)
397 (let ((ret (rfc822/parse-at-domain lal))
401 (setq route (cdr (car ret)))
403 (while (and (setq ret (rfc822/parse-token lal))
404 (setq comma (car ret))
405 (equal (cdr (assq 'specials comma)) ",")
406 (setq ret (rfc822/parse-at-domain (cdr ret)))
408 (setq route (append route comma (cdr (car ret))))
411 (and (setq ret (rfc822/parse-token lal))
412 (setq colon (car ret))
413 (equal (cdr (assq 'specials colon)) ":")
414 (setq route (append route colon))
417 (cons (cons 'route route)
422 (defun rfc822/parse-route-addr (lal)
423 (let ((ret (rfc822/parse-token lal))
427 (equal (cdr (assq 'specials <)) "<")
429 (progn (and (setq ret (rfc822/parse-route lal))
430 (setq route (cdr (car ret)))
433 (setq ret (rfc822/parse-addr-spec lal))
435 (setq addr-spec (cdr (car ret)))
437 (setq ret (rfc822/parse-token lal))
439 (equal (cdr (assq 'specials >)) ">")
441 (cons (cons 'route-addr (append route addr-spec))
446 (defun rfc822/parse-phrase-route-addr (lal)
447 (let ((ret (rfc822/parse-phrase lal)) phrase)
450 (setq phrase (cdr (car ret)))
453 (if (setq ret (rfc822/parse-route-addr lal))
454 (cons (list 'phrase-route-addr
460 (defun rfc822/parse-mailbox (lal)
461 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
462 (rfc822/parse-addr-spec lal)))
466 (setq mbox (car ret))
468 (if (and (setq ret (rfc822/parse-token-or-comment lal))
469 (setq comment (cdr (assq 'comment (car ret))))
473 (cons (list 'mailbox mbox comment)
477 (defun rfc822/parse-group (lal)
478 (let ((ret (rfc822/parse-phrase lal))
479 phrase : comma mbox semicolon)
481 (setq phrase (cdr (car ret)))
483 (setq ret (rfc822/parse-token lal))
485 (equal (cdr (assq 'specials :)) ":")
488 (and (setq ret (rfc822/parse-mailbox lal))
489 (setq mbox (list (car ret)))
492 (while (and (setq ret (rfc822/parse-token lal))
493 (setq comma (car ret))
494 (equal (cdr (assq 'specials comma)) ",")
496 (setq ret (rfc822/parse-mailbox lal))
497 (setq mbox (cons (car ret) mbox))
501 (and (setq ret (rfc822/parse-token lal))
502 (setq semicolon (car ret))
503 (equal (cdr (assq 'specials semicolon)) ";")
505 (cons (list 'group phrase (reverse mbox))
510 (defun rfc822/parse-address (lal)
511 (or (rfc822/parse-group lal)
512 (rfc822/parse-mailbox lal)
515 (defun rfc822/parse-addresses (lal)
516 (let ((ret (rfc822/parse-address lal)))
518 (let ((dest (list (car ret))))
520 (while (and (setq ret (rfc822/parse-token lal))
521 (equal (cdr (assq 'specials (car ret))) ",")
522 (setq ret (rfc822/parse-address (cdr ret)))
524 (setq dest (cons (car ret) dest))
530 (defun rfc822/addr-to-string (seq)
533 (if (eq (car token) 'spaces)
540 (defun rfc822/address-string (address)
541 (if (eq (car address) 'mailbox)
542 (let ((addr (nth 1 address))
544 (rfc822/addr-to-string
545 (if (eq (car addr) 'phrase-route-addr)
551 (defun rfc822/full-name-string (address)
552 (if (eq (car address) 'mailbox)
553 (let ((addr (nth 1 address))
554 (comment (nth 2 address))
556 (if (eq (car addr) 'phrase-route-addr)
557 (setq phrase (mapconcat (function