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.53 1996-08-28 18:07:50 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-ctext-chars "()")
122 (defalias 'rfc822/analyze-spaces 'std11-analyze-spaces)
123 (defalias 'rfc822/analyze-special 'std11-analyze-special)
124 (defalias 'rfc822/analyze-atom 'std11-analyze-atom)
125 (defalias 'rfc822/analyze-quoted-string 'std11-analyze-quoted-string)
126 (defalias 'rfc822/analyze-domain-literal 'std11-analyze-domain-literal)
127 (defalias 'rfc822/analyze-comment 'std11-analyze-comment)
129 (defun rfc822/lexical-analyze (str)
131 (while (not (string-equal str ""))
133 (or (rfc822/analyze-quoted-string str)
134 (rfc822/analyze-domain-literal str)
135 (rfc822/analyze-comment str)
136 (rfc822/analyze-spaces str)
137 (rfc822/analyze-special str)
138 (rfc822/analyze-atom str)
141 (setq dest (cons (car ret) dest))
151 (defun rfc822/ignored-token-p (token)
152 (let ((type (car token)))
153 (or (eq type 'spaces)(eq type 'comment))
156 (defun rfc822/parse-token (lal)
160 (setq token (car lal))
161 (rfc822/ignored-token-p token)
164 (setq itl (cons token itl))
166 (cons (nreverse (cons token itl))
170 (defun rfc822/parse-ascii-token (lal)
171 (let (token itl parsed token-value)
173 (setq token (car lal))
174 (if (and (setq token-value (cdr token))
175 (find-charset-string token-value)
178 (rfc822/ignored-token-p token)
181 (setq itl (cons token itl))
184 (setq parsed (nreverse (cons token itl)))
186 (cons parsed (cdr lal))
189 (defun rfc822/parse-token-or-comment (lal)
193 (setq token (car lal))
194 (eq (car token) 'spaces)
197 (setq itl (cons token itl))
199 (cons (nreverse (cons token itl))
203 (defun rfc822/parse-word (lal)
204 (let ((ret (rfc822/parse-ascii-token lal)))
206 (let ((elt (car ret))
209 (if (or (assq 'atom elt)
210 (assq 'quoted-string elt))
211 (cons (cons 'word elt) rest)
214 (defun rfc822/parse-word-or-comment (lal)
215 (let ((ret (rfc822/parse-token-or-comment lal)))
217 (let ((elt (car ret))
220 (cond ((or (assq 'atom elt)
221 (assq 'quoted-string elt))
222 (cons (cons 'word elt) rest)
225 (cons (cons 'comment-word elt) rest)
229 (defun rfc822/parse-phrase (lal)
231 (while (setq ret (rfc822/parse-word-or-comment lal))
232 (setq phrase (append phrase (cdr (car ret))))
236 (cons (cons 'phrase phrase) lal)
239 (defun rfc822/parse-local-part (lal)
240 (let ((ret (rfc822/parse-word lal)))
242 (let ((local-part (cdr (car ret))) dot)
244 (while (and (setq ret (rfc822/parse-ascii-token lal))
246 (string-equal (cdr (assq 'specials dot)) ".")
247 (setq ret (rfc822/parse-word (cdr ret)))
249 (append local-part dot (cdr (car ret)))
253 (cons (cons 'local-part local-part) lal)
256 (defun rfc822/parse-sub-domain (lal)
257 (let ((ret (rfc822/parse-ascii-token lal)))
259 (let ((sub-domain (car ret)))
260 (if (or (assq 'atom sub-domain)
261 (assq 'domain-literal sub-domain)
263 (cons (cons 'sub-domain sub-domain)
268 (defun rfc822/parse-domain (lal)
269 (let ((ret (rfc822/parse-sub-domain lal)))
271 (let ((domain (cdr (car ret))) dot)
273 (while (and (setq ret (rfc822/parse-ascii-token lal))
275 (string-equal (cdr (assq 'specials dot)) ".")
276 (setq ret (rfc822/parse-sub-domain (cdr ret)))
278 (append domain dot (cdr (car ret)))
282 (cons (cons 'domain domain) lal)
285 (defun rfc822/parse-at-domain (lal)
286 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
288 (setq at-sign (car ret))
289 (string-equal (cdr (assq 'specials at-sign)) "@")
290 (setq ret (rfc822/parse-domain (cdr ret)))
292 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
296 (defun rfc822/parse-addr-spec (lal)
297 (let ((ret (rfc822/parse-local-part lal))
301 (setq addr (cdr (car ret)))
303 (and (setq ret (rfc822/parse-at-domain lal))
304 (setq addr (append addr (cdr (car ret))))
307 (cons (cons 'addr-spec addr) lal)
310 (defun rfc822/parse-route (lal)
311 (let ((ret (rfc822/parse-at-domain lal))
315 (setq route (cdr (car ret)))
317 (while (and (setq ret (rfc822/parse-ascii-token lal))
318 (setq comma (car ret))
319 (string-equal (cdr (assq 'specials comma)) ",")
320 (setq ret (rfc822/parse-at-domain (cdr ret)))
322 (setq route (append route comma (cdr (car ret))))
325 (and (setq ret (rfc822/parse-ascii-token lal))
326 (setq colon (car ret))
327 (string-equal (cdr (assq 'specials colon)) ":")
328 (setq route (append route colon))
331 (cons (cons 'route route)
336 (defun rfc822/parse-route-addr (lal)
337 (let ((ret (rfc822/parse-ascii-token lal))
341 (string-equal (cdr (assq 'specials <)) "<")
343 (progn (and (setq ret (rfc822/parse-route lal))
344 (setq route (cdr (car ret)))
347 (setq ret (rfc822/parse-addr-spec lal))
349 (setq addr-spec (cdr (car ret)))
351 (setq ret (rfc822/parse-ascii-token lal))
353 (string-equal (cdr (assq 'specials >)) ">")
355 (cons (cons 'route-addr (append route addr-spec))
360 (defun rfc822/parse-phrase-route-addr (lal)
361 (let ((ret (rfc822/parse-phrase lal)) phrase)
364 (setq phrase (cdr (car ret)))
367 (if (setq ret (rfc822/parse-route-addr lal))
368 (cons (list 'phrase-route-addr
374 (defun rfc822/parse-mailbox (lal)
375 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
376 (rfc822/parse-addr-spec lal)))
380 (setq mbox (car ret))
382 (if (and (setq ret (rfc822/parse-token-or-comment lal))
383 (setq comment (cdr (assq 'comment (car ret))))
387 (cons (list 'mailbox mbox comment)
391 (defun rfc822/parse-group (lal)
392 (let ((ret (rfc822/parse-phrase lal))
393 phrase colon comma mbox semicolon)
395 (setq phrase (cdr (car ret)))
397 (setq ret (rfc822/parse-ascii-token lal))
398 (setq colon (car ret))
399 (string-equal (cdr (assq 'specials colon)) ":")
402 (and (setq ret (rfc822/parse-mailbox lal))
403 (setq mbox (list (car ret)))
406 (while (and (setq ret (rfc822/parse-ascii-token lal))
407 (setq comma (car ret))
409 (cdr (assq 'specials comma)) ",")
411 (setq ret (rfc822/parse-mailbox lal))
412 (setq mbox (cons (car ret) mbox))
416 (and (setq ret (rfc822/parse-ascii-token lal))
417 (setq semicolon (car ret))
418 (string-equal (cdr (assq 'specials semicolon)) ";")
420 (cons (list 'group phrase (nreverse mbox))
425 (defun rfc822/parse-address (lal)
426 (or (rfc822/parse-group lal)
427 (rfc822/parse-mailbox lal)
430 (defun rfc822/parse-addresses (lal)
431 (let ((ret (rfc822/parse-address lal)))
433 (let ((dest (list (car ret))))
435 (while (and (setq ret (rfc822/parse-ascii-token lal))
436 (string-equal (cdr (assq 'specials (car ret))) ",")
437 (setq ret (rfc822/parse-address (cdr ret)))
439 (setq dest (cons (car ret) dest))
445 (defun rfc822/addr-to-string (seq)
448 (if (eq (car token) 'spaces)
455 (defun rfc822/address-string (address)
456 (cond ((eq (car address) 'group)
457 (mapconcat (function rfc822/address-string)
461 ((eq (car address) 'mailbox)
462 (let ((addr (nth 1 address)))
463 (rfc822/addr-to-string
464 (if (eq (car addr) 'phrase-route-addr)
470 (defun rfc822/full-name-string (address)
471 (cond ((eq (car address) 'group)
478 ((eq (car address) 'mailbox)
479 (let ((addr (nth 1 address))
480 (comment (nth 2 address))
482 (if (eq (car addr) 'phrase-route-addr)
483 (setq phrase (mapconcat (function
492 (defun rfc822/extract-address-components (str)
493 "Extract full name and canonical address from STR.
494 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
495 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
496 (let* ((structure (car
497 (rfc822/parse-address
498 (rfc822/lexical-analyze str)
500 (phrase (rfc822/full-name-string structure))
501 (address (rfc822/address-string structure))
503 (list phrase address)
512 ;;; tl-822.el ends here