2 ;;; tl-822.el --- RFC 822 parser for GNU Emacs
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1995,1996 MORIOKA Tomohiko
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Keywords: mail, news, RFC 822
10 ;;; This file is part of tl (Tiny Library).
12 ;;; This program is free software; you can redistribute it and/or
13 ;;; modify it under the terms of the GNU General Public License as
14 ;;; published by the Free Software Foundation; either version 2, or
15 ;;; (at your option) any later version.
17 ;;; This program is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;;; General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with This program. If not, write to the Free Software
24 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 (defconst rfc822/RCS-ID
33 "$Id: tl-822.el,v 7.6 1996-03-25 10:21:25 morioka Exp $")
34 (defconst rfc822/version (get-version-string rfc822/RCS-ID))
40 (defconst rfc822/field-name-regexp "[!-9;-~]+")
42 (defconst rfc822/field-top-regexp
43 (concat "\\(" rfc822/field-name-regexp "\\):"))
45 (defconst rfc822::next-field-top-regexp (concat "\n" rfc822/field-top-regexp))
47 (defun rfc822/field-end ()
48 (if (re-search-forward rfc822::next-field-top-regexp nil t)
49 (goto-char (match-beginning 0))
50 (if (re-search-forward "^$" nil t)
51 (goto-char (1- (match-beginning 0)))
57 (defun rfc822/get-field-body (name)
58 (let ((case-fold-search t))
62 (goto-char (point-min))
63 (or (and (re-search-forward "^$" nil t) (match-end 0))
66 (goto-char (point-min))
67 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
68 (buffer-substring-no-properties
78 (defun rfc822/get-header-string-except (pat boundary)
79 (let ((case-fold-search t))
82 (narrow-to-region (goto-char (point-min))
85 (concat "^\\(" (regexp-quote boundary) "\\)?$")
89 (goto-char (point-min))
91 (while (re-search-forward rfc822/field-top-regexp nil t)
92 (setq field (buffer-substring (match-beginning 0)
95 (if (not (string-match pat field))
96 (setq header (concat header field "\n"))
105 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
106 (defconst rfc822/quoted-pair-regexp "\\\\.")
107 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
108 (defconst rfc822/qtext-regexp
109 (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) " \t]"))
110 (defconst rfc822/quoted-string-regexp
114 "\\(" rfc822/linear-white-space-regexp "?"
115 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
117 rfc822/linear-white-space-regexp "?"
120 (defun rfc822/wrap-as-quoted-string (str)
121 "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
125 (if (memq chr rfc822/non-qtext-char-list)
126 (concat "\\" (char-to-string chr))
132 (defun rfc822/strip-quoted-pair (str)
138 (setq chr (elt str i))
139 (if (or flag (not (eq chr ?\\)))
141 (setq dest (concat dest (char-to-string chr)))
150 (defun rfc822/strip-quoted-string (str)
151 (rfc822/strip-quoted-pair
152 (let ((max (- (length str) 1))
154 (if (and (eq (elt str 0) ?\")
155 (eq (elt str max) ?\")
157 (substring str 1 max)
165 (defun rfc822/unfolding-string (str)
167 (while (string-match "\n\\s +" str)
168 (setq dest (concat dest (substring str 0 (match-beginning 0)) " "))
169 (setq str (substring str (match-end 0)))
175 ;;; @ lexical analyze
178 (defconst rfc822/special-chars "][()<>@,;:\\<>.\"")
179 (defconst rfc822/space-chars " \t\n")
180 (defconst rfc822/non-atom-chars
181 (concat rfc822/special-chars rfc822/space-chars))
182 (defconst rfc822/non-dtext-chars "[]")
183 (defconst rfc822/non-ctext-chars "()")
185 (defun rfc822/analyze-spaces (str)
186 (let ((i (position-mismatched
189 (find elt rfc822/space-chars)
193 (cons (cons 'spaces (substring str 0 i))
198 (defun rfc822/analyze-special (str)
199 (if (and (> (length str) 0)
200 (find (elt str 0) rfc822/special-chars)
202 (cons (cons 'specials (substring str 0 1))
207 (defun rfc822/analyze-atom (str)
208 (let ((i (position-mismatched
211 (not (find elt rfc822/non-atom-chars))
215 (cons (cons 'atom (substring str 0 i))
220 (defun rfc822/analyze-quoted-pair (str)
221 (if (and (>= (length str) 2)
224 (cons (cons 'quoted-pair (substring str 0 2))
229 (defun rfc822/analyze-quoted-string (str)
230 (if (and (> (length str) 0)
233 (let* ((i (position-mismatched
236 (not (memq elt rfc822/non-qtext-char-list))
238 (setq str (substring str 1))
240 (rest (substring str i))
244 (eq (elt rest 0) ?\")
246 (cons (cons 'quoted-string (substring str 0 i))
251 (defun rfc822/analyze-domain-literal (str)
252 (if (and (> (length str) 0)
255 (let* ((i (position-mismatched
258 (not (find elt rfc822/non-dtext-chars))
260 (setq str (substring str 1))
262 (rest (substring str i))
266 (eq (elt rest 0) ?\])
268 (cons (cons 'domain-literal (substring str 0 i))
273 (defun rfc822/analyze-comment (str)
274 (if (and (> (length str) 0)
279 (setq str (substring str 1))
281 (while (not (string-equal str ""))
282 (setq p (position-mismatched
285 (not (find elt rfc822/non-ctext-chars))
288 (setq dest (concat dest (substring str 0 p)))
289 (setq str (substring str p))
291 ((setq ret (rfc822/analyze-comment str))
292 (setq dest (concat dest "(" (cdr (car ret)) ")"))
298 (if (and (> (length str) 0)
301 (cons (cons 'comment dest)
306 (defun rfc822/lexical-analyze (str)
308 (i 0)(len (length str))
310 (while (not (string-equal str ""))
312 (or (rfc822/analyze-quoted-string str)
313 (rfc822/analyze-domain-literal str)
314 (rfc822/analyze-comment str)
315 (rfc822/analyze-spaces str)
316 (rfc822/analyze-special str)
317 (rfc822/analyze-atom str)
320 (setq dest (cons (car ret) dest))
330 (defun rfc822/ignored-token-p (token)
331 (let ((type (car token)))
332 (or (eq type 'spaces)(eq type 'comment))
335 (defun rfc822/parse-token (lal)
339 (setq token (car lal))
340 (rfc822/ignored-token-p token)
343 (setq itl (cons token itl))
345 (cons (reverse (cons token itl))
349 (defun rfc822/parse-ascii-token (lal)
350 (let (token itl parsed)
352 (setq token (car lal))
353 (if (find-charset-string (cdr token))
355 (rfc822/ignored-token-p token)
358 (setq itl (cons token itl))
361 (setq parsed (reverse (cons token itl)))
363 (cons parsed (cdr lal))
366 (defun rfc822/parse-token-or-comment (lal)
370 (setq token (car lal))
371 (eq (car token) 'spaces)
374 (setq itl (cons token itl))
376 (cons (reverse (cons token itl))
380 (defun rfc822/parse-word (lal)
381 (let ((ret (rfc822/parse-ascii-token lal)))
383 (let ((elt (car ret))
386 (if (or (assq 'atom elt)
387 (assq 'quoted-string elt))
388 (cons (cons 'word elt) rest)
391 (defun rfc822/parse-word-or-comment (lal)
392 (let ((ret (rfc822/parse-token-or-comment lal)))
394 (let ((elt (car ret))
397 (cond ((or (assq 'atom elt)
398 (assq 'quoted-string elt))
399 (cons (cons 'word elt) rest)
402 (cons (cons 'comment-word elt) rest)
406 (defun rfc822/parse-phrase (lal)
408 (while (setq ret (rfc822/parse-word-or-comment lal))
409 (setq phrase (append phrase (cdr (car ret))))
413 (cons (cons 'phrase phrase) lal)
416 (defun rfc822/parse-local-part (lal)
417 (let ((ret (rfc822/parse-word lal)))
419 (let ((local-part (cdr (car ret))) dot)
421 (while (and (setq ret (rfc822/parse-ascii-token lal))
423 (string-equal (cdr (assq 'specials dot)) ".")
424 (setq ret (rfc822/parse-word (cdr ret)))
426 (append local-part dot (cdr (car ret)))
430 (cons (cons 'local-part local-part) lal)
433 (defun rfc822/parse-sub-domain (lal)
434 (let ((ret (rfc822/parse-ascii-token lal)))
436 (let ((sub-domain (car ret)))
437 (if (or (assq 'atom sub-domain)
438 (assq 'domain-literal sub-domain)
440 (cons (cons 'sub-domain sub-domain)
445 (defun rfc822/parse-domain (lal)
446 (let ((ret (rfc822/parse-sub-domain lal)))
448 (let ((domain (cdr (car ret))) dot)
450 (while (and (setq ret (rfc822/parse-ascii-token lal))
452 (string-equal (cdr (assq 'specials dot)) ".")
453 (setq ret (rfc822/parse-sub-domain (cdr ret)))
455 (append domain dot (cdr (car ret)))
459 (cons (cons 'domain domain) lal)
462 (defun rfc822/parse-at-domain (lal)
463 (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
465 (setq at-sign (car ret))
466 (string-equal (cdr (assq 'specials at-sign)) "@")
467 (setq ret (rfc822/parse-domain (cdr ret)))
469 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
473 (defun rfc822/parse-addr-spec (lal)
474 (let ((ret (rfc822/parse-local-part lal))
478 (setq addr (cdr (car ret)))
480 (and (setq ret (rfc822/parse-at-domain lal))
481 (setq addr (append addr (cdr (car ret))))
484 (cons (cons 'addr-spec addr) lal)
487 (defun rfc822/parse-route (lal)
488 (let ((ret (rfc822/parse-at-domain lal))
492 (setq route (cdr (car ret)))
494 (while (and (setq ret (rfc822/parse-ascii-token lal))
495 (setq comma (car ret))
496 (string-equal (cdr (assq 'specials comma)) ",")
497 (setq ret (rfc822/parse-at-domain (cdr ret)))
499 (setq route (append route comma (cdr (car ret))))
502 (and (setq ret (rfc822/parse-ascii-token lal))
503 (setq colon (car ret))
504 (string-equal (cdr (assq 'specials colon)) ":")
505 (setq route (append route colon))
508 (cons (cons 'route route)
513 (defun rfc822/parse-route-addr (lal)
514 (let ((ret (rfc822/parse-ascii-token lal))
518 (string-equal (cdr (assq 'specials <)) "<")
520 (progn (and (setq ret (rfc822/parse-route lal))
521 (setq route (cdr (car ret)))
524 (setq ret (rfc822/parse-addr-spec lal))
526 (setq addr-spec (cdr (car ret)))
528 (setq ret (rfc822/parse-ascii-token lal))
530 (string-equal (cdr (assq 'specials >)) ">")
532 (cons (cons 'route-addr (append route addr-spec))
537 (defun rfc822/parse-phrase-route-addr (lal)
538 (let ((ret (rfc822/parse-phrase lal)) phrase)
541 (setq phrase (cdr (car ret)))
544 (if (setq ret (rfc822/parse-route-addr lal))
545 (cons (list 'phrase-route-addr
551 (defun rfc822/parse-mailbox (lal)
552 (let ((ret (or (rfc822/parse-phrase-route-addr lal)
553 (rfc822/parse-addr-spec lal)))
557 (setq mbox (car ret))
559 (if (and (setq ret (rfc822/parse-token-or-comment lal))
560 (setq comment (cdr (assq 'comment (car ret))))
564 (cons (list 'mailbox mbox comment)
568 (defun rfc822/parse-group (lal)
569 (let ((ret (rfc822/parse-phrase lal))
570 phrase colon comma mbox semicolon)
572 (setq phrase (cdr (car ret)))
574 (setq ret (rfc822/parse-ascii-token lal))
575 (setq colon (car ret))
576 (string-equal (cdr (assq 'specials colon)) ":")
579 (and (setq ret (rfc822/parse-mailbox lal))
580 (setq mbox (list (car ret)))
583 (while (and (setq ret (rfc822/parse-ascii-token lal))
584 (setq comma (car ret))
586 (cdr (assq 'specials comma)) ",")
588 (setq ret (rfc822/parse-mailbox lal))
589 (setq mbox (cons (car ret) mbox))
593 (and (setq ret (rfc822/parse-ascii-token lal))
594 (setq semicolon (car ret))
595 (string-equal (cdr (assq 'specials semicolon)) ";")
597 (cons (list 'group phrase (reverse mbox))
602 (defun rfc822/parse-address (lal)
603 (or (rfc822/parse-group lal)
604 (rfc822/parse-mailbox lal)
607 (defun rfc822/parse-addresses (lal)
608 (let ((ret (rfc822/parse-address lal)))
610 (let ((dest (list (car ret))))
612 (while (and (setq ret (rfc822/parse-ascii-token lal))
613 (string-equal (cdr (assq 'specials (car ret))) ",")
614 (setq ret (rfc822/parse-address (cdr ret)))
616 (setq dest (cons (car ret) dest))
622 (defun rfc822/addr-to-string (seq)
625 (if (eq (car token) 'spaces)
632 (defun rfc822/address-string (address)
633 (if (eq (car address) 'mailbox)
634 (let ((addr (nth 1 address))
636 (rfc822/addr-to-string
637 (if (eq (car addr) 'phrase-route-addr)
643 (defun rfc822/full-name-string (address)
644 (if (eq (car address) 'mailbox)
645 (let ((addr (nth 1 address))
646 (comment (nth 2 address))
648 (if (eq (car addr) 'phrase-route-addr)
649 (setq phrase (mapconcat (function
658 (defun rfc822/extract-address-components (str)
659 "Extract full name and canonical address from STR.
660 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
661 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
662 (let* ((structure (car
663 (rfc822/parse-address
664 (rfc822/lexical-analyze str)
666 (phrase (rfc822/full-name-string structure))
667 (address (rfc822/address-string structure))
669 (list phrase address)
678 ;;; tl-822.el ends here