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)
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 ()
41 "Move to end of field and return this point."
42 (if (re-search-forward std11-next-field-head-regexp nil t)
43 (goto-char (match-beginning 0))
44 (if (re-search-forward "^$" nil 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 "]+")))
292 (defconst std11-atom-regexp
294 (concat "[^" std11-special-char-list std11-space-char-list "]+")))
296 (defun std11-analyze-spaces (string start)
297 (if (and (string-match (eval-when-compile
298 (concat "[" std11-space-char-list "]+"))
300 (= (match-beginning 0) start))
301 (let ((end (match-end 0)))
302 (cons (cons 'spaces (substring string start end))
303 ;;(substring string end)
307 (defun std11-analyze-special (string start)
308 (if (and (> (length string) start)
309 (memq (aref string start) std11-special-char-list))
310 (cons (cons 'specials (substring string start (1+ start)))
311 ;;(substring string 1)
315 (defun std11-analyze-atom (string start)
316 (if (and (string-match std11-atom-regexp string start)
317 (= (match-beginning 0) start))
318 (let ((end (match-end 0)))
319 (cons (cons 'atom (substring string start end))
320 ;;(substring string end)
324 (defun std11-check-enclosure (string open close &optional recursive from)
325 (let ((len (length string))
329 (eq (aref string i) open))
334 (setq chr (aref string i))
347 (setq p (std11-check-enclosure
348 string open close recursive i))
358 (defun std11-analyze-quoted-string (string start)
359 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
361 (cons (cons 'quoted-string (substring string (1+ start) (1- p)))
362 ;;(substring string p))
366 (defun std11-analyze-domain-literal (string start)
367 (let ((p (std11-check-enclosure string ?\[ ?\] nil start)))
369 (cons (cons 'domain-literal (substring string (1+ start) (1- p)))
370 ;;(substring string p))
374 (defun std11-analyze-comment (string start)
375 (let ((p (std11-check-enclosure string ?\( ?\) t start)))
377 (cons (cons 'comment (substring string (1+ start) (1- p)))
378 ;;(substring string p))
383 (defun std11-lexical-analyze (string &optional analyzer start)
384 "Analyze STRING as lexical tokens of STD 11."
386 (setq analyzer std11-lexical-analyzer))
389 (let ((len (length string))
393 (let ((rest analyzer)
395 (while (and (setq func (car rest))
396 (null (setq r (funcall func string start))))
397 (setq rest (cdr rest)))
399 (list (cons 'error (substring string start)) (1+ len)))
401 (setq dest (cons (car ret) dest)
411 (defun std11-ignored-token-p (token)
412 (let ((type (car token)))
413 (or (eq type 'spaces)(eq type 'comment))
416 (defun std11-parse-token (lal)
420 (setq token (car lal))
421 (std11-ignored-token-p token)
424 (setq itl (cons token itl))
426 (cons (nreverse (cons token itl))
430 (defun std11-parse-ascii-token (lal)
431 (let (token itl parsed token-value)
433 (setq token (car lal))
434 (or (std11-ignored-token-p token)
435 (if (and (setq token-value (cdr token))
436 (find-non-ascii-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-in-reply-to (tokens)
724 "Parse lexical TOKENS as In-Reply-To field, 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))
742 (defun std11-addr-to-string (seq)
743 "Return string from lexical analyzed list SEQ
744 represents addr-spec of RFC 822."
747 (let ((name (car token)))
749 ((eq name 'spaces) "")
750 ((eq name 'comment) "")
751 ((eq name 'quoted-string)
752 (concat "\"" (cdr token) "\""))
759 (defun std11-address-string (address)
760 "Return string of address part from parsed ADDRESS of RFC 822."
761 (cond ((eq (car address) 'group)
762 (mapconcat (function std11-address-string)
766 ((eq (car address) 'mailbox)
767 (let ((addr (nth 1 address)))
768 (std11-addr-to-string
769 (if (eq (car addr) 'phrase-route-addr)
775 (defun std11-comment-value-to-string (value)
777 (std11-strip-quoted-pair value)
782 (if (stringp (car value))
785 (std11-comment-value-to-string
794 (defun std11-full-name-string (address)
795 "Return string of full-name part from parsed ADDRESS of RFC 822."
796 (cond ((eq (car address) 'group)
803 ((eq (car address) 'mailbox)
804 (let ((addr (nth 1 address))
805 (comment (nth 2 address))
807 (if (eq (car addr) 'phrase-route-addr)
812 (let ((type (car token)))
813 (cond ((eq type 'quoted-string)
814 (std11-strip-quoted-pair (cdr token))
818 (std11-comment-value-to-string
827 (cond ((> (length phrase) 0) phrase)
828 (comment (std11-comment-value-to-string comment))
833 (defun std11-msg-id-string (msg-id)
834 "Return string from parsed MSG-ID of RFC 822."
835 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
839 (defun std11-fill-msg-id-list-string (string &optional column)
840 "Fill list of msg-id in STRING, and return the result."
843 (let ((lal (std11-lexical-analyze string))
845 (let ((ret (std11-parse-msg-id lal)))
847 (let* ((str (std11-msg-id-string (car ret)))
850 (if (> (+ len column) 76)
851 (setq dest (concat dest "\n " str)
854 column (+ column len))
856 (setq dest (concat dest (cdr (car lal)))
860 (let ((ret (std11-parse-msg-id lal)))
862 (let* ((str (std11-msg-id-string (car ret)))
863 (len (1+ (length str))))
865 (if (> (+ len column) 76)
866 (setq dest (concat dest "\n " str)
868 (setq dest (concat dest " " str)
869 column (+ column len))
871 (setq dest (concat dest (cdr (car lal)))
877 ;;; @ parser with lexical analyzer
881 (defun std11-parse-address-string (string)
882 "Parse STRING as mail address."
883 (std11-parse-address (std11-lexical-analyze string))
887 (defun std11-parse-addresses-string (string)
888 "Parse STRING as mail address list."
889 (std11-parse-addresses (std11-lexical-analyze string))
893 (defun std11-extract-address-components (string)
894 "Extract full name and canonical address from STRING.
895 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
896 If no name can be extracted, FULL-NAME will be nil."
897 (let* ((structure (car (std11-parse-address-string
898 (std11-unfold-string string))))
899 (phrase (std11-full-name-string structure))
900 (address (std11-address-string structure))
902 (list phrase address)
911 ;;; std11.el ends here