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.46 1996-08-28 15:26:10 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)
48 (defalias `rfc822/field-end 'std11-field-end)
49 (defalias 'rfc822/get-field-body 'std11-find-field-body)
50 (defalias 'rfc822/get-field-names 'std11-field-names)
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/special-chars "][()<>@,;:\\<>.\"")
121 (defconst rfc822/space-chars " \t\n")
122 (defconst rfc822/non-atom-chars
123 (concat rfc822/special-chars rfc822/space-chars))
124 (defconst rfc822/non-dtext-chars "][")
125 (defconst rfc822/non-ctext-chars "()")
127 (defun rfc822/analyze-spaces (str)
128 (let ((i (string-match (concat "[^" rfc822/space-chars "]") str)))
131 (cons (cons 'spaces (substring str 0 i))
134 (if (not (string-equal str ""))
135 (cons (cons 'spaces str) "")
138 (defun rfc822/analyze-special (str)
139 (if (and (> (length str) 0)
140 (find (elt str 0) rfc822/special-chars)
142 (cons (cons 'specials (substring str 0 1))
147 (defun rfc822/analyze-atom (str)
148 (let ((i (string-match (concat "[" rfc822/non-atom-chars "]") str)))
151 (cons (cons 'atom (substring str 0 i))
154 (if (not (string-equal str ""))
155 (cons (cons 'spaces str) "")
158 (defun rfc822/analyze-quoted-string (str)
159 (let ((len (length str)))
163 (let ((i 1) chr dest)
166 (setq chr (aref str i))
172 (setq dest (concat dest (char-to-string (aref str i))))
176 (cons (cons 'quoted-string dest)
177 (substring str (1+ i)))
181 (setq dest (concat dest (char-to-string (aref str i))))
186 (defun rfc822/analyze-domain-literal (str)
187 (if (and (> (length str) 0)
188 (eq (aref str 0) ?\[)
190 (let* ((i (string-match (concat "[" rfc822/non-dtext-chars "]") str 1))
191 (rest (and i (substring str i)))
195 (eq (aref rest 0) ?\])
197 (cons (cons 'domain-literal (substring str 1 i))
202 (defun rfc822/analyze-comment (str)
203 (if (and (> (length str) 0)
208 (setq str (substring str 1))
210 (while (not (string-equal str ""))
211 (setq p (string-match (concat "[" rfc822/non-ctext-chars "]") str))
213 (setq dest (concat dest (substring str 0 p)))
214 (setq str (substring str p))
216 ((setq ret (rfc822/analyze-comment str))
217 (setq dest (concat dest "(" (cdr (car ret)) ")"))
223 (if (and (> (length str) 0)
226 (cons (cons 'comment dest)
231 (defun rfc822/lexical-analyze (str)
233 (while (not (string-equal str ""))
235 (or (rfc822/analyze-quoted-string str)
236 (rfc822/analyze-domain-literal str)
237 (rfc822/analyze-comment str)
238 (rfc822/analyze-spaces str)
239 (rfc822/analyze-special str)
240 (rfc822/analyze-atom str)
243 (setq dest (cons (car ret) dest))
253 (defun rfc822/ignored-token-p (token)
254 (let ((type (car token)))
255 (or (eq type 'spaces)(eq type 'comment))
258 (defun rfc822/parse-token (lal)
262 (setq token (car lal))
263 (rfc822/ignored-token-p token)
266 (setq itl (cons token itl))
268 (cons (nreverse (cons token itl))
272 (defun rfc822/parse-ascii-token (lal)
273 (let (token itl parsed token-value)
275 (setq token (car lal))
276 (if (and (setq token-value (cdr token))
277 (find-charset-string token-value)
280 (rfc822/ignored-token-p token)
283 (setq itl (cons token itl))
286 (setq parsed (nreverse (cons token itl)))
288 (cons parsed (cdr lal))
291 (defun rfc822/parse-token-or-comment (lal)
295 (setq token (car lal))
296 (eq (car token) 'spaces)
299 (setq itl (cons token itl))
301 (cons (nreverse (cons token itl))
305 (defun rfc822/parse-word (lal)
306 (let ((ret (rfc822/parse-ascii-token lal)))
308 (let ((elt (car ret))
311 (if (or (assq 'atom elt)
312 (assq 'quoted-string elt))
313 (cons (cons 'word elt) rest)
316 (defun rfc822/parse-word-or-comment (lal)
317 (let ((ret (rfc822/parse-token-or-comment lal)))
319 (let ((elt (car ret))
322 (cond ((or (assq 'atom elt)
323 (assq 'quoted-string elt))
324 (cons (cons 'word elt) rest)
327 (cons (cons 'comment-word elt) rest)
331 (defun rfc822/parse-phrase (lal)
333 (while (setq ret (rfc822/parse-word-or-comment lal))
334 (setq phrase (append phrase (cdr (car ret))))
338 (cons (cons 'phrase phrase) lal)
341 (defun rfc822/parse-local-part (lal)
342 (let ((ret (rfc822/parse-word lal)))
344 (let ((local-part (cdr (car ret))) dot)
346 (while (and (setq ret (rfc822/parse-ascii-token lal))
348 (string-equal (cdr (assq 'specials dot)) ".")
349 (setq ret (rfc822/parse-word (cdr ret)))
351 (append local-part dot (cdr (car ret)))
355 (cons (cons 'local-part local-part) lal)
358 (defun rfc822/parse-sub-domain (lal)
359 (let ((ret (rfc822/parse-ascii-token lal)))
361 (let ((sub-domain (car ret)))
362 (if (or (assq 'atom sub-domain)
363 (assq 'domain-literal sub-domain)
365 (cons (cons 'sub-domain sub-domain)
370 (defun rfc822/parse-domain (lal)
371 (let ((ret (rfc822/parse-sub-domain lal)))
373 (let ((domain (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-sub-domain (cdr ret)))
380 (append domain dot (cdr (car ret)))
384 (cons (cons 'domain domain) lal)
387 (defun rfc822/parse-at-domain (lal)
388 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
390 (setq at-sign (car ret))
391 (string-equal (cdr (assq 'specials at-sign)) "@")
392 (setq ret (rfc822/parse-domain (cdr ret)))
394 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
398 (defun rfc822/parse-addr-spec (lal)
399 (let ((ret (rfc822/parse-local-part lal))
403 (setq addr (cdr (car ret)))
405 (and (setq ret (rfc822/parse-at-domain lal))
406 (setq addr (append addr (cdr (car ret))))
409 (cons (cons 'addr-spec addr) lal)
412 (defun rfc822/parse-route (lal)
413 (let ((ret (rfc822/parse-at-domain lal))
417 (setq route (cdr (car ret)))
419 (while (and (setq ret (rfc822/parse-ascii-token lal))
420 (setq comma (car ret))
421 (string-equal (cdr (assq 'specials comma)) ",")
422 (setq ret (rfc822/parse-at-domain (cdr ret)))
424 (setq route (append route comma (cdr (car ret))))
427 (and (setq ret (rfc822/parse-ascii-token lal))
428 (setq colon (car ret))
429 (string-equal (cdr (assq 'specials colon)) ":")
430 (setq route (append route colon))
433 (cons (cons 'route route)
438 (defun rfc822/parse-route-addr (lal)
439 (let ((ret (rfc822/parse-ascii-token lal))
443 (string-equal (cdr (assq 'specials <)) "<")
445 (progn (and (setq ret (rfc822/parse-route lal))
446 (setq route (cdr (car ret)))
449 (setq ret (rfc822/parse-addr-spec lal))
451 (setq addr-spec (cdr (car ret)))
453 (setq ret (rfc822/parse-ascii-token lal))
455 (string-equal (cdr (assq 'specials >)) ">")
457 (cons (cons 'route-addr (append route addr-spec))
462 (defun rfc822/parse-phrase-route-addr (lal)
463 (let ((ret (rfc822/parse-phrase lal)) phrase)
466 (setq phrase (cdr (car ret)))
469 (if (setq ret (rfc822/parse-route-addr lal))
470 (cons (list 'phrase-route-addr
476 (defun rfc822/parse-mailbox (lal)
477 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
478 (rfc822/parse-addr-spec lal)))
482 (setq mbox (car ret))
484 (if (and (setq ret (rfc822/parse-token-or-comment lal))
485 (setq comment (cdr (assq 'comment (car ret))))
489 (cons (list 'mailbox mbox comment)
493 (defun rfc822/parse-group (lal)
494 (let ((ret (rfc822/parse-phrase lal))
495 phrase colon comma mbox semicolon)
497 (setq phrase (cdr (car ret)))
499 (setq ret (rfc822/parse-ascii-token lal))
500 (setq colon (car ret))
501 (string-equal (cdr (assq 'specials colon)) ":")
504 (and (setq ret (rfc822/parse-mailbox lal))
505 (setq mbox (list (car ret)))
508 (while (and (setq ret (rfc822/parse-ascii-token lal))
509 (setq comma (car ret))
511 (cdr (assq 'specials comma)) ",")
513 (setq ret (rfc822/parse-mailbox lal))
514 (setq mbox (cons (car ret) mbox))
518 (and (setq ret (rfc822/parse-ascii-token lal))
519 (setq semicolon (car ret))
520 (string-equal (cdr (assq 'specials semicolon)) ";")
522 (cons (list 'group phrase (nreverse mbox))
527 (defun rfc822/parse-address (lal)
528 (or (rfc822/parse-group lal)
529 (rfc822/parse-mailbox lal)
532 (defun rfc822/parse-addresses (lal)
533 (let ((ret (rfc822/parse-address lal)))
535 (let ((dest (list (car ret))))
537 (while (and (setq ret (rfc822/parse-ascii-token lal))
538 (string-equal (cdr (assq 'specials (car ret))) ",")
539 (setq ret (rfc822/parse-address (cdr ret)))
541 (setq dest (cons (car ret) dest))
547 (defun rfc822/addr-to-string (seq)
550 (if (eq (car token) 'spaces)
557 (defun rfc822/address-string (address)
558 (cond ((eq (car address) 'group)
559 (mapconcat (function rfc822/address-string)
563 ((eq (car address) 'mailbox)
564 (let ((addr (nth 1 address)))
565 (rfc822/addr-to-string
566 (if (eq (car addr) 'phrase-route-addr)
572 (defun rfc822/full-name-string (address)
573 (cond ((eq (car address) 'group)
580 ((eq (car address) 'mailbox)
581 (let ((addr (nth 1 address))
582 (comment (nth 2 address))
584 (if (eq (car addr) 'phrase-route-addr)
585 (setq phrase (mapconcat (function
594 (defun rfc822/extract-address-components (str)
595 "Extract full name and canonical address from STR.
596 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
597 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
598 (let* ((structure (car
599 (rfc822/parse-address
600 (rfc822/lexical-analyze str)
602 (phrase (rfc822/full-name-string structure))
603 (address (rfc822/address-string structure))
605 (list phrase address)
614 ;;; tl-822.el ends here