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.28 1996-06-13 17:09:18 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-pair (str)
270 (if (and (>= (length str) 2)
273 (cons (cons 'quoted-pair (substring str 0 2))
278 (defun rfc822/analyze-quoted-string (str)
279 (let ((len (length str)))
283 (let ((i 1) chr dest)
286 (setq chr (aref str i))
292 (setq dest (concat dest (char-to-string (aref str i))))
296 (cons (cons 'quoted-string dest)
297 (substring str (1+ i)))
301 (setq dest (concat dest (char-to-string (aref str i))))
306 (defun rfc822/analyze-domain-literal (str)
307 (if (and (> (length str) 0)
310 (let* ((i (position-mismatched
313 (not (find elt rfc822/non-dtext-chars))
315 (setq str (substring str 1))
317 (rest (substring str i))
321 (eq (elt rest 0) ?\])
323 (cons (cons 'domain-literal (substring str 0 i))
328 (defun rfc822/analyze-comment (str)
329 (if (and (> (length str) 0)
334 (setq str (substring str 1))
336 (while (not (string-equal str ""))
337 (setq p (position-mismatched
340 (not (find elt rfc822/non-ctext-chars))
343 (setq dest (concat dest (substring str 0 p)))
344 (setq str (substring str p))
346 ((setq ret (rfc822/analyze-comment str))
347 (setq dest (concat dest "(" (cdr (car ret)) ")"))
353 (if (and (> (length str) 0)
356 (cons (cons 'comment dest)
361 (defun rfc822/lexical-analyze (str)
363 (while (not (string-equal str ""))
365 (or (rfc822/analyze-quoted-string str)
366 (rfc822/analyze-domain-literal str)
367 (rfc822/analyze-comment str)
368 (rfc822/analyze-spaces str)
369 (rfc822/analyze-special str)
370 (rfc822/analyze-atom str)
373 (setq dest (cons (car ret) dest))
383 (defun rfc822/ignored-token-p (token)
384 (let ((type (car token)))
385 (or (eq type 'spaces)(eq type 'comment))
388 (defun rfc822/parse-token (lal)
392 (setq token (car lal))
393 (rfc822/ignored-token-p token)
396 (setq itl (cons token itl))
398 (cons (nreverse (cons token itl))
402 (defun rfc822/parse-ascii-token (lal)
403 (let (token itl parsed token-value)
405 (setq token (car lal))
406 (if (and (setq token-value (cdr token))
407 (find-charset-string token-value)
410 (rfc822/ignored-token-p token)
413 (setq itl (cons token itl))
416 (setq parsed (nreverse (cons token itl)))
418 (cons parsed (cdr lal))
421 (defun rfc822/parse-token-or-comment (lal)
425 (setq token (car lal))
426 (eq (car token) 'spaces)
429 (setq itl (cons token itl))
431 (cons (nreverse (cons token itl))
435 (defun rfc822/parse-word (lal)
436 (let ((ret (rfc822/parse-ascii-token lal)))
438 (let ((elt (car ret))
441 (if (or (assq 'atom elt)
442 (assq 'quoted-string elt))
443 (cons (cons 'word elt) rest)
446 (defun rfc822/parse-word-or-comment (lal)
447 (let ((ret (rfc822/parse-token-or-comment lal)))
449 (let ((elt (car ret))
452 (cond ((or (assq 'atom elt)
453 (assq 'quoted-string elt))
454 (cons (cons 'word elt) rest)
457 (cons (cons 'comment-word elt) rest)
461 (defun rfc822/parse-phrase (lal)
463 (while (setq ret (rfc822/parse-word-or-comment lal))
464 (setq phrase (append phrase (cdr (car ret))))
468 (cons (cons 'phrase phrase) lal)
471 (defun rfc822/parse-local-part (lal)
472 (let ((ret (rfc822/parse-word lal)))
474 (let ((local-part (cdr (car ret))) dot)
476 (while (and (setq ret (rfc822/parse-ascii-token lal))
478 (string-equal (cdr (assq 'specials dot)) ".")
479 (setq ret (rfc822/parse-word (cdr ret)))
481 (append local-part dot (cdr (car ret)))
485 (cons (cons 'local-part local-part) lal)
488 (defun rfc822/parse-sub-domain (lal)
489 (let ((ret (rfc822/parse-ascii-token lal)))
491 (let ((sub-domain (car ret)))
492 (if (or (assq 'atom sub-domain)
493 (assq 'domain-literal sub-domain)
495 (cons (cons 'sub-domain sub-domain)
500 (defun rfc822/parse-domain (lal)
501 (let ((ret (rfc822/parse-sub-domain lal)))
503 (let ((domain (cdr (car ret))) dot)
505 (while (and (setq ret (rfc822/parse-ascii-token lal))
507 (string-equal (cdr (assq 'specials dot)) ".")
508 (setq ret (rfc822/parse-sub-domain (cdr ret)))
510 (append domain dot (cdr (car ret)))
514 (cons (cons 'domain domain) lal)
517 (defun rfc822/parse-at-domain (lal)
518 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
520 (setq at-sign (car ret))
521 (string-equal (cdr (assq 'specials at-sign)) "@")
522 (setq ret (rfc822/parse-domain (cdr ret)))
524 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
528 (defun rfc822/parse-addr-spec (lal)
529 (let ((ret (rfc822/parse-local-part lal))
533 (setq addr (cdr (car ret)))
535 (and (setq ret (rfc822/parse-at-domain lal))
536 (setq addr (append addr (cdr (car ret))))
539 (cons (cons 'addr-spec addr) lal)
542 (defun rfc822/parse-route (lal)
543 (let ((ret (rfc822/parse-at-domain lal))
547 (setq route (cdr (car ret)))
549 (while (and (setq ret (rfc822/parse-ascii-token lal))
550 (setq comma (car ret))
551 (string-equal (cdr (assq 'specials comma)) ",")
552 (setq ret (rfc822/parse-at-domain (cdr ret)))
554 (setq route (append route comma (cdr (car ret))))
557 (and (setq ret (rfc822/parse-ascii-token lal))
558 (setq colon (car ret))
559 (string-equal (cdr (assq 'specials colon)) ":")
560 (setq route (append route colon))
563 (cons (cons 'route route)
568 (defun rfc822/parse-route-addr (lal)
569 (let ((ret (rfc822/parse-ascii-token lal))
573 (string-equal (cdr (assq 'specials <)) "<")
575 (progn (and (setq ret (rfc822/parse-route lal))
576 (setq route (cdr (car ret)))
579 (setq ret (rfc822/parse-addr-spec lal))
581 (setq addr-spec (cdr (car ret)))
583 (setq ret (rfc822/parse-ascii-token lal))
585 (string-equal (cdr (assq 'specials >)) ">")
587 (cons (cons 'route-addr (append route addr-spec))
592 (defun rfc822/parse-phrase-route-addr (lal)
593 (let ((ret (rfc822/parse-phrase lal)) phrase)
596 (setq phrase (cdr (car ret)))
599 (if (setq ret (rfc822/parse-route-addr lal))
600 (cons (list 'phrase-route-addr
606 (defun rfc822/parse-mailbox (lal)
607 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
608 (rfc822/parse-addr-spec lal)))
612 (setq mbox (car ret))
614 (if (and (setq ret (rfc822/parse-token-or-comment lal))
615 (setq comment (cdr (assq 'comment (car ret))))
619 (cons (list 'mailbox mbox comment)
623 (defun rfc822/parse-group (lal)
624 (let ((ret (rfc822/parse-phrase lal))
625 phrase colon comma mbox semicolon)
627 (setq phrase (cdr (car ret)))
629 (setq ret (rfc822/parse-ascii-token lal))
630 (setq colon (car ret))
631 (string-equal (cdr (assq 'specials colon)) ":")
634 (and (setq ret (rfc822/parse-mailbox lal))
635 (setq mbox (list (car ret)))
638 (while (and (setq ret (rfc822/parse-ascii-token lal))
639 (setq comma (car ret))
641 (cdr (assq 'specials comma)) ",")
643 (setq ret (rfc822/parse-mailbox lal))
644 (setq mbox (cons (car ret) mbox))
648 (and (setq ret (rfc822/parse-ascii-token lal))
649 (setq semicolon (car ret))
650 (string-equal (cdr (assq 'specials semicolon)) ";")
652 (cons (list 'group phrase (nreverse mbox))
657 (defun rfc822/parse-address (lal)
658 (or (rfc822/parse-group lal)
659 (rfc822/parse-mailbox lal)
662 (defun rfc822/parse-addresses (lal)
663 (let ((ret (rfc822/parse-address lal)))
665 (let ((dest (list (car ret))))
667 (while (and (setq ret (rfc822/parse-ascii-token lal))
668 (string-equal (cdr (assq 'specials (car ret))) ",")
669 (setq ret (rfc822/parse-address (cdr ret)))
671 (setq dest (cons (car ret) dest))
677 (defun rfc822/addr-to-string (seq)
680 (if (eq (car token) 'spaces)
687 (defun rfc822/address-string (address)
688 (cond ((eq (car address) 'group)
689 (mapconcat (function rfc822/address-string)
693 ((eq (car address) 'mailbox)
694 (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