shr nested table rendering fixups
[gnus] / lisp / mml.el
1 ;;; mml.el --- A package for parsing and validating MML documents
2
3 ;; Copyright (C) 1998-2012  Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;;; Code:
24
25 ;; For Emacs <22.2 and XEmacs.
26 (eval-and-compile
27   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
28
29 (require 'mm-util)
30 (require 'mm-bodies)
31 (require 'mm-encode)
32 (require 'mm-decode)
33 (require 'mml-sec)
34 (eval-when-compile (require 'cl))
35 (eval-when-compile
36   (when (featurep 'xemacs)
37     (require 'easy-mmode))) ; for `define-minor-mode'
38
39 (autoload 'message-make-message-id "message")
40 (declare-function gnus-setup-posting-charset "gnus-msg" (group))
41 (autoload 'gnus-make-local-hook "gnus-util")
42 (autoload 'gnus-completing-read "gnus-util")
43 (autoload 'message-fetch-field "message")
44 (autoload 'message-mark-active-p "message")
45 (autoload 'message-info "message")
46 (autoload 'fill-flowed-encode "flow-fill")
47 (autoload 'message-posting-charset "message")
48 (autoload 'dnd-get-local-file-name "dnd")
49
50 (autoload 'message-options-set    "message")
51 (autoload 'message-narrow-to-head "message")
52 (autoload 'message-in-body-p      "message")
53 (autoload 'message-mail-p         "message")
54
55 (defvar gnus-article-mime-handles)
56 (defvar gnus-mouse-2)
57 (defvar gnus-newsrc-hashtb)
58 (defvar message-default-charset)
59 (defvar message-deletable-headers)
60 (defvar message-options)
61 (defvar message-posting-charset)
62 (defvar message-required-mail-headers)
63 (defvar message-required-news-headers)
64 (defvar dnd-protocol-alist)
65 (defvar mml-dnd-protocol-alist)
66
67 (defcustom mml-content-type-parameters
68   '(name access-type expiration size permission format)
69   "*A list of acceptable parameters in MML tag.
70 These parameters are generated in Content-Type header if exists."
71   :version "22.1"
72   :type '(repeat (symbol :tag "Parameter"))
73   :group 'message)
74
75 (defcustom mml-content-disposition-parameters
76   '(filename creation-date modification-date read-date)
77   "*A list of acceptable parameters in MML tag.
78 These parameters are generated in Content-Disposition header if exists."
79   :version "22.1"
80   :type '(repeat (symbol :tag "Parameter"))
81   :group 'message)
82
83 (defcustom mml-content-disposition-alist
84   '((text (rtf . "attachment") (t . "inline"))
85     (t . "attachment"))
86   "Alist of MIME types or regexps matching file names and default dispositions.
87 Each element should be one of the following three forms:
88
89   (REGEXP . DISPOSITION)
90   (SUPERTYPE (SUBTYPE . DISPOSITION) (SUBTYPE . DISPOSITION)...)
91   (TYPE . DISPOSITION)
92
93 Where REGEXP is a string which matches the file name (if any) of an
94 attachment, SUPERTYPE, SUBTYPE and TYPE should be symbols which are a
95 MIME supertype (e.g., text), a MIME subtype (e.g., plain) and a MIME
96 type (e.g., text/plain) respectively, and DISPOSITION should be either
97 the string \"attachment\" or the string \"inline\".  The value t for
98 SUPERTYPE, SUBTYPE or TYPE matches any of those types.  The first
99 match found will be used."
100   :version "23.1" ;; No Gnus
101   :type (let ((dispositions '(radio :format "DISPOSITION: %v"
102                                     :value "attachment"
103                                     (const :format "%v " "attachment")
104                                     (const :format "%v\n" "inline"))))
105           `(repeat
106             :offset 0
107             (choice :format "%[Value Menu%]%v"
108                     (cons :tag "(REGEXP . DISPOSITION)" :extra-offset 4
109                           (regexp :tag "REGEXP" :value ".*")
110                           ,dispositions)
111                     (cons :tag "(SUPERTYPE (SUBTYPE . DISPOSITION)...)"
112                           :indent 0
113                           (symbol :tag "    SUPERTYPE" :value text)
114                           (repeat :format "%v%i\n" :offset 0 :extra-offset 4
115                                   (cons :format "%v" :extra-offset 5
116                                         (symbol :tag "SUBTYPE" :value t)
117                                         ,dispositions)))
118                     (cons :tag "(TYPE . DISPOSITION)" :extra-offset 4
119                           (symbol :tag "TYPE" :value t)
120                           ,dispositions))))
121   :group 'message)
122
123 (defcustom mml-insert-mime-headers-always t
124   "If non-nil, always put Content-Type: text/plain at top of empty parts.
125 It is necessary to work against a bug in certain clients."
126   :version "24.1"
127   :type 'boolean
128   :group 'message)
129
130 (defcustom mml-enable-flowed t
131   "If non-nil, enable format=flowed usage when encoding a message.
132 This is only performed when filling on text/plain with hard
133 newlines in the text."
134   :version "24.1"
135   :type 'boolean
136   :group 'message)
137
138 (defvar mml-tweak-type-alist nil
139   "A list of (TYPE . FUNCTION) for tweaking MML parts.
140 TYPE is a string containing a regexp to match the MIME type.  FUNCTION
141 is a Lisp function which is called with the MML handle to tweak the
142 part.  This variable is used only when no TWEAK parameter exists in
143 the MML handle.")
144
145 (defvar mml-tweak-function-alist nil
146   "A list of (NAME . FUNCTION) for tweaking MML parts.
147 NAME is a string containing the name of the TWEAK parameter in the MML
148 handle.  FUNCTION is a Lisp function which is called with the MML
149 handle to tweak the part.")
150
151 (defvar mml-tweak-sexp-alist
152   '((mml-externalize-attachments . mml-tweak-externalize-attachments))
153   "A list of (SEXP . FUNCTION) for tweaking MML parts.
154 SEXP is an s-expression.  If the evaluation of SEXP is non-nil, FUNCTION
155 is called.  FUNCTION is a Lisp function which is called with the MML
156 handle to tweak the part.")
157
158 (defvar mml-externalize-attachments nil
159   "*If non-nil, local-file attachments are generated as external parts.")
160
161 (defvar mml-generate-multipart-alist nil
162   "*Alist of multipart generation functions.
163 Each entry has the form (NAME . FUNCTION), where
164 NAME is a string containing the name of the part (without the
165 leading \"/multipart/\"),
166 FUNCTION is a Lisp function which is called to generate the part.
167
168 The Lisp function has to supply the appropriate MIME headers and the
169 contents of this part.")
170
171 (defvar mml-syntax-table
172   (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
173     (modify-syntax-entry ?\\ "/" table)
174     (modify-syntax-entry ?< "(" table)
175     (modify-syntax-entry ?> ")" table)
176     (modify-syntax-entry ?@ "w" table)
177     (modify-syntax-entry ?/ "w" table)
178     (modify-syntax-entry ?= " " table)
179     (modify-syntax-entry ?* " " table)
180     (modify-syntax-entry ?\; " " table)
181     (modify-syntax-entry ?\' " " table)
182     table))
183
184 (defvar mml-boundary-function 'mml-make-boundary
185   "A function called to suggest a boundary.
186 The function may be called several times, and should try to make a new
187 suggestion each time.  The function is called with one parameter,
188 which is a number that says how many times the function has been
189 called for this message.")
190
191 (defvar mml-confirmation-set nil
192   "A list of symbols, each of which disables some warning.
193 `unknown-encoding': always send messages contain characters with
194 unknown encoding; `use-ascii': always use ASCII for those characters
195 with unknown encoding; `multipart': always send messages with more than
196 one charsets.")
197
198 (defvar mml-generate-default-type "text/plain"
199   "Content type by which the Content-Type header can be omitted.
200 The Content-Type header will not be put in the MIME part if the type
201 equals the value and there's no parameter (e.g. charset, format, etc.)
202 and `mml-insert-mime-headers-always' is nil.  The value will be bound
203 to \"message/rfc822\" when encoding an article to be forwarded as a MIME
204 part.  This is for the internal use, you should never modify the value.")
205
206 (defvar mml-buffer-list nil)
207
208 (defun mml-generate-new-buffer (name)
209   (let ((buf (generate-new-buffer name)))
210     (push buf mml-buffer-list)
211     buf))
212
213 (defun mml-destroy-buffers ()
214   (let (kill-buffer-hook)
215     (mapc 'kill-buffer mml-buffer-list)
216     (setq mml-buffer-list nil)))
217
218 (defun mml-parse ()
219   "Parse the current buffer as an MML document."
220   (save-excursion
221     (goto-char (point-min))
222     (with-syntax-table mml-syntax-table
223       (mml-parse-1))))
224
225 (defun mml-parse-1 ()
226   "Parse the current buffer as an MML document."
227   (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
228     (while (and (not (eobp))
229                 (not (looking-at "<#/multipart")))
230       (cond
231        ((looking-at "<#secure")
232         ;; The secure part is essentially a meta-meta tag, which
233         ;; expands to either a part tag if there are no other parts in
234         ;; the document or a multipart tag if there are other parts
235         ;; included in the message
236         (let* (secure-mode
237                (taginfo (mml-read-tag))
238                (keyfile (cdr (assq 'keyfile taginfo)))
239                (certfiles (delq nil (mapcar (lambda (tag)
240                                               (if (eq (car-safe tag) 'certfile)
241                                                   (cdr tag)))
242                                             taginfo)))
243                (recipients (cdr (assq 'recipients taginfo)))
244                (sender (cdr (assq 'sender taginfo)))
245                (location (cdr (assq 'tag-location taginfo)))
246                (mode (cdr (assq 'mode taginfo)))
247                (method (cdr (assq 'method taginfo)))
248                tags)
249           (save-excursion
250             (if (re-search-forward
251                  "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t)
252                 (setq secure-mode "multipart")
253               (setq secure-mode "part")))
254           (save-excursion
255             (goto-char location)
256             (re-search-forward "<#secure[^\n]*>\n"))
257           (delete-region (match-beginning 0) (match-end 0))
258           (cond ((string= mode "sign")
259                  (setq tags (list "sign" method)))
260                 ((string= mode "encrypt")
261                  (setq tags (list "encrypt" method)))
262                 ((string= mode "signencrypt")
263                  (setq tags (list "sign" method "encrypt" method))))
264           (eval `(mml-insert-tag ,secure-mode
265                                  ,@tags
266                                  ,(if keyfile "keyfile")
267                                  ,keyfile
268                                  ,@(apply #'append
269                                           (mapcar (lambda (certfile)
270                                                     (list "certfile" certfile))
271                                                   certfiles))
272                                  ,(if recipients "recipients")
273                                  ,recipients
274                                  ,(if sender "sender")
275                                  ,sender))
276           ;; restart the parse
277           (goto-char location)))
278        ((looking-at "<#multipart")
279         (push (nconc (mml-read-tag) (mml-parse-1)) struct))
280        ((looking-at "<#external")
281         (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
282               struct))
283        (t
284         (if (or (looking-at "<#part") (looking-at "<#mml"))
285             (setq tag (mml-read-tag)
286                   no-markup-p nil
287                   warn nil)
288           (setq tag (list 'part '(type . "text/plain"))
289                 no-markup-p t
290                 warn t))
291         (setq raw (cdr (assq 'raw tag))
292               point (point)
293               contents (mml-read-part (eq 'mml (car tag)))
294               charsets (cond
295                         (raw nil)
296                         ((assq 'charset tag)
297                          (list
298                           (intern (downcase (cdr (assq 'charset tag))))))
299                         (t
300                          (mm-find-mime-charset-region point (point)
301                                                       mm-hack-charsets))))
302         (when (and (not raw) (memq nil charsets))
303           (if (or (memq 'unknown-encoding mml-confirmation-set)
304                   (message-options-get 'unknown-encoding)
305                   (and (y-or-n-p "\
306 Message contains characters with unknown encoding.  Really send? ")
307                        (message-options-set 'unknown-encoding t)))
308               (if (setq use-ascii
309                         (or (memq 'use-ascii mml-confirmation-set)
310                             (message-options-get 'use-ascii)
311                             (and (y-or-n-p "Use ASCII as charset? ")
312                                  (message-options-set 'use-ascii t))))
313                   (setq charsets (delq nil charsets))
314                 (setq warn nil))
315             (error "Edit your message to remove those characters")))
316         (if (or raw
317                 (eq 'mml (car tag))
318                 (< (length charsets) 2))
319             (if (or (not no-markup-p)
320                     (string-match "[^ \t\r\n]" contents))
321                 ;; Don't create blank parts.
322                 (push (nconc tag (list (cons 'contents contents)))
323                       struct))
324           (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
325                           tag point (point) use-ascii)))
326             (when (and warn
327                        (not (memq 'multipart mml-confirmation-set))
328                        (not (message-options-get 'multipart))
329                        (not (and (y-or-n-p (format "\
330 A message part needs to be split into %d charset parts.  Really send? "
331                                                    (length nstruct)))
332                                  (message-options-set 'multipart t))))
333               (error "Edit your message to use only one charset"))
334             (setq struct (nconc nstruct struct)))))))
335     (unless (eobp)
336       (forward-line 1))
337     (nreverse struct)))
338
339 (defun mml-parse-singlepart-with-multiple-charsets
340   (orig-tag beg end &optional use-ascii)
341   (save-excursion
342     (save-restriction
343       (narrow-to-region beg end)
344       (goto-char (point-min))
345       (let ((current (or (mm-mime-charset (mm-charset-after))
346                          (and use-ascii 'us-ascii)))
347             charset struct space newline paragraph)
348         (while (not (eobp))
349           (setq charset (mm-mime-charset (mm-charset-after)))
350           (cond
351            ;; The charset remains the same.
352            ((eq charset 'us-ascii))
353            ((or (and use-ascii (not charset))
354                 (eq charset current))
355             (setq space nil
356                   newline nil
357                   paragraph nil))
358            ;; The initial charset was ascii.
359            ((eq current 'us-ascii)
360             (setq current charset
361                   space nil
362                   newline nil
363                   paragraph nil))
364            ;; We have a change in charsets.
365            (t
366             (push (append
367                    orig-tag
368                    (list (cons 'contents
369                                (buffer-substring-no-properties
370                                 beg (or paragraph newline space (point))))))
371                   struct)
372             (setq beg (or paragraph newline space (point))
373                   current charset
374                   space nil
375                   newline nil
376                   paragraph nil)))
377           ;; Compute places where it might be nice to break the part.
378           (cond
379            ((memq (following-char) '(?  ?\t))
380             (setq space (1+ (point))))
381            ((and (eq (following-char) ?\n)
382                  (not (bobp))
383                  (eq (char-after (1- (point))) ?\n))
384             (setq paragraph (point)))
385            ((eq (following-char) ?\n)
386             (setq newline (1+ (point)))))
387           (forward-char 1))
388         ;; Do the final part.
389         (unless (= beg (point))
390           (push (append orig-tag
391                         (list (cons 'contents
392                                     (buffer-substring-no-properties
393                                      beg (point)))))
394                 struct))
395         struct))))
396
397 (defun mml-read-tag ()
398   "Read a tag and return the contents."
399   (let ((orig-point (point))
400         contents name elem val)
401     (forward-char 2)
402     (setq name (buffer-substring-no-properties
403                 (point) (progn (forward-sexp 1) (point))))
404     (skip-chars-forward " \t\n")
405     (while (not (looking-at ">[ \t]*\n?"))
406       (setq elem (buffer-substring-no-properties
407                   (point) (progn (forward-sexp 1) (point))))
408       (skip-chars-forward "= \t\n")
409       (setq val (buffer-substring-no-properties
410                  (point) (progn (forward-sexp 1) (point))))
411       (when (string-match "\\`\"" val)
412         (setq val (read val))) ;; inverse of prin1 in mml-insert-tag
413       (push (cons (intern elem) val) contents)
414       (skip-chars-forward " \t\n"))
415     (goto-char (match-end 0))
416     ;; Don't skip the leading space.
417     ;;(skip-chars-forward " \t\n")
418     ;; Put the tag location into the returned contents
419     (setq contents (append (list (cons 'tag-location orig-point)) contents))
420     (cons (intern name) (nreverse contents))))
421
422 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
423   (let ((str (buffer-substring-no-properties start end))
424         (bufstart start) tmp)
425     (while (setq tmp (text-property-any start end 'hard 't))
426       (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
427                            '(hard t) str)
428       (setq start (1+ tmp)))
429     str))
430
431 (defun mml-read-part (&optional mml)
432   "Return the buffer up till the next part, multipart or closing part or multipart.
433 If MML is non-nil, return the buffer up till the correspondent mml tag."
434   (let ((beg (point)) (count 1))
435     ;; If the tag ended at the end of the line, we go to the next line.
436     (when (looking-at "[ \t]*\n")
437       (forward-line 1))
438     (if mml
439         (progn
440           (while (and (> count 0) (not (eobp)))
441             (if (re-search-forward "<#\\(/\\)?mml." nil t)
442                 (setq count (+ count (if (match-beginning 1) -1 1)))
443               (goto-char (point-max))))
444           (mml-buffer-substring-no-properties-except-hard-newlines
445            beg (if (> count 0)
446                    (point)
447                  (match-beginning 0))))
448       (if (re-search-forward
449            "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
450           (prog1
451               (mml-buffer-substring-no-properties-except-hard-newlines
452                beg (match-beginning 0))
453             (if (or (not (match-beginning 1))
454                     (equal (match-string 2) "multipart"))
455                 (goto-char (match-beginning 0))
456               (when (looking-at "[ \t]*\n")
457                 (forward-line 1))))
458         (mml-buffer-substring-no-properties-except-hard-newlines
459          beg (goto-char (point-max)))))))
460
461 (defvar mml-boundary nil)
462 (defvar mml-base-boundary "-=-=")
463 (defvar mml-multipart-number 0)
464 (defvar mml-inhibit-compute-boundary nil)
465
466 (defun mml-generate-mime ()
467   "Generate a MIME message based on the current MML document."
468   (let ((cont (mml-parse))
469         (mml-multipart-number mml-multipart-number)
470         (options message-options))
471     (if (not cont)
472         nil
473       (prog1
474           (mm-with-multibyte-buffer
475             (setq message-options options)
476             (if (and (consp (car cont))
477                      (= (length cont) 1))
478                 (mml-generate-mime-1 (car cont))
479               (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
480                                           cont)))
481             (setq options message-options)
482             (buffer-string))
483         (setq message-options options)))))
484
485 (defun mml-generate-mime-1 (cont)
486   (let ((mm-use-ultra-safe-encoding
487          (or mm-use-ultra-safe-encoding (assq 'sign cont))))
488     (save-restriction
489       (narrow-to-region (point) (point))
490       (mml-tweak-part cont)
491       (cond
492        ((or (eq (car cont) 'part) (eq (car cont) 'mml))
493         (let* ((raw (cdr (assq 'raw cont)))
494                (filename (cdr (assq 'filename cont)))
495                (type (or (cdr (assq 'type cont))
496                          (if filename
497                              (or (mm-default-file-encoding filename)
498                                  "application/octet-stream")
499                            "text/plain")))
500                (charset (cdr (assq 'charset cont)))
501                (coding (mm-charset-to-coding-system charset))
502                encoding flowed coded)
503           (cond ((eq coding 'ascii)
504                  (setq charset nil
505                        coding nil))
506                 (charset
507                  ;; The value of `charset' might be a bogus alias that
508                  ;; `mm-charset-synonym-alist' provides, like `utf8',
509                  ;; so we prefer the MIME charset that Emacs knows for
510                  ;; the coding system `coding'.
511                  (setq charset (or (mm-coding-system-to-mime-charset coding)
512                                    (intern (downcase charset))))))
513           (if (and (not raw)
514                    (member (car (split-string type "/")) '("text" "message")))
515               (progn
516                 (with-temp-buffer
517                   (cond
518                    ((cdr (assq 'buffer cont))
519                     (insert-buffer-substring (cdr (assq 'buffer cont))))
520                    ((and filename
521                          (not (equal (cdr (assq 'nofile cont)) "yes")))
522                     (let ((coding-system-for-read coding))
523                       (mm-insert-file-contents filename)))
524                    ((eq 'mml (car cont))
525                     (insert (cdr (assq 'contents cont))))
526                    (t
527                     (save-restriction
528                       (narrow-to-region (point) (point))
529                       (insert (cdr (assq 'contents cont)))
530                       ;; Remove quotes from quoted tags.
531                       (goto-char (point-min))
532                       (while (re-search-forward
533                               "<#!+/?\\(part\\|multipart\\|external\\|mml\\|secure\\)"
534                               nil t)
535                         (delete-region (+ (match-beginning 0) 2)
536                                        (+ (match-beginning 0) 3))))))
537                   (cond
538                    ((eq (car cont) 'mml)
539                     (let ((mml-boundary (mml-compute-boundary cont))
540                           ;; It is necessary for the case where this
541                           ;; function is called recursively since
542                           ;; `m-g-d-t' will be bound to "message/rfc822"
543                           ;; when encoding an article to be forwarded.
544                           (mml-generate-default-type "text/plain"))
545                       (mml-to-mime)
546                       ;; Update handle so mml-compute-boundary can
547                       ;; detect collisions with the nested parts.
548                       (unless mml-inhibit-compute-boundary
549                         (setcdr (assoc 'contents cont) (buffer-string))))
550                     (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
551                       ;; ignore 0x1b, it is part of iso-2022-jp
552                       (setq encoding (mm-body-7-or-8))))
553                    ((string= (car (split-string type "/")) "message")
554                     (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
555                       ;; ignore 0x1b, it is part of iso-2022-jp
556                       (setq encoding (mm-body-7-or-8))))
557                    (t
558                     ;; Only perform format=flowed filling on text/plain
559                     ;; parts where there either isn't a format parameter
560                     ;; in the mml tag or it says "flowed" and there
561                     ;; actually are hard newlines in the text.
562                     (let (use-hard-newlines)
563                       (when (and mml-enable-flowed
564                                  (string= type "text/plain")
565                                  (not (string= (cdr (assq 'sign cont)) "pgp"))
566                                  (or (null (assq 'format cont))
567                                      (string= (cdr (assq 'format cont))
568                                               "flowed"))
569                                  (setq use-hard-newlines
570                                        (text-property-any
571                                         (point-min) (point-max) 'hard 't)))
572                         (fill-flowed-encode)
573                         ;; Indicate that `mml-insert-mime-headers' should
574                         ;; insert a "; format=flowed" string unless the
575                         ;; user has already specified it.
576                         (setq flowed (null (assq 'format cont)))))
577                     ;; Prefer `utf-8' for text/calendar parts.
578                     (if (or charset
579                             (not (string= type "text/calendar")))
580                         (setq charset (mm-encode-body charset))
581                       (let ((mm-coding-system-priorities
582                              (cons 'utf-8 mm-coding-system-priorities)))
583                         (setq charset (mm-encode-body))))
584                     (setq encoding (mm-body-encoding
585                                     charset (cdr (assq 'encoding cont))))))
586                   (setq coded (buffer-string)))
587                 (mml-insert-mime-headers cont type charset encoding flowed)
588                 (insert "\n")
589                 (insert coded))
590             (mm-with-unibyte-buffer
591               (cond
592                ((cdr (assq 'buffer cont))
593                 (insert (mm-string-as-unibyte
594                          (with-current-buffer (cdr (assq 'buffer cont))
595                            (buffer-string)))))
596                ((and filename
597                      (not (equal (cdr (assq 'nofile cont)) "yes")))
598                 (let ((coding-system-for-read mm-binary-coding-system))
599                   (mm-insert-file-contents filename nil nil nil nil t))
600                 (unless charset
601                   (setq charset (mm-coding-system-to-mime-charset
602                                  (mm-find-buffer-file-coding-system
603                                   filename)))))
604                (t
605                 (let ((contents (cdr (assq 'contents cont))))
606                   (if (if (featurep 'xemacs)
607                           (string-match "[^\000-\377]" contents)
608                         (mm-multibyte-string-p contents))
609                       (progn
610                         (mm-enable-multibyte)
611                         (insert contents)
612                         (unless raw
613                           (setq charset (mm-encode-body charset))))
614                     (insert contents)))))
615               (if (setq encoding (cdr (assq 'encoding cont)))
616                   (setq encoding (intern (downcase encoding))))
617               (setq encoding (mm-encode-buffer type encoding)
618                     coded (mm-string-as-multibyte (buffer-string))))
619             (mml-insert-mime-headers cont type charset encoding nil)
620             (insert "\n" coded))))
621        ((eq (car cont) 'external)
622         (insert "Content-Type: message/external-body")
623         (let ((parameters (mml-parameter-string
624                            cont '(expiration size permission)))
625               (name (cdr (assq 'name cont)))
626               (url (cdr (assq 'url cont))))
627           (when name
628             (setq name (mml-parse-file-name name))
629             (if (stringp name)
630                 (mml-insert-parameter
631                  (mail-header-encode-parameter "name" name)
632                  "access-type=local-file")
633               (mml-insert-parameter
634                (mail-header-encode-parameter
635                 "name" (file-name-nondirectory (nth 2 name)))
636                (mail-header-encode-parameter "site" (nth 1 name))
637                (mail-header-encode-parameter
638                 "directory" (file-name-directory (nth 2 name))))
639               (mml-insert-parameter
640                (concat "access-type="
641                        (if (member (nth 0 name) '("ftp@" "anonymous@"))
642                            "anon-ftp"
643                          "ftp")))))
644           (when url
645             (mml-insert-parameter
646              (mail-header-encode-parameter "url" url)
647              "access-type=url"))
648           (when parameters
649             (mml-insert-parameter-string
650              cont '(expiration size permission)))
651           (insert "\n\n")
652           (insert "Content-Type: "
653                   (or (cdr (assq 'type cont))
654                       (if name
655                           (or (mm-default-file-encoding name)
656                               "application/octet-stream")
657                         "text/plain"))
658                &n