declare smobs in alloc.c
[bpt/emacs.git] / lisp / mh-e / mh-mime.el
1 ;;; mh-mime.el --- MH-E MIME support
2
3 ;; Copyright (C) 1993, 1995, 2001-2014 Free Software Foundation, Inc.
4
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Message composition of MIME message is done with either MH-style
28 ;; directives for mhn or mhbuild (MH 6.8 or later) or MML (MIME Meta
29 ;; Language) tags.
30
31 ;; TODO:
32 ;; Paragraph code should not fill # lines if MIME enabled.
33 ;; Implement mh-auto-mh-to-mime (if non-nil, \\[mh-send-letter]
34 ;; invokes mh-mh-to-mime automatically before sending.)
35 ;; Actually, instead of mh-auto-mh-to-mime,
36 ;; should read automhnproc from profile.
37 ;; MIME option to mh-forward command to move to content-description
38 ;; insertion point.
39
40 ;;; Change Log:
41
42 ;;; Code:
43
44 (require 'mh-e)
45 (require 'mh-gnus) ;needed because mh-gnus.el not compiled
46
47 (require 'font-lock)
48 (require 'gnus-util)
49 (require 'mailcap)
50 (require 'mm-decode)
51 (require 'mm-view)
52 (require 'mml)
53
54 (autoload 'article-emphasize "gnus-art")
55 (autoload 'gnus-eval-format "gnus-spec")
56 (autoload 'mail-content-type-get "mail-parse")
57 (autoload 'mail-decode-encoded-word-string "mail-parse")
58 (autoload 'mail-header-parse-content-type "mail-parse")
59 (autoload 'mail-header-strip "mail-parse")
60 (autoload 'mail-strip-quoted-names "mail-utils")
61 (autoload 'message-options-get "message")
62 (autoload 'message-options-set "message")
63 (autoload 'message-options-set-recipient "message")
64 (autoload 'mm-decode-body "mm-bodies")
65 (autoload 'mm-uu-dissect "mm-uu")
66 (autoload 'mml-unsecure-message "mml-sec")
67 (autoload 'rfc2047-decode-region "rfc2047")
68 (autoload 'widget-convert-button "wid-edit")
69
70 \f
71
72 ;;; Variables
73
74 ;; This has to be a macro, since we do: (setf (mh-buffer-data) ...)
75 ;;;###mh-autoload
76 (defmacro mh-buffer-data ()
77 "Convenience macro to get the MIME data structures of the current buffer."
78 `(gethash (current-buffer) mh-globals-hash))
79
80 ;; Structure to keep track of MIME handles on a per buffer basis.
81 (mh-defstruct (mh-buffer-data (:conc-name mh-mime-)
82 (:constructor mh-make-buffer-data))
83 (handles ()) ; List of MIME handles
84 (handles-cache (make-hash-table)) ; Cache to avoid multiple decodes of
85 ; nested messages
86 (parts-count 0) ; The button number is generated from
87 ; this number
88 (part-index-hash (make-hash-table))) ; Avoid incrementing the part number
89 ; for nested messages
90
91 (defvar mh-mm-inline-media-tests
92 `(("image/jpeg"
93 mm-inline-image
94 (lambda (handle)
95 (mm-valid-and-fit-image-p 'jpeg handle)))
96 ("image/png"
97 mm-inline-image
98 (lambda (handle)
99 (mm-valid-and-fit-image-p 'png handle)))
100 ("image/gif"
101 mm-inline-image
102 (lambda (handle)
103 (mm-valid-and-fit-image-p 'gif handle)))
104 ("image/tiff"
105 mm-inline-image
106 (lambda (handle)
107 (mm-valid-and-fit-image-p 'tiff handle)) )
108 ("image/xbm"
109 mm-inline-image
110 (lambda (handle)
111 (mm-valid-and-fit-image-p 'xbm handle)))
112 ("image/x-xbitmap"
113 mm-inline-image
114 (lambda (handle)
115 (mm-valid-and-fit-image-p 'xbm handle)))
116 ("image/xpm"
117 mm-inline-image
118 (lambda (handle)
119 (mm-valid-and-fit-image-p 'xpm handle)))
120 ("image/x-pixmap"
121 mm-inline-image
122 (lambda (handle)
123 (mm-valid-and-fit-image-p 'xpm handle)))
124 ("image/bmp"
125 mm-inline-image
126 (lambda (handle)
127 (mm-valid-and-fit-image-p 'bmp handle)))
128 ("image/x-portable-bitmap"
129 mm-inline-image
130 (lambda (handle)
131 (mm-valid-and-fit-image-p 'pbm handle)))
132 ("text/plain" mm-inline-text identity)
133 ("text/enriched" mm-inline-text identity)
134 ("text/richtext" mm-inline-text identity)
135 ("text/x-patch" mm-display-patch-inline
136 (lambda (handle)
137 (locate-library "diff-mode")))
138 ("application/emacs-lisp" mm-display-elisp-inline identity)
139 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
140 ("text/html"
141 ,(if (fboundp 'mm-inline-text-html) 'mm-inline-text-html 'mm-inline-text)
142 (lambda (handle)
143 (or (and (boundp 'mm-inline-text-html-renderer)
144 mm-inline-text-html-renderer)
145 (and (boundp 'mm-text-html-renderer) mm-text-html-renderer))))
146 ("text/x-vcard"
147 mh-mm-inline-text-vcard
148 (lambda (handle)
149 (or (featurep 'vcard)
150 (locate-library "vcard"))))
151 ("message/delivery-status" mm-inline-text identity)
152 ("message/rfc822" mh-mm-inline-message identity)
153 ;;("message/partial" mm-inline-partial identity)
154 ;;("message/external-body" mm-inline-external-body identity)
155 ("text/.*" mm-inline-text identity)
156 ("audio/wav" mm-inline-audio
157 (lambda (handle)
158 (and (or (featurep 'nas-sound) (featurep 'native-sound))
159 (device-sound-enabled-p))))
160 ("audio/au"
161 mm-inline-audio
162 (lambda (handle)
163 (and (or (featurep 'nas-sound) (featurep 'native-sound))
164 (device-sound-enabled-p))))
165 ("application/pgp-signature" ignore identity)
166 ("application/x-pkcs7-signature" ignore identity)
167 ("application/pkcs7-signature" ignore identity)
168 ("application/x-pkcs7-mime" ignore identity)
169 ("application/pkcs7-mime" ignore identity)
170 ("multipart/alternative" ignore identity)
171 ("multipart/mixed" ignore identity)
172 ("multipart/related" ignore identity)
173 ;; Disable audio and image
174 ("audio/.*" ignore ignore)
175 ("image/.*" ignore ignore)
176 ;; Default to displaying as text
177 (".*" mm-inline-text mh-mm-readable-p))
178 "Alist of media types/tests saying whether types can be displayed inline.")
179
180 (defvar mh-mime-save-parts-directory nil
181 "Default to use for `mh-mime-save-parts-default-directory'.
182 Set from last use.")
183
184 ;; Copied from gnus-art.el (should be checked for other cool things that can
185 ;; be added to the buttons)
186 (defvar mh-mime-button-commands
187 '((mh-press-button "\r" "Toggle Display")))
188 (defvar mh-mime-button-map
189 (let ((map (make-sparse-keymap)))
190 (unless (>= (string-to-number emacs-version) 21)
191 ;; XEmacs doesn't care.
192 (set-keymap-parent map mh-show-mode-map))
193 (mh-do-in-gnu-emacs
194 (define-key map [mouse-2] 'mh-push-button))
195 (mh-do-in-xemacs
196 (define-key map '(button2) 'mh-push-button))
197 (dolist (c mh-mime-button-commands)
198 (define-key map (cadr c) (car c)))
199 map))
200 (defvar mh-mime-button-line-format-alist
201 '((?T long-type ?s)
202 (?d description ?s)
203 (?p index ?s)
204 (?e dots ?s)))
205 (defvar mh-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n")
206 (defvar mh-mime-security-button-pressed nil)
207 (defvar mh-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n")
208 (defvar mh-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n")
209 (defvar mh-mime-security-button-line-format-alist
210 '((?t type ?s)
211 (?i info ?s)
212 (?d details ?s)
213 (?D pressed-details ?s)))
214 (defvar mh-mime-security-button-map
215 (let ((map (make-sparse-keymap)))
216 (unless (>= (string-to-number emacs-version) 21)
217 (set-keymap-parent map mh-show-mode-map))
218 (define-key map "\r" 'mh-press-button)
219 (mh-do-in-gnu-emacs
220 (define-key map [mouse-2] 'mh-push-button))
221 (mh-do-in-xemacs
222 (define-key map '(button2) 'mh-push-button))
223 map))
224
225 \f
226
227 ;;; MH-Folder Commands
228
229 ;; Alphabetical.
230
231 ;;;###mh-autoload
232 (defun mh-display-with-external-viewer (part-index)
233 "View attachment externally.
234
235 If Emacs does not know how to view an attachment, you could save
236 it into a file and then run some program to open it. It is
237 easier, however, to launch the program directly from MH-E with
238 this command. While you'll most likely use this to view
239 spreadsheets and documents, it is also useful to use your browser
240 to view HTML attachments with higher fidelity than what Emacs can
241 provide.
242
243 This command displays the attachment associated with the button
244 under the cursor. If the cursor is not located over a button,
245 then the cursor first moves to the next button, wrapping to the
246 beginning of the message if necessary. You can provide a numeric
247 prefix argument PART-INDEX to view the attachment labeled with
248 that number.
249
250 This command tries to provide a reasonable default for the viewer
251 by calling the Emacs function `mailcap-mime-info'. This function
252 usually reads the file \"/etc/mailcap\"."
253 (interactive "P")
254 (when (consp part-index) (setq part-index (car part-index)))
255 (mh-folder-mime-action
256 part-index
257 #'(lambda ()
258 (let* ((part (get-text-property (point) 'mh-data))
259 (type (mm-handle-media-type part))
260 (methods (mapcar (lambda (x) (list (cdr (assoc 'viewer x))))
261 (mailcap-mime-info type 'all)))
262 (def (caar methods))
263 (prompt (format "Viewer%s: " (if def
264 (format " (default %s)" def)
265 "")))
266 (method (completing-read prompt methods nil nil nil nil def))
267 (folder mh-show-folder-buffer)
268 (buffer-read-only nil))
269 (when (string-match "^[^% \t]+$" method)
270 (setq method (concat method " %s")))
271 (mh-cl-flet
272 ((mm-handle-set-external-undisplayer
273 (handle function)
274 (mh-handle-set-external-undisplayer folder handle function)))
275 (unwind-protect (mm-display-external part method)
276 (set-buffer-modified-p nil)))))
277 nil))
278
279 ;;;###mh-autoload
280 (defun mh-folder-inline-mime-part (part-index)
281 "Show attachment verbatim.
282
283 You can view the raw contents of an attachment with this command.
284 This command displays (or hides) the contents of the attachment
285 associated with the button under the cursor verbatim. If the
286 cursor is not located over a button, then the cursor first moves
287 to the next button, wrapping to the beginning of the message if
288 necessary.
289
290 You can also provide a numeric prefix argument PART-INDEX to view
291 the attachment labeled with that number."
292 (interactive "P")
293 (when (consp part-index) (setq part-index (car part-index)))
294 (mh-folder-mime-action part-index #'mh-mime-inline-part nil))
295
296 (defun mh-mime-inline-part ()
297 "Toggle display of the raw MIME part."
298 (interactive)
299 (let* ((buffer-read-only nil)
300 (data (get-text-property (point) 'mh-data))
301 (inserted-flag (get-text-property (point) 'mh-mime-inserted))
302 (displayed-flag (mm-handle-displayed-p data))
303 (point (point))
304 start end)
305 (cond ((and data (not inserted-flag) (not displayed-flag))
306 (let ((contents (mm-get-part data)))
307 (add-text-properties (mh-line-beginning-position)
308 (mh-line-end-position) '(mh-mime-inserted t))
309 (setq start (point-marker))
310 (forward-line 1)
311 (mm-insert-inline data contents)
312 (setq end (point-marker))
313 (add-text-properties
314 start (progn (goto-char start) (mh-line-end-position))
315 `(mh-region (,start . ,end)))))
316 ((and data (or inserted-flag displayed-flag))
317 (mh-press-button)
318 (message "MIME part already inserted")))
319 (goto-char point)
320 (set-buffer-modified-p nil)))
321
322 ;;;###mh-autoload
323 (defun mh-folder-save-mime-part (part-index)
324 "Save (output) attachment.
325
326 This command saves the attachment associated with the button under the
327 cursor. If the cursor is not located over a button, then the cursor
328 first moves to the next button, wrapping to the beginning of the
329 message if necessary.
330
331 You can also provide a numeric prefix argument PART-INDEX to save the
332 attachment labeled with that number.
333
334 This command prompts you for a filename and suggests a specific name
335 if it is available."
336 (interactive "P")
337 (when (consp part-index) (setq part-index (car part-index)))
338 (mh-folder-mime-action part-index #'mh-mime-save-part nil))
339
340 (defun mh-mime-save-part ()
341 "Save MIME part at point."
342 (interactive)
343 (let ((data (get-text-property (point) 'mh-data)))
344 (when data
345 (let ((mm-default-directory
346 (file-name-as-directory (or mh-mime-save-parts-directory
347 default-directory))))
348 (mh-mm-save-part data)
349 (setq mh-mime-save-parts-directory mm-default-directory)))))
350
351 ;;;###mh-autoload
352 (defun mh-folder-toggle-mime-part (part-index)
353 "View attachment.
354
355 This command displays (or hides) the attachment associated with
356 the button under the cursor. If the cursor is not located over a
357 button, then the cursor first moves to the next button, wrapping
358 to the beginning of the message if necessary. This command has
359 the advantage over related commands of working from the MH-Folder
360 buffer.
361
362 You can also provide a numeric prefix argument PART-INDEX to view
363 the attachment labeled with that number. If Emacs does not know
364 how to display the attachment, then Emacs offers to save the
365 attachment in a file."
366 (interactive "P")
367 (when (consp part-index) (setq part-index (car part-index)))
368 (mh-folder-mime-action part-index #'mh-press-button t))
369
370 ;;;###mh-autoload
371 (defun mh-mime-save-parts (prompt)
372 "Save attachments.
373
374 You can save all of the attachments at once with this command.
375 The attachments are saved in the directory specified by the
376 option `mh-mime-save-parts-default-directory' unless you use a
377 prefix argument PROMPT in which case you are prompted for the
378 directory. These directories may be superseded by MH profile
379 components, since this function calls on \"mhstore\" (\"mhn\") to
380 do the work."
381 (interactive "P")
382 (let ((msg (if (eq major-mode 'mh-show-mode)
383 (mh-show-buffer-message-number)
384 (mh-get-msg-num t)))
385 (folder (if (eq major-mode 'mh-show-mode)
386 mh-show-folder-buffer
387 mh-current-folder))
388 (command (if (mh-variant-p 'nmh) "mhstore" "mhn"))
389 (directory
390 (cond
391 ((and (or prompt
392 (equal nil mh-mime-save-parts-default-directory)
393 (equal t mh-mime-save-parts-default-directory))
394 (not mh-mime-save-parts-directory))
395 (read-directory-name "Store in directory: " nil nil t))
396 ((and (or prompt
397 (equal t mh-mime-save-parts-default-directory))
398 mh-mime-save-parts-directory)
399 (read-directory-name (format
400 "Store in directory (default %s): "
401 mh-mime-save-parts-directory)
402 "" mh-mime-save-parts-directory t ""))
403 ((stringp mh-mime-save-parts-default-directory)
404 mh-mime-save-parts-default-directory)
405 (t
406 mh-mime-save-parts-directory))))
407 (if (and (equal directory "") mh-mime-save-parts-directory)
408 (setq directory mh-mime-save-parts-directory))
409 (if (not (file-directory-p directory))
410 (message "No directory specified")
411 (if (equal nil mh-mime-save-parts-default-directory)
412 (setq mh-mime-save-parts-directory directory))
413 (with-current-buffer (get-buffer-create mh-log-buffer)
414 (cd directory)
415 (setq mh-mime-save-parts-directory directory)
416 (let ((initial-size (mh-truncate-log-buffer)))
417 (apply 'call-process
418 (expand-file-name command mh-progs) nil t nil
419 (mh-list-to-string (list folder msg "-auto"
420 (if (not (mh-variant-p 'nmh))
421 "-store"))))
422 (if (> (buffer-size) initial-size)
423 (save-window-excursion
424 (switch-to-buffer-other-window mh-log-buffer)
425 (sit-for 3))))))))
426
427 ;;;###mh-autoload
428 (defun mh-toggle-mh-decode-mime-flag ()
429 "Toggle the value of `mh-decode-mime-flag'."
430 (interactive)
431 (setq mh-decode-mime-flag (not mh-decode-mime-flag))
432 (mh-show nil t)
433 (message "%s" (if mh-decode-mime-flag
434 "Processing attachments normally"
435 "Displaying raw message")))
436
437 ;;;###mh-autoload
438 (defun mh-toggle-mime-buttons ()
439 "Toggle option `mh-display-buttons-for-inline-parts-flag'."
440 (interactive)
441 (setq mh-display-buttons-for-inline-parts-flag
442 (not mh-display-buttons-for-inline-parts-flag))
443 (mh-show nil t))
444
445 \f
446
447 ;;; MIME Display Routines
448
449 (defun mh-mm-inline-message (handle)
450 "Display message, HANDLE.
451 The function decodes the message and displays it. It avoids
452 decoding the same message multiple times."
453 (let ((b (point))
454 (clean-message-header mh-clean-message-header-flag)
455 (invisible-headers mh-invisible-header-fields-compiled)
456 (visible-headers nil))
457 (save-excursion
458 (save-restriction
459 (narrow-to-region b b)
460 (mm-insert-part handle)
461 (mh-mime-display
462 (or (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
463 (setf (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
464 (let ((handles (mm-dissect-buffer nil)))
465 (if handles
466 (mh-mm-uu-dissect-text-parts handles)
467 (setq handles (mm-uu-dissect)))
468 (setf (mh-mime-handles (mh-buffer-data))
469 (mh-mm-merge-handles
470 handles (mh-mime-handles (mh-buffer-data))))
471 handles))))
472
473 (goto-char (point-min))
474 (mh-show-xface)
475 (cond (clean-message-header
476 (mh-clean-msg-header (point-min)
477 invisible-headers
478 visible-headers)
479 (goto-char (point-min)))
480 (t
481 (mh-start-of-uncleaned-message)))
482 (mh-decode-message-header)
483 (mh-show-addr)
484 ;; The other highlighting types don't need anything special
485 (when (eq mh-highlight-citation-style 'gnus)
486 (mh-gnus-article-highlight-citation))
487 (goto-char (point-min))
488 (insert "\n------- Forwarded Message\n\n")
489 (mh-display-smileys)
490 (mh-display-emphasis)
491 (mm-handle-set-undisplayer
492 handle
493 `(lambda ()
494 (let (buffer-read-only)
495 (if (fboundp 'remove-specifier)
496 ;; This is only valid on XEmacs.
497 (mapcar (lambda (prop)
498 (remove-specifier
499 (face-property 'default prop) (current-buffer)))
500 '(background background-pixmap foreground)))
501 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
502
503 ;;;###mh-autoload
504 (defun mh-decode-message-header ()
505 "Decode RFC2047 encoded message header fields."
506 (when mh-decode-mime-flag
507 (let ((buffer-read-only nil))
508 (rfc2047-decode-region (point-min) (mh-mail-header-end)))))
509
510 ;;;###mh-autoload
511 (defun mh-decode-message-subject ()
512 "Decode RFC2047 encoded message header fields."
513 (when mh-decode-mime-flag
514 (save-excursion
515 (let ((buffer-read-only nil))
516 (rfc2047-decode-region (progn (mh-goto-header-field "Subject:") (point))
517 (progn (mh-header-field-end) (point)))))))
518
519 ;;;###mh-autoload
520 (defun mh-mime-display (&optional pre-dissected-handles)
521 "Display (and possibly decode) MIME handles.
522 Optional argument, PRE-DISSECTED-HANDLES is a list of MIME
523 handles. If present they are displayed otherwise the buffer is
524 parsed and then displayed."
525 (let ((handles ())
526 (folder mh-show-folder-buffer)
527 (raw-message-data (buffer-string)))
528 (mh-cl-flet
529 ((mm-handle-set-external-undisplayer
530 (handle function)
531 (mh-handle-set-external-undisplayer folder handle function)))
532 (goto-char (point-min))
533 (unless (search-forward "\n\n" nil t)
534 (goto-char (point-max))
535 (insert "\n\n"))
536
537 (condition-case err
538 (progn
539 ;; If needed dissect the current buffer
540 (if pre-dissected-handles
541 (setq handles pre-dissected-handles)
542 (if (setq handles (mm-dissect-buffer nil))
543 (mh-mm-uu-dissect-text-parts handles)
544 (setq handles (mm-uu-dissect)))
545 (setf (mh-mime-handles (mh-buffer-data))
546 (mh-mm-merge-handles handles
547 (mh-mime-handles (mh-buffer-data))))
548 (unless handles
549 (mh-decode-message-body)))
550
551 (cond ((and handles
552 (or (not (stringp (car handles)))
553 (cdr handles)))
554 ;; Go to start of message body
555 (goto-char (point-min))
556 (or (search-forward "\n\n" nil t)
557 (goto-char (point-max)))
558
559 ;; Delete the body
560 (delete-region (point) (point-max))
561
562 ;; Display the MIME handles
563 (mh-mime-display-part handles))
564 (t
565 (mh-signature-highlight))))
566 (error
567 (message "Could not display body: %s" (error-message-string err))
568 (delete-region (point-min) (point-max))
569 (insert raw-message-data))))))
570
571 (defun mh-decode-message-body ()
572 "Decode message based on charset.
573 If message has been encoded for transfer take that into account."
574 (let (ct charset cte)
575 (goto-char (point-min))
576 (re-search-forward "\n\n" nil t)
577 (save-restriction
578 (narrow-to-region (point-min) (point))
579 (setq ct (ignore-errors (mail-header-parse-content-type
580 (message-fetch-field "Content-Type" t)))
581 charset (mail-content-type-get ct 'charset)
582 cte (message-fetch-field "Content-Transfer-Encoding")))
583 (when (stringp cte) (setq cte (mail-header-strip cte)))
584 (when (or (not ct) (equal (car ct) "text/plain"))
585 (save-restriction
586 (narrow-to-region (min (1+ (mh-mail-header-end)) (point-max))
587 (point-max))
588 (mm-decode-body charset
589 (and cte (intern (downcase
590 (gnus-strip-whitespace cte))))
591 (car ct))))))
592
593 (defun mh-mime-display-part (handle)
594 "Decides the viewer to call based on the type of HANDLE."
595 (cond ((null handle)
596 nil)
597 ((not (stringp (car handle)))
598 (mh-mime-display-single handle))
599 ((equal (car handle) "multipart/alternative")
600 (mh-mime-display-alternative (cdr handle)))
601 ((and mh-pgp-support-flag
602 (or (equal (car handle) "multipart/signed")
603 (equal (car handle) "multipart/encrypted")))
604 (mh-mime-display-security handle))
605 (t
606 (mh-mime-display-mixed (cdr handle)))))
607
608 (defun mh-mime-display-mixed (handles)
609 "Display the list of MIME parts, HANDLES recursively."
610 (mapcar #'mh-mime-display-part handles))
611
612 (defun mh-mime-display-alternative (handles)
613 "Choose among the alternatives, HANDLES the part that will be displayed.
614 If no part is preferred then all the parts are displayed."
615 (let* ((preferred (mm-preferred-alternative handles))
616 (others (loop for x in handles unless (eq x preferred) collect x)))
617 (cond ((and preferred
618 (stringp (car preferred)))
619 (mh-mime-display-part preferred)
620 (mh-mime-maybe-display-alternatives others))
621 (preferred
622 (save-restriction
623 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
624 (mh-mime-display-single preferred)
625 (mh-mime-maybe-display-alternatives others)
626 (goto-char (point-max))))
627 (t
628 (mh-mime-display-mixed handles)))))
629
630 (defun mh-mime-maybe-display-alternatives (alternatives)
631 "Show buttons for ALTERNATIVES.
632 If `mh-mime-display-alternatives-flag' is non-nil then display
633 buttons for alternative parts that are usually suppressed."
634 (when (and mh-display-buttons-for-alternatives-flag alternatives)
635 (insert "\n----------------------------------------------------\n")
636 (insert "Alternatives:\n")
637 (dolist (x alternatives)
638 (insert "\n")
639 (mh-insert-mime-button x (mh-mime-part-index x) nil))
640 (insert "\n----------------------------------------------------\n")))
641
642 (defun mh-mime-display-security (handle)
643 "Display PGP encrypted/signed message, HANDLE."
644 (save-restriction
645 (narrow-to-region (point) (point))
646 (insert "\n")
647 (mh-insert-mime-security-button handle)
648 (mh-mime-display-mixed (cdr handle))
649 (insert "\n")
650 (let ((mh-mime-security-button-line-format
651 mh-mime-security-button-end-line-format))
652 (mh-insert-mime-security-button handle))
653 (mh-mm-set-handle-multipart-parameter
654 handle 'mh-region (cons (point-min-marker) (point-max-marker)))))
655
656 (defun mh-mime-display-single (handle)
657 "Display a leaf node, HANDLE in the MIME tree."
658 (let* ((type (mm-handle-media-type handle))
659 (small-image-flag (mh-small-image-p handle))
660 (attachmentp (equal (car (mm-handle-disposition handle))
661 "attachment"))
662 (inlinep (and (equal (car (mm-handle-disposition handle)) "inline")
663 (mm-inlinable-p handle)
664 (mm-inlined-p handle)))
665 (displayp (or inlinep ; show if inline OR
666 (mh-inline-vcard-p handle); inline vcard OR
667 (and (not attachmentp) ; if not an attachment
668 (or small-image-flag ; and small image
669 ; and user wants inline
670 (and (not (equal
671 (mm-handle-media-supertype handle)
672 "image"))
673 (mm-inlinable-p handle)
674 (mm-inlined-p handle)))))))
675 (save-restriction
676 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
677 (cond ((and mh-pgp-support-flag
678 (equal type "application/pgp-signature"))
679 nil) ; skip signatures as they are already handled...
680 ((not displayp)
681 (insert "\n")
682 (mh-insert-mime-button handle (mh-mime-part-index handle) nil))
683 ((and displayp
684 (not mh-display-buttons-for-inline-parts-flag))
685 (or (mm-display-part handle)
686 (mm-display-part handle))
687 (mh-signature-highlight handle))
688 ((and displayp
689 mh-display-buttons-for-inline-parts-flag)
690 (insert "\n")
691 (mh-insert-mime-button handle (mh-mime-part-index handle) nil)
692 (forward-line -1)
693 (mh-mm-display-part handle)))
694 (goto-char (point-max)))))
695
696 ;; There is a bug in Gnus inline image display due to which an extra line
697 ;; gets inserted every time it is viewed. To work around that problem we are
698 ;; using an extra property 'mh-region to remember the region that is added
699 ;; when the button is clicked. The region is then deleted to make sure that
700 ;; no extra lines get inserted.
701 (defun mh-mm-display-part (handle)
702 "Toggle display of button for MIME part, HANDLE."
703 (beginning-of-line)
704 (let ((id (get-text-property (point) 'mh-part))
705 (point (point))
706 (window (selected-window))
707 (mail-parse-charset 'nil)
708 (mail-parse-ignored-charsets nil)
709 region buffer-read-only)
710 (save-excursion
711 (unwind-protect
712 (let ((win (get-buffer-window (current-buffer) t)))
713 (when win
714 (select-window win))
715 (goto-char point)
716
717 (if (mm-handle-displayed-p handle)
718 ;; This will remove the part.
719 (progn
720 ;; Delete the button and displayed part (if any)
721 (let ((region (get-text-property point 'mh-region)))
722 (when region
723 (mh-funcall-if-exists
724 remove-images (car region) (cdr region)))
725 (mm-display-part handle)
726 (when region
727 (delete-region (car region) (cdr region))))
728 ;; Delete button (if it still remains). This happens for
729 ;; externally displayed parts where the previous step does
730 ;; nothing.
731 (unless (eolp)
732 (delete-region (point) (progn (forward-line) (point)))))
733 (save-restriction
734 (delete-region (point) (progn (forward-line 1) (point)))
735 (narrow-to-region (point) (point))
736 ;; Maybe we need another unwind-protect here.
737 (when (equal (mm-handle-media-supertype handle) "image")
738 (insert "\n"))
739 (when (and (not (eq (ignore-errors (mm-display-part handle))
740 'inline))
741 (equal (mm-handle-media-supertype handle)
742 "image"))
743 (goto-char (point-min))
744 (delete-char 1))
745 (when (equal (mm-handle-media-supertype handle) "text")
746 (when (eq mh-highlight-citation-style 'gnus)
747 (mh-gnus-article-highlight-citation))
748 (mh-display-smileys)
749 (mh-display-emphasis)
750 (mh-signature-highlight handle))
751 (setq region (cons (progn (goto-char (point-min))
752 (point-marker))
753 (progn (goto-char (point-max))
754 (point-marker)))))))
755 (when (window-live-p window)
756 (select-window window))
757 (goto-char point)
758 (beginning-of-line)
759 (mh-insert-mime-button handle id (mm-handle-displayed-p handle))
760 (goto-char point)
761 (when region
762 (add-text-properties (mh-line-beginning-position)
763 (mh-line-end-position)
764 `(mh-region ,region)))))))
765
766 (defun mh-mime-part-index (handle)
767 "Generate the button number for MIME part, HANDLE.
768 Notice that a hash table is used to display the same number when
769 buttons need to be displayed multiple times (for instance when
770 nested messages are opened)."
771 (or (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
772 (setf (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
773 (incf (mh-mime-parts-count (mh-buffer-data))))))
774
775 (defun mh-small-image-p (handle)
776 "Decide whether HANDLE is a \"small\" image that can be displayed inline.
777 This is only useful if a Content-Disposition header is not present."
778 (let ((media-test (caddr (assoc (car (mm-handle-type handle))
779 mh-mm-inline-media-tests)))
780 (mm-inline-large-images t))
781 (and media-test
782 (equal (mm-handle-media-supertype handle) "image")
783 (funcall media-test handle) ; Since mm-inline-large-images is T,
784 ; this only tells us if the image is
785 ; something that emacs can display
786 (let* ((image (mm-get-image handle)))
787 (or (mh-do-in-xemacs
788 (and (mh-funcall-if-exists glyphp image)
789 (< (glyph-width image)
790 (or mh-max-inline-image-width (window-pixel-width)))
791 (< (glyph-height image)
792 (or mh-max-inline-image-height
793 (window-pixel-height)))))
794 (mh-do-in-gnu-emacs
795 (let ((size (mh-funcall-if-exists image-size image)))
796 (and size
797 (< (cdr size) (or mh-max-inline-image-height
798 (1- (window-height))))
799 (< (car size) (or mh-max-inline-image-width
800 (window-width)))))))))))
801
802 (defun mh-inline-vcard-p (handle)
803 "Decide if HANDLE is a vcard that must be displayed inline."
804 (let ((type (mm-handle-type handle)))
805 (and (or (featurep 'vcard) (fboundp 'vcard-pretty-print))
806 (consp type)
807 (equal (car type) "text/x-vcard")
808 (save-excursion
809 (save-restriction
810 (widen)
811 (goto-char (point-min))
812 (not (mh-signature-separator-p)))))))
813
814 (defun mh-signature-highlight (&optional handle)
815 "Highlight message signature in HANDLE.
816 The optional argument, HANDLE is a MIME handle if the function is
817 being used to highlight the signature in a MIME part."
818 (let ((regexp
819 (cond ((not handle) "^-- $")
820 ((not (and (equal (mm-handle-media-supertype handle) "text")
821 (equal (mm-handle-media-subtype handle) "html")))
822 "^-- $")
823 ((eq (mh-mm-text-html-renderer) 'lynx) "^ --$")
824 (t "^--$"))))
825 (save-excursion
826 (goto-char (point-max))
827 (when (re-search-backward regexp nil t)
828 (mh-do-in-gnu-emacs
829 (let ((ov (make-overlay (point) (point-max))))
830 (overlay-put ov 'face 'mh-show-signature)
831 (overlay-put ov 'evaporate t)))
832 (mh-do-in-xemacs
833 (set-extent-property (make-extent (point) (point-max))
834 'face 'mh-show-signature))))))
835
836 \f
837
838 ;;; Button Display
839
840 ;; Shush compiler.
841 (mh-do-in-xemacs
842 (defvar dots)
843 (defvar type)
844 (defvar ov))
845
846 (defun mh-insert-mime-button (handle index displayed)
847 "Insert MIME button for HANDLE.
848 INDEX is the part number that will be DISPLAYED. It is also used
849 by commands like \"K v\" which operate on individual MIME parts."
850 ;; The button could be displayed by a previous decode. In that case
851 ;; undisplay it if we need a hidden button.
852 (when (and (mm-handle-displayed-p handle) (not displayed))
853 (mm-display-part handle))
854 (let ((name (or (mail-content-type-get (mm-handle-type handle) 'name)
855 (mail-content-type-get (mm-handle-disposition handle)
856 'filename)
857 (mail-content-type-get (mm-handle-type handle) 'url)
858 ""))
859 (type (mm-handle-media-type handle))
860 (description (mail-decode-encoded-word-string
861 (or (mm-handle-description handle) "")))
862 (dots (if (or displayed (mm-handle-displayed-p handle)) " " "..."))
863 long-type begin end)
864 (if (string-match ".*/" name) (setq name (substring name (match-end 0))))
865 (setq long-type (concat type (and (not (equal name ""))
866 (concat "; " name))))
867 (unless (equal description "")
868 (setq long-type (concat " --- " long-type)))
869 (unless (bolp) (insert "\n"))
870 (setq begin (point))
871 (gnus-eval-format
872 mh-mime-button-line-format mh-mime-button-line-format-alist
873 `(,@(mh-gnus-local-map-property mh-mime-button-map)
874 mh-callback mh-mm-display-part
875 mh-part ,index
876 mh-data ,handle))
877 (setq end (point))
878 (widget-convert-button
879 'link begin end
880 :mime-handle handle
881 :action 'mh-widget-press-button
882 :button-keymap mh-mime-button-map
883 :help-echo
884 "Mouse-2 click or press RET (in show buffer) to toggle display")
885 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
886 (mh-funcall-if-exists overlay-put ov 'evaporate t))))
887
888 ;; Shush compiler.
889 (defvar mm-verify-function-alist) ; < Emacs 22
890 (defvar mm-decrypt-function-alist) ; < Emacs 22
891 (mh-do-in-xemacs
892 (defvar pressed-details))
893
894 (defun mh-insert-mime-security-button (handle)
895 "Display buttons for PGP message, HANDLE."
896 (let* ((protocol (mh-mm-handle-multipart-ctl-parameter handle 'protocol))
897 (crypto-type (or (nth 2 (assoc protocol mm-verify-function-alist))
898 (nth 2 (assoc protocol mm-decrypt-function-alist))
899 "Unknown"))
900 (type (concat crypto-type
901 (if (equal (car handle) "multipart/signed")
902 " Signed" " Encrypted")
903 " Part"))
904 (info (or (mh-mm-handle-multipart-ctl-parameter handle 'gnus-info)
905 "Undecided"))
906 (details (mh-mm-handle-multipart-ctl-parameter handle 'gnus-details))
907 pressed-details begin end face)
908 (setq details (if details (concat "\n" details) ""))
909 (setq pressed-details (if mh-mime-security-button-pressed details ""))
910 (setq face (mh-mime-security-button-face info))
911 (unless (bolp) (insert "\n"))
912 (setq begin (point))
913 (gnus-eval-format
914 mh-mime-security-button-line-format
915 mh-mime-security-button-line-format-alist
916 `(,@(mh-gnus-local-map-property mh-mime-security-button-map)
917 mh-button-pressed ,mh-mime-security-button-pressed
918 mh-callback mh-mime-security-press-button
919 mh-line-format ,mh-mime-security-button-line-format
920 mh-data ,handle))
921 (setq end (point))
922 (widget-convert-button 'link begin end
923 :mime-handle handle
924 :action 'mh-widget-press-button
925 :button-keymap mh-mime-security-button-map
926 :button-face face
927 :help-echo "Mouse-2 click or press RET (in show buffer) to see security details.")
928 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
929 (mh-funcall-if-exists overlay-put ov 'evaporate t))
930 (when (equal info "Failed")
931 (let* ((type (if (equal (car handle) "multipart/signed")
932 "verification" "decryption"))
933 (warning (if (equal type "decryption")
934 "(passphrase may be incorrect)" "")))
935 (message "%s %s failed %s" crypto-type type warning)))))
936
937 (defun mh-mime-security-button-face (info)
938 "Return the button face to use for encrypted/signed mail based on INFO."
939 (cond ((string-match "OK" info) ;Decrypted mail
940 'mh-show-pgg-good)
941 ((string-match "Failed" info) ;Decryption failed or signature invalid
942 'mh-show-pgg-bad)
943 ((string-match "Undecided" info);Unprocessed mail
944 'mh-show-pgg-unknown)
945 ((string-match "Untrusted" info);Key not trusted
946 'mh-show-pgg-unknown)
947 (t
948 'mh-show-pgg-good)))
949
950 \f
951
952 ;;; Button Handlers
953
954 (defun mh-folder-mime-action (part-index action include-security-flag)
955 "Go to PART-INDEX and carry out ACTION.
956
957 If PART-INDEX is nil then go to the next part in the buffer. The
958 search for the next buffer wraps around if end of buffer is reached.
959 If argument INCLUDE-SECURITY-FLAG is non-nil then include security
960 info buttons when searching for a suitable parts."
961 (unless mh-showing-mode
962 (mh-show))
963 (mh-in-show-buffer (mh-show-buffer)
964 (let ((criterion
965 (cond (part-index
966 (lambda (p)
967 (let ((part (get-text-property p 'mh-part)))
968 (and (integerp part) (= part part-index)))))
969 (t (lambda (p)
970 (if include-security-flag
971 (get-text-property p 'mh-data)
972 (integerp (get-text-property p 'mh-part)))))))
973 (point (point)))
974 (cond ((and (get-text-property point 'mh-part)
975 (or (null part-index)
976 (= (get-text-property point 'mh-part) part-index)))
977 (funcall action))
978 ((and (get-text-property point 'mh-data)
979 include-security-flag
980 (null part-index))
981 (funcall action))
982 (t
983 (mh-goto-next-button nil criterion)
984 (if (= (point) point)
985 (message "No matching MIME part found")
986 (funcall action)))))))
987
988 ;;;###mh-autoload
989 (defun mh-goto-next-button (backward-flag &optional criterion)
990 "Search for next button satisfying criterion.
991
992 If BACKWARD-FLAG is non-nil search backward in the buffer for a mime
993 button.
994 If CRITERION is a function or a symbol which has a function binding
995 then that function must return non-nil at the button we stop."
996 (unless (or (and (symbolp criterion) (fboundp criterion))
997 (functionp criterion))
998 (setq criterion (lambda (x) t)))
999 ;; Move to the next button in the buffer satisfying criterion
1000 (goto-char (or (save-excursion
1001 (beginning-of-line)
1002 ;; Find point before current button
1003 (let ((point-before-current-button
1004 (save-excursion
1005 (while (get-text-property (point) 'mh-data)
1006 (unless (= (forward-line
1007 (if backward-flag 1 -1))
1008 0)
1009 (if backward-flag
1010 (goto-char (point-min))
1011 (goto-char (point-max)))))
1012 (point))))
1013 ;; Skip over current button
1014 (while (and (get-text-property (point) 'mh-data)
1015 (not (if backward-flag (bobp) (eobp))))
1016 (forward-line (if backward-flag -1 1)))
1017 ;; Stop at next MIME button if any exists.
1018 (block loop
1019 (while (/= (progn
1020 (unless (= (forward-line
1021 (if backward-flag -1 1))
1022 0)
1023 (if backward-flag
1024 (goto-char (point-max))
1025 (goto-char (point-min)))
1026 (beginning-of-line))
1027 (point))
1028 point-before-current-button)
1029 (when (and (get-text-property (point) 'mh-data)
1030 (funcall criterion (point)))
1031 (return-from loop (point))))
1032 nil)))
1033 (point))))
1034
1035 (defun mh-widget-press-button (widget el)
1036 "Callback for widget, WIDGET.
1037 Parameter EL is unused."
1038 (goto-char (widget-get widget :from))
1039 (mh-press-button))
1040
1041 (defun mh-press-button ()
1042 "View contents of button.
1043
1044 This command is a toggle so if you use it again on the same
1045 attachment, the attachment is hidden."
1046 (interactive)
1047 (let ((mm-inline-media-tests mh-mm-inline-media-tests)
1048 (data (get-text-property (point) 'mh-data))
1049 (function (get-text-property (point) 'mh-callback))
1050 (buffer-read-only nil)
1051 (folder mh-show-folder-buffer))
1052 (mh-cl-flet
1053 ((mm-handle-set-external-undisplayer
1054 (handle function)
1055 (mh-handle-set-external-undisplayer folder handle function)))
1056 (when (and function (eolp))
1057 (backward-char))
1058 (unwind-protect (and function (funcall function data))
1059 (set-buffer-modified-p nil)))))
1060
1061 (defun mh-push-button (event)
1062 "Click MIME button for EVENT.
1063
1064 If the MIME part is visible then it is removed. Otherwise the
1065 part is displayed. This function is called when the mouse is used
1066 to click the MIME button."
1067 (interactive "e")
1068 (mh-do-at-event-location event
1069 (let ((folder mh-show-folder-buffer)
1070 (mm-inline-media-tests mh-mm-inline-media-tests)
1071 (data (get-text-property (point) 'mh-data))
1072 (function (get-text-property (point) 'mh-callback)))
1073 (mh-cl-flet
1074 ((mm-handle-set-external-undisplayer
1075 (handle func)
1076 (mh-handle-set-external-undisplayer folder handle func)))
1077 (and function (funcall function data))))))
1078
1079 (defun mh-handle-set-external-undisplayer (folder handle function)
1080 "Replacement for `mm-handle-set-external-undisplayer'.
1081
1082 This is only called in recent versions of Gnus. The MIME handles
1083 are stored in data structures corresponding to MH-E folder buffer
1084 FOLDER instead of in Gnus (as in the original). The MIME part,
1085 HANDLE is associated with the undisplayer FUNCTION."
1086 (if (mh-mm-keep-viewer-alive-p handle)
1087 (let ((new-handle (copy-sequence handle)))
1088 (mm-handle-set-undisplayer new-handle function)
1089 (mm-handle-set-undisplayer handle nil)
1090 (with-current-buffer folder
1091 (push new-handle (mh-mime-handles (mh-buffer-data)))))
1092 (mm-handle-set-undisplayer handle function)))
1093
1094 (defun mh-mime-security-press-button (handle)
1095 "Callback from security button for part HANDLE."
1096 (if (mh-mm-handle-multipart-ctl-parameter handle 'gnus-info)
1097 (mh-mime-security-show-details handle)
1098 (let ((region (mh-mm-handle-multipart-ctl-parameter handle 'mh-region))
1099 point)
1100 (setq point (point))
1101 (goto-char (car region))
1102 (delete-region (car region) (cdr region))
1103 (with-current-buffer (mh-mm-handle-multipart-ctl-parameter handle 'buffer)
1104 (let* ((mm-verify-option 'known)
1105 (mm-decrypt-option 'known)
1106 (new (mh-mm-possibly-verify-or-decrypt (cdr handle) handle)))
1107 (unless (eq new (cdr handle))
1108 (mh-mm-destroy-parts (cdr handle))
1109 (setcdr handle new))))
1110 (mh-mime-display-security handle)
1111 (goto-char point))))
1112
1113 ;; I rewrote the security part because Gnus doesn't seem to ever minimize
1114 ;; the button. That is once the mime-security button is pressed there seems
1115 ;; to be no way of getting rid of the inserted text.
1116 (defun mh-mime-security-show-details (handle)
1117 "Toggle display of detailed security info for HANDLE."
1118 (let ((details (mh-mm-handle-multipart-ctl-parameter handle 'gnus-details)))
1119 (when details
1120 (let ((mh-mime-security-button-pressed
1121 (not (get-text-property (point) 'mh-button-pressed)))
1122 (mh-mime-security-button-line-format
1123 (get-text-property (point) 'mh-line-format)))
1124 (forward-char -1)
1125 (while (eq (get-text-property (point) 'mh-line-format)
1126 mh-mime-security-button-line-format)
1127 (forward-char -1))
1128 (forward-char)
1129 (save-restriction
1130 (narrow-to-region (point) (point))
1131 (mh-insert-mime-security-button handle))
1132 (delete-region
1133 (point)
1134 (or (text-property-not-all
1135 (point) (point-max)
1136 'mh-line-format mh-mime-security-button-line-format)
1137 (point-max)))
1138 (forward-line -1)))))
1139
1140 \f
1141
1142 ;;; Miscellaneous Article Washing
1143
1144 ;;;###mh-autoload
1145 (defun mh-add-missing-mime-version-header ()
1146 "Some mail programs don't put a MIME-Version header.
1147 I have seen this only in spam, so maybe we shouldn't fix
1148 this ;-)"
1149 (save-excursion
1150 (goto-char (point-min))
1151 (re-search-forward "\n\n" nil t)
1152 (save-restriction
1153 (narrow-to-region (point-min) (point))
1154 (when (and (message-fetch-field "content-type")
1155 (not (message-fetch-field "mime-version")))
1156 (goto-char (point-min))
1157 (insert "MIME-Version: 1.0\n")))))
1158
1159 ;;;###mh-autoload
1160 (defun mh-display-smileys ()
1161 "Display smileys."
1162 (when (and mh-graphical-smileys-flag (mh-small-show-buffer-p))
1163 (mh-funcall-if-exists smiley-region (point-min) (point-max))))
1164
1165 ;;;###mh-autoload
1166 (defun mh-display-emphasis ()
1167 "Display graphical emphasis."
1168 (when (and mh-graphical-emphasis-flag (mh-small-show-buffer-p))
1169 (mh-cl-flet
1170 ((article-goto-body ())) ; shadow this function to do nothing
1171 (save-excursion
1172 (goto-char (point-min))
1173 (article-emphasize)))))
1174
1175 (defun mh-small-show-buffer-p ()
1176 "Check if show buffer is small.
1177 This is used to decide if smileys and graphical emphasis should be
1178 displayed."
1179 (let ((max nil))
1180 (when (and (boundp 'font-lock-maximum-size) font-lock-maximum-size)
1181 (cond ((numberp font-lock-maximum-size)
1182 (setq max font-lock-maximum-size))
1183 ((listp font-lock-maximum-size)
1184 (setq max (cdr (or (assoc 'mh-show-mode font-lock-maximum-size)
1185 (assoc t font-lock-maximum-size)))))))
1186 (or (not (numberp max)) (>= (/ max 8) (buffer-size)))))
1187
1188 \f
1189
1190 ;;; MH-Letter Commands
1191
1192 ;; MH-E commands are alphabetical; specific support routines follow command.
1193
1194 ;;;###mh-autoload
1195 (defun mh-compose-forward (&optional description folder range)
1196 "Add tag to forward a message.
1197
1198 You are prompted for a content DESCRIPTION, the name of the
1199 FOLDER in which the messages to forward are located, and a RANGE
1200 of messages, which defaults to the current message in that
1201 folder. Check the documentation of `mh-interactive-range' to see
1202 how RANGE is read in interactive use.
1203
1204 The option `mh-compose-insertion' controls what type of tags are inserted."
1205 (interactive
1206 (let* ((description
1207 (mml-minibuffer-read-description))
1208 (folder
1209 (mh-prompt-for-folder "Message from"
1210 mh-sent-from-folder nil))
1211 (default
1212 (if (and (equal folder mh-sent-from-folder)
1213 (numberp mh-sent-from-msg))
1214 mh-sent-from-msg
1215 (nth 0 (mh-translate-range folder "cur"))))
1216 (range
1217 (mh-read-range "Forward" folder
1218 (or (and default
1219 (number-to-string default))
1220 t)
1221 t t)))
1222 (list description folder range)))
1223 (let ((messages (mapconcat 'identity (mh-list-to-string range) " ")))
1224 (dolist (message (mh-translate-range folder messages))
1225 (if (equal mh-compose-insertion 'mml)
1226 (mh-mml-forward-message description folder (format "%s" message))
1227 (mh-mh-forward-message description folder (format "%s" message))))))
1228
1229 ;;;###mh-autoload
1230 (defun mh-mml-forward-message (description folder message)
1231 "Forward a message as attachment.
1232
1233 The function will prompt the user for a DESCRIPTION, a FOLDER and
1234 MESSAGE number."
1235 (let ((msg (if (and (equal message "") (numberp mh-sent-from-msg))
1236 mh-sent-from-msg
1237 (string-to-number message))))
1238 (cond ((integerp msg)
1239 (mml-attach-file (format "%s%s/%d"
1240 mh-user-path (substring folder 1) msg)
1241 "message/rfc822"
1242 (if (string= "" description) nil description)
1243 "inline"))
1244 (t (error "The message number, %s, is not a integer" msg)))))
1245
1246 (defun mh-mh-forward-message (&optional description folder messages)
1247 "Add tag to forward a message.
1248 You are prompted for a content DESCRIPTION, the name of the
1249 FOLDER in which the messages to forward are located, and the
1250 MESSAGES' numbers.
1251
1252 See also \\[mh-mh-to-mime]."
1253 (interactive (list
1254 (mml-minibuffer-read-description)
1255 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
1256 (read-string (concat "Messages"
1257 (if (numberp mh-sent-from-msg)
1258 (format " (default %d): "
1259 mh-sent-from-msg)
1260 ": ")))))
1261 (beginning-of-line)
1262 (insert "#forw [")
1263 (and description
1264 (not (string= description ""))
1265 (insert description))
1266 (insert "]")
1267 (and folder
1268 (not (string= folder ""))
1269 (insert " " folder))
1270 (if (and messages
1271 (not (string= messages "")))
1272 (let ((start (point)))
1273 (insert " " messages)
1274 (subst-char-in-region start (point) ?, ? ))
1275 (if (numberp mh-sent-from-msg)
1276 (insert " " (int-to-string mh-sent-from-msg))))
1277 (insert "\n"))
1278
1279 ;;;###mh-autoload
1280 (defun mh-compose-insertion (&optional inline)
1281 "Add tag to include a file such as an image or sound.
1282
1283 You are prompted for the filename containing the object, the
1284 media type if it cannot be determined automatically, and a
1285 content description. If you're using MH-style directives, you
1286 will also be prompted for additional attributes.
1287
1288 The option `mh-compose-insertion' controls what type of tags are
1289 inserted. Optional argument INLINE means make it an inline
1290 attachment."
1291 (interactive "P")
1292 (if (equal mh-compose-insertion 'mml)
1293 (if inline
1294 (mh-mml-attach-file "inline")
1295 (mh-mml-attach-file))
1296 (call-interactively 'mh-mh-attach-file)))
1297
1298 (defun mh-mml-attach-file (&optional disposition)
1299 "Add a tag to insert a MIME message part from a file.
1300
1301 You are prompted for the filename containing the object, the
1302 media type if it cannot be determined automatically, a content
1303 description and the DISPOSITION of the attachment.
1304
1305 This is basically `mml-attach-file' from Gnus, modified such that a prefix
1306 argument yields an \"inline\" disposition and Content-Type is determined
1307 automatically."
1308 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1309 (type (mh-minibuffer-read-type file))
1310 (description (mml-minibuffer-read-description))
1311 (dispos (or disposition
1312 (mh-mml-minibuffer-read-disposition type))))
1313 (mml-insert-empty-tag 'part 'type type 'filename file
1314 'disposition dispos 'description description)))
1315
1316 (defun mh-mh-attach-file (filename type description attributes)
1317 "Add a tag to insert a MIME message part from a file.
1318 You are prompted for the FILENAME containing the object, the
1319 media TYPE if it cannot be determined automatically, and a
1320 content DESCRIPTION. In addition, you are also prompted for
1321 additional ATTRIBUTES.
1322
1323 See also \\[mh-mh-to-mime]."
1324 (interactive (let ((filename (mml-minibuffer-read-file "Attach file: ")))
1325 (list
1326 filename
1327 (mh-minibuffer-read-type filename)
1328 (mml-minibuffer-read-description)
1329 (read-string "Attributes: "
1330 (concat "name=\""
1331 (file-name-nondirectory filename)
1332 "\"")))))
1333 (mh-mh-compose-type filename type description attributes))
1334
1335 (defun mh-mh-compose-type (filename type
1336 &optional description attributes comment)
1337 "Insert an MH-style directive to insert a file.
1338 The file specified by FILENAME is encoded as TYPE. An optional
1339 DESCRIPTION is used as the Content-Description field, optional
1340 set of ATTRIBUTES and an optional COMMENT can also be included."
1341 (beginning-of-line)
1342 (insert "#" type)
1343 (and attributes
1344 (insert "; " attributes))
1345 (and comment
1346 (insert " (" comment ")"))
1347 (insert " [")
1348 (and description
1349 (insert description))
1350 (insert "] " (expand-file-name filename))
1351 (insert "\n"))
1352
1353 ;;;###mh-autoload
1354 (defun mh-mh-compose-anon-ftp (host filename type description)
1355 "Add tag to include anonymous ftp reference to a file.
1356
1357 You can have your message initiate an \"ftp\" transfer when the
1358 recipient reads the message. You are prompted for the remote HOST
1359 and FILENAME, the media TYPE, and the content DESCRIPTION.
1360
1361 See also \\[mh-mh-to-mime]."
1362 (interactive (list
1363 (read-string "Remote host: ")
1364 (read-string "Remote filename: ")
1365 (mh-minibuffer-read-type "DUMMY-FILENAME")
1366 (mml-minibuffer-read-description)))
1367 (mh-mh-compose-external-type "anon-ftp" host filename
1368 type description))
1369
1370 ;;;###mh-autoload
1371 (defun mh-mh-compose-external-compressed-tar (host filename description)
1372 "Add tag to include anonymous ftp reference to a compressed tar file.
1373
1374 In addition to retrieving the file via anonymous \"ftp\" as per
1375 the command \\[mh-mh-compose-anon-ftp], the file will also be
1376 uncompressed and untarred. You are prompted for the remote HOST
1377 and FILENAME and the content DESCRIPTION.
1378
1379 See also \\[mh-mh-to-mime]."
1380 (interactive (list
1381 (read-string "Remote host: ")
1382 (read-string "Remote filename: ")
1383 (mml-minibuffer-read-description)))
1384 (mh-mh-compose-external-type "anon-ftp" host filename
1385 "application/octet-stream"
1386 description
1387 "type=tar; conversions=x-compress"
1388 "mode=image"))
1389
1390 ;; RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One:
1391 ;; Format of Internet Message Bodies.
1392 ;; RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two:
1393 ;; Media Types.
1394 ;; RFC 2049 - Multipurpose Internet Mail Extensions (MIME) Part Five:
1395 ;; Conformance Criteria and Examples.
1396 ;; RFC 2017 - Definition of the URL MIME External-Body Access-Type
1397 ;; RFC 1738 - Uniform Resource Locators (URL)
1398 (defvar mh-access-types
1399 '(("anon-ftp") ; RFC2046 Anonymous File Transfer Protocol
1400 ("file") ; RFC1738 Host-specific file names
1401 ("ftp") ; RFC2046 File Transfer Protocol
1402 ("gopher") ; RFC1738 The Gopher Protocol
1403 ("http") ; RFC1738 Hypertext Transfer Protocol
1404 ("local-file") ; RFC2046 Local file access
1405 ("mail-server") ; RFC2046 mail-server Electronic mail address
1406 ("mailto") ; RFC1738 Electronic mail address
1407 ("news") ; RFC1738 Usenet news
1408 ("nntp") ; RFC1738 Usenet news using NNTP access
1409 ("prospero") ; RFC1738 Prospero Directory Service
1410 ("telnet") ; RFC1738 Telnet
1411 ("tftp") ; RFC2046 Trivial File Transfer Protocol
1412 ("url") ; RFC2017 URL scheme MIME access-type Protocol
1413 ("wais")) ; RFC1738 Wide Area Information Servers
1414 "Valid MIME access-type values.")
1415
1416 ;;;###mh-autoload
1417 (defun mh-mh-compose-external-type (access-type host filename type
1418 &optional description
1419 attributes parameters
1420 comment)
1421 "Add tag to refer to a remote file.
1422
1423 This command is a general utility for referencing external files.
1424 In fact, all of the other commands that insert directives to
1425 access external files call this command. You are prompted for the
1426 ACCESS-TYPE, remote HOST and FILENAME, and content TYPE. If you
1427 provide a prefix argument, you are also prompted for a content
1428 DESCRIPTION, ATTRIBUTES, PARAMETERS, and a COMMENT.
1429
1430 See also \\[mh-mh-to-mime]."
1431 (interactive (list
1432 (completing-read "Access type: " mh-access-types)
1433 (read-string "Remote host: ")
1434 (read-string "Remote filename: ")
1435 (mh-minibuffer-read-type "DUMMY-FILENAME")
1436 (if current-prefix-arg (mml-minibuffer-read-description))
1437 (if current-prefix-arg (read-string "Attributes: "))
1438 (if current-prefix-arg (read-string "Parameters: "))
1439 (if current-prefix-arg (read-string "Comment: "))))
1440 (beginning-of-line)
1441 (insert "#@" type)
1442 (and attributes
1443 (insert "; " attributes))
1444 (and comment
1445 (insert " (" comment ") "))
1446 (insert " [")
1447 (and description
1448 (insert description))
1449 (insert "] ")
1450 (insert "access-type=" access-type "; ")
1451 (insert "site=" host)
1452 (insert "; name=" (file-name-nondirectory filename))
1453 (let ((directory (file-name-directory filename)))
1454 (and directory
1455 (insert "; directory=\"" directory "\"")))
1456 (and parameters
1457 (insert "; " parameters))
1458 (insert "\n"))
1459
1460 (defvar mh-mh-to-mime-args nil
1461 "Extra arguments for \\[mh-mh-to-mime] to pass to the \"mhbuild\" command.
1462 The arguments are passed to \"mhbuild\" if \\[mh-mh-to-mime] is
1463 given a prefix argument. Normally default arguments to
1464 \"mhbuild\" are specified in the MH profile.")
1465
1466 ;;;###mh-autoload
1467 (defun mh-mh-to-mime (&optional extra-args)
1468 "Compose MIME message from MH-style directives.
1469
1470 Typically, you send a message with attachments just like any other
1471 message. However, you may take a sneak preview of the MIME encoding if
1472 you wish by running this command.
1473
1474 If you wish to pass additional arguments to \"mhbuild\" (\"mhn\")
1475 to affect how it builds your message, use the option
1476 `mh-mh-to-mime-args'. For example, you can build a consistency
1477 check into the message by setting `mh-mh-to-mime-args' to
1478 \"-check\". The recipient of your message can then run \"mhbuild
1479 -check\" on the message--\"mhbuild\" (\"mhn\") will complain if
1480 the message has been corrupted on the way. This command only
1481 consults this option when given a prefix argument EXTRA-ARGS.
1482
1483 The hook `mh-mh-to-mime-hook' is called after the message has been
1484 formatted.
1485
1486 The effects of this command can be undone by running
1487 \\[mh-mh-to-mime-undo]."
1488 (interactive "*P")
1489 (mh-mh-quote-unescaped-sharp)
1490 (save-buffer)
1491 (message "Running %s..." (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
1492 (cond
1493 ((mh-variant-p 'nmh)
1494 (mh-exec-cmd-error nil
1495 "mhbuild"
1496 (if extra-args mh-mh-to-mime-args)
1497 buffer-file-name))
1498 (t
1499 (mh-exec-cmd-error (format "mhdraft=%s" buffer-file-name)
1500 "mhn"
1501 (if extra-args mh-mh-to-mime-args)
1502 buffer-file-name)))
1503 (revert-buffer t t t)
1504 (message "Running %s...done" (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
1505 (run-hooks 'mh-mh-to-mime-hook))
1506
1507 (defun mh-mh-quote-unescaped-sharp ()
1508 "Quote \"#\" characters that haven't been quoted for \"mhbuild\".
1509 If the \"#\" character is present in the first column, but it isn't
1510 part of a MH-style directive then \"mhbuild\" gives an error.
1511 This function will quote all such characters."
1512 (save-excursion
1513 (goto-char (point-min))
1514 (while (re-search-forward "^#" nil t)
1515 (beginning-of-line)
1516 (unless (mh-mh-directive-present-p (point) (mh-line-end-position))
1517 (insert "#"))
1518 (goto-char (mh-line-end-position)))))
1519
1520 ;;;###mh-autoload
1521 (defun mh-mh-to-mime-undo (noconfirm)
1522 "Undo effects of \\[mh-mh-to-mime].
1523
1524 It does this by reverting to a backup file. You are prompted to
1525 confirm this action, but you can avoid the confirmation by adding
1526 a prefix argument NOCONFIRM."
1527 (interactive "*P")
1528 (if (null buffer-file-name)
1529 (error "Buffer does not seem to be associated with any file"))
1530 (let ((backup-strings '("," "#"))
1531 backup-file)
1532 (while (and backup-strings
1533 (not (file-exists-p
1534 (setq backup-file
1535 (concat (file-name-directory buffer-file-name)
1536 (car backup-strings)
1537 (file-name-nondirectory buffer-file-name)
1538 ".orig")))))
1539 (setq backup-strings (cdr backup-strings)))
1540 (or backup-strings
1541 (error "Backup file for %s no longer exists" buffer-file-name))
1542 (or noconfirm
1543 (yes-or-no-p (format "Revert buffer from file %s? "
1544 backup-file))
1545 (error "Revert not confirmed"))
1546 (let ((buffer-read-only nil))
1547 (erase-buffer)
1548 (insert-file-contents backup-file))
1549 (after-find-file nil nil nil nil t)))
1550
1551 ;; Shush compiler.
1552 (defvar mh-identity-pgg-default-user-id)
1553
1554 ;;;###mh-autoload
1555 (defun mh-mml-secure-message-encrypt (method)
1556 "Add tag to encrypt the message.
1557
1558 A proper multipart message is created for you when you send the
1559 message. Use the command \\[mh-mml-unsecure-message] to remove
1560 this tag. Use a prefix argument METHOD to be prompted for one of
1561 the possible security methods (see `mh-mml-method-default')."
1562 (interactive (list (mh-mml-query-cryptographic-method)))
1563 (mh-secure-message method "encrypt" mh-identity-pgg-default-user-id))
1564
1565 ;;;###mh-autoload
1566 (defun mh-mml-secure-message-sign (method)
1567 "Add tag to sign the message.
1568
1569 A proper multipart message is created for you when you send the
1570 message. Use the command \\[mh-mml-unsecure-message] to remove
1571 this tag. Use a prefix argument METHOD to be prompted for one of
1572 the possible security methods (see `mh-mml-method-default')."
1573 (interactive (list (mh-mml-query-cryptographic-method)))
1574 (mh-secure-message method "sign" mh-identity-pgg-default-user-id))
1575
1576 ;;;###mh-autoload
1577 (defun mh-mml-secure-message-signencrypt (method)
1578 "Add tag to encrypt and sign the message.
1579
1580 A proper multipart message is created for you when you send the
1581 message. Use the command \\[mh-mml-unsecure-message] to remove
1582 this tag. Use a prefix argument METHOD to be prompted for one of
1583 the possible security methods (see `mh-mml-method-default')."
1584 (interactive (list (mh-mml-query-cryptographic-method)))
1585 (mh-secure-message method "signencrypt" mh-identity-pgg-default-user-id))
1586
1587 (defvar mh-mml-cryptographic-method-history ())
1588
1589 (defun mh-mml-query-cryptographic-method ()
1590 "Read the cryptographic method to use."
1591 (if current-prefix-arg
1592 (let ((def (or (car mh-mml-cryptographic-method-history)
1593 mh-mml-method-default)))
1594 (completing-read (format "Method (default %s): " def)
1595 '(("pgp") ("pgpmime") ("smime"))
1596 nil t nil 'mh-mml-cryptographic-method-history def))
1597 mh-mml-method-default))
1598
1599 (defun mh-secure-message (method mode &optional identity)
1600 "Add tag to encrypt or sign message.
1601
1602 METHOD should be one of: \"pgpmime\", \"pgp\", \"smime\".
1603 MODE should be one of: \"sign\", \"encrypt\", \"signencrypt\", \"none\".
1604 IDENTITY is optionally the default-user-id to use."
1605 (if (not mh-pgp-support-flag)
1606 (error "Your version of Gnus does not support PGP/GPG")
1607 ;; Check the arguments
1608 (let ((valid-methods (list "pgpmime" "pgp" "smime"))
1609 (valid-modes (list "sign" "encrypt" "signencrypt" "none")))
1610 (if (not (member method valid-methods))
1611 (error "Method %s is invalid" method))
1612 (if (not (member mode valid-modes))
1613 (error "Mode %s is invalid" mode))
1614 (mml-unsecure-message)
1615 (if (not (string= mode "none"))
1616 (save-excursion
1617 (goto-char (point-min))
1618 (mh-goto-header-end 1)
1619 (if mh-identity-pgg-default-user-id
1620 (mml-insert-tag 'secure 'method method 'mode mode
1621 'sender mh-identity-pgg-default-user-id)
1622 (mml-insert-tag 'secure 'method method 'mode mode)))))))
1623
1624 ;;;###mh-autoload
1625 (defun mh-mml-to-mime ()
1626 "Compose MIME message from MML tags.
1627
1628 Typically, you send a message with attachments just like any
1629 other message. However, you may take a sneak preview of the MIME
1630 encoding if you wish by running this command.
1631
1632 This action can be undone by running \\[undo]."
1633 (interactive)
1634 (require 'message)
1635 (when mh-pgp-support-flag
1636 ;; PGP requires actual e-mail addresses, not aliases.
1637 ;; Parse the recipients and sender from the message.
1638 (message-options-set-recipient)
1639 ;; Do an alias lookup on sender (if From field is present).
1640 (when (message-options-get 'message-sender)
1641 (message-options-set 'message-sender
1642 (mail-strip-quoted-names
1643 (mh-alias-expand
1644 (message-options-get 'message-sender)))))
1645 ;; Do an alias lookup on recipients
1646 (message-options-set 'message-recipients
1647 (mapconcat
1648 (lambda (ali)
1649 (mail-strip-quoted-names (mh-alias-expand ali)))
1650 (split-string (message-options-get 'message-recipients) "[, ]+")
1651 ", ")))
1652 (let ((saved-text (buffer-string))
1653 (buffer (current-buffer))
1654 (modified-flag (buffer-modified-p)))
1655 (condition-case err (mml-to-mime)
1656 (error
1657 (with-current-buffer buffer
1658 (delete-region (point-min) (point-max))
1659 (insert saved-text)
1660 (set-buffer-modified-p modified-flag))
1661 (error (error-message-string err))))))
1662
1663 ;;;###mh-autoload
1664 (defun mh-mml-unsecure-message ()
1665 "Remove any secure message tags."
1666 (interactive)
1667 (if (not mh-pgp-support-flag)
1668 (error "Your version of Gnus does not support PGP/GPG")
1669 (mml-unsecure-message)))
1670
1671 \f
1672
1673 ;;; Support Routines for MH-Letter Commands
1674
1675 ;;;###mh-autoload
1676 (defun mh-mml-tag-present-p ()
1677 "Check if the current buffer has text which may be a MML tag."
1678 (save-excursion
1679 (goto-char (point-min))
1680 (re-search-forward
1681 (concat
1682 "\\(<#\\(mml\\|part\\)\\(.\\|\n\\)*>[ \n\t]*<#/\\(mml\\|part\\)>\\|"
1683 "^<#secure.+>$\\)")
1684 nil t)))
1685
1686 (defvar mh-media-type-regexp
1687 (concat (regexp-opt '("text" "image" "audio" "video" "application"
1688 "multipart" "message") t)
1689 "/[-.+a-zA-Z0-9]+")
1690 "Regexp matching valid media types used in MIME attachment compositions.")
1691
1692 ;;;###mh-autoload
1693 (defun mh-mh-directive-present-p (&optional begin end)
1694 "Check if the text between BEGIN and END might be a MH-style directive.
1695 The optional argument BEGIN defaults to the beginning of the
1696 buffer, while END defaults to the end of the buffer."
1697 (unless begin (setq begin (point-min)))
1698 (unless end (setq end (point-max)))
1699 (save-excursion
1700 (block search-for-mh-directive
1701 (goto-char begin)
1702 (while (re-search-forward "^#" end t)
1703 (let ((s (buffer-substring-no-properties
1704 (point) (mh-line-end-position))))
1705 (cond ((equal s ""))
1706 ((string-match "^forw[ \t\n]+" s)
1707 (return-from search-for-mh-directive t))
1708 (t (let ((first-token (car (split-string s "[ \t;@]"))))
1709 (when (and first-token
1710 (string-match mh-media-type-regexp
1711 first-token))
1712 (return-from search-for-mh-directive t)))))))
1713 nil)))
1714
1715 (defun mh-minibuffer-read-type (filename &optional default)
1716 "Return the content type associated with the given FILENAME.
1717 If the \"file\" command exists and recognizes the given file,
1718 then its value is returned\; otherwise, the user is prompted for
1719 a type (see `mailcap-mime-types').
1720 Optional argument DEFAULT is returned if a type isn't entered."
1721 (mailcap-parse-mimetypes)
1722 (let* ((default (or default
1723 (mm-default-file-encoding filename)
1724 "application/octet-stream"))
1725 (probed-type (mh-file-mime-type filename))
1726 (type (or (and (not (equal probed-type "application/octet-stream"))
1727 probed-type)
1728 (completing-read
1729 (format "Content type (default %s): " default)
1730 (mapcar 'list (mailcap-mime-types))))))
1731 (if (not (equal type ""))
1732 type
1733 default)))
1734
1735 ;;;###mh-autoload
1736 (defun mh-file-mime-type (filename)
1737 "Return MIME type of FILENAME from file command.
1738 Returns nil if file command not on system."
1739 (cond
1740 ((not (mh-have-file-command))
1741 nil) ;no file command, exit now
1742 ((not (and (file-exists-p filename)
1743 (file-readable-p filename)))
1744 nil) ;no file or not readable, ditto
1745 (t
1746 (let ((tmp-buffer (get-buffer-create mh-temp-buffer)))
1747 (with-current-buffer tmp-buffer
1748 (unwind-protect
1749 (progn
1750 (call-process "file" nil '(t nil) nil "-b" "-i"
1751 (expand-file-name filename))
1752 (goto-char (point-min))
1753 (if (not (re-search-forward mh-media-type-regexp nil t))
1754 nil
1755 (mh-file-mime-type-substitute (match-string 0) filename)))
1756 (kill-buffer tmp-buffer)))))))
1757
1758 (defvar mh-file-mime-type-substitutions
1759 '(("application/msword" "\.xls" "application/ms-excel")
1760 ("application/msword" "\.ppt" "application/ms-powerpoint")
1761 ("text/plain" "\.vcf" "text/x-vcard")
1762 ("text/rtf" "\.rtf" "application/rtf")
1763 ("application/x-zip" "\.sxc" "application/vnd.sun.xml.calc")
1764 ("application/x-zip" "\.sxd" "application/vnd.sun.xml.draw")
1765 ("application/x-zip" "\.sxi" "application/vnd.sun.xml.impress")
1766 ("application/x-zip" "\.sxw" "application/vnd.sun.xml.writer")
1767 ("application/x-zip" "\.odg" "application/vnd.oasis.opendocument.graphics")
1768 ("application/x-zip" "\.odi" "application/vnd.oasis.opendocument.image")
1769 ("application/x-zip" "\.odp"
1770 "application/vnd.oasis.opendocument.presentation")
1771 ("application/x-zip" "\.ods"
1772 "application/vnd.oasis.opendocument.spreadsheet")
1773 ("application/x-zip" "\.odt" "application/vnd.oasis.opendocument.text"))
1774 "Substitutions to make for Content-Type returned from file command.
1775 The first element is the Content-Type returned by the file command.
1776 The second element is a regexp matching the file name, usually the
1777 extension.
1778 The third element is the Content-Type to replace with.")
1779
1780 (defun mh-file-mime-type-substitute (content-type filename)
1781 "Return possibly changed CONTENT-TYPE on the FILENAME.
1782 Substitutions are made from the `mh-file-mime-type-substitutions'
1783 variable."
1784 (let ((subst mh-file-mime-type-substitutions)
1785 (type) (match) (answer content-type)
1786 (case-fold-search t))
1787 (while subst
1788 (setq type (car (car subst))
1789 match (elt (car subst) 1))
1790 (if (and (string-equal content-type type)
1791 (string-match match filename))
1792 (setq answer (elt (car subst) 2)
1793 subst nil)
1794 (setq subst (cdr subst))))
1795 answer))
1796
1797 (defvar mh-have-file-command 'undefined
1798 "Cached value of function `mh-have-file-command'.
1799 Do not reference this variable directly as it might not have been
1800 initialized. Always use the command `mh-have-file-command'.")
1801
1802 ;;;###mh-autoload
1803 (defun mh-have-file-command ()
1804 "Return t if 'file' command is on the system.
1805 'file -i' is used to get MIME type of composition insertion."
1806 (when (eq mh-have-file-command 'undefined)
1807 (setq mh-have-file-command
1808 (and (fboundp 'executable-find)
1809 (executable-find "file") ; file command exists
1810 ; and accepts -i and -b args.
1811 (zerop (call-process "file" nil nil nil "-i" "-b"
1812 (expand-file-name "inc" mh-progs))))))
1813 mh-have-file-command)
1814
1815 \f
1816
1817 ;;; MIME Cleanup
1818
1819 ;;;###mh-autoload
1820 (defun mh-mime-cleanup ()
1821 "Free the decoded MIME parts."
1822 (let ((mime-data (gethash (current-buffer) mh-globals-hash)))
1823 ;; This is for Emacs, what about XEmacs?
1824 (mh-funcall-if-exists remove-images (point-min) (point-max))
1825 (when mime-data
1826 (mh-mm-destroy-parts (mh-mime-handles mime-data))
1827 (remhash (current-buffer) mh-globals-hash))))
1828
1829 ;;;###mh-autoload
1830 (defun mh-destroy-postponed-handles ()
1831 "Free MIME data for externally displayed MIME parts."
1832 (let ((mime-data (mh-buffer-data)))
1833 (when mime-data
1834 (mh-mm-destroy-parts (mh-mime-handles mime-data)))
1835 (remhash (current-buffer) mh-globals-hash)))
1836
1837 (provide 'mh-mime)
1838
1839 ;; Local Variables:
1840 ;; indent-tabs-mode: nil
1841 ;; sentence-end-double-space: nil
1842 ;; End:
1843
1844 ;;; mh-mime.el ends here