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)))
51 (defun 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-narrow-to-header (&optional boundary)
62 "Narrow to the message header.
63 If BOUNDARY is not nil, it is used as message header separator."
65 (goto-char (point-min))
66 (if (re-search-forward
67 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
73 (defun std11-field-body (name &optional boundary)
74 "Return the value of the header field NAME.
75 If BOUNDARY is not nil, it is used as message header separator."
78 (inline (std11-narrow-to-header boundary)
79 (std11-fetch-field name))
82 (defun std11-find-field-body (field-names &optional boundary)
83 "Return the first found field-body specified by FIELD-NAMES
84 of the message header in current buffer. If BOUNDARY is not nil, it is
85 used as message header separator."
88 (std11-narrow-to-header boundary)
89 (let ((case-fold-search t)
92 (while (setq field-name (car field-names))
93 (goto-char (point-min))
94 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
96 (buffer-substring-no-properties
97 (match-end 0) (std11-field-end)))
99 (setq field-names (cdr field-names))
102 (defun std11-field-bodies (field-names &optional default-value boundary)
103 "Return list of each field-bodies of FIELD-NAMES of the message header
104 in current buffer. If BOUNDARY is not nil, it is used as message
108 (std11-narrow-to-header boundary)
109 (let* ((case-fold-search t)
110 (dest (make-list (length field-names) default-value))
114 (while (setq field-name (car s-rest))
115 (goto-char (point-min))
116 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
118 (buffer-substring-no-properties
119 (match-end 0) (std11-field-end)))
121 (setq s-rest (cdr s-rest)
126 (defun std11-header-string (regexp &optional boundary)
127 "Return string of message header fields matched by REGEXP.
128 If BOUNDARY is not nil, it is used as message header separator.
130 (let ((case-fold-search t))
133 (std11-narrow-to-header boundary)
134 (goto-char (point-min))
136 (while (re-search-forward std11-field-head-regexp nil t)
138 (buffer-substring (match-beginning 0) (std11-field-end)))
139 (if (string-match regexp field)
140 (setq header (concat header field "\n"))
145 (defun std11-header-string-except (regexp &optional boundary)
146 "Return string of message header fields not matched by REGEXP.
147 If BOUNDARY is not nil, it is used as message header separator.
149 (let ((case-fold-search t))
152 (std11-narrow-to-header boundary)
153 (goto-char (point-min))
155 (while (re-search-forward std11-field-head-regexp nil t)
157 (buffer-substring (match-beginning 0) (std11-field-end)))
158 (if (not (string-match regexp field))
159 (setq header (concat header field "\n"))
164 (defun std11-collect-field-names (&optional boundary)
165 "Return list of all field-names of the message header in current buffer.
166 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))
186 (defun std11-unfold-string (string)
187 "Unfold STRING as message header field."
190 (while (string-match "\n\\([ \t]\\)" string p)
191 (setq dest (concat dest
192 (substring string p (match-beginning 0))
195 (setq p (match-end 0)))
198 (concat dest (substring string p))
205 (defun std11-wrap-as-quoted-pairs (string specials)
209 (len (length string))
212 (let ((chr (aref string i)))
213 (if (memq chr specials)
214 (setq dest (concat dest (substring string b i) "\\")
219 (concat dest (substring string b))
222 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
224 (defun std11-wrap-as-quoted-string (string)
225 "Wrap STRING as RFC 822 quoted-string. [std11.el]"
227 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
230 (defun std11-strip-quoted-pair (string)
231 "Strip quoted-pairs in STRING. [std11.el]"
235 (len (length string))
238 (let ((chr (aref string i)))
240 (setq dest (concat dest (substring string b i))
245 (concat dest (substring string b))
248 (defun std11-strip-quoted-string (string)
249 "Strip quoted-string STRING."
250 (let ((len (length string)))
252 (let ((max (1- len)))
253 (and (eq (aref string 0) ?\")
254 (eq (aref string max) ?\")
255 (std11-strip-quoted-pair (substring string 1 max))
260 ;;; @ lexical analyze
264 (defconst std11-space-char-list '(? ?\t ?\n))
265 (defconst std11-special-char-list '(?\] ?\[
270 ;; (defconst std11-spaces-regexp
271 ;; (eval-when-compile (concat "[" std11-space-char-list "]+")))
272 (defconst std11-atom-regexp
274 (concat "^[^" std11-special-char-list std11-space-char-list "]+")))
276 (defun std11-analyze-spaces (string)
277 (if (and (string-match
278 (eval-when-compile (concat "[" std11-space-char-list "]+"))
280 (= (match-beginning 0) 0))
281 (let ((end (match-end 0)))
282 (cons (cons 'spaces (substring string 0 end))
283 (substring string end)
286 (defun std11-analyze-special (str)
287 (if (and (> (length str) 0)
288 (memq (aref str 0) std11-special-char-list))
289 (cons (cons 'specials (substring str 0 1))
293 (defun std11-analyze-atom (str)
294 (if (string-match std11-atom-regexp str)
295 (let ((end (match-end 0)))
296 (cons (cons 'atom (substring str 0 end))
300 (defun std11-check-enclosure (str open close &optional recursive from)
301 (let ((len (length str))
305 (eq (aref str i) open))
310 (setq chr (aref str i))
323 (setq p (std11-check-enclosure
324 str open close recursive i))
334 (defun std11-analyze-quoted-string (str)
335 (let ((p (std11-check-enclosure str ?\" ?\")))
337 (cons (cons 'quoted-string (substring str 1 (1- p)))
341 (defun std11-analyze-domain-literal (str)
342 (let ((p (std11-check-enclosure str ?\[ ?\])))
344 (cons (cons 'domain-literal (substring str 1 (1- p)))
348 (defun std11-analyze-comment (str)
349 (let ((p (std11-check-enclosure str ?\( ?\) t)))
351 (cons (cons 'comment (substring str 1 (1- p)))
355 (defun std11-lexical-analyze (str)
357 (while (not (string-equal str ""))
359 (or (std11-analyze-quoted-string str)
360 (std11-analyze-domain-literal str)
361 (std11-analyze-comment str)
362 (std11-analyze-spaces str)
363 (std11-analyze-special str)
364 (std11-analyze-atom str)
367 (setq dest (cons (car ret) dest))
377 (defun std11-ignored-token-p (token)
378 (let ((type (car token)))
379 (or (eq type 'spaces)(eq type 'comment))
382 (defun std11-parse-token (lal)
386 (setq token (car lal))
387 (std11-ignored-token-p token)
390 (setq itl (cons token itl))
392 (cons (nreverse (cons token itl))
396 (defun std11-parse-ascii-token (lal)
397 (let (token itl parsed token-value)
399 (setq token (car lal))
400 (or (std11-ignored-token-p token)
401 (if (and (setq token-value (cdr token))
402 (find-non-ascii-charset-string token-value)
407 (setq itl (cons token itl))
410 (setq parsed (nreverse (cons token itl)))
412 (cons parsed (cdr lal))
415 (defun std11-parse-token-or-comment (lal)
419 (setq token (car lal))
420 (eq (car token) 'spaces)
423 (setq itl (cons token itl))
425 (cons (nreverse (cons token itl))
429 (defun std11-parse-word (lal)
430 (let ((ret (std11-parse-ascii-token lal)))
432 (let ((elt (car ret))
435 (if (or (assq 'atom elt)
436 (assq 'quoted-string elt))
437 (cons (cons 'word elt) rest)
440 (defun std11-parse-word-or-comment (lal)
441 (let ((ret (std11-parse-token-or-comment lal)))
443 (let ((elt (car ret))
446 (cond ((or (assq 'atom elt)
447 (assq 'quoted-string elt))
448 (cons (cons 'word elt) rest)
451 (cons (cons 'comment-word elt) rest)
455 (defun std11-parse-phrase (lal)
457 (while (setq ret (std11-parse-word-or-comment lal))
458 (setq phrase (append phrase (cdr (car ret))))
462 (cons (cons 'phrase phrase) lal)
465 (defun std11-parse-local-part (lal)
466 (let ((ret (std11-parse-word lal)))
468 (let ((local-part (cdr (car ret))) dot)
470 (while (and (setq ret (std11-parse-ascii-token lal))
472 (string-equal (cdr (assq 'specials dot)) ".")
473 (setq ret (std11-parse-word (cdr ret)))
475 (append local-part dot (cdr (car ret)))
479 (cons (cons 'local-part local-part) lal)
482 (defun std11-parse-sub-domain (lal)
483 (let ((ret (std11-parse-ascii-token lal)))
485 (let ((sub-domain (car ret)))
486 (if (or (assq 'atom sub-domain)
487 (assq 'domain-literal sub-domain)
489 (cons (cons 'sub-domain sub-domain)
494 (defun std11-parse-domain (lal)
495 (let ((ret (std11-parse-sub-domain lal)))
497 (let ((domain (cdr (car ret))) dot)
499 (while (and (setq ret (std11-parse-ascii-token lal))
501 (string-equal (cdr (assq 'specials dot)) ".")
502 (setq ret (std11-parse-sub-domain (cdr ret)))
504 (append domain dot (cdr (car ret)))
508 (cons (cons 'domain domain) lal)
511 (defun std11-parse-at-domain (lal)
512 (let ((ret (std11-parse-ascii-token lal)) at-sign)
514 (setq at-sign (car ret))
515 (string-equal (cdr (assq 'specials at-sign)) "@")
516 (setq ret (std11-parse-domain (cdr ret)))
518 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
522 (defun std11-parse-addr-spec (lal)
523 (let ((ret (std11-parse-local-part lal))
527 (setq addr (cdr (car ret)))
529 (and (setq ret (std11-parse-at-domain lal))
530 (setq addr (append addr (cdr (car ret))))
533 (cons (cons 'addr-spec addr) lal)
536 (defun std11-parse-route (lal)
537 (let ((ret (std11-parse-at-domain lal))
541 (setq route (cdr (car ret)))
543 (while (and (setq ret (std11-parse-ascii-token lal))
544 (setq comma (car ret))
545 (string-equal (cdr (assq 'specials comma)) ",")
546 (setq ret (std11-parse-at-domain (cdr ret)))
548 (setq route (append route comma (cdr (car ret))))
551 (and (setq ret (std11-parse-ascii-token lal))
552 (setq colon (car ret))
553 (string-equal (cdr (assq 'specials colon)) ":")
554 (setq route (append route colon))
557 (cons (cons 'route route)
562 (defun std11-parse-route-addr (lal)
563 (let ((ret (std11-parse-ascii-token lal))
567 (string-equal (cdr (assq 'specials <)) "<")
569 (progn (and (setq ret (std11-parse-route lal))
570 (setq route (cdr (car ret)))
573 (setq ret (std11-parse-addr-spec lal))
575 (setq addr-spec (cdr (car ret)))
577 (setq ret (std11-parse-ascii-token lal))
579 (string-equal (cdr (assq 'specials >)) ">")
581 (cons (cons 'route-addr (append route addr-spec))
586 (defun std11-parse-phrase-route-addr (lal)
587 (let ((ret (std11-parse-phrase lal)) phrase)
590 (setq phrase (cdr (car ret)))
593 (if (setq ret (std11-parse-route-addr lal))
594 (cons (list 'phrase-route-addr
600 (defun std11-parse-mailbox (lal)
601 (let ((ret (or (std11-parse-phrase-route-addr lal)
602 (std11-parse-addr-spec lal)))
606 (setq mbox (car ret))
608 (if (and (setq ret (std11-parse-token-or-comment lal))
609 (setq comment (cdr (assq 'comment (car ret))))
613 (cons (list 'mailbox mbox comment)
617 (defun std11-parse-group (lal)
618 (let ((ret (std11-parse-phrase lal))
619 phrase colon comma mbox semicolon)
621 (setq phrase (cdr (car ret)))
623 (setq ret (std11-parse-ascii-token lal))
624 (setq colon (car ret))
625 (string-equal (cdr (assq 'specials colon)) ":")
628 (and (setq ret (std11-parse-mailbox lal))
629 (setq mbox (list (car ret)))
632 (while (and (setq ret (std11-parse-ascii-token lal))
633 (setq comma (car ret))
635 (cdr (assq 'specials comma)) ",")
637 (setq ret (std11-parse-mailbox lal))
638 (setq mbox (cons (car ret) mbox))
642 (and (setq ret (std11-parse-ascii-token lal))
643 (setq semicolon (car ret))
644 (string-equal (cdr (assq 'specials semicolon)) ";")
646 (cons (list 'group phrase (nreverse mbox))
651 (defun std11-parse-address (lal)
652 (or (std11-parse-group lal)
653 (std11-parse-mailbox lal)
656 (defun std11-parse-addresses (lal)
657 (let ((ret (std11-parse-address lal)))
659 (let ((dest (list (car ret))))
661 (while (and (setq ret (std11-parse-ascii-token lal))
662 (string-equal (cdr (assq 'specials (car ret))) ",")
663 (setq ret (std11-parse-address (cdr ret)))
665 (setq dest (cons (car ret) dest))
671 (defun std11-parse-msg-id (lal)
672 (let ((ret (std11-parse-ascii-token lal))
676 (string-equal (cdr (assq 'specials <)) "<")
678 (setq ret (std11-parse-addr-spec lal))
679 (setq addr-spec (car ret))
681 (setq ret (std11-parse-ascii-token lal))
683 (string-equal (cdr (assq 'specials >)) ">")
685 (cons (cons 'msg-id (cdr addr-spec))
689 (defun std11-parse-in-reply-to (tokens)
690 "Parse lexical TOKENS as In-Reply-To field, and return the result."
691 (let ((ret (or (std11-parse-msg-id tokens)
692 (std11-parse-phrase tokens))))
694 (let ((dest (list (car ret))))
695 (setq tokens (cdr ret))
696 (while (setq ret (or (std11-parse-msg-id tokens)
697 (std11-parse-phrase tokens)))
698 (setq dest (cons (car ret) dest))
699 (setq tokens (cdr ret))
708 (defun std11-addr-to-string (seq)
709 "Return string from lexical analyzed list SEQ
710 represents addr-spec of RFC 822. [std11.el]"
713 (let ((name (car token)))
715 ((eq name 'spaces) "")
716 ((eq name 'comment) "")
717 ((eq name 'quoted-string)
718 (concat "\"" (cdr token) "\""))
724 (defun std11-address-string (address)
725 "Return string of address part from parsed ADDRESS of RFC 822.
727 (cond ((eq (car address) 'group)
728 (mapconcat (function std11-address-string)
732 ((eq (car address) 'mailbox)
733 (let ((addr (nth 1 address)))
734 (std11-addr-to-string
735 (if (eq (car addr) 'phrase-route-addr)
741 (defun std11-comment-value-to-string (value)
743 (std11-strip-quoted-pair value)
748 (if (stringp (car value))
751 (std11-comment-value-to-string
759 (defun std11-full-name-string (address)
760 "Return string of full-name part from parsed ADDRESS of RFC 822."
761 (cond ((eq (car address) 'group)
768 ((eq (car address) 'mailbox)
769 (let ((addr (nth 1 address))
770 (comment (nth 2 address))
772 (if (eq (car addr) 'phrase-route-addr)
777 (let ((type (car token)))
778 (cond ((eq type 'quoted-string)
779 (std11-strip-quoted-pair (cdr token))
783 (std11-comment-value-to-string
792 (cond ((> (length phrase) 0) phrase)
793 (comment (std11-comment-value-to-string comment))
797 (defun std11-msg-id-string (msg-id)
798 "Return string from parsed MSG-ID of RFC 822."
799 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
802 (defun std11-fill-msg-id-list-string (string &optional column)
803 "Fill list of msg-id in STRING, and return the result."
806 (let ((lal (std11-lexical-analyze string))
808 (let ((ret (std11-parse-msg-id lal)))
810 (let* ((str (std11-msg-id-string (car ret)))
813 (if (> (+ len column) 76)
814 (setq dest (concat dest "\n " str)
817 column (+ column len))
819 (setq dest (concat dest (cdr (car lal)))
823 (let ((ret (std11-parse-msg-id lal)))
825 (let* ((str (std11-msg-id-string (car ret)))
826 (len (1+ (length str))))
828 (if (> (+ len column) 76)
829 (setq dest (concat dest "\n " str)
831 (setq dest (concat dest " " str)
832 column (+ column len))
834 (setq dest (concat dest (cdr (car lal)))
840 ;;; @ parser with lexical analyzer
843 (defun std11-parse-address-string (string)
844 "Parse STRING as mail address. [std11.el]"
845 (std11-parse-address (std11-lexical-analyze string))
848 (defun std11-parse-addresses-string (string)
849 "Parse STRING as mail address list. [std11.el]"
850 (std11-parse-addresses (std11-lexical-analyze string))
853 (defun std11-extract-address-components (string)
854 "Extract full name and canonical address from STRING.
855 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
856 If no name can be extracted, FULL-NAME will be nil. [std11.el]"
857 (let* ((structure (car (std11-parse-address-string
858 (std11-unfold-string string))))
859 (phrase (std11-full-name-string structure))
860 (address (std11-address-string structure))
862 (list phrase address)
871 ;;; std11.el ends here