2 ;;; tl-822.el --- RFC 822 parser for GNU Emacs
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1995,1996 MORIOKA Tomohiko
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Keywords: mail, news, RFC 822
10 ;;; This file is part of tl (Tiny Library).
12 ;;; This program is free software; you can redistribute it and/or
13 ;;; modify it under the terms of the GNU General Public License as
14 ;;; published by the Free Software Foundation; either version 2, or
15 ;;; (at your option) any later version.
17 ;;; This program is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;;; General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with This program. If not, write to the Free Software
24 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 (defconst rfc822/RCS-ID
33 "$Id: tl-822.el,v 7.25 1996-05-17 08:12:46 morioka Exp $")
34 (defconst rfc822/version (get-version-string rfc822/RCS-ID))
40 (defun rfc822/narrow-to-header (&optional boundary)
41 (narrow-to-region (goto-char (point-min))
42 (if (re-search-forward
43 (concat "^\\(" (regexp-quote
44 (or boundary "")) "\\)?$") nil t)
49 (defun rfc822/get-header-string (pat &optional boundary)
50 (let ((case-fold-search t))
53 (rfc822/narrow-to-header boundary)
54 (goto-char (point-min))
56 (while (re-search-forward rfc822/field-top-regexp nil t)
57 (setq field (buffer-substring (match-beginning 0)
60 (if (string-match pat field)
61 (setq header (concat header field "\n"))
66 (defun rfc822/get-header-string-except (pat &optional boundary)
67 (let ((case-fold-search t))
70 (rfc822/narrow-to-header boundary)
71 (goto-char (point-min))
73 (while (re-search-forward rfc822/field-top-regexp nil t)
74 (setq field (buffer-substring (match-beginning 0)
77 (if (not (string-match pat field))
78 (setq header (concat header field "\n"))
87 (defconst rfc822/field-name-regexp "[!-9;-~]+")
89 (defconst rfc822/field-top-regexp
90 (concat "\\(" rfc822/field-name-regexp "\\):"))
92 (defconst rfc822::next-field-top-regexp (concat "\n" rfc822/field-top-regexp))
94 (defun rfc822/get-field-names (&optional boundary)
97 (rfc822/narrow-to-header boundary)
98 (goto-char (point-min))
99 (let ((pat (concat "^\\(" rfc822/field-name-regexp "\\):"))
101 (while (re-search-forward pat nil t)
102 (setq name (buffer-substring (match-beginning 1)(match-end 1)))
103 (or (member name dest)
104 (setq dest (cons name dest))
109 (defun rfc822/field-end ()
110 (if (re-search-forward rfc822::next-field-top-regexp nil t)
111 (goto-char (match-beginning 0))
112 (if (re-search-forward "^$" nil t)
113 (goto-char (1- (match-beginning 0)))
119 (defun rfc822/get-field-body (name &optional boundary)
120 (let ((case-fold-search t))
123 (rfc822/narrow-to-header boundary)
124 (goto-char (point-min))
125 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
126 (buffer-substring-no-properties
132 (defun rfc822/get-field-bodies (field-names &optional default-value boundary)
133 (let ((case-fold-search t))
136 (rfc822/narrow-to-header boundary)
137 (let* ((dest (make-list (length field-names) default-value))
141 (while (setq field-name (car s-rest))
142 (goto-char (point-min))
143 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
145 (buffer-substring-no-properties
147 (rfc822/field-end))))
148 (setq s-rest (cdr s-rest)
157 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
158 (defconst rfc822/quoted-pair-regexp "\\\\.")
159 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
160 (defconst rfc822/qtext-regexp
161 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) " \t]"))
162 (defconst rfc822/quoted-string-regexp
166 "\\(" rfc822/linear-white-space-regexp "?"
167 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
169 rfc822/linear-white-space-regexp "?"
172 (defun rfc822/wrap-as-quoted-string (str)
173 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
177 (if (memq chr rfc822/non-qtext-char-list)
178 (concat "\\" (char-to-string chr))
184 (defun rfc822/strip-quoted-pair (str)
190 (setq chr (elt str i))
191 (if (or flag (not (eq chr ?\\)))
193 (setq dest (concat dest (char-to-string chr)))
202 (defun rfc822/strip-quoted-string (str)
203 (rfc822/strip-quoted-pair
204 (let ((max (- (length str) 1))
206 (if (and (eq (elt str 0) ?\")
207 (eq (elt str max) ?\")
209 (substring str 1 max)
217 (defun rfc822/unfolding-string (str)
219 (while (string-match "\n\\s +" str)
220 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
221 (setq str (substring str (match-end 0)))
227 ;;; @ lexical analyze
230 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
231 (defconst rfc822/space-chars " \t\n")
232 (defconst rfc822/non-atom-chars
233 (concat rfc822/special-chars rfc822/space-chars))
234 (defconst rfc822/non-dtext-chars "[]")
235 (defconst rfc822/non-ctext-chars "()")
237 (defun rfc822/analyze-spaces (str)
238 (let ((i (position-mismatched
241 (find elt rfc822/space-chars)
245 (cons (cons 'spaces (substring str 0 i))
250 (defun rfc822/analyze-special (str)
251 (if (and (> (length str) 0)
252 (find (elt str 0) rfc822/special-chars)
254 (cons (cons 'specials (substring str 0 1))
259 (defun rfc822/analyze-atom (str)
260 (let ((i (position-mismatched
263 (not (find elt rfc822/non-atom-chars))
267 (cons (cons 'atom (substring str 0 i))
272 (defun rfc822/analyze-quoted-pair (str)
273 (if (and (>= (length str) 2)
276 (cons (cons 'quoted-pair (substring str 0 2))
281 (defun rfc822/analyze-quoted-string (str)
282 (if (and (> (length str) 0)
285 (let* ((i (position-mismatched
288 (not (memq elt rfc822/non-qtext-char-list))
290 (setq str (substring str 1))
292 (rest (substring str i))
296 (eq (elt rest 0) ?\")
298 (cons (cons 'quoted-string (substring str 0 i))
303 (defun rfc822/analyze-domain-literal (str)
304 (if (and (> (length str) 0)
307 (let* ((i (position-mismatched
310 (not (find elt rfc822/non-dtext-chars))
312 (setq str (substring str 1))
314 (rest (substring str i))
318 (eq (elt rest 0) ?\])
320 (cons (cons 'domain-literal (substring str 0 i))
325 (defun rfc822/analyze-comment (str)
326 (if (and (> (length str) 0)
331 (setq str (substring str 1))
333 (while (not (string-equal str ""))
334 (setq p (position-mismatched
337 (not (find elt rfc822/non-ctext-chars))
340 (setq dest (concat dest (substring str 0 p)))
341 (setq str (substring str p))
343 ((setq ret (rfc822/analyze-comment str))
344 (setq dest (concat dest "(" (cdr (car ret)) ")"))
350 (if (and (> (length str) 0)
353 (cons (cons 'comment dest)
358 (defun rfc822/lexical-analyze (str)
360 (while (not (string-equal str ""))
362 (or (rfc822/analyze-quoted-string str)
363 (rfc822/analyze-domain-literal str)
364 (rfc822/analyze-comment str)
365 (rfc822/analyze-spaces str)
366 (rfc822/analyze-special str)
367 (rfc822/analyze-atom str)
370 (setq dest (cons (car ret) dest))
380 (defun rfc822/ignored-token-p (token)
381 (let ((type (car token)))
382 (or (eq type 'spaces)(eq type 'comment))
385 (defun rfc822/parse-token (lal)
389 (setq token (car lal))
390 (rfc822/ignored-token-p token)
393 (setq itl (cons token itl))
395 (cons (nreverse (cons token itl))
399 (defun rfc822/parse-ascii-token (lal)
400 (let (token itl parsed token-value)
402 (setq token (car lal))
403 (if (and (setq token-value (cdr token))
404 (find-charset-string token-value)
407 (rfc822/ignored-token-p token)
410 (setq itl (cons token itl))
413 (setq parsed (nreverse (cons token itl)))
415 (cons parsed (cdr lal))
418 (defun rfc822/parse-token-or-comment (lal)
422 (setq token (car lal))
423 (eq (car token) 'spaces)
426 (setq itl (cons token itl))
428 (cons (nreverse (cons token itl))
432 (defun rfc822/parse-word (lal)
433 (let ((ret (rfc822/parse-ascii-token lal)))
435 (let ((elt (car ret))
438 (if (or (assq 'atom elt)
439 (assq 'quoted-string elt))
440 (cons (cons 'word elt) rest)
443 (defun rfc822/parse-word-or-comment (lal)
444 (let ((ret (rfc822/parse-token-or-comment lal)))
446 (let ((elt (car ret))
449 (cond ((or (assq 'atom elt)
450 (assq 'quoted-string elt))
451 (cons (cons 'word elt) rest)
454 (cons (cons 'comment-word elt) rest)
458 (defun rfc822/parse-phrase (lal)
460 (while (setq ret (rfc822/parse-word-or-comment lal))
461 (setq phrase (append phrase (cdr (car ret))))
465 (cons (cons 'phrase phrase) lal)
468 (defun rfc822/parse-local-part (lal)
469 (let ((ret (rfc822/parse-word lal)))
471 (let ((local-part (cdr (car ret))) dot)
473 (while (and (setq ret (rfc822/parse-ascii-token lal))
475 (string-equal (cdr (assq 'specials dot)) ".")
476 (setq ret (rfc822/parse-word (cdr ret)))
478 (append local-part dot (cdr (car ret)))
482 (cons (cons 'local-part local-part) lal)
485 (defun rfc822/parse-sub-domain (lal)
486 (let ((ret (rfc822/parse-ascii-token lal)))
488 (let ((sub-domain (car ret)))
489 (if (or (assq 'atom sub-domain)
490 (assq 'domain-literal sub-domain)
492 (cons (cons 'sub-domain sub-domain)
497 (defun rfc822/parse-domain (lal)
498 (let ((ret (rfc822/parse-sub-domain lal)))
500 (let ((domain (cdr (car ret))) dot)
502 (while (and (setq ret (rfc822/parse-ascii-token lal))
504 (string-equal (cdr (assq 'specials dot)) ".")
505 (setq ret (rfc822/parse-sub-domain (cdr ret)))
507 (append domain dot (cdr (car ret)))
511 (cons (cons 'domain domain) lal)
514 (defun rfc822/parse-at-domain (lal)
515 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
517 (setq at-sign (car ret))
518 (string-equal (cdr (assq 'specials at-sign)) "@")
519 (setq ret (rfc822/parse-domain (cdr ret)))
521 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
525 (defun rfc822/parse-addr-spec (lal)
526 (let ((ret (rfc822/parse-local-part lal))
530 (setq addr (cdr (car ret)))
532 (and (setq ret (rfc822/parse-at-domain lal))
533 (setq addr (append addr (cdr (car ret))))
536 (cons (cons 'addr-spec addr) lal)
539 (defun rfc822/parse-route (lal)
540 (let ((ret (rfc822/parse-at-domain lal))
544 (setq route (cdr (car ret)))
546 (while (and (setq ret (rfc822/parse-ascii-token lal))
547 (setq comma (car ret))
548 (string-equal (cdr (assq 'specials comma)) ",")
549 (setq ret (rfc822/parse-at-domain (cdr ret)))
551 (setq route (append route comma (cdr (car ret))))
554 (and (setq ret (rfc822/parse-ascii-token lal))
555 (setq colon (car ret))
556 (string-equal (cdr (assq 'specials colon)) ":")
557 (setq route (append route colon))
560 (cons (cons 'route route)
565 (defun rfc822/parse-route-addr (lal)
566 (let ((ret (rfc822/parse-ascii-token lal))
570 (string-equal (cdr (assq 'specials <)) "<")
572 (progn (and (setq ret (rfc822/parse-route lal))
573 (setq route (cdr (car ret)))
576 (setq ret (rfc822/parse-addr-spec lal))
578 (setq addr-spec (cdr (car ret)))
580 (setq ret (rfc822/parse-ascii-token lal))
582 (string-equal (cdr (assq 'specials >)) ">")
584 (cons (cons 'route-addr (append route addr-spec))
589 (defun rfc822/parse-phrase-route-addr (lal)
590 (let ((ret (rfc822/parse-phrase lal)) phrase)
593 (setq phrase (cdr (car ret)))
596 (if (setq ret (rfc822/parse-route-addr lal))
597 (cons (list 'phrase-route-addr
603 (defun rfc822/parse-mailbox (lal)
604 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
605 (rfc822/parse-addr-spec lal)))
609 (setq mbox (car ret))
611 (if (and (setq ret (rfc822/parse-token-or-comment lal))
612 (setq comment (cdr (assq 'comment (car ret))))
616 (cons (list 'mailbox mbox comment)
620 (defun rfc822/parse-group (lal)
621 (let ((ret (rfc822/parse-phrase lal))
622 phrase colon comma mbox semicolon)
624 (setq phrase (cdr (car ret)))
626 (setq ret (rfc822/parse-ascii-token lal))
627 (setq colon (car ret))
628 (string-equal (cdr (assq 'specials colon)) ":")
631 (and (setq ret (rfc822/parse-mailbox lal))
632 (setq mbox (list (car ret)))
635 (while (and (setq ret (rfc822/parse-ascii-token lal))
636 (setq comma (car ret))
638 (cdr (assq 'specials comma)) ",")
640 (setq ret (rfc822/parse-mailbox lal))
641 (setq mbox (cons (car ret) mbox))
645 (and (setq ret (rfc822/parse-ascii-token lal))
646 (setq semicolon (car ret))
647 (string-equal (cdr (assq 'specials semicolon)) ";")
649 (cons (list 'group phrase (nreverse mbox))
654 (defun rfc822/parse-address (lal)
655 (or (rfc822/parse-group lal)
656 (rfc822/parse-mailbox lal)
659 (defun rfc822/parse-addresses (lal)
660 (let ((ret (rfc822/parse-address lal)))
662 (let ((dest (list (car ret))))
664 (while (and (setq ret (rfc822/parse-ascii-token lal))
665 (string-equal (cdr (assq 'specials (car ret))) ",")
666 (setq ret (rfc822/parse-address (cdr ret)))
668 (setq dest (cons (car ret) dest))
674 (defun rfc822/addr-to-string (seq)
677 (if (eq (car token) 'spaces)
684 (defun rfc822/address-string (address)
685 (cond ((eq (car address) 'group)
686 (mapconcat (function rfc822/address-string)
690 ((eq (car address) 'mailbox)
691 (let ((addr (nth 1 address))
693 (rfc822/addr-to-string
694 (if (eq (car addr) 'phrase-route-addr)
700 (defun rfc822/full-name-string (address)
701 (cond ((eq (car address) 'group)
708 ((eq (car address) 'mailbox)
709 (let ((addr (nth 1 address))
710 (comment (nth 2 address))
712 (if (eq (car addr) 'phrase-route-addr)
713 (setq phrase (mapconcat (function
722 (defun rfc822/extract-address-components (str)
723 "Extract full name and canonical address from STR.
724 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
725 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
726 (let* ((structure (car
727 (rfc822/parse-address
728 (rfc822/lexical-analyze str)
730 (phrase (rfc822/full-name-string structure))
731 (address (rfc822/address-string structure))
733 (list phrase address)
742 ;;; tl-822.el ends here