1 ;;; std11.el --- STD 11 functions for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998,1999 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 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 (or (fboundp 'buffer-substring-no-properties)
36 (defconst std11-field-name-regexp "[!-9;-~]+")
37 (defconst std11-field-head-regexp
38 (concat "^" std11-field-name-regexp ":"))
39 (defconst std11-next-field-head-regexp
40 (concat "\n" std11-field-name-regexp ":"))
42 (defun std11-field-end (&optional bound)
43 "Move to end of field and return this point.
44 The optional argument BOUNDs the search; it is a buffer position."
45 (if (re-search-forward std11-next-field-head-regexp bound t)
46 (goto-char (match-beginning 0))
47 (if (re-search-forward "^$" bound t)
48 (goto-char (1- (match-beginning 0)))
55 (defun std11-fetch-field (name)
56 "Return the value of the header field NAME.
57 The buffer is expected to be narrowed to just the headers of the message."
59 (goto-char (point-min))
60 (let ((case-fold-search t))
61 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
62 (buffer-substring-no-properties (match-end 0) (std11-field-end))
66 (defun std11-narrow-to-header (&optional boundary)
67 "Narrow to the message header.
68 If BOUNDARY is not nil, it is used as message header separator."
70 (goto-char (point-min))
71 (if (re-search-forward
72 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
79 (defun std11-field-body (name &optional boundary)
80 "Return the value of the header field NAME.
81 If BOUNDARY is not nil, it is used as message header separator."
84 (inline (std11-narrow-to-header boundary)
85 (std11-fetch-field name))
88 (defun std11-find-field-body (field-names &optional boundary)
89 "Return the first found field-body specified by FIELD-NAMES
90 of the message header in current buffer. If BOUNDARY is not nil, it is
91 used as message header separator."
94 (std11-narrow-to-header boundary)
95 (let ((case-fold-search t)
98 (while (setq field-name (car field-names))
99 (goto-char (point-min))
100 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
102 (buffer-substring-no-properties
103 (match-end 0) (std11-field-end)))
105 (setq field-names (cdr field-names))
108 (defun std11-field-bodies (field-names &optional default-value boundary)
109 "Return list of each field-bodies of FIELD-NAMES of the message header
110 in current buffer. If BOUNDARY is not nil, it is used as message
114 (std11-narrow-to-header boundary)
115 (let* ((case-fold-search t)
116 (dest (make-list (length field-names) default-value))
120 (while (setq field-name (car s-rest))
121 (goto-char (point-min))
122 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
124 (buffer-substring-no-properties
125 (match-end 0) (std11-field-end)))
127 (setq s-rest (cdr s-rest)
132 (defun std11-header-string (regexp &optional boundary)
133 "Return string of message header fields matched by REGEXP.
134 If BOUNDARY is not nil, it is used as message header separator."
135 (let ((case-fold-search t))
138 (std11-narrow-to-header boundary)
139 (goto-char (point-min))
141 (while (re-search-forward std11-field-head-regexp nil t)
143 (buffer-substring (match-beginning 0) (std11-field-end)))
144 (if (string-match regexp field)
145 (setq header (concat header field "\n"))
150 (defun std11-header-string-except (regexp &optional boundary)
151 "Return string of message header fields not matched by REGEXP.
152 If BOUNDARY is not nil, it is used as message header separator."
153 (let ((case-fold-search t))
156 (std11-narrow-to-header boundary)
157 (goto-char (point-min))
159 (while (re-search-forward std11-field-head-regexp nil t)
161 (buffer-substring (match-beginning 0) (std11-field-end)))
162 (if (not (string-match regexp field))
163 (setq header (concat header field "\n"))
168 (defun std11-collect-field-names (&optional boundary)
169 "Return list of all field-names of the message header in current buffer.
170 If BOUNDARY is not nil, it is used as message header separator."
173 (std11-narrow-to-header boundary)
174 (goto-char (point-min))
176 (while (re-search-forward std11-field-head-regexp nil t)
177 (setq name (buffer-substring-no-properties
178 (match-beginning 0)(1- (match-end 0))))
179 (or (member name dest)
180 (setq dest (cons name dest))
190 (defun std11-unfold-string (string)
191 "Unfold STRING as message header field."
194 (while (string-match "\n\\([ \t]\\)" string p)
195 (setq dest (concat dest
196 (substring string p (match-beginning 0))
199 (setq p (match-end 0)))
202 (concat dest (substring string p))
209 (defun std11-wrap-as-quoted-pairs (string specials)
213 (len (length string))
216 (let ((chr (aref string i)))
217 (if (memq chr specials)
218 (setq dest (concat dest (substring string b i) "\\")
223 (concat dest (substring string b))
226 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
228 (defun std11-wrap-as-quoted-string (string)
229 "Wrap STRING as RFC 822 quoted-string."
231 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
234 (defun std11-strip-quoted-pair (string)
235 "Strip quoted-pairs in STRING."
239 (len (length string))
242 (let ((chr (aref string i)))
244 (setq dest (concat dest (substring string b i))
249 (concat dest (substring string b))
252 (defun std11-strip-quoted-string (string)
253 "Strip quoted-string STRING."
254 (let ((len (length string)))
256 (let ((max (1- len)))
257 (and (eq (aref string 0) ?\")
258 (eq (aref string max) ?\")
259 (std11-strip-quoted-pair (substring string 1 max))
264 ;;; @ lexical analyze
267 (defcustom std11-lexical-analyzer
268 '(std11-analyze-quoted-string
269 std11-analyze-domain-literal
270 std11-analyze-comment
272 std11-analyze-special
274 "*List of functions to return result of lexical analyze.
275 Each function must have two arguments: STRING and START.
276 STRING is the target string to be analyzed.
277 START is start position of STRING to analyze.
279 Previous function is preferred to next function. If a function
280 returns nil, next function is used. Otherwise the return value will
284 :type '(repeat function))
287 (defconst std11-space-char-list '(? ?\t ?\n))
288 (defconst std11-special-char-list '(?\] ?\[
293 ;; (defconst std11-spaces-regexp
294 ;; (eval-when-compile (concat "[" 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 (and (string-match std11-atom-regexp string start)
320 (= (match-beginning 0) start))
321 (let ((end (match-end 0)))
322 (cons (cons 'atom (substring string start end))
323 ;;(substring string end)
327 (defun std11-check-enclosure (string open close &optional recursive from)
328 (let ((len (length string))
332 (eq (aref string i) open))
337 (setq chr (aref string i))
350 (setq p (std11-check-enclosure
351 string open close recursive i))
361 (defun std11-analyze-quoted-string (string start)
362 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
364 (cons (cons 'quoted-string (substring string (1+ start) (1- p)))
365 ;;(substring string p))
369 (defun std11-analyze-domain-literal (string start)
370 (let ((p (std11-check-enclosure string ?\[ ?\] nil start)))
372 (cons (cons 'domain-literal (substring string (1+ start) (1- p)))
373 ;;(substring string p))
377 (defun std11-analyze-comment (string start)
378 (let ((p (std11-check-enclosure string ?\( ?\) t start)))
380 (cons (cons 'comment (substring string (1+ start) (1- p)))
381 ;;(substring string p))
386 (defun std11-lexical-analyze (string &optional analyzer start)
387 "Analyze STRING as lexical tokens of STD 11."
389 (setq analyzer std11-lexical-analyzer))
392 (let ((len (length string))
396 (let ((rest analyzer)
398 (while (and (setq func (car rest))
399 (null (setq r (funcall func string start))))
400 (setq rest (cdr rest)))
402 (list (cons 'error (substring string start)) (1+ len)))
404 (setq dest (cons (car ret) dest)
414 (defun std11-ignored-token-p (token)
415 (let ((type (car token)))
416 (or (eq type 'spaces)(eq type 'comment))
419 (defun std11-parse-token (lal)
423 (setq token (car lal))
424 (std11-ignored-token-p token)
427 (setq itl (cons token itl))
429 (cons (nreverse (cons token itl))
433 (defun std11-parse-ascii-token (lal)
434 (let (token itl parsed token-value)
436 (setq token (car lal))
437 (or (std11-ignored-token-p token)
438 (if (and (setq token-value (cdr token))
439 (find-non-ascii-charset-string token-value)
444 (setq itl (cons token itl))
447 (setq parsed (nreverse (cons token itl)))
449 (cons parsed (cdr lal))
452 (defun std11-parse-token-or-comment (lal)
456 (setq token (car lal))
457 (eq (car token) 'spaces)
460 (setq itl (cons token itl))
462 (cons (nreverse (cons token itl))
466 (defun std11-parse-word (lal)
467 (let ((ret (std11-parse-ascii-token lal)))
469 (let ((elt (car ret))
472 (if (or (assq 'atom elt)
473 (assq 'quoted-string elt))
474 (cons (cons 'word elt) rest)
477 (defun std11-parse-word-or-comment (lal)
478 (let ((ret (std11-parse-token-or-comment lal)))
480 (let ((elt (car ret))
483 (cond ((or (assq 'atom elt)
484 (assq 'quoted-string elt))
485 (cons (cons 'word elt) rest)
488 (cons (cons 'comment-word elt) rest)
492 (defun std11-parse-phrase (lal)
494 (while (setq ret (std11-parse-word-or-comment lal))
495 (setq phrase (append phrase (cdr (car ret))))
499 (cons (cons 'phrase phrase) lal)
502 (defun std11-parse-local-part (lal)
503 (let ((ret (std11-parse-word lal)))
505 (let ((local-part (cdr (car ret))) dot)
507 (while (and (setq ret (std11-parse-ascii-token lal))
509 (string-equal (cdr (assq 'specials dot)) ".")
510 (setq ret (std11-parse-word (cdr ret)))
512 (append local-part dot (cdr (car ret)))
516 (cons (cons 'local-part local-part) lal)
519 (defun std11-parse-sub-domain (lal)
520 (let ((ret (std11-parse-ascii-token lal)))
522 (let ((sub-domain (car ret)))
523 (if (or (assq 'atom sub-domain)
524 (assq 'domain-literal sub-domain)
526 (cons (cons 'sub-domain sub-domain)
531 (defun std11-parse-domain (lal)
532 (let ((ret (std11-parse-sub-domain lal)))
534 (let ((domain (cdr (car ret))) dot)
536 (while (and (setq ret (std11-parse-ascii-token lal))
538 (string-equal (cdr (assq 'specials dot)) ".")
539 (setq ret (std11-parse-sub-domain (cdr ret)))
541 (append domain dot (cdr (car ret)))
545 (cons (cons 'domain domain) lal)
548 (defun std11-parse-at-domain (lal)
549 (let ((ret (std11-parse-ascii-token lal)) at-sign)
551 (setq at-sign (car ret))
552 (string-equal (cdr (assq 'specials at-sign)) "@")
553 (setq ret (std11-parse-domain (cdr ret)))
555 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
559 (defun std11-parse-addr-spec (lal)
560 (let ((ret (std11-parse-local-part lal))
564 (setq addr (cdr (car ret)))
566 (and (setq ret (std11-parse-at-domain lal))
567 (setq addr (append addr (cdr (car ret))))
570 (cons (cons 'addr-spec addr) lal)
573 (defun std11-parse-route (lal)
574 (let ((ret (std11-parse-at-domain lal))
578 (setq route (cdr (car ret)))
580 (while (and (setq ret (std11-parse-ascii-token lal))
581 (setq comma (car ret))
582 (string-equal (cdr (assq 'specials comma)) ",")
583 (setq ret (std11-parse-at-domain (cdr ret)))
585 (setq route (append route comma (cdr (car ret))))
588 (and (setq ret (std11-parse-ascii-token lal))
589 (setq colon (car ret))
590 (string-equal (cdr (assq 'specials colon)) ":")
591 (setq route (append route colon))
594 (cons (cons 'route route)
599 (defun std11-parse-route-addr (lal)
600 (let ((ret (std11-parse-ascii-token lal))
604 (string-equal (cdr (assq 'specials <)) "<")
606 (progn (and (setq ret (std11-parse-route lal))
607 (setq route (cdr (car ret)))
610 (setq ret (std11-parse-addr-spec lal))
612 (setq addr-spec (cdr (car ret)))
614 (setq ret (std11-parse-ascii-token lal))
616 (string-equal (cdr (assq 'specials >)) ">")
618 (cons (cons 'route-addr (append route addr-spec))
623 (defun std11-parse-phrase-route-addr (lal)
624 (let ((ret (std11-parse-phrase lal)) phrase)
627 (setq phrase (cdr (car ret)))
630 (if (setq ret (std11-parse-route-addr lal))
631 (cons (list 'phrase-route-addr
637 (defun std11-parse-mailbox (lal)
638 (let ((ret (or (std11-parse-phrase-route-addr lal)
639 (std11-parse-addr-spec lal)))
643 (setq mbox (car ret))
645 (if (and (setq ret (std11-parse-token-or-comment lal))
646 (setq comment (cdr (assq 'comment (car ret))))
650 (cons (list 'mailbox mbox comment)
654 (defun std11-parse-group (lal)
655 (let ((ret (std11-parse-phrase lal))
656 phrase colon comma mbox semicolon)
658 (setq phrase (cdr (car ret)))
660 (setq ret (std11-parse-ascii-token lal))
661 (setq colon (car ret))
662 (string-equal (cdr (assq 'specials colon)) ":")
665 (and (setq ret (std11-parse-mailbox lal))
666 (setq mbox (list (car ret)))
669 (while (and (setq ret (std11-parse-ascii-token lal))
670 (setq comma (car ret))
672 (cdr (assq 'specials comma)) ",")
674 (setq ret (std11-parse-mailbox lal))
675 (setq mbox (cons (car ret) mbox))
679 (and (setq ret (std11-parse-ascii-token lal))
680 (setq semicolon (car ret))
681 (string-equal (cdr (assq 'specials semicolon)) ";")
683 (cons (list 'group phrase (nreverse mbox))
688 (defun std11-parse-address (lal)
689 (or (std11-parse-group lal)
690 (std11-parse-mailbox lal)
693 (defun std11-parse-addresses (lal)
694 (let ((ret (std11-parse-address lal)))
696 (let ((dest (list (car ret))))
698 (while (and (setq ret (std11-parse-ascii-token lal))
699 (string-equal (cdr (assq 'specials (car ret))) ",")
700 (setq ret (std11-parse-address (cdr ret)))
702 (setq dest (cons (car ret) dest))
708 (defun std11-parse-msg-id (lal)
709 (let ((ret (std11-parse-ascii-token lal))
713 (string-equal (cdr (assq 'specials <)) "<")
715 (setq ret (std11-parse-addr-spec lal))
716 (setq addr-spec (car ret))
718 (setq ret (std11-parse-ascii-token lal))
720 (string-equal (cdr (assq 'specials >)) ">")
722 (cons (cons 'msg-id (cdr addr-spec))
726 (defun std11-parse-msg-ids (tokens)
727 "Parse lexical TOKENS as `*(phrase / msg-id)', and return the result."
728 (let ((ret (or (std11-parse-msg-id tokens)
729 (std11-parse-phrase tokens))))
731 (let ((dest (list (car ret))))
732 (setq tokens (cdr ret))
733 (while (setq ret (or (std11-parse-msg-id tokens)
734 (std11-parse-phrase tokens)))
735 (setq dest (cons (car ret) dest))
736 (setq tokens (cdr ret))
741 (defalias 'std11-parse-in-reply-to 'std11-parse-msg-ids)
742 (make-obsolete 'std11-parse-in-reply-to 'std11-parse-msg-ids)
748 (defun std11-addr-to-string (seq)
749 "Return string from lexical analyzed list SEQ
750 represents addr-spec of RFC 822."
753 (let ((name (car token)))
755 ((eq name 'spaces) "")
756 ((eq name 'comment) "")
757 ((eq name 'quoted-string)
758 (concat "\"" (cdr token) "\""))
765 (defun std11-address-string (address)
766 "Return string of address part from parsed ADDRESS of RFC 822."
767 (cond ((eq (car address) 'group)
768 (mapconcat (function std11-address-string)
772 ((eq (car address) 'mailbox)
773 (let ((addr (nth 1 address)))
774 (std11-addr-to-string
775 (if (eq (car addr) 'phrase-route-addr)
781 (defun std11-comment-value-to-string (value)
783 (std11-strip-quoted-pair value)
788 (if (stringp (car value))
791 (std11-comment-value-to-string
800 (defun std11-full-name-string (address)
801 "Return string of full-name part from parsed ADDRESS of RFC 822."
802 (cond ((eq (car address) 'group)
809 ((eq (car address) 'mailbox)
810 (let ((addr (nth 1 address))
811 (comment (nth 2 address))
813 (if (eq (car addr) 'phrase-route-addr)
818 (let ((type (car token)))
819 (cond ((eq type 'quoted-string)
820 (std11-strip-quoted-pair (cdr token))
824 (std11-comment-value-to-string
833 (cond ((> (length phrase) 0) phrase)
834 (comment (std11-comment-value-to-string comment))
839 (defun std11-msg-id-string (msg-id)
840 "Return string from parsed MSG-ID of RFC 822."
841 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
845 (defun std11-fill-msg-id-list-string (string &optional column)
846 "Fill list of msg-id in STRING, and return the result."
849 (let ((lal (std11-lexical-analyze string))
851 (let ((ret (std11-parse-msg-id lal)))
853 (let* ((str (std11-msg-id-string (car ret)))
856 (if (> (+ len column) 76)
857 (setq dest (concat dest "\n " str)
860 column (+ column len))
862 (setq dest (concat dest (cdr (car lal)))
866 (let ((ret (std11-parse-msg-id lal)))
868 (let* ((str (std11-msg-id-string (car ret)))
869 (len (1+ (length str))))
871 (if (> (+ len column) 76)
872 (setq dest (concat dest "\n " str)
874 (setq dest (concat dest " " str)
875 column (+ column len))
877 (setq dest (concat dest (cdr (car lal)))
883 ;;; @ parser with lexical analyzer
887 (defun std11-parse-address-string (string)
888 "Parse STRING as mail address."
889 (std11-parse-address (std11-lexical-analyze string))
893 (defun std11-parse-addresses-string (string)
894 "Parse STRING as mail address list."
895 (std11-parse-addresses (std11-lexical-analyze string))
899 (defun std11-parse-msg-id-string (string)
900 "Parse STRING as msg-id."
901 (std11-parse-msg-id (std11-lexical-analyze string))
905 (defun std11-parse-msg-ids-string (string)
906 "Parse STRING as `*(phrase / msg-id)'."
907 (std11-parse-msg-ids (std11-lexical-analyze string))
911 (defun std11-extract-address-components (string)
912 "Extract full name and canonical address from STRING.
913 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
914 If no name can be extracted, FULL-NAME will be nil."
915 (let* ((structure (car (std11-parse-address-string
916 (std11-unfold-string string))))
917 (phrase (std11-full-name-string structure))
918 (address (std11-address-string structure))
920 (list phrase address)
929 ;;; std11.el ends here