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.
31 (defconst rfc822/RCS-ID
32 "$Id: tl-822.el,v 7.33 1996-08-16 05:44:47 morioka Exp $")
33 (defconst rfc822/version (get-version-string rfc822/RCS-ID))
39 (defun rfc822/narrow-to-header (&optional boundary)
40 (narrow-to-region (goto-char (point-min))
41 (if (re-search-forward
42 (concat "^\\(" (regexp-quote
43 (or boundary "")) "\\)?$") nil t)
48 (defun rfc822/get-header-string (pat &optional boundary)
49 (let ((case-fold-search t))
52 (rfc822/narrow-to-header boundary)
53 (goto-char (point-min))
55 (while (re-search-forward rfc822/field-top-regexp nil t)
56 (setq field (buffer-substring (match-beginning 0)
59 (if (string-match pat field)
60 (setq header (concat header field "\n"))
65 (defun rfc822/get-header-string-except (pat &optional boundary)
66 (let ((case-fold-search t))
69 (rfc822/narrow-to-header boundary)
70 (goto-char (point-min))
72 (while (re-search-forward rfc822/field-top-regexp nil t)
73 (setq field (buffer-substring (match-beginning 0)
76 (if (not (string-match pat field))
77 (setq header (concat header field "\n"))
86 (defconst rfc822/field-name-regexp "[!-9;-~]+")
88 (defconst rfc822/field-top-regexp
89 (concat "\\(" rfc822/field-name-regexp "\\):"))
91 (defconst rfc822::next-field-top-regexp (concat "\n" rfc822/field-top-regexp))
93 (defun rfc822/get-field-names (&optional boundary)
96 (rfc822/narrow-to-header boundary)
97 (goto-char (point-min))
98 (let ((pat (concat "^\\(" rfc822/field-name-regexp "\\):"))
100 (while (re-search-forward pat nil t)
101 (setq name (buffer-substring (match-beginning 1)(match-end 1)))
102 (or (member name dest)
103 (setq dest (cons name dest))
108 (defun rfc822/field-end ()
109 (if (re-search-forward rfc822::next-field-top-regexp nil t)
110 (goto-char (match-beginning 0))
111 (if (re-search-forward "^$" nil t)
112 (goto-char (1- (match-beginning 0)))
118 (defun rfc822/get-field-body (name &optional boundary)
119 (let ((case-fold-search t))
122 (rfc822/narrow-to-header boundary)
123 (goto-char (point-min))
124 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
125 (buffer-substring-no-properties
131 (defun rfc822/get-field-bodies (field-names &optional default-value boundary)
132 (let ((case-fold-search t))
135 (rfc822/narrow-to-header boundary)
136 (let* ((dest (make-list (length field-names) default-value))
140 (while (setq field-name (car s-rest))
141 (goto-char (point-min))
142 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
144 (buffer-substring-no-properties
146 (rfc822/field-end))))
147 (setq s-rest (cdr s-rest)
156 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
157 (defconst rfc822/quoted-pair-regexp "\\\\.")
158 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
159 (defconst rfc822/qtext-regexp
160 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) "]"))
161 (defconst rfc822/quoted-string-regexp
164 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-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 (string-match (concat "[^" rfc822/space-chars "]") str)))
237 (cons (cons 'spaces (substring str 0 i))
240 (if (not (string-equal str ""))
241 (cons (cons 'spaces str) "")
244 (defun rfc822/analyze-special (str)
245 (if (and (> (length str) 0)
246 (find (elt str 0) rfc822/special-chars)
248 (cons (cons 'specials (substring str 0 1))
253 (defun rfc822/analyze-atom (str)
254 (let ((i (string-match (concat "[" rfc822/non-atom-chars "]") str)))
257 (cons (cons 'atom (substring str 0 i))
260 (if (not (string-equal str ""))
261 (cons (cons 'spaces str) "")
264 (defun rfc822/analyze-quoted-string (str)
265 (let ((len (length str)))
269 (let ((i 1) chr dest)
272 (setq chr (aref str i))
278 (setq dest (concat dest (char-to-string (aref str i))))
282 (cons (cons 'quoted-string dest)
283 (substring str (1+ i)))
287 (setq dest (concat dest (char-to-string (aref str i))))
292 (defun rfc822/analyze-domain-literal (str)
293 (if (and (> (length str) 0)
296 (let* ((i (position-mismatched
299 (not (find elt rfc822/non-dtext-chars))
301 (setq str (substring str 1))
303 (rest (substring str i))
307 (eq (elt rest 0) ?\])
309 (cons (cons 'domain-literal (substring str 0 i))
314 (defun rfc822/analyze-comment (str)
315 (if (and (> (length str) 0)
320 (setq str (substring str 1))
322 (while (not (string-equal str ""))
323 (setq p (position-mismatched
326 (not (find elt rfc822/non-ctext-chars))
329 (setq dest (concat dest (substring str 0 p)))
330 (setq str (substring str p))
332 ((setq ret (rfc822/analyze-comment str))
333 (setq dest (concat dest "(" (cdr (car ret)) ")"))
339 (if (and (> (length str) 0)
342 (cons (cons 'comment dest)
347 (defun rfc822/lexical-analyze (str)
349 (while (not (string-equal str ""))
351 (or (rfc822/analyze-quoted-string str)
352 (rfc822/analyze-domain-literal str)
353 (rfc822/analyze-comment str)
354 (rfc822/analyze-spaces str)
355 (rfc822/analyze-special str)
356 (rfc822/analyze-atom str)
359 (setq dest (cons (car ret) dest))
369 (defun rfc822/ignored-token-p (token)
370 (let ((type (car token)))
371 (or (eq type 'spaces)(eq type 'comment))
374 (defun rfc822/parse-token (lal)
378 (setq token (car lal))
379 (rfc822/ignored-token-p token)
382 (setq itl (cons token itl))
384 (cons (nreverse (cons token itl))
388 (defun rfc822/parse-ascii-token (lal)
389 (let (token itl parsed token-value)
391 (setq token (car lal))
392 (if (and (setq token-value (cdr token))
393 (find-charset-string token-value)
396 (rfc822/ignored-token-p token)
399 (setq itl (cons token itl))
402 (setq parsed (nreverse (cons token itl)))
404 (cons parsed (cdr lal))
407 (defun rfc822/parse-token-or-comment (lal)
411 (setq token (car lal))
412 (eq (car token) 'spaces)
415 (setq itl (cons token itl))
417 (cons (nreverse (cons token itl))
421 (defun rfc822/parse-word (lal)
422 (let ((ret (rfc822/parse-ascii-token lal)))
424 (let ((elt (car ret))
427 (if (or (assq 'atom elt)
428 (assq 'quoted-string elt))
429 (cons (cons 'word elt) rest)
432 (defun rfc822/parse-word-or-comment (lal)
433 (let ((ret (rfc822/parse-token-or-comment lal)))
435 (let ((elt (car ret))
438 (cond ((or (assq 'atom elt)
439 (assq 'quoted-string elt))
440 (cons (cons 'word elt) rest)
443 (cons (cons 'comment-word elt) rest)
447 (defun rfc822/parse-phrase (lal)
449 (while (setq ret (rfc822/parse-word-or-comment lal))
450 (setq phrase (append phrase (cdr (car ret))))
454 (cons (cons 'phrase phrase) lal)
457 (defun rfc822/parse-local-part (lal)
458 (let ((ret (rfc822/parse-word lal)))
460 (let ((local-part (cdr (car ret))) dot)
462 (while (and (setq ret (rfc822/parse-ascii-token lal))
464 (string-equal (cdr (assq 'specials dot)) ".")
465 (setq ret (rfc822/parse-word (cdr ret)))
467 (append local-part dot (cdr (car ret)))
471 (cons (cons 'local-part local-part) lal)
474 (defun rfc822/parse-sub-domain (lal)
475 (let ((ret (rfc822/parse-ascii-token lal)))
477 (let ((sub-domain (car ret)))
478 (if (or (assq 'atom sub-domain)
479 (assq 'domain-literal sub-domain)
481 (cons (cons 'sub-domain sub-domain)
486 (defun rfc822/parse-domain (lal)
487 (let ((ret (rfc822/parse-sub-domain lal)))
489 (let ((domain (cdr (car ret))) dot)
491 (while (and (setq ret (rfc822/parse-ascii-token lal))
493 (string-equal (cdr (assq 'specials dot)) ".")
494 (setq ret (rfc822/parse-sub-domain (cdr ret)))
496 (append domain dot (cdr (car ret)))
500 (cons (cons 'domain domain) lal)
503 (defun rfc822/parse-at-domain (lal)
504 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
506 (setq at-sign (car ret))
507 (string-equal (cdr (assq 'specials at-sign)) "@")
508 (setq ret (rfc822/parse-domain (cdr ret)))
510 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
514 (defun rfc822/parse-addr-spec (lal)
515 (let ((ret (rfc822/parse-local-part lal))
519 (setq addr (cdr (car ret)))
521 (and (setq ret (rfc822/parse-at-domain lal))
522 (setq addr (append addr (cdr (car ret))))
525 (cons (cons 'addr-spec addr) lal)
528 (defun rfc822/parse-route (lal)
529 (let ((ret (rfc822/parse-at-domain lal))
533 (setq route (cdr (car ret)))
535 (while (and (setq ret (rfc822/parse-ascii-token lal))
536 (setq comma (car ret))
537 (string-equal (cdr (assq 'specials comma)) ",")
538 (setq ret (rfc822/parse-at-domain (cdr ret)))
540 (setq route (append route comma (cdr (car ret))))
543 (and (setq ret (rfc822/parse-ascii-token lal))
544 (setq colon (car ret))
545 (string-equal (cdr (assq 'specials colon)) ":")
546 (setq route (append route colon))
549 (cons (cons 'route route)
554 (defun rfc822/parse-route-addr (lal)
555 (let ((ret (rfc822/parse-ascii-token lal))
559 (string-equal (cdr (assq 'specials <)) "<")
561 (progn (and (setq ret (rfc822/parse-route lal))
562 (setq route (cdr (car ret)))
565 (setq ret (rfc822/parse-addr-spec lal))
567 (setq addr-spec (cdr (car ret)))
569 (setq ret (rfc822/parse-ascii-token lal))
571 (string-equal (cdr (assq 'specials >)) ">")
573 (cons (cons 'route-addr (append route addr-spec))
578 (defun rfc822/parse-phrase-route-addr (lal)
579 (let ((ret (rfc822/parse-phrase lal)) phrase)
582 (setq phrase (cdr (car ret)))
585 (if (setq ret (rfc822/parse-route-addr lal))
586 (cons (list 'phrase-route-addr
592 (defun rfc822/parse-mailbox (lal)
593 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
594 (rfc822/parse-addr-spec lal)))
598 (setq mbox (car ret))
600 (if (and (setq ret (rfc822/parse-token-or-comment lal))
601 (setq comment (cdr (assq 'comment (car ret))))
605 (cons (list 'mailbox mbox comment)
609 (defun rfc822/parse-group (lal)
610 (let ((ret (rfc822/parse-phrase lal))
611 phrase colon comma mbox semicolon)
613 (setq phrase (cdr (car ret)))
615 (setq ret (rfc822/parse-ascii-token lal))
616 (setq colon (car ret))
617 (string-equal (cdr (assq 'specials colon)) ":")
620 (and (setq ret (rfc822/parse-mailbox lal))
621 (setq mbox (list (car ret)))
624 (while (and (setq ret (rfc822/parse-ascii-token lal))
625 (setq comma (car ret))
627 (cdr (assq 'specials comma)) ",")
629 (setq ret (rfc822/parse-mailbox lal))
630 (setq mbox (cons (car ret) mbox))
634 (and (setq ret (rfc822/parse-ascii-token lal))
635 (setq semicolon (car ret))
636 (string-equal (cdr (assq 'specials semicolon)) ";")
638 (cons (list 'group phrase (nreverse mbox))
643 (defun rfc822/parse-address (lal)
644 (or (rfc822/parse-group lal)
645 (rfc822/parse-mailbox lal)
648 (defun rfc822/parse-addresses (lal)
649 (let ((ret (rfc822/parse-address lal)))
651 (let ((dest (list (car ret))))
653 (while (and (setq ret (rfc822/parse-ascii-token lal))
654 (string-equal (cdr (assq 'specials (car ret))) ",")
655 (setq ret (rfc822/parse-address (cdr ret)))
657 (setq dest (cons (car ret) dest))
663 (defun rfc822/addr-to-string (seq)
666 (if (eq (car token) 'spaces)
673 (defun rfc822/address-string (address)
674 (cond ((eq (car address) 'group)
675 (mapconcat (function rfc822/address-string)
679 ((eq (car address) 'mailbox)
680 (let ((addr (nth 1 address)))
681 (rfc822/addr-to-string
682 (if (eq (car addr) 'phrase-route-addr)
688 (defun rfc822/full-name-string (address)
689 (cond ((eq (car address) 'group)
696 ((eq (car address) 'mailbox)
697 (let ((addr (nth 1 address))
698 (comment (nth 2 address))
700 (if (eq (car addr) 'phrase-route-addr)
701 (setq phrase (mapconcat (function
710 (defun rfc822/extract-address-components (str)
711 "Extract full name and canonical address from STR.
712 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
713 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
714 (let* ((structure (car
715 (rfc822/parse-address
716 (rfc822/lexical-analyze str)
718 (phrase (rfc822/full-name-string structure))
719 (address (rfc822/address-string structure))
721 (list phrase address)
730 ;;; tl-822.el ends here