1 ;;; mh-mime.el --- MH-E MIME support
3 ;; Copyright (C) 1993, 1995,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; Message composition of MIME message is done with either MH-style
31 ;; directives for mhn or mhbuild (MH 6.8 or later) or MML (MIME Meta
35 ;; Paragraph code should not fill # lines if MIME enabled.
36 ;; Implement mh-auto-mh-to-mime (if non-nil, \\[mh-send-letter]
37 ;; invokes mh-mh-to-mime automatically before sending.)
38 ;; Actually, instead of mh-auto-mh-to-mime,
39 ;; should read automhnproc from profile.
40 ;; MIME option to mh-forward command to move to content-description
48 (require 'mh-gnus) ;needed because mh-gnus.el not compiled
57 (autoload 'article-emphasize "gnus-art")
58 (autoload 'gnus-eval-format "gnus-spec")
59 (autoload 'mail-content-type-get "mail-parse")
60 (autoload 'mail-decode-encoded-word-string "mail-parse")
61 (autoload 'mail-header-parse-content-type "mail-parse")
62 (autoload 'mail-header-strip "mail-parse")
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")
74 ;; This has to be a macro, since we do: (setf (mh-buffer-data) ...)
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))
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
86 (parts-count 0) ; The button number is generated from
88 (part-index-hash (make-hash-table))) ; Avoid incrementing the part number
91 (defvar mh-mm-inline-media-tests
95 (mm-valid-and-fit-image-p 'jpeg handle)))
99 (mm-valid-and-fit-image-p 'png handle)))
103 (mm-valid-and-fit-image-p 'gif handle)))
107 (mm-valid-and-fit-image-p 'tiff handle)) )
111 (mm-valid-and-fit-image-p 'xbm handle)))
115 (mm-valid-and-fit-image-p 'xbm handle)))
119 (mm-valid-and-fit-image-p 'xpm handle)))
123 (mm-valid-and-fit-image-p 'xpm handle)))
127 (mm-valid-and-fit-image-p 'bmp handle)))
128 ("image/x-portable-bitmap"
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
137 (locate-library "diff-mode")))
138 ("application/emacs-lisp" mm-display-elisp-inline identity)
139 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
141 ,(if (fboundp 'mm-inline-text-html) 'mm-inline-text-html 'mm-inline-text)
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))))
147 mh-mm-inline-text-vcard
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
158 (and (or (featurep 'nas-sound) (featurep 'native-sound))
159 (device-sound-enabled-p))))
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.")
180 (defvar mh-mime-save-parts-directory nil
181 "Default to use for `mh-mime-save-parts-default-directory'.
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))
194 (define-key map [mouse-2] 'mh-push-button))
196 (define-key map '(button2) 'mh-push-button))
197 (dolist (c mh-mime-button-commands)
198 (define-key map (cadr c) (car c)))
200 (defvar mh-mime-button-line-format-alist
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
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)
220 (define-key map [mouse-2] 'mh-push-button))
222 (define-key map '(button2) 'mh-push-button))
227 ;;; MH-Folder Commands
232 (defun mh-display-with-external-viewer (part-index)
233 "View attachment externally.
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
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
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\"."
254 (when (consp part-index) (setq part-index (car part-index)))
255 (mh-folder-mime-action
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)))
263 (prompt (format "Viewer%s: " (if def
264 (format " (default %s)" def)
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 (flet ((mm-handle-set-external-undisplayer (handle function)
272 (mh-handle-set-external-undisplayer folder handle function)))
273 (unwind-protect (mm-display-external part method)
274 (set-buffer-modified-p nil)))))
278 (defun mh-folder-inline-mime-part (part-index)
279 "Show attachment verbatim.
281 You can view the raw contents of an attachment with this command.
282 This command displays (or hides) the contents of the attachment
283 associated with the button under the cursor verbatim. If the
284 cursor is not located over a button, then the cursor first moves
285 to the next button, wrapping to the beginning of the message if
288 You can also provide a numeric prefix argument PART-INDEX to view
289 the attachment labeled with that number."
291 (when (consp part-index) (setq part-index (car part-index)))
292 (mh-folder-mime-action part-index #'mh-mime-inline-part nil))
294 (defun mh-mime-inline-part ()
295 "Toggle display of the raw MIME part."
297 (let* ((buffer-read-only nil)
298 (data (get-text-property (point) 'mh-data))
299 (inserted-flag (get-text-property (point) 'mh-mime-inserted))
300 (displayed-flag (mm-handle-displayed-p data))
303 (cond ((and data (not inserted-flag) (not displayed-flag))
304 (let ((contents (mm-get-part data)))
305 (add-text-properties (mh-line-beginning-position)
306 (mh-line-end-position) '(mh-mime-inserted t))
307 (setq start (point-marker))
309 (mm-insert-inline data contents)
310 (setq end (point-marker))
312 start (progn (goto-char start) (mh-line-end-position))
313 `(mh-region (,start . ,end)))))
314 ((and data (or inserted-flag displayed-flag))
316 (message "MIME part already inserted")))
318 (set-buffer-modified-p nil)))
321 (defun mh-folder-save-mime-part (part-index)
322 "Save (output) attachment.
324 This command saves the attachment associated with the button under the
325 cursor. If the cursor is not located over a button, then the cursor
326 first moves to the next button, wrapping to the beginning of the
327 message if necessary.
329 You can also provide a numeric prefix argument PART-INDEX to save the
330 attachment labeled with that number.
332 This command prompts you for a filename and suggests a specific name
335 (when (consp part-index) (setq part-index (car part-index)))
336 (mh-folder-mime-action part-index #'mh-mime-save-part nil))
338 (defun mh-mime-save-part ()
339 "Save MIME part at point."
341 (let ((data (get-text-property (point) 'mh-data)))
343 (let ((mm-default-directory
344 (file-name-as-directory (or mh-mime-save-parts-directory
345 default-directory))))
346 (mh-mm-save-part data)
347 (setq mh-mime-save-parts-directory mm-default-directory)))))
350 (defun mh-folder-toggle-mime-part (part-index)
353 This command displays (or hides) the attachment associated with
354 the button under the cursor. If the cursor is not located over a
355 button, then the cursor first moves to the next button, wrapping
356 to the beginning of the message if necessary. This command has
357 the advantage over related commands of working from the MH-Folder
360 You can also provide a numeric prefix argument PART-INDEX to view
361 the attachment labeled with that number. If Emacs does not know
362 how to display the attachment, then Emacs offers to save the
363 attachment in a file."
365 (when (consp part-index) (setq part-index (car part-index)))
366 (mh-folder-mime-action part-index #'mh-press-button t))
369 (defun mh-mime-save-parts (prompt)
372 You can save all of the attachments at once with this command.
373 The attachments are saved in the directory specified by the
374 option `mh-mime-save-parts-default-directory' unless you use a
375 prefix argument PROMPT in which case you are prompted for the
376 directory. These directories may be superseded by MH profile
377 components, since this function calls on \"mhstore\" (\"mhn\") to
380 (let ((msg (if (eq major-mode 'mh-show-mode)
381 (mh-show-buffer-message-number)
383 (folder (if (eq major-mode 'mh-show-mode)
384 mh-show-folder-buffer
386 (command (if (mh-variant-p 'nmh) "mhstore" "mhn"))
390 (equal nil mh-mime-save-parts-default-directory)
391 (equal t mh-mime-save-parts-default-directory))
392 (not mh-mime-save-parts-directory))
393 (read-file-name "Store in directory: " nil nil t nil))
395 (equal t mh-mime-save-parts-default-directory))
396 mh-mime-save-parts-directory)
397 (read-file-name (format
398 "Store in directory (default %s): "
399 mh-mime-save-parts-directory)
400 "" mh-mime-save-parts-directory t ""))
401 ((stringp mh-mime-save-parts-default-directory)
402 mh-mime-save-parts-default-directory)
404 mh-mime-save-parts-directory))))
405 (if (and (equal directory "") mh-mime-save-parts-directory)
406 (setq directory mh-mime-save-parts-directory))
407 (if (not (file-directory-p directory))
408 (message "No directory specified")
409 (if (equal nil mh-mime-save-parts-default-directory)
410 (setq mh-mime-save-parts-directory directory))
412 (set-buffer (get-buffer-create mh-log-buffer))
414 (setq mh-mime-save-parts-directory directory)
415 (let ((initial-size (mh-truncate-log-buffer)))
417 (expand-file-name command mh-progs) nil t nil
418 (mh-list-to-string (list folder msg "-auto")))
419 (if (> (buffer-size) initial-size)
420 (save-window-excursion
421 (switch-to-buffer-other-window mh-log-buffer)
425 (defun mh-toggle-mh-decode-mime-flag ()
426 "Toggle the value of `mh-decode-mime-flag'."
428 (setq mh-decode-mime-flag (not mh-decode-mime-flag))
430 (message "%s" (if mh-decode-mime-flag
431 "Processing attachments normally"
432 "Displaying raw message")))
435 (defun mh-toggle-mime-buttons ()
436 "Toggle option `mh-display-buttons-for-inline-parts-flag'."
438 (setq mh-display-buttons-for-inline-parts-flag
439 (not mh-display-buttons-for-inline-parts-flag))
444 ;;; MIME Display Routines
446 (defun mh-mm-inline-message (handle)
447 "Display message, HANDLE.
448 The function decodes the message and displays it. It avoids
449 decoding the same message multiple times."
451 (clean-message-header mh-clean-message-header-flag)
452 (invisible-headers mh-invisible-header-fields-compiled)
453 (visible-headers nil))
456 (narrow-to-region b b)
457 (mm-insert-part handle)
459 (or (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
460 (setf (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
461 (let ((handles (mm-dissect-buffer nil)))
463 (mh-mm-uu-dissect-text-parts handles)
464 (setq handles (mm-uu-dissect)))
465 (setf (mh-mime-handles (mh-buffer-data))
467 handles (mh-mime-handles (mh-buffer-data))))
470 (goto-char (point-min))
472 (cond (clean-message-header
473 (mh-clean-msg-header (point-min)
476 (goto-char (point-min)))
478 (mh-start-of-uncleaned-message)))
479 (mh-decode-message-header)
481 ;; The other highlighting types don't need anything special
482 (when (eq mh-highlight-citation-style 'gnus)
483 (mh-gnus-article-highlight-citation))
484 (goto-char (point-min))
485 (insert "\n------- Forwarded Message\n\n")
487 (mh-display-emphasis)
488 (mm-handle-set-undisplayer
491 (let (buffer-read-only)
492 (if (fboundp 'remove-specifier)
493 ;; This is only valid on XEmacs.
494 (mapcar (lambda (prop)
496 (face-property 'default prop) (current-buffer)))
497 '(background background-pixmap foreground)))
498 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
501 (defun mh-decode-message-header ()
502 "Decode RFC2047 encoded message header fields."
503 (when mh-decode-mime-flag
504 (let ((buffer-read-only nil))
505 (rfc2047-decode-region (point-min) (mh-mail-header-end)))))
508 (defun mh-mime-display (&optional pre-dissected-handles)
509 "Display (and possibly decode) MIME handles.
510 Optional argument, PRE-DISSECTED-HANDLES is a list of MIME
511 handles. If present they are displayed otherwise the buffer is
512 parsed and then displayed."
514 (folder mh-show-folder-buffer)
515 (raw-message-data (buffer-string)))
516 (flet ((mm-handle-set-external-undisplayer
518 (mh-handle-set-external-undisplayer folder handle function)))
519 (goto-char (point-min))
520 (unless (search-forward "\n\n" nil t)
521 (goto-char (point-max))
526 ;; If needed dissect the current buffer
527 (if pre-dissected-handles
528 (setq handles pre-dissected-handles)
529 (if (setq handles (mm-dissect-buffer nil))
530 (mh-mm-uu-dissect-text-parts handles)
531 (setq handles (mm-uu-dissect)))
532 (setf (mh-mime-handles (mh-buffer-data))
533 (mh-mm-merge-handles handles
534 (mh-mime-handles (mh-buffer-data))))
536 (mh-decode-message-body)))
539 (or (not (stringp (car handles)))
541 ;; Go to start of message body
542 (goto-char (point-min))
543 (or (search-forward "\n\n" nil t)
544 (goto-char (point-max)))
547 (delete-region (point) (point-max))
549 ;; Display the MIME handles
550 (mh-mime-display-part handles))
552 (mh-signature-highlight))))
554 (message "Could not display body: %s" (error-message-string err))
555 (delete-region (point-min) (point-max))
556 (insert raw-message-data))))))
558 (defun mh-decode-message-body ()
559 "Decode message based on charset.
560 If message has been encoded for transfer take that into account."
561 (let (ct charset cte)
562 (goto-char (point-min))
563 (re-search-forward "\n\n" nil t)
565 (narrow-to-region (point-min) (point))
566 (setq ct (ignore-errors (mail-header-parse-content-type
567 (message-fetch-field "Content-Type" t)))
568 charset (mail-content-type-get ct 'charset)
569 cte (message-fetch-field "Content-Transfer-Encoding")))
570 (when (stringp cte) (setq cte (mail-header-strip cte)))
571 (when (or (not ct) (equal (car ct) "text/plain"))
573 (narrow-to-region (min (1+ (mh-mail-header-end)) (point-max))
575 (mm-decode-body charset
576 (and cte (intern (downcase
577 (gnus-strip-whitespace cte))))
580 (defun mh-mime-display-part (handle)
581 "Decides the viewer to call based on the type of HANDLE."
584 ((not (stringp (car handle)))
585 (mh-mime-display-single handle))
586 ((equal (car handle) "multipart/alternative")
587 (mh-mime-display-alternative (cdr handle)))
588 ((and mh-pgp-support-flag
589 (or (equal (car handle) "multipart/signed")
590 (equal (car handle) "multipart/encrypted")))
591 (mh-mime-display-security handle))
593 (mh-mime-display-mixed (cdr handle)))))
595 (defun mh-mime-display-mixed (handles)
596 "Display the list of MIME parts, HANDLES recursively."
597 (mapcar #'mh-mime-display-part handles))
599 (defun mh-mime-display-alternative (handles)
600 "Choose among the alternatives, HANDLES the part that will be displayed.
601 If no part is preferred then all the parts are displayed."
602 (let* ((preferred (mm-preferred-alternative handles))
603 (others (loop for x in handles unless (eq x preferred) collect x)))
604 (cond ((and preferred
605 (stringp (car preferred)))
606 (mh-mime-display-part preferred)
607 (mh-mime-maybe-display-alternatives others))
610 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
611 (mh-mime-display-single preferred)
612 (mh-mime-maybe-display-alternatives others)
613 (goto-char (point-max))))
615 (mh-mime-display-mixed handles)))))
617 (defun mh-mime-maybe-display-alternatives (alternatives)
618 "Show buttons for ALTERNATIVES.
619 If `mh-mime-display-alternatives-flag' is non-nil then display
620 buttons for alternative parts that are usually suppressed."
621 (when (and mh-display-buttons-for-alternatives-flag alternatives)
622 (insert "\n----------------------------------------------------\n")
623 (insert "Alternatives:\n")
624 (dolist (x alternatives)
626 (mh-insert-mime-button x (mh-mime-part-index x) nil))
627 (insert "\n----------------------------------------------------\n")))
629 (defun mh-mime-display-security (handle)
630 "Display PGP encrypted/signed message, HANDLE."
632 (narrow-to-region (point) (point))
634 (mh-insert-mime-security-button handle)
635 (mh-mime-display-mixed (cdr handle))
637 (let ((mh-mime-security-button-line-format
638 mh-mime-security-button-end-line-format))
639 (mh-insert-mime-security-button handle))
640 (mh-mm-set-handle-multipart-parameter
641 handle 'mh-region (cons (point-min-marker) (point-max-marker)))))
643 (defun mh-mime-display-single (handle)
644 "Display a leaf node, HANDLE in the MIME tree."
645 (let* ((type (mm-handle-media-type handle))
646 (small-image-flag (mh-small-image-p handle))
647 (attachmentp (equal (car (mm-handle-disposition handle))
649 (inlinep (and (equal (car (mm-handle-disposition handle)) "inline")
650 (mm-inlinable-p handle)
651 (mm-inlined-p handle)))
652 (displayp (or inlinep ; show if inline OR
653 (mh-inline-vcard-p handle); inline vcard OR
654 (and (not attachmentp) ; if not an attachment
655 (or small-image-flag ; and small image
656 ; and user wants inline
658 (mm-handle-media-supertype handle)
660 (mm-inlinable-p handle)
661 (mm-inlined-p handle)))))))
663 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
664 (cond ((and mh-pgp-support-flag
665 (equal type "application/pgp-signature"))
666 nil) ; skip signatures as they are already handled...
669 (mh-insert-mime-button handle (mh-mime-part-index handle) nil))
671 (not mh-display-buttons-for-inline-parts-flag))
672 (or (mm-display-part handle)
673 (mm-display-part handle))
674 (mh-signature-highlight handle))
676 mh-display-buttons-for-inline-parts-flag)
678 (mh-insert-mime-button handle (mh-mime-part-index handle) nil)
680 (mh-mm-display-part handle)))
681 (goto-char (point-max)))))
683 ;; There is a bug in Gnus inline image display due to which an extra line
684 ;; gets inserted every time it is viewed. To work around that problem we are
685 ;; using an extra property 'mh-region to remember the region that is added
686 ;; when the button is clicked. The region is then deleted to make sure that
687 ;; no extra lines get inserted.
688 (defun mh-mm-display-part (handle)
689 "Toggle display of button for MIME part, HANDLE."
691 (let ((id (get-text-property (point) 'mh-part))
693 (window (selected-window))
694 (mail-parse-charset 'nil)
695 (mail-parse-ignored-charsets nil)
696 region buffer-read-only)
699 (let ((win (get-buffer-window (current-buffer) t)))
704 (if (mm-handle-displayed-p handle)
705 ;; This will remove the part.
707 ;; Delete the button and displayed part (if any)
708 (let ((region (get-text-property point 'mh-region)))
710 (mh-funcall-if-exists
711 remove-images (car region) (cdr region)))
712 (mm-display-part handle)
714 (delete-region (car region) (cdr region))))
715 ;; Delete button (if it still remains). This happens for
716 ;; externally displayed parts where the previous step does
719 (delete-region (point) (progn (forward-line) (point)))))
721 (delete-region (point) (progn (forward-line 1) (point)))
722 (narrow-to-region (point) (point))
723 ;; Maybe we need another unwind-protect here.
724 (when (equal (mm-handle-media-supertype handle) "image")
726 (when (and (not (eq (ignore-errors (mm-display-part handle))
728 (equal (mm-handle-media-supertype handle)
730 (goto-char (point-min))
732 (when (equal (mm-handle-media-supertype handle) "text")
733 (when (eq mh-highlight-citation-style 'gnus)
734 (mh-gnus-article-highlight-citation))
736 (mh-display-emphasis)
737 (mh-signature-highlight handle))
738 (setq region (cons (progn (goto-char (point-min))
740 (progn (goto-char (point-max))
742 (when (window-live-p window)
743 (select-window window))
746 (mh-insert-mime-button handle id (mm-handle-displayed-p handle))
749 (add-text-properties (mh-line-beginning-position)
750 (mh-line-end-position)
751 `(mh-region ,region)))))))
753 (defun mh-mime-part-index (handle)
754 "Generate the button number for MIME part, HANDLE.
755 Notice that a hash table is used to display the same number when
756 buttons need to be displayed multiple times (for instance when
757 nested messages are opened)."
758 (or (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
759 (setf (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
760 (incf (mh-mime-parts-count (mh-buffer-data))))))
762 (defun mh-small-image-p (handle)
763 "Decide whether HANDLE is a \"small\" image that can be displayed inline.
764 This is only useful if a Content-Disposition header is not present."
765 (let ((media-test (caddr (assoc (car (mm-handle-type handle))
766 mh-mm-inline-media-tests)))
767 (mm-inline-large-images t))
769 (equal (mm-handle-media-supertype handle) "image")
770 (funcall media-test handle) ; Since mm-inline-large-images is T,
771 ; this only tells us if the image is
772 ; something that emacs can display
773 (let* ((image (mm-get-image handle)))
775 (and (mh-funcall-if-exists glyphp image)
776 (< (glyph-width image)
777 (or mh-max-inline-image-width (window-pixel-width)))
778 (< (glyph-height image)
779 (or mh-max-inline-image-height
780 (window-pixel-height)))))
782 (let ((size (mh-funcall-if-exists image-size image)))
784 (< (cdr size) (or mh-max-inline-image-height
785 (1- (window-height))))
786 (< (car size) (or mh-max-inline-image-width
787 (window-width)))))))))))
789 (defun mh-inline-vcard-p (handle)
790 "Decide if HANDLE is a vcard that must be displayed inline."
791 (let ((type (mm-handle-type handle)))
792 (and (or (featurep 'vcard) (fboundp 'vcard-pretty-print))
794 (equal (car type) "text/x-vcard")
798 (goto-char (point-min))
799 (not (mh-signature-separator-p)))))))
801 (defun mh-signature-highlight (&optional handle)
802 "Highlight message signature in HANDLE.
803 The optional argument, HANDLE is a MIME handle if the function is
804 being used to highlight the signature in a MIME part."
806 (cond ((not handle) "^-- $")
807 ((not (and (equal (mm-handle-media-supertype handle) "text")
808 (equal (mm-handle-media-subtype handle) "html")))
810 ((eq (mh-mm-text-html-renderer) 'lynx) "^ --$")
813 (goto-char (point-max))
814 (when (re-search-backward regexp nil t)
816 (let ((ov (make-overlay (point) (point-max))))
817 (overlay-put ov 'face 'mh-show-signature)
818 (overlay-put ov 'evaporate t)))
820 (set-extent-property (make-extent (point) (point-max))
821 'face 'mh-show-signature))))))
828 (defvar dots) ; XEmacs
829 (defvar type) ; XEmacs
832 (defun mh-insert-mime-button (handle index displayed)
833 "Insert MIME button for HANDLE.
834 INDEX is the part number that will be DISPLAYED. It is also used
835 by commands like \"K v\" which operate on individual MIME parts."
836 ;; The button could be displayed by a previous decode. In that case
837 ;; undisplay it if we need a hidden button.
838 (when (and (mm-handle-displayed-p handle) (not displayed))
839 (mm-display-part handle))
840 (let ((name (or (mail-content-type-get (mm-handle-type handle) 'name)
841 (mail-content-type-get (mm-handle-disposition handle)
843 (mail-content-type-get (mm-handle-type handle) 'url)
845 (type (mm-handle-media-type handle))
846 (description (mail-decode-encoded-word-string
847 (or (mm-handle-description handle) "")))
848 (dots (if (or displayed (mm-handle-displayed-p handle)) " " "..."))
850 (if (string-match ".*/" name) (setq name (substring name (match-end 0))))
851 (setq long-type (concat type (and (not (equal name ""))
852 (concat "; " name))))
853 (unless (equal description "")
854 (setq long-type (concat " --- " long-type)))
855 (unless (bolp) (insert "\n"))
858 mh-mime-button-line-format mh-mime-button-line-format-alist
859 `(,@(mh-gnus-local-map-property mh-mime-button-map)
860 mh-callback mh-mm-display-part
864 (widget-convert-button
867 :action 'mh-widget-press-button
868 :button-keymap mh-mime-button-map
870 "Mouse-2 click or press RET (in show buffer) to toggle display")
871 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
872 (mh-funcall-if-exists overlay-put ov 'evaporate t))))
875 (defvar mm-verify-function-alist) ; < Emacs 22
876 (defvar mm-decrypt-function-alist) ; < Emacs 22
877 (defvar pressed-details) ; XEmacs
879 (defun mh-insert-mime-security-button (handle)
880 "Display buttons for PGP message, HANDLE."
881 (let* ((protocol (mh-mm-handle-multipart-ctl-parameter handle 'protocol))
882 (crypto-type (or (nth 2 (assoc protocol mm-verify-function-alist))
883 (nth 2 (assoc protocol mm-decrypt-function-alist))
885 (type (concat crypto-type
886 (if (equal (car handle) "multipart/signed")
887 " Signed" " Encrypted")
889 (info (or (mh-mm-handle-multipart-ctl-parameter handle 'gnus-info)
891 (details (mh-mm-handle-multipart-ctl-parameter handle 'gnus-details))
892 pressed-details begin end face)
893 (setq details (if details (concat "\n" details) ""))
894 (setq pressed-details (if mh-mime-security-button-pressed details ""))
895 (setq face (mh-mime-security-button-face info))
896 (unless (bolp) (insert "\n"))
899 mh-mime-security-button-line-format
900 mh-mime-security-button-line-format-alist
901 `(,@(mh-gnus-local-map-property mh-mime-security-button-map)
902 mh-button-pressed ,mh-mime-security-button-pressed
903 mh-callback mh-mime-security-press-button
904 mh-line-format ,mh-mime-security-button-line-format
907 (widget-convert-button 'link begin end
909 :action 'mh-widget-press-button
910 :button-keymap mh-mime-security-button-map
912 :help-echo "Mouse-2 click or press RET (in show buffer) to see security details.")
913 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
914 (mh-funcall-if-exists overlay-put ov 'evaporate t))
915 (when (equal info "Failed")
916 (let* ((type (if (equal (car handle) "multipart/signed")
917 "verification" "decryption"))
918 (warning (if (equal type "decryption")
919 "(passphrase may be incorrect)" "")))
920 (message "%s %s failed %s" crypto-type type warning)))))
922 (defun mh-mime-security-button-face (info)
923 "Return the button face to use for encrypted/signed mail based on INFO."
924 (cond ((string-match "OK" info) ;Decrypted mail
926 ((string-match "Failed" info) ;Decryption failed or signature invalid
928 ((string-match "Undecided" info);Unprocessed mail
929 'mh-show-pgg-unknown)
930 ((string-match "Untrusted" info);Key not trusted
931 'mh-show-pgg-unknown)
939 (defun mh-folder-mime-action (part-index action include-security-flag)
940 "Go to PART-INDEX and carry out ACTION.
942 If PART-INDEX is nil then go to the next part in the buffer. The
943 search for the next buffer wraps around if end of buffer is reached.
944 If argument INCLUDE-SECURITY-FLAG is non-nil then include security
945 info buttons when searching for a suitable parts."
946 (unless mh-showing-mode
948 (mh-in-show-buffer (mh-show-buffer)
952 (let ((part (get-text-property p 'mh-part)))
953 (and (integerp part) (= part part-index)))))
955 (if include-security-flag
956 (get-text-property p 'mh-data)
957 (integerp (get-text-property p 'mh-part)))))))
959 (cond ((and (get-text-property point 'mh-part)
960 (or (null part-index)
961 (= (get-text-property point 'mh-part) part-index)))
963 ((and (get-text-property point 'mh-data)
964 include-security-flag
968 (mh-goto-next-button nil criterion)
969 (if (= (point) point)
970 (message "No matching MIME part found")
971 (funcall action)))))))
974 (defun mh-goto-next-button (backward-flag &optional criterion)
975 "Search for next button satisfying criterion.
977 If BACKWARD-FLAG is non-nil search backward in the buffer for a mime
979 If CRITERION is a function or a symbol which has a function binding
980 then that function must return non-nil at the button we stop."
981 (unless (or (and (symbolp criterion) (fboundp criterion))
982 (functionp criterion))
983 (setq criterion (lambda (x) t)))
984 ;; Move to the next button in the buffer satisfying criterion
985 (goto-char (or (save-excursion
987 ;; Find point before current button
988 (let ((point-before-current-button
990 (while (get-text-property (point) 'mh-data)
991 (unless (= (forward-line
992 (if backward-flag 1 -1))
995 (goto-char (point-min))
996 (goto-char (point-max)))))
998 ;; Skip over current button
999 (while (and (get-text-property (point) 'mh-data)
1000 (not (if backward-flag (bobp) (eobp))))
1001 (forward-line (if backward-flag -1 1)))
1002 ;; Stop at next MIME button if any exists.
1005 (unless (= (forward-line
1006 (if backward-flag -1 1))
1009 (goto-char (point-max))
1010 (goto-char (point-min)))
1011 (beginning-of-line))
1013 point-before-current-button)
1014 (when (and (get-text-property (point) 'mh-data)
1015 (funcall criterion (point)))
1016 (return-from loop (point))))
1020 (defun mh-widget-press-button (widget el)
1021 "Callback for widget, WIDGET.
1022 Parameter EL is unused."
1023 (goto-char (widget-get widget :from))
1026 (defun mh-press-button ()
1027 "View contents of button.
1029 This command is a toggle so if you use it again on the same
1030 attachment, the attachment is hidden."
1032 (let ((mm-inline-media-tests mh-mm-inline-media-tests)
1033 (data (get-text-property (point) 'mh-data))
1034 (function (get-text-property (point) 'mh-callback))
1035 (buffer-read-only nil)
1036 (folder mh-show-folder-buffer))
1037 (flet ((mm-handle-set-external-undisplayer
1039 (mh-handle-set-external-undisplayer folder handle function)))
1040 (when (and function (eolp))
1042 (unwind-protect (and function (funcall function data))
1043 (set-buffer-modified-p nil)))))
1045 (defun mh-push-button (event)
1046 "Click MIME button for EVENT.
1048 If the MIME part is visible then it is removed. Otherwise the
1049 part is displayed. This function is called when the mouse is used
1050 to click the MIME button."
1052 (mh-do-at-event-location event
1053 (let ((folder mh-show-folder-buffer)
1054 (mm-inline-media-tests mh-mm-inline-media-tests)
1055 (data (get-text-property (point) 'mh-data))
1056 (function (get-text-property (point) 'mh-callback)))
1057 (flet ((mm-handle-set-external-undisplayer (handle func)
1058 (mh-handle-set-external-undisplayer folder handle func)))
1059 (and function (funcall function data))))))
1061 (defun mh-handle-set-external-undisplayer (folder handle function)
1062 "Replacement for `mm-handle-set-external-undisplayer'.
1064 This is only called in recent versions of Gnus. The MIME handles
1065 are stored in data structures corresponding to MH-E folder buffer
1066 FOLDER instead of in Gnus (as in the original). The MIME part,
1067 HANDLE is associated with the undisplayer FUNCTION."
1068 (if (mh-mm-keep-viewer-alive-p handle)
1069 (let ((new-handle (copy-sequence handle)))
1070 (mm-handle-set-undisplayer new-handle function)
1071 (mm-handle-set-undisplayer handle nil)
1074 (push new-handle (mh-mime-handles (mh-buffer-data)))))
1075 (mm-handle-set-undisplayer handle function)))
1077 (defun mh-mime-security-press-button (handle)
1078 "Callback from security button for part HANDLE."
1079 (if (mh-mm-handle-multipart-ctl-parameter handle 'gnus-info)
1080 (mh-mime-security-show-details handle)
1081 (let ((region (mh-mm-handle-multipart-ctl-parameter handle 'mh-region))
1083 (setq point (point))
1084 (goto-char (car region))
1085 (delete-region (car region) (cdr region))
1086 (with-current-buffer (mh-mm-handle-multipart-ctl-parameter handle 'buffer)
1087 (let* ((mm-verify-option 'known)
1088 (mm-decrypt-option 'known)
1089 (new (mh-mm-possibly-verify-or-decrypt (cdr handle) handle)))
1090 (unless (eq new (cdr handle))
1091 (mh-mm-destroy-parts (cdr handle))
1092 (setcdr handle new))))
1093 (mh-mime-display-security handle)
1094 (goto-char point))))
1096 ;; I rewrote the security part because Gnus doesn't seem to ever minimize
1097 ;; the button. That is once the mime-security button is pressed there seems
1098 ;; to be no way of getting rid of the inserted text.
1099 (defun mh-mime-security-show-details (handle)
1100 "Toggle display of detailed security info for HANDLE."
1101 (let ((details (mh-mm-handle-multipart-ctl-parameter handle 'gnus-details)))
1103 (let ((mh-mime-security-button-pressed
1104 (not (get-text-property (point) 'mh-button-pressed)))
1105 (mh-mime-security-button-line-format
1106 (get-text-property (point) 'mh-line-format)))
1108 (while (eq (get-text-property (point) 'mh-line-format)
1109 mh-mime-security-button-line-format)
1113 (narrow-to-region (point) (point))
1114 (mh-insert-mime-security-button handle))
1117 (or (text-property-not-all
1119 'mh-line-format mh-mime-security-button-line-format)
1121 (forward-line -1)))))
1125 ;;; Miscellaneous Article Washing
1128 (defun mh-add-missing-mime-version-header ()
1129 "Some mail programs don't put a MIME-Version header.
1130 I have seen this only in spam, so maybe we shouldn't fix
1133 (goto-char (point-min))
1134 (re-search-forward "\n\n" nil t)
1136 (narrow-to-region (point-min) (point))
1137 (when (and (message-fetch-field "content-type")
1138 (not (message-fetch-field "mime-version")))
1139 (goto-char (point-min))
1140 (insert "MIME-Version: 1.0\n")))))
1143 (defun mh-display-smileys ()
1145 (when (and mh-graphical-smileys-flag (mh-small-show-buffer-p))
1146 (mh-funcall-if-exists smiley-region (point-min) (point-max))))
1149 (defun mh-display-emphasis ()
1150 "Display graphical emphasis."
1151 (when (and mh-graphical-emphasis-flag (mh-small-show-buffer-p))
1152 (flet ((article-goto-body ())) ; shadow this function to do nothing
1154 (goto-char (point-min))
1155 (article-emphasize)))))
1157 (defun mh-small-show-buffer-p ()
1158 "Check if show buffer is small.
1159 This is used to decide if smileys and graphical emphasis should be
1162 (when (and (boundp 'font-lock-maximum-size) font-lock-maximum-size)
1163 (cond ((numberp font-lock-maximum-size)
1164 (setq max font-lock-maximum-size))
1165 ((listp font-lock-maximum-size)
1166 (setq max (cdr (or (assoc 'mh-show-mode font-lock-maximum-size)
1167 (assoc t font-lock-maximum-size)))))))
1168 (or (not (numberp max)) (>= (/ max 8) (buffer-size)))))
1172 ;;; MH-Letter Commands
1174 ;; MH-E commands are alphabetical; specific support routines follow command.
1177 (defun mh-compose-forward (&optional description folder range)
1178 "Add tag to forward a message.
1180 You are prompted for a content DESCRIPTION, the name of the
1181 FOLDER in which the messages to forward are located, and a RANGE
1182 of messages, which defaults to the current message in that
1183 folder. Check the documentation of `mh-interactive-range' to see
1184 how RANGE is read in interactive use.
1186 The option `mh-compose-insertion' controls what type of tags are inserted."
1189 (mml-minibuffer-read-description))
1191 (mh-prompt-for-folder "Message from"
1192 mh-sent-from-folder nil))
1194 (if (and (equal folder mh-sent-from-folder)
1195 (numberp mh-sent-from-msg))
1197 (nth 0 (mh-translate-range folder "cur"))))
1199 (mh-read-range "Forward" folder
1201 (number-to-string default))
1204 (list description folder range)))
1205 (let ((messages (mapconcat 'identity (mh-list-to-string range) " ")))
1206 (dolist (message (mh-translate-range folder messages))
1207 (if (equal mh-compose-insertion 'mml)
1208 (mh-mml-forward-message description folder (format "%s" message))
1209 (mh-mh-forward-message description folder (format "%s" message))))))
1212 (defun mh-mml-forward-message (description folder message)
1213 "Forward a message as attachment.
1215 The function will prompt the user for a DESCRIPTION, a FOLDER and
1217 (let ((msg (if (and (equal message "") (numberp mh-sent-from-msg))
1219 (string-to-number message))))
1220 (cond ((integerp msg)
1221 (if (string= "" description)
1222 ;; Rationale: mml-attach-file constructs a malformed composition
1223 ;; if the description string is empty. This fixes SF #625168.
1224 (mml-attach-file (format "%s%s/%d"
1225 mh-user-path (substring folder 1) msg)
1227 (mml-attach-file (format "%s%s/%d"
1228 mh-user-path (substring folder 1) msg)
1231 (t (error "The message number, %s, is not a integer" msg)))))
1233 (defun mh-mh-forward-message (&optional description folder messages)
1234 "Add tag to forward a message.
1235 You are prompted for a content DESCRIPTION, the name of the
1236 FOLDER in which the messages to forward are located, and the
1239 See also \\[mh-mh-to-mime]."
1241 (mml-minibuffer-read-description)
1242 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
1243 (read-string (concat "Messages"
1244 (if (numberp mh-sent-from-msg)
1245 (format " (default %d): "
1251 (not (string= description ""))
1252 (insert description))
1255 (not (string= folder ""))
1256 (insert " " folder))
1258 (not (string= messages "")))
1259 (let ((start (point)))
1260 (insert " " messages)
1261 (subst-char-in-region start (point) ?, ? ))
1262 (if (numberp mh-sent-from-msg)
1263 (insert " " (int-to-string mh-sent-from-msg))))
1267 (defun mh-compose-insertion (&optional inline)
1268 "Add tag to include a file such as an image or sound.
1270 You are prompted for the filename containing the object, the
1271 media type if it cannot be determined automatically, and a
1272 content description. If you're using MH-style directives, you
1273 will also be prompted for additional attributes.
1275 The option `mh-compose-insertion' controls what type of tags are
1276 inserted. Optional argument INLINE means make it an inline
1279 (if (equal mh-compose-insertion 'mml)
1281 (mh-mml-attach-file "inline")
1282 (mh-mml-attach-file))
1283 (call-interactively 'mh-mh-attach-file)))
1285 (defun mh-mml-attach-file (&optional disposition)
1286 "Add a tag to insert a MIME message part from a file.
1288 You are prompted for the filename containing the object, the
1289 media type if it cannot be determined automatically, a content
1290 description and the DISPOSITION of the attachment.
1292 This is basically `mml-attach-file' from Gnus, modified such that a prefix
1293 argument yields an \"inline\" disposition and Content-Type is determined
1295 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1296 (type (mh-minibuffer-read-type file))
1297 (description (mml-minibuffer-read-description))
1298 (dispos (or disposition
1299 (mh-mml-minibuffer-read-disposition type))))
1300 (mml-insert-empty-tag 'part 'type type 'filename file
1301 'disposition dispos 'description description)))
1303 (defun mh-mh-attach-file (filename type description attributes)
1304 "Add a tag to insert a MIME message part from a file.
1305 You are prompted for the FILENAME containing the object, the
1306 media TYPE if it cannot be determined automatically, and a
1307 content DESCRIPTION. In addition, you are also prompted for
1308 additional ATTRIBUTES.
1310 See also \\[mh-mh-to-mime]."
1311 (interactive (let ((filename (mml-minibuffer-read-file "Attach file: ")))
1314 (mh-minibuffer-read-type filename)
1315 (mml-minibuffer-read-description)
1316 (read-string "Attributes: "
1318 (file-name-nondirectory filename)
1320 (mh-mh-compose-type filename type description attributes))
1322 (defun mh-mh-compose-type (filename type
1323 &optional description attributes comment)
1324 "Insert an MH-style directive to insert a file.
1325 The file specified by FILENAME is encoded as TYPE. An optional
1326 DESCRIPTION is used as the Content-Description field, optional
1327 set of ATTRIBUTES and an optional COMMENT can also be included."
1331 (insert "; " attributes))
1333 (insert " (" comment ")"))
1336 (insert description))
1337 (insert "] " (expand-file-name filename))
1341 (defun mh-mh-compose-anon-ftp (host filename type description)
1342 "Add tag to include anonymous ftp reference to a file.
1344 You can have your message initiate an \"ftp\" transfer when the
1345 recipient reads the message. You are prompted for the remote HOST
1346 and FILENAME, the media TYPE, and the content DESCRIPTION.
1348 See also \\[mh-mh-to-mime]."
1350 (read-string "Remote host: ")
1351 (read-string "Remote filename: ")
1352 (mh-minibuffer-read-type "DUMMY-FILENAME")
1353 (mml-minibuffer-read-description)))
1354 (mh-mh-compose-external-type "anon-ftp" host filename
1358 (defun mh-mh-compose-external-compressed-tar (host filename description)
1359 "Add tag to include anonymous ftp reference to a compressed tar file.
1361 In addition to retrieving the file via anonymous \"ftp\" as per
1362 the command \\[mh-mh-compose-anon-ftp], the file will also be
1363 uncompressed and untarred. You are prompted for the remote HOST
1364 and FILENAME and the content DESCRIPTION.
1366 See also \\[mh-mh-to-mime]."
1368 (read-string "Remote host: ")
1369 (read-string "Remote filename: ")
1370 (mml-minibuffer-read-description)))
1371 (mh-mh-compose-external-type "anon-ftp" host filename
1372 "application/octet-stream"
1374 "type=tar; conversions=x-compress"
1377 ;; RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One:
1378 ;; Format of Internet Message Bodies.
1379 ;; RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two:
1381 ;; RFC 2049 - Multipurpose Internet Mail Extensions (MIME) Part Five:
1382 ;; Conformance Criteria and Examples.
1383 ;; RFC 2017 - Definition of the URL MIME External-Body Access-Type
1384 ;; RFC 1738 - Uniform Resource Locators (URL)
1385 (defvar mh-access-types
1386 '(("anon-ftp") ; RFC2046 Anonymous File Transfer Protocol
1387 ("file") ; RFC1738 Host-specific file names
1388 ("ftp") ; RFC2046 File Transfer Protocol
1389 ("gopher") ; RFC1738 The Gopher Protocol
1390 ("http") ; RFC1738 Hypertext Transfer Protocol
1391 ("local-file") ; RFC2046 Local file access
1392 ("mail-server") ; RFC2046 mail-server Electronic mail address
1393 ("mailto") ; RFC1738 Electronic mail address
1394 ("news") ; RFC1738 Usenet news
1395 ("nntp") ; RFC1738 Usenet news using NNTP access
1396 ("propspero") ; RFC1738 Prospero Directory Service
1397 ("telnet") ; RFC1738 Telnet
1398 ("tftp") ; RFC2046 Trivial File Transfer Protocol
1399 ("url") ; RFC2017 URL scheme MIME access-type Protocol
1400 ("wais")) ; RFC1738 Wide Area Information Servers
1401 "Valid MIME access-type values.")
1404 (defun mh-mh-compose-external-type (access-type host filename type
1405 &optional description
1406 attributes parameters
1408 "Add tag to refer to a remote file.
1410 This command is a general utility for referencing external files.
1411 In fact, all of the other commands that insert directives to
1412 access external files call this command. You are prompted for the
1413 ACCESS-TYPE, remote HOST and FILENAME, and content TYPE. If you
1414 provide a prefix argument, you are also prompted for a content
1415 DESCRIPTION, ATTRIBUTES, PARAMETERS, and a COMMENT.
1417 See also \\[mh-mh-to-mime]."
1419 (completing-read "Access type: " mh-access-types)
1420 (read-string "Remote host: ")
1421 (read-string "Remote filename: ")
1422 (mh-minibuffer-read-type "DUMMY-FILENAME")
1423 (if current-prefix-arg (mml-minibuffer-read-description))
1424 (if current-prefix-arg (read-string "Attributes: "))
1425 (if current-prefix-arg (read-string "Parameters: "))
1426 (if current-prefix-arg (read-string "Comment: "))))
1430 (insert "; " attributes))
1432 (insert " (" comment ") "))
1435 (insert description))
1437 (insert "access-type=" access-type "; ")
1438 (insert "site=" host)
1439 (insert "; name=" (file-name-nondirectory filename))
1440 (let ((directory (file-name-directory filename)))
1442 (insert "; directory=\"" directory "\"")))
1444 (insert "; " parameters))
1447 (defvar mh-mh-to-mime-args nil
1448 "Extra arguments for \\[mh-mh-to-mime] to pass to the \"mhbuild\" command.
1449 The arguments are passed to \"mhbuild\" if \\[mh-mh-to-mime] is
1450 given a prefix argument. Normally default arguments to
1451 \"mhbuild\" are specified in the MH profile.")
1454 (defun mh-mh-to-mime (&optional extra-args)
1455 "Compose MIME message from MH-style directives.
1457 Typically, you send a message with attachments just like any other
1458 message. However, you may take a sneak preview of the MIME encoding if
1459 you wish by running this command.
1461 If you wish to pass additional arguments to \"mhbuild\" (\"mhn\")
1462 to affect how it builds your message, use the option
1463 `mh-mh-to-mime-args'. For example, you can build a consistency
1464 check into the message by setting `mh-mh-to-mime-args' to
1465 \"-check\". The recipient of your message can then run \"mhbuild
1466 -check\" on the message--\"mhbuild\" (\"mhn\") will complain if
1467 the message has been corrupted on the way. This command only
1468 consults this option when given a prefix argument EXTRA-ARGS.
1470 The hook `mh-mh-to-mime-hook' is called after the message has been
1473 The effects of this command can be undone by running
1474 \\[mh-mh-to-mime-undo]."
1476 (mh-mh-quote-unescaped-sharp)
1478 (message "Running %s..." (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
1480 ((mh-variant-p 'nmh)
1481 (mh-exec-cmd-error nil
1483 (if extra-args mh-mh-to-mime-args)
1486 (mh-exec-cmd-error (format "mhdraft=%s" buffer-file-name)
1488 (if extra-args mh-mh-to-mime-args)
1491 (message "Running %s...done" (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
1492 (run-hooks 'mh-mh-to-mime-hook))
1494 (defun mh-mh-quote-unescaped-sharp ()
1495 "Quote \"#\" characters that haven't been quoted for \"mhbuild\".
1496 If the \"#\" character is present in the first column, but it isn't
1497 part of a MH-style directive then \"mhbuild\" gives an error.
1498 This function will quote all such characters."
1500 (goto-char (point-min))
1501 (while (re-search-forward "^#" nil t)
1503 (unless (mh-mh-directive-present-p (point) (mh-line-end-position))
1505 (goto-char (mh-line-end-position)))))
1508 (defun mh-mh-to-mime-undo (noconfirm)
1509 "Undo effects of \\[mh-mh-to-mime].
1511 It does this by reverting to a backup file. You are prompted to
1512 confirm this action, but you can avoid the confirmation by adding
1513 a prefix argument NOCONFIRM."
1515 (if (null buffer-file-name)
1516 (error "Buffer does not seem to be associated with any file"))
1517 (let ((backup-strings '("," "#"))
1519 (while (and backup-strings
1522 (concat (file-name-directory buffer-file-name)
1523 (car backup-strings)
1524 (file-name-nondirectory buffer-file-name)
1526 (setq backup-strings (cdr backup-strings)))
1528 (error "Backup file for %s no longer exists" buffer-file-name))
1530 (yes-or-no-p (format "Revert buffer from file %s? "
1532 (error "Revert not confirmed"))
1533 (let ((buffer-read-only nil))
1535 (insert-file-contents backup-file))
1536 (after-find-file nil)))
1539 (defvar mh-identity-pgg-default-user-id)
1542 (defun mh-mml-secure-message-encrypt (method)
1543 "Add tag to encrypt the message.
1545 A proper multipart message is created for you when you send the
1546 message. Use the command \\[mh-mml-unsecure-message] to remove
1547 this tag. Use a prefix argument METHOD to be prompted for one of
1548 the possible security methods (see `mh-mml-method-default')."
1549 (interactive (list (mh-mml-query-cryptographic-method)))
1550 (mh-secure-message method "encrypt" mh-identity-pgg-default-user-id))
1553 (defun mh-mml-secure-message-sign (method)
1554 "Add tag to sign the message.
1556 A proper multipart message is created for you when you send the
1557 message. Use the command \\[mh-mml-unsecure-message] to remove
1558 this tag. Use a prefix argument METHOD to be prompted for one of
1559 the possible security methods (see `mh-mml-method-default')."
1560 (interactive (list (mh-mml-query-cryptographic-method)))
1561 (mh-secure-message method "sign" mh-identity-pgg-default-user-id))
1564 (defun mh-mml-secure-message-signencrypt (method)
1565 "Add tag to encrypt and sign the message.
1567 A proper multipart message is created for you when you send the
1568 message. Use the command \\[mh-mml-unsecure-message] to remove
1569 this tag. Use a prefix argument METHOD to be prompted for one of
1570 the possible security methods (see `mh-mml-method-default')."
1571 (interactive (list (mh-mml-query-cryptographic-method)))
1572 (mh-secure-message method "signencrypt" mh-identity-pgg-default-user-id))
1574 (defvar mh-mml-cryptographic-method-history ())
1576 (defun mh-mml-query-cryptographic-method ()
1577 "Read the cryptographic method to use."
1578 (if current-prefix-arg
1579 (let ((def (or (car mh-mml-cryptographic-method-history)
1580 mh-mml-method-default)))
1581 (completing-read (format "Method (default %s): " def)
1582 '(("pgp") ("pgpmime") ("smime"))
1583 nil t nil 'mh-mml-cryptographic-method-history def))
1584 mh-mml-method-default))
1586 (defun mh-secure-message (method mode &optional identity)
1587 "Add tag to encrypt or sign message.
1589 METHOD should be one of: \"pgpmime\", \"pgp\", \"smime\".
1590 MODE should be one of: \"sign\", \"encrypt\", \"signencrypt\", \"none\".
1591 IDENTITY is optionally the default-user-id to use."
1592 (if (not mh-pgp-support-flag)
1593 (error "Your version of Gnus does not support PGP/GPG")
1594 ;; Check the arguments
1595 (let ((valid-methods (list "pgpmime" "pgp" "smime"))
1596 (valid-modes (list "sign" "encrypt" "signencrypt" "none")))
1597 (if (not (member method valid-methods))
1598 (error "Method %s is invalid" method))
1599 (if (not (member mode valid-modes))
1600 (error "Mode %s is invalid" mode))
1601 (mml-unsecure-message)
1602 (if (not (string= mode "none"))
1604 (goto-char (point-min))
1605 (mh-goto-header-end 1)
1606 (if mh-identity-pgg-default-user-id
1607 (mml-insert-tag 'secure 'method method 'mode mode
1608 'sender mh-identity-pgg-default-user-id)
1609 (mml-insert-tag 'secure 'method method 'mode mode)))))))
1612 (defun mh-mml-to-mime ()
1613 "Compose MIME message from MML tags.
1615 Typically, you send a message with attachments just like any
1616 other message. However, you may take a sneak preview of the MIME
1617 encoding if you wish by running this command.
1619 This action can be undone by running \\[undo]."
1622 (when mh-pgp-support-flag ;; This is only needed for PGP
1623 (message-options-set-recipient))
1624 (let ((saved-text (buffer-string))
1625 (buffer (current-buffer))
1626 (modified-flag (buffer-modified-p)))
1627 (condition-case err (mml-to-mime)
1629 (with-current-buffer buffer
1630 (delete-region (point-min) (point-max))
1632 (set-buffer-modified-p modified-flag))
1633 (error (error-message-string err))))))
1636 (defun mh-mml-unsecure-message ()
1637 "Remove any secure message tags."
1639 (if (not mh-pgp-support-flag)
1640 (error "Your version of Gnus does not support PGP/GPG")
1641 (mml-unsecure-message)))
1645 ;;; Support Routines for MH-Letter Commands
1648 (defun mh-mml-tag-present-p ()
1649 "Check if the current buffer has text which may be a MML tag."
1651 (goto-char (point-min))
1654 "\\(<#\\(mml\\|part\\)\\(.\\|\n\\)*>[ \n\t]*<#/\\(mml\\|part\\)>\\|"
1658 (defvar mh-media-type-regexp
1659 (concat (regexp-opt '("text" "image" "audio" "video" "application"
1660 "multipart" "message") t)
1662 "Regexp matching valid media types used in MIME attachment compositions.")
1665 (defun mh-mh-directive-present-p (&optional begin end)
1666 "Check if the text between BEGIN and END might be a MH-style directive.
1667 The optional argument BEGIN defaults to the beginning of the
1668 buffer, while END defaults to the the end of the buffer."
1669 (unless begin (setq begin (point-min)))
1670 (unless end (setq end (point-max)))
1672 (block 'search-for-mh-directive
1674 (while (re-search-forward "^#" end t)
1675 (let ((s (buffer-substring-no-properties
1676 (point) (mh-line-end-position))))
1677 (cond ((equal s ""))
1678 ((string-match "^forw[ \t\n]+" s)
1679 (return-from 'search-for-mh-directive t))
1680 (t (let ((first-token (car (split-string s "[ \t;@]"))))
1681 (when (and first-token
1682 (string-match mh-media-type-regexp
1684 (return-from 'search-for-mh-directive t)))))))
1687 (defun mh-minibuffer-read-type (filename &optional default)
1688 "Return the content type associated with the given FILENAME.
1689 If the \"file\" command exists and recognizes the given file,
1690 then its value is returned\; otherwise, the user is prompted for
1691 a type (see `mailcap-mime-types').
1692 Optional argument DEFAULT is returned if a type isn't entered."
1693 (mailcap-parse-mimetypes)
1694 (let* ((default (or default
1695 (mm-default-file-encoding filename)
1696 "application/octet-stream"))
1697 (probed-type (mh-file-mime-type filename))
1698 (type (or (and (not (equal probed-type "application/octet-stream"))
1701 (format "Content type (default %s): " default)
1702 (mapcar 'list (mailcap-mime-types))))))
1703 (if (not (equal type ""))
1708 (defun mh-file-mime-type (filename)
1709 "Return MIME type of FILENAME from file command.
1710 Returns nil if file command not on system."
1712 ((not (mh-have-file-command))
1713 nil) ;no file command, exit now
1714 ((not (and (file-exists-p filename)
1715 (file-readable-p filename)))
1716 nil) ;no file or not readable, ditto
1719 (let ((tmp-buffer (get-buffer-create mh-temp-buffer)))
1720 (set-buffer tmp-buffer)
1723 (call-process "file" nil '(t nil) nil "-b" "-i"
1724 (expand-file-name filename))
1725 (goto-char (point-min))
1726 (if (not (re-search-forward mh-media-type-regexp nil t))
1728 (mh-file-mime-type-substitute (match-string 0) filename)))
1729 (kill-buffer tmp-buffer)))))))
1731 (defvar mh-file-mime-type-substitutions
1732 '(("application/msword" "\.xls" "application/ms-excel")
1733 ("application/msword" "\.ppt" "application/ms-powerpoint")
1734 ("text/plain" "\.vcf" "text/x-vcard")
1735 ("text/rtf" "\.rtf" "application/rtf")
1736 ("application/x-zip" "\.sxc" "application/vnd.sun.xml.calc")
1737 ("application/x-zip" "\.sxd" "application/vnd.sun.xml.draw")
1738 ("application/x-zip" "\.sxi" "application/vnd.sun.xml.impress")
1739 ("application/x-zip" "\.sxw" "application/vnd.sun.xml.writer")
1740 ("application/x-zip" "\.odg" "application/vnd.oasis.opendocument.graphics")
1741 ("application/x-zip" "\.odi" "application/vnd.oasis.opendocument.image")
1742 ("application/x-zip" "\.odp"
1743 "application/vnd.oasis.opendocument.presentation")
1744 ("application/x-zip" "\.ods"
1745 "application/vnd.oasis.opendocument.spreadsheet")
1746 ("application/x-zip" "\.odt" "application/vnd.oasis.opendocument.text"))
1747 "Substitutions to make for Content-Type returned from file command.
1748 The first element is the Content-Type returned by the file command.
1749 The second element is a regexp matching the file name, usually the
1751 The third element is the Content-Type to replace with.")
1753 (defun mh-file-mime-type-substitute (content-type filename)
1754 "Return possibly changed CONTENT-TYPE on the FILENAME.
1755 Substitutions are made from the `mh-file-mime-type-substitutions'
1757 (let ((subst mh-file-mime-type-substitutions)
1758 (type) (match) (answer content-type)
1759 (case-fold-search t))
1761 (setq type (car (car subst))
1762 match (elt (car subst) 1))
1763 (if (and (string-equal content-type type)
1764 (string-match match filename))
1765 (setq answer (elt (car subst) 2)
1767 (setq subst (cdr subst))))
1770 (defvar mh-have-file-command 'undefined
1771 "Cached value of function `mh-have-file-command'.
1772 Do not reference this variable directly as it might not have been
1773 initialized. Always use the command `mh-have-file-command'.")
1776 (defun mh-have-file-command ()
1777 "Return t if 'file' command is on the system.
1778 'file -i' is used to get MIME type of composition insertion."
1779 (when (eq mh-have-file-command 'undefined)
1780 (setq mh-have-file-command
1781 (and (fboundp 'executable-find)
1782 (executable-find "file") ; file command exists
1783 ; and accepts -i and -b args.
1784 (zerop (call-process "file" nil nil nil "-i" "-b"
1785 (expand-file-name "inc" mh-progs))))))
1786 mh-have-file-command)
1793 (defun mh-mime-cleanup ()
1794 "Free the decoded MIME parts."
1795 (let ((mime-data (gethash (current-buffer) mh-globals-hash)))
1796 ;; This is for Emacs, what about XEmacs?
1797 (mh-funcall-if-exists remove-images (point-min) (point-max))
1799 (mh-mm-destroy-parts (mh-mime-handles mime-data))
1800 (remhash (current-buffer) mh-globals-hash))))
1803 (defun mh-destroy-postponed-handles ()
1804 "Free MIME data for externally displayed MIME parts."
1805 (let ((mime-data (mh-buffer-data)))
1807 (mh-mm-destroy-parts (mh-mime-handles mime-data)))
1808 (remhash (current-buffer) mh-globals-hash)))
1813 ;; indent-tabs-mode: nil
1814 ;; sentence-end-double-space: nil
1817 ;; arch-tag: 0dd36518-1b64-4a84-8f4e-59f422d3f002
1818 ;;; mh-mime.el ends here