1 ;;; mh-mime.el --- MH-E support for composing MIME messages
3 ;; Copyright (C) 1993, 1995,
4 ;; 2001, 2002, 2003, 2004, 2005 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 ;; Internal support for MH-E package.
31 ;; Support for generating MH-style directives for mhn or mhbuild as well as
32 ;; MML (MIME Meta Language) tags. MH-style directives are supported by MH 6.8
39 (eval-when-compile (require 'mh-acros))
45 (autoload 'article-emphasize "gnus-art")
46 (autoload 'gnus-article-goto-header "gnus-art")
47 (autoload 'gnus-eval-format "gnus-spec")
48 (autoload 'gnus-get-buffer-create "gnus")
49 (autoload 'message-options-set-recipient "message")
50 (autoload 'mm-uu-dissect "mm-uu")
51 (autoload 'mml-unsecure-message "mml-sec")
52 (autoload 'rfc2047-decode-region "rfc2047")
53 (autoload 'widget-convert-button "wid-edit")
56 (defun mh-compose-insertion (&optional inline)
57 "Add tag to include a file such as an image or sound.
58 You are prompted for the filename containing the object, the media type if it
59 cannot be determined automatically, and a content description. If you're using
60 MH-style directives, you will also be prompted for additional attributes.
62 The option `mh-compose-insertion' controls what type of tags are inserted.
63 Optional argument INLINE means make it an inline attachment."
65 (if (equal mh-compose-insertion 'mml)
67 (mh-mml-attach-file "inline")
69 (call-interactively 'mh-mh-attach-file)))
72 (defun mh-compose-forward (&optional description folder messages)
73 "Add tag to forward a message.
74 You are prompted for a content DESCRIPTION, the name of the FOLDER in which
75 the messages to forward are located, and the MESSAGES' numbers.
77 The option `mh-compose-insertion' controls what type of tags are inserted."
79 ((description (mml-minibuffer-read-description))
80 (folder (mh-prompt-for-folder "Message from"
81 mh-sent-from-folder nil))
82 (messages (let ((default-message
84 folder mh-sent-from-folder)
85 (numberp mh-sent-from-msg))
87 (nth 0 (mh-translate-range
91 (format "Messages (default %d): "
94 (number-to-string default-message))
95 (read-string (format "Messages: "))))))
96 (list description folder messages)))
101 (setq range (mh-translate-range folder messages))
103 (error "No messages in specified range"))
104 (dolist (message range)
105 (if (equal mh-compose-insertion 'mml)
106 (mh-mml-forward-message description folder (format "%s" message))
107 (mh-mh-forward-message description folder (format "%s" message))))))
110 ;; paragraph code should not fill # lines if MIME enabled.
111 ;; implement mh-auto-mh-to-mime (if non-nil, \\[mh-send-letter]
112 ;; invokes mh-mh-to-mime automatically before sending.)
113 ;; actually, instead of mh-auto-mh-to-mime,
114 ;; should read automhnproc from profile
115 ;; MIME option to mh-forward
116 ;; command to move to content-description insertion point
118 (defvar mh-mh-to-mime-args nil
119 "Extra arguments for \\[mh-mh-to-mime] to pass to the \"mhbuild\" command.
120 The arguments are passed to \"mhbuild\" if \\[mh-mh-to-mime] is given a prefix
121 argument. Normally default arguments to \"mhbuild\" are specified in the MH
124 (defvar mh-media-type-regexp
125 (concat (regexp-opt '("text" "image" "audio" "video" "application"
126 "multipart" "message") t)
128 "Regexp matching valid media types used in MIME attachment compositions.")
130 ;; Just defvar the variable to avoid compiler warning... This doesn't bind
131 ;; the variable, so things should work exactly as before.
132 (defvar mh-have-file-command)
135 (defun mh-have-file-command ()
136 "Return t if 'file' command is on the system.
137 'file -i' is used to get MIME type of composition insertion."
138 (when (not (boundp 'mh-have-file-command))
139 (load "executable" t t) ; executable-find not autoloaded in emacs20
140 (setq mh-have-file-command
141 (and (fboundp 'executable-find)
142 (executable-find "file") ; file command exists
143 ; and accepts -i and -b args.
144 (zerop (call-process "file" nil nil nil "-i" "-b"
145 (expand-file-name "inc" mh-progs))))))
146 mh-have-file-command)
148 (defvar mh-file-mime-type-substitutions
149 '(("application/msword" "\.xls" "application/ms-excel")
150 ("application/msword" "\.ppt" "application/ms-powerpoint")
151 ("text/plain" "\.vcf" "text/x-vcard"))
152 "Substitutions to make for Content-Type returned from file command.
153 The first element is the Content-Type returned by the file command.
154 The second element is a regexp matching the file name, usually the extension.
155 The third element is the Content-Type to replace with.")
157 (defun mh-file-mime-type-substitute (content-type filename)
158 "Return possibly changed CONTENT-TYPE on the FILENAME.
159 Substitutions are made from the `mh-file-mime-type-substitutions' variable."
160 (let ((subst mh-file-mime-type-substitutions)
161 (type) (match) (answer content-type)
162 (case-fold-search t))
164 (setq type (car (car subst))
165 match (elt (car subst) 1))
166 (if (and (string-equal content-type type)
167 (string-match match filename))
168 (setq answer (elt (car subst) 2)
170 (setq subst (cdr subst))))
174 (defun mh-file-mime-type (filename)
175 "Return MIME type of FILENAME from file command.
176 Returns nil if file command not on system."
178 ((not (mh-have-file-command))
179 nil) ;No file command, exit now.
180 ((not (and (file-exists-p filename)(file-readable-p filename)))
184 (let ((tmp-buffer (get-buffer-create mh-temp-buffer)))
185 (set-buffer tmp-buffer)
188 (call-process "file" nil '(t nil) nil "-b" "-i"
189 (expand-file-name filename))
190 (goto-char (point-min))
191 (if (not (re-search-forward mh-media-type-regexp nil t))
193 (mh-file-mime-type-substitute (match-string 0) filename)))
194 (kill-buffer tmp-buffer)))))))
196 (defvar mh-mime-content-types
197 '(("application/mac-binhex40") ("application/msword")
198 ("application/octet-stream") ("application/pdf") ("application/pgp-keys")
199 ("application/pgp-signature") ("application/pkcs7-signature")
200 ("application/postscript") ("application/rtf")
201 ("application/vnd.ms-excel") ("application/vnd.ms-powerpoint")
202 ("application/vnd.ms-project") ("application/vnd.ms-tnef")
203 ("application/wordperfect5.1") ("application/wordperfect6.0")
206 ("audio/basic") ("audio/mpeg")
208 ("image/gif") ("image/jpeg") ("image/png")
210 ("message/delivery-status")
211 ("message/external-body") ("message/partial") ("message/rfc822")
213 ("text/enriched") ("text/html") ("text/plain") ("text/rfc822-headers")
214 ("text/richtext") ("text/x-vcard") ("text/xml")
216 ("video/mpeg") ("video/quicktime"))
217 "Valid MIME content types for Emacs 20.
218 Obsolete; use `mailcap-mime-types'.
220 See also \\[mh-mh-to-mime].")
222 ;; Delete mh-minibuffer-read-type and mh-mime-content-types and use
223 ;; mml-minibuffer-read-type when Emacs20 is no longer supported unless we
224 ;; think (mh-file-mime-type) is better than (mm-default-file-encoding).
226 (defun mh-minibuffer-read-type (filename &optional default)
227 "Return the content type associated with the given FILENAME.
228 If the \"file\" command exists and recognizes the given file, then its value
229 is returned\; otherwise, the user is prompted for a type (see
230 `mailcap-mime-types' and for Emacs 20, `mh-mime-content-types').
231 Optional argument DEFAULT is returned if a type isn't entered."
232 (mailcap-parse-mimetypes)
233 (let* ((default (or default
234 (mm-default-file-encoding filename)
235 "application/octet-stream"))
236 (type (or (mh-file-mime-type filename)
238 (format "Content type (default %s): " default)
239 (if (fboundp 'mailcap-mime-types)
240 (mapcar 'list (mailcap-mime-types))
241 mh-mime-content-types)))))
242 (if (not (equal type ""))
246 ;; RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One:
247 ;; Format of Internet Message Bodies.
248 ;; RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two:
250 ;; RFC 2049 - Multipurpose Internet Mail Extensions (MIME) Part Five:
251 ;; Conformance Criteria and Examples.
252 ;; RFC 2017 - Definition of the URL MIME External-Body Access-Type
253 ;; RFC 1738 - Uniform Resource Locators (URL)
254 (defvar mh-access-types
255 '(("anon-ftp") ; RFC2046 Anonymous File Transfer Protocol
256 ("file") ; RFC1738 Host-specific file names
257 ("ftp") ; RFC2046 File Transfer Protocol
258 ("gopher") ; RFC1738 The Gopher Protocol
259 ("http") ; RFC1738 Hypertext Transfer Protocol
260 ("local-file") ; RFC2046 Local file access
261 ("mail-server") ; RFC2046 mail-server Electronic mail address
262 ("mailto") ; RFC1738 Electronic mail address
263 ("news") ; RFC1738 Usenet news
264 ("nntp") ; RFC1738 Usenet news using NNTP access
265 ("propspero") ; RFC1738 Prospero Directory Service
266 ("telnet") ; RFC1738 Telnet
267 ("tftp") ; RFC2046 Trivial File Transfer Protocol
268 ("url") ; RFC2017 URL scheme MIME access-type Protocol
269 ("wais")) ; RFC1738 Wide Area Information Servers
270 "Valid MIME access-type values.")
273 (defun mh-mh-attach-file (filename type description attributes)
274 "Add a tag to insert a MIME message part from a file.
275 You are prompted for the FILENAME containing the object, the media TYPE if it
276 cannot be determined automatically, and a content DESCRIPTION. In addition,
277 you are also prompted for additional ATTRIBUTES.
279 See also \\[mh-mh-to-mime]."
280 (interactive (let ((filename (mml-minibuffer-read-file "Attach file: ")))
283 (mh-minibuffer-read-type filename)
284 (mml-minibuffer-read-description)
285 (read-string "Attributes: "
287 (file-name-nondirectory filename)
289 (mh-mh-compose-type filename type description attributes))
291 (defun mh-mh-compose-type (filename type
292 &optional description attributes comment)
293 "Insert an MH-style directive to insert a file.
294 The file specified by FILENAME is encoded as TYPE. An optional DESCRIPTION is
295 used as the Content-Description field, optional set of ATTRIBUTES and an
296 optional COMMENT can also be included."
300 (insert "; " attributes))
302 (insert " (" comment ")"))
305 (insert description))
306 (insert "] " (expand-file-name filename))
310 (defun mh-mh-compose-anon-ftp (host filename type description)
311 "Add tag to include anonymous ftp reference to a file.
312 You can even have your message initiate an \"ftp\" transfer when the
313 recipient reads the message. You are prompted for the remote
314 HOST and FILENAME, the media TYPE, and the content DESCRIPTION.
316 See also \\[mh-mh-to-mime]."
318 (read-string "Remote host: ")
319 (read-string "Remote filename: ")
320 (mh-minibuffer-read-type "DUMMY-FILENAME")
321 (mml-minibuffer-read-description)))
322 (mh-mh-compose-external-type "anon-ftp" host filename
326 (defun mh-mh-compose-external-compressed-tar (host filename description)
327 "Add tag to include anonymous ftp reference to a compressed tar file.
328 In addition to retrieving the file via anonymous \"ftp\" as per the
329 \\[mh-mh-compose-anon-ftp] command, the file will also be uncompressed and
330 untarred. You are prompted for the remote HOST and FILENAME and the content
333 See also \\[mh-mh-to-mime]."
335 (read-string "Remote host: ")
336 (read-string "Remote filename: ")
337 (mml-minibuffer-read-description)))
338 (mh-mh-compose-external-type "anon-ftp" host filename
339 "application/octet-stream"
341 "type=tar; conversions=x-compress"
345 (defun mh-mh-compose-external-type (access-type host filename type
346 &optional description
347 attributes parameters
349 "Add tag to refer to a remote file.
350 This command is a general utility for referencing external files. In fact, all
351 of the other commands that insert directives to access external files call
352 this command. You are prompted for the ACCESS-TYPE, remote HOST and FILENAME,
353 and content TYPE. If you provide a prefix argument, you are also prompted for
354 a content DESCRIPTION, ATTRIBUTES, PARAMETERS, and a COMMENT.
356 See also \\[mh-mh-to-mime]."
358 (completing-read "Access type: " mh-access-types)
359 (read-string "Remote host: ")
360 (read-string "Remote filename: ")
361 (mh-minibuffer-read-type "DUMMY-FILENAME")
362 (if current-prefix-arg (mml-minibuffer-read-description))
363 (if current-prefix-arg (read-string "Attributes: "))
364 (if current-prefix-arg (read-string "Parameters: "))
365 (if current-prefix-arg (read-string "Comment: "))))
369 (insert "; " attributes))
371 (insert " (" comment ") "))
374 (insert description))
376 (insert "access-type=" access-type "; ")
377 (insert "site=" host)
378 (insert "; name=" (file-name-nondirectory filename))
379 (let ((directory (file-name-directory filename)))
381 (insert "; directory=\"" directory "\"")))
383 (insert "; " parameters))
387 (defun mh-mh-forward-message (&optional description folder messages)
388 "Add tag to forward a message.
389 You are prompted for a content DESCRIPTION, the name of the FOLDER in which
390 the messages to forward are located, and the MESSAGES' numbers.
392 See also \\[mh-mh-to-mime]."
394 (mml-minibuffer-read-description)
395 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
396 (read-string (concat "Messages"
397 (if (numberp mh-sent-from-msg)
398 (format " (default %d): "
404 (not (string= description ""))
405 (insert description))
408 (not (string= folder ""))
411 (not (string= messages "")))
412 (let ((start (point)))
413 (insert " " messages)
414 (subst-char-in-region start (point) ?, ? ))
415 (if (numberp mh-sent-from-msg)
416 (insert " " (int-to-string mh-sent-from-msg))))
420 (defun mh-mh-to-mime (&optional extra-args)
421 "Compose MIME message from MH-style directives.
422 Typically, you send a message with attachments just like any other message.
423 However, you may take a sneak preview of the MIME encoding if you wish by
424 running this command.
426 If you wish to pass additional arguments to \"mhbuild\" (\"mhn\") to affect
427 how it builds your message, use the `mh-mh-to-mime-args' option. For example,
428 you can build a consistency check into the message by setting
429 `mh-mh-to-mime-args' to \"-check\". The recipient of your message can then run
430 \"mhbuild -check\" on the message--\"mhbuild\" (\"mhn\") will complain if the
431 message has been corrupted on the way. This command only consults this option
432 when given a prefix argument EXTRA-ARGS.
434 The value of `mh-mh-to-mime-hook' is a list of functions to be called after
435 the message has been formatted.
437 The effects of this command can be undone by running \\[mh-mh-to-mime-undo]."
439 (mh-mh-quote-unescaped-sharp)
441 (message "Running %s..." (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
444 (mh-exec-cmd-error nil
446 (if extra-args mh-mh-to-mime-args)
449 (mh-exec-cmd-error (format "mhdraft=%s" buffer-file-name)
451 (if extra-args mh-mh-to-mime-args)
454 (message "Running %s...done" (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
455 (run-hooks 'mh-mh-to-mime-hook))
457 (defun mh-mh-quote-unescaped-sharp ()
458 "Quote `#' characters that haven't been quoted for \"mhbuild\".
459 If the `#' character is present in the first column, but it isn't part of a
460 MH-style directive then \"mhbuild\" gives an error. This function will quote
461 all such characters."
463 (goto-char (point-min))
464 (while (re-search-forward "^#" nil t)
466 (unless (mh-mh-directive-present-p (point) (line-end-position))
468 (goto-char (line-end-position)))))
471 (defun mh-mh-to-mime-undo (noconfirm)
472 "Undo effects of \\[mh-mh-to-mime].
473 Optional non-nil argument NOCONFIRM means don't ask for confirmation."
475 (if (null buffer-file-name)
476 (error "Buffer does not seem to be associated with any file"))
477 (let ((backup-strings '("," "#"))
479 (while (and backup-strings
482 (concat (file-name-directory buffer-file-name)
484 (file-name-nondirectory buffer-file-name)
486 (setq backup-strings (cdr backup-strings)))
488 (error "Backup file for %s no longer exists!" buffer-file-name))
490 (yes-or-no-p (format "Revert buffer from file %s? "
492 (error "Revert not confirmed"))
493 (let ((buffer-read-only nil))
495 (insert-file-contents backup-file))
496 (after-find-file nil)))
499 (defun mh-mh-directive-present-p (&optional begin end)
500 "Check if the text between BEGIN and END might be a MH-style directive.
501 The optional argument BEGIN defaults to the beginning of the buffer, while END
502 defaults to the the end of the buffer."
503 (unless begin (setq begin (point-min)))
504 (unless end (setq end (point-max)))
506 (block 'search-for-mh-directive
508 (while (re-search-forward "^#" end t)
509 (let ((s (buffer-substring-no-properties (point) (line-end-position))))
511 ((string-match "^forw[ \t\n]+" s)
512 (return-from 'search-for-mh-directive t))
513 (t (let ((first-token (car (split-string s "[ \t;@]"))))
514 (when (and first-token
515 (string-match mh-media-type-regexp
517 (return-from 'search-for-mh-directive t)))))))
522 ;;; MIME composition functions
525 (defun mh-mml-to-mime ()
526 "Compose MIME message from MML tags.
527 Typically, you send a message with attachments just like any other message.
528 However, you may take a sneak preview of the MIME encoding if you wish by
529 running this command.
531 This action can be undone by running \\[undo]."
534 (when mh-pgp-support-flag ;; This is only needed for PGP
535 (message-options-set-recipient))
536 (let ((saved-text (buffer-string))
537 (buffer (current-buffer))
538 (modified-flag (buffer-modified-p)))
539 (condition-case err (mml-to-mime)
541 (with-current-buffer buffer
542 (delete-region (point-min) (point-max))
544 (set-buffer-modified-p modified-flag))
545 (error (error-message-string err))))))
548 (defun mh-mml-forward-message (description folder message)
549 "Forward a message as attachment.
550 The function will prompt the user for a DESCRIPTION, a FOLDER and MESSAGE
552 (let ((msg (if (and (equal message "") (numberp mh-sent-from-msg))
554 (car (read-from-string message)))))
555 (cond ((integerp msg)
556 (if (string= "" description)
557 ;; Rationale: mml-attach-file constructs a malformed composition
558 ;; if the description string is empty. This fixes SF #625168.
559 (mml-attach-file (format "%s%s/%d"
560 mh-user-path (substring folder 1) msg)
562 (mml-attach-file (format "%s%s/%d"
563 mh-user-path (substring folder 1) msg)
566 (t (error "The message number, %s is not a integer!" msg)))))
568 (defvar mh-mml-cryptographic-method-history ())
571 (defun mh-mml-query-cryptographic-method ()
572 "Read the cryptographic method to use."
573 (if current-prefix-arg
574 (let ((def (or (car mh-mml-cryptographic-method-history)
575 mh-mml-method-default)))
576 (completing-read (format "Method (default %s): " def)
577 '(("pgp") ("pgpmime") ("smime"))
578 nil t nil 'mh-mml-cryptographic-method-history def))
579 mh-mml-method-default))
582 (defun mh-mml-attach-file (&optional disposition)
583 "Add a tag to insert a MIME message part from a file.
584 You are prompted for the filename containing the object, the media type if it
585 cannot be determined automatically, a content description and the DISPOSITION
588 This is basically `mml-attach-file' from Gnus, modified such that a prefix
589 argument yields an `inline' disposition and Content-Type is determined
591 (let* ((file (mml-minibuffer-read-file "Attach file: "))
592 (type (mh-minibuffer-read-type file))
593 (description (mml-minibuffer-read-description))
594 (dispos (or disposition
595 (mml-minibuffer-read-disposition type))))
596 (mml-insert-empty-tag 'part 'type type 'filename file
597 'disposition dispos 'description description)))
599 (defvar mh-identity-pgg-default-user-id)
601 (defun mh-secure-message (method mode &optional identity)
602 "Add tag to encrypt or sign message.
603 METHOD should be one of: \"pgpmime\", \"pgp\", \"smime\".
604 MODE should be one of: \"sign\", \"encrypt\", \"signencrypt\", \"none\".
605 IDENTITY is optionally the default-user-id to use."
606 (if (not mh-pgp-support-flag)
607 (error "Your version of Gnus does not support PGP/GPG")
608 ;; Check the arguments
609 (let ((valid-methods (list "pgpmime" "pgp" "smime"))
610 (valid-modes (list "sign" "encrypt" "signencrypt" "none")))
611 (if (not (member method valid-methods))
612 (error "Method \"%s\" is invalid" method))
613 (if (not (member mode valid-modes))
614 (error "Mode \"%s\" is invalid" mode))
615 (mml-unsecure-message)
616 (if (not (string= mode "none"))
618 (goto-char (point-min))
619 (mh-goto-header-end 1)
620 (if mh-identity-pgg-default-user-id
621 (mml-insert-tag 'secure 'method method 'mode mode
622 'sender mh-identity-pgg-default-user-id)
623 (mml-insert-tag 'secure 'method method 'mode mode)))))))
626 (defun mh-mml-unsecure-message (&optional ignore)
627 "Remove any secure message tags.
628 The argument IGNORE is not used."
630 (if (not mh-pgp-support-flag)
631 (error "Your version of Gnus does not support PGP/GPG")
632 (mml-unsecure-message)))
635 (defun mh-mml-secure-message-sign (method)
636 "Add tag to sign the message.
637 A proper multipart message is created for you when you send the message. Use
638 the \\[mh-mml-unsecure-message] command to remove this tag. Use a prefix
639 argument METHOD to be prompted for one of the possible security methods
640 \(see `mh-mml-method-default')."
641 (interactive (list (mh-mml-query-cryptographic-method)))
642 (mh-secure-message method "sign" mh-identity-pgg-default-user-id))
645 (defun mh-mml-secure-message-encrypt (method)
646 "Add tag to encrypt the message.
647 A proper multipart message is created for you when you send the message. Use
648 the \\[mh-mml-unsecure-message] command to remove this tag. Use a prefix
649 argument METHOD to be prompted for one of the possible security methods
650 \(see `mh-mml-method-default')."
651 (interactive (list (mh-mml-query-cryptographic-method)))
652 (mh-secure-message method "encrypt" mh-identity-pgg-default-user-id))
655 (defun mh-mml-secure-message-signencrypt (method)
656 "Add tag to encrypt and sign the message.
657 A proper multipart message is created for you when you send the message. Use
658 the \\[mh-mml-unsecure-message] command to remove this tag. Use a prefix
659 argument METHOD to be prompted for one of the possible security methods
660 \(see `mh-mml-method-default')."
661 (interactive (list (mh-mml-query-cryptographic-method)))
662 (mh-secure-message method "signencrypt" mh-identity-pgg-default-user-id))
665 (defun mh-mml-tag-present-p ()
666 "Check if the current buffer has text which may be a MML tag."
668 (goto-char (point-min))
670 "\\(<#part\\(.\\|\n\\)*>[ \n\t]*<#/part>\\|^<#secure.+>$\\)"
678 (defun mh-mime-cleanup ()
679 "Free the decoded MIME parts."
680 (let ((mime-data (gethash (current-buffer) mh-globals-hash)))
681 ;; This is for Emacs, what about XEmacs?
682 (mh-funcall-if-exists remove-images (point-min) (point-max))
684 (mm-destroy-parts (mh-mime-handles mime-data))
685 (remhash (current-buffer) mh-globals-hash))))
688 (defun mh-destroy-postponed-handles ()
689 "Free MIME data for externally displayed MIME parts."
690 (let ((mime-data (mh-buffer-data)))
692 (mm-destroy-parts (mh-mime-handles mime-data)))
693 (remhash (current-buffer) mh-globals-hash)))
695 (defun mh-handle-set-external-undisplayer (folder handle function)
696 "Replacement for `mm-handle-set-external-undisplayer'.
697 This is only called in recent versions of Gnus. The MIME handles are stored
698 in data structures corresponding to MH-E folder buffer FOLDER instead of in
699 Gnus (as in the original). The MIME part, HANDLE is associated with the
700 undisplayer FUNCTION."
701 (if (mm-keep-viewer-alive-p handle)
702 (let ((new-handle (copy-sequence handle)))
703 (mm-handle-set-undisplayer new-handle function)
704 (mm-handle-set-undisplayer handle nil)
707 (push new-handle (mh-mime-handles (mh-buffer-data)))))
708 (mm-handle-set-undisplayer handle function)))
712 ;;; MIME transformations
713 (eval-when-compile (require 'font-lock))
716 (defun mh-add-missing-mime-version-header ()
717 "Some mail programs don't put a MIME-Version header.
718 I have seen this only in spam, so maybe we shouldn't fix this ;-)"
720 (goto-char (point-min))
721 (re-search-forward "\n\n" nil t)
723 (narrow-to-region (point-min) (point))
724 (when (and (message-fetch-field "content-type")
725 (not (message-fetch-field "mime-version")))
726 (goto-char (point-min))
727 (insert "MIME-Version: 1.0\n")))))
729 (defun mh-small-show-buffer-p ()
730 "Check if show buffer is small.
731 This is used to decide if smileys and graphical emphasis will be displayed."
733 (when (and (boundp 'font-lock-maximum-size) font-lock-maximum-size)
734 (cond ((numberp font-lock-maximum-size)
735 (setq max font-lock-maximum-size))
736 ((listp font-lock-maximum-size)
737 (setq max (cdr (or (assoc 'mh-show-mode font-lock-maximum-size)
738 (assoc t font-lock-maximum-size)))))))
739 (or (not (numberp max)) (>= (/ max 8) (buffer-size)))))
742 (defun mh-display-smileys ()
744 (when (and mh-graphical-smileys-flag (mh-small-show-buffer-p))
745 (mh-funcall-if-exists smiley-region (point-min) (point-max))))
748 (defun mh-display-emphasis ()
749 "Display graphical emphasis."
750 (when (and mh-graphical-emphasis-flag (mh-small-show-buffer-p))
751 (flet ((article-goto-body ())) ; shadow this function to do nothing
753 (goto-char (point-min))
754 (article-emphasize)))))
756 ;; Copied from gnus-art.el (should be checked for other cool things that can
757 ;; be added to the buttons)
758 (defvar mh-mime-button-commands
759 '((mh-press-button "\r" "Toggle Display")))
760 (defvar mh-mime-button-map
761 (let ((map (make-sparse-keymap)))
762 (unless (>= (string-to-number emacs-version) 21)
763 ;; XEmacs doesn't care.
764 (set-keymap-parent map mh-show-mode-map))
766 (define-key map [mouse-2] 'mh-push-button))
768 (define-key map '(button2) 'mh-push-button))
769 (dolist (c mh-mime-button-commands)
770 (define-key map (cadr c) (car c)))
772 (defvar mh-mime-button-line-format-alist
777 (defvar mh-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n")
778 (defvar mh-mime-security-button-pressed nil)
779 (defvar mh-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n")
780 (defvar mh-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n")
781 (defvar mh-mime-security-button-line-format-alist
785 (?D pressed-details ?s)))
786 (defvar mh-mime-security-button-map
787 (let ((map (make-sparse-keymap)))
788 (unless (>= (string-to-number emacs-version) 21)
789 (set-keymap-parent map mh-show-mode-map))
790 (define-key map "\r" 'mh-press-button)
792 (define-key map [mouse-2] 'mh-push-button))
794 (define-key map '(button2) 'mh-push-button))
797 (defvar mh-mime-save-parts-directory nil
798 "Default to use for `mh-mime-save-parts-default-directory'.
802 (defun mh-mime-save-parts (prompt)
805 You can save all of the attachments at once with this command. The attachments
806 are saved in the directory specified by the option
807 `mh-mime-save-parts-default-directory' unless you use a prefix argument PROMPT
808 in which case you are prompted for the directory. These directories may be
809 superseded by MH profile components, since this function calls on
810 \"mhstore\" (\"mhn\") to do the work."
812 (let ((msg (if (eq major-mode 'mh-show-mode)
813 (mh-show-buffer-message-number)
815 (folder (if (eq major-mode 'mh-show-mode)
816 mh-show-folder-buffer
818 (command (if (mh-variant-p 'nmh) "mhstore" "mhn"))
822 (equal nil mh-mime-save-parts-default-directory)
823 (equal t mh-mime-save-parts-default-directory))
824 (not mh-mime-save-parts-directory))
825 (read-file-name "Store in directory: " nil nil t nil))
827 (equal t mh-mime-save-parts-default-directory))
828 mh-mime-save-parts-directory)
829 (read-file-name (format
830 "Store in directory: [%s] "
831 mh-mime-save-parts-directory)
832 "" mh-mime-save-parts-directory t ""))
833 ((stringp mh-mime-save-parts-default-directory)
834 mh-mime-save-parts-default-directory)
836 mh-mime-save-parts-directory))))
837 (if (and (equal directory "") mh-mime-save-parts-directory)
838 (setq directory mh-mime-save-parts-directory))
839 (if (not (file-directory-p directory))
840 (message "No directory specified")
841 (if (equal nil mh-mime-save-parts-default-directory)
842 (setq mh-mime-save-parts-directory directory))
844 (set-buffer (get-buffer-create mh-log-buffer))
846 (setq mh-mime-save-parts-directory directory)
847 (let ((initial-size (mh-truncate-log-buffer)))
849 (expand-file-name command mh-progs) nil t nil
850 (mh-list-to-string (list folder msg "-auto")))
851 (if (> (buffer-size) initial-size)
852 (save-window-excursion
853 (switch-to-buffer-other-window mh-log-buffer)
856 ;; Avoid errors if gnus-sum isn't loaded yet...
857 (defvar gnus-newsgroup-charset nil)
858 (defvar gnus-newsgroup-name nil)
860 (defun mh-decode-message-body ()
861 "Decode message based on charset.
862 If message has been encoded for transfer take that into account."
863 (let (ct charset cte)
864 (goto-char (point-min))
865 (re-search-forward "\n\n" nil t)
867 (narrow-to-region (point-min) (point))
868 (setq ct (ignore-errors (mail-header-parse-content-type
869 (message-fetch-field "Content-Type" t)))
870 charset (mail-content-type-get ct 'charset)
871 cte (message-fetch-field "Content-Transfer-Encoding")))
872 (when (stringp cte) (setq cte (mail-header-strip cte)))
873 (when (or (not ct) (equal (car ct) "text/plain"))
875 (narrow-to-region (min (1+ (mh-mail-header-end)) (point-max))
877 (mm-decode-body charset
878 (and cte (intern (downcase
879 (gnus-strip-whitespace cte))))
883 (defun mh-toggle-mh-decode-mime-flag ()
884 "Toggle the value of `mh-decode-mime-flag'."
886 (setq mh-decode-mime-flag (not mh-decode-mime-flag))
888 (message "%s" (if mh-decode-mime-flag
889 "Processing attachments normally"
890 "Displaying raw message")))
893 (defun mh-decode-message-header ()
894 "Decode RFC2047 encoded message header fields."
895 (when mh-decode-mime-flag
896 (let ((buffer-read-only nil))
897 (rfc2047-decode-region (point-min) (mh-mail-header-end)))))
900 (defun mh-mime-display (&optional pre-dissected-handles)
901 "Display (and possibly decode) MIME handles.
902 Optional argument, PRE-DISSECTED-HANDLES is a list of MIME handles. If
903 present they are displayed otherwise the buffer is parsed and then
906 (folder mh-show-folder-buffer)
907 (raw-message-data (buffer-string)))
908 (flet ((mm-handle-set-external-undisplayer
910 (mh-handle-set-external-undisplayer folder handle function)))
911 (goto-char (point-min))
912 (unless (search-forward "\n\n" nil t)
913 (goto-char (point-max))
918 ;; If needed dissect the current buffer
919 (if pre-dissected-handles
920 (setq handles pre-dissected-handles)
921 (setq handles (or (mm-dissect-buffer nil) (mm-uu-dissect)))
922 (setf (mh-mime-handles (mh-buffer-data))
923 (mm-merge-handles handles
924 (mh-mime-handles (mh-buffer-data))))
925 (unless handles (mh-decode-message-body)))
928 (or (not (stringp (car handles))) (cdr handles)))
929 ;; Goto start of message body
930 (goto-char (point-min))
931 (or (search-forward "\n\n" nil t) (goto-char (point-max)))
934 (delete-region (point) (point-max))
936 ;; Display the MIME handles
937 (mh-mime-display-part handles))
938 (t (mh-signature-highlight))))
940 (message "Please report this error. The error message is:\n %s"
941 (error-message-string err))
942 (delete-region (point-min) (point-max))
943 (insert raw-message-data))))))
945 (defun mh-mime-display-part (handle)
946 "Decides the viewer to call based on the type of HANDLE."
947 (cond ((null handle) nil)
948 ((not (stringp (car handle)))
949 (mh-mime-display-single handle))
950 ((equal (car handle) "multipart/alternative")
951 (mh-mime-display-alternative (cdr handle)))
952 ((and mh-pgp-support-flag
953 (or (equal (car handle) "multipart/signed")
954 (equal (car handle) "multipart/encrypted")))
955 (mh-mime-display-security handle))
956 (t (mh-mime-display-mixed (cdr handle)))))
958 (defun mh-mime-display-alternative (handles)
959 "Choose among the alternatives, HANDLES the part that will be displayed.
960 If no part is preferred then all the parts are displayed."
961 (let* ((preferred (mm-preferred-alternative handles))
962 (others (loop for x in handles unless (eq x preferred) collect x)))
963 (cond ((and preferred (stringp (car preferred)))
964 (mh-mime-display-part preferred)
965 (mh-mime-maybe-display-alternatives others))
968 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
969 (mh-mime-display-single preferred)
970 (mh-mime-maybe-display-alternatives others)
971 (goto-char (point-max))))
972 (t (mh-mime-display-mixed handles)))))
974 (defun mh-mime-maybe-display-alternatives (alternatives)
975 "Show buttons for ALTERNATIVES.
976 If `mh-mime-display-alternatives-flag' is non-nil then display buttons for
977 alternative parts that are usually suppressed."
978 (when (and mh-display-buttons-for-alternatives-flag alternatives)
979 (insert "\n----------------------------------------------------\n")
980 (insert "Alternatives:\n")
981 (dolist (x alternatives)
983 (mh-insert-mime-button x (mh-mime-part-index x) nil))
984 (insert "\n----------------------------------------------------\n")))
986 (defun mh-mime-display-mixed (handles)
987 "Display the list of MIME parts, HANDLES recursively."
988 (mapcar #'mh-mime-display-part handles))
990 (defun mh-mime-part-index (handle)
991 "Generate the button number for MIME part, HANDLE.
992 Notice that a hash table is used to display the same number when buttons need
993 to be displayed multiple times (for instance when nested messages are
995 (or (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
996 (setf (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
997 (incf (mh-mime-parts-count (mh-buffer-data))))))
999 (defun mh-small-image-p (handle)
1000 "Decide whether HANDLE is a \"small\" image that can be displayed inline.
1001 This is only useful if a Content-Disposition header is not present."
1002 (let ((media-test (caddr (assoc (car (mm-handle-type handle))
1003 mh-mm-inline-media-tests)))
1004 (mm-inline-large-images t))
1006 (equal (mm-handle-media-supertype handle) "image")
1007 (funcall media-test handle) ; Since mm-inline-large-images is T,
1008 ; this only tells us if the image is
1009 ; something that emacs can display
1010 (let* ((image (mm-get-image handle)))
1011 (or (mh-do-in-xemacs
1012 (and (mh-funcall-if-exists glyphp image)
1013 (< (glyph-width image)
1014 (or mh-max-inline-image-width (window-pixel-width)))
1015 (< (glyph-height image)
1016 (or mh-max-inline-image-height
1017 (window-pixel-height)))))
1019 (let ((size (mh-funcall-if-exists image-size image)))
1021 (< (cdr size) (or mh-max-inline-image-height
1022 (1- (window-height))))
1023 (< (car size) (or mh-max-inline-image-width
1024 (window-width)))))))))))
1026 (defun mh-inline-vcard-p (handle)
1027 "Decide if HANDLE is a vcard that must be displayed inline."
1028 (let ((type (mm-handle-type handle)))
1029 (and (or (featurep 'vcard) (fboundp 'vcard-pretty-print))
1031 (equal (car type) "text/x-vcard")
1035 (goto-char (point-min))
1036 (not (mh-signature-separator-p)))))))
1038 (defun mh-mime-display-single (handle)
1039 "Display a leaf node, HANDLE in the MIME tree."
1040 (let* ((type (mm-handle-media-type handle))
1041 (small-image-flag (mh-small-image-p handle))
1042 (attachmentp (equal (car (mm-handle-disposition handle))
1044 (inlinep (and (equal (car (mm-handle-disposition handle)) "inline")
1045 (mm-inlinable-p handle)
1046 (mm-inlined-p handle)))
1047 (displayp (or inlinep ; show if inline OR
1048 (mh-inline-vcard-p handle); inline vcard OR
1049 (and (not attachmentp) ; if not an attachment
1050 (or small-image-flag ; and small image
1051 ; and user wants inline
1053 (mm-handle-media-supertype handle)
1055 (mm-inlinable-p handle)
1056 (mm-inlined-p handle)))))))
1058 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
1059 (cond ((and mh-pgp-support-flag
1060 (equal type "application/pgp-signature"))
1061 nil) ; skip signatures as they are already handled...
1064 (mh-insert-mime-button handle (mh-mime-part-index handle) nil))
1065 ((and displayp (not mh-display-buttons-for-inline-parts-flag))
1066 (or (mm-display-part handle) (mm-display-part handle))
1067 (mh-signature-highlight handle))
1068 ((and displayp mh-display-buttons-for-inline-parts-flag)
1070 (mh-insert-mime-button handle (mh-mime-part-index handle) nil)
1072 (mh-mm-display-part handle)))
1073 (goto-char (point-max)))))
1075 (defun mh-signature-highlight (&optional handle)
1076 "Highlight message signature in HANDLE.
1077 The optional argument, HANDLE is a MIME handle if the function is being used
1078 to highlight the signature in a MIME part."
1080 (cond ((not handle) "^-- $")
1081 ((not (and (equal (mm-handle-media-supertype handle) "text")
1082 (equal (mm-handle-media-subtype handle) "html")))
1084 ((eq (mh-mm-text-html-renderer) 'lynx) "^ --$")
1087 (goto-char (point-max))
1088 (when (re-search-backward regexp nil t)
1090 (let ((ov (make-overlay (point) (point-max))))
1091 (overlay-put ov 'face 'mh-show-signature)
1092 (overlay-put ov 'evaporate t)))
1094 (set-extent-property (make-extent (point) (point-max))
1095 'face 'mh-show-signature))))))
1101 (defun mh-insert-mime-button (handle index displayed)
1102 "Insert MIME button for HANDLE.
1103 INDEX is the part number that will be DISPLAYED. It is also used by commands
1104 like \"K v\" which operate on individual MIME parts."
1105 ;; The button could be displayed by a previous decode. In that case
1106 ;; undisplay it if we need a hidden button.
1107 (when (and (mm-handle-displayed-p handle) (not displayed))
1108 (mm-display-part handle))
1109 (let ((name (or (mail-content-type-get (mm-handle-type handle) 'name)
1110 (mail-content-type-get (mm-handle-disposition handle)
1112 (mail-content-type-get (mm-handle-type handle) 'url)
1114 (type (mm-handle-media-type handle))
1115 (description (mail-decode-encoded-word-string
1116 (or (mm-handle-description handle) "")))
1117 (dots (if (or displayed (mm-handle-displayed-p handle)) " " "..."))
1118 long-type begin end)
1119 (if (string-match ".*/" name) (setq name (substring name (match-end 0))))
1120 (setq long-type (concat type (and (not (equal name ""))
1121 (concat "; " name))))
1122 (unless (equal description "")
1123 (setq long-type (concat " --- " long-type)))
1124 (unless (bolp) (insert "\n"))
1125 (setq begin (point))
1127 mh-mime-button-line-format mh-mime-button-line-format-alist
1128 `(,@(gnus-local-map-property mh-mime-button-map)
1129 mh-callback mh-mm-display-part
1133 (widget-convert-button
1136 :action 'mh-widget-press-button
1137 :button-keymap mh-mime-button-map
1139 "Mouse-2 click or press RET (in show buffer) to toggle display")
1140 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
1141 (mh-funcall-if-exists overlay-put ov 'evaporate t))))
1143 ;; There is a bug in Gnus inline image display due to which an extra line
1144 ;; gets inserted every time it is viewed. To work around that problem we are
1145 ;; using an extra property 'mh-region to remember the region that is added
1146 ;; when the button is clicked. The region is then deleted to make sure that
1147 ;; no extra lines get inserted.
1148 (defun mh-mm-display-part (handle)
1149 "Toggle display of button for MIME part, HANDLE."
1151 (let ((id (get-text-property (point) 'mh-part))
1153 (window (selected-window))
1154 (mail-parse-charset 'nil)
1155 (mail-parse-ignored-charsets nil)
1156 region buffer-read-only)
1159 (let ((win (get-buffer-window (current-buffer) t)))
1161 (select-window win))
1164 (if (mm-handle-displayed-p handle)
1165 ;; This will remove the part.
1167 ;; Delete the button and displayed part (if any)
1168 (let ((region (get-text-property point 'mh-region)))
1170 (mh-funcall-if-exists
1171 remove-images (car region) (cdr region)))
1172 (mm-display-part handle)
1174 (delete-region (car region) (cdr region))))
1175 ;; Delete button (if it still remains). This happens for
1176 ;; externally displayed parts where the previous step does
1179 (delete-region (point) (progn (forward-line) (point)))))
1181 (delete-region (point) (progn (forward-line 1) (point)))
1182 (narrow-to-region (point) (point))
1183 ;; Maybe we need another unwind-protect here.
1184 (when (equal (mm-handle-media-supertype handle) "image")
1186 (when (and (not (eq (ignore-errors (mm-display-part handle))
1188 (equal (mm-handle-media-supertype handle)
1190 (goto-char (point-min))
1192 (when (equal (mm-handle-media-supertype handle) "text")
1193 (when (eq mh-highlight-citation-style 'gnus)
1194 (mh-gnus-article-highlight-citation))
1195 (mh-display-smileys)
1196 (mh-display-emphasis)
1197 (mh-signature-highlight handle))
1198 (setq region (cons (progn (goto-char (point-min))
1200 (progn (goto-char (point-max))
1201 (point-marker)))))))
1202 (when (window-live-p window)
1203 (select-window window))
1206 (mh-insert-mime-button handle id (mm-handle-displayed-p handle))
1209 (add-text-properties (line-beginning-position) (line-end-position)
1210 `(mh-region ,region)))))))
1213 (defun mh-press-button ()
1214 "View contents of button.
1216 This command is a toggle so if you use it again on the same attachment, the
1217 attachment is hidden."
1219 (let ((mm-inline-media-tests mh-mm-inline-media-tests)
1220 (data (get-text-property (point) 'mh-data))
1221 (function (get-text-property (point) 'mh-callback))
1222 (buffer-read-only nil)
1223 (folder mh-show-folder-buffer))
1224 (flet ((mm-handle-set-external-undisplayer
1226 (mh-handle-set-external-undisplayer folder handle function)))
1227 (when (and function (eolp))
1229 (unwind-protect (and function (funcall function data))
1230 (set-buffer-modified-p nil)))))
1233 (defun mh-push-button (event)
1234 "Click MIME button for EVENT.
1235 If the MIME part is visible then it is removed. Otherwise the part is
1236 displayed. This function is called when the mouse is used to click the MIME
1239 (mh-do-at-event-location event
1240 (let ((folder mh-show-folder-buffer)
1241 (mm-inline-media-tests mh-mm-inline-media-tests)
1242 (data (get-text-property (point) 'mh-data))
1243 (function (get-text-property (point) 'mh-callback)))
1244 (flet ((mm-handle-set-external-undisplayer (handle func)
1245 (mh-handle-set-external-undisplayer folder handle func)))
1246 (and function (funcall function data))))))
1249 (defun mh-mime-save-part ()
1250 "Save MIME part at point."
1252 (let ((data (get-text-property (point) 'mh-data)))
1254 (let ((mm-default-directory
1255 (file-name-as-directory (or mh-mime-save-parts-directory
1256 default-directory))))
1257 (mh-mm-save-part data)
1258 (setq mh-mime-save-parts-directory mm-default-directory)))))
1261 (defun mh-mime-inline-part ()
1262 "Toggle display of the raw MIME part."
1264 (let* ((buffer-read-only nil)
1265 (data (get-text-property (point) 'mh-data))
1266 (inserted-flag (get-text-property (point) 'mh-mime-inserted))
1267 (displayed-flag (mm-handle-displayed-p data))
1270 (cond ((and data (not inserted-flag) (not displayed-flag))
1271 (let ((contents (mm-get-part data)))
1272 (add-text-properties (line-beginning-position) (line-end-position)
1273 '(mh-mime-inserted t))
1274 (setq start (point-marker))
1276 (mm-insert-inline data contents)
1277 (setq end (point-marker))
1278 (add-text-properties
1279 start (progn (goto-char start) (line-end-position))
1280 `(mh-region (,start . ,end)))))
1281 ((and data (or inserted-flag displayed-flag))
1283 (message "MIME part already inserted")))
1285 (set-buffer-modified-p nil)))
1288 (defun mh-display-with-external-viewer (part-index)
1289 "View attachment externally.
1291 If Emacs does not know how to view an attachment, you could save it into a
1292 file and then run some program to open it. It is easier, however, to launch
1293 the program directly from MH-E with this command. While you'll most likely use
1294 this to view spreadsheets and documents, it is also useful to use your browser
1295 to view HTML attachments with higher fidelity than what Emacs can provide.
1297 This command displays the attachment associated with the button under the
1298 cursor. If the cursor is not located over a button, then the cursor first
1299 moves to the next button, wrapping to the beginning of the message if
1300 necessary. You can provide a numeric prefix argument PART-INDEX to view the
1301 attachment labeled with that number.
1303 This command tries to provide a reasonable default for the viewer by calling
1304 the Emacs function `mailcap-mime-info'. This function usually reads the file
1307 (when (consp part-index) (setq part-index (car part-index)))
1308 (mh-folder-mime-action
1311 (let* ((part (get-text-property (point) 'mh-data))
1312 (type (mm-handle-media-type part))
1313 (methods (mapcar (lambda (x) (list (cdr (assoc 'viewer x))))
1314 (mailcap-mime-info type 'all)))
1315 (def (caar methods))
1316 (prompt (format "Viewer%s: " (if def
1317 (format " (default %s)" def)
1319 (method (completing-read prompt methods nil nil nil nil def))
1320 (folder mh-show-folder-buffer)
1321 (buffer-read-only nil))
1322 (when (string-match "^[^% \t]+$" method)
1323 (setq method (concat method " %s")))
1324 (flet ((mm-handle-set-external-undisplayer (handle function)
1325 (mh-handle-set-external-undisplayer folder handle function)))
1326 (unwind-protect (mm-display-external part method)
1327 (set-buffer-modified-p nil)))))
1330 (defun mh-widget-press-button (widget el)
1331 "Callback for widget, WIDGET.
1332 Parameter EL is unused."
1333 (goto-char (widget-get widget :from))
1336 (defun mh-mime-display-security (handle)
1337 "Display PGP encrypted/signed message, HANDLE."
1339 (narrow-to-region (point) (point))
1341 (mh-insert-mime-security-button handle)
1342 (mh-mime-display-mixed (cdr handle))
1344 (let ((mh-mime-security-button-line-format
1345 mh-mime-security-button-end-line-format))
1346 (mh-insert-mime-security-button handle))
1347 (mm-set-handle-multipart-parameter
1348 handle 'mh-region (cons (point-min-marker) (point-max-marker)))))
1350 ;; I rewrote the security part because Gnus doesn't seem to ever minimize
1351 ;; the button. That is once the mime-security button is pressed there seems
1352 ;; to be no way of getting rid of the inserted text.
1353 (defun mh-mime-security-show-details (handle)
1354 "Toggle display of detailed security info for HANDLE."
1355 (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
1357 (let ((mh-mime-security-button-pressed
1358 (not (get-text-property (point) 'mh-button-pressed)))
1359 (mh-mime-security-button-line-format
1360 (get-text-property (point) 'mh-line-format)))
1362 (while (eq (get-text-property (point) 'mh-line-format)
1363 mh-mime-security-button-line-format)
1367 (narrow-to-region (point) (point))
1368 (mh-insert-mime-security-button handle))
1371 (or (text-property-not-all
1373 'mh-line-format mh-mime-security-button-line-format)
1375 (forward-line -1)))))
1377 (defun mh-mime-security-button-face (info)
1378 "Return the button face to use for encrypted/signed mail based on INFO."
1379 (cond ((string-match "OK" info) ;Decrypted mail
1380 mh-show-pgg-good-face)
1381 ((string-match "Failed" info) ;Decryption failed or signature invalid
1382 mh-show-pgg-bad-face)
1383 ((string-match "Undecided" info);Unprocessed mail
1384 mh-show-pgg-unknown-face)
1385 ((string-match "Untrusted" info);Key not trusted
1386 mh-show-pgg-unknown-face)
1387 (t mh-show-pgg-good-face)))
1389 (defun mh-mime-security-press-button (handle)
1390 "Callback from security button for part HANDLE."
1391 (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
1392 (mh-mime-security-show-details handle)
1393 (let ((region (mm-handle-multipart-ctl-parameter handle 'mh-region))
1395 (setq point (point))
1396 (goto-char (car region))
1397 (delete-region (car region) (cdr region))
1398 (with-current-buffer (mm-handle-multipart-ctl-parameter handle 'buffer)
1399 (let* ((mm-verify-option 'known)
1400 (mm-decrypt-option 'known)
1401 (new (mm-possibly-verify-or-decrypt (cdr handle) handle)))
1402 (unless (eq new (cdr handle))
1403 (mm-destroy-parts (cdr handle))
1404 (setcdr handle new))))
1405 (mh-mime-display-security handle)
1406 (goto-char point))))
1408 ;; These variables should already be initialized in mm-decode.el if we have a
1409 ;; recent enough Gnus. The defvars are here to avoid compiler warnings.
1410 (defvar mm-verify-function-alist nil)
1411 (defvar mm-decrypt-function-alist nil)
1413 (defvar pressed-details)
1415 (defun mh-insert-mime-security-button (handle)
1416 "Display buttons for PGP message, HANDLE."
1417 (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
1418 (crypto-type (or (nth 2 (assoc protocol mm-verify-function-alist))
1419 (nth 2 (assoc protocol mm-decrypt-function-alist))
1421 (type (concat crypto-type
1422 (if (equal (car handle) "multipart/signed")
1423 " Signed" " Encrypted")
1425 (info (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
1427 (details (mm-handle-multipart-ctl-parameter handle 'gnus-details))
1428 pressed-details begin end face)
1429 (setq details (if details (concat "\n" details) ""))
1430 (setq pressed-details (if mh-mime-security-button-pressed details ""))
1431 (setq face (mh-mime-security-button-face info))
1432 (unless (bolp) (insert "\n"))
1433 (setq begin (point))
1435 mh-mime-security-button-line-format
1436 mh-mime-security-button-line-format-alist
1437 `(,@(gnus-local-map-property mh-mime-security-button-map)
1438 mh-button-pressed ,mh-mime-security-button-pressed
1439 mh-callback mh-mime-security-press-button
1440 mh-line-format ,mh-mime-security-button-line-format
1443 (widget-convert-button 'link begin end
1445 :action 'mh-widget-press-button
1446 :button-keymap mh-mime-security-button-map
1448 :help-echo "Mouse-2 click or press RET (in show buffer) to see security details.")
1449 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
1450 (mh-funcall-if-exists overlay-put ov 'evaporate t))
1451 (when (equal info "Failed")
1452 (let* ((type (if (equal (car handle) "multipart/signed")
1453 "verification" "decryption"))
1454 (warning (if (equal type "decryption")
1455 "(passphrase may be incorrect)" "")))
1456 (message "%s %s failed %s" crypto-type type warning)))))
1458 (defun mh-mm-inline-message (handle)
1459 "Display message, HANDLE.
1460 The function decodes the message and displays it. It avoids decoding the same
1461 message multiple times."
1463 (clean-message-header mh-clean-message-header-flag)
1464 (invisible-headers mh-invisible-header-fields-compiled)
1465 (visible-headers nil))
1468 (narrow-to-region b b)
1469 (mm-insert-part handle)
1471 (or (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
1472 (setf (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
1473 (let ((handles (or (mm-dissect-buffer nil)
1475 (setf (mh-mime-handles (mh-buffer-data))
1477 handles (mh-mime-handles (mh-buffer-data))))
1480 (goto-char (point-min))
1482 (cond (clean-message-header
1483 (mh-clean-msg-header (point-min)
1486 (goto-char (point-min)))
1488 (mh-start-of-uncleaned-message)))
1489 (mh-decode-message-header)
1491 ;; The other highlighting types don't need anything special
1492 (when (eq mh-highlight-citation-style 'gnus)
1493 (mh-gnus-article-highlight-citation))
1494 (goto-char (point-min))
1495 (insert "\n------- Forwarded Message\n\n")
1496 (mh-display-smileys)
1497 (mh-display-emphasis)
1498 (mm-handle-set-undisplayer
1501 (let (buffer-read-only)
1502 (if (fboundp 'remove-specifier)
1503 ;; This is only valid on XEmacs.
1504 (mapcar (lambda (prop)
1506 (face-property 'default prop) (current-buffer)))
1507 '(background background-pixmap foreground)))
1508 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
1513 ;; indent-tabs-mode: nil
1514 ;; sentence-end-double-space: nil
1517 ;; arch-tag: 0dd36518-1b64-4a84-8f4e-59f422d3f002
1518 ;;; mh-mime.el ends here