1 ;;; tl-822.el --- RFC 822 parser for GNU Emacs
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, RFC 822
8 ;; This file is part of tl (Tiny Library).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with This program; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
32 (defconst rfc822/RCS-ID
33 "$Id: tl-822.el,v 7.37 1996-08-28 12:40:28 morioka Exp $")
34 (defconst rfc822/version (get-version-string rfc822/RCS-ID))
40 (defalias 'rfc822/narrow-to-header 'std11-narrow-to-header)
41 (defalias 'rfc822/get-header-string 'std11-header-string)
42 (defalias 'rfc822/get-header-string-except 'std11-header-string-except)
48 (defconst rfc822/field-name-regexp "[!-9;-~]+")
50 (defconst rfc822/field-top-regexp
51 (concat "\\(" rfc822/field-name-regexp "\\):"))
53 (defconst rfc822::next-field-top-regexp (concat "\n" rfc822/field-top-regexp))
55 (defun rfc822/get-field-names (&optional boundary)
58 (rfc822/narrow-to-header boundary)
59 (goto-char (point-min))
60 (let ((pat (concat "^\\(" rfc822/field-name-regexp "\\):"))
62 (while (re-search-forward pat nil t)
63 (setq name (buffer-substring (match-beginning 1)(match-end 1)))
64 (or (member name dest)
65 (setq dest (cons name dest))
70 (defalias `rfc822/field-end 'std11-field-end)
72 (defalias 'rfc822/get-field-body 'std11-field-body)
74 (defun rfc822/get-field-bodies (field-names &optional default-value boundary)
75 (let ((case-fold-search t))
78 (rfc822/narrow-to-header boundary)
79 (let* ((dest (make-list (length field-names) default-value))
83 (while (setq field-name (car s-rest))
84 (goto-char (point-min))
85 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
87 (buffer-substring-no-properties
90 (setq s-rest (cdr s-rest)
99 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
100 (defconst rfc822/quoted-pair-regexp "\\\\.")
101 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
102 (defconst rfc822/qtext-regexp
103 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) "]"))
104 (defconst rfc822/quoted-string-regexp
107 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
111 (defun rfc822/wrap-as-quoted-string (str)
112 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
116 (if (memq chr rfc822/non-qtext-char-list)
117 (concat "\\" (char-to-string chr))
123 (defun rfc822/strip-quoted-pair (str)
129 (setq chr (elt str i))
130 (if (or flag (not (eq chr ?\\)))
132 (setq dest (concat dest (char-to-string chr)))
141 (defun rfc822/strip-quoted-string (str)
142 (rfc822/strip-quoted-pair
143 (let ((max (- (length str) 1))
145 (if (and (eq (elt str 0) ?\")
146 (eq (elt str max) ?\")
148 (substring str 1 max)
156 (defun rfc822/unfolding-string (str)
158 (while (string-match "\n\\s +" str)
159 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
160 (setq str (substring str (match-end 0)))
166 ;;; @ lexical analyze
169 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
170 (defconst rfc822/space-chars " \t\n")
171 (defconst rfc822/non-atom-chars
172 (concat rfc822/special-chars rfc822/space-chars))
173 (defconst rfc822/non-dtext-chars "][")
174 (defconst rfc822/non-ctext-chars "()")
176 (defun rfc822/analyze-spaces (str)
177 (let ((i (string-match (concat "[^" rfc822/space-chars "]") str)))
180 (cons (cons 'spaces (substring str 0 i))
183 (if (not (string-equal str ""))
184 (cons (cons 'spaces str) "")
187 (defun rfc822/analyze-special (str)
188 (if (and (> (length str) 0)
189 (find (elt str 0) rfc822/special-chars)
191 (cons (cons 'specials (substring str 0 1))
196 (defun rfc822/analyze-atom (str)
197 (let ((i (string-match (concat "[" rfc822/non-atom-chars "]") str)))
200 (cons (cons 'atom (substring str 0 i))
203 (if (not (string-equal str ""))
204 (cons (cons 'spaces str) "")
207 (defun rfc822/analyze-quoted-string (str)
208 (let ((len (length str)))
212 (let ((i 1) chr dest)
215 (setq chr (aref str i))
221 (setq dest (concat dest (char-to-string (aref str i))))
225 (cons (cons 'quoted-string dest)
226 (substring str (1+ i)))
230 (setq dest (concat dest (char-to-string (aref str i))))
235 (defun rfc822/analyze-domain-literal (str)
236 (if (and (> (length str) 0)
237 (eq (aref str 0) ?\[)
239 (let* ((i (string-match (concat "[" rfc822/non-dtext-chars "]") str 1))
240 (rest (and i (substring str i)))
244 (eq (aref rest 0) ?\])
246 (cons (cons 'domain-literal (substring str 1 i))
251 (defun rfc822/analyze-comment (str)
252 (if (and (> (length str) 0)
257 (setq str (substring str 1))
259 (while (not (string-equal str ""))
260 (setq p (string-match (concat "[" rfc822/non-ctext-chars "]") str))
262 (setq dest (concat dest (substring str 0 p)))
263 (setq str (substring str p))
265 ((setq ret (rfc822/analyze-comment str))
266 (setq dest (concat dest "(" (cdr (car ret)) ")"))
272 (if (and (> (length str) 0)
275 (cons (cons 'comment dest)
280 (defun rfc822/lexical-analyze (str)
282 (while (not (string-equal str ""))
284 (or (rfc822/analyze-quoted-string str)
285 (rfc822/analyze-domain-literal str)
286 (rfc822/analyze-comment str)
287 (rfc822/analyze-spaces str)
288 (rfc822/analyze-special str)
289 (rfc822/analyze-atom str)
292 (setq dest (cons (car ret) dest))
302 (defun rfc822/ignored-token-p (token)
303 (let ((type (car token)))
304 (or (eq type 'spaces)(eq type 'comment))
307 (defun rfc822/parse-token (lal)
311 (setq token (car lal))
312 (rfc822/ignored-token-p token)
315 (setq itl (cons token itl))
317 (cons (nreverse (cons token itl))
321 (defun rfc822/parse-ascii-token (lal)
322 (let (token itl parsed token-value)
324 (setq token (car lal))
325 (if (and (setq token-value (cdr token))
326 (find-charset-string token-value)
329 (rfc822/ignored-token-p token)
332 (setq itl (cons token itl))
335 (setq parsed (nreverse (cons token itl)))
337 (cons parsed (cdr lal))
340 (defun rfc822/parse-token-or-comment (lal)
344 (setq token (car lal))
345 (eq (car token) 'spaces)
348 (setq itl (cons token itl))
350 (cons (nreverse (cons token itl))
354 (defun rfc822/parse-word (lal)
355 (let ((ret (rfc822/parse-ascii-token lal)))
357 (let ((elt (car ret))
360 (if (or (assq 'atom elt)
361 (assq 'quoted-string elt))
362 (cons (cons 'word elt) rest)
365 (defun rfc822/parse-word-or-comment (lal)
366 (let ((ret (rfc822/parse-token-or-comment lal)))
368 (let ((elt (car ret))
371 (cond ((or (assq 'atom elt)
372 (assq 'quoted-string elt))
373 (cons (cons 'word elt) rest)
376 (cons (cons 'comment-word elt) rest)
380 (defun rfc822/parse-phrase (lal)
382 (while (setq ret (rfc822/parse-word-or-comment lal))
383 (setq phrase (append phrase (cdr (car ret))))
387 (cons (cons 'phrase phrase) lal)
390 (defun rfc822/parse-local-part (lal)
391 (let ((ret (rfc822/parse-word lal)))
393 (let ((local-part (cdr (car ret))) dot)
395 (while (and (setq ret (rfc822/parse-ascii-token lal))
397 (string-equal (cdr (assq 'specials dot)) ".")
398 (setq ret (rfc822/parse-word (cdr ret)))
400 (append local-part dot (cdr (car ret)))
404 (cons (cons 'local-part local-part) lal)
407 (defun rfc822/parse-sub-domain (lal)
408 (let ((ret (rfc822/parse-ascii-token lal)))
410 (let ((sub-domain (car ret)))
411 (if (or (assq 'atom sub-domain)
412 (assq 'domain-literal sub-domain)
414 (cons (cons 'sub-domain sub-domain)
419 (defun rfc822/parse-domain (lal)
420 (let ((ret (rfc822/parse-sub-domain lal)))
422 (let ((domain (cdr (car ret))) dot)
424 (while (and (setq ret (rfc822/parse-ascii-token lal))
426 (string-equal (cdr (assq 'specials dot)) ".")
427 (setq ret (rfc822/parse-sub-domain (cdr ret)))
429 (append domain dot (cdr (car ret)))
433 (cons (cons 'domain domain) lal)
436 (defun rfc822/parse-at-domain (lal)
437 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
439 (setq at-sign (car ret))
440 (string-equal (cdr (assq 'specials at-sign)) "@")
441 (setq ret (rfc822/parse-domain (cdr ret)))
443 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
447 (defun rfc822/parse-addr-spec (lal)
448 (let ((ret (rfc822/parse-local-part lal))
452 (setq addr (cdr (car ret)))
454 (and (setq ret (rfc822/parse-at-domain lal))
455 (setq addr (append addr (cdr (car ret))))
458 (cons (cons 'addr-spec addr) lal)
461 (defun rfc822/parse-route (lal)
462 (let ((ret (rfc822/parse-at-domain lal))
466 (setq route (cdr (car ret)))
468 (while (and (setq ret (rfc822/parse-ascii-token lal))
469 (setq comma (car ret))
470 (string-equal (cdr (assq 'specials comma)) ",")
471 (setq ret (rfc822/parse-at-domain (cdr ret)))
473 (setq route (append route comma (cdr (car ret))))
476 (and (setq ret (rfc822/parse-ascii-token lal))
477 (setq colon (car ret))
478 (string-equal (cdr (assq 'specials colon)) ":")
479 (setq route (append route colon))
482 (cons (cons 'route route)
487 (defun rfc822/parse-route-addr (lal)
488 (let ((ret (rfc822/parse-ascii-token lal))
492 (string-equal (cdr (assq 'specials <)) "<")
494 (progn (and (setq ret (rfc822/parse-route lal))
495 (setq route (cdr (car ret)))
498 (setq ret (rfc822/parse-addr-spec lal))
500 (setq addr-spec (cdr (car ret)))
502 (setq ret (rfc822/parse-ascii-token lal))
504 (string-equal (cdr (assq 'specials >)) ">")
506 (cons (cons 'route-addr (append route addr-spec))
511 (defun rfc822/parse-phrase-route-addr (lal)
512 (let ((ret (rfc822/parse-phrase lal)) phrase)
515 (setq phrase (cdr (car ret)))
518 (if (setq ret (rfc822/parse-route-addr lal))
519 (cons (list 'phrase-route-addr
525 (defun rfc822/parse-mailbox (lal)
526 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
527 (rfc822/parse-addr-spec lal)))
531 (setq mbox (car ret))
533 (if (and (setq ret (rfc822/parse-token-or-comment lal))
534 (setq comment (cdr (assq 'comment (car ret))))
538 (cons (list 'mailbox mbox comment)
542 (defun rfc822/parse-group (lal)
543 (let ((ret (rfc822/parse-phrase lal))
544 phrase colon comma mbox semicolon)
546 (setq phrase (cdr (car ret)))
548 (setq ret (rfc822/parse-ascii-token lal))
549 (setq colon (car ret))
550 (string-equal (cdr (assq 'specials colon)) ":")
553 (and (setq ret (rfc822/parse-mailbox lal))
554 (setq mbox (list (car ret)))
557 (while (and (setq ret (rfc822/parse-ascii-token lal))
558 (setq comma (car ret))
560 (cdr (assq 'specials comma)) ",")
562 (setq ret (rfc822/parse-mailbox lal))
563 (setq mbox (cons (car ret) mbox))
567 (and (setq ret (rfc822/parse-ascii-token lal))
568 (setq semicolon (car ret))
569 (string-equal (cdr (assq 'specials semicolon)) ";")
571 (cons (list 'group phrase (nreverse mbox))
576 (defun rfc822/parse-address (lal)
577 (or (rfc822/parse-group lal)
578 (rfc822/parse-mailbox lal)
581 (defun rfc822/parse-addresses (lal)
582 (let ((ret (rfc822/parse-address lal)))
584 (let ((dest (list (car ret))))
586 (while (and (setq ret (rfc822/parse-ascii-token lal))
587 (string-equal (cdr (assq 'specials (car ret))) ",")
588 (setq ret (rfc822/parse-address (cdr ret)))
590 (setq dest (cons (car ret) dest))
596 (defun rfc822/addr-to-string (seq)
599 (if (eq (car token) 'spaces)
606 (defun rfc822/address-string (address)
607 (cond ((eq (car address) 'group)
608 (mapconcat (function rfc822/address-string)
612 ((eq (car address) 'mailbox)
613 (let ((addr (nth 1 address)))
614 (rfc822/addr-to-string
615 (if (eq (car addr) 'phrase-route-addr)
621 (defun rfc822/full-name-string (address)
622 (cond ((eq (car address) 'group)
629 ((eq (car address) 'mailbox)
630 (let ((addr (nth 1 address))
631 (comment (nth 2 address))
633 (if (eq (car addr) 'phrase-route-addr)
634 (setq phrase (mapconcat (function
643 (defun rfc822/extract-address-components (str)
644 "Extract full name and canonical address from STR.
645 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
646 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
647 (let* ((structure (car
648 (rfc822/parse-address
649 (rfc822/lexical-analyze str)
651 (phrase (rfc822/full-name-string structure))
652 (address (rfc822/address-string structure))
654 (list phrase address)
663 ;;; tl-822.el ends here