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 (defun std11-field-body (name &optional boundary)
52 "Return body of field NAME.
53 If BOUNDARY is not nil, it is used as message header separator.
57 (std11-narrow-to-header boundary)
58 (goto-char (point-min))
59 (let ((case-fold-search t))
60 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
61 (buffer-substring-no-properties (match-end 0) (std11-field-end))
64 (defun std11-find-field-body (field-names &optional boundary)
65 "Return the first found field-body specified by FIELD-NAMES
66 of the message header in current buffer. If BOUNDARY is not nil, it is
67 used as message header separator. [std11.el]"
70 (std11-narrow-to-header boundary)
71 (let ((case-fold-search t)
74 (while (setq field-name (car field-names))
75 (goto-char (point-min))
76 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
78 (buffer-substring-no-properties
79 (match-end 0) (std11-field-end)))
81 (setq field-names (cdr field-names))
84 (defun std11-field-bodies (field-names &optional default-value boundary)
85 "Return list of each field-bodies of FIELD-NAMES of the message header
86 in current buffer. If BOUNDARY is not nil, it is used as message
87 header separator. [std11.el]"
90 (std11-narrow-to-header boundary)
91 (let* ((case-fold-search t)
92 (dest (make-list (length field-names) default-value))
96 (while (setq field-name (car s-rest))
97 (goto-char (point-min))
98 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
100 (buffer-substring-no-properties
101 (match-end 0) (std11-field-end)))
103 (setq s-rest (cdr s-rest)
112 (defun std11-unfold-string (string)
113 "Unfold STRING as message header field. [std11.el]"
115 (while (string-match "\n\\([ \t]\\)" string)
116 (setq dest (concat dest
117 (substring string 0 (match-beginning 0))
118 (match-string 1 string)
120 (setq string (substring string (match-end 0)))
129 (defun std11-narrow-to-header (&optional boundary)
130 "Narrow to the message header.
131 If BOUNDARY is not nil, it is used as message header separator.
134 (goto-char (point-min))
135 (if (re-search-forward
136 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
142 (defun std11-header-string (regexp &optional boundary)
143 "Return string of message header fields matched by REGEXP.
144 If BOUNDARY is not nil, it is used as message header separator.
146 (let ((case-fold-search t))
149 (std11-narrow-to-header boundary)
150 (goto-char (point-min))
152 (while (re-search-forward std11-field-head-regexp nil t)
154 (buffer-substring (match-beginning 0) (std11-field-end)))
155 (if (string-match regexp field)
156 (setq header (concat header field "\n"))
161 (defun std11-header-string-except (regexp &optional boundary)
162 "Return string of message header fields not matched by REGEXP.
163 If BOUNDARY is not nil, it is used as message header separator.
165 (let ((case-fold-search t))
168 (std11-narrow-to-header boundary)
169 (goto-char (point-min))
171 (while (re-search-forward std11-field-head-regexp nil t)
173 (buffer-substring (match-beginning 0) (std11-field-end)))
174 (if (not (string-match regexp field))
175 (setq header (concat header field "\n"))
180 (defun std11-collect-field-names (&optional boundary)
181 "Return list of all field-names of the message header in current buffer.
182 If BOUNDARY is not nil, it is used as message header separator.
186 (std11-narrow-to-header boundary)
187 (goto-char (point-min))
189 (while (re-search-forward std11-field-head-regexp nil t)
190 (setq name (buffer-substring-no-properties
191 (match-beginning 0)(1- (match-end 0))))
192 (or (member name dest)
193 (setq dest (cons name dest))
202 (defun std11-wrap-as-quoted-pairs (string specials)
206 (len (length string))
209 (let ((chr (aref string i)))
210 (if (memq chr specials)
211 (setq dest (concat dest (substring string b i) "\\")
216 (concat dest (substring string b))
219 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
221 (defun std11-wrap-as-quoted-string (string)
222 "Wrap STRING as RFC 822 quoted-string. [std11.el]"
224 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
227 (defun std11-strip-quoted-pair (string)
228 "Strip quoted-pairs in STRING. [std11.el]"
232 (len (length string))
235 (let ((chr (aref string i)))
237 (setq dest (concat dest (substring string b i))
242 (concat dest (substring string b))
245 (defun std11-strip-quoted-string (string)
246 "Strip quoted-string STRING. [std11.el]"
247 (let ((len (length string)))
249 (let ((max (1- len)))
250 (and (eq (aref string 0) ?\")
251 (eq (aref string max) ?\")
252 (std11-strip-quoted-pair (substring string 1 max))
257 ;;; @ lexical analyze
260 (defconst std11-space-chars " \t\n")
261 (defconst std11-spaces-regexp (` (, (concat "[" std11-space-chars "]+"))))
262 (defconst std11-special-char-list '(?\] ?\[
266 (defconst std11-atom-regexp
267 (` (, (concat "^[^" std11-special-char-list std11-space-chars "]+"))))
269 (defun std11-analyze-spaces (string)
270 (if (and (string-match std11-spaces-regexp string)
271 (= (match-beginning 0) 0))
272 (let ((end (match-end 0)))
273 (cons (cons 'spaces (substring string 0 end))
274 (substring string end)
277 (defun std11-analyze-special (str)
278 (if (and (> (length str) 0)
279 (memq (aref str 0) std11-special-char-list))
280 (cons (cons 'specials (substring str 0 1))
284 (defun std11-analyze-atom (str)
285 (if (string-match std11-atom-regexp str)
286 (let ((end (match-end 0)))
287 (cons (cons 'atom (substring str 0 end))
291 (defun std11-check-enclosure (str open close &optional recursive from)
292 (let ((len (length str))
296 (eq (aref str i) open))
301 (setq chr (aref str i))
314 (setq p (std11-check-enclosure
315 str open close recursive i))
325 (defun std11-analyze-quoted-string (str)
326 (let ((p (std11-check-enclosure str ?\" ?\")))
328 (cons (cons 'quoted-string (substring str 1 (1- p)))
332 (defun std11-analyze-domain-literal (str)
333 (let ((p (std11-check-enclosure str ?\[ ?\])))
335 (cons (cons 'domain-literal (substring str 1 (1- p)))
339 (defun std11-analyze-comment (str)
340 (let ((p (std11-check-enclosure str ?\( ?\) t)))
342 (cons (cons 'comment (substring str 1 (1- p)))
346 (defun std11-lexical-analyze (str)
348 (while (not (string-equal str ""))
350 (or (std11-analyze-quoted-string str)
351 (std11-analyze-domain-literal str)
352 (std11-analyze-comment str)
353 (std11-analyze-spaces str)
354 (std11-analyze-special str)
355 (std11-analyze-atom str)
358 (setq dest (cons (car ret) dest))
368 (defun std11-ignored-token-p (token)
369 (let ((type (car token)))
370 (or (eq type 'spaces)(eq type 'comment))
373 (defun std11-parse-token (lal)
377 (setq token (car lal))
378 (std11-ignored-token-p token)
381 (setq itl (cons token itl))
383 (cons (nreverse (cons token itl))
387 (defun std11-parse-ascii-token (lal)
388 (let (token itl parsed token-value)
390 (setq token (car lal))
391 (or (std11-ignored-token-p token)
392 (if (and (setq token-value (cdr token))
393 (find-non-ascii-charset-string token-value)
398 (setq itl (cons token itl))
401 (setq parsed (nreverse (cons token itl)))
403 (cons parsed (cdr lal))
406 (defun std11-parse-token-or-comment (lal)
410 (setq token (car lal))
411 (eq (car token) 'spaces)
414 (setq itl (cons token itl))
416 (cons (nreverse (cons token itl))
420 (defun std11-parse-word (lal)
421 (let ((ret (std11-parse-ascii-token lal)))
423 (let ((elt (car ret))
426 (if (or (assq 'atom elt)
427 (assq 'quoted-string elt))
428 (cons (cons 'word elt) rest)
431 (defun std11-parse-word-or-comment (lal)
432 (let ((ret (std11-parse-token-or-comment lal)))
434 (let ((elt (car ret))
437 (cond ((or (assq 'atom elt)
438 (assq 'quoted-string elt))
439 (cons (cons 'word elt) rest)
442 (cons (cons 'comment-word elt) rest)
446 (defun std11-parse-phrase (lal)
448 (while (setq ret (std11-parse-word-or-comment lal))
449 (setq phrase (append phrase (cdr (car ret))))
453 (cons (cons 'phrase phrase) lal)
456 (defun std11-parse-local-part (lal)
457 (let ((ret (std11-parse-word lal)))
459 (let ((local-part (cdr (car ret))) dot)
461 (while (and (setq ret (std11-parse-ascii-token lal))
463 (string-equal (cdr (assq 'specials dot)) ".")
464 (setq ret (std11-parse-word (cdr ret)))
466 (append local-part dot (cdr (car ret)))
470 (cons (cons 'local-part local-part) lal)
473 (defun std11-parse-sub-domain (lal)
474 (let ((ret (std11-parse-ascii-token lal)))
476 (let ((sub-domain (car ret)))
477 (if (or (assq 'atom sub-domain)
478 (assq 'domain-literal sub-domain)
480 (cons (cons 'sub-domain sub-domain)
485 (defun std11-parse-domain (lal)
486 (let ((ret (std11-parse-sub-domain lal)))
488 (let ((domain (cdr (car ret))) dot)
490 (while (and (setq ret (std11-parse-ascii-token lal))
492 (string-equal (cdr (assq 'specials dot)) ".")
493 (setq ret (std11-parse-sub-domain (cdr ret)))
495 (append domain dot (cdr (car ret)))
499 (cons (cons 'domain domain) lal)
502 (defun std11-parse-at-domain (lal)
503 (let ((ret (std11-parse-ascii-token lal)) at-sign)
505 (setq at-sign (car ret))
506 (string-equal (cdr (assq 'specials at-sign)) "@")
507 (setq ret (std11-parse-domain (cdr ret)))
509 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
513 (defun std11-parse-addr-spec (lal)
514 (let ((ret (std11-parse-local-part lal))
518 (setq addr (cdr (car ret)))
520 (and (setq ret (std11-parse-at-domain lal))
521 (setq addr (append addr (cdr (car ret))))
524 (cons (cons 'addr-spec addr) lal)
527 (defun std11-parse-route (lal)
528 (let ((ret (std11-parse-at-domain lal))
532 (setq route (cdr (car ret)))
534 (while (and (setq ret (std11-parse-ascii-token lal))
535 (setq comma (car ret))
536 (string-equal (cdr (assq 'specials comma)) ",")
537 (setq ret (std11-parse-at-domain (cdr ret)))
539 (setq route (append route comma (cdr (car ret))))
542 (and (setq ret (std11-parse-ascii-token lal))
543 (setq colon (car ret))
544 (string-equal (cdr (assq 'specials colon)) ":")
545 (setq route (append route colon))
548 (cons (cons 'route route)
553 (defun std11-parse-route-addr (lal)
554 (let ((ret (std11-parse-ascii-token lal))
558 (string-equal (cdr (assq 'specials <)) "<")
560 (progn (and (setq ret (std11-parse-route lal))
561 (setq route (cdr (car ret)))
564 (setq ret (std11-parse-addr-spec lal))
566 (setq addr-spec (cdr (car ret)))
568 (setq ret (std11-parse-ascii-token lal))
570 (string-equal (cdr (assq 'specials >)) ">")
572 (cons (cons 'route-addr (append route addr-spec))
577 (defun std11-parse-phrase-route-addr (lal)
578 (let ((ret (std11-parse-phrase lal)) phrase)
581 (setq phrase (cdr (car ret)))
584 (if (setq ret (std11-parse-route-addr lal))
585 (cons (list 'phrase-route-addr
591 (defun std11-parse-mailbox (lal)
592 (let ((ret (or (std11-parse-phrase-route-addr lal)
593 (std11-parse-addr-spec lal)))
597 (setq mbox (car ret))
599 (if (and (setq ret (std11-parse-token-or-comment lal))
600 (setq comment (cdr (assq 'comment (car ret))))
604 (cons (list 'mailbox mbox comment)
608 (defun std11-parse-group (lal)
609 (let ((ret (std11-parse-phrase lal))
610 phrase colon comma mbox semicolon)
612 (setq phrase (cdr (car ret)))
614 (setq ret (std11-parse-ascii-token lal))
615 (setq colon (car ret))
616 (string-equal (cdr (assq 'specials colon)) ":")
619 (and (setq ret (std11-parse-mailbox lal))
620 (setq mbox (list (car ret)))
623 (while (and (setq ret (std11-parse-ascii-token lal))
624 (setq comma (car ret))
626 (cdr (assq 'specials comma)) ",")
628 (setq ret (std11-parse-mailbox lal))
629 (setq mbox (cons (car ret) mbox))
633 (and (setq ret (std11-parse-ascii-token lal))
634 (setq semicolon (car ret))
635 (string-equal (cdr (assq 'specials semicolon)) ";")
637 (cons (list 'group phrase (nreverse mbox))
642 (defun std11-parse-address (lal)
643 (or (std11-parse-group lal)
644 (std11-parse-mailbox lal)
647 (defun std11-parse-addresses (lal)
648 (let ((ret (std11-parse-address lal)))
650 (let ((dest (list (car ret))))
652 (while (and (setq ret (std11-parse-ascii-token lal))
653 (string-equal (cdr (assq 'specials (car ret))) ",")
654 (setq ret (std11-parse-address (cdr ret)))
656 (setq dest (cons (car ret) dest))
662 (defun std11-parse-msg-id (lal)
663 (let ((ret (std11-parse-ascii-token lal))
667 (string-equal (cdr (assq 'specials <)) "<")
669 (setq ret (std11-parse-addr-spec lal))
670 (setq addr-spec (car ret))
672 (setq ret (std11-parse-ascii-token lal))
674 (string-equal (cdr (assq 'specials >)) ">")
676 (cons (cons 'msg-id (cdr addr-spec))
684 (defun std11-addr-to-string (seq)
685 "Return string from lexical analyzed list SEQ
686 represents addr-spec of RFC 822. [std11.el]"
689 (let ((name (car token)))
691 ((eq name 'spaces) "")
692 ((eq name 'comment) "")
693 ((eq name 'quoted-string)
694 (concat "\"" (cdr token) "\""))
700 (defun std11-address-string (address)
701 "Return string of address part from parsed ADDRESS of RFC 822.
703 (cond ((eq (car address) 'group)
704 (mapconcat (function std11-address-string)
708 ((eq (car address) 'mailbox)
709 (let ((addr (nth 1 address)))
710 (std11-addr-to-string
711 (if (eq (car addr) 'phrase-route-addr)
717 (defun std11-full-name-string (address)
718 "Return string of full-name part from parsed ADDRESS of RFC 822.
720 (cond ((eq (car address) 'group)
727 ((eq (car address) 'mailbox)
728 (let ((addr (nth 1 address))
729 (comment (nth 2 address))
731 (if (eq (car addr) 'phrase-route-addr)
736 (let ((type (car token)))
737 (cond ((eq type 'quoted-string)
738 (std11-strip-quoted-pair (cdr token))
743 (std11-strip-quoted-pair (cdr token))
751 (cond ((> (length phrase) 0) phrase)
752 (comment (std11-strip-quoted-pair comment))
756 (defun std11-msg-id-string (msg-id)
757 "Return string from parsed MSG-ID of RFC 822."
758 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
761 (defun std11-fill-msg-id-list-string (string &optional column)
762 "Fill list of msg-id in STRING, and return the result."
765 (let ((lal (std11-lexical-analyze string))
767 (let ((ret (std11-parse-msg-id lal)))
769 (let* ((str (std11-msg-id-string (car ret)))
772 (if (> (+ len column) 76)
773 (setq dest (concat dest "\n " str)
776 column (+ column len))
778 (setq dest (concat dest (cdr (car lal)))
782 (let ((ret (std11-parse-msg-id lal)))
784 (let* ((str (std11-msg-id-string (car ret)))
785 (len (1+ (length str))))
787 (if (> (+ len column) 76)
788 (setq dest (concat dest "\n " str)
790 (setq dest (concat dest " " str)
791 column (+ column len))
793 (setq dest (concat dest (cdr (car lal)))
799 ;;; @ parser with lexical analyzer
802 (defun std11-parse-address-string (string)
803 "Parse STRING as mail address. [std11.el]"
804 (std11-parse-address (std11-lexical-analyze string))
807 (defun std11-parse-addresses-string (string)
808 "Parse STRING as mail address list. [std11.el]"
809 (std11-parse-addresses (std11-lexical-analyze string))
812 (defun std11-extract-address-components (string)
813 "Extract full name and canonical address from STRING.
814 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
815 If no name can be extracted, FULL-NAME will be nil. [std11.el]"
816 (let* ((structure (car (std11-parse-address-string
817 (std11-unfold-string string))))
818 (phrase (std11-full-name-string structure))
819 (address (std11-address-string structure))
821 (list phrase address)
830 ;;; std11.el ends here