1 ;;; std11.el --- STD 11 functions for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998 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 MU (Message Utilities).
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 (autoload 'buffer-substring-no-properties "emu")
28 (autoload 'member "emu")
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. [std11.el]"
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)))
51 (defsubst std11-fetch-field (name)
52 "Return the value of the header field NAME.
53 The buffer is expected to be narrowed to just the headers of the message."
55 (goto-char (point-min))
56 (let ((case-fold-search t))
57 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
58 (buffer-substring-no-properties (match-end 0) (std11-field-end))
61 (defun std11-field-body (name &optional boundary)
62 "Return the value of the header field NAME.
63 If BOUNDARY is not nil, it is used as message header separator."
66 (std11-narrow-to-header boundary)
67 (std11-fetch-field name)
70 (defun std11-find-field-body (field-names &optional boundary)
71 "Return the first found field-body specified by FIELD-NAMES
72 of the message header in current buffer. If BOUNDARY is not nil, it is
73 used as message header separator. [std11.el]"
76 (std11-narrow-to-header boundary)
77 (let ((case-fold-search t)
80 (while (setq field-name (car field-names))
81 (goto-char (point-min))
82 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
84 (buffer-substring-no-properties
85 (match-end 0) (std11-field-end)))
87 (setq field-names (cdr field-names))
90 (defun std11-field-bodies (field-names &optional default-value boundary)
91 "Return list of each field-bodies of FIELD-NAMES of the message header
92 in current buffer. If BOUNDARY is not nil, it is used as message
93 header separator. [std11.el]"
96 (std11-narrow-to-header boundary)
97 (let* ((case-fold-search t)
98 (dest (make-list (length field-names) default-value))
102 (while (setq field-name (car s-rest))
103 (goto-char (point-min))
104 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
106 (buffer-substring-no-properties
107 (match-end 0) (std11-field-end)))
109 (setq s-rest (cdr s-rest)
118 (defun std11-unfold-string (string)
119 "Unfold STRING as message header field."
122 (while (string-match "\n\\([ \t]\\)" string p)
123 (setq dest (concat dest
124 (substring string p (match-beginning 0))
127 (setq p (match-end 0)))
130 (concat dest (substring string p))
137 (defun std11-narrow-to-header (&optional boundary)
138 "Narrow to the message header.
139 If BOUNDARY is not nil, it is used as message header separator.
142 (goto-char (point-min))
143 (if (re-search-forward
144 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
150 (defun std11-header-string (regexp &optional boundary)
151 "Return string of message header fields matched by REGEXP.
152 If BOUNDARY is not nil, it is used as message header separator.
154 (let ((case-fold-search t))
157 (std11-narrow-to-header boundary)
158 (goto-char (point-min))
160 (while (re-search-forward std11-field-head-regexp nil t)
162 (buffer-substring (match-beginning 0) (std11-field-end)))
163 (if (string-match regexp field)
164 (setq header (concat header field "\n"))
169 (defun std11-header-string-except (regexp &optional boundary)
170 "Return string of message header fields not matched by REGEXP.
171 If BOUNDARY is not nil, it is used as message header separator.
173 (let ((case-fold-search t))
176 (std11-narrow-to-header boundary)
177 (goto-char (point-min))
179 (while (re-search-forward std11-field-head-regexp nil t)
181 (buffer-substring (match-beginning 0) (std11-field-end)))
182 (if (not (string-match regexp field))
183 (setq header (concat header field "\n"))
188 (defun std11-collect-field-names (&optional boundary)
189 "Return list of all field-names of the message header in current buffer.
190 If BOUNDARY is not nil, it is used as message header separator.
194 (std11-narrow-to-header boundary)
195 (goto-char (point-min))
197 (while (re-search-forward std11-field-head-regexp nil t)
198 (setq name (buffer-substring-no-properties
199 (match-beginning 0)(1- (match-end 0))))
200 (or (member name dest)
201 (setq dest (cons name dest))
210 (defun std11-wrap-as-quoted-pairs (string specials)
214 (len (length string))
217 (let ((chr (aref string i)))
218 (if (memq chr specials)
219 (setq dest (concat dest (substring string b i) "\\")
224 (concat dest (substring string b))
227 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
229 (defun std11-wrap-as-quoted-string (string)
230 "Wrap STRING as RFC 822 quoted-string. [std11.el]"
232 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
235 (defun std11-strip-quoted-pair (string)
236 "Strip quoted-pairs in STRING. [std11.el]"
240 (len (length string))
243 (let ((chr (aref string i)))
245 (setq dest (concat dest (substring string b i))
250 (concat dest (substring string b))
253 (defun std11-strip-quoted-string (string)
254 "Strip quoted-string STRING. [std11.el]"
255 (let ((len (length string)))
257 (let ((max (1- len)))
258 (and (eq (aref string 0) ?\")
259 (eq (aref string max) ?\")
260 (std11-strip-quoted-pair (substring string 1 max))
265 ;;; @ lexical analyze
268 (defconst std11-space-chars " \t\n")
269 (defconst std11-spaces-regexp (` (, (concat "[" std11-space-chars "]+"))))
270 (defconst std11-special-char-list '(?\] ?\[
274 (defconst std11-atom-regexp
275 (` (, (concat "^[^" std11-special-char-list std11-space-chars "]+"))))
277 (defun std11-analyze-spaces (string)
278 (if (and (string-match std11-spaces-regexp string)
279 (= (match-beginning 0) 0))
280 (let ((end (match-end 0)))
281 (cons (cons 'spaces (substring string 0 end))
282 (substring string end)
285 (defun std11-analyze-special (str)
286 (if (and (> (length str) 0)
287 (memq (aref str 0) std11-special-char-list))
288 (cons (cons 'specials (substring str 0 1))
292 (defun std11-analyze-atom (str)
293 (if (string-match std11-atom-regexp str)
294 (let ((end (match-end 0)))
295 (cons (cons 'atom (substring str 0 end))
299 (defun std11-check-enclosure (str open close &optional recursive from)
300 (let ((len (length str))
304 (eq (aref str i) open))
309 (setq chr (aref str i))
322 (setq p (std11-check-enclosure
323 str open close recursive i))
333 (defun std11-analyze-quoted-string (str)
334 (let ((p (std11-check-enclosure str ?\" ?\")))
336 (cons (cons 'quoted-string (substring str 1 (1- p)))
340 (defun std11-analyze-domain-literal (str)
341 (let ((p (std11-check-enclosure str ?\[ ?\])))
343 (cons (cons 'domain-literal (substring str 1 (1- p)))
347 (defun std11-analyze-comment (str)
348 (let ((p (std11-check-enclosure str ?\( ?\) t)))
350 (cons (cons 'comment (substring str 1 (1- p)))
354 (defun std11-lexical-analyze (str)
356 (while (not (string-equal str ""))
358 (or (std11-analyze-quoted-string str)
359 (std11-analyze-domain-literal str)
360 (std11-analyze-comment str)
361 (std11-analyze-spaces str)
362 (std11-analyze-special str)
363 (std11-analyze-atom str)
366 (setq dest (cons (car ret) dest))
376 (defun std11-ignored-token-p (token)
377 (let ((type (car token)))
378 (or (eq type 'spaces)(eq type 'comment))
381 (defun std11-parse-token (lal)
385 (setq token (car lal))
386 (std11-ignored-token-p token)
389 (setq itl (cons token itl))
391 (cons (nreverse (cons token itl))
395 (defun std11-parse-ascii-token (lal)
396 (let (token itl parsed token-value)
398 (setq token (car lal))
399 (or (std11-ignored-token-p token)
400 (if (and (setq token-value (cdr token))
401 (find-non-ascii-charset-string token-value)
406 (setq itl (cons token itl))
409 (setq parsed (nreverse (cons token itl)))
411 (cons parsed (cdr lal))
414 (defun std11-parse-token-or-comment (lal)
418 (setq token (car lal))
419 (eq (car token) 'spaces)
422 (setq itl (cons token itl))
424 (cons (nreverse (cons token itl))
428 (defun std11-parse-word (lal)
429 (let ((ret (std11-parse-ascii-token lal)))
431 (let ((elt (car ret))
434 (if (or (assq 'atom elt)
435 (assq 'quoted-string elt))
436 (cons (cons 'word elt) rest)
439 (defun std11-parse-word-or-comment (lal)
440 (let ((ret (std11-parse-token-or-comment lal)))
442 (let ((elt (car ret))
445 (cond ((or (assq 'atom elt)
446 (assq 'quoted-string elt))
447 (cons (cons 'word elt) rest)
450 (cons (cons 'comment-word elt) rest)
454 (defun std11-parse-phrase (lal)
456 (while (setq ret (std11-parse-word-or-comment lal))
457 (setq phrase (append phrase (cdr (car ret))))
461 (cons (cons 'phrase phrase) lal)
464 (defun std11-parse-local-part (lal)
465 (let ((ret (std11-parse-word lal)))
467 (let ((local-part (cdr (car ret))) dot)
469 (while (and (setq ret (std11-parse-ascii-token lal))
471 (string-equal (cdr (assq 'specials dot)) ".")
472 (setq ret (std11-parse-word (cdr ret)))
474 (append local-part dot (cdr (car ret)))
478 (cons (cons 'local-part local-part) lal)
481 (defun std11-parse-sub-domain (lal)
482 (let ((ret (std11-parse-ascii-token lal)))
484 (let ((sub-domain (car ret)))
485 (if (or (assq 'atom sub-domain)
486 (assq 'domain-literal sub-domain)
488 (cons (cons 'sub-domain sub-domain)
493 (defun std11-parse-domain (lal)
494 (let ((ret (std11-parse-sub-domain lal)))
496 (let ((domain (cdr (car ret))) dot)
498 (while (and (setq ret (std11-parse-ascii-token lal))
500 (string-equal (cdr (assq 'specials dot)) ".")
501 (setq ret (std11-parse-sub-domain (cdr ret)))
503 (append domain dot (cdr (car ret)))
507 (cons (cons 'domain domain) lal)
510 (defun std11-parse-at-domain (lal)
511 (let ((ret (std11-parse-ascii-token lal)) at-sign)
513 (setq at-sign (car ret))
514 (string-equal (cdr (assq 'specials at-sign)) "@")
515 (setq ret (std11-parse-domain (cdr ret)))
517 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
521 (defun std11-parse-addr-spec (lal)
522 (let ((ret (std11-parse-local-part lal))
526 (setq addr (cdr (car ret)))
528 (and (setq ret (std11-parse-at-domain lal))
529 (setq addr (append addr (cdr (car ret))))
532 (cons (cons 'addr-spec addr) lal)
535 (defun std11-parse-route (lal)
536 (let ((ret (std11-parse-at-domain lal))
540 (setq route (cdr (car ret)))
542 (while (and (setq ret (std11-parse-ascii-token lal))
543 (setq comma (car ret))
544 (string-equal (cdr (assq 'specials comma)) ",")
545 (setq ret (std11-parse-at-domain (cdr ret)))
547 (setq route (append route comma (cdr (car ret))))
550 (and (setq ret (std11-parse-ascii-token lal))
551 (setq colon (car ret))
552 (string-equal (cdr (assq 'specials colon)) ":")
553 (setq route (append route colon))
556 (cons (cons 'route route)
561 (defun std11-parse-route-addr (lal)
562 (let ((ret (std11-parse-ascii-token lal))
566 (string-equal (cdr (assq 'specials <)) "<")
568 (progn (and (setq ret (std11-parse-route lal))
569 (setq route (cdr (car ret)))
572 (setq ret (std11-parse-addr-spec lal))
574 (setq addr-spec (cdr (car ret)))
576 (setq ret (std11-parse-ascii-token lal))
578 (string-equal (cdr (assq 'specials >)) ">")
580 (cons (cons 'route-addr (append route addr-spec))
585 (defun std11-parse-phrase-route-addr (lal)
586 (let ((ret (std11-parse-phrase lal)) phrase)
589 (setq phrase (cdr (car ret)))
592 (if (setq ret (std11-parse-route-addr lal))
593 (cons (list 'phrase-route-addr
599 (defun std11-parse-mailbox (lal)
600 (let ((ret (or (std11-parse-phrase-route-addr lal)
601 (std11-parse-addr-spec lal)))
605 (setq mbox (car ret))
607 (if (and (setq ret (std11-parse-token-or-comment lal))
608 (setq comment (cdr (assq 'comment (car ret))))
612 (cons (list 'mailbox mbox comment)
616 (defun std11-parse-group (lal)
617 (let ((ret (std11-parse-phrase lal))
618 phrase colon comma mbox semicolon)
620 (setq phrase (cdr (car ret)))
622 (setq ret (std11-parse-ascii-token lal))
623 (setq colon (car ret))
624 (string-equal (cdr (assq 'specials colon)) ":")
627 (and (setq ret (std11-parse-mailbox lal))
628 (setq mbox (list (car ret)))
631 (while (and (setq ret (std11-parse-ascii-token lal))
632 (setq comma (car ret))
634 (cdr (assq 'specials comma)) ",")
636 (setq ret (std11-parse-mailbox lal))
637 (setq mbox (cons (car ret) mbox))
641 (and (setq ret (std11-parse-ascii-token lal))
642 (setq semicolon (car ret))
643 (string-equal (cdr (assq 'specials semicolon)) ";")
645 (cons (list 'group phrase (nreverse mbox))
650 (defun std11-parse-address (lal)
651 (or (std11-parse-group lal)
652 (std11-parse-mailbox lal)
655 (defun std11-parse-addresses (lal)
656 (let ((ret (std11-parse-address lal)))
658 (let ((dest (list (car ret))))
660 (while (and (setq ret (std11-parse-ascii-token lal))
661 (string-equal (cdr (assq 'specials (car ret))) ",")
662 (setq ret (std11-parse-address (cdr ret)))
664 (setq dest (cons (car ret) dest))
670 (defun std11-parse-msg-id (lal)
671 (let ((ret (std11-parse-ascii-token lal))
675 (string-equal (cdr (assq 'specials <)) "<")
677 (setq ret (std11-parse-addr-spec lal))
678 (setq addr-spec (car ret))
680 (setq ret (std11-parse-ascii-token lal))
682 (string-equal (cdr (assq 'specials >)) ">")
684 (cons (cons 'msg-id (cdr addr-spec))
688 (defun std11-parse-in-reply-to (tokens)
689 "Parse lexical TOKENS as In-Reply-To field, and return the result."
690 (let ((ret (or (std11-parse-msg-id tokens)
691 (std11-parse-phrase tokens))))
693 (let ((dest (list (car ret))))
694 (setq tokens (cdr ret))
695 (while (setq ret (or (std11-parse-msg-id tokens)
696 (std11-parse-phrase tokens)))
697 (setq dest (cons (car ret) dest))
698 (setq tokens (cdr ret))
707 (defun std11-addr-to-string (seq)
708 "Return string from lexical analyzed list SEQ
709 represents addr-spec of RFC 822. [std11.el]"
712 (let ((name (car token)))
714 ((eq name 'spaces) "")
715 ((eq name 'comment) "")
716 ((eq name 'quoted-string)
717 (concat "\"" (cdr token) "\""))
723 (defun std11-address-string (address)
724 "Return string of address part from parsed ADDRESS of RFC 822.
726 (cond ((eq (car address) 'group)
727 (mapconcat (function std11-address-string)
731 ((eq (car address) 'mailbox)
732 (let ((addr (nth 1 address)))
733 (std11-addr-to-string
734 (if (eq (car addr) 'phrase-route-addr)
740 (defun std11-full-name-string (address)
741 "Return string of full-name part from parsed ADDRESS of RFC 822.
743 (cond ((eq (car address) 'group)
750 ((eq (car address) 'mailbox)
751 (let ((addr (nth 1 address))
752 (comment (nth 2 address))
754 (if (eq (car addr) 'phrase-route-addr)
759 (let ((type (car token)))
760 (cond ((eq type 'quoted-string)
761 (std11-strip-quoted-pair (cdr token))
766 (std11-strip-quoted-pair (cdr token))
774 (cond ((> (length phrase) 0) phrase)
775 (comment (std11-strip-quoted-pair comment))
779 (defun std11-msg-id-string (msg-id)
780 "Return string from parsed MSG-ID of RFC 822."
781 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
784 (defun std11-fill-msg-id-list-string (string &optional column)
785 "Fill list of msg-id in STRING, and return the result."
788 (let ((lal (std11-lexical-analyze string))
790 (let ((ret (std11-parse-msg-id lal)))
792 (let* ((str (std11-msg-id-string (car ret)))
795 (if (> (+ len column) 76)
796 (setq dest (concat dest "\n " str)
799 column (+ column len))
801 (setq dest (concat dest (cdr (car lal)))
805 (let ((ret (std11-parse-msg-id lal)))
807 (let* ((str (std11-msg-id-string (car ret)))
808 (len (1+ (length str))))
810 (if (> (+ len column) 76)
811 (setq dest (concat dest "\n " str)
813 (setq dest (concat dest " " str)
814 column (+ column len))
816 (setq dest (concat dest (cdr (car lal)))
822 ;;; @ parser with lexical analyzer
825 (defun std11-parse-address-string (string)
826 "Parse STRING as mail address. [std11.el]"
827 (std11-parse-address (std11-lexical-analyze string))
830 (defun std11-parse-addresses-string (string)
831 "Parse STRING as mail address list. [std11.el]"
832 (std11-parse-addresses (std11-lexical-analyze string))
835 (defun std11-extract-address-components (string)
836 "Extract full name and canonical address from STRING.
837 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
838 If no name can be extracted, FULL-NAME will be nil. [std11.el]"
839 (let* ((structure (car (std11-parse-address-string
840 (std11-unfold-string string))))
841 (phrase (std11-full-name-string structure))
842 (address (std11-address-string structure))
844 (list phrase address)
853 ;;; std11.el ends here