1 ;;; mh-mime.el --- MH-E support for composing MIME messages
3 ;; Copyright (C) 1993, 1995, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Internal support for MH-E package.
30 ;; Support for generating an mhn composition file.
31 ;; MIME is supported only by MH 6.8 or later.
35 ;; $Id: mh-mime.el,v 1.100 2003/01/25 19:18:51 satyaki Exp $
42 (load "mm-decode" t t
) ; Non-fatal dependency
43 (load "mm-uu" t t
) ; Non-fatal dependency
44 (load "mailcap" t t
) ; Non-fatal dependency
45 (load "smiley" t t
) ; Non-fatal dependency
48 (autoload 'gnus-article-goto-header
"gnus-art")
49 (autoload 'article-emphasize
"gnus-art")
50 (autoload 'gnus-get-buffer-create
"gnus")
51 (autoload 'gnus-eval-format
"gnus-spec")
52 (autoload 'widget-convert-button
"wid-edit")
53 (autoload 'message-options-set-recipient
"message")
54 (autoload 'mml-secure-message-sign-pgpmime
"mml-sec")
55 (autoload 'mml-secure-message-encrypt-pgpmime
"mml-sec")
56 (autoload 'mml-minibuffer-read-file
"mml")
57 (autoload 'mml-minibuffer-read-description
"mml")
58 (autoload 'mml-insert-empty-tag
"mml")
59 (autoload 'mml-to-mime
"mml")
60 (autoload 'mml-attach-file
"mml")
63 (defun mh-compose-insertion (&optional inline
)
64 "Add a directive to insert a MIME part from a file, using mhn or gnus.
65 If the variable `mh-compose-insertion' is set to 'mhn, then that will be used.
66 If it is set to 'gnus, then that will be used instead.
67 Optional argument INLINE means make it an inline attachment."
69 (if (equal mh-compose-insertion
'gnus
)
71 (mh-mml-attach-file "inline")
73 (call-interactively 'mh-mhn-compose-insertion
)))
76 (defun mh-compose-forward (&optional description folder message
)
77 "Add a MIME directive to forward a message, using mhn or gnus.
78 If the variable `mh-compose-insertion' is set to 'mhn, then that will be used.
79 If it is set to 'gnus, then that will be used instead.
80 Optional argument DESCRIPTION is a description of the attachment.
81 Optional argument FOLDER is the folder from which the forwarded message should
83 Optional argument MESSAGE is the message to forward.
84 If any of the optional arguments are absent, they are prompted for."
86 (read-string "Forw Content-description: ")
87 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil
)
88 (read-string (format "Messages%s: "
90 (format " [%d]" mh-sent-from-msg
)
92 (if (equal mh-compose-insertion
'gnus
)
93 (mh-mml-forward-message description folder message
)
94 (mh-mhn-compose-forw description folder message
)))
97 ;; paragraph code should not fill # lines if MIME enabled.
98 ;; implement mh-auto-edit-mhn (if non-nil, \\[mh-send-letter]
99 ;; invokes mh-edit-mhn automatically before sending.)
100 ;; actually, instead of mh-auto-edit-mhn,
101 ;; should read automhnproc from profile
102 ;; MIME option to mh-forward
103 ;; command to move to content-description insertion point
105 (defvar mh-mhn-args nil
106 "Extra arguments to have \\[mh-edit-mhn] pass to the \"mhn\" command.
107 The arguments are passed to mhn if \\[mh-edit-mhn] is given a
108 prefix argument. Normally default arguments to mhn are specified in the
111 (defvar mh-media-type-regexp
112 (concat (regexp-opt '("text" "image" "audio" "video" "application"
113 "multipart" "message") t
)
115 "Regexp matching valid media types used in MIME attachment compositions.")
117 ;; Just defvar the variable to avoid compiler warning... This doesn't bind
118 ;; the variable, so things should work exactly as before.
119 (defvar mh-have-file-command
)
121 (defun mh-have-file-command ()
122 "Return t if 'file' command is on the system.
123 'file -i' is used to get MIME type of composition insertion."
124 (when (not (boundp 'mh-have-file-command
))
125 (load "executable" t t
) ; executable-find not autoloaded in emacs20
126 (setq mh-have-file-command
127 (and (fboundp 'executable-find
)
128 (executable-find "file") ; file command exists
129 ; and accepts -i and -b args.
130 (zerop (call-process "file" nil nil nil
"-i" "-b"
131 (expand-file-name "inc" mh-progs
))))))
132 mh-have-file-command
)
134 (defvar mh-file-mime-type-substitutions
135 '(("application/msword" "\.xls" "application/ms-excel")
136 ("application/msword" "\.ppt" "application/ms-powerpoint"))
137 "Substitutions to make for Content-Type returned from file command.
138 The first element is the Content-Type returned by the file command.
139 The second element is a regexp matching the file name, usually the extension.
140 The third element is the Content-Type to replace with.")
142 (defun mh-file-mime-type-substitute (content-type filename
)
143 "Return possibly changed CONTENT-TYPE on the FILENAME.
144 Substitutions are made from the `mh-file-mime-type-substitutions' variable."
145 (let ((subst mh-file-mime-type-substitutions
)
146 (type) (match) (answer content-type
)
147 (case-fold-search t
))
149 (setq type
(car (car subst
))
150 match
(elt (car subst
) 1))
151 (if (and (string-equal content-type type
)
152 (string-match match filename
))
153 (setq answer
(elt (car subst
) 2)
155 (setq subst
(cdr subst
))))
158 (defun mh-file-mime-type (filename)
159 "Return MIME type of FILENAME from file command.
160 Returns nil if file command not on system."
162 ((not (mh-have-file-command))
163 nil
) ;No file command, exit now.
164 ((not (and (file-exists-p filename
)(file-readable-p filename
)))
168 (let ((tmp-buffer (get-buffer-create mh-temp-buffer
)))
169 (set-buffer tmp-buffer
)
172 (call-process "file" nil
'(t nil
) nil
"-b" "-i"
173 (expand-file-name filename
))
174 (goto-char (point-min))
175 (if (not (re-search-forward mh-media-type-regexp nil t
))
177 (mh-file-mime-type-substitute (match-string 0) filename
)))
178 (kill-buffer tmp-buffer
)))))))
180 ;;; This is needed for Emacs20 which doesn't have mailcap-mime-types.
181 (defvar mh-mime-content-types
182 '(("application/mac-binhex40") ("application/msword")
183 ("application/octet-stream") ("application/pdf") ("application/pgp-keys")
184 ("application/pgp-signature") ("application/pkcs7-signature")
185 ("application/postscript") ("application/rtf")
186 ("application/vnd.ms-excel") ("application/vnd.ms-powerpoint")
187 ("application/vnd.ms-project") ("application/vnd.ms-tnef")
188 ("application/wordperfect5.1") ("application/wordperfect6.0")
191 ("audio/basic") ("audio/mpeg")
193 ("image/gif") ("image/jpeg") ("image/png")
195 ("message/delivery-status")
196 ("message/external-body") ("message/partial") ("message/rfc822")
198 ("text/enriched") ("text/html") ("text/plain") ("text/rfc822-headers")
199 ("text/richtext") ("text/xml")
201 ("video/mpeg") ("video/quicktime"))
202 "Legal MIME content types.
203 See documentation for \\[mh-edit-mhn].")
206 (defun mh-mhn-compose-insertion (filename type description attributes
)
207 "Add a directive to insert a MIME message part from a file.
208 This is the typical way to insert non-text parts in a message.
210 Arguments are FILENAME, which tells where to find the file, TYPE, the MIME
211 content type, DESCRIPTION, a line of text for the Content-Description field.
212 ATTRIBUTES is a comma separated list of name=value pairs that is appended to
213 the Content-Type field of the attachment.
215 See also \\[mh-edit-mhn]."
216 (interactive (let ((filename (read-file-name "Insert contents of: ")))
219 (or (mh-file-mime-type filename
)
220 (completing-read "Content-Type: "
221 (if (fboundp 'mailcap-mime-types
)
222 (mapcar 'list
(mailcap-mime-types))
223 mh-mime-content-types
)))
224 (read-string "Content-Description: ")
225 (read-string "Content-Attributes: "
227 (file-name-nondirectory filename
)
229 (mh-mhn-compose-type filename type description attributes
))
231 (defun mh-mhn-compose-type (filename type
232 &optional description attributes comment
)
233 "Insert a mhn directive to insert a file.
235 The file specified by FILENAME is encoded as TYPE. An optional DESCRIPTION is
236 used as the Content-Description field, optional set of ATTRIBUTES and an
237 optional COMMENT can also be included."
238 (setq mh-mhn-compose-insert-flag t
)
242 (insert "; " attributes
))
244 (insert " (" comment
")"))
247 (insert description
))
248 (insert "] " (expand-file-name filename
))
253 (defun mh-mhn-compose-anon-ftp (host filename type description
)
254 "Add a directive for a MIME anonymous ftp external body part.
255 This directive tells MH to include a reference to a message/external-body part
256 retrievable by anonymous FTP.
258 Arguments are HOST and FILENAME, which tell where to find the file, TYPE, the
259 MIME content type, and DESCRIPTION, a line of text for the Content-description
262 See also \\[mh-edit-mhn]."
264 (read-string "Remote host: ")
265 (read-string "Remote filename: ")
266 (completing-read "External Content-Type: "
267 (if (fboundp 'mailcap-mime-types
)
268 (mapcar 'list
(mailcap-mime-types))
269 mh-mime-content-types
))
270 (read-string "External Content-Description: ")))
271 (mh-mhn-compose-external-type "anon-ftp" host filename
275 (defun mh-mhn-compose-external-compressed-tar (host filename description
)
276 "Add a directive to include a MIME reference to a compressed tar file.
277 The file should be available via anonymous ftp. This directive tells MH to
278 include a reference to a message/external-body part.
280 Arguments are HOST and FILENAME, which tell where to find the file, and
281 DESCRIPTION, a line of text for the Content-description header.
283 See also \\[mh-edit-mhn]."
285 (read-string "Remote host: ")
286 (read-string "Remote filename: ")
287 (read-string "Tar file Content-description: ")))
288 (mh-mhn-compose-external-type "anon-ftp" host filename
289 "application/octet-stream"
291 "type=tar; conversions=x-compress"
295 (defun mh-mhn-compose-external-type (access-type host filename type
296 &optional description
297 attributes extra-params
299 "Add a directive to include a MIME reference to a remote file.
300 The file should be available via anonymous ftp. This directive tells MH to
301 include a reference to a message/external-body part.
303 Arguments are ACCESS-TYPE, HOST and FILENAME, which tell where to find the
304 file and TYPE which is the MIME Content-Type. Optional arguments include
305 DESCRIPTION, a line of text for the Content-description header, ATTRIBUTES,
306 EXTRA-PARAMS, and COMMENT.
308 See also \\[mh-edit-mhn]."
309 (setq mh-mhn-compose-insert-flag t
)
313 (insert "; " attributes
))
315 (insert " (" comment
") "))
318 (insert description
))
320 (insert "access-type=" access-type
"; ")
321 (insert "site=" host
)
322 (insert "; name=" (file-name-nondirectory filename
))
323 (insert "; directory=\"" (file-name-directory filename
) "\"")
325 (insert "; " extra-params
))
329 (defun mh-mhn-compose-forw (&optional description folder messages
)
330 "Add a forw directive to this message, to forward a message with MIME.
331 This directive tells MH to include the named messages in this one.
333 Arguments are DESCRIPTION, a line of text for the Content-description header,
334 and FOLDER and MESSAGES, which name the message(s) to be forwarded.
336 See also \\[mh-edit-mhn]."
338 (read-string "Forw Content-description: ")
339 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil
)
340 (read-string (format "Messages%s: "
342 (format " [%d]" mh-sent-from-msg
)
344 (setq mh-mhn-compose-insert-flag t
)
348 (not (string= description
""))
349 (insert description
))
352 (not (string= folder
""))
355 (not (string= messages
"")))
356 (let ((start (point)))
357 (insert " " messages
)
358 (subst-char-in-region start
(point) ?
, ?
))
360 (insert " " (int-to-string mh-sent-from-msg
))))
364 (defun mh-edit-mhn (&optional extra-args
)
365 "Format the current draft for MIME, expanding any mhn directives.
367 Process the current draft with the mhn program, which, using directives
368 already inserted in the draft, fills in all the MIME components and header
371 This step should be done last just before sending the message.
373 The `\\[mh-revert-mhn-edit]' command undoes this command. The arguments in the
374 list `mh-mhn-args' are passed to mhn if this function is passed an optional
375 prefix argument EXTRA-ARGS.
377 For assistance with creating mhn directives to insert various types of
378 components in a message, see \\[mh-mhn-compose-insertion] (generic insertion
379 from a file), \\[mh-mhn-compose-anon-ftp] (external reference to file via
380 anonymous ftp), \\[mh-mhn-compose-external-compressed-tar] \ \(reference to
381 compressed tar file via anonymous ftp), and \\[mh-mhn-compose-forw] (forward
382 message). If these helper functions are used, `mh-edit-mhn' is run
383 automatically when the draft is sent.
385 The value of `mh-edit-mhn-hook' is a list of functions to be called, with no
386 arguments, after performing the conversion.
388 The mhn program is part of MH version 6.8 or later."
391 (message "mhn editing...")
394 (mh-exec-cmd-error nil
395 "mhbuild" (if extra-args mh-mhn-args
) buffer-file-name
))
397 (mh-exec-cmd-error (format "mhdraft=%s" buffer-file-name
)
398 "mhn" (if extra-args mh-mhn-args
) buffer-file-name
)))
399 (setq mh-mhn-compose-insert-flag nil
)
401 (message "mhn editing...done")
402 (run-hooks 'mh-edit-mhn-hook
))
405 (defun mh-revert-mhn-edit (noconfirm)
406 "Undo the effect of \\[mh-edit-mhn] by reverting to the backup file.
407 Optional non-nil argument NOCONFIRM means don't ask for confirmation."
409 (if (null buffer-file-name
)
410 (error "Buffer does not seem to be associated with any file"))
411 (let ((backup-strings '("," "#"))
413 (while (and backup-strings
416 (concat (file-name-directory buffer-file-name
)
418 (file-name-nondirectory buffer-file-name
)
420 (setq backup-strings
(cdr backup-strings
)))
422 (error "Backup file for %s no longer exists!" buffer-file-name
))
424 (yes-or-no-p (format "Revert buffer from file %s? "
426 (error "Revert not confirmed"))
427 (let ((buffer-read-only nil
))
429 (insert-file-contents backup-file
))
430 (after-find-file nil
)))
434 ;;; MIME composition functions
437 (defun mh-mml-to-mime ()
438 "Compose MIME message from mml directives."
440 (when mh-gnus-pgp-support-flag
;; This is only needed for PGP
441 (message-options-set-recipient))
443 (setq mh-mml-compose-insert-flag nil
))
446 (defun mh-mml-forward-message (description folder message
)
447 "Forward a message as attachment.
448 The function will prompt the user for a DESCRIPTION, a FOLDER and MESSAGE
450 (let ((msg (if (equal message
"")
452 (car (read-from-string message
)))))
453 (cond ((integerp msg
)
454 (if (string= "" description
)
455 ;; Rationale: mml-attach-file constructs a malformed composition
456 ;; if the description string is empty. This fixes SF #625168.
457 (mml-attach-file (format "%s%s/%d"
458 mh-user-path
(substring folder
1) msg
)
460 (mml-attach-file (format "%s%s/%d"
461 mh-user-path
(substring folder
1) msg
)
464 (setq mh-mml-compose-insert-flag t
))
465 (t (error "The message number, %s is not a integer!" msg
)))))
468 (defun mh-mml-attach-file (&optional disposition
)
469 "Attach a file to the outgoing MIME message.
470 The file is not inserted or encoded until you send the message with
471 `\\[mh-send-letter]'.
472 Message disposition is \"inline\" or \"attachment\" and is prompted for if
475 This is basically `mml-attach-file' from gnus, modified such that a prefix
476 argument yields an `inline' disposition and Content-Type is determined
478 (let* ((file (mml-minibuffer-read-file "Attach file: "))
479 (type (or (mh-file-mime-type file
)
480 (completing-read "Content-Type: "
481 (if (fboundp 'mailcap-mime-types
)
482 (mapcar 'list
(mailcap-mime-types))
483 mh-mime-content-types
))))
484 (description (mml-minibuffer-read-description))
485 (dispos (or disposition
486 (completing-read "Disposition: [attachment] "
487 '(("attachment")("inline"))
490 (mml-insert-empty-tag 'part
'type type
'filename file
491 'disposition dispos
'description description
)
492 (setq mh-mml-compose-insert-flag t
)))
495 (defun mh-mml-secure-message-sign-pgpmime ()
496 "Add directive to encrypt/sign the entire message."
498 (if (not mh-gnus-pgp-support-flag
)
499 (error "Sorry. Your version of gnus does not support PGP/GPG")
500 (mml-secure-message-sign-pgpmime)
501 (setq mh-mml-compose-insert-flag t
)))
504 (defun mh-mml-secure-message-encrypt-pgpmime (&optional dontsign
)
505 "Add directive to encrypt and sign the entire message.
506 If called with a prefix argument DONTSIGN, only encrypt (do NOT sign)."
508 (if (not mh-gnus-pgp-support-flag
)
509 (error "Sorry. Your version of gnus does not support PGP/GPG")
510 (mml-secure-message-encrypt-pgpmime dontsign
)
511 (setq mh-mml-compose-insert-flag t
)))
517 (defmacro mh-defun-compat
(function arg-list
&rest body
)
518 "This is a macro to define functions which are not defined.
519 It is used for Gnus utility functions which were added recently. If FUNCTION
520 is not defined then it is defined to have argument list, ARG-LIST and body,
522 (let ((defined-p (fboundp function
)))
524 `(defun ,function
,arg-list
,@body
))))
525 (put 'mh-defun-compat
'lisp-indent-function
'defun
)
527 ;; Copy of original function from gnus-util.el
528 (mh-defun-compat gnus-local-map-property
(map)
529 "Return a list suitable for a text property list specifying keymap MAP."
530 (cond (mh-xemacs-flag (list 'keymap map
))
531 ((>= emacs-major-version
21) (list 'keymap map
))
532 (t (list 'local-map map
))))
534 ;; Copy of original function from mm-decode.el
535 (mh-defun-compat mm-merge-handles
(handles1 handles2
)
536 (append (if (listp (car handles1
)) handles1
(list handles1
))
537 (if (listp (car handles2
)) handles2
(list handles2
))))
539 ;; Copy of function from mm-decode.el
540 (mh-defun-compat mm-set-handle-multipart-parameter
(handle parameter value
)
541 ;; HANDLE could be a CTL.
543 (put-text-property 0 (length (car handle
)) parameter value
546 ;; Copy of original macro is in mm-decode.el
547 (mh-defun-compat mm-handle-multipart-ctl-parameter
(handle parameter
)
548 (get-text-property 0 parameter
(car handle
)))
550 ;; Copy of original function in mm-decode.el
551 (mh-defun-compat mm-readable-p
(handle)
552 "Say whether the content of HANDLE is readable."
553 (and (< (with-current-buffer (mm-handle-buffer handle
)
554 (buffer-size)) 10000)
555 (mm-with-unibyte-buffer
556 (mm-insert-part handle
)
557 (and (eq (mm-body-7-or-8) '7bit
)
558 (not (mm-long-lines-p 76))))))
560 ;; Copy of original function in mm-bodies.el
561 (mh-defun-compat mm-long-lines-p
(length)
562 "Say whether any of the lines in the buffer is longer than LINES."
564 (goto-char (point-min))
566 (while (and (not (eobp))
567 (not (> (current-column) length
)))
570 (and (> (current-column) length
)
573 (mh-defun-compat mm-keep-viewer-alive-p
(handle)
574 ;; Released Gnus doesn't keep handles associated with externally displayed
575 ;; MIME parts. So this will always return nil.
578 (mh-defun-compat mm-destroy-parts
(list)
579 "Older emacs don't have this function."
582 ;;; This is mm-save-part from gnus 5.10 since that function in emacs21.2 is
583 ;;; buggy (the args to read-file-name are incorrect). When all supported
584 ;;; versions of Emacs come with at least Gnus 5.10, we can delete this
585 ;;; function and rename calls to mh-mm-save-part to mm-save-part.
586 (defun mh-mm-save-part (handle)
587 "Write HANDLE to a file."
588 (let ((name (mail-content-type-get (mm-handle-type handle
) 'name
))
589 (filename (mail-content-type-get
590 (mm-handle-disposition handle
) 'filename
))
593 (setq filename
(file-name-nondirectory filename
)))
594 (setq file
(read-file-name "Save MIME part to: "
595 (or mm-default-directory
597 nil nil
(or filename name
"")))
598 (setq mm-default-directory
(file-name-directory file
))
599 (and (or (not (file-exists-p file
))
600 (yes-or-no-p (format "File %s already exists; overwrite? "
602 (mm-save-part-to-file handle file
))))
609 (defun mh-mime-cleanup ()
610 "Free the decoded MIME parts."
611 (let ((mime-data (gethash (current-buffer) mh-globals-hash
)))
612 ;; This is for Emacs, what about XEmacs?
613 (cond ((fboundp 'remove-images
)
614 (remove-images (point-min) (point-max))))
616 (mm-destroy-parts (mh-mime-handles mime-data
))
617 (remhash (current-buffer) mh-globals-hash
))))
620 (defun mh-destroy-postponed-handles ()
621 "Free MIME data for externally displayed mime parts."
622 (let ((mime-data (mh-buffer-data)))
624 (mm-destroy-parts (mh-mime-handles mime-data
)))
625 (remhash (current-buffer) mh-globals-hash
)))
627 (defun mh-handle-set-external-undisplayer (folder handle function
)
628 "Replacement for `mm-handle-set-external-undisplayer'.
629 This is only called in recent versions of Gnus. The MIME handles are stored
630 in data structures corresponding to MH-E folder buffer FOLDER instead of in
631 Gnus (as in the original). The MIME part, HANDLE is associated with the
632 undisplayer FUNCTION."
633 (if (mm-keep-viewer-alive-p handle
)
634 (let ((new-handle (copy-sequence handle
)))
635 (mm-handle-set-undisplayer new-handle function
)
636 (mm-handle-set-undisplayer handle nil
)
639 (push new-handle
(mh-mime-handles (mh-buffer-data)))))
640 (mm-handle-set-undisplayer handle function
)))
644 ;;; MIME transformations
645 (eval-when-compile (require 'font-lock
))
648 (defun mh-add-missing-mime-version-header ()
649 "Some mail programs don't put a MIME-Version header.
650 I have seen this only in spam, so maybe we shouldn't fix this ;-)"
652 (goto-char (point-min))
653 (when (and (message-fetch-field "content-type")
654 (not (message-fetch-field "mime-version")))
655 (when (search-forward "\n\n" nil t
)
657 (insert "MIME-Version: 1.0\n")))))
660 (defun mh-display-smileys ()
661 "Function to display smileys."
662 (when (and mh-graphical-smileys-flag
663 (fboundp 'smiley-region
)
664 (boundp 'font-lock-maximum-size
)
665 (>= (/ font-lock-maximum-size
8) (buffer-size)))
666 (smiley-region (point-min) (point-max))))
669 (defun mh-display-emphasis ()
670 "Function to display graphical emphasis."
671 (when (and mh-graphical-emphasis-flag
672 (boundp 'font-lock-maximum-size
)
673 (>= (/ font-lock-maximum-size
8) (buffer-size)))
674 (flet ((article-goto-body ())) ; shadow this function to do nothing
676 (goto-char (point-min))
677 (article-emphasize)))))
679 ;; Copied from gnus-art.el (should be checked for other cool things that can
680 ;; be added to the buttons)
681 (defvar mh-mime-button-commands
682 '((mh-press-button "\r" "Toggle Display")))
683 (defvar mh-mime-button-map
684 (let ((map (make-sparse-keymap)))
685 (unless (>= (string-to-number emacs-version
) 21)
686 ;; XEmacs doesn't care.
687 (set-keymap-parent map mh-show-mode-map
))
688 (define-key map
[mouse-2
] 'mh-push-button
)
689 (dolist (c mh-mime-button-commands
)
690 (define-key map
(cadr c
) (car c
)))
692 (defvar mh-mime-button-line-format-alist
697 (defvar mh-mime-button-line-format
"%{%([%p. %d%T]%)%}%e\n")
698 (defvar mh-mime-security-button-pressed nil
)
699 (defvar mh-mime-security-button-line-format
"%{%([[%t:%i]%D]%)%}\n")
700 (defvar mh-mime-security-button-end-line-format
"%{%([[End of %t]%D]%)%}\n")
701 (defvar mh-mime-security-button-line-format-alist
705 (?D pressed-details ?s
)))
706 (defvar mh-mime-security-button-map
707 (let ((map (make-sparse-keymap)))
708 (unless (>= (string-to-number emacs-version
) 21)
709 (set-keymap-parent map mh-show-mode-map
))
710 (define-key map
"\r" 'mh-press-button
)
711 (define-key map
[mouse-2
] 'mh-push-button
)
714 (defvar mh-mime-save-parts-directory nil
715 "Default to use for `mh-mime-save-parts-default-directory'.
719 (defun mh-mime-save-parts (arg)
720 "Store the MIME parts of the current message.
721 If ARG, prompt for directory, else use that specified by the variable
722 `mh-mime-save-parts-default-directory'. These directories may be superseded by
723 mh_profile directives, since this function calls on mhstore or mhn to do the
726 (let ((msg (if (eq major-mode
'mh-show-mode
)
727 (mh-show-buffer-message-number)
729 (folder (if (eq major-mode
'mh-show-mode
)
730 mh-show-folder-buffer
732 (command (if mh-nmh-flag
"mhstore" "mhn"))
736 (equal nil mh-mime-save-parts-default-directory
)
737 (equal t mh-mime-save-parts-default-directory
))
738 (not mh-mime-save-parts-directory
))
739 (read-file-name "Store in what directory? " nil nil t nil
))
741 (equal t mh-mime-save-parts-default-directory
))
742 mh-mime-save-parts-directory
)
743 (read-file-name (format
744 "Store in what directory? [%s] "
745 mh-mime-save-parts-directory
)
746 "" mh-mime-save-parts-directory t
""))
747 ((stringp mh-mime-save-parts-default-directory
)
748 mh-mime-save-parts-default-directory
)
750 mh-mime-save-parts-directory
))))
751 (if (and (equal directory
"") mh-mime-save-parts-directory
)
752 (setq directory mh-mime-save-parts-directory
))
753 (if (not (file-directory-p directory
))
754 (message "No directory specified.")
755 (if (equal nil mh-mime-save-parts-default-directory
)
756 (setq mh-mime-save-parts-directory directory
))
758 (set-buffer (get-buffer-create " *mh-store*"))
760 (setq mh-mime-save-parts-directory directory
)
763 (expand-file-name command mh-progs
) nil t nil
764 (mh-list-to-string (list folder msg
"-auto")))
765 (if (> (buffer-size) 0)
766 (save-window-excursion
767 (switch-to-buffer-other-window " *mh-store*")
770 ;; Avoid errors if gnus-sum isn't loaded yet...
771 (defvar gnus-newsgroup-charset nil
)
772 (defvar gnus-newsgroup-name nil
)
775 (defun mh-mime-display (&optional pre-dissected-handles
)
776 "Display (and possibly decode) MIME handles.
777 Optional argument, PRE-DISSECTED-HANDLES is a list of MIME handles. If
778 present they are displayed otherwise the buffer is parsed and then
781 (folder mh-show-folder-buffer
))
782 (flet ((mm-handle-set-external-undisplayer
784 (mh-handle-set-external-undisplayer folder handle function
)))
785 ;; If needed dissect the current buffer
786 (if pre-dissected-handles
787 (setq handles pre-dissected-handles
)
788 (setq handles
(or (mm-dissect-buffer nil
) (mm-uu-dissect)))
789 (setf (mh-mime-handles (mh-buffer-data))
790 (mm-merge-handles handles
(mh-mime-handles (mh-buffer-data))))
792 ;; Use charset to decode body...
794 (let* ((ct (ignore-errors
795 (mail-header-parse-content-type
796 (message-fetch-field "Content-Type" t
))))
797 (charset (mail-content-type-get ct
'charset
)))
798 (when (stringp charset
)
799 (mm-decode-body charset
)))))
801 (when (and handles
(or (not (stringp (car handles
))) (cdr handles
)))
802 ;; Goto start of message body
803 (goto-char (point-min))
804 (or (search-forward "\n\n" nil t
) (goto-char (point-max)))
807 (delete-region (point) (point-max))
809 ;; Display the MIME handles
810 (mh-mime-display-part handles
)))))
812 (defun mh-mime-display-part (handle)
813 "Decides the viewer to call based on the type of HANDLE."
814 (cond ((null handle
) nil
)
815 ((not (stringp (car handle
)))
816 (mh-mime-display-single handle
))
817 ((equal (car handle
) "multipart/alternative")
818 (mh-mime-display-alternative (cdr handle
)))
819 ((and mh-gnus-pgp-support-flag
820 (or (equal (car handle
) "multipart/signed")
821 (equal (car handle
) "multipart/encrypted")))
822 (mh-mime-display-security handle
))
823 (t (mh-mime-display-mixed (cdr handle
)))))
825 (defun mh-mime-display-alternative (handles)
826 "Choose among the alternatives, HANDLES the part that will be displayed.
827 If no part is preferred then all the parts are displayed."
828 (let ((preferred (mm-preferred-alternative handles
)))
829 (cond ((and preferred
(stringp (car preferred
)))
830 (mh-mime-display-part preferred
))
833 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
834 (mh-mime-display-single preferred
)
835 (goto-char (point-max))))
836 (t (mh-mime-display-mixed handles
)))))
838 (defun mh-mime-display-mixed (handles)
839 "Display the list of MIME parts, HANDLES recursively."
840 (mapcar #'mh-mime-display-part handles
))
842 (defun mh-mime-part-index (handle)
843 "Generate the button number for MIME part, HANDLE.
844 Notice that a hash table is used to display the same number when buttons need
845 to be displayed multiple times (for instance when nested messages are
847 (or (gethash handle
(mh-mime-part-index-hash (mh-buffer-data)))
848 (setf (gethash handle
(mh-mime-part-index-hash (mh-buffer-data)))
849 (incf (mh-mime-parts-count (mh-buffer-data))))))
851 ;;; Avoid compiler warnings for XEmacs functions...
853 (loop for function in
'(glyph-width window-pixel-width
854 glyph-height window-pixel-height
)
855 do
(or (fboundp function
) (defalias function
'ignore
))))
857 (defun mh-small-image-p (handle)
858 "Decide whether HANDLE is a \"small\" image that can be displayed inline.
859 This is only useful if a Content-Disposition header is not present."
860 (let ((media-test (caddr (assoc (car (mm-handle-type handle
))
861 mh-mm-inline-media-tests
)))
862 (mm-inline-large-images t
))
864 (equal (mm-handle-media-supertype handle
) "image")
865 (funcall media-test handle
) ; Since mm-inline-large-images is T,
866 ; this only tells us if the image is
867 ; something that emacs can display
868 (let* ((image (mm-get-image handle
)))
869 (cond ((fboundp 'glyph-width
)
870 ;; XEmacs -- totally untested, copied from gnus
871 (and (< (glyph-width image
)
872 (or mh-max-inline-image-width
873 (window-pixel-width)))
874 (< (glyph-height image
)
875 (or mh-max-inline-image-height
876 (window-pixel-height)))))
877 ((fboundp 'image-size
)
878 ;; Emacs21 -- copied from gnus
879 (let ((size (image-size image
)))
881 (or mh-max-inline-image-height
882 (1- (window-height))))
884 (or mh-max-inline-image-width
(window-width))))))
886 ;; Can't show image inline
889 (defun mh-inline-vcard-p (handle)
890 "Decide if HANDLE is a vcard that must be displayed inline."
891 (let ((type (mm-handle-type handle
)))
893 (equal (car type
) "text/x-vcard")
897 (goto-char (point-min))
898 (not (re-search-forward "^-- $" nil t
)))))))
900 (defun mh-mime-display-single (handle)
901 "Display a leaf node, HANDLE in the MIME tree."
902 (let* ((type (mm-handle-media-type handle
))
903 (small-image-flag (mh-small-image-p handle
))
904 (attachmentp (equal (car (mm-handle-disposition handle
))
906 (inlinep (and (equal (car (mm-handle-disposition handle
)) "inline")
907 (mm-inlinable-p handle
)
908 (mm-inlined-p handle
)))
909 (displayp (or inlinep
; show if inline OR
910 (mh-inline-vcard-p handle
); inline vcard OR
911 (and (not attachmentp
) ; if not an attachment
912 (or small-image-flag
; and small image
913 ; and user wants inline
915 (mm-handle-media-supertype handle
)
917 (mm-inlinable-p handle
)
918 (mm-inlined-p handle
)))))))
920 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
921 (cond ((and mh-gnus-pgp-support-flag
922 (equal type
"application/pgp-signature"))
923 nil
) ; skip signatures as they are already handled...
926 (mh-insert-mime-button handle
(mh-mime-part-index handle
) nil
))
927 ((and displayp
(not mh-display-buttons-for-inline-parts-flag
))
928 (or (mm-display-part handle
) (mm-display-part handle
)))
929 ((and displayp mh-display-buttons-for-inline-parts-flag
)
931 (mh-insert-mime-button handle
(mh-mime-part-index handle
) nil
)
933 (mh-mm-display-part handle
)))
934 (goto-char (point-max)))))
936 (defun mh-insert-mime-button (handle index displayed
)
937 "Insert MIME button for HANDLE.
938 INDEX is the part number that will be DISPLAYED. It is also used by commands
939 like \"K v\" which operate on individual MIME parts."
940 ;; The button could be displayed by a previous decode. In that case
941 ;; undisplay it if we need a hidden button.
942 (when (and (mm-handle-displayed-p handle
) (not displayed
))
943 (mm-display-part handle
))
944 (let ((name (or (mail-content-type-get (mm-handle-type handle
) 'name
)
945 (mail-content-type-get (mm-handle-disposition handle
)
947 (mail-content-type-get (mm-handle-type handle
) 'url
)
949 (type (mm-handle-media-type handle
))
950 (description (mail-decode-encoded-word-string
951 (or (mm-handle-description handle
) "")))
952 (dots (if (or displayed
(mm-handle-displayed-p handle
)) " " "..."))
954 (if (string-match ".*/" name
) (setq name
(substring name
(match-end 0))))
955 (setq long-type
(concat type
(and (not (equal name
""))
956 (concat "; " name
))))
957 (unless (equal description
"")
958 (setq long-type
(concat " --- " long-type
)))
959 (unless (bolp) (insert "\n"))
962 mh-mime-button-line-format mh-mime-button-line-format-alist
963 `(,@(gnus-local-map-property mh-mime-button-map
)
964 mh-callback mh-mm-display-part
968 (widget-convert-button
971 :action
'mh-widget-press-button
972 :button-keymap mh-mime-button-map
974 "Mouse-2 click or press RET (in show buffer) to toggle display")))
976 ;; There is a bug in Gnus inline image display due to which an extra line
977 ;; gets inserted every time it is viewed. To work around that problem we are
978 ;; using an extra property 'mh-region to remember the region that is added
979 ;; when the button is clicked. The region is then deleted to make sure that
980 ;; no extra lines get inserted.
981 (defun mh-mm-display-part (handle)
982 "Toggle display of button for MIME part, HANDLE."
984 (let ((id (get-text-property (point) 'mh-part
))
986 (window (selected-window))
987 (mail-parse-charset 'nil
)
988 (mail-parse-ignored-charsets nil
)
989 region buffer-read-only
)
992 (let ((win (get-buffer-window (current-buffer) t
)))
997 (if (mm-handle-displayed-p handle
)
998 ;; This will remove the part.
1000 ;; Delete the button and displayed part (if any)
1001 (let ((region (get-text-property point
'mh-region
)))
1003 (when (fboundp 'remove-images
)
1004 (remove-images (car region
) (cdr region
))))
1005 (mm-display-part handle
)
1007 (delete-region (car region
) (cdr region
))))
1008 ;; Delete button (if it still remains). This happens for
1009 ;; externally displayed parts where the previous step does
1012 (delete-region (point) (progn (forward-line) (point)))))
1014 (delete-region (point) (progn (forward-line 1) (point)))
1015 (narrow-to-region (point) (point))
1016 ;; Maybe we need another unwind-protect here.
1017 (when (equal (mm-handle-media-supertype handle
) "image")
1019 (when (and (not (eq (ignore-errors (mm-display-part handle
))
1021 (equal (mm-handle-media-supertype handle
)
1023 (goto-char (point-min))
1025 (when (equal (mm-handle-media-supertype handle
) "text")
1026 (when (eq mh-highlight-citation-p
'gnus
)
1027 (mh-gnus-article-highlight-citation))
1028 (mh-display-smileys)
1029 (mh-display-emphasis))
1030 (setq region
(cons (progn (goto-char (point-min))
1032 (progn (goto-char (point-max))
1033 (point-marker)))))))
1034 (when (window-live-p window
)
1035 (select-window window
))
1038 (mh-insert-mime-button handle id
(mm-handle-displayed-p handle
))
1041 (add-text-properties (line-beginning-position) (line-end-position)
1042 `(mh-region ,region
)))))))
1045 (defun mh-press-button ()
1047 If the MIME part is visible then it is removed. Otherwise the part is
1050 (let ((mm-inline-media-tests mh-mm-inline-media-tests
)
1051 (data (get-text-property (point) 'mh-data
))
1052 (function (get-text-property (point) 'mh-callback
))
1053 (buffer-read-only nil
)
1054 (folder mh-show-folder-buffer
))
1055 (flet ((mm-handle-set-external-undisplayer
1057 (mh-handle-set-external-undisplayer folder handle function
)))
1058 (when (and function
(eolp))
1060 (unwind-protect (and function
(funcall function data
))
1061 (set-buffer-modified-p nil
)))))
1064 (defun mh-push-button (event)
1065 "Click MIME button for EVENT.
1066 If the MIME part is visible then it is removed. Otherwise the part is
1067 displayed. This function is called when the mouse is used to click the MIME
1070 (set-buffer (window-buffer (posn-window (event-start event
))))
1071 (select-window (posn-window (event-start event
)))
1072 (let* ((pos (posn-point (event-start event
)))
1073 (folder mh-show-folder-buffer
)
1074 (mm-inline-media-tests mh-mm-inline-media-tests
)
1075 (data (get-text-property pos
'mh-data
))
1076 (function (get-text-property pos
'mh-callback
))
1077 (buffer-read-only nil
))
1078 (flet ((mm-handle-set-external-undisplayer
1080 (mh-handle-set-external-undisplayer folder handle function
)))
1082 (unwind-protect (and function
(funcall function data
))
1083 (set-buffer-modified-p nil
)))))
1086 (defun mh-mime-save-part ()
1087 "Save MIME part at point."
1089 (let ((data (get-text-property (point) 'mh-data
)))
1091 (let ((mm-default-directory mh-mime-save-parts-directory
))
1092 (mh-mm-save-part data
)
1093 (setq mh-mime-save-parts-directory mm-default-directory
)))))
1096 (defun mh-mime-inline-part ()
1097 "Toggle display of the raw MIME part."
1099 (let* ((buffer-read-only nil
)
1100 (data (get-text-property (point) 'mh-data
))
1101 (inserted-flag (get-text-property (point) 'mh-mime-inserted
))
1102 (displayed-flag (mm-handle-displayed-p data
))
1105 (cond ((and data
(not inserted-flag
) (not displayed-flag
))
1106 (let ((contents (mm-get-part data
)))
1107 (add-text-properties (line-beginning-position) (line-end-position)
1108 '(mh-mime-inserted t
))
1109 (setq start
(point-marker))
1111 (mm-insert-inline data contents
)
1112 (setq end
(point-marker))
1113 (add-text-properties
1114 start
(progn (goto-char start
) (line-end-position))
1115 `(mh-region (,start .
,end
)))))
1116 ((and data
(or inserted-flag displayed-flag
))
1118 (message "MIME part already inserted")))
1120 (set-buffer-modified-p nil
)))
1122 (defun mh-widget-press-button (widget el
)
1123 "Callback for widget, WIDGET.
1124 Parameter EL is unused."
1125 (goto-char (widget-get widget
:from
))
1128 (defun mh-mime-display-security (handle)
1129 "Display PGP encrypted/signed message, HANDLE."
1132 (narrow-to-region (point) (point))
1133 (mh-insert-mime-security-button handle
)
1134 (mh-mime-display-mixed (cdr handle
))
1136 (let ((mh-mime-security-button-line-format
1137 mh-mime-security-button-end-line-format
))
1138 (mh-insert-mime-security-button handle
))
1139 (mm-set-handle-multipart-parameter
1141 (cons (set-marker (make-marker) (point-min))
1142 (set-marker (make-marker) (point-max))))))
1144 ;;; I rewrote the security part because Gnus doesn't seem to ever minimize
1145 ;;; the button. That is once the mime-security button is pressed there seems
1146 ;;; to be no way of getting rid of the inserted text.
1147 (defun mh-mime-security-show-details (handle)
1148 "Toggle display of detailed security info for HANDLE."
1149 (let ((details (mm-handle-multipart-ctl-parameter handle
'gnus-details
)))
1151 (let ((mh-mime-security-button-pressed
1152 (not (get-text-property (point) 'mh-button-pressed
)))
1153 (mh-mime-security-button-line-format
1154 (get-text-property (point) 'mh-line-format
)))
1156 (while (eq (get-text-property (point) 'mh-line-format
)
1157 mh-mime-security-button-line-format
)
1161 (narrow-to-region (point) (point))
1162 (mh-insert-mime-security-button handle
))
1165 (or (text-property-not-all
1167 'mh-line-format mh-mime-security-button-line-format
)
1169 (forward-line -
1)))))
1171 (defun mh-mime-security-press-button (handle)
1172 "Callback from security button for part HANDLE."
1173 (when (mm-handle-multipart-ctl-parameter handle
'gnus-info
)
1174 (mh-mime-security-show-details handle
)))
1176 ;; These variables should already be initialized in mm-decode.el if we have a
1177 ;; recent enough Gnus. The defvars are here to avoid compiler warnings.
1178 (defvar mm-verify-function-alist nil
)
1179 (defvar mm-decrypt-function-alist nil
)
1181 (defvar pressed-details
)
1183 (defun mh-insert-mime-security-button (handle)
1184 "Display buttons for PGP message, HANDLE."
1185 (let* ((protocol (mm-handle-multipart-ctl-parameter handle
'protocol
))
1186 (crypto-type (or (nth 2 (assoc protocol mm-verify-function-alist
))
1187 (nth 2 (assoc protocol mm-decrypt-function-alist
))
1189 (type (concat crypto-type
1190 (if (equal (car handle
) "multipart/signed")
1191 " Signed" " Encrypted")
1193 (info (or (mm-handle-multipart-ctl-parameter handle
'gnus-info
)
1195 (details (mm-handle-multipart-ctl-parameter handle
'gnus-details
))
1196 pressed-details begin end
)
1197 (setq details
(if details
(concat "\n" details
) ""))
1198 (setq pressed-details
(if mh-mime-security-button-pressed details
""))
1199 (unless (bolp) (insert "\n"))
1200 (setq begin
(point))
1202 mh-mime-security-button-line-format
1203 mh-mime-security-button-line-format-alist
1204 `(,@(gnus-local-map-property mh-mime-security-button-map
)
1205 mh-button-pressed
,mh-mime-security-button-pressed
1206 mh-callback mh-mime-security-press-button
1207 mh-line-format
,mh-mime-security-button-line-format
1210 (widget-convert-button 'link begin end
1212 :action
'mh-widget-press-button
1213 :button-keymap mh-mime-security-button-map
1214 :help-echo
"Mouse-2 click or press RET (in show buffer) to see security details.")
1215 (when (equal info
"Failed")
1216 (let* ((type (if (equal (car handle
) "multipart/signed")
1217 "verification" "decryption"))
1218 (warning (if (equal type
"decryption")
1219 "(passphrase may be incorrect)" "")))
1220 (message "%s %s failed %s" crypto-type type warning
)))))
1222 (defun mh-mm-inline-message (handle)
1223 "Display message, HANDLE.
1224 The function decodes the message and displays it. It avoids decoding the same
1225 message multiple times."
1227 (clean-message-header mh-clean-message-header-flag
)
1228 (invisible-headers mh-invisible-headers
)
1229 (visible-headers mh-visible-headers
))
1232 (narrow-to-region b b
)
1233 (mm-insert-part handle
)
1235 (or (gethash handle
(mh-mime-handles-cache (mh-buffer-data)))
1236 (setf (gethash handle
(mh-mime-handles-cache (mh-buffer-data)))
1237 (let ((handles (or (mm-dissect-buffer nil
)
1239 (setf (mh-mime-handles (mh-buffer-data))
1241 handles
(mh-mime-handles (mh-buffer-data))))
1244 (goto-char (point-min))
1245 (cond (clean-message-header
1246 (mh-clean-msg-header (point-min)
1249 (goto-char (point-min)))
1251 (mh-start-of-uncleaned-message)))
1254 ;; The other highlighting types don't need anything special
1255 (when (eq mh-highlight-citation-p
'gnus
)
1256 (mh-gnus-article-highlight-citation))
1257 (goto-char (point-min))
1258 (insert "\n------- Forwarded Message\n\n")
1259 (mh-display-smileys)
1260 (mh-display-emphasis)
1261 (mm-handle-set-undisplayer
1264 (let (buffer-read-only)
1265 (if (fboundp 'remove-specifier
)
1266 ;; This is only valid on XEmacs.
1267 (mapcar (lambda (prop)
1269 (face-property 'default prop
) (current-buffer)))
1270 '(background background-pixmap foreground
)))
1271 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
1275 ;;; Local Variables:
1276 ;;; indent-tabs-mode: nil
1277 ;;; sentence-end-double-space: nil
1280 ;;; mh-mime.el ends here