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.55 1996-08-28 18:15:17 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 (defalias 'rfc822/lexical-analyze 'std11-lexical-analyze)
126 (defun rfc822/ignored-token-p (token)
127 (let ((type (car token)))
128 (or (eq type 'spaces)(eq type 'comment))
131 (defun rfc822/parse-token (lal)
135 (setq token (car lal))
136 (rfc822/ignored-token-p token)
139 (setq itl (cons token itl))
141 (cons (nreverse (cons token itl))
145 (defun rfc822/parse-ascii-token (lal)
146 (let (token itl parsed token-value)
148 (setq token (car lal))
149 (if (and (setq token-value (cdr token))
150 (find-charset-string token-value)
153 (rfc822/ignored-token-p token)
156 (setq itl (cons token itl))
159 (setq parsed (nreverse (cons token itl)))
161 (cons parsed (cdr lal))
164 (defun rfc822/parse-token-or-comment (lal)
168 (setq token (car lal))
169 (eq (car token) 'spaces)
172 (setq itl (cons token itl))
174 (cons (nreverse (cons token itl))
178 (defun rfc822/parse-word (lal)
179 (let ((ret (rfc822/parse-ascii-token lal)))
181 (let ((elt (car ret))
184 (if (or (assq 'atom elt)
185 (assq 'quoted-string elt))
186 (cons (cons 'word elt) rest)
189 (defun rfc822/parse-word-or-comment (lal)
190 (let ((ret (rfc822/parse-token-or-comment lal)))
192 (let ((elt (car ret))
195 (cond ((or (assq 'atom elt)
196 (assq 'quoted-string elt))
197 (cons (cons 'word elt) rest)
200 (cons (cons 'comment-word elt) rest)
204 (defun rfc822/parse-phrase (lal)
206 (while (setq ret (rfc822/parse-word-or-comment lal))
207 (setq phrase (append phrase (cdr (car ret))))
211 (cons (cons 'phrase phrase) lal)
214 (defun rfc822/parse-local-part (lal)
215 (let ((ret (rfc822/parse-word lal)))
217 (let ((local-part (cdr (car ret))) dot)
219 (while (and (setq ret (rfc822/parse-ascii-token lal))
221 (string-equal (cdr (assq 'specials dot)) ".")
222 (setq ret (rfc822/parse-word (cdr ret)))
224 (append local-part dot (cdr (car ret)))
228 (cons (cons 'local-part local-part) lal)
231 (defun rfc822/parse-sub-domain (lal)
232 (let ((ret (rfc822/parse-ascii-token lal)))
234 (let ((sub-domain (car ret)))
235 (if (or (assq 'atom sub-domain)
236 (assq 'domain-literal sub-domain)
238 (cons (cons 'sub-domain sub-domain)
243 (defun rfc822/parse-domain (lal)
244 (let ((ret (rfc822/parse-sub-domain lal)))
246 (let ((domain (cdr (car ret))) dot)
248 (while (and (setq ret (rfc822/parse-ascii-token lal))
250 (string-equal (cdr (assq 'specials dot)) ".")
251 (setq ret (rfc822/parse-sub-domain (cdr ret)))
253 (append domain dot (cdr (car ret)))
257 (cons (cons 'domain domain) lal)
260 (defun rfc822/parse-at-domain (lal)
261 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
263 (setq at-sign (car ret))
264 (string-equal (cdr (assq 'specials at-sign)) "@")
265 (setq ret (rfc822/parse-domain (cdr ret)))
267 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
271 (defun rfc822/parse-addr-spec (lal)
272 (let ((ret (rfc822/parse-local-part lal))
276 (setq addr (cdr (car ret)))
278 (and (setq ret (rfc822/parse-at-domain lal))
279 (setq addr (append addr (cdr (car ret))))
282 (cons (cons 'addr-spec addr) lal)
285 (defun rfc822/parse-route (lal)
286 (let ((ret (rfc822/parse-at-domain lal))
290 (setq route (cdr (car ret)))
292 (while (and (setq ret (rfc822/parse-ascii-token lal))
293 (setq comma (car ret))
294 (string-equal (cdr (assq 'specials comma)) ",")
295 (setq ret (rfc822/parse-at-domain (cdr ret)))
297 (setq route (append route comma (cdr (car ret))))
300 (and (setq ret (rfc822/parse-ascii-token lal))
301 (setq colon (car ret))
302 (string-equal (cdr (assq 'specials colon)) ":")
303 (setq route (append route colon))
306 (cons (cons 'route route)
311 (defun rfc822/parse-route-addr (lal)
312 (let ((ret (rfc822/parse-ascii-token lal))
316 (string-equal (cdr (assq 'specials <)) "<")
318 (progn (and (setq ret (rfc822/parse-route lal))
319 (setq route (cdr (car ret)))
322 (setq ret (rfc822/parse-addr-spec lal))
324 (setq addr-spec (cdr (car ret)))
326 (setq ret (rfc822/parse-ascii-token lal))
328 (string-equal (cdr (assq 'specials >)) ">")
330 (cons (cons 'route-addr (append route addr-spec))
335 (defun rfc822/parse-phrase-route-addr (lal)
336 (let ((ret (rfc822/parse-phrase lal)) phrase)
339 (setq phrase (cdr (car ret)))
342 (if (setq ret (rfc822/parse-route-addr lal))
343 (cons (list 'phrase-route-addr
349 (defun rfc822/parse-mailbox (lal)
350 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
351 (rfc822/parse-addr-spec lal)))
355 (setq mbox (car ret))
357 (if (and (setq ret (rfc822/parse-token-or-comment lal))
358 (setq comment (cdr (assq 'comment (car ret))))
362 (cons (list 'mailbox mbox comment)
366 (defun rfc822/parse-group (lal)
367 (let ((ret (rfc822/parse-phrase lal))
368 phrase colon comma mbox semicolon)
370 (setq phrase (cdr (car ret)))
372 (setq ret (rfc822/parse-ascii-token lal))
373 (setq colon (car ret))
374 (string-equal (cdr (assq 'specials colon)) ":")
377 (and (setq ret (rfc822/parse-mailbox lal))
378 (setq mbox (list (car ret)))
381 (while (and (setq ret (rfc822/parse-ascii-token lal))
382 (setq comma (car ret))
384 (cdr (assq 'specials comma)) ",")
386 (setq ret (rfc822/parse-mailbox lal))
387 (setq mbox (cons (car ret) mbox))
391 (and (setq ret (rfc822/parse-ascii-token lal))
392 (setq semicolon (car ret))
393 (string-equal (cdr (assq 'specials semicolon)) ";")
395 (cons (list 'group phrase (nreverse mbox))
400 (defun rfc822/parse-address (lal)
401 (or (rfc822/parse-group lal)
402 (rfc822/parse-mailbox lal)
405 (defun rfc822/parse-addresses (lal)
406 (let ((ret (rfc822/parse-address lal)))
408 (let ((dest (list (car ret))))
410 (while (and (setq ret (rfc822/parse-ascii-token lal))
411 (string-equal (cdr (assq 'specials (car ret))) ",")
412 (setq ret (rfc822/parse-address (cdr ret)))
414 (setq dest (cons (car ret) dest))
420 (defun rfc822/addr-to-string (seq)
423 (if (eq (car token) 'spaces)
430 (defun rfc822/address-string (address)
431 (cond ((eq (car address) 'group)
432 (mapconcat (function rfc822/address-string)
436 ((eq (car address) 'mailbox)
437 (let ((addr (nth 1 address)))
438 (rfc822/addr-to-string
439 (if (eq (car addr) 'phrase-route-addr)
445 (defun rfc822/full-name-string (address)
446 (cond ((eq (car address) 'group)
453 ((eq (car address) 'mailbox)
454 (let ((addr (nth 1 address))
455 (comment (nth 2 address))
457 (if (eq (car addr) 'phrase-route-addr)
458 (setq phrase (mapconcat (function
467 (defun rfc822/extract-address-components (str)
468 "Extract full name and canonical address from STR.
469 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
470 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
471 (let* ((structure (car
472 (rfc822/parse-address
473 (rfc822/lexical-analyze str)
475 (phrase (rfc822/full-name-string structure))
476 (address (rfc822/address-string structure))
478 (list phrase address)
487 ;;; tl-822.el ends here