1 ;;; std11.el --- STD 11 functions for GNU Emacs
3 ;; Copyright (C) 1995,96,97,98,99,2000,01,02 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)))
50 (defun std11-fetch-field (name)
51 "Return the value of the header field NAME.
52 The buffer is expected to be narrowed to just the headers of the message."
54 (goto-char (point-min))
55 (let ((case-fold-search t))
56 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
57 (buffer-substring-no-properties (match-end 0) (std11-field-end))
61 (defun std11-narrow-to-header (&optional boundary)
62 "Narrow to the message header.
63 If BOUNDARY is not nil, it is used as message header separator."
65 (goto-char (point-min))
66 (if (re-search-forward
67 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
74 (defun std11-field-body (name &optional boundary)
75 "Return the value of the header field NAME.
76 If BOUNDARY is not nil, it is used as message header separator."
79 (inline (std11-narrow-to-header boundary)
80 (std11-fetch-field name))
83 (defun std11-find-field-body (field-names &optional boundary)
84 "Return the first found field-body specified by FIELD-NAMES
85 of the message header in current buffer. If BOUNDARY is not nil, it is
86 used as message header separator."
89 (std11-narrow-to-header boundary)
90 (let ((case-fold-search t)
93 (while (setq field-name (car field-names))
94 (goto-char (point-min))
95 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
97 (buffer-substring-no-properties
98 (match-end 0) (std11-field-end)))
100 (setq field-names (cdr field-names))
103 (defun std11-field-bodies (field-names &optional default-value boundary)
104 "Return list of each field-bodies of FIELD-NAMES of the message header
105 in current buffer. If BOUNDARY is not nil, it is used as message
109 (std11-narrow-to-header boundary)
110 (let* ((case-fold-search t)
111 (dest (make-list (length field-names) default-value))
115 (while (setq field-name (car s-rest))
116 (goto-char (point-min))
117 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
119 (buffer-substring-no-properties
120 (match-end 0) (std11-field-end)))
122 (setq s-rest (cdr s-rest)
127 (defun std11-header-string (regexp &optional boundary)
128 "Return string of message header fields matched by REGEXP.
129 If BOUNDARY is not nil, it is used as message header separator."
130 (let ((case-fold-search t))
133 (std11-narrow-to-header boundary)
134 (goto-char (point-min))
136 (while (re-search-forward std11-field-head-regexp nil t)
138 (buffer-substring (match-beginning 0) (std11-field-end)))
139 (if (string-match regexp field)
140 (setq header (concat header field "\n"))
145 (defun std11-header-string-except (regexp &optional boundary)
146 "Return string of message header fields not matched by REGEXP.
147 If BOUNDARY is not nil, it is used as message header separator."
148 (let ((case-fold-search t))
151 (std11-narrow-to-header boundary)
152 (goto-char (point-min))
154 (while (re-search-forward std11-field-head-regexp nil t)
156 (buffer-substring (match-beginning 0) (std11-field-end)))
157 (if (not (string-match regexp field))
158 (setq header (concat header field "\n"))
163 (defun std11-collect-field-names (&optional boundary)
164 "Return list of all field-names of the message header in current buffer.
165 If BOUNDARY is not nil, it is used as message header separator."
168 (std11-narrow-to-header boundary)
169 (goto-char (point-min))
171 (while (re-search-forward std11-field-head-regexp nil t)
172 (setq name (buffer-substring-no-properties
173 (match-beginning 0)(1- (match-end 0))))
174 (or (member name dest)
175 (setq dest (cons name dest))
185 (defun std11-unfold-string (string)
186 "Unfold STRING as message header field."
189 (while (string-match "\n\\([ \t]\\)" string p)
190 (setq dest (concat dest
191 (substring string p (match-beginning 0))
194 (setq p (match-end 0)))
197 (concat dest (substring string p))
204 (defun std11-wrap-as-quoted-pairs (string specials)
208 (len (length string))
211 (let ((chr (aref string i)))
212 (if (memq chr specials)
213 (setq dest (concat dest (substring string b i) "\\")
218 (concat dest (substring string b))
221 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
223 (defun std11-wrap-as-quoted-string (string)
224 "Wrap STRING as RFC 822 quoted-string."
226 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
229 (defun std11-strip-quoted-pair (string)
230 "Strip quoted-pairs in STRING."
234 (len (length string))
237 (let ((chr (aref string i)))
239 (setq dest (concat dest (substring string b i))
244 (concat dest (substring string b))
247 (defun std11-strip-quoted-string (string)
248 "Strip quoted-string STRING."
249 (let ((len (length string)))
251 (let ((max (1- len)))
252 (and (eq (aref string 0) ?\")
253 (eq (aref string max) ?\")
254 (std11-strip-quoted-pair (substring string 1 max))
259 ;;; @ lexical analyze
262 (defcustom std11-lexical-analyzer
263 '(std11-analyze-quoted-string
264 std11-analyze-domain-literal
265 std11-analyze-comment
267 std11-analyze-special
269 "*List of functions to return result of lexical analyze.
270 Each function must have two arguments: STRING and START.
271 STRING is the target string to be analyzed.
272 START is start position of STRING to analyze.
274 Previous function is preferred to next function. If a function
275 returns nil, next function is used. Otherwise the return value will
279 :type '(repeat function))
282 (defconst std11-space-char-list '(? ?\t ?\n))
283 (defconst std11-special-char-list '(?\] ?\[
288 ;; (defconst std11-spaces-regexp
289 ;; (eval-when-compile (concat "[" std11-space-char-list "]+")))
291 (defconst std11-non-atom-regexp
293 (concat "[" std11-special-char-list std11-space-char-list "]")))
295 (defconst std11-atom-regexp
297 (concat "[^" std11-special-char-list std11-space-char-list "]+")))
299 (defun std11-analyze-spaces (string start)
300 (if (and (string-match (eval-when-compile
301 (concat "[" std11-space-char-list "]+"))
303 (= (match-beginning 0) start))
304 (let ((end (match-end 0)))
305 (cons (cons 'spaces (substring string start end))
306 ;;(substring string end)
310 (defun std11-analyze-special (string start)
311 (if (and (> (length string) start)
312 (memq (aref string start) std11-special-char-list))
313 (cons (cons 'specials (substring string start (1+ start)))
314 ;;(substring string 1)
318 (defun std11-analyze-atom (string start)
319 (if (string-match std11-non-atom-regexp string start)
320 (if (> (match-beginning 0) start)
321 (cons (cons 'atom (substring string start (match-beginning 0)))
324 (cons (cons 'atom (substring string start))
326 ;; (if (and (string-match std11-atom-regexp string start)
327 ;; (= (match-beginning 0) start))
328 ;; (let ((end (match-end 0)))
329 ;; (cons (cons 'atom (substring string start end))
330 ;; ;;(substring string end)
335 (defun std11-check-enclosure (string open close &optional recursive from)
336 (let ((len (length string))
340 (eq (aref string i) open))
345 (setq chr (aref string i))
358 (setq p (std11-check-enclosure
359 string open close recursive i))
369 (defun std11-analyze-quoted-string (string start)
370 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
372 (cons (cons 'quoted-string (substring string (1+ start) (1- p)))
373 ;;(substring string p))
377 (defun std11-analyze-domain-literal (string start)
378 (let ((p (std11-check-enclosure string ?\[ ?\] nil start)))
380 (cons (cons 'domain-literal (substring string (1+ start) (1- p)))
381 ;;(substring string p))
385 (defun std11-analyze-comment (string start)
386 (let ((p (std11-check-enclosure string ?\( ?\) t start)))
388 (cons (cons 'comment (substring string (1+ start) (1- p)))
389 ;;(substring string p))
394 (defun std11-lexical-analyze (string &optional analyzer start)
395 "Analyze STRING as lexical tokens of STD 11."
397 (setq analyzer std11-lexical-analyzer))
400 (let ((len (length string))
404 (let ((rest analyzer)
406 (while (and (setq func (car rest))
407 (null (setq r (funcall func string start))))
408 (setq rest (cdr rest)))
410 (cons (cons 'error (substring string start)) (1+ len)))
412 (setq dest (cons (car ret) dest)
422 (defun std11-ignored-token-p (token)
423 (let ((type (car token)))
424 (or (eq type 'spaces)(eq type 'comment))
427 (defun std11-parse-token (lal)
431 (setq token (car lal))
432 (std11-ignored-token-p token)
435 (setq itl (cons token itl))
437 (cons (nreverse (cons token itl))
441 (defun std11-parse-ascii-token (lal)
442 (let (token itl parsed token-value)
444 (setq token (car lal))
445 (or (std11-ignored-token-p token)
446 (if (and (setq token-value (cdr token))
447 (delq 'ascii (find-charset-string token-value)))
451 (setq itl (cons token itl))
454 (setq parsed (nreverse (cons token itl)))
456 (cons parsed (cdr lal))
459 (defun std11-parse-token-or-comment (lal)
463 (setq token (car lal))
464 (eq (car token) 'spaces)
467 (setq itl (cons token itl))
469 (cons (nreverse (cons token itl))
473 (defun std11-parse-word (lal)
474 (let ((ret (std11-parse-ascii-token lal)))
476 (let ((elt (car ret))
479 (if (or (assq 'atom elt)
480 (assq 'quoted-string elt))
481 (cons (cons 'word elt) rest)
484 (defun std11-parse-word-or-comment (lal)
485 (let ((ret (std11-parse-token-or-comment lal)))
487 (let ((elt (car ret))
490 (cond ((or (assq 'atom elt)
491 (assq 'quoted-string elt))
492 (cons (cons 'word elt) rest)
495 (cons (cons 'comment-word elt) rest)
499 (defun std11-parse-phrase (lal)
501 (while (setq ret (std11-parse-word-or-comment lal))
502 (setq phrase (append phrase (cdr (car ret))))
506 (cons (cons 'phrase phrase) lal)
509 (defun std11-parse-local-part (lal)
510 (let ((ret (std11-parse-word lal)))
512 (let ((local-part (cdr (car ret))) dot)
514 (while (and (setq ret (std11-parse-ascii-token lal))
516 (string-equal (cdr (assq 'specials dot)) ".")
517 (setq ret (std11-parse-word (cdr ret)))
519 (append local-part dot (cdr (car ret)))
523 (cons (cons 'local-part local-part) lal)
526 (defun std11-parse-sub-domain (lal)
527 (let ((ret (std11-parse-ascii-token lal)))
529 (let ((sub-domain (car ret)))
530 (if (or (assq 'atom sub-domain)
531 (assq 'domain-literal sub-domain)
533 (cons (cons 'sub-domain sub-domain)
538 (defun std11-parse-domain (lal)
539 (let ((ret (std11-parse-sub-domain lal)))
541 (let ((domain (cdr (car ret))) dot)
543 (while (and (setq ret (std11-parse-ascii-token lal))
545 (string-equal (cdr (assq 'specials dot)) ".")
546 (setq ret (std11-parse-sub-domain (cdr ret)))
548 (append domain dot (cdr (car ret)))
552 (cons (cons 'domain domain) lal)
555 (defun std11-parse-at-domain (lal)
556 (let ((ret (std11-parse-ascii-token lal)) at-sign)
558 (setq at-sign (car ret))
559 (string-equal (cdr (assq 'specials at-sign)) "@")
560 (setq ret (std11-parse-domain (cdr ret)))
562 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
566 (defun std11-parse-addr-spec (lal)
567 (let ((ret (std11-parse-local-part lal))
571 (setq addr (cdr (car ret)))
573 (and (setq ret (std11-parse-at-domain lal))
574 (setq addr (append addr (cdr (car ret))))
577 (cons (cons 'addr-spec addr) lal)
580 (defun std11-parse-route (lal)
581 (let ((ret (std11-parse-at-domain lal))
585 (setq route (cdr (car ret)))
587 (while (and (setq ret (std11-parse-ascii-token lal))
588 (setq comma (car ret))
589 (string-equal (cdr (assq 'specials comma)) ",")
590 (setq ret (std11-parse-at-domain (cdr ret)))
592 (setq route (append route comma (cdr (car ret))))
595 (and (setq ret (std11-parse-ascii-token lal))
596 (setq colon (car ret))
597 (string-equal (cdr (assq 'specials colon)) ":")
598 (setq route (append route colon))
601 (cons (cons 'route route)
606 (defun std11-parse-route-addr (lal)
607 (let ((ret (std11-parse-ascii-token lal))
611 (string-equal (cdr (assq 'specials <)) "<")
613 (progn (and (setq ret (std11-parse-route lal))
614 (setq route (cdr (car ret)))
617 (setq ret (std11-parse-addr-spec lal))
619 (setq addr-spec (cdr (car ret)))
621 (setq ret (std11-parse-ascii-token lal))
623 (string-equal (cdr (assq 'specials >)) ">")
625 (cons (cons 'route-addr (append route addr-spec))
630 (defun std11-parse-phrase-route-addr (lal)
631 (let ((ret (std11-parse-phrase lal)) phrase)
634 (setq phrase (cdr (car ret)))
637 (if (setq ret (std11-parse-route-addr lal))
638 (cons (list 'phrase-route-addr
644 (defun std11-parse-mailbox (lal)
645 (let ((ret (or (std11-parse-phrase-route-addr lal)
646 (std11-parse-addr-spec lal)))
650 (setq mbox (car ret))
652 (if (and (setq ret (std11-parse-token-or-comment lal))
653 (setq comment (cdr (assq 'comment (car ret))))
657 (cons (list 'mailbox mbox comment)
661 (defun std11-parse-group (lal)
662 (let ((ret (std11-parse-phrase lal))
663 phrase colon comma mbox semicolon)
665 (setq phrase (cdr (car ret)))
667 (setq ret (std11-parse-ascii-token lal))
668 (setq colon (car ret))
669 (string-equal (cdr (assq 'specials colon)) ":")
672 (and (setq ret (std11-parse-mailbox lal))
673 (setq mbox (list (car ret)))
676 (while (and (setq ret (std11-parse-ascii-token lal))
677 (setq comma (car ret))
679 (cdr (assq 'specials comma)) ",")
681 (setq ret (std11-parse-mailbox lal))
682 (setq mbox (cons (car ret) mbox))
686 (and (setq ret (std11-parse-ascii-token lal))
687 (setq semicolon (car ret))
688 (string-equal (cdr (assq 'specials semicolon)) ";")
690 (cons (list 'group phrase (nreverse mbox))
695 (defun std11-parse-address (lal)
696 (or (std11-parse-group lal)
697 (std11-parse-mailbox lal)
700 (defun std11-parse-addresses (lal)
701 (let ((ret (std11-parse-address lal)))
703 (let ((dest (list (car ret))))
705 (while (and (setq ret (std11-parse-ascii-token lal))
706 (string-equal (cdr (assq 'specials (car ret))) ",")
707 (setq ret (std11-parse-address (cdr ret)))
709 (setq dest (cons (car ret) dest))
715 (defun std11-parse-msg-id (lal)
716 (let ((ret (std11-parse-ascii-token lal))
720 (string-equal (cdr (assq 'specials <)) "<")
722 (setq ret (std11-parse-addr-spec lal))
723 (setq addr-spec (car ret))
725 (setq ret (std11-parse-ascii-token lal))
727 (string-equal (cdr (assq 'specials >)) ">")
729 (cons (cons 'msg-id (cdr addr-spec))
733 (defun std11-parse-msg-ids (tokens)
734 "Parse lexical TOKENS as `*(phrase / msg-id)', and return the result."
735 (let ((ret (or (std11-parse-msg-id tokens)
736 (std11-parse-phrase tokens))))
738 (let ((dest (list (car ret))))
739 (setq tokens (cdr ret))
740 (while (setq ret (or (std11-parse-msg-id tokens)
741 (std11-parse-phrase tokens)))
742 (setq dest (cons (car ret) dest))
743 (setq tokens (cdr ret))
748 (defalias 'std11-parse-in-reply-to 'std11-parse-msg-ids)
749 (make-obsolete 'std11-parse-in-reply-to 'std11-parse-msg-ids)
755 (defun std11-addr-to-string (seq)
756 "Return string from lexical analyzed list SEQ
757 represents addr-spec of RFC 822."
760 (let ((name (car token)))
762 ((eq name 'spaces) "")
763 ((eq name 'comment) "")
764 ((eq name 'quoted-string)
765 (concat "\"" (cdr token) "\""))
772 (defun std11-address-string (address)
773 "Return string of address part from parsed ADDRESS of RFC 822."
774 (cond ((eq (car address) 'group)
775 (mapconcat (function std11-address-string)
779 ((eq (car address) 'mailbox)
780 (let ((addr (nth 1 address)))
781 (std11-addr-to-string
782 (if (eq (car addr) 'phrase-route-addr)
788 (defun std11-comment-value-to-string (value)
790 (std11-strip-quoted-pair value)
795 (if (stringp (car value))
798 (std11-comment-value-to-string
807 (defun std11-full-name-string (address)
808 "Return string of full-name part from parsed ADDRESS of RFC 822."
809 (cond ((eq (car address) 'group)
816 ((eq (car address) 'mailbox)
817 (let ((addr (nth 1 address))
818 (comment (nth 2 address))
820 (if (eq (car addr) 'phrase-route-addr)
825 (let ((type (car token)))
826 (cond ((eq type 'quoted-string)
827 (std11-strip-quoted-pair (cdr token))
831 (std11-comment-value-to-string
840 (cond ((> (length phrase) 0) phrase)
841 (comment (std11-comment-value-to-string comment))
846 (defun std11-msg-id-string (msg-id)
847 "Return string from parsed MSG-ID of RFC 822."
848 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
852 (defun std11-fill-msg-id-list-string (string &optional column)
853 "Fill list of msg-id in STRING, and return the result."
856 (let ((lal (std11-lexical-analyze string))
858 (let ((ret (std11-parse-msg-id lal)))
860 (let* ((str (std11-msg-id-string (car ret)))
863 (if (> (+ len column) 76)
864 (setq dest (concat dest "\n " str)
867 column (+ column len))
869 (setq dest (concat dest (cdr (car lal)))
873 (let ((ret (std11-parse-msg-id lal)))
875 (let* ((str (std11-msg-id-string (car ret)))
876 (len (1+ (length str))))
878 (if (> (+ len column) 76)
879 (setq dest (concat dest "\n " str)
881 (setq dest (concat dest " " str)
882 column (+ column len))
884 (setq dest (concat dest (cdr (car lal)))
890 ;;; @ parser with lexical analyzer
894 (defun std11-parse-address-string (string)
895 "Parse STRING as mail address."
896 (std11-parse-address (std11-lexical-analyze string))
900 (defun std11-parse-addresses-string (string)
901 "Parse STRING as mail address list."
902 (std11-parse-addresses (std11-lexical-analyze string))
906 (defun std11-parse-msg-id-string (string)
907 "Parse STRING as msg-id."
908 (std11-parse-msg-id (std11-lexical-analyze string))
912 (defun std11-parse-msg-ids-string (string)
913 "Parse STRING as `*(phrase / msg-id)'."
914 (std11-parse-msg-ids (std11-lexical-analyze string))
918 (defun std11-extract-address-components (string)
919 "Extract full name and canonical address from STRING.
920 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
921 If no name can be extracted, FULL-NAME will be nil."
922 (let* ((structure (car (std11-parse-address-string
923 (std11-unfold-string string))))
924 (phrase (std11-full-name-string structure))
925 (address (std11-address-string structure))
927 (list phrase address)
936 ;;; std11.el ends here