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.
28 (require 'poem) ; find-non-ascii-charset-string
29 (require 'pcustom) ; std11-lexical-analyzer
35 (defconst std11-field-name-regexp "[!-9;-~]+")
36 (defconst std11-field-head-regexp
37 (concat "^" std11-field-name-regexp ":"))
38 (defconst std11-next-field-head-regexp
39 (concat "\n" std11-field-name-regexp ":"))
41 (defun std11-field-end (&optional bound)
42 "Move to end of field and return this point.
43 The optional argument BOUNDs the search; it is a buffer position."
44 (if (re-search-forward std11-next-field-head-regexp bound t)
45 (goto-char (match-beginning 0))
46 (if (re-search-forward "^$" bound t)
47 (goto-char (1- (match-beginning 0)))
54 (defun std11-fetch-field (name)
55 "Return the value of the header field NAME.
56 The buffer is expected to be narrowed to just the headers of the message."
58 (goto-char (point-min))
59 (let ((case-fold-search t))
60 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
61 (buffer-substring-no-properties (match-end 0) (std11-field-end))
65 (defun std11-narrow-to-header (&optional boundary)
66 "Narrow to the message header.
67 If BOUNDARY is not nil, it is used as message header separator."
69 (goto-char (point-min))
70 (if (re-search-forward
71 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
78 (defun std11-field-body (name &optional boundary)
79 "Return the value of the header field NAME.
80 If BOUNDARY is not nil, it is used as message header separator."
83 (inline (std11-narrow-to-header boundary)
84 (std11-fetch-field name))
87 (defun std11-find-field-body (field-names &optional boundary)
88 "Return the first found field-body specified by FIELD-NAMES
89 of the message header in current buffer. If BOUNDARY is not nil, it is
90 used as message header separator."
93 (std11-narrow-to-header boundary)
94 (let ((case-fold-search t)
97 (while (setq field-name (car field-names))
98 (goto-char (point-min))
99 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
101 (buffer-substring-no-properties
102 (match-end 0) (std11-field-end)))
104 (setq field-names (cdr field-names))
107 (defun std11-field-bodies (field-names &optional default-value boundary)
108 "Return list of each field-bodies of FIELD-NAMES of the message header
109 in current buffer. If BOUNDARY is not nil, it is used as message
113 (std11-narrow-to-header boundary)
114 (let* ((case-fold-search t)
115 (dest (make-list (length field-names) default-value))
119 (while (setq field-name (car s-rest))
120 (goto-char (point-min))
121 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
123 (buffer-substring-no-properties
124 (match-end 0) (std11-field-end)))
126 (setq s-rest (cdr s-rest)
131 (defun std11-header-string (regexp &optional boundary)
132 "Return string of message header fields matched by REGEXP.
133 If BOUNDARY is not nil, it is used as message header separator."
134 (let ((case-fold-search t))
137 (std11-narrow-to-header boundary)
138 (goto-char (point-min))
140 (while (re-search-forward std11-field-head-regexp nil t)
142 (buffer-substring (match-beginning 0) (std11-field-end)))
143 (if (string-match regexp field)
144 (setq header (concat header field "\n"))
149 (defun std11-header-string-except (regexp &optional boundary)
150 "Return string of message header fields not matched by REGEXP.
151 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 (not (string-match regexp field))
162 (setq header (concat header field "\n"))
167 (defun std11-collect-field-names (&optional boundary)
168 "Return list of all field-names of the message header in current buffer.
169 If BOUNDARY is not nil, it is used as message header separator."
172 (std11-narrow-to-header boundary)
173 (goto-char (point-min))
175 (while (re-search-forward std11-field-head-regexp nil t)
176 (setq name (buffer-substring-no-properties
177 (match-beginning 0)(1- (match-end 0))))
178 (or (member name dest)
179 (setq dest (cons name dest))
189 (defun std11-unfold-string (string)
190 "Unfold STRING as message header field."
193 (while (string-match "\n\\([ \t]\\)" string p)
194 (setq dest (concat dest
195 (substring string p (match-beginning 0))
198 (setq p (match-end 0)))
201 (concat dest (substring string p))
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."
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."
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."
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 (defcustom std11-lexical-analyzer
267 '(std11-analyze-quoted-string
268 std11-analyze-domain-literal
269 std11-analyze-comment
271 std11-analyze-special
273 "*List of functions to return result of lexical analyze.
274 Each function must have two arguments: STRING and START.
275 STRING is the target string to be analyzed.
276 START is start position of STRING to analyze.
278 Previous function is preferred to next function. If a function
279 returns nil, next function is used. Otherwise the return value will
283 :type '(repeat function))
286 (defconst std11-space-char-list '(? ?\t ?\n))
287 (defconst std11-special-char-list '(?\] ?\[
292 ;; (defconst std11-spaces-regexp
293 ;; (eval-when-compile (concat "[" std11-space-char-list "]+")))
294 (defconst std11-atom-regexp
296 (concat "[^" std11-special-char-list std11-space-char-list "]+")))
298 (defun std11-analyze-spaces (string start)
299 (if (and (string-match (eval-when-compile
300 (concat "[" std11-space-char-list "]+"))
302 (= (match-beginning 0) start))
303 (let ((end (match-end 0)))
304 (cons (cons 'spaces (substring string start end))
305 ;;(substring string end)
309 (defun std11-analyze-special (string start)
310 (if (and (> (length string) start)
311 (memq (aref string start) std11-special-char-list))
312 (cons (cons 'specials (substring string start (1+ start)))
313 ;;(substring string 1)
317 (defun std11-analyze-atom (string start)
318 (if (and (string-match std11-atom-regexp string start)
319 (= (match-beginning 0) start))
320 (let ((end (match-end 0)))
321 (cons (cons 'atom (substring string start end))
322 ;;(substring string end)
326 (defun std11-check-enclosure (string open close &optional recursive from)
327 (let ((len (length string))
331 (eq (aref string i) open))
336 (setq chr (aref string i))
349 (setq p (std11-check-enclosure
350 string open close recursive i))
360 (defun std11-analyze-quoted-string (string start)
361 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
363 (cons (cons 'quoted-string (substring string (1+ start) (1- p)))
364 ;;(substring string p))
368 (defun std11-analyze-domain-literal (string start)
369 (let ((p (std11-check-enclosure string ?\[ ?\] nil start)))
371 (cons (cons 'domain-literal (substring string (1+ start) (1- p)))
372 ;;(substring string p))
376 (defun std11-analyze-comment (string start)
377 (let ((p (std11-check-enclosure string ?\( ?\) t start)))
379 (cons (cons 'comment (substring string (1+ start) (1- p)))
380 ;;(substring string p))
385 (defun std11-lexical-analyze (string &optional analyzer start)
386 "Analyze STRING as lexical tokens of STD 11."
388 (setq analyzer std11-lexical-analyzer))
391 (let ((len (length string))
395 (let ((rest analyzer)
397 (while (and (setq func (car rest))
398 (null (setq r (funcall func string start))))
399 (setq rest (cdr rest)))
401 (list (cons 'error (substring string start)) (1+ len)))
403 (setq dest (cons (car ret) dest)
413 (defun std11-ignored-token-p (token)
414 (let ((type (car token)))
415 (or (eq type 'spaces)(eq type 'comment))
418 (defun std11-parse-token (lal)
422 (setq token (car lal))
423 (std11-ignored-token-p token)
426 (setq itl (cons token itl))
428 (cons (nreverse (cons token itl))
432 (defun std11-parse-ascii-token (lal)
433 (let (token itl parsed token-value)
435 (setq token (car lal))
436 (or (std11-ignored-token-p token)
437 (if (and (setq token-value (cdr token))
438 (find-non-ascii-charset-string token-value))
442 (setq itl (cons token itl))
445 (setq parsed (nreverse (cons token itl)))
447 (cons parsed (cdr lal))
450 (defun std11-parse-token-or-comment (lal)
454 (setq token (car lal))
455 (eq (car token) 'spaces)
458 (setq itl (cons token itl))
460 (cons (nreverse (cons token itl))
464 (defun std11-parse-word (lal)
465 (let ((ret (std11-parse-ascii-token lal)))
467 (let ((elt (car ret))
470 (if (or (assq 'atom elt)
471 (assq 'quoted-string elt))
472 (cons (cons 'word elt) rest)
475 (defun std11-parse-word-or-comment (lal)
476 (let ((ret (std11-parse-token-or-comment lal)))
478 (let ((elt (car ret))
481 (cond ((or (assq 'atom elt)
482 (assq 'quoted-string elt))
483 (cons (cons 'word elt) rest)
486 (cons (cons 'comment-word elt) rest)
490 (defun std11-parse-phrase (lal)
492 (while (setq ret (std11-parse-word-or-comment lal))
493 (setq phrase (append phrase (cdr (car ret))))
497 (cons (cons 'phrase phrase) lal)
500 (defun std11-parse-local-part (lal)
501 (let ((ret (std11-parse-word lal)))
503 (let ((local-part (cdr (car ret))) dot)
505 (while (and (setq ret (std11-parse-ascii-token lal))
507 (string-equal (cdr (assq 'specials dot)) ".")
508 (setq ret (std11-parse-word (cdr ret)))
510 (append local-part dot (cdr (car ret)))
514 (cons (cons 'local-part local-part) lal)
517 (defun std11-parse-sub-domain (lal)
518 (let ((ret (std11-parse-ascii-token lal)))
520 (let ((sub-domain (car ret)))
521 (if (or (assq 'atom sub-domain)
522 (assq 'domain-literal sub-domain)
524 (cons (cons 'sub-domain sub-domain)
529 (defun std11-parse-domain (lal)
530 (let ((ret (std11-parse-sub-domain lal)))
532 (let ((domain (cdr (car ret))) dot)
534 (while (and (setq ret (std11-parse-ascii-token lal))
536 (string-equal (cdr (assq 'specials dot)) ".")
537 (setq ret (std11-parse-sub-domain (cdr ret)))
539 (append domain dot (cdr (car ret)))
543 (cons (cons 'domain domain) lal)
546 (defun std11-parse-at-domain (lal)
547 (let ((ret (std11-parse-ascii-token lal)) at-sign)
549 (setq at-sign (car ret))
550 (string-equal (cdr (assq 'specials at-sign)) "@")
551 (setq ret (std11-parse-domain (cdr ret)))
553 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
557 (defun std11-parse-addr-spec (lal)
558 (let ((ret (std11-parse-local-part lal))
562 (setq addr (cdr (car ret)))
564 (and (setq ret (std11-parse-at-domain lal))
565 (setq addr (append addr (cdr (car ret))))
568 (cons (cons 'addr-spec addr) lal)
571 (defun std11-parse-route (lal)
572 (let ((ret (std11-parse-at-domain lal))
576 (setq route (cdr (car ret)))
578 (while (and (setq ret (std11-parse-ascii-token lal))
579 (setq comma (car ret))
580 (string-equal (cdr (assq 'specials comma)) ",")
581 (setq ret (std11-parse-at-domain (cdr ret)))
583 (setq route (append route comma (cdr (car ret))))
586 (and (setq ret (std11-parse-ascii-token lal))
587 (setq colon (car ret))
588 (string-equal (cdr (assq 'specials colon)) ":")
589 (setq route (append route colon))
592 (cons (cons 'route route)
597 (defun std11-parse-route-addr (lal)
598 (let ((ret (std11-parse-ascii-token lal))
602 (string-equal (cdr (assq 'specials <)) "<")
604 (progn (and (setq ret (std11-parse-route lal))
605 (setq route (cdr (car ret)))
608 (setq ret (std11-parse-addr-spec lal))
610 (setq addr-spec (cdr (car ret)))
612 (setq ret (std11-parse-ascii-token lal))
614 (string-equal (cdr (assq 'specials >)) ">")
616 (cons (cons 'route-addr (append route addr-spec))
621 (defun std11-parse-phrase-route-addr (lal)
622 (let ((ret (std11-parse-phrase lal)) phrase)
625 (setq phrase (cdr (car ret)))
628 (if (setq ret (std11-parse-route-addr lal))
629 (cons (list 'phrase-route-addr
635 (defun std11-parse-mailbox (lal)
636 (let ((ret (or (std11-parse-phrase-route-addr lal)
637 (std11-parse-addr-spec lal)))
641 (setq mbox (car ret))
643 (if (and (setq ret (std11-parse-token-or-comment lal))
644 (setq comment (cdr (assq 'comment (car ret))))
648 (cons (list 'mailbox mbox comment)
652 (defun std11-parse-group (lal)
653 (let ((ret (std11-parse-phrase lal))
654 phrase colon comma mbox semicolon)
656 (setq phrase (cdr (car ret)))
658 (setq ret (std11-parse-ascii-token lal))
659 (setq colon (car ret))
660 (string-equal (cdr (assq 'specials colon)) ":")
663 (and (setq ret (std11-parse-mailbox lal))
664 (setq mbox (list (car ret)))
667 (while (and (setq ret (std11-parse-ascii-token lal))
668 (setq comma (car ret))
670 (cdr (assq 'specials comma)) ",")
672 (setq ret (std11-parse-mailbox lal))
673 (setq mbox (cons (car ret) mbox))
677 (and (setq ret (std11-parse-ascii-token lal))
678 (setq semicolon (car ret))
679 (string-equal (cdr (assq 'specials semicolon)) ";")
681 (cons (list 'group phrase (nreverse mbox))
686 (defun std11-parse-address (lal)
687 (or (std11-parse-group lal)
688 (std11-parse-mailbox lal)
691 (defun std11-parse-addresses (lal)
692 (let ((ret (std11-parse-address lal)))
694 (let ((dest (list (car ret))))
696 (while (and (setq ret (std11-parse-ascii-token lal))
697 (string-equal (cdr (assq 'specials (car ret))) ",")
698 (setq ret (std11-parse-address (cdr ret)))
700 (setq dest (cons (car ret) dest))
706 (defun std11-parse-msg-id (lal)
707 (let ((ret (std11-parse-ascii-token lal))
711 (string-equal (cdr (assq 'specials <)) "<")
713 (setq ret (std11-parse-addr-spec lal))
714 (setq addr-spec (car ret))
716 (setq ret (std11-parse-ascii-token lal))
718 (string-equal (cdr (assq 'specials >)) ">")
720 (cons (cons 'msg-id (cdr addr-spec))
724 (defun std11-parse-msg-ids (tokens)
725 "Parse lexical TOKENS as `*(phrase / msg-id)', and return the result."
726 (let ((ret (or (std11-parse-msg-id tokens)
727 (std11-parse-phrase tokens))))
729 (let ((dest (list (car ret))))
730 (setq tokens (cdr ret))
731 (while (setq ret (or (std11-parse-msg-id tokens)
732 (std11-parse-phrase tokens)))
733 (setq dest (cons (car ret) dest))
734 (setq tokens (cdr ret))
739 (defalias 'std11-parse-in-reply-to 'std11-parse-msg-ids)
740 (make-obsolete 'std11-parse-in-reply-to 'std11-parse-msg-ids)
746 (defun std11-addr-to-string (seq)
747 "Return string from lexical analyzed list SEQ
748 represents addr-spec of RFC 822."
751 (let ((name (car token)))
753 ((eq name 'spaces) "")
754 ((eq name 'comment) "")
755 ((eq name 'quoted-string)
756 (concat "\"" (cdr token) "\""))
763 (defun std11-address-string (address)
764 "Return string of address part from parsed ADDRESS of RFC 822."
765 (cond ((eq (car address) 'group)
766 (mapconcat (function std11-address-string)
770 ((eq (car address) 'mailbox)
771 (let ((addr (nth 1 address)))
772 (std11-addr-to-string
773 (if (eq (car addr) 'phrase-route-addr)
779 (defun std11-comment-value-to-string (value)
781 (std11-strip-quoted-pair value)
786 (if (stringp (car value))
789 (std11-comment-value-to-string
798 (defun std11-full-name-string (address)
799 "Return string of full-name part from parsed ADDRESS of RFC 822."
800 (cond ((eq (car address) 'group)
807 ((eq (car address) 'mailbox)
808 (let ((addr (nth 1 address))
809 (comment (nth 2 address))
811 (if (eq (car addr) 'phrase-route-addr)
816 (let ((type (car token)))
817 (cond ((eq type 'quoted-string)
818 (std11-strip-quoted-pair (cdr token))
822 (std11-comment-value-to-string
831 (cond ((> (length phrase) 0) phrase)
832 (comment (std11-comment-value-to-string comment))
837 (defun std11-msg-id-string (msg-id)
838 "Return string from parsed MSG-ID of RFC 822."
839 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
843 (defun std11-fill-msg-id-list-string (string &optional column)
844 "Fill list of msg-id in STRING, and return the result."
847 (let ((lal (std11-lexical-analyze string))
849 (let ((ret (std11-parse-msg-id lal)))
851 (let* ((str (std11-msg-id-string (car ret)))
854 (if (> (+ len column) 76)
855 (setq dest (concat dest "\n " str)
858 column (+ column len))
860 (setq dest (concat dest (cdr (car lal)))
864 (let ((ret (std11-parse-msg-id lal)))
866 (let* ((str (std11-msg-id-string (car ret)))
867 (len (1+ (length str))))
869 (if (> (+ len column) 76)
870 (setq dest (concat dest "\n " str)
872 (setq dest (concat dest " " str)
873 column (+ column len))
875 (setq dest (concat dest (cdr (car lal)))
881 ;;; @ parser with lexical analyzer
885 (defun std11-parse-address-string (string)
886 "Parse STRING as mail address."
887 (std11-parse-address (std11-lexical-analyze string))
891 (defun std11-parse-addresses-string (string)
892 "Parse STRING as mail address list."
893 (std11-parse-addresses (std11-lexical-analyze string))
897 (defun std11-parse-msg-id-string (string)
898 "Parse STRING as msg-id."
899 (std11-parse-msg-id (std11-lexical-analyze string))
903 (defun std11-parse-msg-ids-string (string)
904 "Parse STRING as `*(phrase / msg-id)'."
905 (std11-parse-msg-ids (std11-lexical-analyze string))
909 (defun std11-extract-address-components (string)
910 "Extract full name and canonical address from STRING.
911 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
912 If no name can be extracted, FULL-NAME will be nil."
913 (let* ((structure (car (std11-parse-address-string
914 (std11-unfold-string string))))
915 (phrase (std11-full-name-string structure))
916 (address (std11-address-string structure))
918 (list phrase address)
927 ;;; std11.el ends here