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.17 1996-04-25 16:08:35 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)
58 (let ((case-fold-search t))
62 (goto-char (point-min))
63 (or (and (re-search-forward "^$" nil t) (match-end 0))
66 (goto-char (point-min))
67 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
68 (buffer-substring-no-properties
74 (defun rfc822/get-field-bodies (field-names &optional default-value)
75 (let ((case-fold-search t))
79 (goto-char (point-min))
80 (or (and (re-search-forward "^$" nil t) (match-end 0))
83 (goto-char (point-min))
84 (let* ((dest (make-list (length field-names) default-value))
88 (while (setq field-name (car s-rest))
89 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
91 (buffer-substring-no-properties
94 (setq s-rest (cdr s-rest)
103 (defun rfc822/get-header-string (pat boundary)
104 (let ((case-fold-search t))
107 (narrow-to-region (goto-char (point-min))
110 (concat "^\\(" (regexp-quote boundary) "\\)?$")
114 (goto-char (point-min))
116 (while (re-search-forward rfc822/field-top-regexp nil t)
117 (setq field (buffer-substring (match-beginning 0)
120 (if (string-match pat field)
121 (setq header (concat header field "\n"))
126 (defun rfc822/get-header-string-except (pat boundary)
127 (let ((case-fold-search t))
130 (narrow-to-region (goto-char (point-min))
133 (concat "^\\(" (regexp-quote boundary) "\\)?$")
137 (goto-char (point-min))
139 (while (re-search-forward rfc822/field-top-regexp nil t)
140 (setq field (buffer-substring (match-beginning 0)
143 (if (not (string-match pat field))
144 (setq header (concat header field "\n"))
153 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
154 (defconst rfc822/quoted-pair-regexp "\\\\.")
155 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
156 (defconst rfc822/qtext-regexp
157 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) " \t]"))
158 (defconst rfc822/quoted-string-regexp
162 "\\(" rfc822/linear-white-space-regexp "?"
163 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
165 rfc822/linear-white-space-regexp "?"
168 (defun rfc822/wrap-as-quoted-string (str)
169 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
173 (if (memq chr rfc822/non-qtext-char-list)
174 (concat "\\" (char-to-string chr))
180 (defun rfc822/strip-quoted-pair (str)
186 (setq chr (elt str i))
187 (if (or flag (not (eq chr ?\\)))
189 (setq dest (concat dest (char-to-string chr)))
198 (defun rfc822/strip-quoted-string (str)
199 (rfc822/strip-quoted-pair
200 (let ((max (- (length str) 1))
202 (if (and (eq (elt str 0) ?\")
203 (eq (elt str max) ?\")
205 (substring str 1 max)
213 (defun rfc822/unfolding-string (str)
215 (while (string-match "\n\\s +" str)
216 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
217 (setq str (substring str (match-end 0)))
223 ;;; @ lexical analyze
226 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
227 (defconst rfc822/space-chars " \t\n")
228 (defconst rfc822/non-atom-chars
229 (concat rfc822/special-chars rfc822/space-chars))
230 (defconst rfc822/non-dtext-chars "[]")
231 (defconst rfc822/non-ctext-chars "()")
233 (defun rfc822/analyze-spaces (str)
234 (let ((i (position-mismatched
237 (find elt rfc822/space-chars)
241 (cons (cons 'spaces (substring str 0 i))
246 (defun rfc822/analyze-special (str)
247 (if (and (> (length str) 0)
248 (find (elt str 0) rfc822/special-chars)
250 (cons (cons 'specials (substring str 0 1))
255 (defun rfc822/analyze-atom (str)
256 (let ((i (position-mismatched
259 (not (find elt rfc822/non-atom-chars))
263 (cons (cons 'atom (substring str 0 i))
268 (defun rfc822/analyze-quoted-pair (str)
269 (if (and (>= (length str) 2)
272 (cons (cons 'quoted-pair (substring str 0 2))
277 (defun rfc822/analyze-quoted-string (str)
278 (if (and (> (length str) 0)
281 (let* ((i (position-mismatched
284 (not (memq elt rfc822/non-qtext-char-list))
286 (setq str (substring str 1))
288 (rest (substring str i))
292 (eq (elt rest 0) ?\")
294 (cons (cons 'quoted-string (substring str 0 i))
299 (defun rfc822/analyze-domain-literal (str)
300 (if (and (> (length str) 0)
303 (let* ((i (position-mismatched
306 (not (find elt rfc822/non-dtext-chars))
308 (setq str (substring str 1))
310 (rest (substring str i))
314 (eq (elt rest 0) ?\])
316 (cons (cons 'domain-literal (substring str 0 i))
321 (defun rfc822/analyze-comment (str)
322 (if (and (> (length str) 0)
327 (setq str (substring str 1))
329 (while (not (string-equal str ""))
330 (setq p (position-mismatched
333 (not (find elt rfc822/non-ctext-chars))
336 (setq dest (concat dest (substring str 0 p)))
337 (setq str (substring str p))
339 ((setq ret (rfc822/analyze-comment str))
340 (setq dest (concat dest "(" (cdr (car ret)) ")"))
346 (if (and (> (length str) 0)
349 (cons (cons 'comment dest)
354 (defun rfc822/lexical-analyze (str)
356 (i 0)(len (length str))
358 (while (not (string-equal str ""))
360 (or (rfc822/analyze-quoted-string str)
361 (rfc822/analyze-domain-literal str)
362 (rfc822/analyze-comment str)
363 (rfc822/analyze-spaces str)
364 (rfc822/analyze-special str)
365 (rfc822/analyze-atom str)
368 (setq dest (cons (car ret) dest))
378 (defun rfc822/ignored-token-p (token)
379 (let ((type (car token)))
380 (or (eq type 'spaces)(eq type 'comment))
383 (defun rfc822/parse-token (lal)
387 (setq token (car lal))
388 (rfc822/ignored-token-p token)
391 (setq itl (cons token itl))
393 (cons (nreverse (cons token itl))
397 (defun rfc822/parse-ascii-token (lal)
398 (let (token itl parsed token-value)
400 (setq token (car lal))
401 (if (and (setq token-value (cdr token))
402 (find-charset-string token-value)
405 (rfc822/ignored-token-p token)
408 (setq itl (cons token itl))
411 (setq parsed (nreverse (cons token itl)))
413 (cons parsed (cdr lal))
416 (defun rfc822/parse-token-or-comment (lal)
420 (setq token (car lal))
421 (eq (car token) 'spaces)
424 (setq itl (cons token itl))
426 (cons (nreverse (cons token itl))
430 (defun rfc822/parse-word (lal)
431 (let ((ret (rfc822/parse-ascii-token lal)))
433 (let ((elt (car ret))
436 (if (or (assq 'atom elt)
437 (assq 'quoted-string elt))
438 (cons (cons 'word elt) rest)
441 (defun rfc822/parse-word-or-comment (lal)
442 (let ((ret (rfc822/parse-token-or-comment lal)))
444 (let ((elt (car ret))
447 (cond ((or (assq 'atom elt)
448 (assq 'quoted-string elt))
449 (cons (cons 'word elt) rest)
452 (cons (cons 'comment-word elt) rest)
456 (defun rfc822/parse-phrase (lal)
458 (while (setq ret (rfc822/parse-word-or-comment lal))
459 (setq phrase (append phrase (cdr (car ret))))
463 (cons (cons 'phrase phrase) lal)
466 (defun rfc822/parse-local-part (lal)
467 (let ((ret (rfc822/parse-word lal)))
469 (let ((local-part (cdr (car ret))) dot)
471 (while (and (setq ret (rfc822/parse-ascii-token lal))
473 (string-equal (cdr (assq 'specials dot)) ".")
474 (setq ret (rfc822/parse-word (cdr ret)))
476 (append local-part dot (cdr (car ret)))
480 (cons (cons 'local-part local-part) lal)
483 (defun rfc822/parse-sub-domain (lal)
484 (let ((ret (rfc822/parse-ascii-token lal)))
486 (let ((sub-domain (car ret)))
487 (if (or (assq 'atom sub-domain)
488 (assq 'domain-literal sub-domain)
490 (cons (cons 'sub-domain sub-domain)
495 (defun rfc822/parse-domain (lal)
496 (let ((ret (rfc822/parse-sub-domain lal)))
498 (let ((domain (cdr (car ret))) dot)
500 (while (and (setq ret (rfc822/parse-ascii-token lal))
502 (string-equal (cdr (assq 'specials dot)) ".")
503 (setq ret (rfc822/parse-sub-domain (cdr ret)))
505 (append domain dot (cdr (car ret)))
509 (cons (cons 'domain domain) lal)
512 (defun rfc822/parse-at-domain (lal)
513 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
515 (setq at-sign (car ret))
516 (string-equal (cdr (assq 'specials at-sign)) "@")
517 (setq ret (rfc822/parse-domain (cdr ret)))
519 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
523 (defun rfc822/parse-addr-spec (lal)
524 (let ((ret (rfc822/parse-local-part lal))
528 (setq addr (cdr (car ret)))
530 (and (setq ret (rfc822/parse-at-domain lal))
531 (setq addr (append addr (cdr (car ret))))
534 (cons (cons 'addr-spec addr) lal)
537 (defun rfc822/parse-route (lal)
538 (let ((ret (rfc822/parse-at-domain lal))
542 (setq route (cdr (car ret)))
544 (while (and (setq ret (rfc822/parse-ascii-token lal))
545 (setq comma (car ret))
546 (string-equal (cdr (assq 'specials comma)) ",")
547 (setq ret (rfc822/parse-at-domain (cdr ret)))
549 (setq route (append route comma (cdr (car ret))))
552 (and (setq ret (rfc822/parse-ascii-token lal))
553 (setq colon (car ret))
554 (string-equal (cdr (assq 'specials colon)) ":")
555 (setq route (append route colon))
558 (cons (cons 'route route)
563 (defun rfc822/parse-route-addr (lal)
564 (let ((ret (rfc822/parse-ascii-token lal))
568 (string-equal (cdr (assq 'specials <)) "<")
570 (progn (and (setq ret (rfc822/parse-route lal))
571 (setq route (cdr (car ret)))
574 (setq ret (rfc822/parse-addr-spec lal))
576 (setq addr-spec (cdr (car ret)))
578 (setq ret (rfc822/parse-ascii-token lal))
580 (string-equal (cdr (assq 'specials >)) ">")
582 (cons (cons 'route-addr (append route addr-spec))
587 (defun rfc822/parse-phrase-route-addr (lal)
588 (let ((ret (rfc822/parse-phrase lal)) phrase)
591 (setq phrase (cdr (car ret)))
594 (if (setq ret (rfc822/parse-route-addr lal))
595 (cons (list 'phrase-route-addr
601 (defun rfc822/parse-mailbox (lal)
602 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
603 (rfc822/parse-addr-spec lal)))
607 (setq mbox (car ret))
609 (if (and (setq ret (rfc822/parse-token-or-comment lal))
610 (setq comment (cdr (assq 'comment (car ret))))
614 (cons (list 'mailbox mbox comment)
618 (defun rfc822/parse-group (lal)
619 (let ((ret (rfc822/parse-phrase lal))
620 phrase colon comma mbox semicolon)
622 (setq phrase (cdr (car ret)))
624 (setq ret (rfc822/parse-ascii-token lal))
625 (setq colon (car ret))
626 (string-equal (cdr (assq 'specials colon)) ":")
629 (and (setq ret (rfc822/parse-mailbox lal))
630 (setq mbox (list (car ret)))
633 (while (and (setq ret (rfc822/parse-ascii-token lal))
634 (setq comma (car ret))
636 (cdr (assq 'specials comma)) ",")
638 (setq ret (rfc822/parse-mailbox lal))
639 (setq mbox (cons (car ret) mbox))
643 (and (setq ret (rfc822/parse-ascii-token lal))
644 (setq semicolon (car ret))
645 (string-equal (cdr (assq 'specials semicolon)) ";")
647 (cons (list 'group phrase (nreverse mbox))
652 (defun rfc822/parse-address (lal)
653 (or (rfc822/parse-group lal)
654 (rfc822/parse-mailbox lal)
657 (defun rfc822/parse-addresses (lal)
658 (let ((ret (rfc822/parse-address lal)))
660 (let ((dest (list (car ret))))
662 (while (and (setq ret (rfc822/parse-ascii-token lal))
663 (string-equal (cdr (assq 'specials (car ret))) ",")
664 (setq ret (rfc822/parse-address (cdr ret)))
666 (setq dest (cons (car ret) dest))
672 (defun rfc822/addr-to-string (seq)
675 (if (eq (car token) 'spaces)
682 (defun rfc822/address-string (address)
683 (cond ((eq (car address) 'group)
684 (mapconcat (function rfc822/address-string)
688 ((eq (car address) 'mailbox)
689 (let ((addr (nth 1 address))
691 (rfc822/addr-to-string
692 (if (eq (car addr) 'phrase-route-addr)
698 (defun rfc822/full-name-string (address)
699 (cond ((eq (car address) 'group)
706 ((eq (car address) 'mailbox)
707 (let ((addr (nth 1 address))
708 (comment (nth 2 address))
710 (if (eq (car addr) 'phrase-route-addr)
711 (setq phrase (mapconcat (function
720 (defun rfc822/extract-address-components (str)
721 "Extract full name and canonical address from STR.
722 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
723 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
724 (let* ((structure (car
725 (rfc822/parse-address
726 (rfc822/lexical-analyze str)
728 (phrase (rfc822/full-name-string structure))
729 (address (rfc822/address-string structure))
731 (list phrase address)
740 ;;; tl-822.el ends here