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.39 1996-08-28 13:03:05 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)
43 (defalias 'rfc822/get-field-names 'std11-field-names)
49 (defconst rfc822/field-name-regexp "[!-9;-~]+")
51 (defconst rfc822/field-top-regexp
52 (concat "\\(" rfc822/field-name-regexp "\\):"))
54 (defalias `rfc822/field-end 'std11-field-end)
56 (defalias 'rfc822/get-field-body 'std11-field-body)
58 (defun rfc822/get-field-bodies (field-names &optional default-value boundary)
59 (let ((case-fold-search t))
62 (rfc822/narrow-to-header boundary)
63 (let* ((dest (make-list (length field-names) default-value))
67 (while (setq field-name (car s-rest))
68 (goto-char (point-min))
69 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
71 (buffer-substring-no-properties
74 (setq s-rest (cdr s-rest)
83 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
84 (defconst rfc822/quoted-pair-regexp "\\\\.")
85 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
86 (defconst rfc822/qtext-regexp
87 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) "]"))
88 (defconst rfc822/quoted-string-regexp
91 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
95 (defun rfc822/wrap-as-quoted-string (str)
96 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
100 (if (memq chr rfc822/non-qtext-char-list)
101 (concat "\\" (char-to-string chr))
107 (defun rfc822/strip-quoted-pair (str)
113 (setq chr (elt str i))
114 (if (or flag (not (eq chr ?\\)))
116 (setq dest (concat dest (char-to-string chr)))
125 (defun rfc822/strip-quoted-string (str)
126 (rfc822/strip-quoted-pair
127 (let ((max (- (length str) 1))
129 (if (and (eq (elt str 0) ?\")
130 (eq (elt str max) ?\")
132 (substring str 1 max)
140 (defun rfc822/unfolding-string (str)
142 (while (string-match "\n\\s +" str)
143 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
144 (setq str (substring str (match-end 0)))
150 ;;; @ lexical analyze
153 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
154 (defconst rfc822/space-chars " \t\n")
155 (defconst rfc822/non-atom-chars
156 (concat rfc822/special-chars rfc822/space-chars))
157 (defconst rfc822/non-dtext-chars "][")
158 (defconst rfc822/non-ctext-chars "()")
160 (defun rfc822/analyze-spaces (str)
161 (let ((i (string-match (concat "[^" rfc822/space-chars "]") str)))
164 (cons (cons 'spaces (substring str 0 i))
167 (if (not (string-equal str ""))
168 (cons (cons 'spaces str) "")
171 (defun rfc822/analyze-special (str)
172 (if (and (> (length str) 0)
173 (find (elt str 0) rfc822/special-chars)
175 (cons (cons 'specials (substring str 0 1))
180 (defun rfc822/analyze-atom (str)
181 (let ((i (string-match (concat "[" rfc822/non-atom-chars "]") str)))
184 (cons (cons 'atom (substring str 0 i))
187 (if (not (string-equal str ""))
188 (cons (cons 'spaces str) "")
191 (defun rfc822/analyze-quoted-string (str)
192 (let ((len (length str)))
196 (let ((i 1) chr dest)
199 (setq chr (aref str i))
205 (setq dest (concat dest (char-to-string (aref str i))))
209 (cons (cons 'quoted-string dest)
210 (substring str (1+ i)))
214 (setq dest (concat dest (char-to-string (aref str i))))
219 (defun rfc822/analyze-domain-literal (str)
220 (if (and (> (length str) 0)
221 (eq (aref str 0) ?\[)
223 (let* ((i (string-match (concat "[" rfc822/non-dtext-chars "]") str 1))
224 (rest (and i (substring str i)))
228 (eq (aref rest 0) ?\])
230 (cons (cons 'domain-literal (substring str 1 i))
235 (defun rfc822/analyze-comment (str)
236 (if (and (> (length str) 0)
241 (setq str (substring str 1))
243 (while (not (string-equal str ""))
244 (setq p (string-match (concat "[" rfc822/non-ctext-chars "]") str))
246 (setq dest (concat dest (substring str 0 p)))
247 (setq str (substring str p))
249 ((setq ret (rfc822/analyze-comment str))
250 (setq dest (concat dest "(" (cdr (car ret)) ")"))
256 (if (and (> (length str) 0)
259 (cons (cons 'comment dest)
264 (defun rfc822/lexical-analyze (str)
266 (while (not (string-equal str ""))
268 (or (rfc822/analyze-quoted-string str)
269 (rfc822/analyze-domain-literal str)
270 (rfc822/analyze-comment str)
271 (rfc822/analyze-spaces str)
272 (rfc822/analyze-special str)
273 (rfc822/analyze-atom str)
276 (setq dest (cons (car ret) dest))
286 (defun rfc822/ignored-token-p (token)
287 (let ((type (car token)))
288 (or (eq type 'spaces)(eq type 'comment))
291 (defun rfc822/parse-token (lal)
295 (setq token (car lal))
296 (rfc822/ignored-token-p token)
299 (setq itl (cons token itl))
301 (cons (nreverse (cons token itl))
305 (defun rfc822/parse-ascii-token (lal)
306 (let (token itl parsed token-value)
308 (setq token (car lal))
309 (if (and (setq token-value (cdr token))
310 (find-charset-string token-value)
313 (rfc822/ignored-token-p token)
316 (setq itl (cons token itl))
319 (setq parsed (nreverse (cons token itl)))
321 (cons parsed (cdr lal))
324 (defun rfc822/parse-token-or-comment (lal)
328 (setq token (car lal))
329 (eq (car token) 'spaces)
332 (setq itl (cons token itl))
334 (cons (nreverse (cons token itl))
338 (defun rfc822/parse-word (lal)
339 (let ((ret (rfc822/parse-ascii-token lal)))
341 (let ((elt (car ret))
344 (if (or (assq 'atom elt)
345 (assq 'quoted-string elt))
346 (cons (cons 'word elt) rest)
349 (defun rfc822/parse-word-or-comment (lal)
350 (let ((ret (rfc822/parse-token-or-comment lal)))
352 (let ((elt (car ret))
355 (cond ((or (assq 'atom elt)
356 (assq 'quoted-string elt))
357 (cons (cons 'word elt) rest)
360 (cons (cons 'comment-word elt) rest)
364 (defun rfc822/parse-phrase (lal)
366 (while (setq ret (rfc822/parse-word-or-comment lal))
367 (setq phrase (append phrase (cdr (car ret))))
371 (cons (cons 'phrase phrase) lal)
374 (defun rfc822/parse-local-part (lal)
375 (let ((ret (rfc822/parse-word lal)))
377 (let ((local-part (cdr (car ret))) dot)
379 (while (and (setq ret (rfc822/parse-ascii-token lal))
381 (string-equal (cdr (assq 'specials dot)) ".")
382 (setq ret (rfc822/parse-word (cdr ret)))
384 (append local-part dot (cdr (car ret)))
388 (cons (cons 'local-part local-part) lal)
391 (defun rfc822/parse-sub-domain (lal)
392 (let ((ret (rfc822/parse-ascii-token lal)))
394 (let ((sub-domain (car ret)))
395 (if (or (assq 'atom sub-domain)
396 (assq 'domain-literal sub-domain)
398 (cons (cons 'sub-domain sub-domain)
403 (defun rfc822/parse-domain (lal)
404 (let ((ret (rfc822/parse-sub-domain lal)))
406 (let ((domain (cdr (car ret))) dot)
408 (while (and (setq ret (rfc822/parse-ascii-token lal))
410 (string-equal (cdr (assq 'specials dot)) ".")
411 (setq ret (rfc822/parse-sub-domain (cdr ret)))
413 (append domain dot (cdr (car ret)))
417 (cons (cons 'domain domain) lal)
420 (defun rfc822/parse-at-domain (lal)
421 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
423 (setq at-sign (car ret))
424 (string-equal (cdr (assq 'specials at-sign)) "@")
425 (setq ret (rfc822/parse-domain (cdr ret)))
427 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
431 (defun rfc822/parse-addr-spec (lal)
432 (let ((ret (rfc822/parse-local-part lal))
436 (setq addr (cdr (car ret)))
438 (and (setq ret (rfc822/parse-at-domain lal))
439 (setq addr (append addr (cdr (car ret))))
442 (cons (cons 'addr-spec addr) lal)
445 (defun rfc822/parse-route (lal)
446 (let ((ret (rfc822/parse-at-domain lal))
450 (setq route (cdr (car ret)))
452 (while (and (setq ret (rfc822/parse-ascii-token lal))
453 (setq comma (car ret))
454 (string-equal (cdr (assq 'specials comma)) ",")
455 (setq ret (rfc822/parse-at-domain (cdr ret)))
457 (setq route (append route comma (cdr (car ret))))
460 (and (setq ret (rfc822/parse-ascii-token lal))
461 (setq colon (car ret))
462 (string-equal (cdr (assq 'specials colon)) ":")
463 (setq route (append route colon))
466 (cons (cons 'route route)
471 (defun rfc822/parse-route-addr (lal)
472 (let ((ret (rfc822/parse-ascii-token lal))
476 (string-equal (cdr (assq 'specials <)) "<")
478 (progn (and (setq ret (rfc822/parse-route lal))
479 (setq route (cdr (car ret)))
482 (setq ret (rfc822/parse-addr-spec lal))
484 (setq addr-spec (cdr (car ret)))
486 (setq ret (rfc822/parse-ascii-token lal))
488 (string-equal (cdr (assq 'specials >)) ">")
490 (cons (cons 'route-addr (append route addr-spec))
495 (defun rfc822/parse-phrase-route-addr (lal)
496 (let ((ret (rfc822/parse-phrase lal)) phrase)
499 (setq phrase (cdr (car ret)))
502 (if (setq ret (rfc822/parse-route-addr lal))
503 (cons (list 'phrase-route-addr
509 (defun rfc822/parse-mailbox (lal)
510 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
511 (rfc822/parse-addr-spec lal)))
515 (setq mbox (car ret))
517 (if (and (setq ret (rfc822/parse-token-or-comment lal))
518 (setq comment (cdr (assq 'comment (car ret))))
522 (cons (list 'mailbox mbox comment)
526 (defun rfc822/parse-group (lal)
527 (let ((ret (rfc822/parse-phrase lal))
528 phrase colon comma mbox semicolon)
530 (setq phrase (cdr (car ret)))
532 (setq ret (rfc822/parse-ascii-token lal))
533 (setq colon (car ret))
534 (string-equal (cdr (assq 'specials colon)) ":")
537 (and (setq ret (rfc822/parse-mailbox lal))
538 (setq mbox (list (car ret)))
541 (while (and (setq ret (rfc822/parse-ascii-token lal))
542 (setq comma (car ret))
544 (cdr (assq 'specials comma)) ",")
546 (setq ret (rfc822/parse-mailbox lal))
547 (setq mbox (cons (car ret) mbox))
551 (and (setq ret (rfc822/parse-ascii-token lal))
552 (setq semicolon (car ret))
553 (string-equal (cdr (assq 'specials semicolon)) ";")
555 (cons (list 'group phrase (nreverse mbox))
560 (defun rfc822/parse-address (lal)
561 (or (rfc822/parse-group lal)
562 (rfc822/parse-mailbox lal)
565 (defun rfc822/parse-addresses (lal)
566 (let ((ret (rfc822/parse-address lal)))
568 (let ((dest (list (car ret))))
570 (while (and (setq ret (rfc822/parse-ascii-token lal))
571 (string-equal (cdr (assq 'specials (car ret))) ",")
572 (setq ret (rfc822/parse-address (cdr ret)))
574 (setq dest (cons (car ret) dest))
580 (defun rfc822/addr-to-string (seq)
583 (if (eq (car token) 'spaces)
590 (defun rfc822/address-string (address)
591 (cond ((eq (car address) 'group)
592 (mapconcat (function rfc822/address-string)
596 ((eq (car address) 'mailbox)
597 (let ((addr (nth 1 address)))
598 (rfc822/addr-to-string
599 (if (eq (car addr) 'phrase-route-addr)
605 (defun rfc822/full-name-string (address)
606 (cond ((eq (car address) 'group)
613 ((eq (car address) 'mailbox)
614 (let ((addr (nth 1 address))
615 (comment (nth 2 address))
617 (if (eq (car addr) 'phrase-route-addr)
618 (setq phrase (mapconcat (function
627 (defun rfc822/extract-address-components (str)
628 "Extract full name and canonical address from STR.
629 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
630 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
631 (let* ((structure (car
632 (rfc822/parse-address
633 (rfc822/lexical-analyze str)
635 (phrase (rfc822/full-name-string structure))
636 (address (rfc822/address-string structure))
638 (list phrase address)
647 ;;; tl-822.el ends here