1 ;;; std11.el --- STD 11 functions for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: mail, news, RFC 822, STD 11
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
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 (require 'custom) ; std11-lexical-analyzer
33 (defconst std11-field-name-regexp "[!-9;-~]+")
34 (defconst std11-field-head-regexp
35 (concat "^" std11-field-name-regexp ":"))
36 (defconst std11-next-field-head-regexp
37 (concat "\n" std11-field-name-regexp ":"))
39 (defun std11-field-end (&optional bound)
40 "Move to end of field and return this point.
41 The optional argument BOUNDs the search; it is a buffer position."
42 (if (re-search-forward std11-next-field-head-regexp bound t)
43 (goto-char (match-beginning 0))
44 (if (re-search-forward "^$" bound t)
45 (goto-char (1- (match-beginning 0)))
52 (defun std11-fetch-field (name)
53 "Return the value of the header field NAME.
54 The buffer is expected to be narrowed to just the headers of the message."
56 (goto-char (point-min))
57 (let ((case-fold-search t))
58 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
59 (buffer-substring-no-properties (match-end 0) (std11-field-end))
63 (defun std11-narrow-to-header (&optional boundary)
64 "Narrow to the message header.
65 If BOUNDARY is not nil, it is used as message header separator."
67 (goto-char (point-min))
68 (if (re-search-forward
69 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
76 (defun std11-field-body (name &optional boundary)
77 "Return the value of the header field NAME.
78 If BOUNDARY is not nil, it is used as message header separator."
81 (inline (std11-narrow-to-header boundary)
82 (std11-fetch-field name))
85 (defun std11-find-field-body (field-names &optional boundary)
86 "Return the first found field-body specified by FIELD-NAMES
87 of the message header in current buffer. If BOUNDARY is not nil, it is
88 used as message header separator."
91 (std11-narrow-to-header boundary)
92 (let ((case-fold-search t)
95 (while (setq field-name (car field-names))
96 (goto-char (point-min))
97 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
99 (buffer-substring-no-properties
100 (match-end 0) (std11-field-end)))
102 (setq field-names (cdr field-names))
105 (defun std11-field-bodies (field-names &optional default-value boundary)
106 "Return list of each field-bodies of FIELD-NAMES of the message header
107 in current buffer. If BOUNDARY is not nil, it is used as message
111 (std11-narrow-to-header boundary)
112 (let* ((case-fold-search t)
113 (dest (make-list (length field-names) default-value))
117 (while (setq field-name (car s-rest))
118 (goto-char (point-min))
119 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
121 (buffer-substring-no-properties
122 (match-end 0) (std11-field-end)))
124 (setq s-rest (cdr s-rest)
129 (defun std11-header-string (regexp &optional boundary)
130 "Return string of message header fields matched by REGEXP.
131 If BOUNDARY is not nil, it is used as message header separator."
132 (let ((case-fold-search t))
135 (std11-narrow-to-header boundary)
136 (goto-char (point-min))
138 (while (re-search-forward std11-field-head-regexp nil t)
140 (buffer-substring (match-beginning 0) (std11-field-end)))
141 (if (string-match regexp field)
142 (setq header (concat header field "\n"))
147 (defun std11-header-string-except (regexp &optional boundary)
148 "Return string of message header fields not matched by REGEXP.
149 If BOUNDARY is not nil, it is used as message header separator."
150 (let ((case-fold-search t))
153 (std11-narrow-to-header boundary)
154 (goto-char (point-min))
156 (while (re-search-forward std11-field-head-regexp nil t)
158 (buffer-substring (match-beginning 0) (std11-field-end)))
159 (if (not (string-match regexp field))
160 (setq header (concat header field "\n"))
165 (defun std11-collect-field-names (&optional boundary)
166 "Return list of all field-names of the message header in current buffer.
167 If BOUNDARY is not nil, it is used as message header separator."
170 (std11-narrow-to-header boundary)
171 (goto-char (point-min))
173 (while (re-search-forward std11-field-head-regexp nil t)
174 (setq name (buffer-substring-no-properties
175 (match-beginning 0)(1- (match-end 0))))
176 (or (member name dest)
177 (setq dest (cons name dest))
187 (defun std11-unfold-string (string)
188 "Unfold STRING as message header field."
191 (while (string-match "\n\\([ \t]\\)" string p)
192 (setq dest (concat dest
193 (substring string p (match-beginning 0))
196 (setq p (match-end 0)))
199 (concat dest (substring string p))
206 (defun std11-wrap-as-quoted-pairs (string specials)
210 (len (length string))
213 (let ((chr (aref string i)))
214 (if (memq chr specials)
215 (setq dest (concat dest (substring string b i) "\\")
220 (concat dest (substring string b))
223 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
225 (defun std11-wrap-as-quoted-string (string)
226 "Wrap STRING as RFC 822 quoted-string."
228 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
231 (defun std11-strip-quoted-pair (string)
232 "Strip quoted-pairs in STRING."
236 (len (length string))
239 (let ((chr (aref string i)))
241 (setq dest (concat dest (substring string b i))
246 (concat dest (substring string b))
249 (defun std11-strip-quoted-string (string)
250 "Strip quoted-string STRING."
251 (let ((len (length string)))
253 (let ((max (1- len)))
254 (and (eq (aref string 0) ?\")
255 (eq (aref string max) ?\")
256 (std11-strip-quoted-pair (substring string 1 max))
261 ;;; @ lexical analyze
264 (defcustom std11-lexical-analyzer
265 '(std11-analyze-quoted-string
266 std11-analyze-domain-literal
267 std11-analyze-comment
269 std11-analyze-special
271 "*List of functions to return result of lexical analyze.
272 Each function must have two arguments: STRING and START.
273 STRING is the target string to be analyzed.
274 START is start position of STRING to analyze.
276 Previous function is preferred to next function. If a function
277 returns nil, next function is used. Otherwise the return value will
281 :type '(repeat function))
284 (defconst std11-space-char-list '(? ?\t ?\n))
285 (defconst std11-special-char-list '(?\] ?\[
290 ;; (defconst std11-spaces-regexp
291 ;; (eval-when-compile (concat "[" std11-space-char-list "]+")))
292 (defconst std11-atom-regexp
294 (concat "[^" std11-special-char-list std11-space-char-list "]+")))
296 (defun std11-analyze-spaces (string start)
297 (if (and (string-match (eval-when-compile
298 (concat "[" std11-space-char-list "]+"))
300 (= (match-beginning 0) start))
301 (let ((end (match-end 0)))
302 (cons (cons 'spaces (substring string start end))
303 ;;(substring string end)
307 (defun std11-analyze-special (string start)
308 (if (and (> (length string) start)
309 (memq (aref string start) std11-special-char-list))
310 (cons (cons 'specials (substring string start (1+ start)))
311 ;;(substring string 1)
315 (defun std11-analyze-atom (string start)
316 (if (and (string-match std11-atom-regexp string start)
317 (= (match-beginning 0) start))
318 (let ((end (match-end 0)))
319 (cons (cons 'atom (substring string start end))
320 ;;(substring string end)
324 (defun std11-check-enclosure (string open close &optional recursive from)
325 (let ((len (length string))
329 (eq (aref string i) open))
334 (setq chr (aref string i))
347 (setq p (std11-check-enclosure
348 string open close recursive i))
358 (defun std11-analyze-quoted-string (string start)
359 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
361 (cons (cons 'quoted-string (substring string (1+ start) (1- p)))
362 ;;(substring string p))
366 (defun std11-analyze-domain-literal (string start)
367 (let ((p (std11-check-enclosure string ?\[ ?\] nil start)))
369 (cons (cons 'domain-literal (substring string (1+ start) (1- p)))
370 ;;(substring string p))
374 (defun std11-analyze-comment (string start)
375 (let ((p (std11-check-enclosure string ?\( ?\) t start)))
377 (cons (cons 'comment (substring string (1+ start) (1- p)))
378 ;;(substring string p))
383 (defun std11-lexical-analyze (string &optional analyzer start)
384 "Analyze STRING as lexical tokens of STD 11."
386 (setq analyzer std11-lexical-analyzer))
389 (let ((len (length string))
393 (let ((rest analyzer)
395 (while (and (setq func (car rest))
396 (null (setq r (funcall func string start))))
397 (setq rest (cdr rest)))
399 (cons (cons 'error (substring string start)) (1+ len)))
401 (setq dest (cons (car ret) dest)
411 (defun std11-ignored-token-p (token)
412 (let ((type (car token)))
413 (or (eq type 'spaces)(eq type 'comment))
416 (defun std11-parse-token (lal)
420 (setq token (car lal))
421 (std11-ignored-token-p token)
424 (setq itl (cons token itl))
426 (cons (nreverse (cons token itl))
430 (defun std11-parse-ascii-token (lal)
431 (let (token itl parsed token-value)
433 (setq token (car lal))
434 (or (std11-ignored-token-p token)
435 (if (and (setq token-value (cdr token))
436 (delq 'ascii (find-charset-string token-value)))
440 (setq itl (cons token itl))
443 (setq parsed (nreverse (cons token itl)))
445 (cons parsed (cdr lal))
448 (defun std11-parse-token-or-comment (lal)
452 (setq token (car lal))
453 (eq (car token) 'spaces)
456 (setq itl (cons token itl))
458 (cons (nreverse (cons token itl))
462 (defun std11-parse-word (lal)
463 (let ((ret (std11-parse-ascii-token lal)))
465 (let ((elt (car ret))
468 (if (or (assq 'atom elt)
469 (assq 'quoted-string elt))
470 (cons (cons 'word elt) rest)
473 (defun std11-parse-word-or-comment (lal)
474 (let ((ret (std11-parse-token-or-comment lal)))
476 (let ((elt (car ret))
479 (cond ((or (assq 'atom elt)
480 (assq 'quoted-string elt))
481 (cons (cons 'word elt) rest)
484 (cons (cons 'comment-word elt) rest)
488 (defun std11-parse-phrase (lal)
490 (while (setq ret (std11-parse-word-or-comment lal))
491 (setq phrase (append phrase (cdr (car ret))))
495 (cons (cons 'phrase phrase) lal)
498 (defun std11-parse-local-part (lal)
499 (let ((ret (std11-parse-word lal)))
501 (let ((local-part (cdr (car ret))) dot)
503 (while (and (setq ret (std11-parse-ascii-token lal))
505 (string-equal (cdr (assq 'specials dot)) ".")
506 (setq ret (std11-parse-word (cdr ret)))
508 (append local-part dot (cdr (car ret)))
512 (cons (cons 'local-part local-part) lal)
515 (defun std11-parse-sub-domain (lal)
516 (let ((ret (std11-parse-ascii-token lal)))
518 (let ((sub-domain (car ret)))
519 (if (or (assq 'atom sub-domain)
520 (assq 'domain-literal sub-domain)
522 (cons (cons 'sub-domain sub-domain)
527 (defun std11-parse-domain (lal)
528 (let ((ret (std11-parse-sub-domain lal)))
530 (let ((domain (cdr (car ret))) dot)
532 (while (and (setq ret (std11-parse-ascii-token lal))
534 (string-equal (cdr (assq 'specials dot)) ".")
535 (setq ret (std11-parse-sub-domain (cdr ret)))
537 (append domain dot (cdr (car ret)))
541 (cons (cons 'domain domain) lal)
544 (defun std11-parse-at-domain (lal)
545 (let ((ret (std11-parse-ascii-token lal)) at-sign)
547 (setq at-sign (car ret))
548 (string-equal (cdr (assq 'specials at-sign)) "@")
549 (setq ret (std11-parse-domain (cdr ret)))
551 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
555 (defun std11-parse-addr-spec (lal)
556 (let ((ret (std11-parse-local-part lal))
560 (setq addr (cdr (car ret)))
562 (and (setq ret (std11-parse-at-domain lal))
563 (setq addr (append addr (cdr (car ret))))
566 (cons (cons 'addr-spec addr) lal)
569 (defun std11-parse-route (lal)
570 (let ((ret (std11-parse-at-domain lal))
574 (setq route (cdr (car ret)))
576 (while (and (setq ret (std11-parse-ascii-token lal))
577 (setq comma (car ret))
578 (string-equal (cdr (assq 'specials comma)) ",")
579 (setq ret (std11-parse-at-domain (cdr ret)))
581 (setq route (append route comma (cdr (car ret))))
584 (and (setq ret (std11-parse-ascii-token lal))
585 (setq colon (car ret))
586 (string-equal (cdr (assq 'specials colon)) ":")
587 (setq route (append route colon))
590 (cons (cons 'route route)
595 (defun std11-parse-route-addr (lal)
596 (let ((ret (std11-parse-ascii-token lal))
600 (string-equal (cdr (assq 'specials <)) "<")
602 (progn (and (setq ret (std11-parse-route lal))
603 (setq route (cdr (car ret)))
606 (setq ret (std11-parse-addr-spec lal))
608 (setq addr-spec (cdr (car ret)))
610 (setq ret (std11-parse-ascii-token lal))
612 (string-equal (cdr (assq 'specials >)) ">")
614 (cons (cons 'route-addr (append route addr-spec))
619 (defun std11-parse-phrase-route-addr (lal)
620 (let ((ret (std11-parse-phrase lal)) phrase)
623 (setq phrase (cdr (car ret)))
626 (if (setq ret (std11-parse-route-addr lal))
627 (cons (list 'phrase-route-addr
633 (defun std11-parse-mailbox (lal)
634 (let ((ret (or (std11-parse-phrase-route-addr lal)
635 (std11-parse-addr-spec lal)))
639 (setq mbox (car ret))
641 (if (and (setq ret (std11-parse-token-or-comment lal))
642 (setq comment (cdr (assq 'comment (car ret))))
646 (cons (list 'mailbox mbox comment)
650 (defun std11-parse-group (lal)
651 (let ((ret (std11-parse-phrase lal))
652 phrase colon comma mbox semicolon)
654 (setq phrase (cdr (car ret)))
656 (setq ret (std11-parse-ascii-token lal))
657 (setq colon (car ret))
658 (string-equal (cdr (assq 'specials colon)) ":")
661 (and (setq ret (std11-parse-mailbox lal))
662 (setq mbox (list (car ret)))
665 (while (and (setq ret (std11-parse-ascii-token lal))
666 (setq comma (car ret))
668 (cdr (assq 'specials comma)) ",")
670 (setq ret (std11-parse-mailbox lal))
671 (setq mbox (cons (car ret) mbox))
675 (and (setq ret (std11-parse-ascii-token lal))
676 (setq semicolon (car ret))
677 (string-equal (cdr (assq 'specials semicolon)) ";")
679 (cons (list 'group phrase (nreverse mbox))
684 (defun std11-parse-address (lal)
685 (or (std11-parse-group lal)
686 (std11-parse-mailbox lal)
689 (defun std11-parse-addresses (lal)
690 (let ((ret (std11-parse-address lal)))
692 (let ((dest (list (car ret))))
694 (while (and (setq ret (std11-parse-ascii-token lal))
695 (string-equal (cdr (assq 'specials (car ret))) ",")
696 (setq ret (std11-parse-address (cdr ret)))
698 (setq dest (cons (car ret) dest))
704 (defun std11-parse-msg-id (lal)
705 (let ((ret (std11-parse-ascii-token lal))
709 (string-equal (cdr (assq 'specials <)) "<")
711 (setq ret (std11-parse-addr-spec lal))
712 (setq addr-spec (car ret))
714 (setq ret (std11-parse-ascii-token lal))
716 (string-equal (cdr (assq 'specials >)) ">")
718 (cons (cons 'msg-id (cdr addr-spec))
722 (defun std11-parse-msg-ids (tokens)
723 "Parse lexical TOKENS as `*(phrase / msg-id)', and return the result."
724 (let ((ret (or (std11-parse-msg-id tokens)
725 (std11-parse-phrase tokens))))
727 (let ((dest (list (car ret))))
728 (setq tokens (cdr ret))
729 (while (setq ret (or (std11-parse-msg-id tokens)
730 (std11-parse-phrase tokens)))
731 (setq dest (cons (car ret) dest))
732 (setq tokens (cdr ret))
737 (defalias 'std11-parse-in-reply-to 'std11-parse-msg-ids)
738 (make-obsolete 'std11-parse-in-reply-to 'std11-parse-msg-ids)
744 (defun std11-addr-to-string (seq)
745 "Return string from lexical analyzed list SEQ
746 represents addr-spec of RFC 822."
749 (let ((name (car token)))
751 ((eq name 'spaces) "")
752 ((eq name 'comment) "")
753 ((eq name 'quoted-string)
754 (concat "\"" (cdr token) "\""))
761 (defun std11-address-string (address)
762 "Return string of address part from parsed ADDRESS of RFC 822."
763 (cond ((eq (car address) 'group)
764 (mapconcat (function std11-address-string)
768 ((eq (car address) 'mailbox)
769 (let ((addr (nth 1 address)))
770 (std11-addr-to-string
771 (if (eq (car addr) 'phrase-route-addr)
777 (defun std11-comment-value-to-string (value)
779 (std11-strip-quoted-pair value)
784 (if (stringp (car value))
787 (std11-comment-value-to-string
796 (defun std11-full-name-string (address)
797 "Return string of full-name part from parsed ADDRESS of RFC 822."
798 (cond ((eq (car address) 'group)
805 ((eq (car address) 'mailbox)
806 (let ((addr (nth 1 address))
807 (comment (nth 2 address))
809 (if (eq (car addr) 'phrase-route-addr)
814 (let ((type (car token)))
815 (cond ((eq type 'quoted-string)
816 (std11-strip-quoted-pair (cdr token))
820 (std11-comment-value-to-string
829 (cond ((> (length phrase) 0) phrase)
830 (comment (std11-comment-value-to-string comment))
835 (defun std11-msg-id-string (msg-id)
836 "Return string from parsed MSG-ID of RFC 822."
837 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
841 (defun std11-fill-msg-id-list-string (string &optional column)
842 "Fill list of msg-id in STRING, and return the result."
845 (let ((lal (std11-lexical-analyze string))
847 (let ((ret (std11-parse-msg-id lal)))
849 (let* ((str (std11-msg-id-string (car ret)))
852 (if (> (+ len column) 76)
853 (setq dest (concat dest "\n " str)
856 column (+ column len))
858 (setq dest (concat dest (cdr (car lal)))
862 (let ((ret (std11-parse-msg-id lal)))
864 (let* ((str (std11-msg-id-string (car ret)))
865 (len (1+ (length str))))
867 (if (> (+ len column) 76)
868 (setq dest (concat dest "\n " str)
870 (setq dest (concat dest " " str)
871 column (+ column len))
873 (setq dest (concat dest (cdr (car lal)))
879 ;;; @ parser with lexical analyzer
883 (defun std11-parse-address-string (string)
884 "Parse STRING as mail address."
885 (std11-parse-address (std11-lexical-analyze string))
889 (defun std11-parse-addresses-string (string)
890 "Parse STRING as mail address list."
891 (std11-parse-addresses (std11-lexical-analyze string))
895 (defun std11-parse-msg-id-string (string)
896 "Parse STRING as msg-id."
897 (std11-parse-msg-id (std11-lexical-analyze string))
901 (defun std11-parse-msg-ids-string (string)
902 "Parse STRING as `*(phrase / msg-id)'."
903 (std11-parse-msg-ids (std11-lexical-analyze string))
907 (defun std11-extract-address-components (string)
908 "Extract full name and canonical address from STRING.
909 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
910 If no name can be extracted, FULL-NAME will be nil."
911 (let* ((structure (car (std11-parse-address-string
912 (std11-unfold-string string))))
913 (phrase (std11-full-name-string structure))
914 (address (std11-address-string structure))
916 (list phrase address)
925 ;;; std11.el ends here