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.30 1996-06-13 17:13:13 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) "]"))
162 ;; (defconst rfc822/quoted-string-regexp
165 ;; (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
169 (defun rfc822/wrap-as-quoted-string (str)
170 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
174 (if (memq chr rfc822/non-qtext-char-list)
175 (concat "\\" (char-to-string chr))
181 (defun rfc822/strip-quoted-pair (str)
187 (setq chr (elt str i))
188 (if (or flag (not (eq chr ?\\)))
190 (setq dest (concat dest (char-to-string chr)))
199 (defun rfc822/strip-quoted-string (str)
200 (rfc822/strip-quoted-pair
201 (let ((max (- (length str) 1))
203 (if (and (eq (elt str 0) ?\")
204 (eq (elt str max) ?\")
206 (substring str 1 max)
214 (defun rfc822/unfolding-string (str)
216 (while (string-match "\n\\s +" str)
217 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
218 (setq str (substring str (match-end 0)))
224 ;;; @ lexical analyze
227 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
228 (defconst rfc822/space-chars " \t\n")
229 (defconst rfc822/non-atom-chars
230 (concat rfc822/special-chars rfc822/space-chars))
231 (defconst rfc822/non-dtext-chars "[]")
232 (defconst rfc822/non-ctext-chars "()")
234 (defun rfc822/analyze-spaces (str)
235 (let ((i (position-mismatched
238 (find elt rfc822/space-chars)
242 (cons (cons 'spaces (substring str 0 i))
247 (defun rfc822/analyze-special (str)
248 (if (and (> (length str) 0)
249 (find (elt str 0) rfc822/special-chars)
251 (cons (cons 'specials (substring str 0 1))
256 (defun rfc822/analyze-atom (str)
257 (let ((i (position-mismatched
260 (not (find elt rfc822/non-atom-chars))
264 (cons (cons 'atom (substring str 0 i))
269 (defun rfc822/analyze-quoted-string (str)
270 (let ((len (length str)))
274 (let ((i 1) chr dest)
277 (setq chr (aref str i))
283 (setq dest (concat dest (char-to-string (aref str i))))
287 (cons (cons 'quoted-string dest)
288 (substring str (1+ i)))
292 (setq dest (concat dest (char-to-string (aref str i))))
297 (defun rfc822/analyze-domain-literal (str)
298 (if (and (> (length str) 0)
301 (let* ((i (position-mismatched
304 (not (find elt rfc822/non-dtext-chars))
306 (setq str (substring str 1))
308 (rest (substring str i))
312 (eq (elt rest 0) ?\])
314 (cons (cons 'domain-literal (substring str 0 i))
319 (defun rfc822/analyze-comment (str)
320 (if (and (> (length str) 0)
325 (setq str (substring str 1))
327 (while (not (string-equal str ""))
328 (setq p (position-mismatched
331 (not (find elt rfc822/non-ctext-chars))
334 (setq dest (concat dest (substring str 0 p)))
335 (setq str (substring str p))
337 ((setq ret (rfc822/analyze-comment str))
338 (setq dest (concat dest "(" (cdr (car ret)) ")"))
344 (if (and (> (length str) 0)
347 (cons (cons 'comment dest)
352 (defun rfc822/lexical-analyze (str)
354 (while (not (string-equal str ""))
356 (or (rfc822/analyze-quoted-string str)
357 (rfc822/analyze-domain-literal str)
358 (rfc822/analyze-comment str)
359 (rfc822/analyze-spaces str)
360 (rfc822/analyze-special str)
361 (rfc822/analyze-atom str)
364 (setq dest (cons (car ret) dest))
374 (defun rfc822/ignored-token-p (token)
375 (let ((type (car token)))
376 (or (eq type 'spaces)(eq type 'comment))
379 (defun rfc822/parse-token (lal)
383 (setq token (car lal))
384 (rfc822/ignored-token-p token)
387 (setq itl (cons token itl))
389 (cons (nreverse (cons token itl))
393 (defun rfc822/parse-ascii-token (lal)
394 (let (token itl parsed token-value)
396 (setq token (car lal))
397 (if (and (setq token-value (cdr token))
398 (find-charset-string token-value)
401 (rfc822/ignored-token-p token)
404 (setq itl (cons token itl))
407 (setq parsed (nreverse (cons token itl)))
409 (cons parsed (cdr lal))
412 (defun rfc822/parse-token-or-comment (lal)
416 (setq token (car lal))
417 (eq (car token) 'spaces)
420 (setq itl (cons token itl))
422 (cons (nreverse (cons token itl))
426 (defun rfc822/parse-word (lal)
427 (let ((ret (rfc822/parse-ascii-token lal)))
429 (let ((elt (car ret))
432 (if (or (assq 'atom elt)
433 (assq 'quoted-string elt))
434 (cons (cons 'word elt) rest)
437 (defun rfc822/parse-word-or-comment (lal)
438 (let ((ret (rfc822/parse-token-or-comment lal)))
440 (let ((elt (car ret))
443 (cond ((or (assq 'atom elt)
444 (assq 'quoted-string elt))
445 (cons (cons 'word elt) rest)
448 (cons (cons 'comment-word elt) rest)
452 (defun rfc822/parse-phrase (lal)
454 (while (setq ret (rfc822/parse-word-or-comment lal))
455 (setq phrase (append phrase (cdr (car ret))))
459 (cons (cons 'phrase phrase) lal)
462 (defun rfc822/parse-local-part (lal)
463 (let ((ret (rfc822/parse-word lal)))
465 (let ((local-part (cdr (car ret))) dot)
467 (while (and (setq ret (rfc822/parse-ascii-token lal))
469 (string-equal (cdr (assq 'specials dot)) ".")
470 (setq ret (rfc822/parse-word (cdr ret)))
472 (append local-part dot (cdr (car ret)))
476 (cons (cons 'local-part local-part) lal)
479 (defun rfc822/parse-sub-domain (lal)
480 (let ((ret (rfc822/parse-ascii-token lal)))
482 (let ((sub-domain (car ret)))
483 (if (or (assq 'atom sub-domain)
484 (assq 'domain-literal sub-domain)
486 (cons (cons 'sub-domain sub-domain)
491 (defun rfc822/parse-domain (lal)
492 (let ((ret (rfc822/parse-sub-domain lal)))
494 (let ((domain (cdr (car ret))) dot)
496 (while (and (setq ret (rfc822/parse-ascii-token lal))
498 (string-equal (cdr (assq 'specials dot)) ".")
499 (setq ret (rfc822/parse-sub-domain (cdr ret)))
501 (append domain dot (cdr (car ret)))
505 (cons (cons 'domain domain) lal)
508 (defun rfc822/parse-at-domain (lal)
509 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
511 (setq at-sign (car ret))
512 (string-equal (cdr (assq 'specials at-sign)) "@")
513 (setq ret (rfc822/parse-domain (cdr ret)))
515 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
519 (defun rfc822/parse-addr-spec (lal)
520 (let ((ret (rfc822/parse-local-part lal))
524 (setq addr (cdr (car ret)))
526 (and (setq ret (rfc822/parse-at-domain lal))
527 (setq addr (append addr (cdr (car ret))))
530 (cons (cons 'addr-spec addr) lal)
533 (defun rfc822/parse-route (lal)
534 (let ((ret (rfc822/parse-at-domain lal))
538 (setq route (cdr (car ret)))
540 (while (and (setq ret (rfc822/parse-ascii-token lal))
541 (setq comma (car ret))
542 (string-equal (cdr (assq 'specials comma)) ",")
543 (setq ret (rfc822/parse-at-domain (cdr ret)))
545 (setq route (append route comma (cdr (car ret))))
548 (and (setq ret (rfc822/parse-ascii-token lal))
549 (setq colon (car ret))
550 (string-equal (cdr (assq 'specials colon)) ":")
551 (setq route (append route colon))
554 (cons (cons 'route route)
559 (defun rfc822/parse-route-addr (lal)
560 (let ((ret (rfc822/parse-ascii-token lal))
564 (string-equal (cdr (assq 'specials <)) "<")
566 (progn (and (setq ret (rfc822/parse-route lal))
567 (setq route (cdr (car ret)))
570 (setq ret (rfc822/parse-addr-spec lal))
572 (setq addr-spec (cdr (car ret)))
574 (setq ret (rfc822/parse-ascii-token lal))
576 (string-equal (cdr (assq 'specials >)) ">")
578 (cons (cons 'route-addr (append route addr-spec))
583 (defun rfc822/parse-phrase-route-addr (lal)
584 (let ((ret (rfc822/parse-phrase lal)) phrase)
587 (setq phrase (cdr (car ret)))
590 (if (setq ret (rfc822/parse-route-addr lal))
591 (cons (list 'phrase-route-addr
597 (defun rfc822/parse-mailbox (lal)
598 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
599 (rfc822/parse-addr-spec lal)))
603 (setq mbox (car ret))
605 (if (and (setq ret (rfc822/parse-token-or-comment lal))
606 (setq comment (cdr (assq 'comment (car ret))))
610 (cons (list 'mailbox mbox comment)
614 (defun rfc822/parse-group (lal)
615 (let ((ret (rfc822/parse-phrase lal))
616 phrase colon comma mbox semicolon)
618 (setq phrase (cdr (car ret)))
620 (setq ret (rfc822/parse-ascii-token lal))
621 (setq colon (car ret))
622 (string-equal (cdr (assq 'specials colon)) ":")
625 (and (setq ret (rfc822/parse-mailbox lal))
626 (setq mbox (list (car ret)))
629 (while (and (setq ret (rfc822/parse-ascii-token lal))
630 (setq comma (car ret))
632 (cdr (assq 'specials comma)) ",")
634 (setq ret (rfc822/parse-mailbox lal))
635 (setq mbox (cons (car ret) mbox))
639 (and (setq ret (rfc822/parse-ascii-token lal))
640 (setq semicolon (car ret))
641 (string-equal (cdr (assq 'specials semicolon)) ";")
643 (cons (list 'group phrase (nreverse mbox))
648 (defun rfc822/parse-address (lal)
649 (or (rfc822/parse-group lal)
650 (rfc822/parse-mailbox lal)
653 (defun rfc822/parse-addresses (lal)
654 (let ((ret (rfc822/parse-address lal)))
656 (let ((dest (list (car ret))))
658 (while (and (setq ret (rfc822/parse-ascii-token lal))
659 (string-equal (cdr (assq 'specials (car ret))) ",")
660 (setq ret (rfc822/parse-address (cdr ret)))
662 (setq dest (cons (car ret) dest))
668 (defun rfc822/addr-to-string (seq)
671 (if (eq (car token) 'spaces)
678 (defun rfc822/address-string (address)
679 (cond ((eq (car address) 'group)
680 (mapconcat (function rfc822/address-string)
684 ((eq (car address) 'mailbox)
685 (let ((addr (nth 1 address)))
686 (rfc822/addr-to-string
687 (if (eq (car addr) 'phrase-route-addr)
693 (defun rfc822/full-name-string (address)
694 (cond ((eq (car address) 'group)
701 ((eq (car address) 'mailbox)
702 (let ((addr (nth 1 address))
703 (comment (nth 2 address))
705 (if (eq (car addr) 'phrase-route-addr)
706 (setq phrase (mapconcat (function
715 (defun rfc822/extract-address-components (str)
716 "Extract full name and canonical address from STR.
717 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
718 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
719 (let* ((structure (car
720 (rfc822/parse-address
721 (rfc822/lexical-analyze str)
723 (phrase (rfc822/full-name-string structure))
724 (address (rfc822/address-string structure))
726 (list phrase address)
735 ;;; tl-822.el ends here