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