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>
8 ;;; Keywords: mail, news, RFC 822
10 ;;; This file is part of tm (Tools for MIME).
17 (defconst rfc822/RCS-ID
18 "$Id: tl-822.el,v 7.1 1995-12-19 17:57:08 morioka Exp $")
19 (defconst rfc822/version (get-version-string rfc822/RCS-ID))
25 (defconst rfc822/field-name-regexp "[!-9;-~]+")
27 (defconst rfc822/field-top-regexp
28 (concat "\\(" rfc822/field-name-regexp "\\):"))
30 (defconst rfc822::next-field-top-regexp (concat "\n" rfc822/field-top-regexp))
32 (defun rfc822/field-end ()
33 (if (re-search-forward rfc822::next-field-top-regexp nil t)
34 (goto-char (match-beginning 0))
35 (if (re-search-forward "^$" nil t)
36 (goto-char (1- (match-beginning 0)))
42 (defun rfc822/get-field-body (name)
43 (let ((case-fold-search t))
47 (goto-char (point-min))
48 (or (and (re-search-forward "^$" nil t) (match-end 0))
51 (goto-char (point-min))
52 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
53 (buffer-substring-no-properties
63 (defun rfc822/get-header-string-except (pat boundary)
64 (let ((case-fold-search t))
67 (narrow-to-region (goto-char (point-min))
70 (concat "^\\(" (regexp-quote boundary) "\\)?$")
74 (goto-char (point-min))
76 (while (re-search-forward rfc822/field-top-regexp nil t)
77 (setq field (buffer-substring (match-beginning 0)
80 (if (not (string-match pat field))
81 (setq header (concat header field "\n"))
90 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
91 (defconst rfc822/quoted-pair-regexp "\\\\.")
92 (defconst rfc822/qtext-regexp "[^\"\\\n\t \t]")
93 (defconst rfc822/quoted-string-regexp
97 "\\(" rfc822/linear-white-space-regexp "?"
98 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
100 rfc822/linear-white-space-regexp "?"
103 (defun rfc822/strip-quoted-pair (str)
109 (setq chr (elt str i))
110 (if (or flag (not (eq chr ?\\)))
112 (setq dest (concat dest (char-to-string chr)))
121 (defun rfc822/strip-quoted-string (str)
122 (rfc822/strip-quoted-pair
123 (let ((max (- (length str) 1))
125 (if (and (eq (elt str 0) ?\")
126 (eq (elt str max) ?\")
128 (substring str 1 max)
136 (defun rfc822/unfolding-string (str)
138 (while (string-match "\n\\s +" str)
139 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
140 (setq str (substring str (match-end 0)))
146 ;;; @ lexical analyze
149 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
150 (defconst rfc822/space-chars " \t\n")
151 (defconst rfc822/non-atom-chars
152 (concat rfc822/special-chars rfc822/space-chars))
153 (defconst rfc822/non-qtext-chars "\"")
154 (defconst rfc822/non-dtext-chars "[]")
155 (defconst rfc822/non-ctext-chars "()")
157 (defun rfc822/analyze-spaces (str)
158 (let ((i (position-mismatched
161 (find elt rfc822/space-chars)
165 (cons (cons 'spaces (substring str 0 i))
170 (defun rfc822/analyze-special (str)
171 (if (and (> (length str) 0)
172 (find (elt str 0) rfc822/special-chars)
174 (cons (cons 'specials (substring str 0 1))
179 (defun rfc822/analyze-atom (str)
180 (let ((i (position-mismatched
183 (not (find elt rfc822/non-atom-chars))
187 (cons (cons 'atom (substring str 0 i))
192 (defun rfc822/analyze-quoted-pair (str)
193 (if (and (>= (length str) 2)
196 (cons (cons 'quoted-pair (substring str 0 2))
201 (defun rfc822/analyze-quoted-string (str)
202 (if (and (> (length str) 0)
205 (let* ((i (position-mismatched
208 (not (find elt rfc822/non-qtext-chars))
210 (setq str (substring str 1))
212 (rest (substring str i))
216 (eq (elt rest 0) ?\")
218 (cons (cons 'quoted-string (substring str 0 i))
223 (defun rfc822/analyze-domain-literal (str)
224 (if (and (> (length str) 0)
227 (let* ((i (position-mismatched
230 (not (find elt rfc822/non-dtext-chars))
232 (setq str (substring str 1))
234 (rest (substring str i))
238 (eq (elt rest 0) ?\])
240 (cons (cons 'domain-literal (substring str 0 i))
245 (defun rfc822/analyze-comment (str)
246 (if (and (> (length str) 0)
251 (setq str (substring str 1))
253 (while (not (string-equal str ""))
254 (setq p (position-mismatched
257 (not (find elt rfc822/non-ctext-chars))
260 (setq dest (concat dest (substring str 0 p)))
261 (setq str (substring str p))
263 ((setq ret (rfc822/analyze-comment str))
264 (setq dest (concat dest "(" (cdr (car ret)) ")"))
270 (if (and (> (length str) 0)
273 (cons (cons 'comment dest)
278 (defun rfc822/lexical-analyze (str)
280 (i 0)(len (length str))
282 (while (not (string-equal str ""))
284 (or (rfc822/analyze-quoted-string str)
285 (rfc822/analyze-domain-literal str)
286 (rfc822/analyze-comment str)
287 (rfc822/analyze-spaces str)
288 (rfc822/analyze-special str)
289 (rfc822/analyze-atom str)
292 (setq dest (cons (car ret) dest))
302 (defun rfc822/ignored-token-p (token)
303 (let ((type (car token)))
304 (or (eq type 'spaces)(eq type 'comment))
307 (defun rfc822/parse-token (lal)
311 (setq token (car lal))
312 (rfc822/ignored-token-p token)
315 (setq itl (cons token itl))
317 (cons (reverse (cons token itl))
321 (defun rfc822/parse-token-or-comment (lal)
325 (setq token (car lal))
326 (eq (car token) 'spaces)
329 (setq itl (cons token itl))
331 (cons (reverse (cons token itl))
335 (defun rfc822/parse-word (lal)
336 (let ((ret (rfc822/parse-token lal)))
338 (let ((elt (car ret))
341 (if (or (assq 'atom elt)
342 (assq 'quoted-string elt))
343 (cons (cons 'word elt) rest)
346 (defun rfc822/parse-word-or-comment (lal)
347 (let ((ret (rfc822/parse-token-or-comment lal)))
349 (let ((elt (car ret))
352 (cond ((or (assq 'atom elt)
353 (assq 'quoted-string elt))
354 (cons (cons 'word elt) rest)
357 (cons (cons 'comment-word elt) rest)
361 (defun rfc822/parse-phrase (lal)
363 (while (setq ret (rfc822/parse-word-or-comment lal))
364 (setq phrase (append phrase (cdr (car ret))))
368 (cons (cons 'phrase phrase) lal)
371 (defun rfc822/parse-local-part (lal)
372 (let ((ret (rfc822/parse-word lal)))
374 (let ((local-part (cdr (car ret))) dot)
376 (while (and (setq ret (rfc822/parse-token lal))
378 (equal (cdr (assq 'specials dot)) ".")
379 (setq ret (rfc822/parse-word (cdr ret)))
381 (append local-part dot (cdr (car ret)))
385 (cons (cons 'local-part local-part) lal)
388 (defun rfc822/parse-sub-domain (lal)
389 (let ((ret (rfc822/parse-token lal)))
391 (let ((sub-domain (car ret)))
392 (if (or (assq 'atom sub-domain)
393 (assq 'domain-literal sub-domain)
395 (cons (cons 'sub-domain sub-domain)
400 (defun rfc822/parse-domain (lal)
401 (let ((ret (rfc822/parse-sub-domain lal)))
403 (let ((domain (cdr (car ret))) dot)
405 (while (and (setq ret (rfc822/parse-token lal))
407 (equal (cdr (assq 'specials dot)) ".")
408 (setq ret (rfc822/parse-sub-domain (cdr ret)))
410 (append domain dot (cdr (car ret)))
414 (cons (cons 'domain domain) lal)
417 (defun rfc822/parse-at-domain (lal)
418 (let ((ret (rfc822/parse-token lal)) at-sign)
420 (setq at-sign (car ret))
421 (equal (cdr (assq 'specials at-sign)) "@")
422 (setq ret (rfc822/parse-domain (cdr ret)))
424 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
428 (defun rfc822/parse-addr-spec (lal)
429 (let ((ret (rfc822/parse-local-part lal))
433 (setq addr (cdr (car ret)))
435 (and (setq ret (rfc822/parse-at-domain lal))
436 (setq addr (append addr (cdr (car ret))))
439 (cons (cons 'addr-spec addr) lal)
442 (defun rfc822/parse-route (lal)
443 (let ((ret (rfc822/parse-at-domain lal))
447 (setq route (cdr (car ret)))
449 (while (and (setq ret (rfc822/parse-token lal))
450 (setq comma (car ret))
451 (equal (cdr (assq 'specials comma)) ",")
452 (setq ret (rfc822/parse-at-domain (cdr ret)))
454 (setq route (append route comma (cdr (car ret))))
457 (and (setq ret (rfc822/parse-token lal))
458 (setq colon (car ret))
459 (equal (cdr (assq 'specials colon)) ":")
460 (setq route (append route colon))
463 (cons (cons 'route route)
468 (defun rfc822/parse-route-addr (lal)
469 (let ((ret (rfc822/parse-token lal))
473 (equal (cdr (assq 'specials <)) "<")
475 (progn (and (setq ret (rfc822/parse-route lal))
476 (setq route (cdr (car ret)))
479 (setq ret (rfc822/parse-addr-spec lal))
481 (setq addr-spec (cdr (car ret)))
483 (setq ret (rfc822/parse-token lal))
485 (equal (cdr (assq 'specials >)) ">")
487 (cons (cons 'route-addr (append route addr-spec))
492 (defun rfc822/parse-phrase-route-addr (lal)
493 (let ((ret (rfc822/parse-phrase lal)) phrase)
496 (setq phrase (cdr (car ret)))
499 (if (setq ret (rfc822/parse-route-addr lal))
500 (cons (list 'phrase-route-addr
506 (defun rfc822/parse-mailbox (lal)
507 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
508 (rfc822/parse-addr-spec lal)))
512 (setq mbox (car ret))
514 (if (and (setq ret (rfc822/parse-token-or-comment lal))
515 (setq comment (cdr (assq 'comment (car ret))))
519 (cons (list 'mailbox mbox comment)
523 (defun rfc822/parse-group (lal)
524 (let ((ret (rfc822/parse-phrase lal))
525 phrase colon comma mbox semicolon)
527 (setq phrase (cdr (car ret)))
529 (setq ret (rfc822/parse-token lal))
530 (setq colon (car ret))
531 (equal (cdr (assq 'specials colon)) ":")
534 (and (setq ret (rfc822/parse-mailbox lal))
535 (setq mbox (list (car ret)))
538 (while (and (setq ret (rfc822/parse-token lal))
539 (setq comma (car ret))
540 (equal (cdr (assq 'specials comma)) ",")
542 (setq ret (rfc822/parse-mailbox lal))
543 (setq mbox (cons (car ret) mbox))
547 (and (setq ret (rfc822/parse-token lal))
548 (setq semicolon (car ret))
549 (equal (cdr (assq 'specials semicolon)) ";")
551 (cons (list 'group phrase (reverse mbox))
556 (defun rfc822/parse-address (lal)
557 (or (rfc822/parse-group lal)
558 (rfc822/parse-mailbox lal)
561 (defun rfc822/parse-addresses (lal)
562 (let ((ret (rfc822/parse-address lal)))
564 (let ((dest (list (car ret))))
566 (while (and (setq ret (rfc822/parse-token lal))
567 (equal (cdr (assq 'specials (car ret))) ",")
568 (setq ret (rfc822/parse-address (cdr ret)))
570 (setq dest (cons (car ret) dest))
576 (defun rfc822/addr-to-string (seq)
579 (if (eq (car token) 'spaces)
586 (defun rfc822/address-string (address)
587 (if (eq (car address) 'mailbox)
588 (let ((addr (nth 1 address))
590 (rfc822/addr-to-string
591 (if (eq (car addr) 'phrase-route-addr)
597 (defun rfc822/full-name-string (address)
598 (if (eq (car address) 'mailbox)
599 (let ((addr (nth 1 address))
600 (comment (nth 2 address))
602 (if (eq (car addr) 'phrase-route-addr)
603 (setq phrase (mapconcat (function
612 (defun rfc822/extract-address-components (str)
613 "Extract full name and canonical address from STR.
614 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
615 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
616 (let* ((structure (car
617 (rfc822/parse-address
618 (rfc822/lexical-analyze str)
620 (phrase (rfc822/full-name-string structure))
621 (address (rfc822/address-string structure))
623 (list phrase address)