a7f7ffc4ff4f38b77d575c27959db19de20c5c4e
[elisp/gnus.git-] / lisp / mml.el
1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'mm-util)
27 (require 'mm-bodies)
28 (require 'mm-encode)
29
30 (eval-and-compile
31   (autoload 'message-make-message-id "message"))
32
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)
44     table))
45
46 (defun mml-parse ()
47   "Parse the current buffer as an MML document."
48   (goto-char (point-min))
49   (let ((table (syntax-table)))
50     (unwind-protect
51         (progn
52           (set-syntax-table mml-syntax-table)
53           (mml-parse-1))
54       (set-syntax-table table))))
55   
56 (defun mml-parse-1 ()
57   "Parse the current buffer as an MML document."
58   (let (struct)
59     (while (and (not (eobp))
60                 (not (looking-at "<#/multipart")))
61       (cond
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))))
66               struct))
67        ((looking-at "<#external")
68         (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
69               struct))
70        (t
71         (push (list 'part '(type . "text/plain")
72                     (cons 'contents (mml-read-part))) struct))))
73     (unless (eobp)
74       (forward-line 1))
75     (nreverse struct)))
76
77 (defun mml-read-tag ()
78   "Read a tag and return the contents."
79   (let (contents name elem val)
80     (forward-char 2)
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"))
91     (forward-char 1)
92     (cons (intern name) (nreverse contents))))
93
94 (defun mml-read-part ()
95   "Return the buffer up till the next part, multipart or closing part or multipart."
96   (let ((beg (point)))
97     ;; If the tag ended at the end of the line, we go to the next line.
98     (when (looking-at "[ \t]*\n")
99       (forward-line 1))
100     (if (re-search-forward "<#/?\\(multipart\\|part\\|external\\)." nil t)
101         (prog1
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")
106               (forward-line 1))))
107       (buffer-substring beg (goto-char (point-max))))))
108
109 (defvar mml-boundary nil)
110 (defvar mml-base-boundary "=-=-=")
111 (defvar mml-multipart-number 0)
112
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))
117     (if (not cont)
118         nil
119       (with-temp-buffer
120         (if (and (consp (car cont))
121                  (= (length cont) 1))
122             (mml-generate-mime-1 (car cont))
123           (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
124                                       cont)))
125         (buffer-string)))))
126
127 (defun mml-generate-mime-1 (cont)
128   (cond
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")
133           (with-temp-buffer
134             (if (setq filename (cdr (assq 'filename cont)))
135                 (insert-file-contents-literally filename)
136               (save-restriction
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)
155       (insert "\n")
156       (insert coded)))
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))))
162       (when name
163         (setq name (mml-parse-file-name name))
164         (if (stringp name)
165             (insert ";\n name=\"" (prin1-to-string name)
166                     "\";\n access-type=local-file")
167           (insert
168            (format ";\n name=%S;\n site=%S;\n directory=%S"
169                    (file-name-nondirectory (nth 2 name))
170                    (nth 1 name)
171                    (file-name-directory (nth 2 name))))
172           (insert ";\n access-type="
173                   (if (member (nth 0 name) '("ftp@" "anonymous@"))
174                       "anon-ftp"
175                     "ftp"))))
176       (when parameters
177         (insert parameters)))
178     (insert "\n\n")
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"))
183     (insert "\n\n")
184     (insert (or (cdr (assq 'contents cont))))
185     (insert "\n"))
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")
190                       mml-boundary))
191       (insert "\n")
192       (setq cont (cddr cont))
193       (while cont
194         (insert "\n--" mml-boundary "\n")
195         (mml-generate-mime-1 (pop cont)))
196       (insert "\n--" mml-boundary "--\n")))
197    (t
198     (error "Invalid element: %S" cont))))
199
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))))
207     mml-boundary))
208
209 (defun mml-compute-boundary-1 (cont)
210   (let (filename)
211     (cond
212      ((eq (car cont) 'part)
213       (with-temp-buffer
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))))
223     t))
224
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)
229             "")
230           mml-base-boundary))
231
232 (defun mml-make-string (num string)
233   (let ((out ""))
234     (while (not (zerop (decf num)))
235       (setq out (concat out string)))
236     out))
237
238 (defun mml-insert-mime-headers (cont type charset encoding)
239   (let (parameters disposition description)
240     (when (or charset
241               (setq parameters
242                     (mml-parameter-string
243                      cont '(name access-type expiration size permission)))
244               (not (equal type "text/plain")))
245       (insert "Content-Type: " type)
246       (when charset
247         (insert (format "; charset=\"%s\"" charset)))
248       (when parameters
249         (insert parameters))
250       (insert "\n"))
251     (when (or (setq disposition (cdr (assq 'disposition cont)))
252               (setq parameters
253                     (mml-parameter-string
254                      cont '(filename creation-date modification-date
255                                      read-date))))
256       (insert "Content-Disposition: " (or disposition "inline"))
257       (when parameters
258         (insert parameters))
259       (insert "\n"))
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"))
264     ))
265
266 (defun mml-parameter-string (cont types)
267   (let ((string "")
268         value type)
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)
274                                value)))))
275     (when (not (zerop (length string)))
276       string)))
277
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))))
287     path))
288
289 (provide 'mml)
290
291 ;;; mml.el ends here