2 ;;; tl-822.el --- RFC 822 parser for GNU Emacs
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1995 MORIOKA Tomohiko
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; $Id: tl-822.el,v 1.1 1995-10-03 05:17:31 morioka Exp $
10 ;;; Keywords: mail, news, RFC 822
12 ;;; This file is part of tm (Tools for MIME).
21 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
22 (defconst rfc822/space-chars " \t\n")
23 (defconst rfc822/non-atom-chars
24 (concat rfc822/special-chars rfc822/space-chars))
25 (defconst rfc822/non-qtext-chars "\"")
26 (defconst rfc822/non-dtext-chars "[]")
27 (defconst rfc822/non-ctext-chars "()")
29 (defun rfc822/analyze-spaces (str)
30 (let ((i (position-mismatched
33 (find elt rfc822/space-chars)
37 (cons (cons 'spaces (substring str 0 i))
42 (defun rfc822/analyze-special (str)
43 (if (and (> (length str) 0)
44 (find (elt str 0) rfc822/special-chars)
46 (cons (cons 'specials (substring str 0 1))
51 (defun rfc822/analyze-atom (str)
52 (let ((i (position-mismatched
55 (not (find elt rfc822/non-atom-chars))
59 (cons (cons 'atom (substring str 0 i))
64 (defun rfc822/analyze-quoted-pair (str)
65 (if (and (>= (length str) 2)
68 (cons (cons 'quoted-pair (substring str 0 2))
73 (defun rfc822/analyze-quoted-string (str)
74 (if (and (> (length str) 0)
77 (let* ((i (position-mismatched
80 (not (find elt rfc822/non-qtext-chars))
82 (setq str (substring str 1))
84 (rest (substring str i))
90 (cons (cons 'quoted-string (substring str 0 i))
95 (defun rfc822/analyze-domain-literal (str)
96 (if (and (> (length str) 0)
99 (let* ((i (position-mismatched
102 (not (find elt rfc822/non-dtext-chars))
104 (setq str (substring str 1))
106 (rest (substring str i))
110 (eq (elt rest 0) ?\])
112 (cons (cons 'domain-literal (substring str 0 i))
117 (defun rfc822/analyze-comment (str)
118 (if (and (> (length str) 0)
123 (setq str (substring str 1))
125 (while (not (string-equal str ""))
126 (setq p (position-mismatched
129 (not (find elt rfc822/non-ctext-chars))
132 (setq dest (concat dest (substring str 0 p)))
133 (setq str (substring str p))
135 ((setq ret (rfc822/analyze-comment str))
136 (setq dest (concat dest "(" (cdr (car ret)) ")"))
142 (if (and (> (length str) 0)
145 (cons (cons 'comment dest)
150 (defun rfc822/lexical-analyze (str)
152 (i 0)(len (length str))
154 (while (not (string-equal str ""))
156 (or (rfc822/analyze-quoted-string str)
157 (rfc822/analyze-domain-literal str)
158 (rfc822/analyze-comment str)
159 (rfc822/analyze-spaces str)
160 (rfc822/analyze-special str)
161 (rfc822/analyze-atom str)
164 (setq dest (cons (car ret) dest))
174 (defun rfc822/ignored-token-p (token)
175 (let ((type (car token)))
176 (or (eq type 'spaces)(eq type 'comment))
179 (defun rfc822/parse-token (lal)
183 (setq token (car lal))
184 (rfc822/ignored-token-p token)
187 (setq itl (cons token itl))
189 (cons (reverse (cons token itl))
193 (defun rfc822/parse-token-or-comment (lal)
197 (setq token (car lal))
198 (eq (car token) 'spaces)
201 (setq itl (cons token itl))
203 (cons (reverse (cons token itl))
207 (defun rfc822/parse-word (lal)
208 (let ((ret (rfc822/parse-token lal)))
210 (let ((elt (car ret))
213 (if (or (assq 'atom elt)
214 (assq 'quoted-string elt))
215 (cons (cons 'word elt) rest)
218 (defun rfc822/parse-word-or-comment (lal)
219 (let ((ret (rfc822/parse-token-or-comment lal)))
221 (let ((elt (car ret))
224 (cond ((or (assq 'atom elt)
225 (assq 'quoted-string elt))
226 (cons (cons 'word elt) rest)
229 (cons (cons 'comment-word elt) rest)
233 (defun rfc822/parse-phrase (lal)
235 (while (setq ret (rfc822/parse-word-or-comment lal))
236 (setq phrase (append phrase (cdr (car ret))))
240 (cons (cons 'phrase phrase) lal)
243 (defun rfc822/parse-local-part (lal)
244 (let ((ret (rfc822/parse-word lal)))
246 (let ((local-part (cdr (car ret))) dot)
248 (while (and (setq ret (rfc822/parse-token lal))
250 (equal (cdr (assq 'specials dot)) ".")
251 (setq ret (rfc822/parse-word (cdr ret)))
253 (append local-part dot (cdr (car ret)))
257 (cons (cons 'local-part local-part) lal)
260 (defun rfc822/parse-sub-domain (lal)
261 (let ((ret (rfc822/parse-token lal)))
263 (let ((sub-domain (car ret)))
264 (if (or (assq 'atom sub-domain)
265 (assq 'domain-literal sub-domain)
267 (cons (cons 'sub-domain sub-domain)
272 (defun rfc822/parse-domain (lal)
273 (let ((ret (rfc822/parse-sub-domain lal)))
275 (let ((domain (cdr (car ret))) dot)
277 (while (and (setq ret (rfc822/parse-token lal))
279 (equal (cdr (assq 'specials dot)) ".")
280 (setq ret (rfc822/parse-sub-domain (cdr ret)))
282 (append domain dot (cdr (car ret)))
286 (cons (cons 'domain domain) lal)
289 (defun rfc822/parse-at-domain (lal)
290 (let ((ret (rfc822/parse-token lal)) at-sign)
292 (setq at-sign (car ret))
293 (equal (cdr (assq 'specials at-sign)) "@")
294 (setq ret (rfc822/parse-domain (cdr ret)))
296 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
300 (defun rfc822/parse-addr-spec (lal)
301 (let ((ret (rfc822/parse-local-part lal))
305 (setq addr (cdr (car ret)))
307 (and (setq ret (rfc822/parse-at-domain lal))
308 (setq addr (append addr (cdr (car ret))))
311 (cons (cons 'addr-spec addr) lal)
314 (defun rfc822/parse-route (lal)
315 (let ((ret (rfc822/parse-at-domain lal))
319 (setq route (cdr (car ret)))
321 (while (and (setq ret (rfc822/parse-token lal))
322 (setq comma (car ret))
323 (equal (cdr (assq 'specials comma)) ",")
324 (setq ret (rfc822/parse-at-domain (cdr ret)))
326 (setq route (append route comma (cdr (car ret))))
329 (and (setq ret (rfc822/parse-token lal))
330 (setq colon (car ret))
331 (equal (cdr (assq 'specials colon)) ":")
332 (setq route (append route colon))
335 (cons (cons 'route route)
340 (defun rfc822/parse-route-addr (lal)
341 (let ((ret (rfc822/parse-token lal))
345 (equal (cdr (assq 'specials <)) "<")
347 (progn (and (setq ret (rfc822/parse-route lal))
348 (setq route (cdr (car ret)))
351 (setq ret (rfc822/parse-addr-spec lal))
353 (setq addr-spec (cdr (car ret)))
355 (setq ret (rfc822/parse-token lal))
357 (equal (cdr (assq 'specials >)) ">")
359 (cons (cons 'route-addr (append route addr-spec))
364 (defun rfc822/parse-phrase-route-addr (lal)
365 (let ((ret (rfc822/parse-phrase lal)) phrase)
368 (setq phrase (cdr (car ret)))
371 (if (setq ret (rfc822/parse-route-addr lal))
372 (cons (list 'phrase-route-addr
378 (defun rfc822/parse-mailbox (lal)
379 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
380 (rfc822/parse-addr-spec lal)))
384 (setq mbox (car ret))
386 (if (and (setq ret (rfc822/parse-token-or-comment lal))
387 (setq comment (cdr (assq 'comment (car ret))))
391 (cons (list 'mailbox mbox comment)
395 (defun rfc822/parse-group (lal)
396 (let ((ret (rfc822/parse-phrase lal))
397 phrase : comma mbox semicolon)
399 (setq phrase (cdr (car ret)))
401 (setq ret (rfc822/parse-token lal))
403 (equal (cdr (assq 'specials :)) ":")
406 (and (setq ret (rfc822/parse-mailbox lal))
407 (setq mbox (list (car ret)))
410 (while (and (setq ret (rfc822/parse-token lal))
411 (setq comma (car ret))
412 (equal (cdr (assq 'specials comma)) ",")
414 (setq ret (rfc822/parse-mailbox lal))
415 (setq mbox (cons (car ret) mbox))
419 (and (setq ret (rfc822/parse-token lal))
420 (setq semicolon (car ret))
421 (equal (cdr (assq 'specials semicolon)) ";")
423 (cons (list 'group phrase (reverse mbox))
428 (defun rfc822/parse-address (lal)
429 (or (rfc822/parse-group lal)
430 (rfc822/parse-mailbox lal)
433 (defun rfc822/parse-addresses (lal)
434 (let ((ret (rfc822/parse-address lal)))
436 (let ((dest (list (car ret))))
438 (while (and (setq ret (rfc822/parse-token lal))
439 (equal (cdr (assq 'specials (car ret))) ",")
440 (setq ret (rfc822/parse-address (cdr ret)))
442 (setq dest (cons (car ret) dest))
448 (defun rfc822/addr-to-string (seq)
451 (if (eq (car token) 'spaces)
458 (defun rfc822/address-string (address)
459 (if (eq (car address) 'mailbox)
460 (let ((addr (nth 1 address))
462 (rfc822/addr-to-string
463 (if (eq (car addr) 'phrase-route-addr)
469 (defun rfc822/full-name-string (address)
470 (if (eq (car address) 'mailbox)
471 (let ((addr (nth 1 address))
472 (comment (nth 2 address))
474 (if (eq (car addr) 'phrase-route-addr)
475 (setq phrase (mapconcat (function