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 'custom) ; std11-lexical-analyzer
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 (&optional bound)
41 "Move to end of field and return this point.
42 The optional argument BOUNDs the search; it is a buffer position."
43 (if (re-search-forward std11-next-field-head-regexp bound t)
44 (goto-char (match-beginning 0))
45 (if (re-search-forward "^$" bound t)
46 (goto-char (1- (match-beginning 0)))
53 (defun std11-fetch-field (name)
54 "Return the value of the header field NAME.
55 The buffer is expected to be narrowed to just the headers of the message."
57 (goto-char (point-min))
58 (let ((case-fold-search t))
59 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
60 (buffer-substring-no-properties (match-end 0) (std11-field-end))
64 (defun std11-narrow-to-header (&optional boundary)
65 "Narrow to the message header.
66 If BOUNDARY is not nil, it is used as message header separator."
68 (goto-char (point-min))
69 (if (re-search-forward
70 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
77 (defun std11-field-body (name &optional boundary)
78 "Return the value of the header field NAME.
79 If BOUNDARY is not nil, it is used as message header separator."
82 (inline (std11-narrow-to-header boundary)
83 (std11-fetch-field name))
86 (defun std11-find-field-body (field-names &optional boundary)
87 "Return the first found field-body specified by FIELD-NAMES
88 of the message header in current buffer. If BOUNDARY is not nil, it is
89 used as message header separator."
92 (std11-narrow-to-header boundary)
93 (let ((case-fold-search t)
96 (while (setq field-name (car field-names))
97 (goto-char (point-min))
98 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
100 (buffer-substring-no-properties
101 (match-end 0) (std11-field-end)))
103 (setq field-names (cdr field-names))
106 (defun std11-field-bodies (field-names &optional default-value boundary)
107 "Return list of each field-bodies of FIELD-NAMES of the message header
108 in current buffer. If BOUNDARY is not nil, it is used as message
112 (std11-narrow-to-header boundary)
113 (let* ((case-fold-search t)
114 (dest (make-list (length field-names) default-value))
118 (while (setq field-name (car s-rest))
119 (goto-char (point-min))
120 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
122 (buffer-substring-no-properties
123 (match-end 0) (std11-field-end)))
125 (setq s-rest (cdr s-rest)
130 (defun std11-header-string (regexp &optional boundary)
131 "Return string of message header fields matched by REGEXP.
132 If BOUNDARY is not nil, it is used as message header separator."
133 (let ((case-fold-search t))
136 (std11-narrow-to-header boundary)
137 (goto-char (point-min))
139 (while (re-search-forward std11-field-head-regexp nil t)
141 (buffer-substring (match-beginning 0) (std11-field-end)))
142 (if (string-match regexp field)
143 (setq header (concat header field "\n"))
148 (defun std11-header-string-except (regexp &optional boundary)
149 "Return string of message header fields not matched by REGEXP.
150 If BOUNDARY is not nil, it is used as message header separator."
151 (let ((case-fold-search t))
154 (std11-narrow-to-header boundary)
155 (goto-char (point-min))
157 (while (re-search-forward std11-field-head-regexp nil t)
159 (buffer-substring (match-beginning 0) (std11-field-end)))
160 (if (not (string-match regexp field))
161 (setq header (concat header field "\n"))
166 (defun std11-collect-field-names (&optional boundary)
167 "Return list of all field-names of the message header in current buffer.
168 If BOUNDARY is not nil, it is used as message header separator."
171 (std11-narrow-to-header boundary)
172 (goto-char (point-min))
174 (while (re-search-forward std11-field-head-regexp nil t)
175 (setq name (buffer-substring-no-properties
176 (match-beginning 0)(1- (match-end 0))))
177 (or (member name dest)
178 (setq dest (cons name dest))
188 (defun std11-unfold-string (string)
189 "Unfold STRING as message header field."
192 (while (string-match "\n\\([ \t]\\)" string p)
193 (setq dest (concat dest
194 (substring string p (match-beginning 0))
197 (setq p (match-end 0)))
200 (concat dest (substring string p))
207 (defun std11-wrap-as-quoted-pairs (string specials)
211 (len (length string))
214 (let ((chr (aref string i)))
215 (if (memq chr specials)
216 (setq dest (concat dest (substring string b i) "\\")
221 (concat dest (substring string b))
224 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
226 (defun std11-wrap-as-quoted-string (string)
227 "Wrap STRING as RFC 822 quoted-string."
229 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
232 (defun std11-strip-quoted-pair (string)
233 "Strip quoted-pairs in STRING."
237 (len (length string))
240 (let ((chr (aref string i)))
242 (setq dest (concat dest (substring string b i))
247 (concat dest (substring string b))
250 (defun std11-strip-quoted-string (string)
251 "Strip quoted-string STRING."
252 (let ((len (length string)))
254 (let ((max (1- len)))
255 (and (eq (aref string 0) ?\")
256 (eq (aref string max) ?\")
257 (std11-strip-quoted-pair (substring string 1 max))
262 ;;; @ lexical analyze
265 (defcustom std11-lexical-analyzer
266 '(std11-analyze-quoted-string
267 std11-analyze-domain-literal
268 std11-analyze-comment
270 std11-analyze-special
272 "*List of functions to return result of lexical analyze.
273 Each function must have two arguments: STRING and START.
274 STRING is the target string to be analyzed.
275 START is start position of STRING to analyze.
277 Previous function is preferred to next function. If a function
278 returns nil, next function is used. Otherwise the return value will
282 :type '(repeat function))
285 (defconst std11-space-char-list '(? ?\t ?\n))
286 (defconst std11-special-char-list '(?\] ?\[
291 ;; (defconst std11-spaces-regexp
292 ;; (eval-when-compile (concat "[" std11-space-char-list "]+")))
293 (defconst std11-atom-regexp
295 (concat "[^" std11-special-char-list std11-space-char-list "]+")))
297 (defun std11-analyze-spaces (string start)
298 (if (and (string-match (eval-when-compile
299 (concat "[" std11-space-char-list "]+"))
301 (= (match-beginning 0) start))
302 (let ((end (match-end 0)))
303 (cons (cons 'spaces (substring string start end))
304 ;;(substring string end)
308 (defun std11-analyze-special (string start)
309 (if (and (> (length string) start)
310 (memq (aref string start) std11-special-char-list))
311 (cons (cons 'specials (substring string start (1+ start)))
312 ;;(substring string 1)
316 (defun std11-analyze-atom (string start)
317 (if (and (string-match std11-atom-regexp string start)
318 (= (match-beginning 0) start))
319 (let ((end (match-end 0)))
320 (cons (cons 'atom (substring string start end))
321 ;;(substring string end)
325 (defun std11-check-enclosure (string open close &optional recursive from)
326 (let ((len (length string))
330 (eq (aref string i) open))
335 (setq chr (aref string i))
348 (setq p (std11-check-enclosure
349 string open close recursive i))
359 (defun std11-analyze-quoted-string (string start)
360 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
362 (cons (cons 'quoted-string (substring string (1+ start) (1- p)))
363 ;;(substring string p))
367 (defun std11-analyze-domain-literal (string start)
368 (let ((p (std11-check-enclosure string ?\[ ?\] nil start)))
370 (cons (cons 'domain-literal (substring string (1+ start) (1- p)))
371 ;;(substring string p))
375 (defun std11-analyze-comment (string start)
376 (let ((p (std11-check-enclosure string ?\( ?\) t start)))
378 (cons (cons 'comment (substring string (1+ start) (1- p)))
379 ;;(substring string p))
384 (defun std11-lexical-analyze (string &optional analyzer start)
385 "Analyze STRING as lexical tokens of STD 11."
387 (setq analyzer std11-lexical-analyzer))
390 (let ((len (length string))
394 (let ((rest analyzer)
396 (while (and (setq func (car rest))
397 (null (setq r (funcall func string start))))
398 (setq rest (cdr rest)))
400 (list (cons 'error (substring string start)) (1+ len)))
402 (setq dest (cons (car ret) dest)
412 (defun std11-ignored-token-p (token)
413 (let ((type (car token)))
414 (or (eq type 'spaces)(eq type 'comment))
417 (defun std11-parse-token (lal)
421 (setq token (car lal))
422 (std11-ignored-token-p token)
425 (setq itl (cons token itl))
427 (cons (nreverse (cons token itl))
431 (defun std11-parse-ascii-token (lal)
432 (let (token itl parsed token-value)
434 (setq token (car lal))
435 (or (std11-ignored-token-p token)
436 (if (and (setq token-value (cdr token))
437 (delq 'ascii (find-charset-string token-value)))
441 (setq itl (cons token itl))
444 (setq parsed (nreverse (cons token itl)))
446 (cons parsed (cdr lal))
449 (defun std11-parse-token-or-comment (lal)
453 (setq token (car lal))
454 (eq (car token) 'spaces)
457 (setq itl (cons token itl))
459 (cons (nreverse (cons token itl))
463 (defun std11-parse-word (lal)
464 (let ((ret (std11-parse-ascii-token lal)))
466 (let ((elt (car ret))
469 (if (or (assq 'atom elt)
470 (assq 'quoted-string elt))
471 (cons (cons 'word elt) rest)
474 (defun std11-parse-word-or-comment (lal)
475 (let ((ret (std11-parse-token-or-comment lal)))
477 (let ((elt (car ret))
480 (cond ((or (assq 'atom elt)
481 (assq 'quoted-string elt))
482 (cons (cons 'word elt) rest)
485 (cons (cons 'comment-word elt) rest)
489 (defun std11-parse-phrase (lal)
491 (while (setq ret (std11-parse-word-or-comment lal))
492 (setq phrase (append phrase (cdr (car ret))))
496 (cons (cons 'phrase phrase) lal)
499 (defun std11-parse-local-part (lal)
500 (let ((ret (std11-parse-word lal)))
502 (let ((local-part (cdr (car ret))) dot)
504 (while (and (setq ret (std11-parse-ascii-token lal))
506 (string-equal (cdr (assq 'specials dot)) ".")
507 (setq ret (std11-parse-word (cdr ret)))
509 (append local-part dot (cdr (car ret)))
513 (cons (cons 'local-part local-part) lal)
516 (defun std11-parse-sub-domain (lal)
517 (let ((ret (std11-parse-ascii-token lal)))
519 (let ((sub-domain (car ret)))
520 (if (or (assq 'atom sub-domain)
521 (assq 'domain-literal sub-domain)
523 (cons (cons 'sub-domain sub-domain)
528 (defun std11-parse-domain (lal)
529 (let ((ret (std11-parse-sub-domain lal)))
531 (let ((domain (cdr (car ret))) dot)
533 (while (and (setq ret (std11-parse-ascii-token lal))
535 (string-equal (cdr (assq 'specials dot)) ".")
536 (setq ret (std11-parse-sub-domain (cdr ret)))
538 (append domain dot (cdr (car ret)))
542 (cons (cons 'domain domain) lal)
545 (defun std11-parse-at-domain (lal)
546 (let ((ret (std11-parse-ascii-token lal)) at-sign)
548 (setq at-sign (car ret))
549 (string-equal (cdr (assq 'specials at-sign)) "@")
550 (setq ret (std11-parse-domain (cdr ret)))
552 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
556 (defun std11-parse-addr-spec (lal)
557 (let ((ret (std11-parse-local-part lal))
561 (setq addr (cdr (car ret)))
563 (and (setq ret (std11-parse-at-domain lal))
564 (setq addr (append addr (cdr (car ret))))
567 (cons (cons 'addr-spec addr) lal)
570 (defun std11-parse-route (lal)
571 (let ((ret (std11-parse-at-domain lal))
575 (setq route (cdr (car ret)))
577 (while (and (setq ret (std11-parse-ascii-token lal))
578 (setq comma (car ret))
579 (string-equal (cdr (assq 'specials comma)) ",")
580 (setq ret (std11-parse-at-domain (cdr ret)))
582 (setq route (append route comma (cdr (car ret))))
585 (and (setq ret (std11-parse-ascii-token lal))
586 (setq colon (car ret))
587 (string-equal (cdr (assq 'specials colon)) ":")
588 (setq route (append route colon))
591 (cons (cons 'route route)
596 (defun std11-parse-route-addr (lal)
597 (let ((ret (std11-parse-ascii-token lal))
601 (string-equal (cdr (assq 'specials <)) "<")
603 (progn (and (setq ret (std11-parse-route lal))
604 (setq route (cdr (car ret)))
607 (setq ret (std11-parse-addr-spec lal))
609 (setq addr-spec (cdr (car ret)))
611 (setq ret (std11-parse-ascii-token lal))
613 (string-equal (cdr (assq 'specials >)) ">")
615 (cons (cons 'route-addr (append route addr-spec))
620 (defun std11-parse-phrase-route-addr (lal)
621 (let ((ret (std11-parse-phrase lal)) phrase)
624 (setq phrase (cdr (car ret)))
627 (if (setq ret (std11-parse-route-addr lal))
628 (cons (list 'phrase-route-addr
634 (defun std11-parse-mailbox (lal)
635 (let ((ret (or (std11-parse-phrase-route-addr lal)
636 (std11-parse-addr-spec lal)))
640 (setq mbox (car ret))
642 (if (and (setq ret (std11-parse-token-or-comment lal))
643 (setq comment (cdr (assq 'comment (car ret))))
647 (cons (list 'mailbox mbox comment)
651 (defun std11-parse-group (lal)
652 (let ((ret (std11-parse-phrase lal))
653 phrase colon comma mbox semicolon)
655 (setq phrase (cdr (car ret)))
657 (setq ret (std11-parse-ascii-token lal))
658 (setq colon (car ret))
659 (string-equal (cdr (assq 'specials colon)) ":")
662 (and (setq ret (std11-parse-mailbox lal))
663 (setq mbox (list (car ret)))
666 (while (and (setq ret (std11-parse-ascii-token lal))
667 (setq comma (car ret))
669 (cdr (assq 'specials comma)) ",")
671 (setq ret (std11-parse-mailbox lal))
672 (setq mbox (cons (car ret) mbox))
676 (and (setq ret (std11-parse-ascii-token lal))
677 (setq semicolon (car ret))
678 (string-equal (cdr (assq 'specials semicolon)) ";")
680 (cons (list 'group phrase (nreverse mbox))
685 (defun std11-parse-address (lal)
686 (or (std11-parse-group lal)
687 (std11-parse-mailbox lal)
690 (defun std11-parse-addresses (lal)
691 (let ((ret (std11-parse-address lal)))
693 (let ((dest (list (car ret))))
695 (while (and (setq ret (std11-parse-ascii-token lal))
696 (string-equal (cdr (assq 'specials (car ret))) ",")
697 (setq ret (std11-parse-address (cdr ret)))
699 (setq dest (cons (car ret) dest))
705 (defun std11-parse-msg-id (lal)
706 (let ((ret (std11-parse-ascii-token lal))
710 (string-equal (cdr (assq 'specials <)) "<")
712 (setq ret (std11-parse-addr-spec lal))
713 (setq addr-spec (car ret))
715 (setq ret (std11-parse-ascii-token lal))
717 (string-equal (cdr (assq 'specials >)) ">")
719 (cons (cons 'msg-id (cdr addr-spec))
723 (defun std11-parse-msg-ids (tokens)
724 "Parse lexical TOKENS as `*(phrase / msg-id)', and return the result."
725 (let ((ret (or (std11-parse-msg-id tokens)
726 (std11-parse-phrase tokens))))
728 (let ((dest (list (car ret))))
729 (setq tokens (cdr ret))
730 (while (setq ret (or (std11-parse-msg-id tokens)
731 (std11-parse-phrase tokens)))
732 (setq dest (cons (car ret) dest))
733 (setq tokens (cdr ret))
738 (defalias 'std11-parse-in-reply-to 'std11-parse-msg-ids)
739 (make-obsolete 'std11-parse-in-reply-to 'std11-parse-msg-ids)
745 (defun std11-addr-to-string (seq)
746 "Return string from lexical analyzed list SEQ
747 represents addr-spec of RFC 822."
750 (let ((name (car token)))
752 ((eq name 'spaces) "")
753 ((eq name 'comment) "")
754 ((eq name 'quoted-string)
755 (concat "\"" (cdr token) "\""))
762 (defun std11-address-string (address)
763 "Return string of address part from parsed ADDRESS of RFC 822."
764 (cond ((eq (car address) 'group)
765 (mapconcat (function std11-address-string)
769 ((eq (car address) 'mailbox)
770 (let ((addr (nth 1 address)))
771 (std11-addr-to-string
772 (if (eq (car addr) 'phrase-route-addr)
778 (defun std11-comment-value-to-string (value)
780 (std11-strip-quoted-pair value)
785 (if (stringp (car value))
788 (std11-comment-value-to-string
797 (defun std11-full-name-string (address)
798 "Return string of full-name part from parsed ADDRESS of RFC 822."
799 (cond ((eq (car address) 'group)
806 ((eq (car address) 'mailbox)
807 (let ((addr (nth 1 address))
808 (comment (nth 2 address))
810 (if (eq (car addr) 'phrase-route-addr)
815 (let ((type (car token)))
816 (cond ((eq type 'quoted-string)
817 (std11-strip-quoted-pair (cdr token))
821 (std11-comment-value-to-string
830 (cond ((> (length phrase) 0) phrase)
831 (comment (std11-comment-value-to-string comment))
836 (defun std11-msg-id-string (msg-id)
837 "Return string from parsed MSG-ID of RFC 822."
838 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
842 (defun std11-fill-msg-id-list-string (string &optional column)
843 "Fill list of msg-id in STRING, and return the result."
846 (let ((lal (std11-lexical-analyze string))
848 (let ((ret (std11-parse-msg-id lal)))
850 (let* ((str (std11-msg-id-string (car ret)))
853 (if (> (+ len column) 76)
854 (setq dest (concat dest "\n " str)
857 column (+ column len))
859 (setq dest (concat dest (cdr (car lal)))
863 (let ((ret (std11-parse-msg-id lal)))
865 (let* ((str (std11-msg-id-string (car ret)))
866 (len (1+ (length str))))
868 (if (> (+ len column) 76)
869 (setq dest (concat dest "\n " str)
871 (setq dest (concat dest " " str)
872 column (+ column len))
874 (setq dest (concat dest (cdr (car lal)))
880 ;;; @ parser with lexical analyzer
884 (defun std11-parse-address-string (string)
885 "Parse STRING as mail address."
886 (std11-parse-address (std11-lexical-analyze string))
890 (defun std11-parse-addresses-string (string)
891 "Parse STRING as mail address list."
892 (std11-parse-addresses (std11-lexical-analyze string))
896 (defun std11-parse-msg-id-string (string)
897 "Parse STRING as msg-id."
898 (std11-parse-msg-id (std11-lexical-analyze string))
902 (defun std11-parse-msg-ids-string (string)
903 "Parse STRING as `*(phrase / msg-id)'."
904 (std11-parse-msg-ids (std11-lexical-analyze string))
908 (defun std11-extract-address-components (string)
909 "Extract full name and canonical address from STRING.
910 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
911 If no name can be extracted, FULL-NAME will be nil."
912 (let* ((structure (car (std11-parse-address-string
913 (std11-unfold-string string))))
914 (phrase (std11-full-name-string structure))
915 (address (std11-address-string structure))
917 (list phrase address)
926 ;;; std11.el ends here