1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
31 (autoload 'message-make-message-id "message"))
33 (defvar mml-syntax-table
34 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
35 (modify-syntax-entry ?\\ "/" table)
36 (modify-syntax-entry ?< "(" table)
37 (modify-syntax-entry ?> ")" table)
38 (modify-syntax-entry ?@ "w" table)
39 (modify-syntax-entry ?/ "w" table)
40 (modify-syntax-entry ?= " " table)
41 (modify-syntax-entry ?* " " table)
42 (modify-syntax-entry ?\; " " table)
43 (modify-syntax-entry ?\' " " table)
47 "Parse the current buffer as an MML document."
48 (goto-char (point-min))
49 (let ((table (syntax-table)))
52 (set-syntax-table mml-syntax-table)
54 (set-syntax-table table))))
57 "Parse the current buffer as an MML document."
59 (while (and (not (eobp))
60 (not (looking-at "<#/multipart")))
62 ((looking-at "<#multipart")
63 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
64 ((looking-at "<#part")
65 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
67 ((looking-at "<#external")
68 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
71 (push (list 'part '(type . "text/plain")
72 (cons 'contents (mml-read-part))) struct))))
77 (defun mml-read-tag ()
78 "Read a tag and return the contents."
79 (let (contents name elem val)
81 (setq name (buffer-substring (point) (progn (forward-sexp 1) (point))))
82 (skip-chars-forward " \t\n")
83 (while (not (looking-at ">"))
84 (setq elem (buffer-substring (point) (progn (forward-sexp 1) (point))))
85 (skip-chars-forward "= \t\n")
86 (setq val (buffer-substring (point) (progn (forward-sexp 1) (point))))
87 (when (string-match "^\"\\(.*\\)\"$" val)
88 (setq val (match-string 1 val)))
89 (push (cons (intern elem) val) contents)
90 (skip-chars-forward " \t\n"))
92 (cons (intern name) (nreverse contents))))
94 (defun mml-read-part ()
95 "Return the buffer up till the next part, multipart or closing part or multipart."
97 ;; If the tag ended at the end of the line, we go to the next line.
98 (when (looking-at "[ \t]*\n")
100 (if (re-search-forward "<#/?\\(multipart\\|part\\|external\\)." nil t)
102 (buffer-substring beg (match-beginning 0))
103 (if (equal (match-string 0) "<#/multipart>")
104 (goto-char (match-beginning 0))
105 (when (looking-at "[ \t]*\n")
107 (buffer-substring beg (goto-char (point-max))))))
109 (defvar mml-boundary nil)
110 (defvar mml-base-boundary "=-=-=")
111 (defvar mml-multipart-number 0)
113 (defun mml-generate-mime ()
114 "Generate a MIME message based on the current MML document."
115 (let ((cont (mml-parse))
116 (mml-multipart-number 0))
120 (if (and (consp (car cont))
122 (mml-generate-mime-1 (car cont))
123 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
127 (defun mml-generate-mime-1 (cont)
129 ((eq (car cont) 'part)
130 (let (coded encoding charset filename type parameters)
131 (setq type (or (cdr (assq 'type cont)) "text/plain"))
132 (if (equal (car (split-string type "/")) "text")
134 (if (setq filename (cdr (assq 'filename cont)))
135 (insert-file-contents-literally filename)
137 (narrow-to-region (point) (point))
138 (insert (cdr (assq 'contents cont)))
139 ;; Remove quotes from quoted tags.
140 (goto-char (point-min))
141 (while (re-search-forward
142 "<#!+\\(part\\|multipart\\|external\\)" nil t)
143 (delete-region (+ (match-beginning 0) 2)
144 (+ (match-beginning 0) 3)))))
145 (setq charset (mm-encode-body)
146 encoding (mm-body-encoding))
147 (setq coded (buffer-string)))
148 (mm-with-unibyte-buffer
149 (if (setq filename (cdr (assq 'filename cont)))
150 (insert-file-contents-literally filename)
151 (insert (cdr (assq 'contents cont))))
152 (setq encoding (mm-encode-buffer type)
153 coded (buffer-string))))
154 (mml-insert-mime-headers cont type charset encoding)
157 ((eq (car cont) 'external)
158 (insert "Content-Type: message/external-body")
159 (let ((parameters (mml-parameter-string
160 cont '(expiration size permission)))
161 (name (cdr (assq 'name cont))))
163 (setq name (mml-parse-file-name name))
165 (insert ";\n name=\"" (prin1-to-string name)
166 "\";\n access-type=local-file")
168 (format ";\n name=%S;\n site=%S;\n directory=%S"
169 (file-name-nondirectory (nth 2 name))
171 (file-name-directory (nth 2 name))))
172 (insert ";\n access-type="
173 (if (member (nth 0 name) '("ftp@" "anonymous@"))
177 (insert parameters)))
179 (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
180 (insert "Content-ID: " (message-make-message-id) "\n")
181 (insert "Content-Transfer-Encoding: "
182 (or (cdr (assq 'encoding cont)) "binary"))
184 (insert (or (cdr (assq 'contents cont))))
186 ((eq (car cont) 'multipart)
187 (let ((mml-boundary (mml-compute-boundary cont)))
188 (insert (format "Content-Type: multipart/%s; boundary=\"%s\"\n"
189 (or (cdr (assq 'type cont)) "mixed")
192 (setq cont (cddr cont))
194 (insert "\n--" mml-boundary "\n")
195 (mml-generate-mime-1 (pop cont)))
196 (insert "\n--" mml-boundary "--\n")))
198 (error "Invalid element: %S" cont))))
200 (defun mml-compute-boundary (cont)
201 "Return a unique boundary that does not exist in CONT."
202 (let ((mml-boundary (mml-make-boundary)))
203 ;; This function tries again and again until it has found
204 ;; a unique boundary.
205 (while (not (catch 'not-unique
206 (mml-compute-boundary-1 cont))))
209 (defun mml-compute-boundary-1 (cont)
212 ((eq (car cont) 'part)
214 (if (setq filename (cdr (assq 'filename cont)))
215 (insert-file-contents-literally filename)
216 (insert (cdr (assq 'contents cont))))
217 (goto-char (point-min))
218 (when (re-search-forward (concat "^--" mml-boundary) nil t)
219 (setq mml-boundary (mml-make-boundary))
220 (throw 'not-unique nil))))
221 ((eq (car cont) 'multipart)
222 (mapcar 'mml-compute-boundary-1 (cddr cont))))
225 (defun mml-make-boundary ()
226 (concat (make-string (% (incf mml-multipart-number) 60) ?=)
227 (if (> mml-multipart-number 17)
228 (format "%x" mml-multipart-number)
232 (defun mml-make-string (num string)
234 (while (not (zerop (decf num)))
235 (setq out (concat out string)))
238 (defun mml-insert-mime-headers (cont type charset encoding)
239 (let (parameters disposition description)
242 (mml-parameter-string
243 cont '(name access-type expiration size permission)))
244 (not (equal type "text/plain")))
245 (insert "Content-Type: " type)
247 (insert (format "; charset=\"%s\"" charset)))
251 (when (or (setq disposition (cdr (assq 'disposition cont)))
253 (mml-parameter-string
254 cont '(filename creation-date modification-date
256 (insert "Content-Disposition: " (or disposition "inline"))
260 (unless (eq encoding '7bit)
261 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
262 (when (setq description (cdr (assq 'description cont)))
263 (insert "Content-Description: " description "\n"))
266 (defun mml-parameter-string (cont types)
269 (while (setq type (pop types))
270 (when (setq value (cdr (assq type cont)))
271 (setq string (concat string ";\n " (symbol-name type) "="
272 (if (string-match "[^_0-9A-Za-z]" value)
273 (prin1-to-string value)
275 (when (not (zerop (length string)))
278 (defvar ange-ftp-path-format)
279 (defvar efs-path-regexp)
280 (defun mml-parse-file-name (path)
281 (if (if (boundp 'efs-path-regexp)
282 (string-match efs-path-regexp path)
283 (if (boundp 'ange-ftp-path-format)
284 (string-match (car ange-ftp-path-format))))
285 (list (match-string 1 path) (match-string 2 path)
286 (substring path (1+ (match-end 2))))