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.40 1996-08-28 13:05:04 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 (defalias `rfc822/field-end 'std11-field-end)
52 (defalias 'rfc822/get-field-body 'std11-field-body)
54 (defun rfc822/get-field-bodies (field-names &optional default-value boundary)
55 (let ((case-fold-search t))
58 (rfc822/narrow-to-header boundary)
59 (let* ((dest (make-list (length field-names) default-value))
63 (while (setq field-name (car s-rest))
64 (goto-char (point-min))
65 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
67 (buffer-substring-no-properties
70 (setq s-rest (cdr s-rest)
79 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
80 (defconst rfc822/quoted-pair-regexp "\\\\.")
81 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
82 (defconst rfc822/qtext-regexp
83 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) "]"))
84 (defconst rfc822/quoted-string-regexp
87 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
91 (defun rfc822/wrap-as-quoted-string (str)
92 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
96 (if (memq chr rfc822/non-qtext-char-list)
97 (concat "\\" (char-to-string chr))
103 (defun rfc822/strip-quoted-pair (str)
109 (setq chr (elt str i))
110 (if (or flag (not (eq chr ?\\)))
112 (setq dest (concat dest (char-to-string chr)))
121 (defun rfc822/strip-quoted-string (str)
122 (rfc822/strip-quoted-pair
123 (let ((max (- (length str) 1))
125 (if (and (eq (elt str 0) ?\")
126 (eq (elt str max) ?\")
128 (substring str 1 max)
136 (defun rfc822/unfolding-string (str)
138 (while (string-match "\n\\s +" str)
139 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
140 (setq str (substring str (match-end 0)))
146 ;;; @ lexical analyze
149 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
150 (defconst rfc822/space-chars " \t\n")
151 (defconst rfc822/non-atom-chars
152 (concat rfc822/special-chars rfc822/space-chars))
153 (defconst rfc822/non-dtext-chars "][")
154 (defconst rfc822/non-ctext-chars "()")
156 (defun rfc822/analyze-spaces (str)
157 (let ((i (string-match (concat "[^" rfc822/space-chars "]") str)))
160 (cons (cons 'spaces (substring str 0 i))
163 (if (not (string-equal str ""))
164 (cons (cons 'spaces str) "")
167 (defun rfc822/analyze-special (str)
168 (if (and (> (length str) 0)
169 (find (elt str 0) rfc822/special-chars)
171 (cons (cons 'specials (substring str 0 1))
176 (defun rfc822/analyze-atom (str)
177 (let ((i (string-match (concat "[" rfc822/non-atom-chars "]") str)))
180 (cons (cons 'atom (substring str 0 i))
183 (if (not (string-equal str ""))
184 (cons (cons 'spaces str) "")
187 (defun rfc822/analyze-quoted-string (str)
188 (let ((len (length str)))
192 (let ((i 1) chr dest)
195 (setq chr (aref str i))
201 (setq dest (concat dest (char-to-string (aref str i))))
205 (cons (cons 'quoted-string dest)
206 (substring str (1+ i)))
210 (setq dest (concat dest (char-to-string (aref str i))))
215 (defun rfc822/analyze-domain-literal (str)
216 (if (and (> (length str) 0)
217 (eq (aref str 0) ?\[)
219 (let* ((i (string-match (concat "[" rfc822/non-dtext-chars "]") str 1))
220 (rest (and i (substring str i)))
224 (eq (aref rest 0) ?\])
226 (cons (cons 'domain-literal (substring str 1 i))
231 (defun rfc822/analyze-comment (str)
232 (if (and (> (length str) 0)
237 (setq str (substring str 1))
239 (while (not (string-equal str ""))
240 (setq p (string-match (concat "[" rfc822/non-ctext-chars "]") str))
242 (setq dest (concat dest (substring str 0 p)))
243 (setq str (substring str p))
245 ((setq ret (rfc822/analyze-comment str))
246 (setq dest (concat dest "(" (cdr (car ret)) ")"))
252 (if (and (> (length str) 0)
255 (cons (cons 'comment dest)
260 (defun rfc822/lexical-analyze (str)
262 (while (not (string-equal str ""))
264 (or (rfc822/analyze-quoted-string str)
265 (rfc822/analyze-domain-literal str)
266 (rfc822/analyze-comment str)
267 (rfc822/analyze-spaces str)
268 (rfc822/analyze-special str)
269 (rfc822/analyze-atom str)
272 (setq dest (cons (car ret) dest))
282 (defun rfc822/ignored-token-p (token)
283 (let ((type (car token)))
284 (or (eq type 'spaces)(eq type 'comment))
287 (defun rfc822/parse-token (lal)
291 (setq token (car lal))
292 (rfc822/ignored-token-p token)
295 (setq itl (cons token itl))
297 (cons (nreverse (cons token itl))
301 (defun rfc822/parse-ascii-token (lal)
302 (let (token itl parsed token-value)
304 (setq token (car lal))
305 (if (and (setq token-value (cdr token))
306 (find-charset-string token-value)
309 (rfc822/ignored-token-p token)
312 (setq itl (cons token itl))
315 (setq parsed (nreverse (cons token itl)))
317 (cons parsed (cdr lal))
320 (defun rfc822/parse-token-or-comment (lal)
324 (setq token (car lal))
325 (eq (car token) 'spaces)
328 (setq itl (cons token itl))
330 (cons (nreverse (cons token itl))
334 (defun rfc822/parse-word (lal)
335 (let ((ret (rfc822/parse-ascii-token lal)))
337 (let ((elt (car ret))
340 (if (or (assq 'atom elt)
341 (assq 'quoted-string elt))
342 (cons (cons 'word elt) rest)
345 (defun rfc822/parse-word-or-comment (lal)
346 (let ((ret (rfc822/parse-token-or-comment lal)))
348 (let ((elt (car ret))
351 (cond ((or (assq 'atom elt)
352 (assq 'quoted-string elt))
353 (cons (cons 'word elt) rest)
356 (cons (cons 'comment-word elt) rest)
360 (defun rfc822/parse-phrase (lal)
362 (while (setq ret (rfc822/parse-word-or-comment lal))
363 (setq phrase (append phrase (cdr (car ret))))
367 (cons (cons 'phrase phrase) lal)
370 (defun rfc822/parse-local-part (lal)
371 (let ((ret (rfc822/parse-word lal)))
373 (let ((local-part (cdr (car ret))) dot)
375 (while (and (setq ret (rfc822/parse-ascii-token lal))
377 (string-equal (cdr (assq 'specials dot)) ".")
378 (setq ret (rfc822/parse-word (cdr ret)))
380 (append local-part dot (cdr (car ret)))
384 (cons (cons 'local-part local-part) lal)
387 (defun rfc822/parse-sub-domain (lal)
388 (let ((ret (rfc822/parse-ascii-token lal)))
390 (let ((sub-domain (car ret)))
391 (if (or (assq 'atom sub-domain)
392 (assq 'domain-literal sub-domain)
394 (cons (cons 'sub-domain sub-domain)
399 (defun rfc822/parse-domain (lal)
400 (let ((ret (rfc822/parse-sub-domain lal)))
402 (let ((domain (cdr (car ret))) dot)
404 (while (and (setq ret (rfc822/parse-ascii-token lal))
406 (string-equal (cdr (assq 'specials dot)) ".")
407 (setq ret (rfc822/parse-sub-domain (cdr ret)))
409 (append domain dot (cdr (car ret)))
413 (cons (cons 'domain domain) lal)
416 (defun rfc822/parse-at-domain (lal)
417 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
419 (setq at-sign (car ret))
420 (string-equal (cdr (assq 'specials at-sign)) "@")
421 (setq ret (rfc822/parse-domain (cdr ret)))
423 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
427 (defun rfc822/parse-addr-spec (lal)
428 (let ((ret (rfc822/parse-local-part lal))
432 (setq addr (cdr (car ret)))
434 (and (setq ret (rfc822/parse-at-domain lal))
435 (setq addr (append addr (cdr (car ret))))
438 (cons (cons 'addr-spec addr) lal)
441 (defun rfc822/parse-route (lal)
442 (let ((ret (rfc822/parse-at-domain lal))
446 (setq route (cdr (car ret)))
448 (while (and (setq ret (rfc822/parse-ascii-token lal))
449 (setq comma (car ret))
450 (string-equal (cdr (assq 'specials comma)) ",")
451 (setq ret (rfc822/parse-at-domain (cdr ret)))
453 (setq route (append route comma (cdr (car ret))))
456 (and (setq ret (rfc822/parse-ascii-token lal))
457 (setq colon (car ret))
458 (string-equal (cdr (assq 'specials colon)) ":")
459 (setq route (append route colon))
462 (cons (cons 'route route)
467 (defun rfc822/parse-route-addr (lal)
468 (let ((ret (rfc822/parse-ascii-token lal))
472 (string-equal (cdr (assq 'specials <)) "<")
474 (progn (and (setq ret (rfc822/parse-route lal))
475 (setq route (cdr (car ret)))
478 (setq ret (rfc822/parse-addr-spec lal))
480 (setq addr-spec (cdr (car ret)))
482 (setq ret (rfc822/parse-ascii-token lal))
484 (string-equal (cdr (assq 'specials >)) ">")
486 (cons (cons 'route-addr (append route addr-spec))
491 (defun rfc822/parse-phrase-route-addr (lal)
492 (let ((ret (rfc822/parse-phrase lal)) phrase)
495 (setq phrase (cdr (car ret)))
498 (if (setq ret (rfc822/parse-route-addr lal))
499 (cons (list 'phrase-route-addr
505 (defun rfc822/parse-mailbox (lal)
506 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
507 (rfc822/parse-addr-spec lal)))
511 (setq mbox (car ret))
513 (if (and (setq ret (rfc822/parse-token-or-comment lal))
514 (setq comment (cdr (assq 'comment (car ret))))
518 (cons (list 'mailbox mbox comment)
522 (defun rfc822/parse-group (lal)
523 (let ((ret (rfc822/parse-phrase lal))
524 phrase colon comma mbox semicolon)
526 (setq phrase (cdr (car ret)))
528 (setq ret (rfc822/parse-ascii-token lal))
529 (setq colon (car ret))
530 (string-equal (cdr (assq 'specials colon)) ":")
533 (and (setq ret (rfc822/parse-mailbox lal))
534 (setq mbox (list (car ret)))
537 (while (and (setq ret (rfc822/parse-ascii-token lal))
538 (setq comma (car ret))
540 (cdr (assq 'specials comma)) ",")
542 (setq ret (rfc822/parse-mailbox lal))
543 (setq mbox (cons (car ret) mbox))
547 (and (setq ret (rfc822/parse-ascii-token lal))
548 (setq semicolon (car ret))
549 (string-equal (cdr (assq 'specials semicolon)) ";")
551 (cons (list 'group phrase (nreverse mbox))
556 (defun rfc822/parse-address (lal)
557 (or (rfc822/parse-group lal)
558 (rfc822/parse-mailbox lal)
561 (defun rfc822/parse-addresses (lal)
562 (let ((ret (rfc822/parse-address lal)))
564 (let ((dest (list (car ret))))
566 (while (and (setq ret (rfc822/parse-ascii-token lal))
567 (string-equal (cdr (assq 'specials (car ret))) ",")
568 (setq ret (rfc822/parse-address (cdr ret)))
570 (setq dest (cons (car ret) dest))
576 (defun rfc822/addr-to-string (seq)
579 (if (eq (car token) 'spaces)
586 (defun rfc822/address-string (address)
587 (cond ((eq (car address) 'group)
588 (mapconcat (function rfc822/address-string)
592 ((eq (car address) 'mailbox)
593 (let ((addr (nth 1 address)))
594 (rfc822/addr-to-string
595 (if (eq (car addr) 'phrase-route-addr)
601 (defun rfc822/full-name-string (address)
602 (cond ((eq (car address) 'group)
609 ((eq (car address) 'mailbox)
610 (let ((addr (nth 1 address))
611 (comment (nth 2 address))
613 (if (eq (car addr) 'phrase-route-addr)
614 (setq phrase (mapconcat (function
623 (defun rfc822/extract-address-components (str)
624 "Extract full name and canonical address from STR.
625 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
626 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
627 (let* ((structure (car
628 (rfc822/parse-address
629 (rfc822/lexical-analyze str)
631 (phrase (rfc822/full-name-string structure))
632 (address (rfc822/address-string structure))
634 (list phrase address)
643 ;;; tl-822.el ends here