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.20 1996-04-25 21:28:03 morioka Exp $")
34 (defconst rfc822/version (get-version-string rfc822/RCS-ID))
40 (defconst rfc822/field-name-regexp "[!-9;-~]+")
42 (defconst rfc822/field-top-regexp
43 (concat "\\(" rfc822/field-name-regexp "\\):"))
45 (defconst rfc822::next-field-top-regexp (concat "\n" rfc822/field-top-regexp))
47 (defun rfc822/field-end ()
48 (if (re-search-forward rfc822::next-field-top-regexp nil t)
49 (goto-char (match-beginning 0))
50 (if (re-search-forward "^$" nil t)
51 (goto-char (1- (match-beginning 0)))
57 (defun rfc822/get-field-body (name &optional boundary)
58 (let ((case-fold-search t))
61 (rfc822/narrow-to-header boundary)
62 (goto-char (point-min))
63 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
64 (buffer-substring-no-properties
70 (defun rfc822/get-field-bodies (field-names &optional default-value)
71 (let ((case-fold-search t))
75 (goto-char (point-min))
76 (or (and (re-search-forward "^$" nil t) (match-end 0))
79 (goto-char (point-min))
80 (let* ((dest (make-list (length field-names) default-value))
84 (while (setq field-name (car s-rest))
85 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
87 (buffer-substring-no-properties
90 (setq s-rest (cdr s-rest)
99 (defun rfc822/narrow-to-header (&optional boundary)
100 (narrow-to-region (goto-char (point-min))
101 (if (re-search-forward
102 (concat "^\\(" (regexp-quote
103 (or boundary "")) "\\)?$") nil t)
108 (defun rfc822/get-header-string (pat &optional boundary)
109 (let ((case-fold-search t))
112 (rfc822/narrow-to-header boundary)
113 (goto-char (point-min))
115 (while (re-search-forward rfc822/field-top-regexp nil t)
116 (setq field (buffer-substring (match-beginning 0)
119 (if (string-match pat field)
120 (setq header (concat header field "\n"))
125 (defun rfc822/get-header-string-except (pat &optional boundary)
126 (let ((case-fold-search t))
129 (rfc822/narrow-to-header boundary)
130 (goto-char (point-min))
132 (while (re-search-forward rfc822/field-top-regexp nil t)
133 (setq field (buffer-substring (match-beginning 0)
136 (if (not (string-match pat field))
137 (setq header (concat header field "\n"))
146 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
147 (defconst rfc822/quoted-pair-regexp "\\\\.")
148 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
149 (defconst rfc822/qtext-regexp
150 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) " \t]"))
151 (defconst rfc822/quoted-string-regexp
155 "\\(" rfc822/linear-white-space-regexp "?"
156 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
158 rfc822/linear-white-space-regexp "?"
161 (defun rfc822/wrap-as-quoted-string (str)
162 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
166 (if (memq chr rfc822/non-qtext-char-list)
167 (concat "\\" (char-to-string chr))
173 (defun rfc822/strip-quoted-pair (str)
179 (setq chr (elt str i))
180 (if (or flag (not (eq chr ?\\)))
182 (setq dest (concat dest (char-to-string chr)))
191 (defun rfc822/strip-quoted-string (str)
192 (rfc822/strip-quoted-pair
193 (let ((max (- (length str) 1))
195 (if (and (eq (elt str 0) ?\")
196 (eq (elt str max) ?\")
198 (substring str 1 max)
206 (defun rfc822/unfolding-string (str)
208 (while (string-match "\n\\s +" str)
209 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
210 (setq str (substring str (match-end 0)))
216 ;;; @ lexical analyze
219 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
220 (defconst rfc822/space-chars " \t\n")
221 (defconst rfc822/non-atom-chars
222 (concat rfc822/special-chars rfc822/space-chars))
223 (defconst rfc822/non-dtext-chars "[]")
224 (defconst rfc822/non-ctext-chars "()")
226 (defun rfc822/analyze-spaces (str)
227 (let ((i (position-mismatched
230 (find elt rfc822/space-chars)
234 (cons (cons 'spaces (substring str 0 i))
239 (defun rfc822/analyze-special (str)
240 (if (and (> (length str) 0)
241 (find (elt str 0) rfc822/special-chars)
243 (cons (cons 'specials (substring str 0 1))
248 (defun rfc822/analyze-atom (str)
249 (let ((i (position-mismatched
252 (not (find elt rfc822/non-atom-chars))
256 (cons (cons 'atom (substring str 0 i))
261 (defun rfc822/analyze-quoted-pair (str)
262 (if (and (>= (length str) 2)
265 (cons (cons 'quoted-pair (substring str 0 2))
270 (defun rfc822/analyze-quoted-string (str)
271 (if (and (> (length str) 0)
274 (let* ((i (position-mismatched
277 (not (memq elt rfc822/non-qtext-char-list))
279 (setq str (substring str 1))
281 (rest (substring str i))
285 (eq (elt rest 0) ?\")
287 (cons (cons 'quoted-string (substring str 0 i))
292 (defun rfc822/analyze-domain-literal (str)
293 (if (and (> (length str) 0)
296 (let* ((i (position-mismatched
299 (not (find elt rfc822/non-dtext-chars))
301 (setq str (substring str 1))
303 (rest (substring str i))
307 (eq (elt rest 0) ?\])
309 (cons (cons 'domain-literal (substring str 0 i))
314 (defun rfc822/analyze-comment (str)
315 (if (and (> (length str) 0)
320 (setq str (substring str 1))
322 (while (not (string-equal str ""))
323 (setq p (position-mismatched
326 (not (find elt rfc822/non-ctext-chars))
329 (setq dest (concat dest (substring str 0 p)))
330 (setq str (substring str p))
332 ((setq ret (rfc822/analyze-comment str))
333 (setq dest (concat dest "(" (cdr (car ret)) ")"))
339 (if (and (> (length str) 0)
342 (cons (cons 'comment dest)
347 (defun rfc822/lexical-analyze (str)
349 (i 0)(len (length str))
351 (while (not (string-equal str ""))
353 (or (rfc822/analyze-quoted-string str)
354 (rfc822/analyze-domain-literal str)
355 (rfc822/analyze-comment str)
356 (rfc822/analyze-spaces str)
357 (rfc822/analyze-special str)
358 (rfc822/analyze-atom str)
361 (setq dest (cons (car ret) dest))
371 (defun rfc822/ignored-token-p (token)
372 (let ((type (car token)))
373 (or (eq type 'spaces)(eq type 'comment))
376 (defun rfc822/parse-token (lal)
380 (setq token (car lal))
381 (rfc822/ignored-token-p token)
384 (setq itl (cons token itl))
386 (cons (nreverse (cons token itl))
390 (defun rfc822/parse-ascii-token (lal)
391 (let (token itl parsed token-value)
393 (setq token (car lal))
394 (if (and (setq token-value (cdr token))
395 (find-charset-string token-value)
398 (rfc822/ignored-token-p token)
401 (setq itl (cons token itl))
404 (setq parsed (nreverse (cons token itl)))
406 (cons parsed (cdr lal))
409 (defun rfc822/parse-token-or-comment (lal)
413 (setq token (car lal))
414 (eq (car token) 'spaces)
417 (setq itl (cons token itl))
419 (cons (nreverse (cons token itl))
423 (defun rfc822/parse-word (lal)
424 (let ((ret (rfc822/parse-ascii-token lal)))
426 (let ((elt (car ret))
429 (if (or (assq 'atom elt)
430 (assq 'quoted-string elt))
431 (cons (cons 'word elt) rest)
434 (defun rfc822/parse-word-or-comment (lal)
435 (let ((ret (rfc822/parse-token-or-comment lal)))
437 (let ((elt (car ret))
440 (cond ((or (assq 'atom elt)
441 (assq 'quoted-string elt))
442 (cons (cons 'word elt) rest)
445 (cons (cons 'comment-word elt) rest)
449 (defun rfc822/parse-phrase (lal)
451 (while (setq ret (rfc822/parse-word-or-comment lal))
452 (setq phrase (append phrase (cdr (car ret))))
456 (cons (cons 'phrase phrase) lal)
459 (defun rfc822/parse-local-part (lal)
460 (let ((ret (rfc822/parse-word lal)))
462 (let ((local-part (cdr (car ret))) dot)
464 (while (and (setq ret (rfc822/parse-ascii-token lal))
466 (string-equal (cdr (assq 'specials dot)) ".")
467 (setq ret (rfc822/parse-word (cdr ret)))
469 (append local-part dot (cdr (car ret)))
473 (cons (cons 'local-part local-part) lal)
476 (defun rfc822/parse-sub-domain (lal)
477 (let ((ret (rfc822/parse-ascii-token lal)))
479 (let ((sub-domain (car ret)))
480 (if (or (assq 'atom sub-domain)
481 (assq 'domain-literal sub-domain)
483 (cons (cons 'sub-domain sub-domain)
488 (defun rfc822/parse-domain (lal)
489 (let ((ret (rfc822/parse-sub-domain lal)))
491 (let ((domain (cdr (car ret))) dot)
493 (while (and (setq ret (rfc822/parse-ascii-token lal))
495 (string-equal (cdr (assq 'specials dot)) ".")
496 (setq ret (rfc822/parse-sub-domain (cdr ret)))
498 (append domain dot (cdr (car ret)))
502 (cons (cons 'domain domain) lal)
505 (defun rfc822/parse-at-domain (lal)
506 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
508 (setq at-sign (car ret))
509 (string-equal (cdr (assq 'specials at-sign)) "@")
510 (setq ret (rfc822/parse-domain (cdr ret)))
512 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
516 (defun rfc822/parse-addr-spec (lal)
517 (let ((ret (rfc822/parse-local-part lal))
521 (setq addr (cdr (car ret)))
523 (and (setq ret (rfc822/parse-at-domain lal))
524 (setq addr (append addr (cdr (car ret))))
527 (cons (cons 'addr-spec addr) lal)
530 (defun rfc822/parse-route (lal)
531 (let ((ret (rfc822/parse-at-domain lal))
535 (setq route (cdr (car ret)))
537 (while (and (setq ret (rfc822/parse-ascii-token lal))
538 (setq comma (car ret))
539 (string-equal (cdr (assq 'specials comma)) ",")
540 (setq ret (rfc822/parse-at-domain (cdr ret)))
542 (setq route (append route comma (cdr (car ret))))
545 (and (setq ret (rfc822/parse-ascii-token lal))
546 (setq colon (car ret))
547 (string-equal (cdr (assq 'specials colon)) ":")
548 (setq route (append route colon))
551 (cons (cons 'route route)
556 (defun rfc822/parse-route-addr (lal)
557 (let ((ret (rfc822/parse-ascii-token lal))
561 (string-equal (cdr (assq 'specials <)) "<")
563 (progn (and (setq ret (rfc822/parse-route lal))
564 (setq route (cdr (car ret)))
567 (setq ret (rfc822/parse-addr-spec lal))
569 (setq addr-spec (cdr (car ret)))
571 (setq ret (rfc822/parse-ascii-token lal))
573 (string-equal (cdr (assq 'specials >)) ">")
575 (cons (cons 'route-addr (append route addr-spec))
580 (defun rfc822/parse-phrase-route-addr (lal)
581 (let ((ret (rfc822/parse-phrase lal)) phrase)
584 (setq phrase (cdr (car ret)))
587 (if (setq ret (rfc822/parse-route-addr lal))
588 (cons (list 'phrase-route-addr
594 (defun rfc822/parse-mailbox (lal)
595 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
596 (rfc822/parse-addr-spec lal)))
600 (setq mbox (car ret))
602 (if (and (setq ret (rfc822/parse-token-or-comment lal))
603 (setq comment (cdr (assq 'comment (car ret))))
607 (cons (list 'mailbox mbox comment)
611 (defun rfc822/parse-group (lal)
612 (let ((ret (rfc822/parse-phrase lal))
613 phrase colon comma mbox semicolon)
615 (setq phrase (cdr (car ret)))
617 (setq ret (rfc822/parse-ascii-token lal))
618 (setq colon (car ret))
619 (string-equal (cdr (assq 'specials colon)) ":")
622 (and (setq ret (rfc822/parse-mailbox lal))
623 (setq mbox (list (car ret)))
626 (while (and (setq ret (rfc822/parse-ascii-token lal))
627 (setq comma (car ret))
629 (cdr (assq 'specials comma)) ",")
631 (setq ret (rfc822/parse-mailbox lal))
632 (setq mbox (cons (car ret) mbox))
636 (and (setq ret (rfc822/parse-ascii-token lal))
637 (setq semicolon (car ret))
638 (string-equal (cdr (assq 'specials semicolon)) ";")
640 (cons (list 'group phrase (nreverse mbox))
645 (defun rfc822/parse-address (lal)
646 (or (rfc822/parse-group lal)
647 (rfc822/parse-mailbox lal)
650 (defun rfc822/parse-addresses (lal)
651 (let ((ret (rfc822/parse-address lal)))
653 (let ((dest (list (car ret))))
655 (while (and (setq ret (rfc822/parse-ascii-token lal))
656 (string-equal (cdr (assq 'specials (car ret))) ",")
657 (setq ret (rfc822/parse-address (cdr ret)))
659 (setq dest (cons (car ret) dest))
665 (defun rfc822/addr-to-string (seq)
668 (if (eq (car token) 'spaces)
675 (defun rfc822/address-string (address)
676 (cond ((eq (car address) 'group)
677 (mapconcat (function rfc822/address-string)
681 ((eq (car address) 'mailbox)
682 (let ((addr (nth 1 address))
684 (rfc822/addr-to-string
685 (if (eq (car addr) 'phrase-route-addr)
691 (defun rfc822/full-name-string (address)
692 (cond ((eq (car address) 'group)
699 ((eq (car address) 'mailbox)
700 (let ((addr (nth 1 address))
701 (comment (nth 2 address))
703 (if (eq (car addr) 'phrase-route-addr)
704 (setq phrase (mapconcat (function
713 (defun rfc822/extract-address-components (str)
714 "Extract full name and canonical address from STR.
715 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
716 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
717 (let* ((structure (car
718 (rfc822/parse-address
719 (rfc822/lexical-analyze str)
721 (phrase (rfc822/full-name-string structure))
722 (address (rfc822/address-string structure))
724 (list phrase address)
733 ;;; tl-822.el ends here