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)))
52 (defun std11-fetch-field (name)
53 "Return the value of the header field NAME.
54 The buffer is expected to be narrowed to just the headers of the message."
56 (goto-char (point-min))
57 (let ((case-fold-search t))
58 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
59 (buffer-substring-no-properties (match-end 0) (std11-field-end))
63 (defun std11-narrow-to-header (&optional boundary)
64 "Narrow to the message header.
65 If BOUNDARY is not nil, it is used as message header separator."
67 (goto-char (point-min))
68 (if (re-search-forward
69 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
76 (defun std11-field-body (name &optional boundary)
77 "Return the value of the header field NAME.
78 If BOUNDARY is not nil, it is used as message header separator."
81 (inline (std11-narrow-to-header boundary)
82 (std11-fetch-field name))
85 (defun std11-find-field-body (field-names &optional boundary)
86 "Return the first found field-body specified by FIELD-NAMES
87 of the message header in current buffer. If BOUNDARY is not nil, it is
88 used as message header separator."
91 (std11-narrow-to-header boundary)
92 (let ((case-fold-search t)
95 (while (setq field-name (car field-names))
96 (goto-char (point-min))
97 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
99 (buffer-substring-no-properties
100 (match-end 0) (std11-field-end)))
102 (setq field-names (cdr field-names))
105 (defun std11-field-bodies (field-names &optional default-value boundary)
106 "Return list of each field-bodies of FIELD-NAMES of the message header
107 in current buffer. If BOUNDARY is not nil, it is used as message
111 (std11-narrow-to-header boundary)
112 (let* ((case-fold-search t)
113 (dest (make-list (length field-names) default-value))
117 (while (setq field-name (car s-rest))
118 (goto-char (point-min))
119 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
121 (buffer-substring-no-properties
122 (match-end 0) (std11-field-end)))
124 (setq s-rest (cdr s-rest)
129 (defun std11-header-string (regexp &optional boundary)
130 "Return string of message header fields matched by REGEXP.
131 If BOUNDARY is not nil, it is used as message header separator."
132 (let ((case-fold-search t))
135 (std11-narrow-to-header boundary)
136 (goto-char (point-min))
138 (while (re-search-forward std11-field-head-regexp nil t)
140 (buffer-substring (match-beginning 0) (std11-field-end)))
141 (if (string-match regexp field)
142 (setq header (concat header field "\n"))
147 (defun std11-header-string-except (regexp &optional boundary)
148 "Return string of message header fields not matched by REGEXP.
149 If BOUNDARY is not nil, it is used as message header separator."
150 (let ((case-fold-search t))
153 (std11-narrow-to-header boundary)
154 (goto-char (point-min))
156 (while (re-search-forward std11-field-head-regexp nil t)
158 (buffer-substring (match-beginning 0) (std11-field-end)))
159 (if (not (string-match regexp field))
160 (setq header (concat header field "\n"))
165 (defun std11-collect-field-names (&optional boundary)
166 "Return list of all field-names of the message header in current buffer.
167 If BOUNDARY is not nil, it is used as message header separator."
170 (std11-narrow-to-header boundary)
171 (goto-char (point-min))
173 (while (re-search-forward std11-field-head-regexp nil t)
174 (setq name (buffer-substring-no-properties
175 (match-beginning 0)(1- (match-end 0))))
176 (or (member name dest)
177 (setq dest (cons name dest))
187 (defun std11-unfold-string (string)
188 "Unfold STRING as message header field."
191 (while (string-match "\n\\([ \t]\\)" string p)
192 (setq dest (concat dest
193 (substring string p (match-beginning 0))
196 (setq p (match-end 0)))
199 (concat dest (substring string p))
206 (defun std11-wrap-as-quoted-pairs (string specials)
210 (len (length string))
213 (let ((chr (aref string i)))
214 (if (memq chr specials)
215 (setq dest (concat dest (substring string b i) "\\")
220 ;; unlimited patch by simm-emacs@fan.gr.jp
221 ;; Mon, 10 Jan 2000 13:03:02 +0900
222 (if mime-decode-unlimited
223 (eword-encode-string (concat dest (substring string b)))
224 (concat dest (substring string b)))))
226 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
228 ;; unlimited patch by simm-emacs@fan.gr.jp
229 ;; Mon, 10 Jan 2000 13:03:02 +0900
230 (defvar std11-filename-coding-system nil
231 "Define coding-system for non-ASCII filename when send.
232 Set this variable coding system symbol (ie. 'iso-2022-jp) or nil.
233 If non-nil, std11-wrap-as-quoted-string use encode-coding-string.")
235 (defun std11-wrap-as-quoted-string (string)
236 "Wrap STRING as RFC 822 quoted-string."
238 ;; unlimited patch by simm-emacs@fan.gr.jp
239 ;; Mon, 10 Jan 2000 13:03:02 +0900
240 (if std11-filename-coding-system
241 (encode-coding-system string std11-filename-coding-system)
242 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list))
245 (defun std11-strip-quoted-pair (string)
246 "Strip quoted-pairs in STRING."
250 (len (length string))
253 (let ((chr (aref string i)))
255 (setq dest (concat dest (substring string b i))
260 (concat dest (substring string b))
263 (defun std11-strip-quoted-string (string)
264 "Strip quoted-string STRING."
265 (let ((len (length string)))
267 (let ((max (1- len)))
268 (and (eq (aref string 0) ?\")
269 (eq (aref string max) ?\")
270 (std11-strip-quoted-pair (substring string 1 max))
275 ;;; @ lexical analyze
278 (defcustom std11-lexical-analyzer
279 '(std11-analyze-quoted-string
280 std11-analyze-domain-literal
281 std11-analyze-comment
283 std11-analyze-special
285 "*List of functions to return result of lexical analyze.
286 Each function must have two arguments: STRING and START.
287 STRING is the target string to be analyzed.
288 START is start position of STRING to analyze.
290 Previous function is preferred to next function. If a function
291 returns nil, next function is used. Otherwise the return value will
295 :type '(repeat function))
298 (defconst std11-space-char-list '(? ?\t ?\n))
299 (defconst std11-special-char-list '(?\] ?\[
304 ;; (defconst std11-spaces-regexp
305 ;; (eval-when-compile (concat "[" std11-space-char-list "]+")))
307 (defconst std11-non-atom-regexp
309 (concat "[" std11-special-char-list std11-space-char-list "]")))
311 (defconst std11-atom-regexp
313 (concat "[^" std11-special-char-list std11-space-char-list "]+")))
315 (defun std11-analyze-spaces (string start)
316 (if (and (string-match (eval-when-compile
317 (concat "[" std11-space-char-list "]+"))
319 (= (match-beginning 0) start))
320 (let ((end (match-end 0)))
321 (cons (cons 'spaces (substring string start end))
322 ;;(substring string end)
326 (defun std11-analyze-special (string start)
327 (if (and (> (length string) start)
328 (memq (aref string start) std11-special-char-list))
329 (cons (cons 'specials (substring string start (1+ start)))
330 ;;(substring string 1)
334 (defun std11-analyze-atom (string start)
335 (if (string-match std11-non-atom-regexp string start)
336 (if (> (match-beginning 0) start)
337 (cons (cons 'atom (substring string start (match-beginning 0)))
340 (cons (cons 'atom (substring string start))
342 ;; (if (and (string-match std11-atom-regexp string start)
343 ;; (= (match-beginning 0) start))
344 ;; (let ((end (match-end 0)))
345 ;; (cons (cons 'atom (substring string start end))
346 ;; ;;(substring string end)
351 (defun std11-check-enclosure (string open close &optional recursive from)
352 (let ((len (length string))
356 (eq (aref string i) open))
361 (setq chr (aref string i))
374 (setq p (std11-check-enclosure
375 string open close recursive i))
385 (defun std11-analyze-quoted-string (string start)
386 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
388 (cons (cons 'quoted-string (substring string (1+ start) (1- p)))
389 ;;(substring string p))
393 (defun std11-analyze-domain-literal (string start)
394 (let ((p (std11-check-enclosure string ?\[ ?\] nil start)))
396 (cons (cons 'domain-literal (substring string (1+ start) (1- p)))
397 ;;(substring string p))
401 (defun std11-analyze-comment (string start)
402 (let ((p (std11-check-enclosure string ?\( ?\) t start)))
404 (cons (cons 'comment (substring string (1+ start) (1- p)))
405 ;;(substring string p))
410 (defun std11-lexical-analyze (string &optional analyzer start)
411 "Analyze STRING as lexical tokens of STD 11."
413 (setq analyzer std11-lexical-analyzer))
416 (let ((len (length string))
420 (let ((rest analyzer)
422 (while (and (setq func (car rest))
423 (null (setq r (funcall func string start))))
424 (setq rest (cdr rest)))
426 (cons (cons 'error (substring string start)) (1+ len)))
428 (setq dest (cons (car ret) dest)
438 (defun std11-ignored-token-p (token)
439 (let ((type (car token)))
440 (or (eq type 'spaces)(eq type 'comment))
443 (defun std11-parse-token (lal)
447 (setq token (car lal))
448 (std11-ignored-token-p token)
451 (setq itl (cons token itl))
453 (cons (nreverse (cons token itl))
457 (defun std11-parse-ascii-token (lal)
458 (let (token itl parsed token-value)
460 (setq token (car lal))
461 (or (std11-ignored-token-p token)
462 (if (and (setq token-value (cdr token))
463 (delq 'ascii (find-charset-string token-value)))
467 (setq itl (cons token itl))
470 (setq parsed (nreverse (cons token itl)))
472 (cons parsed (cdr lal))
475 (defun std11-parse-token-or-comment (lal)
479 (setq token (car lal))
480 (eq (car token) 'spaces)
483 (setq itl (cons token itl))
485 (cons (nreverse (cons token itl))
489 (defun std11-parse-word (lal)
490 (let ((ret (std11-parse-ascii-token lal)))
492 (let ((elt (car ret))
495 (if (or (assq 'atom elt)
496 (assq 'quoted-string elt))
497 (cons (cons 'word elt) rest)
500 (defun std11-parse-word-or-comment (lal)
501 (let ((ret (std11-parse-token-or-comment lal)))
503 (let ((elt (car ret))
506 (cond ((or (assq 'atom elt)
507 (assq 'quoted-string elt))
508 (cons (cons 'word elt) rest)
511 (cons (cons 'comment-word elt) rest)
515 (defun std11-parse-phrase (lal)
517 (while (setq ret (std11-parse-word-or-comment lal))
518 (setq phrase (append phrase (cdr (car ret))))
522 (cons (cons 'phrase phrase) lal)
525 (defun std11-parse-local-part (lal)
526 (let ((ret (std11-parse-word lal)))
528 (let ((local-part (cdr (car ret))) dot)
530 (while (and (setq ret (std11-parse-ascii-token lal))
532 (string-equal (cdr (assq 'specials dot)) ".")
533 (setq ret (std11-parse-word (cdr ret)))
535 (append local-part dot (cdr (car ret)))
539 (cons (cons 'local-part local-part) lal)
542 (defun std11-parse-sub-domain (lal)
543 (let ((ret (std11-parse-ascii-token lal)))
545 (let ((sub-domain (car ret)))
546 (if (or (assq 'atom sub-domain)
547 (assq 'domain-literal sub-domain)
549 (cons (cons 'sub-domain sub-domain)
554 (defun std11-parse-domain (lal)
555 (let ((ret (std11-parse-sub-domain lal)))
557 (let ((domain (cdr (car ret))) dot)
559 (while (and (setq ret (std11-parse-ascii-token lal))
561 (string-equal (cdr (assq 'specials dot)) ".")
562 (setq ret (std11-parse-sub-domain (cdr ret)))
564 (append domain dot (cdr (car ret)))
568 (cons (cons 'domain domain) lal)
571 (defun std11-parse-at-domain (lal)
572 (let ((ret (std11-parse-ascii-token lal)) at-sign)
574 (setq at-sign (car ret))
575 (string-equal (cdr (assq 'specials at-sign)) "@")
576 (setq ret (std11-parse-domain (cdr ret)))
578 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
582 (defun std11-parse-addr-spec (lal)
583 (let ((ret (std11-parse-local-part lal))
587 (setq addr (cdr (car ret)))
589 (and (setq ret (std11-parse-at-domain lal))
590 (setq addr (append addr (cdr (car ret))))
593 (cons (cons 'addr-spec addr) lal)
596 (defun std11-parse-route (lal)
597 (let ((ret (std11-parse-at-domain lal))
601 (setq route (cdr (car ret)))
603 (while (and (setq ret (std11-parse-ascii-token lal))
604 (setq comma (car ret))
605 (string-equal (cdr (assq 'specials comma)) ",")
606 (setq ret (std11-parse-at-domain (cdr ret)))
608 (setq route (append route comma (cdr (car ret))))
611 (and (setq ret (std11-parse-ascii-token lal))
612 (setq colon (car ret))
613 (string-equal (cdr (assq 'specials colon)) ":")
614 (setq route (append route colon))
617 (cons (cons 'route route)
622 (defun std11-parse-route-addr (lal)
623 (let ((ret (std11-parse-ascii-token lal))
627 (string-equal (cdr (assq 'specials <)) "<")
629 (progn (and (setq ret (std11-parse-route lal))
630 (setq route (cdr (car ret)))
633 (setq ret (std11-parse-addr-spec lal))
635 (setq addr-spec (cdr (car ret)))
637 (setq ret (std11-parse-ascii-token lal))
639 (string-equal (cdr (assq 'specials >)) ">")
641 (cons (cons 'route-addr (append route addr-spec))
646 (defun std11-parse-phrase-route-addr (lal)
647 (let ((ret (std11-parse-phrase lal)) phrase)
650 (setq phrase (cdr (car ret)))
653 (if (setq ret (std11-parse-route-addr lal))
654 (cons (list 'phrase-route-addr
660 (defun std11-parse-mailbox (lal)
661 (let ((ret (or (std11-parse-phrase-route-addr lal)
662 (std11-parse-addr-spec lal)))
666 (setq mbox (car ret))
668 (if (and (setq ret (std11-parse-token-or-comment lal))
669 (setq comment (cdr (assq 'comment (car ret))))
673 (cons (list 'mailbox mbox comment)
677 (defun std11-parse-group (lal)
678 (let ((ret (std11-parse-phrase lal))
679 phrase colon comma mbox semicolon)
681 (setq phrase (cdr (car ret)))
683 (setq ret (std11-parse-ascii-token lal))
684 (setq colon (car ret))
685 (string-equal (cdr (assq 'specials colon)) ":")
688 (and (setq ret (std11-parse-mailbox lal))
689 (setq mbox (list (car ret)))
692 (while (and (setq ret (std11-parse-ascii-token lal))
693 (setq comma (car ret))
695 (cdr (assq 'specials comma)) ",")
697 (setq ret (std11-parse-mailbox lal))
698 (setq mbox (cons (car ret) mbox))
702 (and (setq ret (std11-parse-ascii-token lal))
703 (setq semicolon (car ret))
704 (string-equal (cdr (assq 'specials semicolon)) ";")
706 (cons (list 'group phrase (nreverse mbox))
711 (defun std11-parse-address (lal)
712 (or (std11-parse-group lal)
713 (std11-parse-mailbox lal)
716 (defun std11-parse-addresses (lal)
717 (let ((ret (std11-parse-address lal)))
719 (let ((dest (list (car ret))))
721 (while (and (setq ret (std11-parse-ascii-token lal))
722 (string-equal (cdr (assq 'specials (car ret))) ",")
723 (setq ret (std11-parse-address (cdr ret)))
725 (setq dest (cons (car ret) dest))
731 (defun std11-parse-msg-id (lal)
732 (let ((ret (std11-parse-ascii-token lal))
736 (string-equal (cdr (assq 'specials <)) "<")
738 (setq ret (std11-parse-addr-spec lal))
739 (setq addr-spec (car ret))
741 (setq ret (std11-parse-ascii-token lal))
743 (string-equal (cdr (assq 'specials >)) ">")
745 (cons (cons 'msg-id (cdr addr-spec))
749 (defun std11-parse-msg-ids (tokens)
750 "Parse lexical TOKENS as `*(phrase / msg-id)', and return the result."
751 (let ((ret (or (std11-parse-msg-id tokens)
752 (std11-parse-phrase tokens))))
754 (let ((dest (list (car ret))))
755 (setq tokens (cdr ret))
756 (while (setq ret (or (std11-parse-msg-id tokens)
757 (std11-parse-phrase tokens)))
758 (setq dest (cons (car ret) dest))
759 (setq tokens (cdr ret))
764 (defalias 'std11-parse-in-reply-to 'std11-parse-msg-ids)
765 (make-obsolete 'std11-parse-in-reply-to 'std11-parse-msg-ids)
771 (defun std11-addr-to-string (seq)
772 "Return string from lexical analyzed list SEQ
773 represents addr-spec of RFC 822."
776 (let ((name (car token)))
778 ((eq name 'spaces) "")
779 ((eq name 'comment) "")
780 ((eq name 'quoted-string)
781 (concat "\"" (cdr token) "\""))
788 (defun std11-address-string (address)
789 "Return string of address part from parsed ADDRESS of RFC 822."
790 (cond ((eq (car address) 'group)
791 (mapconcat (function std11-address-string)
795 ((eq (car address) 'mailbox)
796 (let ((addr (nth 1 address)))
797 (std11-addr-to-string
798 (if (eq (car addr) 'phrase-route-addr)
804 (defun std11-comment-value-to-string (value)
806 (std11-strip-quoted-pair value)
811 (if (stringp (car value))
814 (std11-comment-value-to-string
823 (defun std11-full-name-string (address)
824 "Return string of full-name part from parsed ADDRESS of RFC 822."
825 (cond ((eq (car address) 'group)
832 ((eq (car address) 'mailbox)
833 (let ((addr (nth 1 address))
834 (comment (nth 2 address))
836 (if (eq (car addr) 'phrase-route-addr)
841 (let ((type (car token)))
842 (cond ((eq type 'quoted-string)
843 (std11-strip-quoted-pair (cdr token))
847 (std11-comment-value-to-string
856 (cond ((> (length phrase) 0) phrase)
857 (comment (std11-comment-value-to-string comment))
862 (defun std11-msg-id-string (msg-id)
863 "Return string from parsed MSG-ID of RFC 822."
864 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
868 (defun std11-fill-msg-id-list-string (string &optional column)
869 "Fill list of msg-id in STRING, and return the result."
872 (let ((lal (std11-lexical-analyze string))
874 (let ((ret (std11-parse-msg-id lal)))
876 (let* ((str (std11-msg-id-string (car ret)))
879 (if (> (+ len column) 76)
880 (setq dest (concat dest "\n " str)
883 column (+ column len))
885 (setq dest (concat dest (cdr (car lal)))
889 (let ((ret (std11-parse-msg-id lal)))
891 (let* ((str (std11-msg-id-string (car ret)))
892 (len (1+ (length str))))
894 (if (> (+ len column) 76)
895 (setq dest (concat dest "\n " str)
897 (setq dest (concat dest " " str)
898 column (+ column len))
900 (setq dest (concat dest (cdr (car lal)))
906 ;;; @ parser with lexical analyzer
910 (defun std11-parse-address-string (string)
911 "Parse STRING as mail address."
912 (std11-parse-address (std11-lexical-analyze string))
916 (defun std11-parse-addresses-string (string)
917 "Parse STRING as mail address list."
918 (std11-parse-addresses (std11-lexical-analyze string))
922 (defun std11-parse-msg-id-string (string)
923 "Parse STRING as msg-id."
924 (std11-parse-msg-id (std11-lexical-analyze string))
928 (defun std11-parse-msg-ids-string (string)
929 "Parse STRING as `*(phrase / msg-id)'."
930 (std11-parse-msg-ids (std11-lexical-analyze string))
934 (defun std11-extract-address-components (string)
935 "Extract full name and canonical address from STRING.
936 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
937 If no name can be extracted, FULL-NAME will be nil."
938 (let* ((structure (car (std11-parse-address-string
939 (std11-unfold-string string))))
940 (phrase (std11-full-name-string structure))
941 (address (std11-address-string structure))
943 (list phrase address)
952 ;;; std11.el ends here