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 ;; unlimited patch by simm-emacs@fan.gr.jp
219 ;; Mon, 10 Jan 2000 13:03:02 +0900
220 (if mime-decode-unlimited
221 (eword-encode-string (concat dest (substring string b)))
222 (concat dest (substring string b)))))
224 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
226 ;; unlimited patch by simm-emacs@fan.gr.jp
227 ;; Mon, 10 Jan 2000 13:03:02 +0900
228 (defvar std11-filename-coding-system nil
229 "Define coding-system for non-ASCII filename when send.
230 Set this variable coding system symbol (ie. 'iso-2022-jp) or nil.
231 If non-nil, std11-wrap-as-quoted-string use encode-coding-string.")
233 (defun std11-wrap-as-quoted-string (string)
234 "Wrap STRING as RFC 822 quoted-string."
236 ;; unlimited patch by simm-emacs@fan.gr.jp
237 ;; Mon, 10 Jan 2000 13:03:02 +0900
238 (if std11-filename-coding-system
239 (encode-coding-system string std11-filename-coding-system)
240 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list))
243 (defun std11-strip-quoted-pair (string)
244 "Strip quoted-pairs in STRING."
248 (len (length string))
251 (let ((chr (aref string i)))
253 (setq dest (concat dest (substring string b i))
258 (concat dest (substring string b))
261 (defun std11-strip-quoted-string (string)
262 "Strip quoted-string STRING."
263 (let ((len (length string)))
265 (let ((max (1- len)))
266 (and (eq (aref string 0) ?\")
267 (eq (aref string max) ?\")
268 (std11-strip-quoted-pair (substring string 1 max))
273 ;;; @ lexical analyze
276 (defcustom std11-lexical-analyzer
277 '(std11-analyze-quoted-string
278 std11-analyze-domain-literal
279 std11-analyze-comment
281 std11-analyze-special
283 "*List of functions to return result of lexical analyze.
284 Each function must have two arguments: STRING and START.
285 STRING is the target string to be analyzed.
286 START is start position of STRING to analyze.
288 Previous function is preferred to next function. If a function
289 returns nil, next function is used. Otherwise the return value will
293 :type '(repeat function))
296 (defconst std11-space-char-list '(? ?\t ?\n))
297 (defconst std11-special-char-list '(?\] ?\[
302 ;; (defconst std11-spaces-regexp
303 ;; (eval-when-compile (concat "[" std11-space-char-list "]+")))
305 (defconst std11-non-atom-regexp
307 (concat "[" std11-special-char-list std11-space-char-list "]")))
309 (defconst std11-atom-regexp
311 (concat "[^" std11-special-char-list std11-space-char-list "]+")))
313 (defun std11-analyze-spaces (string start)
314 (if (and (string-match (eval-when-compile
315 (concat "[" std11-space-char-list "]+"))
317 (= (match-beginning 0) start))
318 (let ((end (match-end 0)))
319 (cons (cons 'spaces (substring string start end))
320 ;;(substring string end)
324 (defun std11-analyze-special (string start)
325 (if (and (> (length string) start)
326 (memq (aref string start) std11-special-char-list))
327 (cons (cons 'specials (substring string start (1+ start)))
328 ;;(substring string 1)
332 (defun std11-analyze-atom (string start)
333 (if (string-match std11-non-atom-regexp string start)
334 (if (> (match-beginning 0) start)
335 (cons (cons 'atom (substring string start (match-beginning 0)))
338 (cons (cons 'atom (substring string start))
340 ;; (if (and (string-match std11-atom-regexp string start)
341 ;; (= (match-beginning 0) start))
342 ;; (let ((end (match-end 0)))
343 ;; (cons (cons 'atom (substring string start end))
344 ;; ;;(substring string end)
349 (defun std11-check-enclosure (string open close &optional recursive from)
350 (let ((len (length string))
354 (eq (aref string i) open))
359 (setq chr (aref string i))
372 (setq p (std11-check-enclosure
373 string open close recursive i))
383 (defun std11-analyze-quoted-string (string start)
384 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
386 (cons (cons 'quoted-string (substring string (1+ start) (1- p)))
387 ;;(substring string p))
391 (defun std11-analyze-domain-literal (string start)
392 (let ((p (std11-check-enclosure string ?\[ ?\] nil start)))
394 (cons (cons 'domain-literal (substring string (1+ start) (1- p)))
395 ;;(substring string p))
399 (defun std11-analyze-comment (string start)
400 (let ((p (std11-check-enclosure string ?\( ?\) t start)))
402 (cons (cons 'comment (substring string (1+ start) (1- p)))
403 ;;(substring string p))
408 (defun std11-lexical-analyze (string &optional analyzer start)
409 "Analyze STRING as lexical tokens of STD 11."
411 (setq analyzer std11-lexical-analyzer))
414 (let ((len (length string))
418 (let ((rest analyzer)
420 (while (and (setq func (car rest))
421 (null (setq r (funcall func string start))))
422 (setq rest (cdr rest)))
424 (cons (cons 'error (substring string start)) (1+ len)))
426 (setq dest (cons (car ret) dest)
436 (defun std11-ignored-token-p (token)
437 (let ((type (car token)))
438 (or (eq type 'spaces)(eq type 'comment))
441 (defun std11-parse-token (lal)
445 (setq token (car lal))
446 (std11-ignored-token-p token)
449 (setq itl (cons token itl))
451 (cons (nreverse (cons token itl))
455 (defun std11-parse-ascii-token (lal)
456 (let (token itl parsed token-value)
458 (setq token (car lal))
459 (or (std11-ignored-token-p token)
460 (if (and (setq token-value (cdr token))
461 (delq 'ascii (find-charset-string token-value)))
465 (setq itl (cons token itl))
468 (setq parsed (nreverse (cons token itl)))
470 (cons parsed (cdr lal))
473 (defun std11-parse-token-or-comment (lal)
477 (setq token (car lal))
478 (eq (car token) 'spaces)
481 (setq itl (cons token itl))
483 (cons (nreverse (cons token itl))
487 (defun std11-parse-word (lal)
488 (let ((ret (std11-parse-ascii-token lal)))
490 (let ((elt (car ret))
493 (if (or (assq 'atom elt)
494 (assq 'quoted-string elt))
495 (cons (cons 'word elt) rest)
498 (defun std11-parse-word-or-comment (lal)
499 (let ((ret (std11-parse-token-or-comment lal)))
501 (let ((elt (car ret))
504 (cond ((or (assq 'atom elt)
505 (assq 'quoted-string elt))
506 (cons (cons 'word elt) rest)
509 (cons (cons 'comment-word elt) rest)
513 (defun std11-parse-phrase (lal)
515 (while (setq ret (std11-parse-word-or-comment lal))
516 (setq phrase (append phrase (cdr (car ret))))
520 (cons (cons 'phrase phrase) lal)
523 (defun std11-parse-local-part (lal)
524 (let ((ret (std11-parse-word lal)))
526 (let ((local-part (cdr (car ret))) dot)
528 (while (and (setq ret (std11-parse-ascii-token lal))
530 (string-equal (cdr (assq 'specials dot)) ".")
531 (setq ret (std11-parse-word (cdr ret)))
533 (append local-part dot (cdr (car ret)))
537 (cons (cons 'local-part local-part) lal)
540 (defun std11-parse-sub-domain (lal)
541 (let ((ret (std11-parse-ascii-token lal)))
543 (let ((sub-domain (car ret)))
544 (if (or (assq 'atom sub-domain)
545 (assq 'domain-literal sub-domain)
547 (cons (cons 'sub-domain sub-domain)
552 (defun std11-parse-domain (lal)
553 (let ((ret (std11-parse-sub-domain lal)))
555 (let ((domain (cdr (car ret))) dot)
557 (while (and (setq ret (std11-parse-ascii-token lal))
559 (string-equal (cdr (assq 'specials dot)) ".")
560 (setq ret (std11-parse-sub-domain (cdr ret)))
562 (append domain dot (cdr (car ret)))
566 (cons (cons 'domain domain) lal)
569 (defun std11-parse-at-domain (lal)
570 (let ((ret (std11-parse-ascii-token lal)) at-sign)
572 (setq at-sign (car ret))
573 (string-equal (cdr (assq 'specials at-sign)) "@")
574 (setq ret (std11-parse-domain (cdr ret)))
576 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
580 (defun std11-parse-addr-spec (lal)
581 (let ((ret (std11-parse-local-part lal))
585 (setq addr (cdr (car ret)))
587 (and (setq ret (std11-parse-at-domain lal))
588 (setq addr (append addr (cdr (car ret))))
591 (cons (cons 'addr-spec addr) lal)
594 (defun std11-parse-route (lal)
595 (let ((ret (std11-parse-at-domain lal))
599 (setq route (cdr (car ret)))
601 (while (and (setq ret (std11-parse-ascii-token lal))
602 (setq comma (car ret))
603 (string-equal (cdr (assq 'specials comma)) ",")
604 (setq ret (std11-parse-at-domain (cdr ret)))
606 (setq route (append route comma (cdr (car ret))))
609 (and (setq ret (std11-parse-ascii-token lal))
610 (setq colon (car ret))
611 (string-equal (cdr (assq 'specials colon)) ":")
612 (setq route (append route colon))
615 (cons (cons 'route route)
620 (defun std11-parse-route-addr (lal)
621 (let ((ret (std11-parse-ascii-token lal))
625 (string-equal (cdr (assq 'specials <)) "<")
627 (progn (and (setq ret (std11-parse-route lal))
628 (setq route (cdr (car ret)))
631 (setq ret (std11-parse-addr-spec lal))
633 (setq addr-spec (cdr (car ret)))
635 (setq ret (std11-parse-ascii-token lal))
637 (string-equal (cdr (assq 'specials >)) ">")
639 (cons (cons 'route-addr (append route addr-spec))
644 (defun std11-parse-phrase-route-addr (lal)
645 (let ((ret (std11-parse-phrase lal)) phrase)
648 (setq phrase (cdr (car ret)))
651 (if (setq ret (std11-parse-route-addr lal))
652 (cons (list 'phrase-route-addr
658 (defun std11-parse-mailbox (lal)
659 (let ((ret (or (std11-parse-phrase-route-addr lal)
660 (std11-parse-addr-spec lal)))
664 (setq mbox (car ret))
666 (if (and (setq ret (std11-parse-token-or-comment lal))
667 (setq comment (cdr (assq 'comment (car ret))))
671 (cons (list 'mailbox mbox comment)
675 (defun std11-parse-group (lal)
676 (let ((ret (std11-parse-phrase lal))
677 phrase colon comma mbox semicolon)
679 (setq phrase (cdr (car ret)))
681 (setq ret (std11-parse-ascii-token lal))
682 (setq colon (car ret))
683 (string-equal (cdr (assq 'specials colon)) ":")
686 (and (setq ret (std11-parse-mailbox lal))
687 (setq mbox (list (car ret)))
690 (while (and (setq ret (std11-parse-ascii-token lal))
691 (setq comma (car ret))
693 (cdr (assq 'specials comma)) ",")
695 (setq ret (std11-parse-mailbox lal))
696 (setq mbox (cons (car ret) mbox))
700 (and (setq ret (std11-parse-ascii-token lal))
701 (setq semicolon (car ret))
702 (string-equal (cdr (assq 'specials semicolon)) ";")
704 (cons (list 'group phrase (nreverse mbox))
709 (defun std11-parse-address (lal)
710 (or (std11-parse-group lal)
711 (std11-parse-mailbox lal)
714 (defun std11-parse-addresses (lal)
715 (let ((ret (std11-parse-address lal)))
717 (let ((dest (list (car ret))))
719 (while (and (setq ret (std11-parse-ascii-token lal))
720 (string-equal (cdr (assq 'specials (car ret))) ",")
721 (setq ret (std11-parse-address (cdr ret)))
723 (setq dest (cons (car ret) dest))
729 (defun std11-parse-msg-id (lal)
730 (let ((ret (std11-parse-ascii-token lal))
734 (string-equal (cdr (assq 'specials <)) "<")
736 (setq ret (std11-parse-addr-spec lal))
737 (setq addr-spec (car ret))
739 (setq ret (std11-parse-ascii-token lal))
741 (string-equal (cdr (assq 'specials >)) ">")
743 (cons (cons 'msg-id (cdr addr-spec))
747 (defun std11-parse-msg-ids (tokens)
748 "Parse lexical TOKENS as `*(phrase / msg-id)', and return the result."
749 (let ((ret (or (std11-parse-msg-id tokens)
750 (std11-parse-phrase tokens))))
752 (let ((dest (list (car ret))))
753 (setq tokens (cdr ret))
754 (while (setq ret (or (std11-parse-msg-id tokens)
755 (std11-parse-phrase tokens)))
756 (setq dest (cons (car ret) dest))
757 (setq tokens (cdr ret))
762 (defalias 'std11-parse-in-reply-to 'std11-parse-msg-ids)
763 (make-obsolete 'std11-parse-in-reply-to 'std11-parse-msg-ids)
769 (defun std11-addr-to-string (seq)
770 "Return string from lexical analyzed list SEQ
771 represents addr-spec of RFC 822."
774 (let ((name (car token)))
776 ((eq name 'spaces) "")
777 ((eq name 'comment) "")
778 ((eq name 'quoted-string)
779 (concat "\"" (cdr token) "\""))
786 (defun std11-address-string (address)
787 "Return string of address part from parsed ADDRESS of RFC 822."
788 (cond ((eq (car address) 'group)
789 (mapconcat (function std11-address-string)
793 ((eq (car address) 'mailbox)
794 (let ((addr (nth 1 address)))
795 (std11-addr-to-string
796 (if (eq (car addr) 'phrase-route-addr)
802 (defun std11-comment-value-to-string (value)
804 (std11-strip-quoted-pair value)
809 (if (stringp (car value))
812 (std11-comment-value-to-string
821 (defun std11-full-name-string (address)
822 "Return string of full-name part from parsed ADDRESS of RFC 822."
823 (cond ((eq (car address) 'group)
830 ((eq (car address) 'mailbox)
831 (let ((addr (nth 1 address))
832 (comment (nth 2 address))
834 (if (eq (car addr) 'phrase-route-addr)
839 (let ((type (car token)))
840 (cond ((eq type 'quoted-string)
841 (std11-strip-quoted-pair (cdr token))
845 (std11-comment-value-to-string
854 (cond ((> (length phrase) 0) phrase)
855 (comment (std11-comment-value-to-string comment))
860 (defun std11-msg-id-string (msg-id)
861 "Return string from parsed MSG-ID of RFC 822."
862 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
866 (defun std11-fill-msg-id-list-string (string &optional column)
867 "Fill list of msg-id in STRING, and return the result."
870 (let ((lal (std11-lexical-analyze string))
872 (let ((ret (std11-parse-msg-id lal)))
874 (let* ((str (std11-msg-id-string (car ret)))
877 (if (> (+ len column) 76)
878 (setq dest (concat dest "\n " str)
881 column (+ column len))
883 (setq dest (concat dest (cdr (car lal)))
887 (let ((ret (std11-parse-msg-id lal)))
889 (let* ((str (std11-msg-id-string (car ret)))
890 (len (1+ (length str))))
892 (if (> (+ len column) 76)
893 (setq dest (concat dest "\n " str)
895 (setq dest (concat dest " " str)
896 column (+ column len))
898 (setq dest (concat dest (cdr (car lal)))
904 ;;; @ parser with lexical analyzer
908 (defun std11-parse-address-string (string)
909 "Parse STRING as mail address."
910 (std11-parse-address (std11-lexical-analyze string))
914 (defun std11-parse-addresses-string (string)
915 "Parse STRING as mail address list."
916 (std11-parse-addresses (std11-lexical-analyze string))
920 (defun std11-parse-msg-id-string (string)
921 "Parse STRING as msg-id."
922 (std11-parse-msg-id (std11-lexical-analyze string))
926 (defun std11-parse-msg-ids-string (string)
927 "Parse STRING as `*(phrase / msg-id)'."
928 (std11-parse-msg-ids (std11-lexical-analyze string))
932 (defun std11-extract-address-components (string)
933 "Extract full name and canonical address from STRING.
934 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
935 If no name can be extracted, FULL-NAME will be nil."
936 (let* ((structure (car (std11-parse-address-string
937 (std11-unfold-string string))))
938 (phrase (std11-full-name-string structure))
939 (address (std11-address-string structure))
941 (list phrase address)
950 ;;; std11.el ends here