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.36 1996-08-28 12:28:54 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 (defun rfc822/get-field-body (name &optional boundary)
73 (let ((case-fold-search t))
76 (rfc822/narrow-to-header boundary)
77 (goto-char (point-min))
78 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
79 (buffer-substring-no-properties
85 (defun rfc822/get-field-bodies (field-names &optional default-value boundary)
86 (let ((case-fold-search t))
89 (rfc822/narrow-to-header boundary)
90 (let* ((dest (make-list (length field-names) default-value))
94 (while (setq field-name (car s-rest))
95 (goto-char (point-min))
96 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
98 (buffer-substring-no-properties
100 (rfc822/field-end))))
101 (setq s-rest (cdr s-rest)
110 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
111 (defconst rfc822/quoted-pair-regexp "\\\\.")
112 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
113 (defconst rfc822/qtext-regexp
114 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) "]"))
115 (defconst rfc822/quoted-string-regexp
118 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
122 (defun rfc822/wrap-as-quoted-string (str)
123 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
127 (if (memq chr rfc822/non-qtext-char-list)
128 (concat "\\" (char-to-string chr))
134 (defun rfc822/strip-quoted-pair (str)
140 (setq chr (elt str i))
141 (if (or flag (not (eq chr ?\\)))
143 (setq dest (concat dest (char-to-string chr)))
152 (defun rfc822/strip-quoted-string (str)
153 (rfc822/strip-quoted-pair
154 (let ((max (- (length str) 1))
156 (if (and (eq (elt str 0) ?\")
157 (eq (elt str max) ?\")
159 (substring str 1 max)
167 (defun rfc822/unfolding-string (str)
169 (while (string-match "\n\\s +" str)
170 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
171 (setq str (substring str (match-end 0)))
177 ;;; @ lexical analyze
180 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
181 (defconst rfc822/space-chars " \t\n")
182 (defconst rfc822/non-atom-chars
183 (concat rfc822/special-chars rfc822/space-chars))
184 (defconst rfc822/non-dtext-chars "][")
185 (defconst rfc822/non-ctext-chars "()")
187 (defun rfc822/analyze-spaces (str)
188 (let ((i (string-match (concat "[^" rfc822/space-chars "]") str)))
191 (cons (cons 'spaces (substring str 0 i))
194 (if (not (string-equal str ""))
195 (cons (cons 'spaces str) "")
198 (defun rfc822/analyze-special (str)
199 (if (and (> (length str) 0)
200 (find (elt str 0) rfc822/special-chars)
202 (cons (cons 'specials (substring str 0 1))
207 (defun rfc822/analyze-atom (str)
208 (let ((i (string-match (concat "[" rfc822/non-atom-chars "]") str)))
211 (cons (cons 'atom (substring str 0 i))
214 (if (not (string-equal str ""))
215 (cons (cons 'spaces str) "")
218 (defun rfc822/analyze-quoted-string (str)
219 (let ((len (length str)))
223 (let ((i 1) chr dest)
226 (setq chr (aref str i))
232 (setq dest (concat dest (char-to-string (aref str i))))
236 (cons (cons 'quoted-string dest)
237 (substring str (1+ i)))
241 (setq dest (concat dest (char-to-string (aref str i))))
246 (defun rfc822/analyze-domain-literal (str)
247 (if (and (> (length str) 0)
248 (eq (aref str 0) ?\[)
250 (let* ((i (string-match (concat "[" rfc822/non-dtext-chars "]") str 1))
251 (rest (and i (substring str i)))
255 (eq (aref rest 0) ?\])
257 (cons (cons 'domain-literal (substring str 1 i))
262 (defun rfc822/analyze-comment (str)
263 (if (and (> (length str) 0)
268 (setq str (substring str 1))
270 (while (not (string-equal str ""))
271 (setq p (string-match (concat "[" rfc822/non-ctext-chars "]") str))
273 (setq dest (concat dest (substring str 0 p)))
274 (setq str (substring str p))
276 ((setq ret (rfc822/analyze-comment str))
277 (setq dest (concat dest "(" (cdr (car ret)) ")"))
283 (if (and (> (length str) 0)
286 (cons (cons 'comment dest)
291 (defun rfc822/lexical-analyze (str)
293 (while (not (string-equal str ""))
295 (or (rfc822/analyze-quoted-string str)
296 (rfc822/analyze-domain-literal str)
297 (rfc822/analyze-comment str)
298 (rfc822/analyze-spaces str)
299 (rfc822/analyze-special str)
300 (rfc822/analyze-atom str)
303 (setq dest (cons (car ret) dest))
313 (defun rfc822/ignored-token-p (token)
314 (let ((type (car token)))
315 (or (eq type 'spaces)(eq type 'comment))
318 (defun rfc822/parse-token (lal)
322 (setq token (car lal))
323 (rfc822/ignored-token-p token)
326 (setq itl (cons token itl))
328 (cons (nreverse (cons token itl))
332 (defun rfc822/parse-ascii-token (lal)
333 (let (token itl parsed token-value)
335 (setq token (car lal))
336 (if (and (setq token-value (cdr token))
337 (find-charset-string token-value)
340 (rfc822/ignored-token-p token)
343 (setq itl (cons token itl))
346 (setq parsed (nreverse (cons token itl)))
348 (cons parsed (cdr lal))
351 (defun rfc822/parse-token-or-comment (lal)
355 (setq token (car lal))
356 (eq (car token) 'spaces)
359 (setq itl (cons token itl))
361 (cons (nreverse (cons token itl))
365 (defun rfc822/parse-word (lal)
366 (let ((ret (rfc822/parse-ascii-token lal)))
368 (let ((elt (car ret))
371 (if (or (assq 'atom elt)
372 (assq 'quoted-string elt))
373 (cons (cons 'word elt) rest)
376 (defun rfc822/parse-word-or-comment (lal)
377 (let ((ret (rfc822/parse-token-or-comment lal)))
379 (let ((elt (car ret))
382 (cond ((or (assq 'atom elt)
383 (assq 'quoted-string elt))
384 (cons (cons 'word elt) rest)
387 (cons (cons 'comment-word elt) rest)
391 (defun rfc822/parse-phrase (lal)
393 (while (setq ret (rfc822/parse-word-or-comment lal))
394 (setq phrase (append phrase (cdr (car ret))))
398 (cons (cons 'phrase phrase) lal)
401 (defun rfc822/parse-local-part (lal)
402 (let ((ret (rfc822/parse-word lal)))
404 (let ((local-part (cdr (car ret))) dot)
406 (while (and (setq ret (rfc822/parse-ascii-token lal))
408 (string-equal (cdr (assq 'specials dot)) ".")
409 (setq ret (rfc822/parse-word (cdr ret)))
411 (append local-part dot (cdr (car ret)))
415 (cons (cons 'local-part local-part) lal)
418 (defun rfc822/parse-sub-domain (lal)
419 (let ((ret (rfc822/parse-ascii-token lal)))
421 (let ((sub-domain (car ret)))
422 (if (or (assq 'atom sub-domain)
423 (assq 'domain-literal sub-domain)
425 (cons (cons 'sub-domain sub-domain)
430 (defun rfc822/parse-domain (lal)
431 (let ((ret (rfc822/parse-sub-domain lal)))
433 (let ((domain (cdr (car ret))) dot)
435 (while (and (setq ret (rfc822/parse-ascii-token lal))
437 (string-equal (cdr (assq 'specials dot)) ".")
438 (setq ret (rfc822/parse-sub-domain (cdr ret)))
440 (append domain dot (cdr (car ret)))
444 (cons (cons 'domain domain) lal)
447 (defun rfc822/parse-at-domain (lal)
448 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
450 (setq at-sign (car ret))
451 (string-equal (cdr (assq 'specials at-sign)) "@")
452 (setq ret (rfc822/parse-domain (cdr ret)))
454 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
458 (defun rfc822/parse-addr-spec (lal)
459 (let ((ret (rfc822/parse-local-part lal))
463 (setq addr (cdr (car ret)))
465 (and (setq ret (rfc822/parse-at-domain lal))
466 (setq addr (append addr (cdr (car ret))))
469 (cons (cons 'addr-spec addr) lal)
472 (defun rfc822/parse-route (lal)
473 (let ((ret (rfc822/parse-at-domain lal))
477 (setq route (cdr (car ret)))
479 (while (and (setq ret (rfc822/parse-ascii-token lal))
480 (setq comma (car ret))
481 (string-equal (cdr (assq 'specials comma)) ",")
482 (setq ret (rfc822/parse-at-domain (cdr ret)))
484 (setq route (append route comma (cdr (car ret))))
487 (and (setq ret (rfc822/parse-ascii-token lal))
488 (setq colon (car ret))
489 (string-equal (cdr (assq 'specials colon)) ":")
490 (setq route (append route colon))
493 (cons (cons 'route route)
498 (defun rfc822/parse-route-addr (lal)
499 (let ((ret (rfc822/parse-ascii-token lal))
503 (string-equal (cdr (assq 'specials <)) "<")
505 (progn (and (setq ret (rfc822/parse-route lal))
506 (setq route (cdr (car ret)))
509 (setq ret (rfc822/parse-addr-spec lal))
511 (setq addr-spec (cdr (car ret)))
513 (setq ret (rfc822/parse-ascii-token lal))
515 (string-equal (cdr (assq 'specials >)) ">")
517 (cons (cons 'route-addr (append route addr-spec))
522 (defun rfc822/parse-phrase-route-addr (lal)
523 (let ((ret (rfc822/parse-phrase lal)) phrase)
526 (setq phrase (cdr (car ret)))
529 (if (setq ret (rfc822/parse-route-addr lal))
530 (cons (list 'phrase-route-addr
536 (defun rfc822/parse-mailbox (lal)
537 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
538 (rfc822/parse-addr-spec lal)))
542 (setq mbox (car ret))
544 (if (and (setq ret (rfc822/parse-token-or-comment lal))
545 (setq comment (cdr (assq 'comment (car ret))))
549 (cons (list 'mailbox mbox comment)
553 (defun rfc822/parse-group (lal)
554 (let ((ret (rfc822/parse-phrase lal))
555 phrase colon comma mbox semicolon)
557 (setq phrase (cdr (car ret)))
559 (setq ret (rfc822/parse-ascii-token lal))
560 (setq colon (car ret))
561 (string-equal (cdr (assq 'specials colon)) ":")
564 (and (setq ret (rfc822/parse-mailbox lal))
565 (setq mbox (list (car ret)))
568 (while (and (setq ret (rfc822/parse-ascii-token lal))
569 (setq comma (car ret))
571 (cdr (assq 'specials comma)) ",")
573 (setq ret (rfc822/parse-mailbox lal))
574 (setq mbox (cons (car ret) mbox))
578 (and (setq ret (rfc822/parse-ascii-token lal))
579 (setq semicolon (car ret))
580 (string-equal (cdr (assq 'specials semicolon)) ";")
582 (cons (list 'group phrase (nreverse mbox))
587 (defun rfc822/parse-address (lal)
588 (or (rfc822/parse-group lal)
589 (rfc822/parse-mailbox lal)
592 (defun rfc822/parse-addresses (lal)
593 (let ((ret (rfc822/parse-address lal)))
595 (let ((dest (list (car ret))))
597 (while (and (setq ret (rfc822/parse-ascii-token lal))
598 (string-equal (cdr (assq 'specials (car ret))) ",")
599 (setq ret (rfc822/parse-address (cdr ret)))
601 (setq dest (cons (car ret) dest))
607 (defun rfc822/addr-to-string (seq)
610 (if (eq (car token) 'spaces)
617 (defun rfc822/address-string (address)
618 (cond ((eq (car address) 'group)
619 (mapconcat (function rfc822/address-string)
623 ((eq (car address) 'mailbox)
624 (let ((addr (nth 1 address)))
625 (rfc822/addr-to-string
626 (if (eq (car addr) 'phrase-route-addr)
632 (defun rfc822/full-name-string (address)
633 (cond ((eq (car address) 'group)
640 ((eq (car address) 'mailbox)
641 (let ((addr (nth 1 address))
642 (comment (nth 2 address))
644 (if (eq (car addr) 'phrase-route-addr)
645 (setq phrase (mapconcat (function
654 (defun rfc822/extract-address-components (str)
655 "Extract full name and canonical address from STR.
656 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
657 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
658 (let* ((structure (car
659 (rfc822/parse-address
660 (rfc822/lexical-analyze str)
662 (phrase (rfc822/full-name-string structure))
663 (address (rfc822/address-string structure))
665 (list phrase address)
674 ;;; tl-822.el ends here