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 (concat dest (substring string b))
223 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
225 (defun std11-wrap-as-quoted-string (string)
226 "Wrap STRING as RFC 822 quoted-string."
228 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
231 (defun std11-strip-quoted-pair (string)
232 "Strip quoted-pairs in STRING."
236 (len (length string))
239 (let ((chr (aref string i)))
241 (setq dest (concat dest (substring string b i))
246 (concat dest (substring string b))
249 (defun std11-strip-quoted-string (string)
250 "Strip quoted-string STRING."
251 (let ((len (length string)))
253 (let ((max (1- len)))
254 (and (eq (aref string 0) ?\")
255 (eq (aref string max) ?\")
256 (std11-strip-quoted-pair (substring string 1 max))
261 ;;; @ lexical analyze
264 (defcustom std11-lexical-analyzer
265 '(std11-analyze-quoted-string
266 std11-analyze-domain-literal
267 std11-analyze-comment
269 std11-analyze-special
271 "*List of functions to return result of lexical analyze.
272 Each function must have two arguments: STRING and START.
273 STRING is the target string to be analyzed.
274 START is start position of STRING to analyze.
276 Previous function is preferred to next function. If a function
277 returns nil, next function is used. Otherwise the return value will
281 :type '(repeat function))
284 (defconst std11-space-char-list '(? ?\t ?\n))
285 (defconst std11-special-char-list '(?\] ?\[
290 ;; (defconst std11-spaces-regexp
291 ;; (eval-when-compile (concat "[" std11-space-char-list "]+")))
293 (defconst std11-non-atom-regexp
295 (concat "[" std11-special-char-list std11-space-char-list "]")))
297 (defconst std11-atom-regexp
299 (concat "[^" std11-special-char-list std11-space-char-list "]+")))
301 (defun std11-analyze-spaces (string start)
302 (if (and (string-match (eval-when-compile
303 (concat "[" std11-space-char-list "]+"))
305 (= (match-beginning 0) start))
306 (let ((end (match-end 0)))
307 (cons (cons 'spaces (substring string start end))
308 ;;(substring string end)
312 (defun std11-analyze-special (string start)
313 (if (and (> (length string) start)
314 (memq (aref string start) std11-special-char-list))
315 (cons (cons 'specials (substring string start (1+ start)))
316 ;;(substring string 1)
320 (defun std11-analyze-atom (string start)
321 (if (string-match std11-non-atom-regexp string start)
322 (if (> (match-beginning 0) start)
323 (cons (cons 'atom (substring string start (match-beginning 0)))
326 (cons (cons 'atom (substring string start))
328 ;; (if (and (string-match std11-atom-regexp string start)
329 ;; (= (match-beginning 0) start))
330 ;; (let ((end (match-end 0)))
331 ;; (cons (cons 'atom (substring string start end))
332 ;; ;;(substring string end)
337 (defun std11-check-enclosure (string open close &optional recursive from)
338 (let ((len (length string))
342 (eq (aref string i) open))
347 (setq chr (aref string i))
360 (setq p (std11-check-enclosure
361 string open close recursive i))
371 (defun std11-analyze-quoted-string (string start)
372 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
374 (cons (cons 'quoted-string (substring string (1+ start) (1- p)))
375 ;;(substring string p))
379 (defun std11-analyze-domain-literal (string start)
380 (let ((p (std11-check-enclosure string ?\[ ?\] nil start)))
382 (cons (cons 'domain-literal (substring string (1+ start) (1- p)))
383 ;;(substring string p))
387 (defun std11-analyze-comment (string start)
388 (let ((p (std11-check-enclosure string ?\( ?\) t start)))
390 (cons (cons 'comment (substring string (1+ start) (1- p)))
391 ;;(substring string p))
396 (defun std11-lexical-analyze (string &optional analyzer start)
397 "Analyze STRING as lexical tokens of STD 11."
399 (setq analyzer std11-lexical-analyzer))
402 (let ((len (length string))
406 (let ((rest analyzer)
408 (while (and (setq func (car rest))
409 (null (setq r (funcall func string start))))
410 (setq rest (cdr rest)))
412 (cons (cons 'error (substring string start)) (1+ len)))
414 (setq dest (cons (car ret) dest)
424 (defun std11-ignored-token-p (token)
425 (let ((type (car token)))
426 (or (eq type 'spaces)(eq type 'comment))
429 (defun std11-parse-token (lal)
433 (setq token (car lal))
434 (std11-ignored-token-p token)
437 (setq itl (cons token itl))
439 (cons (nreverse (cons token itl))
443 (defun std11-parse-ascii-token (lal)
444 (let (token itl parsed token-value)
446 (setq token (car lal))
447 (or (std11-ignored-token-p token)
448 (if (and (setq token-value (cdr token))
449 (delq 'ascii (find-charset-string token-value)))
453 (setq itl (cons token itl))
456 (setq parsed (nreverse (cons token itl)))
458 (cons parsed (cdr lal))
461 (defun std11-parse-token-or-comment (lal)
465 (setq token (car lal))
466 (eq (car token) 'spaces)
469 (setq itl (cons token itl))
471 (cons (nreverse (cons token itl))
475 (defun std11-parse-word (lal)
476 (let ((ret (std11-parse-ascii-token lal)))
478 (let ((elt (car ret))
481 (if (or (assq 'atom elt)
482 (assq 'quoted-string elt))
483 (cons (cons 'word elt) rest)
486 (defun std11-parse-word-or-comment (lal)
487 (let ((ret (std11-parse-token-or-comment lal)))
489 (let ((elt (car ret))
492 (cond ((or (assq 'atom elt)
493 (assq 'quoted-string elt))
494 (cons (cons 'word elt) rest)
497 (cons (cons 'comment-word elt) rest)
501 (defun std11-parse-phrase (lal)
503 (while (setq ret (std11-parse-word-or-comment lal))
504 (setq phrase (append phrase (cdr (car ret))))
508 (cons (cons 'phrase phrase) lal)
511 (defun std11-parse-local-part (lal)
512 (let ((ret (std11-parse-word lal)))
514 (let ((local-part (cdr (car ret))) dot)
516 (while (and (setq ret (std11-parse-ascii-token lal))
518 (string-equal (cdr (assq 'specials dot)) ".")
519 (setq ret (std11-parse-word (cdr ret)))
521 (append local-part dot (cdr (car ret)))
525 (cons (cons 'local-part local-part) lal)
528 (defun std11-parse-sub-domain (lal)
529 (let ((ret (std11-parse-ascii-token lal)))
531 (let ((sub-domain (car ret)))
532 (if (or (assq 'atom sub-domain)
533 (assq 'domain-literal sub-domain)
535 (cons (cons 'sub-domain sub-domain)
540 (defun std11-parse-domain (lal)
541 (let ((ret (std11-parse-sub-domain lal)))
543 (let ((domain (cdr (car ret))) dot)
545 (while (and (setq ret (std11-parse-ascii-token lal))
547 (string-equal (cdr (assq 'specials dot)) ".")
548 (setq ret (std11-parse-sub-domain (cdr ret)))
550 (append domain dot (cdr (car ret)))
554 (cons (cons 'domain domain) lal)
557 (defun std11-parse-at-domain (lal)
558 (let ((ret (std11-parse-ascii-token lal)) at-sign)
560 (setq at-sign (car ret))
561 (string-equal (cdr (assq 'specials at-sign)) "@")
562 (setq ret (std11-parse-domain (cdr ret)))
564 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
568 (defun std11-parse-addr-spec (lal)
569 (let ((ret (std11-parse-local-part lal))
573 (setq addr (cdr (car ret)))
575 (and (setq ret (std11-parse-at-domain lal))
576 (setq addr (append addr (cdr (car ret))))
579 (cons (cons 'addr-spec addr) lal)
582 (defun std11-parse-route (lal)
583 (let ((ret (std11-parse-at-domain lal))
587 (setq route (cdr (car ret)))
589 (while (and (setq ret (std11-parse-ascii-token lal))
590 (setq comma (car ret))
591 (string-equal (cdr (assq 'specials comma)) ",")
592 (setq ret (std11-parse-at-domain (cdr ret)))
594 (setq route (append route comma (cdr (car ret))))
597 (and (setq ret (std11-parse-ascii-token lal))
598 (setq colon (car ret))
599 (string-equal (cdr (assq 'specials colon)) ":")
600 (setq route (append route colon))
603 (cons (cons 'route route)
608 (defun std11-parse-route-addr (lal)
609 (let ((ret (std11-parse-ascii-token lal))
613 (string-equal (cdr (assq 'specials <)) "<")
615 (progn (and (setq ret (std11-parse-route lal))
616 (setq route (cdr (car ret)))
619 (setq ret (std11-parse-addr-spec lal))
621 (setq addr-spec (cdr (car ret)))
623 (setq ret (std11-parse-ascii-token lal))
625 (string-equal (cdr (assq 'specials >)) ">")
627 (cons (cons 'route-addr (append route addr-spec))
632 (defun std11-parse-phrase-route-addr (lal)
633 (let ((ret (std11-parse-phrase lal)) phrase)
636 (setq phrase (cdr (car ret)))
639 (if (setq ret (std11-parse-route-addr lal))
640 (cons (list 'phrase-route-addr
646 (defun std11-parse-mailbox (lal)
647 (let ((ret (or (std11-parse-phrase-route-addr lal)
648 (std11-parse-addr-spec lal)))
652 (setq mbox (car ret))
654 (if (and (setq ret (std11-parse-token-or-comment lal))
655 (setq comment (cdr (assq 'comment (car ret))))
659 (cons (list 'mailbox mbox comment)
663 (defun std11-parse-group (lal)
664 (let ((ret (std11-parse-phrase lal))
665 phrase colon comma mbox semicolon)
667 (setq phrase (cdr (car ret)))
669 (setq ret (std11-parse-ascii-token lal))
670 (setq colon (car ret))
671 (string-equal (cdr (assq 'specials colon)) ":")
674 (and (setq ret (std11-parse-mailbox lal))
675 (setq mbox (list (car ret)))
678 (while (and (setq ret (std11-parse-ascii-token lal))
679 (setq comma (car ret))
681 (cdr (assq 'specials comma)) ",")
683 (setq ret (std11-parse-mailbox lal))
684 (setq mbox (cons (car ret) mbox))
688 (and (setq ret (std11-parse-ascii-token lal))
689 (setq semicolon (car ret))
690 (string-equal (cdr (assq 'specials semicolon)) ";")
692 (cons (list 'group phrase (nreverse mbox))
697 (defun std11-parse-address (lal)
698 (or (std11-parse-group lal)
699 (std11-parse-mailbox lal)
702 (defun std11-parse-addresses (lal)
703 (let ((ret (std11-parse-address lal)))
705 (let ((dest (list (car ret))))
707 (while (and (setq ret (std11-parse-ascii-token lal))
708 (string-equal (cdr (assq 'specials (car ret))) ",")
709 (setq ret (std11-parse-address (cdr ret)))
711 (setq dest (cons (car ret) dest))
717 (defun std11-parse-msg-id (lal)
718 (let ((ret (std11-parse-ascii-token lal))
722 (string-equal (cdr (assq 'specials <)) "<")
724 (setq ret (std11-parse-addr-spec lal))
725 (setq addr-spec (car ret))
727 (setq ret (std11-parse-ascii-token lal))
729 (string-equal (cdr (assq 'specials >)) ">")
731 (cons (cons 'msg-id (cdr addr-spec))
735 (defun std11-parse-msg-ids (tokens)
736 "Parse lexical TOKENS as `*(phrase / msg-id)', and return the result."
737 (let ((ret (or (std11-parse-msg-id tokens)
738 (std11-parse-phrase tokens))))
740 (let ((dest (list (car ret))))
741 (setq tokens (cdr ret))
742 (while (setq ret (or (std11-parse-msg-id tokens)
743 (std11-parse-phrase tokens)))
744 (setq dest (cons (car ret) dest))
745 (setq tokens (cdr ret))
750 (defalias 'std11-parse-in-reply-to 'std11-parse-msg-ids)
751 (make-obsolete 'std11-parse-in-reply-to 'std11-parse-msg-ids)
757 (defun std11-addr-to-string (seq)
758 "Return string from lexical analyzed list SEQ
759 represents addr-spec of RFC 822."
762 (let ((name (car token)))
764 ((eq name 'spaces) "")
765 ((eq name 'comment) "")
766 ((eq name 'quoted-string)
767 (concat "\"" (cdr token) "\""))
774 (defun std11-address-string (address)
775 "Return string of address part from parsed ADDRESS of RFC 822."
776 (cond ((eq (car address) 'group)
777 (mapconcat (function std11-address-string)
781 ((eq (car address) 'mailbox)
782 (let ((addr (nth 1 address)))
783 (std11-addr-to-string
784 (if (eq (car addr) 'phrase-route-addr)
790 (defun std11-comment-value-to-string (value)
792 (std11-strip-quoted-pair value)
797 (if (stringp (car value))
800 (std11-comment-value-to-string
809 (defun std11-full-name-string (address)
810 "Return string of full-name part from parsed ADDRESS of RFC 822."
811 (cond ((eq (car address) 'group)
818 ((eq (car address) 'mailbox)
819 (let ((addr (nth 1 address))
820 (comment (nth 2 address))
822 (if (eq (car addr) 'phrase-route-addr)
827 (let ((type (car token)))
828 (cond ((eq type 'quoted-string)
829 (std11-strip-quoted-pair (cdr token))
833 (std11-comment-value-to-string
842 (cond ((> (length phrase) 0) phrase)
843 (comment (std11-comment-value-to-string comment))
848 (defun std11-msg-id-string (msg-id)
849 "Return string from parsed MSG-ID of RFC 822."
850 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
854 (defun std11-fill-msg-id-list-string (string &optional column)
855 "Fill list of msg-id in STRING, and return the result."
858 (let ((lal (std11-lexical-analyze string))
860 (let ((ret (std11-parse-msg-id lal)))
862 (let* ((str (std11-msg-id-string (car ret)))
865 (if (> (+ len column) 76)
866 (setq dest (concat dest "\n " str)
869 column (+ column len))
871 (setq dest (concat dest (cdr (car lal)))
875 (let ((ret (std11-parse-msg-id lal)))
877 (let* ((str (std11-msg-id-string (car ret)))
878 (len (1+ (length str))))
880 (if (> (+ len column) 76)
881 (setq dest (concat dest "\n " str)
883 (setq dest (concat dest " " str)
884 column (+ column len))
886 (setq dest (concat dest (cdr (car lal)))
892 ;;; @ parser with lexical analyzer
896 (defun std11-parse-address-string (string)
897 "Parse STRING as mail address."
898 (std11-parse-address (std11-lexical-analyze string))
902 (defun std11-parse-addresses-string (string)
903 "Parse STRING as mail address list."
904 (std11-parse-addresses (std11-lexical-analyze string))
908 (defun std11-parse-msg-id-string (string)
909 "Parse STRING as msg-id."
910 (std11-parse-msg-id (std11-lexical-analyze string))
914 (defun std11-parse-msg-ids-string (string)
915 "Parse STRING as `*(phrase / msg-id)'."
916 (std11-parse-msg-ids (std11-lexical-analyze string))
920 (defun std11-extract-address-components (string)
921 "Extract full name and canonical address from STRING.
922 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
923 If no name can be extracted, FULL-NAME will be nil."
924 (let* ((structure (car (std11-parse-address-string
925 (std11-unfold-string string))))
926 (phrase (std11-full-name-string structure))
927 (address (std11-address-string structure))
929 (list phrase address)
938 ;;; std11.el ends here