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.22 1996-04-25 22:15:52 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 (i 0)(len (length str))
362 (while (not (string-equal str ""))
364 (or (rfc822/analyze-quoted-string str)
365 (rfc822/analyze-domain-literal str)
366 (rfc822/analyze-comment str)
367 (rfc822/analyze-spaces str)
368 (rfc822/analyze-special str)
369 (rfc822/analyze-atom str)
372 (setq dest (cons (car ret) dest))
382 (defun rfc822/ignored-token-p (token)
383 (let ((type (car token)))
384 (or (eq type 'spaces)(eq type 'comment))
387 (defun rfc822/parse-token (lal)
391 (setq token (car lal))
392 (rfc822/ignored-token-p token)
395 (setq itl (cons token itl))
397 (cons (nreverse (cons token itl))
401 (defun rfc822/parse-ascii-token (lal)
402 (let (token itl parsed token-value)
404 (setq token (car lal))
405 (if (and (setq token-value (cdr token))
406 (find-charset-string token-value)
409 (rfc822/ignored-token-p token)
412 (setq itl (cons token itl))
415 (setq parsed (nreverse (cons token itl)))
417 (cons parsed (cdr lal))
420 (defun rfc822/parse-token-or-comment (lal)
424 (setq token (car lal))
425 (eq (car token) 'spaces)
428 (setq itl (cons token itl))
430 (cons (nreverse (cons token itl))
434 (defun rfc822/parse-word (lal)
435 (let ((ret (rfc822/parse-ascii-token lal)))
437 (let ((elt (car ret))
440 (if (or (assq 'atom elt)
441 (assq 'quoted-string elt))
442 (cons (cons 'word elt) rest)
445 (defun rfc822/parse-word-or-comment (lal)
446 (let ((ret (rfc822/parse-token-or-comment lal)))
448 (let ((elt (car ret))
451 (cond ((or (assq 'atom elt)
452 (assq 'quoted-string elt))
453 (cons (cons 'word elt) rest)
456 (cons (cons 'comment-word elt) rest)
460 (defun rfc822/parse-phrase (lal)
462 (while (setq ret (rfc822/parse-word-or-comment lal))
463 (setq phrase (append phrase (cdr (car ret))))
467 (cons (cons 'phrase phrase) lal)
470 (defun rfc822/parse-local-part (lal)
471 (let ((ret (rfc822/parse-word lal)))
473 (let ((local-part (cdr (car ret))) dot)
475 (while (and (setq ret (rfc822/parse-ascii-token lal))
477 (string-equal (cdr (assq 'specials dot)) ".")
478 (setq ret (rfc822/parse-word (cdr ret)))
480 (append local-part dot (cdr (car ret)))
484 (cons (cons 'local-part local-part) lal)
487 (defun rfc822/parse-sub-domain (lal)
488 (let ((ret (rfc822/parse-ascii-token lal)))
490 (let ((sub-domain (car ret)))
491 (if (or (assq 'atom sub-domain)
492 (assq 'domain-literal sub-domain)
494 (cons (cons 'sub-domain sub-domain)
499 (defun rfc822/parse-domain (lal)
500 (let ((ret (rfc822/parse-sub-domain lal)))
502 (let ((domain (cdr (car ret))) dot)
504 (while (and (setq ret (rfc822/parse-ascii-token lal))
506 (string-equal (cdr (assq 'specials dot)) ".")
507 (setq ret (rfc822/parse-sub-domain (cdr ret)))
509 (append domain dot (cdr (car ret)))
513 (cons (cons 'domain domain) lal)
516 (defun rfc822/parse-at-domain (lal)
517 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
519 (setq at-sign (car ret))
520 (string-equal (cdr (assq 'specials at-sign)) "@")
521 (setq ret (rfc822/parse-domain (cdr ret)))
523 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
527 (defun rfc822/parse-addr-spec (lal)
528 (let ((ret (rfc822/parse-local-part lal))
532 (setq addr (cdr (car ret)))
534 (and (setq ret (rfc822/parse-at-domain lal))
535 (setq addr (append addr (cdr (car ret))))
538 (cons (cons 'addr-spec addr) lal)
541 (defun rfc822/parse-route (lal)
542 (let ((ret (rfc822/parse-at-domain lal))
546 (setq route (cdr (car ret)))
548 (while (and (setq ret (rfc822/parse-ascii-token lal))
549 (setq comma (car ret))
550 (string-equal (cdr (assq 'specials comma)) ",")
551 (setq ret (rfc822/parse-at-domain (cdr ret)))
553 (setq route (append route comma (cdr (car ret))))
556 (and (setq ret (rfc822/parse-ascii-token lal))
557 (setq colon (car ret))
558 (string-equal (cdr (assq 'specials colon)) ":")
559 (setq route (append route colon))
562 (cons (cons 'route route)
567 (defun rfc822/parse-route-addr (lal)
568 (let ((ret (rfc822/parse-ascii-token lal))
572 (string-equal (cdr (assq 'specials <)) "<")
574 (progn (and (setq ret (rfc822/parse-route lal))
575 (setq route (cdr (car ret)))
578 (setq ret (rfc822/parse-addr-spec lal))
580 (setq addr-spec (cdr (car ret)))
582 (setq ret (rfc822/parse-ascii-token lal))
584 (string-equal (cdr (assq 'specials >)) ">")
586 (cons (cons 'route-addr (append route addr-spec))
591 (defun rfc822/parse-phrase-route-addr (lal)
592 (let ((ret (rfc822/parse-phrase lal)) phrase)
595 (setq phrase (cdr (car ret)))
598 (if (setq ret (rfc822/parse-route-addr lal))
599 (cons (list 'phrase-route-addr
605 (defun rfc822/parse-mailbox (lal)
606 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
607 (rfc822/parse-addr-spec lal)))
611 (setq mbox (car ret))
613 (if (and (setq ret (rfc822/parse-token-or-comment lal))
614 (setq comment (cdr (assq 'comment (car ret))))
618 (cons (list 'mailbox mbox comment)
622 (defun rfc822/parse-group (lal)
623 (let ((ret (rfc822/parse-phrase lal))
624 phrase colon comma mbox semicolon)
626 (setq phrase (cdr (car ret)))
628 (setq ret (rfc822/parse-ascii-token lal))
629 (setq colon (car ret))
630 (string-equal (cdr (assq 'specials colon)) ":")
633 (and (setq ret (rfc822/parse-mailbox lal))
634 (setq mbox (list (car ret)))
637 (while (and (setq ret (rfc822/parse-ascii-token lal))
638 (setq comma (car ret))
640 (cdr (assq 'specials comma)) ",")
642 (setq ret (rfc822/parse-mailbox lal))
643 (setq mbox (cons (car ret) mbox))
647 (and (setq ret (rfc822/parse-ascii-token lal))
648 (setq semicolon (car ret))
649 (string-equal (cdr (assq 'specials semicolon)) ";")
651 (cons (list 'group phrase (nreverse mbox))
656 (defun rfc822/parse-address (lal)
657 (or (rfc822/parse-group lal)
658 (rfc822/parse-mailbox lal)
661 (defun rfc822/parse-addresses (lal)
662 (let ((ret (rfc822/parse-address lal)))
664 (let ((dest (list (car ret))))
666 (while (and (setq ret (rfc822/parse-ascii-token lal))
667 (string-equal (cdr (assq 'specials (car ret))) ",")
668 (setq ret (rfc822/parse-address (cdr ret)))
670 (setq dest (cons (car ret) dest))
676 (defun rfc822/addr-to-string (seq)
679 (if (eq (car token) 'spaces)
686 (defun rfc822/address-string (address)
687 (cond ((eq (car address) 'group)
688 (mapconcat (function rfc822/address-string)
692 ((eq (car address) 'mailbox)
693 (let ((addr (nth 1 address))
695 (rfc822/addr-to-string
696 (if (eq (car addr) 'phrase-route-addr)
702 (defun rfc822/full-name-string (address)
703 (cond ((eq (car address) 'group)
710 ((eq (car address) 'mailbox)
711 (let ((addr (nth 1 address))
712 (comment (nth 2 address))
714 (if (eq (car addr) 'phrase-route-addr)
715 (setq phrase (mapconcat (function
724 (defun rfc822/extract-address-components (str)
725 "Extract full name and canonical address from STR.
726 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
727 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
728 (let* ((structure (car
729 (rfc822/parse-address
730 (rfc822/lexical-analyze str)
732 (phrase (rfc822/full-name-string structure))
733 (address (rfc822/address-string structure))
735 (list phrase address)
744 ;;; tl-822.el ends here