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.51 1996-08-28 17:25:39 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-collect-field-names)
49 (defalias `rfc822/field-end 'std11-field-end)
50 (defalias 'rfc822/get-field-body 'std11-find-field-body)
51 (defalias 'rfc822/get-field-bodies 'std11-find-field-bodies)
57 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
58 (defconst rfc822/quoted-pair-regexp "\\\\.")
59 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
60 (defconst rfc822/qtext-regexp
61 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) "]"))
62 (defconst rfc822/quoted-string-regexp
65 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
69 (defun rfc822/wrap-as-quoted-string (str)
70 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
74 (if (memq chr rfc822/non-qtext-char-list)
75 (concat "\\" (char-to-string chr))
81 (defun rfc822/strip-quoted-pair (str)
87 (setq chr (elt str i))
88 (if (or flag (not (eq chr ?\\)))
90 (setq dest (concat dest (char-to-string chr)))
99 (defun rfc822/strip-quoted-string (str)
100 (rfc822/strip-quoted-pair
101 (let ((max (- (length str) 1))
103 (if (and (eq (elt str 0) ?\")
104 (eq (elt str max) ?\")
106 (substring str 1 max)
114 (defalias 'rfc822/unfolding-string 'std11-unfold-string)
117 ;;; @ lexical analyze
120 (defconst rfc822/non-dtext-chars "][")
121 (defconst rfc822/non-ctext-chars "()")
123 (defalias 'rfc822/analyze-spaces 'std11-analyze-spaces)
124 (defalias 'rfc822/analyze-special 'std11-analyze-special)
125 (defalias 'rfc822/analyze-atom 'std11-analyze-atom)
126 (defalias 'rfc822/analyze-quoted-string 'std11-analyze-quoted-string)
128 (defun rfc822/analyze-domain-literal (str)
129 (if (and (> (length str) 0)
130 (eq (aref str 0) ?\[)
132 (let* ((i (string-match (concat "[" rfc822/non-dtext-chars "]") str 1))
133 (rest (and i (substring str i)))
137 (eq (aref rest 0) ?\])
139 (cons (cons 'domain-literal (substring str 1 i))
144 (defun rfc822/analyze-comment (str)
145 (if (and (> (length str) 0)
150 (setq str (substring str 1))
152 (while (not (string-equal str ""))
153 (setq p (string-match (concat "[" rfc822/non-ctext-chars "]") str))
155 (setq dest (concat dest (substring str 0 p)))
156 (setq str (substring str p))
158 ((setq ret (rfc822/analyze-comment str))
159 (setq dest (concat dest "(" (cdr (car ret)) ")"))
165 (if (and (> (length str) 0)
168 (cons (cons 'comment dest)
173 (defun rfc822/lexical-analyze (str)
175 (while (not (string-equal str ""))
177 (or (rfc822/analyze-quoted-string str)
178 (rfc822/analyze-domain-literal str)
179 (rfc822/analyze-comment str)
180 (rfc822/analyze-spaces str)
181 (rfc822/analyze-special str)
182 (rfc822/analyze-atom str)
185 (setq dest (cons (car ret) dest))
195 (defun rfc822/ignored-token-p (token)
196 (let ((type (car token)))
197 (or (eq type 'spaces)(eq type 'comment))
200 (defun rfc822/parse-token (lal)
204 (setq token (car lal))
205 (rfc822/ignored-token-p token)
208 (setq itl (cons token itl))
210 (cons (nreverse (cons token itl))
214 (defun rfc822/parse-ascii-token (lal)
215 (let (token itl parsed token-value)
217 (setq token (car lal))
218 (if (and (setq token-value (cdr token))
219 (find-charset-string token-value)
222 (rfc822/ignored-token-p token)
225 (setq itl (cons token itl))
228 (setq parsed (nreverse (cons token itl)))
230 (cons parsed (cdr lal))
233 (defun rfc822/parse-token-or-comment (lal)
237 (setq token (car lal))
238 (eq (car token) 'spaces)
241 (setq itl (cons token itl))
243 (cons (nreverse (cons token itl))
247 (defun rfc822/parse-word (lal)
248 (let ((ret (rfc822/parse-ascii-token lal)))
250 (let ((elt (car ret))
253 (if (or (assq 'atom elt)
254 (assq 'quoted-string elt))
255 (cons (cons 'word elt) rest)
258 (defun rfc822/parse-word-or-comment (lal)
259 (let ((ret (rfc822/parse-token-or-comment lal)))
261 (let ((elt (car ret))
264 (cond ((or (assq 'atom elt)
265 (assq 'quoted-string elt))
266 (cons (cons 'word elt) rest)
269 (cons (cons 'comment-word elt) rest)
273 (defun rfc822/parse-phrase (lal)
275 (while (setq ret (rfc822/parse-word-or-comment lal))
276 (setq phrase (append phrase (cdr (car ret))))
280 (cons (cons 'phrase phrase) lal)
283 (defun rfc822/parse-local-part (lal)
284 (let ((ret (rfc822/parse-word lal)))
286 (let ((local-part (cdr (car ret))) dot)
288 (while (and (setq ret (rfc822/parse-ascii-token lal))
290 (string-equal (cdr (assq 'specials dot)) ".")
291 (setq ret (rfc822/parse-word (cdr ret)))
293 (append local-part dot (cdr (car ret)))
297 (cons (cons 'local-part local-part) lal)
300 (defun rfc822/parse-sub-domain (lal)
301 (let ((ret (rfc822/parse-ascii-token lal)))
303 (let ((sub-domain (car ret)))
304 (if (or (assq 'atom sub-domain)
305 (assq 'domain-literal sub-domain)
307 (cons (cons 'sub-domain sub-domain)
312 (defun rfc822/parse-domain (lal)
313 (let ((ret (rfc822/parse-sub-domain lal)))
315 (let ((domain (cdr (car ret))) dot)
317 (while (and (setq ret (rfc822/parse-ascii-token lal))
319 (string-equal (cdr (assq 'specials dot)) ".")
320 (setq ret (rfc822/parse-sub-domain (cdr ret)))
322 (append domain dot (cdr (car ret)))
326 (cons (cons 'domain domain) lal)
329 (defun rfc822/parse-at-domain (lal)
330 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
332 (setq at-sign (car ret))
333 (string-equal (cdr (assq 'specials at-sign)) "@")
334 (setq ret (rfc822/parse-domain (cdr ret)))
336 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
340 (defun rfc822/parse-addr-spec (lal)
341 (let ((ret (rfc822/parse-local-part lal))
345 (setq addr (cdr (car ret)))
347 (and (setq ret (rfc822/parse-at-domain lal))
348 (setq addr (append addr (cdr (car ret))))
351 (cons (cons 'addr-spec addr) lal)
354 (defun rfc822/parse-route (lal)
355 (let ((ret (rfc822/parse-at-domain lal))
359 (setq route (cdr (car ret)))
361 (while (and (setq ret (rfc822/parse-ascii-token lal))
362 (setq comma (car ret))
363 (string-equal (cdr (assq 'specials comma)) ",")
364 (setq ret (rfc822/parse-at-domain (cdr ret)))
366 (setq route (append route comma (cdr (car ret))))
369 (and (setq ret (rfc822/parse-ascii-token lal))
370 (setq colon (car ret))
371 (string-equal (cdr (assq 'specials colon)) ":")
372 (setq route (append route colon))
375 (cons (cons 'route route)
380 (defun rfc822/parse-route-addr (lal)
381 (let ((ret (rfc822/parse-ascii-token lal))
385 (string-equal (cdr (assq 'specials <)) "<")
387 (progn (and (setq ret (rfc822/parse-route lal))
388 (setq route (cdr (car ret)))
391 (setq ret (rfc822/parse-addr-spec lal))
393 (setq addr-spec (cdr (car ret)))
395 (setq ret (rfc822/parse-ascii-token lal))
397 (string-equal (cdr (assq 'specials >)) ">")
399 (cons (cons 'route-addr (append route addr-spec))
404 (defun rfc822/parse-phrase-route-addr (lal)
405 (let ((ret (rfc822/parse-phrase lal)) phrase)
408 (setq phrase (cdr (car ret)))
411 (if (setq ret (rfc822/parse-route-addr lal))
412 (cons (list 'phrase-route-addr
418 (defun rfc822/parse-mailbox (lal)
419 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
420 (rfc822/parse-addr-spec lal)))
424 (setq mbox (car ret))
426 (if (and (setq ret (rfc822/parse-token-or-comment lal))
427 (setq comment (cdr (assq 'comment (car ret))))
431 (cons (list 'mailbox mbox comment)
435 (defun rfc822/parse-group (lal)
436 (let ((ret (rfc822/parse-phrase lal))
437 phrase colon comma mbox semicolon)
439 (setq phrase (cdr (car ret)))
441 (setq ret (rfc822/parse-ascii-token lal))
442 (setq colon (car ret))
443 (string-equal (cdr (assq 'specials colon)) ":")
446 (and (setq ret (rfc822/parse-mailbox lal))
447 (setq mbox (list (car ret)))
450 (while (and (setq ret (rfc822/parse-ascii-token lal))
451 (setq comma (car ret))
453 (cdr (assq 'specials comma)) ",")
455 (setq ret (rfc822/parse-mailbox lal))
456 (setq mbox (cons (car ret) mbox))
460 (and (setq ret (rfc822/parse-ascii-token lal))
461 (setq semicolon (car ret))
462 (string-equal (cdr (assq 'specials semicolon)) ";")
464 (cons (list 'group phrase (nreverse mbox))
469 (defun rfc822/parse-address (lal)
470 (or (rfc822/parse-group lal)
471 (rfc822/parse-mailbox lal)
474 (defun rfc822/parse-addresses (lal)
475 (let ((ret (rfc822/parse-address lal)))
477 (let ((dest (list (car ret))))
479 (while (and (setq ret (rfc822/parse-ascii-token lal))
480 (string-equal (cdr (assq 'specials (car ret))) ",")
481 (setq ret (rfc822/parse-address (cdr ret)))
483 (setq dest (cons (car ret) dest))
489 (defun rfc822/addr-to-string (seq)
492 (if (eq (car token) 'spaces)
499 (defun rfc822/address-string (address)
500 (cond ((eq (car address) 'group)
501 (mapconcat (function rfc822/address-string)
505 ((eq (car address) 'mailbox)
506 (let ((addr (nth 1 address)))
507 (rfc822/addr-to-string
508 (if (eq (car addr) 'phrase-route-addr)
514 (defun rfc822/full-name-string (address)
515 (cond ((eq (car address) 'group)
522 ((eq (car address) 'mailbox)
523 (let ((addr (nth 1 address))
524 (comment (nth 2 address))
526 (if (eq (car addr) 'phrase-route-addr)
527 (setq phrase (mapconcat (function
536 (defun rfc822/extract-address-components (str)
537 "Extract full name and canonical address from STR.
538 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
539 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
540 (let* ((structure (car
541 (rfc822/parse-address
542 (rfc822/lexical-analyze str)
544 (phrase (rfc822/full-name-string structure))
545 (address (rfc822/address-string structure))
547 (list phrase address)
556 ;;; tl-822.el ends here