Mercurial > emacs
diff lisp/mail/mh-mime.el @ 48595:8aaba207e44b
Upgraded to MH-E version 7.0.
author | Bill Wohler <wohler@newt.com> |
---|---|
date | Fri, 29 Nov 2002 18:15:21 +0000 |
parents | 2568d5a27317 |
children | 30c4902b654d |
line wrap: on
line diff
--- a/lisp/mail/mh-mime.el Fri Nov 29 18:02:52 2002 +0000 +++ b/lisp/mail/mh-mime.el Fri Nov 29 18:15:21 2002 +0000 @@ -1,4 +1,4 @@ -;;; mh-mime.el --- mh-e support for composing MIME messages +;;; mh-mime.el --- MH-E support for composing MIME messages ;; Copyright (C) 1993, 1995, 2001, 2002 Free Software Foundation, Inc. @@ -26,18 +26,93 @@ ;;; Commentary: -;; Internal support for mh-e package. +;; Internal support for MH-E package. ;; Support for generating an mhn composition file. ;; MIME is supported only by MH 6.8 or later. ;;; Change Log: -;; $Id: mh-mime.el,v 1.26 2002/04/07 19:20:56 wohler Exp $ +;; $Id: mh-mime.el,v 1.90 2002/11/22 20:00:48 satyaki Exp $ ;;; Code: -(provide 'mh-mime) +(require 'cl) (require 'mh-comp) +(require 'mh-utils) +(load "mm-decode" t t) ; Non-fatal dependency +(load "mm-uu" t t) ; Non-fatal dependency +(load "mailcap" t t) ; Non-fatal dependency +(load "smiley" t t) ; Non-fatal dependency +(require 'gnus-util) + +(autoload 'gnus-article-goto-header "gnus-art") +(autoload 'article-emphasize "gnus-art") +(autoload 'gnus-get-buffer-create "gnus") +(autoload 'gnus-eval-format "gnus-spec") +(autoload 'widget-convert-button "wid-edit") +(autoload 'message-options-set-recipient "message") +(autoload 'mml-secure-message-sign-pgpmime "mml-sec") +(autoload 'mml-secure-message-encrypt-pgpmime "mml-sec") +(autoload 'mml-minibuffer-read-file "mml") +(autoload 'mml-minibuffer-read-description "mml") +(autoload 'mml-insert-empty-tag "mml") +(autoload 'mml-to-mime "mml") +(autoload 'mml-attach-file "mml") + +;;; Hooks +(defcustom mh-edit-mhn-hook nil + "Invoked on the formatted letter by \\<mh-letter-mode-map>\\[mh-edit-mhn]." + :type 'hook + :group 'mh-hook) + +;; Keeps assorted MIME data +(defstruct (mh-buffer-data (:conc-name mh-mime-) + (:constructor mh-make-buffer-data)) + ;; Structure to keep track of MIME handles on a per buffer basis. + (handles ()) ; List of MIME handles + (handles-cache (make-hash-table)) ; Cache to avoid multiple decodes of + ; nested messages + (parts-count 0) ; The button number is generated from + ; this number + (part-index-hash (make-hash-table))) ; Avoid incrementing the part number + ; for nested messages + +;;; This has to be a macro, since we do: (setf (mh-buffer-data) ...) +(defmacro mh-buffer-data () + "Convenience macro to get the MIME data structures of the current buffer." + `(gethash (current-buffer) mh-globals-hash)) + +(defun mh-compose-insertion (&optional inline) + "Add a directive to insert a MIME part from a file, using mhn or gnus. +If the variable `mh-compose-insertion' is set to 'mhn, then that will be used. +If it is set to 'gnus, then that will be used instead. +Optional argument INLINE means make it an inline attachment." + (interactive "P") + (if (equal mh-compose-insertion 'gnus) + (if inline + (mh-mml-attach-file "inline") + (mh-mml-attach-file)) + (call-interactively 'mh-mhn-compose-insertion))) + +(defun mh-compose-forward (&optional description folder message) + "Add a MIME directive to forward a message, using mhn or gnus. +If the variable `mh-compose-insertion' is set to 'mhn, then that will be used. +If it is set to 'gnus, then that will be used instead. +Optional argument DESCRIPTION is a description of the attachment. +Optional argument FOLDER is the folder from which the forwarded message should +come. +Optional argument MESSAGE is the message to forward. +If any of the optional arguments are absent, they are prompted for." + (interactive (list + (read-string "Forw Content-description: ") + (mh-prompt-for-folder "Message from" mh-sent-from-folder nil) + (read-string (format "Messages%s: " + (if mh-sent-from-msg + (format " [%d]" mh-sent-from-msg) + ""))))) + (if (equal mh-compose-insertion 'gnus) + (mh-mml-forward-message description folder message) + (mh-mhn-compose-forw description folder message))) ;; To do: ;; paragraph code should not fill # lines if MIME enabled. @@ -54,8 +129,15 @@ prefix argument. Normally default arguments to mhn are specified in the MH profile.") -(defvar mh-edit-mhn-hook nil - "Invoked on the formatted letter by \\<mh-letter-mode-map>\\[mh-edit-mhn].") +(defvar mh-media-type-regexp + (concat (regexp-opt '("text" "image" "audio" "video" "application" + "multipart" "message") t) + "/[-.+a-zA-Z0-9]+") + "Regexp matching valid media types used in MIME attachment compositions.") + +;; Just defvar the variable to avoid compiler warning... This doesn't bind +;; the variable, so things should work exactly as before. +(defvar mh-have-file-command) (defun mh-have-file-command () "Return t if 'file' command is on the system. @@ -70,6 +152,30 @@ (expand-file-name "inc" mh-progs)))))) mh-have-file-command) +(defvar mh-file-mime-type-substitutions + '(("application/msword" "\.xls" "application/ms-excel") + ("application/msword" "\.ppt" "application/ms-powerpoint")) + "Substitutions to make for Content-Type returned from file command. +The first element is the Content-Type returned by the file command. +The second element is a regexp matching the file name, usually the extension. +The third element is the Content-Type to replace with.") + +(defun mh-file-mime-type-substitute (content-type filename) + "Return possibly changed CONTENT-TYPE on the FILENAME. +Substitutions are made from the `mh-file-mime-type-substitutions' variable." + (let ((subst mh-file-mime-type-substitutions) + (type) (match) (answer content-type) + (case-fold-search t)) + (while subst + (setq type (car (car subst)) + match (elt (car subst) 1)) + (if (and (string-equal content-type type) + (string-match match filename)) + (setq answer (elt (car subst) 2) + subst nil) + (setq subst (cdr subst)))) + answer)) + (defun mh-file-mime-type (filename) "Return MIME type of FILENAME from file command. Returns nil if file command not on system." @@ -89,14 +195,9 @@ (goto-char (point-min)) (if (not (re-search-forward mh-media-type-regexp nil t)) nil - (match-string 0))) + (mh-file-mime-type-substitute (match-string 0) filename))) (kill-buffer tmp-buffer))))))) -(defvar mh-mhn-compose-insert-p nil - "Buffer-local variable to know whether MIME insertion was done. -Triggers an automatic call to `mh-edit-mhn' in `mh-send-letter'.") -(make-variable-buffer-local 'mh-mhn-compose-insert-p) - ;;; This is needed for Emacs20 which doesn't have mailcap-mime-types. (defvar mh-mime-content-types '(("application/mac-binhex40") ("application/msword") @@ -122,27 +223,22 @@ "Legal MIME content types. See documentation for \\[mh-edit-mhn].") -(defvar mh-media-type-regexp - (concat (regexp-opt '("text" "image" "audio" "video" "application" - "multipart" "message") t) - "/[-.+a-zA-Z0-9]+") - "Regexp matching valid media types used in MIME attachment compositions.") - (defun mh-mhn-compose-insertion (filename type description attributes) "Add a directive to insert a MIME message part from a file. -This is the typical way to insert non-text parts in a message. Arguments are -FILENAME, which tells where to find the file, TYPE, the MIME content type, -DESCRIPTION, a line of text for the Content-Description field. ATTRIBUTES is a -comma separated list of name=value pairs that is appended to the Content-Type -field of the attachment. +This is the typical way to insert non-text parts in a message. + +Arguments are FILENAME, which tells where to find the file, TYPE, the MIME +content type, DESCRIPTION, a line of text for the Content-Description field. +ATTRIBUTES is a comma separated list of name=value pairs that is appended to +the Content-Type field of the attachment. + See also \\[mh-edit-mhn]." (interactive (let ((filename (read-file-name "Insert contents of: "))) (list filename (or (mh-file-mime-type filename) (completing-read "Content-Type: " - (if (and (require 'mailcap nil t) - (fboundp 'mailcap-mime-types)) + (if (fboundp 'mailcap-mime-types) (mapcar 'list (mailcap-mime-types)) mh-mime-content-types))) (read-string "Content-Description: ") @@ -154,7 +250,12 @@ (defun mh-mhn-compose-type (filename type &optional description attributes comment) - (setq mh-mhn-compose-insert-p t) + "Insert a mhn directive to insert a file. + +The file specified by FILENAME is encoded as TYPE. An optional DESCRIPTION is +used as the Content-Description field, optional set of ATTRIBUTES and an +optional COMMENT can also be included." + (setq mh-mhn-compose-insert-flag t) (beginning-of-line) (insert "#" type) (and attributes @@ -170,17 +271,19 @@ (defun mh-mhn-compose-anon-ftp (host filename type description) "Add a directive for a MIME anonymous ftp external body part. -This directive tells MH to include a reference to a -message/external-body part retrievable by anonymous FTP. Arguments -are HOST and FILENAME, which tell where to find the file, TYPE, the -MIME content type, and DESCRIPTION, a line of text for the -Content-description header. See also \\[mh-edit-mhn]." +This directive tells MH to include a reference to a message/external-body part +retrievable by anonymous FTP. + +Arguments are HOST and FILENAME, which tell where to find the file, TYPE, the +MIME content type, and DESCRIPTION, a line of text for the Content-description +header. + +See also \\[mh-edit-mhn]." (interactive (list (read-string "Remote host: ") (read-string "Remote filename: ") (completing-read "External Content-Type: " - (if (and (require 'mailcap nil t) - (fboundp 'mailcap-mime-types)) + (if (fboundp 'mailcap-mime-types) (mapcar 'list (mailcap-mime-types)) mh-mime-content-types)) (read-string "External Content-Description: "))) @@ -189,10 +292,12 @@ (defun mh-mhn-compose-external-compressed-tar (host filename description) "Add a directive to include a MIME reference to a compressed tar file. -The file should be available via anonymous ftp. This directive -tells MH to include a reference to a message/external-body part. +The file should be available via anonymous ftp. This directive tells MH to +include a reference to a message/external-body part. + Arguments are HOST and FILENAME, which tell where to find the file, and DESCRIPTION, a line of text for the Content-description header. + See also \\[mh-edit-mhn]." (interactive (list (read-string "Remote host: ") @@ -208,7 +313,17 @@ (defun mh-mhn-compose-external-type (access-type host filename type &optional description attributes extra-params comment) - (setq mh-mhn-compose-insert-p t) + "Add a directive to include a MIME reference to a remote file. +The file should be available via anonymous ftp. This directive tells MH to +include a reference to a message/external-body part. + +Arguments are ACCESS-TYPE, HOST and FILENAME, which tell where to find the +file and TYPE which is the MIME Content-Type. Optional arguments include +DESCRIPTION, a line of text for the Content-description header, ATTRIBUTES, +EXTRA-PARAMS, and COMMENT. + +See also \\[mh-edit-mhn]." + (setq mh-mhn-compose-insert-flag t) (beginning-of-line) (insert "#@" type) (and attributes @@ -230,8 +345,10 @@ (defun mh-mhn-compose-forw (&optional description folder messages) "Add a forw directive to this message, to forward a message with MIME. This directive tells MH to include the named messages in this one. + Arguments are DESCRIPTION, a line of text for the Content-description header, and FOLDER and MESSAGES, which name the message(s) to be forwarded. + See also \\[mh-edit-mhn]." (interactive (list (read-string "Forw Content-description: ") @@ -240,7 +357,7 @@ (if mh-sent-from-msg (format " [%d]" mh-sent-from-msg) ""))))) - (setq mh-mhn-compose-insert-p t) + (setq mh-mhn-compose-insert-flag t) (beginning-of-line) (insert "#forw [") (and description @@ -280,23 +397,25 @@ message). If these helper functions are used, `mh-edit-mhn' is run automatically when the draft is sent. +The value of `mh-edit-mhn-hook' is a list of functions to be called, with no +arguments, after performing the conversion. + The mhn program is part of MH version 6.8 or later." (interactive "*P") (save-buffer) (message "mhn editing...") (cond - (mh-nmh-p + (mh-nmh-flag (mh-exec-cmd-error nil "mhbuild" (if extra-args mh-mhn-args) buffer-file-name)) (t (mh-exec-cmd-error (format "mhdraft=%s" buffer-file-name) "mhn" (if extra-args mh-mhn-args) buffer-file-name))) - (setq mh-mhn-compose-insert-p nil) + (setq mh-mhn-compose-insert-flag nil) (revert-buffer t t) (message "mhn editing...done") (run-hooks 'mh-edit-mhn-hook)) - (defun mh-revert-mhn-edit (noconfirm) "Undo the effect of \\[mh-edit-mhn] by reverting to the backup file. Optional non-nil argument NOCONFIRM means don't ask for confirmation." @@ -324,4 +443,857 @@ (insert-file-contents backup-file)) (after-find-file nil))) + + +;;; MIME composition functions + +(defun mh-mml-to-mime () + "Compose MIME message from mml directives." + (interactive) + (when mh-gnus-pgp-support-flag ;; This is only needed for PGP + (message-options-set-recipient)) + (mml-to-mime) + (setq mh-mml-compose-insert-flag nil)) + +(defun mh-mml-forward-message (description folder message) + "Forward a message as attachment. +The function will prompt the user for a DESCRIPTION, a FOLDER and MESSAGE +number." + (let ((msg (if (equal message "") + mh-sent-from-msg + (car (read-from-string message))))) + (cond ((integerp msg) + (if (string= "" description) + ;; Rationale: mml-attach-file constructs a malformed composition + ;; if the description string is empty. This fixes SF #625168. + (mml-attach-file (format "%s%s/%d" + mh-user-path (substring folder 1) msg) + "message/rfc822") + (mml-attach-file (format "%s%s/%d" + mh-user-path (substring folder 1) msg) + "message/rfc822" + description)) + (setq mh-mml-compose-insert-flag t)) + (t (error "The message number, %s is not a integer!" msg))))) + +(defun mh-mml-attach-file (&optional disposition) + "Attach a file to the outgoing MIME message. +The file is not inserted or encoded until you send the message with +`\\[mh-send-letter]'. +Message disposition is \"inline\" or \"attachment\" and is prompted for if +DISPOSITION is nil. + +This is basically `mml-attach-file' from gnus, modified such that a prefix +argument yields an `inline' disposition and Content-Type is determined +automatically." + (let* ((file (mml-minibuffer-read-file "Attach file: ")) + (type (or (mh-file-mime-type file) + (completing-read "Content-Type: " + (if (fboundp 'mailcap-mime-types) + (mapcar 'list (mailcap-mime-types)) + mh-mime-content-types)))) + (description (mml-minibuffer-read-description)) + (dispos (or disposition + (completing-read "Disposition: [attachment] " + '(("attachment")("inline")) + nil t nil nil + "attachment")))) + (mml-insert-empty-tag 'part 'type type 'filename file + 'disposition dispos 'description description) + (setq mh-mml-compose-insert-flag t))) + +(defun mh-mml-secure-message-sign-pgpmime () + "Add directive to encrypt/sign the entire message." + (interactive) + (if (not mh-gnus-pgp-support-flag) + (error "Sorry. Your version of gnus does not support PGP/GPG") + (mml-secure-message-sign-pgpmime) + (setq mh-mml-compose-insert-flag t))) + +(defun mh-mml-secure-message-encrypt-pgpmime (&optional dontsign) + "Add directive to encrypt and sign the entire message. +If called with a prefix argument DONTSIGN, only encrypt (do NOT sign)." + (interactive "P") + (if (not mh-gnus-pgp-support-flag) + (error "Sorry. Your version of gnus does not support PGP/GPG") + (mml-secure-message-encrypt-pgpmime dontsign) + (setq mh-mml-compose-insert-flag t))) + + + +;;; MIME decoding + +(defcustom mh-graphical-smileys-flag t + "*Non-nil means graphical smileys are displayed. +Non-nil means that small graphics will be used in the show buffer instead of +patterns like :-), ;-) etc. The setting only has effect if +`mh-decode-mime-flag' is non-nil." + :type 'boolean + :group 'mh-buffer) + +(defcustom mh-graphical-emphasis-flag t + "*Non-nil means graphical emphasis is displayed. +Non-nil means that _underline_ will be underlined, *bold* will appear in bold, +/italic/ will appear in italic etc. See `gnus-emphasis-alist' for the whole +list. The setting only has effect if `mh-decode-mime-flag' is non-nil." + :type 'boolean + :group 'mh-buffer) + +;; Small image definition +(defcustom mh-max-inline-image-width nil + "*Maximum inline image width if Content-Disposition is not present. +If nil, image will be displayed if its width is smaller than the width of the +window." + :type '(choice (const nil) integer) + :group 'mh-buffer) + +(defcustom mh-max-inline-image-height nil + "*Maximum inline image height if Content-Disposition is not present. +If nil, image will be displayed if its height is smaller than the height of +the window." + :type '(choice (const nil) integer) + :group 'mh-buffer) + +(defcustom mh-display-buttons-for-inline-parts-flag nil + "*Non-nil means display buttons for all inline MIME parts. +If non-nil, buttons are displayed for all MIME parts. Inline parts start off +in displayed state but they can be hidden by clicking the button. If nil no +buttons are shown for inline parts." + :type 'boolean + :group 'mh-buffer) + +(defcustom mh-mime-save-parts-default-directory t + "Default directory to use for `mh-mime-save-parts'. +If nil, prompt and set for next time the command is used during same session. +If t, prompt always" + :type '(choice (const :tag "Prompt the first time" nil) + (const :tag "Prompt always" t) + directory) + :group 'mh) + +(defmacro mh-defun-compat (function arg-list &rest body) + "This is a macro to define functions which are not defined. +It is used for Gnus utility functions which were added recently. If FUNCTION +is not defined then it is defined to have argument list, ARG-LIST and body, +BODY." + (let ((defined-p (fboundp function))) + (unless defined-p + `(defun ,function ,arg-list ,@body)))) + +;; Copy of original function from gnus-util.el +(mh-defun-compat gnus-local-map-property (map) + "Return a list suitable for a text property list specifying keymap MAP." + (cond (mh-xemacs-flag (list 'keymap map)) + ((>= emacs-major-version 21) (list 'keymap map)) + (t (list 'local-map map)))) + +;; Copy of original function from mm-decode.el +(mh-defun-compat mm-merge-handles (handles1 handles2) + (append (if (listp (car handles1)) handles1 (list handles1)) + (if (listp (car handles2)) handles2 (list handles2)))) + +;; Copy of function from mm-decode.el +(mh-defun-compat mm-set-handle-multipart-parameter (handle parameter value) + ;; HANDLE could be a CTL. + (if handle + (put-text-property 0 (length (car handle)) parameter value + (car handle)))) + +;; Copy of original macro is in mm-decode.el +(mh-defun-compat mm-handle-multipart-ctl-parameter (handle parameter) + (get-text-property 0 parameter (car handle))) + +;; Copy of original function in mm-decode.el +(mh-defun-compat mm-readable-p (handle) + "Say whether the content of HANDLE is readable." + (and (< (with-current-buffer (mm-handle-buffer handle) + (buffer-size)) 10000) + (mm-with-unibyte-buffer + (mm-insert-part handle) + (and (eq (mm-body-7-or-8) '7bit) + (not (mm-long-lines-p 76)))))) + +;; Copy of original function in mm-bodies.el +(mh-defun-compat mm-long-lines-p (length) + "Say whether any of the lines in the buffer is longer than LINES." + (save-excursion + (goto-char (point-min)) + (end-of-line) + (while (and (not (eobp)) + (not (> (current-column) length))) + (forward-line 1) + (end-of-line)) + (and (> (current-column) length) + (current-column)))) + +(mh-defun-compat mm-keep-viewer-alive-p (handle) + ;; Released Gnus doesn't keep handles associated with externally displayed + ;; MIME parts. So this will always return nil. + nil) + +(mh-defun-compat mm-destroy-parts (list) + "Older emacs don't have this function." + nil) + +;;; This is mm-save-part from gnus 5.10 since that function in emacs21.2 is +;;; buggy (the args to read-file-name are incorrect). When all supported +;;; versions of Emacs come with at least Gnus 5.10, we can delete this +;;; function and rename calls to mh-mm-save-part to mm-save-part. +(defun mh-mm-save-part (handle) + "Write HANDLE to a file." + (let ((name (mail-content-type-get (mm-handle-type handle) 'name)) + (filename (mail-content-type-get + (mm-handle-disposition handle) 'filename)) + file) + (when filename + (setq filename (file-name-nondirectory filename))) + (setq file (read-file-name "Save MIME part to: " + (or mm-default-directory + default-directory) + nil nil (or filename name ""))) + (setq mm-default-directory (file-name-directory file)) + (and (or (not (file-exists-p file)) + (yes-or-no-p (format "File %s already exists; overwrite? " + file))) + (mm-save-part-to-file handle file)))) + + + +;;; MIME cleanup + +(defun mh-mime-cleanup () + "Free the decoded MIME parts." + (let ((mime-data (gethash (current-buffer) mh-globals-hash))) + ;; This is for Emacs, what about XEmacs? + (cond ((fboundp 'remove-images) + (remove-images (point-min) (point-max)))) + (when mime-data + (mm-destroy-parts (mh-mime-handles mime-data)) + (remhash (current-buffer) mh-globals-hash)))) + +(defun mh-destroy-postponed-handles () + "Free MIME data for externally displayed mime parts." + (let ((mime-data (mh-buffer-data))) + (when mime-data + (mm-destroy-parts (mh-mime-handles mime-data))) + (remhash (current-buffer) mh-globals-hash))) + +(defun mh-handle-set-external-undisplayer (folder handle function) + "Replacement for `mm-handle-set-external-undisplayer'. +This is only called in recent versions of Gnus. The MIME handles are stored +in data structures corresponding to MH-E folder buffer FOLDER instead of in +Gnus (as in the original). The MIME part, HANDLE is associated with the +undisplayer FUNCTION." + (if (mm-keep-viewer-alive-p handle) + (let ((new-handle (copy-sequence handle))) + (mm-handle-set-undisplayer new-handle function) + (mm-handle-set-undisplayer handle nil) + (save-excursion + (set-buffer folder) + (push new-handle (mh-mime-handles (mh-buffer-data))))) + (mm-handle-set-undisplayer handle function))) + + + +;;; MIME transformations + +(defun mh-add-missing-mime-version-header () + "Some mail programs don't put a MIME-Version header. +I have seen this only in spam, so maybe we shouldn't fix this ;-)" + (save-excursion + (goto-char (point-min)) + (when (and (message-fetch-field "content-type") + (not (message-fetch-field "mime-version"))) + (when (search-forward "\n\n" nil t) + (forward-line -1) + (insert "MIME-Version: 1.0\n"))))) + +(defun mh-display-smileys () + "Function to display smileys." + (when (and mh-graphical-smileys-flag (fboundp 'smiley-region)) + (smiley-region (point-min) (point-max)))) + +(defun mh-display-emphasis () + "Function to display graphical emphasis." + (when mh-graphical-emphasis-flag + (flet ((article-goto-body ())) ; shadow this function to do nothing + (save-excursion + (goto-char (point-min)) + (article-emphasize))))) + +;; Copied from gnus-art.el (should be checked for other cool things that can +;; be added to the buttons) +(defvar mh-mime-button-commands + '((mh-press-button "\r" "Toggle Display"))) +(defvar mh-mime-button-map + (let ((map (make-sparse-keymap))) + (unless (>= (string-to-number emacs-version) 21) + ;; XEmacs doesn't care. + (set-keymap-parent map mh-show-mode-map)) + (define-key map [mouse-2] 'mh-push-button) + (dolist (c mh-mime-button-commands) + (define-key map (cadr c) (car c))) + map)) +(defvar mh-mime-button-line-format-alist + '((?T long-type ?s) + (?d description ?s) + (?p index ?s) + (?e dots ?s))) +(defvar mh-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n") +(defvar mh-mime-security-button-pressed nil) +(defvar mh-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n") +(defvar mh-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n") +(defvar mh-mime-security-button-line-format-alist + '((?t type ?s) + (?i info ?s) + (?d details ?s) + (?D pressed-details ?s))) +(defvar mh-mime-security-button-map + (let ((map (make-sparse-keymap))) + (unless (>= (string-to-number emacs-version) 21) + (set-keymap-parent map mh-show-mode-map)) + (define-key map "\r" 'mh-press-button) + (define-key map [mouse-2] 'mh-push-button) + map)) + +(defvar mh-mime-save-parts-directory nil + "Default to use for `mh-mime-save-parts-default-directory'. +Set from last use.") + +(defun mh-mime-save-parts (arg) + "Store the MIME parts of the current message. +If ARG, prompt for directory, else use that specified by the variable +`mh-mime-save-parts-default-directory'. These directories may be superseded by +mh_profile directives, since this function calls on mhstore or mhn to do the +actual storing." + (interactive "P") + (let ((msg (if (eq major-mode 'mh-show-mode) + (mh-show-buffer-message-number) + (mh-get-msg-num t))) + (folder (if (eq major-mode 'mh-show-mode) + mh-show-folder-buffer + mh-current-folder)) + (command (if mh-nmh-flag "mhstore" "mhn")) + (directory + (cond + ((and (or arg + (equal nil mh-mime-save-parts-default-directory) + (equal t mh-mime-save-parts-default-directory)) + (not mh-mime-save-parts-directory)) + (read-file-name "Store in what directory? " nil nil t nil)) + ((and (or arg + (equal t mh-mime-save-parts-default-directory)) + mh-mime-save-parts-directory) + (read-file-name (format + "Store in what directory? [%s] " + mh-mime-save-parts-directory) + "" mh-mime-save-parts-directory t "")) + ((stringp mh-mime-save-parts-default-directory) + mh-mime-save-parts-default-directory) + (t + mh-mime-save-parts-directory)))) + (if (and (equal directory "") mh-mime-save-parts-directory) + (setq directory mh-mime-save-parts-directory)) + (if (not (file-directory-p directory)) + (message "No directory specified.") + (if (equal nil mh-mime-save-parts-default-directory) + (setq mh-mime-save-parts-directory directory)) + (save-excursion + (set-buffer (get-buffer-create " *mh-store*")) + (cd directory) + (setq mh-mime-save-parts-directory directory) + (erase-buffer) + (apply 'call-process + (expand-file-name command mh-progs) nil t nil + (mh-list-to-string (list folder msg "-auto"))) + (if (> (buffer-size) 0) + (save-window-excursion + (switch-to-buffer-other-window " *mh-store*") + (sit-for 3))))))) + +;; Avoid errors if gnus-sum isn't loaded yet... +(defvar gnus-newsgroup-charset nil) +(defvar gnus-newsgroup-name nil) + +(defun mh-mime-display (&optional pre-dissected-handles) + "Display (and possibly decode) MIME handles. +Optional argument, PRE-DISSECTED-HANDLES is a list of MIME handles. If +present they are displayed otherwise the buffer is parsed and then +displayed." + (let ((handles ()) + (folder mh-show-folder-buffer)) + (flet ((mm-handle-set-external-undisplayer (handle function) + (mh-handle-set-external-undisplayer folder handle function))) + ;; If needed dissect the current buffer + (if pre-dissected-handles + (setq handles pre-dissected-handles) + (setq handles (or (mm-dissect-buffer nil) (mm-uu-dissect))) + (setf (mh-mime-handles (mh-buffer-data)) + (mm-merge-handles handles (mh-mime-handles (mh-buffer-data))))) + + (when (and handles (or (not (stringp (car handles))) (cdr handles))) + ;; Goto start of message body + (goto-char (point-min)) + (or (search-forward "\n\n" nil t) (goto-char (point-max))) + + ;; Delete the body + (delete-region (point) (point-max)) + + ;; Display the MIME handles + (mh-mime-display-part handles))))) + +(defun mh-mime-display-part (handle) + "Decides the viewer to call based on the type of HANDLE." + (cond ((null handle) nil) + ((not (stringp (car handle))) + (mh-mime-display-single handle)) + ((equal (car handle) "multipart/alternative") + (mh-mime-display-alternative (cdr handle))) + ((and mh-gnus-pgp-support-flag + (or (equal (car handle) "multipart/signed") + (equal (car handle) "multipart/encrypted"))) + (mh-mime-display-security handle)) + (t (mh-mime-display-mixed (cdr handle))))) + +(defun mh-mime-display-alternative (handles) + "Choose among the alternatives, HANDLES the part that will be displayed. +If no part is preferred then all the parts are displayed." + (let ((preferred (mm-preferred-alternative handles))) + (cond ((and preferred (stringp (car preferred))) + (mh-mime-display-part preferred)) + (preferred + (save-restriction + (narrow-to-region (point) (if (eobp) (point) (1+ (point)))) + (or (mm-display-part preferred) (mm-display-part preferred)) + (goto-char (point-max)))) + (t (mh-mime-display-mixed handles))))) + +(defun mh-mime-display-mixed (handles) + "Display the list of MIME parts, HANDLES recursively." + (mapcar #'mh-mime-display-part handles)) + +(defun mh-mime-part-index (handle) + "Generate the button number for MIME part, HANDLE. +Notice that a hash table is used to display the same number when buttons need +to be displayed multiple times (for instance when nested messages are +opened)." + (or (gethash handle (mh-mime-part-index-hash (mh-buffer-data))) + (setf (gethash handle (mh-mime-part-index-hash (mh-buffer-data))) + (incf (mh-mime-parts-count (mh-buffer-data)))))) + +;;; Avoid compiler warnings for XEmacs functions... +(eval-when (compile) + (loop for function in '(glyph-width window-pixel-width + glyph-height window-pixel-height) + do (or (fboundp function) (defalias function 'ignore)))) + +(defun mh-small-image-p (handle) + "Decide whether HANDLE is a \"small\" image that can be displayed inline. +This is only useful if a Content-Disposition header is not present." + (let ((media-test (caddr (assoc (car (mm-handle-type handle)) + mh-mm-inline-media-tests))) + (mm-inline-large-images t)) + (and media-test + (equal (mm-handle-media-supertype handle) "image") + (funcall media-test handle) ; Since mm-inline-large-images is T, + ; this only tells us if the image is + ; something that emacs can display + (let* ((image (mm-get-image handle))) + (cond ((fboundp 'glyph-width) + ;; XEmacs -- totally untested, copied from gnus + (and (< (glyph-width image) + (or mh-max-inline-image-width + (window-pixel-width))) + (< (glyph-height image) + (or mh-max-inline-image-height + (window-pixel-height))))) + ((fboundp 'image-size) + ;; Emacs21 -- copied from gnus + (let ((size (image-size image))) + (and (< (cdr size) + (or mh-max-inline-image-height + (1- (window-height)))) + (< (car size) + (or mh-max-inline-image-width (window-width)))))) + (t + ;; Can't show image inline + nil)))))) + +(defun mh-mime-display-single (handle) + "Display a leaf node, HANDLE in the MIME tree." + (let* ((type (mm-handle-media-type handle)) + (small-image-flag (mh-small-image-p handle)) + (attachmentp (equal (car (mm-handle-disposition handle)) + "attachment")) + (inlinep (and (equal (car (mm-handle-disposition handle)) "inline") + (mm-inlinable-p handle) + (mm-inlined-p handle))) + (displayp (or inlinep ; display if inline + (and (not attachmentp) ; if it is not an attachment + (or small-image-flag ; display if small image + ; or if user wants inline. + (and (not (equal + (mm-handle-media-supertype handle) + "image")) + (mm-inlinable-p handle) + (mm-inlined-p handle))))))) + (save-restriction + (narrow-to-region (point) (if (eobp) (point) (1+ (point)))) + (cond ((and mh-gnus-pgp-support-flag + (equal type "application/pgp-signature")) + nil) ; skip signatures as they are already handled... + ((not displayp) + (insert "\n") + (mh-insert-mime-button handle (mh-mime-part-index handle) nil)) + ((and displayp (not mh-display-buttons-for-inline-parts-flag)) + (or (mm-display-part handle) (mm-display-part handle))) + ((and displayp mh-display-buttons-for-inline-parts-flag) + (insert "\n") + (mh-insert-mime-button handle (mh-mime-part-index handle) nil) + (forward-line -1) + (mh-mm-display-part handle))) + (goto-char (point-max))))) + +(defun mh-insert-mime-button (handle index displayed) + "Insert MIME button for HANDLE. +INDEX is the part number that will be DISPLAYED. It is also used by commands +like \"K v\" which operate on individual MIME parts." + ;; The button could be displayed by a previous decode. In that case + ;; undisplay it if we need a hidden button. + (when (and (mm-handle-displayed-p handle) (not displayed)) + (mm-display-part handle)) + (let ((name (or (mail-content-type-get (mm-handle-type handle) 'name) + (mail-content-type-get (mm-handle-disposition handle) + 'filename) + (mail-content-type-get (mm-handle-type handle) 'url) + "")) + (type (mm-handle-media-type handle)) + (description (mail-decode-encoded-word-string + (or (mm-handle-description handle) ""))) + (dots (if (or displayed (mm-handle-displayed-p handle)) " " "...")) + long-type begin end) + (if (string-match ".*/" name) (setq name (substring name (match-end 0)))) + (setq long-type (concat type (and (not (equal name "")) + (concat "; " name)))) + (unless (equal description "") + (setq long-type (concat " --- " long-type))) + (unless (bolp) (insert "\n")) + (setq begin (point)) + (gnus-eval-format + mh-mime-button-line-format mh-mime-button-line-format-alist + `(,@(gnus-local-map-property mh-mime-button-map) + mh-callback mh-mm-display-part + mh-part ,index + mh-data ,handle)) + (setq end (point)) + (widget-convert-button + 'link begin end + :mime-handle handle + :action 'mh-widget-press-button + :button-keymap mh-mime-button-map + :help-echo + "Mouse-2 click or press RET (in show buffer) to toggle display"))) + +;; There is a bug in Gnus inline image display due to which an extra line +;; gets inserted every time it is viewed. To work around that problem we are +;; using an extra property 'mh-region to remember the region that is added +;; when the button is clicked. The region is then deleted to make sure that +;; no extra lines get inserted. +(defun mh-mm-display-part (handle) + "Toggle display of button for MIME part, HANDLE." + (beginning-of-line) + (let ((id (get-text-property (point) 'mh-part)) + (point (point)) + (window (selected-window)) + (mail-parse-charset 'nil) + (mail-parse-ignored-charsets nil) + region buffer-read-only) + (save-excursion + (unwind-protect + (let ((win (get-buffer-window (current-buffer) t))) + (when win + (select-window win)) + (goto-char point) + + (if (mm-handle-displayed-p handle) + ;; This will remove the part. + (progn + ;; Delete the button and displayed part (if any) + (let ((region (get-text-property point 'mh-region))) + (when region + (when (fboundp 'remove-images) + (remove-images (car region) (cdr region)))) + (mm-display-part handle) + (when region + (delete-region (car region) (cdr region)))) + ;; Delete button (if it still remains). This happens for + ;; externally displayed parts where the previous step does + ;; nothing. + (unless (eolp) + (delete-region (point) (progn (forward-line) (point))))) + (save-restriction + (delete-region (point) (progn (forward-line 1) (point))) + (narrow-to-region (point) (point)) + ;; Maybe we need another unwind-protect here. + (when (equal (mm-handle-media-supertype handle) "image") + (insert "\n")) + (when (and (not (eq (ignore-errors (mm-display-part handle)) + 'inline)) + (equal (mm-handle-media-supertype handle) + "image")) + (goto-char (point-min)) + (delete-char 1)) + (when (equal (mm-handle-media-supertype handle) "text") + (when (eq mh-highlight-citation-p 'gnus) + (mh-gnus-article-highlight-citation)) + (mh-display-smileys) + (mh-display-emphasis)) + (setq region (cons (progn (goto-char (point-min)) + (point-marker)) + (progn (goto-char (point-max)) + (point-marker))))))) + (when (window-live-p window) + (select-window window)) + (goto-char point) + (beginning-of-line) + (mh-insert-mime-button handle id (mm-handle-displayed-p handle)) + (goto-char point) + (when region + (add-text-properties (line-beginning-position) (line-end-position) + `(mh-region ,region))))))) + +(defun mh-press-button () + "Press MIME button. +If the MIME part is visible then it is removed. Otherwise the part is +displayed." + (interactive) + (let ((mm-inline-media-tests mh-mm-inline-media-tests) + (data (get-text-property (point) 'mh-data)) + (function (get-text-property (point) 'mh-callback)) + (buffer-read-only nil) + (folder mh-show-folder-buffer)) + (flet ((mm-handle-set-external-undisplayer (handle function) + (mh-handle-set-external-undisplayer folder handle function))) + (when (and function (eolp)) + (backward-char)) + (unwind-protect (and function (funcall function data)) + (set-buffer-modified-p nil))))) + +(defun mh-push-button (event) + "Click MIME button for EVENT. +If the MIME part is visible then it is removed. Otherwise the part is +displayed. This function is called when the mouse is used to click the MIME +button." + (interactive "e") + (set-buffer (window-buffer (posn-window (event-start event)))) + (select-window (posn-window (event-start event))) + (let* ((pos (posn-point (event-start event))) + (folder mh-show-folder-buffer) + (mm-inline-media-tests mh-mm-inline-media-tests) + (data (get-text-property pos 'mh-data)) + (function (get-text-property pos 'mh-callback)) + (buffer-read-only nil)) + (flet ((mm-handle-set-external-undisplayer (handle function) + (mh-handle-set-external-undisplayer folder handle function))) + (goto-char pos) + (unwind-protect (and function (funcall function data)) + (set-buffer-modified-p nil))))) + +(defun mh-mime-save-part () + "Save MIME part at point." + (interactive) + (let ((data (get-text-property (point) 'mh-data))) + (when data + (let ((mm-default-directory mh-mime-save-parts-directory)) + (mh-mm-save-part data) + (setq mh-mime-save-parts-directory mm-default-directory))))) + +(defun mh-mime-inline-part () + "Toggle display of the raw MIME part." + (interactive) + (let* ((buffer-read-only nil) + (data (get-text-property (point) 'mh-data)) + (inserted-flag (get-text-property (point) 'mh-mime-inserted)) + (displayed-flag (mm-handle-displayed-p data)) + (point (point)) + start end) + (cond ((and data (not inserted-flag) (not displayed-flag)) + (let ((contents (mm-get-part data))) + (add-text-properties (line-beginning-position) (line-end-position) + '(mh-mime-inserted t)) + (setq start (point-marker)) + (forward-line 1) + (mm-insert-inline data contents) + (setq end (point-marker)) + (add-text-properties + start (progn (goto-char start) (line-end-position)) + `(mh-region (,start . ,end))))) + ((and data (or inserted-flag displayed-flag)) + (mh-press-button) + (message "MIME part already inserted"))) + (goto-char point) + (set-buffer-modified-p nil))) + +(defun mh-widget-press-button (widget el) + "Callback for widget, WIDGET. +Parameter EL is unused." + (goto-char (widget-get widget :from)) + (mh-press-button)) + +(defun mh-mime-display-security (handle) + "Display PGP encrypted/signed message, HANDLE." + (insert "\n") + (save-restriction + (narrow-to-region (point) (point)) + (mh-insert-mime-security-button handle) + (mh-mime-display-mixed (cdr handle)) + (insert "\n") + (let ((mh-mime-security-button-line-format + mh-mime-security-button-end-line-format)) + (mh-insert-mime-security-button handle)) + (mm-set-handle-multipart-parameter + handle 'mh-region + (cons (set-marker (make-marker) (point-min)) + (set-marker (make-marker) (point-max)))))) + +;;; I rewrote the security part because Gnus doesn't seem to ever minimize +;;; the button. That is once the mime-security button is pressed there seems +;;; to be no way of getting rid of the inserted text. +(defun mh-mime-security-show-details (handle) + "Toggle display of detailed security info for HANDLE." + (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details))) + (when details + (let ((mh-mime-security-button-pressed + (not (get-text-property (point) 'mh-button-pressed))) + (mh-mime-security-button-line-format + (get-text-property (point) 'mh-line-format))) + (forward-char -1) + (while (eq (get-text-property (point) 'mh-line-format) + mh-mime-security-button-line-format) + (forward-char -1)) + (forward-char) + (save-restriction + (narrow-to-region (point) (point)) + (mh-insert-mime-security-button handle)) + (delete-region + (point) + (or (text-property-not-all + (point) (point-max) + 'mh-line-format mh-mime-security-button-line-format) + (point-max))) + (forward-line -1))))) + +(defun mh-mime-security-press-button (handle) + "Callback from security button for part HANDLE." + (when (mm-handle-multipart-ctl-parameter handle 'gnus-info) + (mh-mime-security-show-details handle))) + +;; These variables should already be initialized in mm-decode.el if we have a +;; recent enough Gnus. The defvars are here to avoid compiler warnings. +(defvar mm-verify-function-alist nil) +(defvar mm-decrypt-function-alist nil) + +(defvar pressed-details) + +(defun mh-insert-mime-security-button (handle) + "Display buttons for PGP message, HANDLE." + (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol)) + (crypto-type (or (nth 2 (assoc protocol mm-verify-function-alist)) + (nth 2 (assoc protocol mm-decrypt-function-alist)) + "Unknown")) + (type (concat crypto-type + (if (equal (car handle) "multipart/signed") + " Signed" " Encrypted") + " Part")) + (info (or (mm-handle-multipart-ctl-parameter handle 'gnus-info) + "Undecided")) + (details (mm-handle-multipart-ctl-parameter handle 'gnus-details)) + pressed-details begin end) + (setq details (if details (concat "\n" details) "")) + (setq pressed-details (if mh-mime-security-button-pressed details "")) + (unless (bolp) (insert "\n")) + (setq begin (point)) + (gnus-eval-format + mh-mime-security-button-line-format + mh-mime-security-button-line-format-alist + `(,@(gnus-local-map-property mh-mime-security-button-map) + mh-button-pressed ,mh-mime-security-button-pressed + mh-callback mh-mime-security-press-button + mh-line-format ,mh-mime-security-button-line-format + mh-data ,handle)) + (setq end (point)) + (widget-convert-button 'link begin end + :mime-handle handle + :action 'mh-widget-press-button + :button-keymap mh-mime-security-button-map + :help-echo "Mouse-2 click or press RET (in show buffer) to see security details.") + (when (equal info "Failed") + (let* ((type (if (equal (car handle) "multipart/signed") + "verification" "decryption")) + (warning (if (equal type "decryption") + "(passphrase may be incorrect)" ""))) + (message "%s %s failed %s" crypto-type type warning))))) + +(defun mh-mm-inline-message (handle) + "Display message, HANDLE. +The function decodes the message and displays it. It avoids decoding the same +message multiple times." + (let ((b (point)) + (charset (mail-content-type-get (mm-handle-type handle) 'charset)) + (clean-message-header mh-clean-message-header-flag) + (invisible-headers mh-invisible-headers) + (visible-headers mh-visible-headers)) + (when (and charset (stringp charset)) + (setq charset (intern (downcase charset))) + (when (eq charset 'us-ascii) + (setq charset nil))) + (save-excursion + (save-restriction + (narrow-to-region b b) + (mm-insert-part handle) + (mh-mime-display + (or (gethash handle (mh-mime-handles-cache (mh-buffer-data))) + (setf (gethash handle (mh-mime-handles-cache (mh-buffer-data))) + (let ((handles (or (mm-dissect-buffer nil) + (mm-uu-dissect)))) + (setf (mh-mime-handles (mh-buffer-data)) + (mm-merge-handles + handles (mh-mime-handles (mh-buffer-data)))) + handles)))) + + (goto-char (point-min)) + (cond (clean-message-header + (mh-clean-msg-header (point-min) + invisible-headers + visible-headers) + (goto-char (point-min))) + (t + (mh-start-of-uncleaned-message))) + (mh-show-xface) + (mh-show-addr) + ;; The other highlighting types don't need anything special + (when (eq mh-highlight-citation-p 'gnus) + (mh-gnus-article-highlight-citation)) + (goto-char (point-min)) + (insert "\n------- Forwarded Message\n\n") + (mh-display-smileys) + (mh-display-emphasis) + (mm-handle-set-undisplayer + handle + `(lambda () + (let (buffer-read-only) + (if (fboundp 'remove-specifier) + ;; This is only valid on XEmacs. + (mapcar (lambda (prop) + (remove-specifier + (face-property 'default prop) (current-buffer))) + '(background background-pixmap foreground))) + (delete-region ,(point-min-marker) ,(point-max-marker))))))))) + +(provide 'mh-mime) + +;;; Local Variables: +;;; sentence-end-double-space: nil +;;; End: + ;;; mh-mime.el ends here