1 ;;; std11.el --- STD 11 functions for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, RFC 822, STD 11
8 ;; This file is part of MU (Message Utilities).
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 GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 (autoload 'buffer-substring-no-properties "emu")
28 (autoload 'member "emu")
34 (defconst std11-field-name-regexp "[!-9;-~]+")
35 (defconst std11-field-head-regexp
36 (concat "^" std11-field-name-regexp ":"))
37 (defconst std11-next-field-head-regexp
38 (concat "\n" std11-field-name-regexp ":"))
40 (defun std11-field-end ()
41 "Move to end of field and return this point. [std11.el]"
42 (if (re-search-forward std11-next-field-head-regexp nil t)
43 (goto-char (match-beginning 0))
44 (if (re-search-forward "^$" nil t)
45 (goto-char (1- (match-beginning 0)))
51 (defsubst std11-fetch-field (name)
52 "Return the value of the header field NAME.
53 The buffer is expected to be narrowed to just the headers of the message."
55 (goto-char (point-min))
56 (let ((case-fold-search t))
57 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
58 (buffer-substring-no-properties (match-end 0) (std11-field-end))
61 (defun std11-field-body (name &optional boundary)
62 "Return the value of the header field NAME.
63 If BOUNDARY is not nil, it is used as message header separator."
66 (std11-narrow-to-header boundary)
67 (std11-fetch-field name)
70 (defun std11-find-field-body (field-names &optional boundary)
71 "Return the first found field-body specified by FIELD-NAMES
72 of the message header in current buffer. If BOUNDARY is not nil, it is
73 used as message header separator. [std11.el]"
76 (std11-narrow-to-header boundary)
77 (let ((case-fold-search t)
80 (while (setq field-name (car field-names))
81 (goto-char (point-min))
82 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
84 (buffer-substring-no-properties
85 (match-end 0) (std11-field-end)))
87 (setq field-names (cdr field-names))
90 (defun std11-field-bodies (field-names &optional default-value boundary)
91 "Return list of each field-bodies of FIELD-NAMES of the message header
92 in current buffer. If BOUNDARY is not nil, it is used as message
93 header separator. [std11.el]"
96 (std11-narrow-to-header boundary)
97 (let* ((case-fold-search t)
98 (dest (make-list (length field-names) default-value))
102 (while (setq field-name (car s-rest))
103 (goto-char (point-min))
104 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
106 (buffer-substring-no-properties
107 (match-end 0) (std11-field-end)))
109 (setq s-rest (cdr s-rest)
118 (defun std11-unfold-string (string)
119 "Unfold STRING as message header field. [std11.el]"
121 (while (string-match "\n\\([ \t]\\)" string)
122 (setq dest (concat dest
123 (substring string 0 (match-beginning 0))
124 (match-string 1 string)
126 (setq string (substring string (match-end 0)))
135 (defun std11-narrow-to-header (&optional boundary)
136 "Narrow to the message header.
137 If BOUNDARY is not nil, it is used as message header separator.
140 (goto-char (point-min))
141 (if (re-search-forward
142 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
148 (defun std11-header-string (regexp &optional boundary)
149 "Return string of message header fields matched by REGEXP.
150 If BOUNDARY is not nil, it is used as message header separator.
152 (let ((case-fold-search t))
155 (std11-narrow-to-header boundary)
156 (goto-char (point-min))
158 (while (re-search-forward std11-field-head-regexp nil t)
160 (buffer-substring (match-beginning 0) (std11-field-end)))
161 (if (string-match regexp field)
162 (setq header (concat header field "\n"))
167 (defun std11-header-string-except (regexp &optional boundary)
168 "Return string of message header fields not matched by REGEXP.
169 If BOUNDARY is not nil, it is used as message header separator.
171 (let ((case-fold-search t))
174 (std11-narrow-to-header boundary)
175 (goto-char (point-min))
177 (while (re-search-forward std11-field-head-regexp nil t)
179 (buffer-substring (match-beginning 0) (std11-field-end)))
180 (if (not (string-match regexp field))
181 (setq header (concat header field "\n"))
186 (defun std11-collect-field-names (&optional boundary)
187 "Return list of all field-names of the message header in current buffer.
188 If BOUNDARY is not nil, it is used as message header separator.
192 (std11-narrow-to-header boundary)
193 (goto-char (point-min))
195 (while (re-search-forward std11-field-head-regexp nil t)
196 (setq name (buffer-substring-no-properties
197 (match-beginning 0)(1- (match-end 0))))
198 (or (member name dest)
199 (setq dest (cons name dest))
208 (defun std11-wrap-as-quoted-pairs (string specials)
212 (len (length string))
215 (let ((chr (aref string i)))
216 (if (memq chr specials)
217 (setq dest (concat dest (substring string b i) "\\")
222 (concat dest (substring string b))
225 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
227 (defun std11-wrap-as-quoted-string (string)
228 "Wrap STRING as RFC 822 quoted-string. [std11.el]"
230 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
233 (defun std11-strip-quoted-pair (string)
234 "Strip quoted-pairs in STRING. [std11.el]"
238 (len (length string))
241 (let ((chr (aref string i)))
243 (setq dest (concat dest (substring string b i))
248 (concat dest (substring string b))
251 (defun std11-strip-quoted-string (string)
252 "Strip quoted-string STRING. [std11.el]"
253 (let ((len (length string)))
255 (let ((max (1- len)))
256 (and (eq (aref string 0) ?\")
257 (eq (aref string max) ?\")
258 (std11-strip-quoted-pair (substring string 1 max))
263 ;;; @ lexical analyze
266 (defconst std11-space-chars " \t\n")
267 (defconst std11-spaces-regexp (` (, (concat "[" std11-space-chars "]+"))))
268 (defconst std11-special-char-list '(?\] ?\[
272 (defconst std11-atom-regexp
273 (` (, (concat "^[^" std11-special-char-list std11-space-chars "]+"))))
275 (defun std11-analyze-spaces (string)
276 (if (and (string-match std11-spaces-regexp string)
277 (= (match-beginning 0) 0))
278 (let ((end (match-end 0)))
279 (cons (cons 'spaces (substring string 0 end))
280 (substring string end)
283 (defun std11-analyze-special (str)
284 (if (and (> (length str) 0)
285 (memq (aref str 0) std11-special-char-list))
286 (cons (cons 'specials (substring str 0 1))
290 (defun std11-analyze-atom (str)
291 (if (string-match std11-atom-regexp str)
292 (let ((end (match-end 0)))
293 (cons (cons 'atom (substring str 0 end))
297 (defun std11-check-enclosure (str open close &optional recursive from)
298 (let ((len (length str))
302 (eq (aref str i) open))
307 (setq chr (aref str i))
320 (setq p (std11-check-enclosure
321 str open close recursive i))
331 (defun std11-analyze-quoted-string (str)
332 (let ((p (std11-check-enclosure str ?\" ?\")))
334 (cons (cons 'quoted-string (substring str 1 (1- p)))
338 (defun std11-analyze-domain-literal (str)
339 (let ((p (std11-check-enclosure str ?\[ ?\])))
341 (cons (cons 'domain-literal (substring str 1 (1- p)))
345 (defun std11-analyze-comment (str)
346 (let ((p (std11-check-enclosure str ?\( ?\) t)))
348 (cons (cons 'comment (substring str 1 (1- p)))
352 (defun std11-lexical-analyze (str)
354 (while (not (string-equal str ""))
356 (or (std11-analyze-quoted-string str)
357 (std11-analyze-domain-literal str)
358 (std11-analyze-comment str)
359 (std11-analyze-spaces str)
360 (std11-analyze-special str)
361 (std11-analyze-atom str)
364 (setq dest (cons (car ret) dest))
374 (defun std11-ignored-token-p (token)
375 (let ((type (car token)))
376 (or (eq type 'spaces)(eq type 'comment))
379 (defun std11-parse-token (lal)
383 (setq token (car lal))
384 (std11-ignored-token-p token)
387 (setq itl (cons token itl))
389 (cons (nreverse (cons token itl))
393 (defun std11-parse-ascii-token (lal)
394 (let (token itl parsed token-value)
396 (setq token (car lal))
397 (or (std11-ignored-token-p token)
398 (if (and (setq token-value (cdr token))
399 (find-non-ascii-charset-string token-value)
404 (setq itl (cons token itl))
407 (setq parsed (nreverse (cons token itl)))
409 (cons parsed (cdr lal))
412 (defun std11-parse-token-or-comment (lal)
416 (setq token (car lal))
417 (eq (car token) 'spaces)
420 (setq itl (cons token itl))
422 (cons (nreverse (cons token itl))
426 (defun std11-parse-word (lal)
427 (let ((ret (std11-parse-ascii-token lal)))
429 (let ((elt (car ret))
432 (if (or (assq 'atom elt)
433 (assq 'quoted-string elt))
434 (cons (cons 'word elt) rest)
437 (defun std11-parse-word-or-comment (lal)
438 (let ((ret (std11-parse-token-or-comment lal)))
440 (let ((elt (car ret))
443 (cond ((or (assq 'atom elt)
444 (assq 'quoted-string elt))
445 (cons (cons 'word elt) rest)
448 (cons (cons 'comment-word elt) rest)
452 (defun std11-parse-phrase (lal)
454 (while (setq ret (std11-parse-word-or-comment lal))
455 (setq phrase (append phrase (cdr (car ret))))
459 (cons (cons 'phrase phrase) lal)
462 (defun std11-parse-local-part (lal)
463 (let ((ret (std11-parse-word lal)))
465 (let ((local-part (cdr (car ret))) dot)
467 (while (and (setq ret (std11-parse-ascii-token lal))
469 (string-equal (cdr (assq 'specials dot)) ".")
470 (setq ret (std11-parse-word (cdr ret)))
472 (append local-part dot (cdr (car ret)))
476 (cons (cons 'local-part local-part) lal)
479 (defun std11-parse-sub-domain (lal)
480 (let ((ret (std11-parse-ascii-token lal)))
482 (let ((sub-domain (car ret)))
483 (if (or (assq 'atom sub-domain)
484 (assq 'domain-literal sub-domain)
486 (cons (cons 'sub-domain sub-domain)
491 (defun std11-parse-domain (lal)
492 (let ((ret (std11-parse-sub-domain lal)))
494 (let ((domain (cdr (car ret))) dot)
496 (while (and (setq ret (std11-parse-ascii-token lal))
498 (string-equal (cdr (assq 'specials dot)) ".")
499 (setq ret (std11-parse-sub-domain (cdr ret)))
501 (append domain dot (cdr (car ret)))
505 (cons (cons 'domain domain) lal)
508 (defun std11-parse-at-domain (lal)
509 (let ((ret (std11-parse-ascii-token lal)) at-sign)
511 (setq at-sign (car ret))
512 (string-equal (cdr (assq 'specials at-sign)) "@")
513 (setq ret (std11-parse-domain (cdr ret)))
515 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
519 (defun std11-parse-addr-spec (lal)
520 (let ((ret (std11-parse-local-part lal))
524 (setq addr (cdr (car ret)))
526 (and (setq ret (std11-parse-at-domain lal))
527 (setq addr (append addr (cdr (car ret))))
530 (cons (cons 'addr-spec addr) lal)
533 (defun std11-parse-route (lal)
534 (let ((ret (std11-parse-at-domain lal))
538 (setq route (cdr (car ret)))
540 (while (and (setq ret (std11-parse-ascii-token lal))
541 (setq comma (car ret))
542 (string-equal (cdr (assq 'specials comma)) ",")
543 (setq ret (std11-parse-at-domain (cdr ret)))
545 (setq route (append route comma (cdr (car ret))))
548 (and (setq ret (std11-parse-ascii-token lal))
549 (setq colon (car ret))
550 (string-equal (cdr (assq 'specials colon)) ":")
551 (setq route (append route colon))
554 (cons (cons 'route route)
559 (defun std11-parse-route-addr (lal)
560 (let ((ret (std11-parse-ascii-token lal))
564 (string-equal (cdr (assq 'specials <)) "<")
566 (progn (and (setq ret (std11-parse-route lal))
567 (setq route (cdr (car ret)))
570 (setq ret (std11-parse-addr-spec lal))
572 (setq addr-spec (cdr (car ret)))
574 (setq ret (std11-parse-ascii-token lal))
576 (string-equal (cdr (assq 'specials >)) ">")
578 (cons (cons 'route-addr (append route addr-spec))
583 (defun std11-parse-phrase-route-addr (lal)
584 (let ((ret (std11-parse-phrase lal)) phrase)
587 (setq phrase (cdr (car ret)))
590 (if (setq ret (std11-parse-route-addr lal))
591 (cons (list 'phrase-route-addr
597 (defun std11-parse-mailbox (lal)
598 (let ((ret (or (std11-parse-phrase-route-addr lal)
599 (std11-parse-addr-spec lal)))
603 (setq mbox (car ret))
605 (if (and (setq ret (std11-parse-token-or-comment lal))
606 (setq comment (cdr (assq 'comment (car ret))))
610 (cons (list 'mailbox mbox comment)
614 (defun std11-parse-group (lal)
615 (let ((ret (std11-parse-phrase lal))
616 phrase colon comma mbox semicolon)
618 (setq phrase (cdr (car ret)))
620 (setq ret (std11-parse-ascii-token lal))
621 (setq colon (car ret))
622 (string-equal (cdr (assq 'specials colon)) ":")
625 (and (setq ret (std11-parse-mailbox lal))
626 (setq mbox (list (car ret)))
629 (while (and (setq ret (std11-parse-ascii-token lal))
630 (setq comma (car ret))
632 (cdr (assq 'specials comma)) ",")
634 (setq ret (std11-parse-mailbox lal))
635 (setq mbox (cons (car ret) mbox))
639 (and (setq ret (std11-parse-ascii-token lal))
640 (setq semicolon (car ret))
641 (string-equal (cdr (assq 'specials semicolon)) ";")
643 (cons (list 'group phrase (nreverse mbox))
648 (defun std11-parse-address (lal)
649 (or (std11-parse-group lal)
650 (std11-parse-mailbox lal)
653 (defun std11-parse-addresses (lal)
654 (let ((ret (std11-parse-address lal)))
656 (let ((dest (list (car ret))))
658 (while (and (setq ret (std11-parse-ascii-token lal))
659 (string-equal (cdr (assq 'specials (car ret))) ",")
660 (setq ret (std11-parse-address (cdr ret)))
662 (setq dest (cons (car ret) dest))
668 (defun std11-parse-msg-id (lal)
669 (let ((ret (std11-parse-ascii-token lal))
673 (string-equal (cdr (assq 'specials <)) "<")
675 (setq ret (std11-parse-addr-spec lal))
676 (setq addr-spec (car ret))
678 (setq ret (std11-parse-ascii-token lal))
680 (string-equal (cdr (assq 'specials >)) ">")
682 (cons (cons 'msg-id (cdr addr-spec))
690 (defun std11-addr-to-string (seq)
691 "Return string from lexical analyzed list SEQ
692 represents addr-spec of RFC 822. [std11.el]"
695 (let ((name (car token)))
697 ((eq name 'spaces) "")
698 ((eq name 'comment) "")
699 ((eq name 'quoted-string)
700 (concat "\"" (cdr token) "\""))
706 (defun std11-address-string (address)
707 "Return string of address part from parsed ADDRESS of RFC 822.
709 (cond ((eq (car address) 'group)
710 (mapconcat (function std11-address-string)
714 ((eq (car address) 'mailbox)
715 (let ((addr (nth 1 address)))
716 (std11-addr-to-string
717 (if (eq (car addr) 'phrase-route-addr)
723 (defun std11-full-name-string (address)
724 "Return string of full-name part from parsed ADDRESS of RFC 822.
726 (cond ((eq (car address) 'group)
733 ((eq (car address) 'mailbox)
734 (let ((addr (nth 1 address))
735 (comment (nth 2 address))
737 (if (eq (car addr) 'phrase-route-addr)
742 (let ((type (car token)))
743 (cond ((eq type 'quoted-string)
744 (std11-strip-quoted-pair (cdr token))
749 (std11-strip-quoted-pair (cdr token))
757 (cond ((> (length phrase) 0) phrase)
758 (comment (std11-strip-quoted-pair comment))
762 (defun std11-msg-id-string (msg-id)
763 "Return string from parsed MSG-ID of RFC 822."
764 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
767 (defun std11-fill-msg-id-list-string (string &optional column)
768 "Fill list of msg-id in STRING, and return the result."
771 (let ((lal (std11-lexical-analyze string))
773 (let ((ret (std11-parse-msg-id lal)))
775 (let* ((str (std11-msg-id-string (car ret)))
778 (if (> (+ len column) 76)
779 (setq dest (concat dest "\n " str)
782 column (+ column len))
784 (setq dest (concat dest (cdr (car lal)))
788 (let ((ret (std11-parse-msg-id lal)))
790 (let* ((str (std11-msg-id-string (car ret)))
791 (len (1+ (length str))))
793 (if (> (+ len column) 76)
794 (setq dest (concat dest "\n " str)
796 (setq dest (concat dest " " str)
797 column (+ column len))
799 (setq dest (concat dest (cdr (car lal)))
805 ;;; @ parser with lexical analyzer
808 (defun std11-parse-address-string (string)
809 "Parse STRING as mail address. [std11.el]"
810 (std11-parse-address (std11-lexical-analyze string))
813 (defun std11-parse-addresses-string (string)
814 "Parse STRING as mail address list. [std11.el]"
815 (std11-parse-addresses (std11-lexical-analyze string))
818 (defun std11-extract-address-components (string)
819 "Extract full name and canonical address from STRING.
820 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
821 If no name can be extracted, FULL-NAME will be nil. [std11.el]"
822 (let* ((structure (car (std11-parse-address-string
823 (std11-unfold-string string))))
824 (phrase (std11-full-name-string structure))
825 (address (std11-address-string structure))
827 (list phrase address)
836 ;;; std11.el ends here