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.21 1996-04-25 21:30:12 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 boundary)
71 (let ((case-fold-search t))
74 (rfc822/narrow-to-header boundary)
75 (goto-char (point-min))
76 (let* ((dest (make-list (length field-names) default-value))
80 (while (setq field-name (car s-rest))
81 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
83 (buffer-substring-no-properties
86 (setq s-rest (cdr s-rest)
95 (defun rfc822/narrow-to-header (&optional boundary)
96 (narrow-to-region (goto-char (point-min))
97 (if (re-search-forward
98 (concat "^\\(" (regexp-quote
99 (or boundary "")) "\\)?$") nil t)
104 (defun rfc822/get-header-string (pat &optional boundary)
105 (let ((case-fold-search t))
108 (rfc822/narrow-to-header boundary)
109 (goto-char (point-min))
111 (while (re-search-forward rfc822/field-top-regexp nil t)
112 (setq field (buffer-substring (match-beginning 0)
115 (if (string-match pat field)
116 (setq header (concat header field "\n"))
121 (defun rfc822/get-header-string-except (pat &optional boundary)
122 (let ((case-fold-search t))
125 (rfc822/narrow-to-header boundary)
126 (goto-char (point-min))
128 (while (re-search-forward rfc822/field-top-regexp nil t)
129 (setq field (buffer-substring (match-beginning 0)
132 (if (not (string-match pat field))
133 (setq header (concat header field "\n"))
142 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
143 (defconst rfc822/quoted-pair-regexp "\\\\.")
144 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
145 (defconst rfc822/qtext-regexp
146 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) " \t]"))
147 (defconst rfc822/quoted-string-regexp
151 "\\(" rfc822/linear-white-space-regexp "?"
152 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
154 rfc822/linear-white-space-regexp "?"
157 (defun rfc822/wrap-as-quoted-string (str)
158 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
162 (if (memq chr rfc822/non-qtext-char-list)
163 (concat "\\" (char-to-string chr))
169 (defun rfc822/strip-quoted-pair (str)
175 (setq chr (elt str i))
176 (if (or flag (not (eq chr ?\\)))
178 (setq dest (concat dest (char-to-string chr)))
187 (defun rfc822/strip-quoted-string (str)
188 (rfc822/strip-quoted-pair
189 (let ((max (- (length str) 1))
191 (if (and (eq (elt str 0) ?\")
192 (eq (elt str max) ?\")
194 (substring str 1 max)
202 (defun rfc822/unfolding-string (str)
204 (while (string-match "\n\\s +" str)
205 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
206 (setq str (substring str (match-end 0)))
212 ;;; @ lexical analyze
215 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
216 (defconst rfc822/space-chars " \t\n")
217 (defconst rfc822/non-atom-chars
218 (concat rfc822/special-chars rfc822/space-chars))
219 (defconst rfc822/non-dtext-chars "[]")
220 (defconst rfc822/non-ctext-chars "()")
222 (defun rfc822/analyze-spaces (str)
223 (let ((i (position-mismatched
226 (find elt rfc822/space-chars)
230 (cons (cons 'spaces (substring str 0 i))
235 (defun rfc822/analyze-special (str)
236 (if (and (> (length str) 0)
237 (find (elt str 0) rfc822/special-chars)
239 (cons (cons 'specials (substring str 0 1))
244 (defun rfc822/analyze-atom (str)
245 (let ((i (position-mismatched
248 (not (find elt rfc822/non-atom-chars))
252 (cons (cons 'atom (substring str 0 i))
257 (defun rfc822/analyze-quoted-pair (str)
258 (if (and (>= (length str) 2)
261 (cons (cons 'quoted-pair (substring str 0 2))
266 (defun rfc822/analyze-quoted-string (str)
267 (if (and (> (length str) 0)
270 (let* ((i (position-mismatched
273 (not (memq elt rfc822/non-qtext-char-list))
275 (setq str (substring str 1))
277 (rest (substring str i))
281 (eq (elt rest 0) ?\")
283 (cons (cons 'quoted-string (substring str 0 i))
288 (defun rfc822/analyze-domain-literal (str)
289 (if (and (> (length str) 0)
292 (let* ((i (position-mismatched
295 (not (find elt rfc822/non-dtext-chars))
297 (setq str (substring str 1))
299 (rest (substring str i))
303 (eq (elt rest 0) ?\])
305 (cons (cons 'domain-literal (substring str 0 i))
310 (defun rfc822/analyze-comment (str)
311 (if (and (> (length str) 0)
316 (setq str (substring str 1))
318 (while (not (string-equal str ""))
319 (setq p (position-mismatched
322 (not (find elt rfc822/non-ctext-chars))
325 (setq dest (concat dest (substring str 0 p)))
326 (setq str (substring str p))
328 ((setq ret (rfc822/analyze-comment str))
329 (setq dest (concat dest "(" (cdr (car ret)) ")"))
335 (if (and (> (length str) 0)
338 (cons (cons 'comment dest)
343 (defun rfc822/lexical-analyze (str)
345 (i 0)(len (length str))
347 (while (not (string-equal str ""))
349 (or (rfc822/analyze-quoted-string str)
350 (rfc822/analyze-domain-literal str)
351 (rfc822/analyze-comment str)
352 (rfc822/analyze-spaces str)
353 (rfc822/analyze-special str)
354 (rfc822/analyze-atom str)
357 (setq dest (cons (car ret) dest))
367 (defun rfc822/ignored-token-p (token)
368 (let ((type (car token)))
369 (or (eq type 'spaces)(eq type 'comment))
372 (defun rfc822/parse-token (lal)
376 (setq token (car lal))
377 (rfc822/ignored-token-p token)
380 (setq itl (cons token itl))
382 (cons (nreverse (cons token itl))
386 (defun rfc822/parse-ascii-token (lal)
387 (let (token itl parsed token-value)
389 (setq token (car lal))
390 (if (and (setq token-value (cdr token))
391 (find-charset-string token-value)
394 (rfc822/ignored-token-p token)
397 (setq itl (cons token itl))
400 (setq parsed (nreverse (cons token itl)))
402 (cons parsed (cdr lal))
405 (defun rfc822/parse-token-or-comment (lal)
409 (setq token (car lal))
410 (eq (car token) 'spaces)
413 (setq itl (cons token itl))
415 (cons (nreverse (cons token itl))
419 (defun rfc822/parse-word (lal)
420 (let ((ret (rfc822/parse-ascii-token lal)))
422 (let ((elt (car ret))
425 (if (or (assq 'atom elt)
426 (assq 'quoted-string elt))
427 (cons (cons 'word elt) rest)
430 (defun rfc822/parse-word-or-comment (lal)
431 (let ((ret (rfc822/parse-token-or-comment lal)))
433 (let ((elt (car ret))
436 (cond ((or (assq 'atom elt)
437 (assq 'quoted-string elt))
438 (cons (cons 'word elt) rest)
441 (cons (cons 'comment-word elt) rest)
445 (defun rfc822/parse-phrase (lal)
447 (while (setq ret (rfc822/parse-word-or-comment lal))
448 (setq phrase (append phrase (cdr (car ret))))
452 (cons (cons 'phrase phrase) lal)
455 (defun rfc822/parse-local-part (lal)
456 (let ((ret (rfc822/parse-word lal)))
458 (let ((local-part (cdr (car ret))) dot)
460 (while (and (setq ret (rfc822/parse-ascii-token lal))
462 (string-equal (cdr (assq 'specials dot)) ".")
463 (setq ret (rfc822/parse-word (cdr ret)))
465 (append local-part dot (cdr (car ret)))
469 (cons (cons 'local-part local-part) lal)
472 (defun rfc822/parse-sub-domain (lal)
473 (let ((ret (rfc822/parse-ascii-token lal)))
475 (let ((sub-domain (car ret)))
476 (if (or (assq 'atom sub-domain)
477 (assq 'domain-literal sub-domain)
479 (cons (cons 'sub-domain sub-domain)
484 (defun rfc822/parse-domain (lal)
485 (let ((ret (rfc822/parse-sub-domain lal)))
487 (let ((domain (cdr (car ret))) dot)
489 (while (and (setq ret (rfc822/parse-ascii-token lal))
491 (string-equal (cdr (assq 'specials dot)) ".")
492 (setq ret (rfc822/parse-sub-domain (cdr ret)))
494 (append domain dot (cdr (car ret)))
498 (cons (cons 'domain domain) lal)
501 (defun rfc822/parse-at-domain (lal)
502 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
504 (setq at-sign (car ret))
505 (string-equal (cdr (assq 'specials at-sign)) "@")
506 (setq ret (rfc822/parse-domain (cdr ret)))
508 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
512 (defun rfc822/parse-addr-spec (lal)
513 (let ((ret (rfc822/parse-local-part lal))
517 (setq addr (cdr (car ret)))
519 (and (setq ret (rfc822/parse-at-domain lal))
520 (setq addr (append addr (cdr (car ret))))
523 (cons (cons 'addr-spec addr) lal)
526 (defun rfc822/parse-route (lal)
527 (let ((ret (rfc822/parse-at-domain lal))
531 (setq route (cdr (car ret)))
533 (while (and (setq ret (rfc822/parse-ascii-token lal))
534 (setq comma (car ret))
535 (string-equal (cdr (assq 'specials comma)) ",")
536 (setq ret (rfc822/parse-at-domain (cdr ret)))
538 (setq route (append route comma (cdr (car ret))))
541 (and (setq ret (rfc822/parse-ascii-token lal))
542 (setq colon (car ret))
543 (string-equal (cdr (assq 'specials colon)) ":")
544 (setq route (append route colon))
547 (cons (cons 'route route)
552 (defun rfc822/parse-route-addr (lal)
553 (let ((ret (rfc822/parse-ascii-token lal))
557 (string-equal (cdr (assq 'specials <)) "<")
559 (progn (and (setq ret (rfc822/parse-route lal))
560 (setq route (cdr (car ret)))
563 (setq ret (rfc822/parse-addr-spec lal))
565 (setq addr-spec (cdr (car ret)))
567 (setq ret (rfc822/parse-ascii-token lal))
569 (string-equal (cdr (assq 'specials >)) ">")
571 (cons (cons 'route-addr (append route addr-spec))
576 (defun rfc822/parse-phrase-route-addr (lal)
577 (let ((ret (rfc822/parse-phrase lal)) phrase)
580 (setq phrase (cdr (car ret)))
583 (if (setq ret (rfc822/parse-route-addr lal))
584 (cons (list 'phrase-route-addr
590 (defun rfc822/parse-mailbox (lal)
591 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
592 (rfc822/parse-addr-spec lal)))
596 (setq mbox (car ret))
598 (if (and (setq ret (rfc822/parse-token-or-comment lal))
599 (setq comment (cdr (assq 'comment (car ret))))
603 (cons (list 'mailbox mbox comment)
607 (defun rfc822/parse-group (lal)
608 (let ((ret (rfc822/parse-phrase lal))
609 phrase colon comma mbox semicolon)
611 (setq phrase (cdr (car ret)))
613 (setq ret (rfc822/parse-ascii-token lal))
614 (setq colon (car ret))
615 (string-equal (cdr (assq 'specials colon)) ":")
618 (and (setq ret (rfc822/parse-mailbox lal))
619 (setq mbox (list (car ret)))
622 (while (and (setq ret (rfc822/parse-ascii-token lal))
623 (setq comma (car ret))
625 (cdr (assq 'specials comma)) ",")
627 (setq ret (rfc822/parse-mailbox lal))
628 (setq mbox (cons (car ret) mbox))
632 (and (setq ret (rfc822/parse-ascii-token lal))
633 (setq semicolon (car ret))
634 (string-equal (cdr (assq 'specials semicolon)) ";")
636 (cons (list 'group phrase (nreverse mbox))
641 (defun rfc822/parse-address (lal)
642 (or (rfc822/parse-group lal)
643 (rfc822/parse-mailbox lal)
646 (defun rfc822/parse-addresses (lal)
647 (let ((ret (rfc822/parse-address lal)))
649 (let ((dest (list (car ret))))
651 (while (and (setq ret (rfc822/parse-ascii-token lal))
652 (string-equal (cdr (assq 'specials (car ret))) ",")
653 (setq ret (rfc822/parse-address (cdr ret)))
655 (setq dest (cons (car ret) dest))
661 (defun rfc822/addr-to-string (seq)
664 (if (eq (car token) 'spaces)
671 (defun rfc822/address-string (address)
672 (cond ((eq (car address) 'group)
673 (mapconcat (function rfc822/address-string)
677 ((eq (car address) 'mailbox)
678 (let ((addr (nth 1 address))
680 (rfc822/addr-to-string
681 (if (eq (car addr) 'phrase-route-addr)
687 (defun rfc822/full-name-string (address)
688 (cond ((eq (car address) 'group)
695 ((eq (car address) 'mailbox)
696 (let ((addr (nth 1 address))
697 (comment (nth 2 address))
699 (if (eq (car addr) 'phrase-route-addr)
700 (setq phrase (mapconcat (function
709 (defun rfc822/extract-address-components (str)
710 "Extract full name and canonical address from STR.
711 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
712 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
713 (let* ((structure (car
714 (rfc822/parse-address
715 (rfc822/lexical-analyze str)
717 (phrase (rfc822/full-name-string structure))
718 (address (rfc822/address-string structure))
720 (list phrase address)
729 ;;; tl-822.el ends here