* mh-comp.el (mh-show-buffer-message-number): Replace (car
[emacs/old-mirror.git] / lisp / mh-e / mh-mime.el
bloba91d7b1212a11c6ef1548810125285b64a1799db
1 ;;; mh-mime.el --- MH-E support for composing MIME messages
3 ;; Copyright (C) 1993, 1995,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
8 ;; Keywords: mail
9 ;; See: mh-e.el
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)
16 ;; any later version.
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.
28 ;;; Commentary:
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
33 ;; or later.
35 ;;; Change Log:
37 ;;; Code:
39 ;;(message "> mh-mime")
40 (eval-when-compile (require 'mh-acros))
41 (mh-require-cl)
43 (require 'gnus-util)
44 (require 'mh-buffers)
45 (require 'mh-comp)
46 (require 'mh-gnus)
47 ;;(message "< mh-mime")
49 (autoload 'article-emphasize "gnus-art")
50 (autoload 'gnus-article-goto-header "gnus-art")
51 (autoload 'gnus-eval-format "gnus-spec")
52 (autoload 'gnus-get-buffer-create "gnus")
53 (autoload 'message-options-set-recipient "message")
54 (autoload 'mm-uu-dissect "mm-uu")
55 (autoload 'mml-unsecure-message "mml-sec")
56 (autoload 'rfc2047-decode-region "rfc2047")
57 (autoload 'widget-convert-button "wid-edit")
59 ;;;###mh-autoload
60 (defun mh-compose-insertion (&optional inline)
61 "Add tag to include a file such as an image or sound.
63 You are prompted for the filename containing the object, the
64 media type if it cannot be determined automatically, and a
65 content description. If you're using MH-style directives, you
66 will also be prompted for additional attributes.
68 The option `mh-compose-insertion' controls what type of tags are
69 inserted. Optional argument INLINE means make it an inline
70 attachment."
71 (interactive "P")
72 (if (equal mh-compose-insertion 'mml)
73 (if inline
74 (mh-mml-attach-file "inline")
75 (mh-mml-attach-file))
76 (call-interactively 'mh-mh-attach-file)))
78 ;;;###mh-autoload
79 (defun mh-compose-forward (&optional description folder range)
80 "Add tag to forward a message.
82 You are prompted for a content DESCRIPTION, the name of the
83 FOLDER in which the messages to forward are located, and a RANGE
84 of messages, which defaults to the current message in that
85 folder. Check the documentation of `mh-interactive-range' to see
86 how RANGE is read in interactive use.
88 The option `mh-compose-insertion' controls what type of tags are inserted."
89 (interactive
90 (let* ((description
91 (mml-minibuffer-read-description))
92 (folder
93 (mh-prompt-for-folder "Message from"
94 mh-sent-from-folder nil))
95 (default
96 (if (and (equal folder mh-sent-from-folder)
97 (numberp mh-sent-from-msg))
98 mh-sent-from-msg
99 (nth 0 (mh-translate-range folder "cur"))))
100 (range
101 (mh-read-range "Forward" folder
102 (or (and default
103 (number-to-string default))
105 t t)))
106 (list description folder range)))
107 (let ((messages (mapconcat 'identity (mh-list-to-string range) " ")))
108 (dolist (message (mh-translate-range folder messages))
109 (if (equal mh-compose-insertion 'mml)
110 (mh-mml-forward-message description folder (format "%s" message))
111 (mh-mh-forward-message description folder (format "%s" message))))))
113 ;; To do:
114 ;; paragraph code should not fill # lines if MIME enabled.
115 ;; implement mh-auto-mh-to-mime (if non-nil, \\[mh-send-letter]
116 ;; invokes mh-mh-to-mime automatically before sending.)
117 ;; actually, instead of mh-auto-mh-to-mime,
118 ;; should read automhnproc from profile
119 ;; MIME option to mh-forward
120 ;; command to move to content-description insertion point
122 (defvar mh-mh-to-mime-args nil
123 "Extra arguments for \\[mh-mh-to-mime] to pass to the \"mhbuild\" command.
124 The arguments are passed to \"mhbuild\" if \\[mh-mh-to-mime] is
125 given a prefix argument. Normally default arguments to
126 \"mhbuild\" are specified in the MH profile.")
128 (defvar mh-media-type-regexp
129 (concat (regexp-opt '("text" "image" "audio" "video" "application"
130 "multipart" "message") t)
131 "/[-.+a-zA-Z0-9]+")
132 "Regexp matching valid media types used in MIME attachment compositions.")
134 (defvar mh-have-file-command 'undefined
135 "Cached value of function `mh-have-file-command'.
136 Do not reference this variable directly as it might not have been
137 initialized. Always use the command `mh-have-file-command'.")
139 ;;;###mh-autoload
140 (defun mh-have-file-command ()
141 "Return t if 'file' command is on the system.
142 'file -i' is used to get MIME type of composition insertion."
143 (when (eq mh-have-file-command 'undefined)
144 (setq mh-have-file-command
145 (and (fboundp 'executable-find)
146 (executable-find "file") ; file command exists
147 ; and accepts -i and -b args.
148 (zerop (call-process "file" nil nil nil "-i" "-b"
149 (expand-file-name "inc" mh-progs))))))
150 mh-have-file-command)
152 (defvar mh-file-mime-type-substitutions
153 '(("application/msword" "\.xls" "application/ms-excel")
154 ("application/msword" "\.ppt" "application/ms-powerpoint")
155 ("text/plain" "\.vcf" "text/x-vcard"))
156 "Substitutions to make for Content-Type returned from file command.
157 The first element is the Content-Type returned by the file command.
158 The second element is a regexp matching the file name, usually the
159 extension.
160 The third element is the Content-Type to replace with.")
162 (defun mh-file-mime-type-substitute (content-type filename)
163 "Return possibly changed CONTENT-TYPE on the FILENAME.
164 Substitutions are made from the `mh-file-mime-type-substitutions'
165 variable."
166 (let ((subst mh-file-mime-type-substitutions)
167 (type) (match) (answer content-type)
168 (case-fold-search t))
169 (while subst
170 (setq type (car (car subst))
171 match (elt (car subst) 1))
172 (if (and (string-equal content-type type)
173 (string-match match filename))
174 (setq answer (elt (car subst) 2)
175 subst nil)
176 (setq subst (cdr subst))))
177 answer))
179 ;;;###mh-autoload
180 (defun mh-file-mime-type (filename)
181 "Return MIME type of FILENAME from file command.
182 Returns nil if file command not on system."
183 (cond
184 ((not (mh-have-file-command))
185 nil) ;no file command, exit now
186 ((not (and (file-exists-p filename)
187 (file-readable-p filename)))
188 nil) ;no file or not readable, ditto
190 (save-excursion
191 (let ((tmp-buffer (get-buffer-create mh-temp-buffer)))
192 (set-buffer tmp-buffer)
193 (unwind-protect
194 (progn
195 (call-process "file" nil '(t nil) nil "-b" "-i"
196 (expand-file-name filename))
197 (goto-char (point-min))
198 (if (not (re-search-forward mh-media-type-regexp nil t))
200 (mh-file-mime-type-substitute (match-string 0) filename)))
201 (kill-buffer tmp-buffer)))))))
203 (defun mh-minibuffer-read-type (filename &optional default)
204 "Return the content type associated with the given FILENAME.
205 If the \"file\" command exists and recognizes the given file,
206 then its value is returned\; otherwise, the user is prompted for
207 a type (see `mailcap-mime-types' and for Emacs 20,
208 `mh-mime-content-types').
209 Optional argument DEFAULT is returned if a type isn't entered."
210 (mailcap-parse-mimetypes)
211 (let* ((default (or default
212 (mm-default-file-encoding filename)
213 "application/octet-stream"))
214 (probed-type (mh-file-mime-type filename))
215 (type (or (and (not (equal probed-type "application/octet-stream"))
216 probed-type)
217 (completing-read
218 (format "Content type (default %s): " default)
219 (mapcar 'list (mailcap-mime-types))))))
220 (if (not (equal type ""))
221 type
222 default)))
224 ;; RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One:
225 ;; Format of Internet Message Bodies.
226 ;; RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two:
227 ;; Media Types.
228 ;; RFC 2049 - Multipurpose Internet Mail Extensions (MIME) Part Five:
229 ;; Conformance Criteria and Examples.
230 ;; RFC 2017 - Definition of the URL MIME External-Body Access-Type
231 ;; RFC 1738 - Uniform Resource Locators (URL)
232 (defvar mh-access-types
233 '(("anon-ftp") ; RFC2046 Anonymous File Transfer Protocol
234 ("file") ; RFC1738 Host-specific file names
235 ("ftp") ; RFC2046 File Transfer Protocol
236 ("gopher") ; RFC1738 The Gopher Protocol
237 ("http") ; RFC1738 Hypertext Transfer Protocol
238 ("local-file") ; RFC2046 Local file access
239 ("mail-server") ; RFC2046 mail-server Electronic mail address
240 ("mailto") ; RFC1738 Electronic mail address
241 ("news") ; RFC1738 Usenet news
242 ("nntp") ; RFC1738 Usenet news using NNTP access
243 ("propspero") ; RFC1738 Prospero Directory Service
244 ("telnet") ; RFC1738 Telnet
245 ("tftp") ; RFC2046 Trivial File Transfer Protocol
246 ("url") ; RFC2017 URL scheme MIME access-type Protocol
247 ("wais")) ; RFC1738 Wide Area Information Servers
248 "Valid MIME access-type values.")
250 ;;;###mh-autoload
251 (defun mh-mh-attach-file (filename type description attributes)
252 "Add a tag to insert a MIME message part from a file.
253 You are prompted for the FILENAME containing the object, the
254 media TYPE if it cannot be determined automatically, and a
255 content DESCRIPTION. In addition, you are also prompted for
256 additional ATTRIBUTES.
258 See also \\[mh-mh-to-mime]."
259 (interactive (let ((filename (mml-minibuffer-read-file "Attach file: ")))
260 (list
261 filename
262 (mh-minibuffer-read-type filename)
263 (mml-minibuffer-read-description)
264 (read-string "Attributes: "
265 (concat "name=\""
266 (file-name-nondirectory filename)
267 "\"")))))
268 (mh-mh-compose-type filename type description attributes))
270 (defun mh-mh-compose-type (filename type
271 &optional description attributes comment)
272 "Insert an MH-style directive to insert a file.
273 The file specified by FILENAME is encoded as TYPE. An optional
274 DESCRIPTION is used as the Content-Description field, optional
275 set of ATTRIBUTES and an optional COMMENT can also be included."
276 (beginning-of-line)
277 (insert "#" type)
278 (and attributes
279 (insert "; " attributes))
280 (and comment
281 (insert " (" comment ")"))
282 (insert " [")
283 (and description
284 (insert description))
285 (insert "] " (expand-file-name filename))
286 (insert "\n"))
288 ;;;###mh-autoload
289 (defun mh-mh-compose-anon-ftp (host filename type description)
290 "Add tag to include anonymous ftp reference to a file.
292 You can have your message initiate an \"ftp\" transfer when the
293 recipient reads the message. You are prompted for the remote HOST
294 and FILENAME, the media TYPE, and the content DESCRIPTION.
296 See also \\[mh-mh-to-mime]."
297 (interactive (list
298 (read-string "Remote host: ")
299 (read-string "Remote filename: ")
300 (mh-minibuffer-read-type "DUMMY-FILENAME")
301 (mml-minibuffer-read-description)))
302 (mh-mh-compose-external-type "anon-ftp" host filename
303 type description))
305 ;;;###mh-autoload
306 (defun mh-mh-compose-external-compressed-tar (host filename description)
307 "Add tag to include anonymous ftp reference to a compressed tar file.
309 In addition to retrieving the file via anonymous \"ftp\" as per
310 the command \\[mh-mh-compose-anon-ftp], the file will also be
311 uncompressed and untarred. You are prompted for the remote HOST
312 and FILENAME and the content DESCRIPTION.
314 See also \\[mh-mh-to-mime]."
315 (interactive (list
316 (read-string "Remote host: ")
317 (read-string "Remote filename: ")
318 (mml-minibuffer-read-description)))
319 (mh-mh-compose-external-type "anon-ftp" host filename
320 "application/octet-stream"
321 description
322 "type=tar; conversions=x-compress"
323 "mode=image"))
325 ;;;###mh-autoload
326 (defun mh-mh-compose-external-type (access-type host filename type
327 &optional description
328 attributes parameters
329 comment)
330 "Add tag to refer to a remote file.
332 This command is a general utility for referencing external files.
333 In fact, all of the other commands that insert directives to
334 access external files call this command. You are prompted for the
335 ACCESS-TYPE, remote HOST and FILENAME, and content TYPE. If you
336 provide a prefix argument, you are also prompted for a content
337 DESCRIPTION, ATTRIBUTES, PARAMETERS, and a COMMENT.
339 See also \\[mh-mh-to-mime]."
340 (interactive (list
341 (completing-read "Access type: " mh-access-types)
342 (read-string "Remote host: ")
343 (read-string "Remote filename: ")
344 (mh-minibuffer-read-type "DUMMY-FILENAME")
345 (if current-prefix-arg (mml-minibuffer-read-description))
346 (if current-prefix-arg (read-string "Attributes: "))
347 (if current-prefix-arg (read-string "Parameters: "))
348 (if current-prefix-arg (read-string "Comment: "))))
349 (beginning-of-line)
350 (insert "#@" type)
351 (and attributes
352 (insert "; " attributes))
353 (and comment
354 (insert " (" comment ") "))
355 (insert " [")
356 (and description
357 (insert description))
358 (insert "] ")
359 (insert "access-type=" access-type "; ")
360 (insert "site=" host)
361 (insert "; name=" (file-name-nondirectory filename))
362 (let ((directory (file-name-directory filename)))
363 (and directory
364 (insert "; directory=\"" directory "\"")))
365 (and parameters
366 (insert "; " parameters))
367 (insert "\n"))
369 ;;;###mh-autoload
370 (defun mh-mh-forward-message (&optional description folder messages)
371 "Add tag to forward a message.
372 You are prompted for a content DESCRIPTION, the name of the
373 FOLDER in which the messages to forward are located, and the
374 MESSAGES' numbers.
376 See also \\[mh-mh-to-mime]."
377 (interactive (list
378 (mml-minibuffer-read-description)
379 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
380 (read-string (concat "Messages"
381 (if (numberp mh-sent-from-msg)
382 (format " (default %d): "
383 mh-sent-from-msg)
384 ": ")))))
385 (beginning-of-line)
386 (insert "#forw [")
387 (and description
388 (not (string= description ""))
389 (insert description))
390 (insert "]")
391 (and folder
392 (not (string= folder ""))
393 (insert " " folder))
394 (if (and messages
395 (not (string= messages "")))
396 (let ((start (point)))
397 (insert " " messages)
398 (subst-char-in-region start (point) ?, ? ))
399 (if (numberp mh-sent-from-msg)
400 (insert " " (int-to-string mh-sent-from-msg))))
401 (insert "\n"))
403 ;;;###mh-autoload
404 (defun mh-mh-to-mime (&optional extra-args)
405 "Compose MIME message from MH-style directives.
407 Typically, you send a message with attachments just like any other
408 message. However, you may take a sneak preview of the MIME encoding if
409 you wish by running this command.
411 If you wish to pass additional arguments to \"mhbuild\" (\"mhn\")
412 to affect how it builds your message, use the option
413 `mh-mh-to-mime-args'. For example, you can build a consistency
414 check into the message by setting `mh-mh-to-mime-args' to
415 \"-check\". The recipient of your message can then run \"mhbuild
416 -check\" on the message--\"mhbuild\" (\"mhn\") will complain if
417 the message has been corrupted on the way. This command only
418 consults this option when given a prefix argument EXTRA-ARGS.
420 The hook `mh-mh-to-mime-hook' is called after the message has been
421 formatted.
423 The effects of this command can be undone by running
424 \\[mh-mh-to-mime-undo]."
425 (interactive "*P")
426 (mh-mh-quote-unescaped-sharp)
427 (save-buffer)
428 (message "Running %s..." (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
429 (cond
430 ((mh-variant-p 'nmh)
431 (mh-exec-cmd-error nil
432 "mhbuild"
433 (if extra-args mh-mh-to-mime-args)
434 buffer-file-name))
436 (mh-exec-cmd-error (format "mhdraft=%s" buffer-file-name)
437 "mhn"
438 (if extra-args mh-mh-to-mime-args)
439 buffer-file-name)))
440 (revert-buffer t t)
441 (message "Running %s...done" (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
442 (run-hooks 'mh-mh-to-mime-hook))
444 (defun mh-mh-quote-unescaped-sharp ()
445 "Quote \"#\" characters that haven't been quoted for \"mhbuild\".
446 If the \"#\" character is present in the first column, but it isn't
447 part of a MH-style directive then \"mhbuild\" gives an error.
448 This function will quote all such characters."
449 (save-excursion
450 (goto-char (point-min))
451 (while (re-search-forward "^#" nil t)
452 (beginning-of-line)
453 (unless (mh-mh-directive-present-p (point) (line-end-position))
454 (insert "#"))
455 (goto-char (line-end-position)))))
457 ;;;###mh-autoload
458 (defun mh-mh-to-mime-undo (noconfirm)
459 "Undo effects of \\[mh-mh-to-mime].
461 It does this by reverting to a backup file. You are prompted to
462 confirm this action, but you can avoid the confirmation by adding
463 a prefix argument NOCONFIRM."
464 (interactive "*P")
465 (if (null buffer-file-name)
466 (error "Buffer does not seem to be associated with any file"))
467 (let ((backup-strings '("," "#"))
468 backup-file)
469 (while (and backup-strings
470 (not (file-exists-p
471 (setq backup-file
472 (concat (file-name-directory buffer-file-name)
473 (car backup-strings)
474 (file-name-nondirectory buffer-file-name)
475 ".orig")))))
476 (setq backup-strings (cdr backup-strings)))
477 (or backup-strings
478 (error "Backup file for %s no longer exists" buffer-file-name))
479 (or noconfirm
480 (yes-or-no-p (format "Revert buffer from file %s? "
481 backup-file))
482 (error "Revert not confirmed"))
483 (let ((buffer-read-only nil))
484 (erase-buffer)
485 (insert-file-contents backup-file))
486 (after-find-file nil)))
488 ;;;###mh-autoload
489 (defun mh-mh-directive-present-p (&optional begin end)
490 "Check if the text between BEGIN and END might be a MH-style directive.
491 The optional argument BEGIN defaults to the beginning of the
492 buffer, while END defaults to the the end of the buffer."
493 (unless begin (setq begin (point-min)))
494 (unless end (setq end (point-max)))
495 (save-excursion
496 (block 'search-for-mh-directive
497 (goto-char begin)
498 (while (re-search-forward "^#" end t)
499 (let ((s (buffer-substring-no-properties (point) (line-end-position))))
500 (cond ((equal s ""))
501 ((string-match "^forw[ \t\n]+" s)
502 (return-from 'search-for-mh-directive t))
503 (t (let ((first-token (car (split-string s "[ \t;@]"))))
504 (when (and first-token
505 (string-match mh-media-type-regexp
506 first-token))
507 (return-from 'search-for-mh-directive t)))))))
508 nil)))
512 ;;; MIME composition functions
514 ;;;###mh-autoload
515 (defun mh-mml-to-mime ()
516 "Compose MIME message from MML tags.
518 Typically, you send a message with attachments just like any
519 other message. However, you may take a sneak preview of the MIME
520 encoding if you wish by running this command.
522 This action can be undone by running \\[undo]."
523 (interactive)
524 (require 'message)
525 (when mh-pgp-support-flag ;; This is only needed for PGP
526 (message-options-set-recipient))
527 (let ((saved-text (buffer-string))
528 (buffer (current-buffer))
529 (modified-flag (buffer-modified-p)))
530 (condition-case err (mml-to-mime)
531 (error
532 (with-current-buffer buffer
533 (delete-region (point-min) (point-max))
534 (insert saved-text)
535 (set-buffer-modified-p modified-flag))
536 (error (error-message-string err))))))
538 ;;;###mh-autoload
539 (defun mh-mml-forward-message (description folder message)
540 "Forward a message as attachment.
542 The function will prompt the user for a DESCRIPTION, a FOLDER and
543 MESSAGE number."
544 (let ((msg (if (and (equal message "") (numberp mh-sent-from-msg))
545 mh-sent-from-msg
546 (string-to-number message))))
547 (cond ((integerp msg)
548 (if (string= "" description)
549 ;; Rationale: mml-attach-file constructs a malformed composition
550 ;; if the description string is empty. This fixes SF #625168.
551 (mml-attach-file (format "%s%s/%d"
552 mh-user-path (substring folder 1) msg)
553 "message/rfc822")
554 (mml-attach-file (format "%s%s/%d"
555 mh-user-path (substring folder 1) msg)
556 "message/rfc822"
557 description)))
558 (t (error "The message number, %s, is not a integer" msg)))))
560 (defvar mh-mml-cryptographic-method-history ())
562 ;;;###mh-autoload
563 (defun mh-mml-query-cryptographic-method ()
564 "Read the cryptographic method to use."
565 (if current-prefix-arg
566 (let ((def (or (car mh-mml-cryptographic-method-history)
567 mh-mml-method-default)))
568 (completing-read (format "Method (default %s): " def)
569 '(("pgp") ("pgpmime") ("smime"))
570 nil t nil 'mh-mml-cryptographic-method-history def))
571 mh-mml-method-default))
573 ;;;###mh-autoload
574 (defun mh-mml-attach-file (&optional disposition)
575 "Add a tag to insert a MIME message part from a file.
577 You are prompted for the filename containing the object, the
578 media type if it cannot be determined automatically, a content
579 description and the DISPOSITION of the attachment.
581 This is basically `mml-attach-file' from Gnus, modified such that a prefix
582 argument yields an \"inline\" disposition and Content-Type is determined
583 automatically."
584 (let* ((file (mml-minibuffer-read-file "Attach file: "))
585 (type (mh-minibuffer-read-type file))
586 (description (mml-minibuffer-read-description))
587 (dispos (or disposition
588 (mml-minibuffer-read-disposition type))))
589 (mml-insert-empty-tag 'part 'type type 'filename file
590 'disposition dispos 'description description)))
592 ;; Shush compiler.
593 (eval-when-compile (defvar mh-identity-pgg-default-user-id))
595 (defun mh-secure-message (method mode &optional identity)
596 "Add tag to encrypt or sign message.
598 METHOD should be one of: \"pgpmime\", \"pgp\", \"smime\".
599 MODE should be one of: \"sign\", \"encrypt\", \"signencrypt\", \"none\".
600 IDENTITY is optionally the default-user-id to use."
601 (if (not mh-pgp-support-flag)
602 (error "Your version of Gnus does not support PGP/GPG")
603 ;; Check the arguments
604 (let ((valid-methods (list "pgpmime" "pgp" "smime"))
605 (valid-modes (list "sign" "encrypt" "signencrypt" "none")))
606 (if (not (member method valid-methods))
607 (error "Method %s is invalid" method))
608 (if (not (member mode valid-modes))
609 (error "Mode %s is invalid" mode))
610 (mml-unsecure-message)
611 (if (not (string= mode "none"))
612 (save-excursion
613 (goto-char (point-min))
614 (mh-goto-header-end 1)
615 (if mh-identity-pgg-default-user-id
616 (mml-insert-tag 'secure 'method method 'mode mode
617 'sender mh-identity-pgg-default-user-id)
618 (mml-insert-tag 'secure 'method method 'mode mode)))))))
620 ;;;###mh-autoload
621 (defun mh-mml-unsecure-message ()
622 "Remove any secure message tags."
623 (interactive)
624 (if (not mh-pgp-support-flag)
625 (error "Your version of Gnus does not support PGP/GPG")
626 (mml-unsecure-message)))
628 ;;;###mh-autoload
629 (defun mh-mml-secure-message-sign (method)
630 "Add tag to sign the message.
632 A proper multipart message is created for you when you send the
633 message. Use the command \\[mh-mml-unsecure-message] to remove
634 this tag. Use a prefix argument METHOD to be prompted for one of
635 the possible security methods (see `mh-mml-method-default')."
636 (interactive (list (mh-mml-query-cryptographic-method)))
637 (mh-secure-message method "sign" mh-identity-pgg-default-user-id))
639 ;;;###mh-autoload
640 (defun mh-mml-secure-message-encrypt (method)
641 "Add tag to encrypt the message.
643 A proper multipart message is created for you when you send the
644 message. Use the command \\[mh-mml-unsecure-message] to remove
645 this tag. Use a prefix argument METHOD to be prompted for one of
646 the possible security methods (see `mh-mml-method-default')."
647 (interactive (list (mh-mml-query-cryptographic-method)))
648 (mh-secure-message method "encrypt" mh-identity-pgg-default-user-id))
650 ;;;###mh-autoload
651 (defun mh-mml-secure-message-signencrypt (method)
652 "Add tag to encrypt and sign the message.
654 A proper multipart message is created for you when you send the
655 message. Use the command \\[mh-mml-unsecure-message] to remove
656 this tag. Use a prefix argument METHOD to be prompted for one of
657 the possible security methods (see `mh-mml-method-default')."
658 (interactive (list (mh-mml-query-cryptographic-method)))
659 (mh-secure-message method "signencrypt" mh-identity-pgg-default-user-id))
661 ;;;###mh-autoload
662 (defun mh-mml-tag-present-p ()
663 "Check if the current buffer has text which may be a MML tag."
664 (save-excursion
665 (goto-char (point-min))
666 (re-search-forward
667 (concat
668 "\\(<#\\(mml\\|part\\)\\(.\\|\n\\)*>[ \n\t]*<#/\\(mml\\|part\\)>\\|"
669 "^<#secure.+>$\\)")
670 nil t)))
674 ;;; MIME cleanup
676 ;;;###mh-autoload
677 (defun mh-mime-cleanup ()
678 "Free the decoded MIME parts."
679 (let ((mime-data (gethash (current-buffer) mh-globals-hash)))
680 ;; This is for Emacs, what about XEmacs?
681 (mh-funcall-if-exists remove-images (point-min) (point-max))
682 (when mime-data
683 (mm-destroy-parts (mh-mime-handles mime-data))
684 (remhash (current-buffer) mh-globals-hash))))
686 ;;;###mh-autoload
687 (defun mh-destroy-postponed-handles ()
688 "Free MIME data for externally displayed MIME parts."
689 (let ((mime-data (mh-buffer-data)))
690 (when mime-data
691 (mm-destroy-parts (mh-mime-handles mime-data)))
692 (remhash (current-buffer) mh-globals-hash)))
694 (defun mh-handle-set-external-undisplayer (folder handle function)
695 "Replacement for `mm-handle-set-external-undisplayer'.
697 This is only called in recent versions of Gnus. The MIME handles
698 are stored in data structures corresponding to MH-E folder buffer
699 FOLDER instead of in Gnus (as in the original). The MIME part,
700 HANDLE is associated with the 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)
705 (save-excursion
706 (set-buffer folder)
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))
715 ;;;###mh-autoload
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
719 this ;-)"
720 (save-excursion
721 (goto-char (point-min))
722 (re-search-forward "\n\n" nil t)
723 (save-restriction
724 (narrow-to-region (point-min) (point))
725 (when (and (message-fetch-field "content-type")
726 (not (message-fetch-field "mime-version")))
727 (goto-char (point-min))
728 (insert "MIME-Version: 1.0\n")))))
730 (defun mh-small-show-buffer-p ()
731 "Check if show buffer is small.
732 This is used to decide if smileys and graphical emphasis will be
733 displayed."
734 (let ((max nil))
735 (when (and (boundp 'font-lock-maximum-size) font-lock-maximum-size)
736 (cond ((numberp font-lock-maximum-size)
737 (setq max font-lock-maximum-size))
738 ((listp font-lock-maximum-size)
739 (setq max (cdr (or (assoc 'mh-show-mode font-lock-maximum-size)
740 (assoc t font-lock-maximum-size)))))))
741 (or (not (numberp max)) (>= (/ max 8) (buffer-size)))))
743 ;;;###mh-autoload
744 (defun mh-display-smileys ()
745 "Display smileys."
746 (when (and mh-graphical-smileys-flag (mh-small-show-buffer-p))
747 (mh-funcall-if-exists smiley-region (point-min) (point-max))))
749 ;;;###mh-autoload
750 (defun mh-display-emphasis ()
751 "Display graphical emphasis."
752 (when (and mh-graphical-emphasis-flag (mh-small-show-buffer-p))
753 (flet ((article-goto-body ())) ; shadow this function to do nothing
754 (save-excursion
755 (goto-char (point-min))
756 (article-emphasize)))))
758 ;; Copied from gnus-art.el (should be checked for other cool things that can
759 ;; be added to the buttons)
760 (defvar mh-mime-button-commands
761 '((mh-press-button "\r" "Toggle Display")))
762 (defvar mh-mime-button-map
763 (let ((map (make-sparse-keymap)))
764 (unless (>= (string-to-number emacs-version) 21)
765 ;; XEmacs doesn't care.
766 (set-keymap-parent map mh-show-mode-map))
767 (mh-do-in-gnu-emacs
768 (define-key map [mouse-2] 'mh-push-button))
769 (mh-do-in-xemacs
770 (define-key map '(button2) 'mh-push-button))
771 (dolist (c mh-mime-button-commands)
772 (define-key map (cadr c) (car c)))
773 map))
774 (defvar mh-mime-button-line-format-alist
775 '((?T long-type ?s)
776 (?d description ?s)
777 (?p index ?s)
778 (?e dots ?s)))
779 (defvar mh-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n")
780 (defvar mh-mime-security-button-pressed nil)
781 (defvar mh-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n")
782 (defvar mh-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n")
783 (defvar mh-mime-security-button-line-format-alist
784 '((?t type ?s)
785 (?i info ?s)
786 (?d details ?s)
787 (?D pressed-details ?s)))
788 (defvar mh-mime-security-button-map
789 (let ((map (make-sparse-keymap)))
790 (unless (>= (string-to-number emacs-version) 21)
791 (set-keymap-parent map mh-show-mode-map))
792 (define-key map "\r" 'mh-press-button)
793 (mh-do-in-gnu-emacs
794 (define-key map [mouse-2] 'mh-push-button))
795 (mh-do-in-xemacs
796 (define-key map '(button2) 'mh-push-button))
797 map))
799 (defvar mh-mime-save-parts-directory nil
800 "Default to use for `mh-mime-save-parts-default-directory'.
801 Set from last use.")
803 ;;;###mh-autoload
804 (defun mh-mime-save-parts (prompt)
805 "Save attachments.
807 You can save all of the attachments at once with this command.
808 The attachments are saved in the directory specified by the
809 option `mh-mime-save-parts-default-directory' unless you use a
810 prefix argument PROMPT in which case you are prompted for the
811 directory. These directories may be superseded by MH profile
812 components, since this function calls on \"mhstore\" (\"mhn\") to
813 do the work."
814 (interactive "P")
815 (let ((msg (if (eq major-mode 'mh-show-mode)
816 (mh-show-buffer-message-number)
817 (mh-get-msg-num t)))
818 (folder (if (eq major-mode 'mh-show-mode)
819 mh-show-folder-buffer
820 mh-current-folder))
821 (command (if (mh-variant-p 'nmh) "mhstore" "mhn"))
822 (directory
823 (cond
824 ((and (or prompt
825 (equal nil mh-mime-save-parts-default-directory)
826 (equal t mh-mime-save-parts-default-directory))
827 (not mh-mime-save-parts-directory))
828 (read-file-name "Store in directory: " nil nil t nil))
829 ((and (or prompt
830 (equal t mh-mime-save-parts-default-directory))
831 mh-mime-save-parts-directory)
832 (read-file-name (format
833 "Store in directory (default %s): "
834 mh-mime-save-parts-directory)
835 "" mh-mime-save-parts-directory t ""))
836 ((stringp mh-mime-save-parts-default-directory)
837 mh-mime-save-parts-default-directory)
839 mh-mime-save-parts-directory))))
840 (if (and (equal directory "") mh-mime-save-parts-directory)
841 (setq directory mh-mime-save-parts-directory))
842 (if (not (file-directory-p directory))
843 (message "No directory specified")
844 (if (equal nil mh-mime-save-parts-default-directory)
845 (setq mh-mime-save-parts-directory directory))
846 (save-excursion
847 (set-buffer (get-buffer-create mh-log-buffer))
848 (cd directory)
849 (setq mh-mime-save-parts-directory directory)
850 (let ((initial-size (mh-truncate-log-buffer)))
851 (apply 'call-process
852 (expand-file-name command mh-progs) nil t nil
853 (mh-list-to-string (list folder msg "-auto")))
854 (if (> (buffer-size) initial-size)
855 (save-window-excursion
856 (switch-to-buffer-other-window mh-log-buffer)
857 (sit-for 3))))))))
859 ;; Avoid errors if gnus-sum isn't loaded yet...
860 (defvar gnus-newsgroup-charset nil)
861 (defvar gnus-newsgroup-name nil)
863 (defun mh-decode-message-body ()
864 "Decode message based on charset.
865 If message has been encoded for transfer take that into account."
866 (let (ct charset cte)
867 (goto-char (point-min))
868 (re-search-forward "\n\n" nil t)
869 (save-restriction
870 (narrow-to-region (point-min) (point))
871 (setq ct (ignore-errors (mail-header-parse-content-type
872 (message-fetch-field "Content-Type" t)))
873 charset (mail-content-type-get ct 'charset)
874 cte (message-fetch-field "Content-Transfer-Encoding")))
875 (when (stringp cte) (setq cte (mail-header-strip cte)))
876 (when (or (not ct) (equal (car ct) "text/plain"))
877 (save-restriction
878 (narrow-to-region (min (1+ (mh-mail-header-end)) (point-max))
879 (point-max))
880 (mm-decode-body charset
881 (and cte (intern (downcase
882 (gnus-strip-whitespace cte))))
883 (car ct))))))
885 ;;;###mh-autoload
886 (defun mh-toggle-mh-decode-mime-flag ()
887 "Toggle the value of `mh-decode-mime-flag'."
888 (interactive)
889 (setq mh-decode-mime-flag (not mh-decode-mime-flag))
890 (mh-show nil t)
891 (message "%s" (if mh-decode-mime-flag
892 "Processing attachments normally"
893 "Displaying raw message")))
895 ;;;###mh-autoload
896 (defun mh-decode-message-header ()
897 "Decode RFC2047 encoded message header fields."
898 (when mh-decode-mime-flag
899 (let ((buffer-read-only nil))
900 (rfc2047-decode-region (point-min) (mh-mail-header-end)))))
902 ;;;###mh-autoload
903 (defun mh-mime-display (&optional pre-dissected-handles)
904 "Display (and possibly decode) MIME handles.
905 Optional argument, PRE-DISSECTED-HANDLES is a list of MIME
906 handles. If present they are displayed otherwise the buffer is
907 parsed and then displayed."
908 (let ((handles ())
909 (folder mh-show-folder-buffer)
910 (raw-message-data (buffer-string)))
911 (flet ((mm-handle-set-external-undisplayer
912 (handle function)
913 (mh-handle-set-external-undisplayer folder handle function)))
914 (goto-char (point-min))
915 (unless (search-forward "\n\n" nil t)
916 (goto-char (point-max))
917 (insert "\n\n"))
919 (condition-case err
920 (progn
921 ;; If needed dissect the current buffer
922 (if pre-dissected-handles
923 (setq handles pre-dissected-handles)
924 (if (setq handles (mm-dissect-buffer nil))
925 (when (fboundp 'mm-uu-dissect-text-parts)
926 (mm-uu-dissect-text-parts handles))
927 (setq handles (mm-uu-dissect)))
928 (setf (mh-mime-handles (mh-buffer-data))
929 (mm-merge-handles handles
930 (mh-mime-handles (mh-buffer-data))))
931 (unless handles (mh-decode-message-body)))
933 (cond ((and handles
934 (or (not (stringp (car handles))) (cdr handles)))
935 ;; Goto start of message body
936 (goto-char (point-min))
937 (or (search-forward "\n\n" nil t) (goto-char (point-max)))
939 ;; Delete the body
940 (delete-region (point) (point-max))
942 ;; Display the MIME handles
943 (mh-mime-display-part handles))
944 (t (mh-signature-highlight))))
945 (error
946 (message "Could not display body: %s" (error-message-string err))
947 (delete-region (point-min) (point-max))
948 (insert raw-message-data))))))
950 (defun mh-mime-display-part (handle)
951 "Decides the viewer to call based on the type of HANDLE."
952 (cond ((null handle) nil)
953 ((not (stringp (car handle)))
954 (mh-mime-display-single handle))
955 ((equal (car handle) "multipart/alternative")
956 (mh-mime-display-alternative (cdr handle)))
957 ((and mh-pgp-support-flag
958 (or (equal (car handle) "multipart/signed")
959 (equal (car handle) "multipart/encrypted")))
960 (mh-mime-display-security handle))
961 (t (mh-mime-display-mixed (cdr handle)))))
963 (defun mh-mime-display-alternative (handles)
964 "Choose among the alternatives, HANDLES the part that will be displayed.
965 If no part is preferred then all the parts are displayed."
966 (let* ((preferred (mm-preferred-alternative handles))
967 (others (loop for x in handles unless (eq x preferred) collect x)))
968 (cond ((and preferred (stringp (car preferred)))
969 (mh-mime-display-part preferred)
970 (mh-mime-maybe-display-alternatives others))
971 (preferred
972 (save-restriction
973 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
974 (mh-mime-display-single preferred)
975 (mh-mime-maybe-display-alternatives others)
976 (goto-char (point-max))))
977 (t (mh-mime-display-mixed handles)))))
979 (defun mh-mime-maybe-display-alternatives (alternatives)
980 "Show buttons for ALTERNATIVES.
981 If `mh-mime-display-alternatives-flag' is non-nil then display
982 buttons for alternative parts that are usually suppressed."
983 (when (and mh-display-buttons-for-alternatives-flag alternatives)
984 (insert "\n----------------------------------------------------\n")
985 (insert "Alternatives:\n")
986 (dolist (x alternatives)
987 (insert "\n")
988 (mh-insert-mime-button x (mh-mime-part-index x) nil))
989 (insert "\n----------------------------------------------------\n")))
991 (defun mh-mime-display-mixed (handles)
992 "Display the list of MIME parts, HANDLES recursively."
993 (mapcar #'mh-mime-display-part handles))
995 (defun mh-mime-part-index (handle)
996 "Generate the button number for MIME part, HANDLE.
997 Notice that a hash table is used to display the same number when
998 buttons need to be displayed multiple times (for instance when
999 nested messages are opened)."
1000 (or (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
1001 (setf (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
1002 (incf (mh-mime-parts-count (mh-buffer-data))))))
1004 (defun mh-small-image-p (handle)
1005 "Decide whether HANDLE is a \"small\" image that can be displayed inline.
1006 This is only useful if a Content-Disposition header is not present."
1007 (let ((media-test (caddr (assoc (car (mm-handle-type handle))
1008 mh-mm-inline-media-tests)))
1009 (mm-inline-large-images t))
1010 (and media-test
1011 (equal (mm-handle-media-supertype handle) "image")
1012 (funcall media-test handle) ; Since mm-inline-large-images is T,
1013 ; this only tells us if the image is
1014 ; something that emacs can display
1015 (let* ((image (mm-get-image handle)))
1016 (or (mh-do-in-xemacs
1017 (and (mh-funcall-if-exists glyphp image)
1018 (< (glyph-width image)
1019 (or mh-max-inline-image-width (window-pixel-width)))
1020 (< (glyph-height image)
1021 (or mh-max-inline-image-height
1022 (window-pixel-height)))))
1023 (mh-do-in-gnu-emacs
1024 (let ((size (mh-funcall-if-exists image-size image)))
1025 (and size
1026 (< (cdr size) (or mh-max-inline-image-height
1027 (1- (window-height))))
1028 (< (car size) (or mh-max-inline-image-width
1029 (window-width)))))))))))
1031 (defun mh-inline-vcard-p (handle)
1032 "Decide if HANDLE is a vcard that must be displayed inline."
1033 (let ((type (mm-handle-type handle)))
1034 (and (or (featurep 'vcard) (fboundp 'vcard-pretty-print))
1035 (consp type)
1036 (equal (car type) "text/x-vcard")
1037 (save-excursion
1038 (save-restriction
1039 (widen)
1040 (goto-char (point-min))
1041 (not (mh-signature-separator-p)))))))
1043 (defun mh-mime-display-single (handle)
1044 "Display a leaf node, HANDLE in the MIME tree."
1045 (let* ((type (mm-handle-media-type handle))
1046 (small-image-flag (mh-small-image-p handle))
1047 (attachmentp (equal (car (mm-handle-disposition handle))
1048 "attachment"))
1049 (inlinep (and (equal (car (mm-handle-disposition handle)) "inline")
1050 (mm-inlinable-p handle)
1051 (mm-inlined-p handle)))
1052 (displayp (or inlinep ; show if inline OR
1053 (mh-inline-vcard-p handle); inline vcard OR
1054 (and (not attachmentp) ; if not an attachment
1055 (or small-image-flag ; and small image
1056 ; and user wants inline
1057 (and (not (equal
1058 (mm-handle-media-supertype handle)
1059 "image"))
1060 (mm-inlinable-p handle)
1061 (mm-inlined-p handle)))))))
1062 (save-restriction
1063 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
1064 (cond ((and mh-pgp-support-flag
1065 (equal type "application/pgp-signature"))
1066 nil) ; skip signatures as they are already handled...
1067 ((not displayp)
1068 (insert "\n")
1069 (mh-insert-mime-button handle (mh-mime-part-index handle) nil))
1070 ((and displayp (not mh-display-buttons-for-inline-parts-flag))
1071 (or (mm-display-part handle) (mm-display-part handle))
1072 (mh-signature-highlight handle))
1073 ((and displayp mh-display-buttons-for-inline-parts-flag)
1074 (insert "\n")
1075 (mh-insert-mime-button handle (mh-mime-part-index handle) nil)
1076 (forward-line -1)
1077 (mh-mm-display-part handle)))
1078 (goto-char (point-max)))))
1080 (defun mh-signature-highlight (&optional handle)
1081 "Highlight message signature in HANDLE.
1082 The optional argument, HANDLE is a MIME handle if the function is
1083 being used to highlight the signature in a MIME part."
1084 (let ((regexp
1085 (cond ((not handle) "^-- $")
1086 ((not (and (equal (mm-handle-media-supertype handle) "text")
1087 (equal (mm-handle-media-subtype handle) "html")))
1088 "^-- $")
1089 ((eq (mh-mm-text-html-renderer) 'lynx) "^ --$")
1090 (t "^--$"))))
1091 (save-excursion
1092 (goto-char (point-max))
1093 (when (re-search-backward regexp nil t)
1094 (mh-do-in-gnu-emacs
1095 (let ((ov (make-overlay (point) (point-max))))
1096 (overlay-put ov 'face 'mh-show-signature)
1097 (overlay-put ov 'evaporate t)))
1098 (mh-do-in-xemacs
1099 (set-extent-property (make-extent (point) (point-max))
1100 'face 'mh-show-signature))))))
1102 (mh-do-in-xemacs
1103 (defvar dots)
1104 (defvar type))
1106 (defun mh-insert-mime-button (handle index displayed)
1107 "Insert MIME button for HANDLE.
1108 INDEX is the part number that will be DISPLAYED. It is also used
1109 by commands like \"K v\" which operate on individual MIME parts."
1110 ;; The button could be displayed by a previous decode. In that case
1111 ;; undisplay it if we need a hidden button.
1112 (when (and (mm-handle-displayed-p handle) (not displayed))
1113 (mm-display-part handle))
1114 (let ((name (or (mail-content-type-get (mm-handle-type handle) 'name)
1115 (mail-content-type-get (mm-handle-disposition handle)
1116 'filename)
1117 (mail-content-type-get (mm-handle-type handle) 'url)
1118 ""))
1119 (type (mm-handle-media-type handle))
1120 (description (mail-decode-encoded-word-string
1121 (or (mm-handle-description handle) "")))
1122 (dots (if (or displayed (mm-handle-displayed-p handle)) " " "..."))
1123 long-type begin end)
1124 (if (string-match ".*/" name) (setq name (substring name (match-end 0))))
1125 (setq long-type (concat type (and (not (equal name ""))
1126 (concat "; " name))))
1127 (unless (equal description "")
1128 (setq long-type (concat " --- " long-type)))
1129 (unless (bolp) (insert "\n"))
1130 (setq begin (point))
1131 (gnus-eval-format
1132 mh-mime-button-line-format mh-mime-button-line-format-alist
1133 `(,@(gnus-local-map-property mh-mime-button-map)
1134 mh-callback mh-mm-display-part
1135 mh-part ,index
1136 mh-data ,handle))
1137 (setq end (point))
1138 (widget-convert-button
1139 'link begin end
1140 :mime-handle handle
1141 :action 'mh-widget-press-button
1142 :button-keymap mh-mime-button-map
1143 :help-echo
1144 "Mouse-2 click or press RET (in show buffer) to toggle display")
1145 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
1146 (mh-funcall-if-exists overlay-put ov 'evaporate t))))
1148 ;; There is a bug in Gnus inline image display due to which an extra line
1149 ;; gets inserted every time it is viewed. To work around that problem we are
1150 ;; using an extra property 'mh-region to remember the region that is added
1151 ;; when the button is clicked. The region is then deleted to make sure that
1152 ;; no extra lines get inserted.
1153 (defun mh-mm-display-part (handle)
1154 "Toggle display of button for MIME part, HANDLE."
1155 (beginning-of-line)
1156 (let ((id (get-text-property (point) 'mh-part))
1157 (point (point))
1158 (window (selected-window))
1159 (mail-parse-charset 'nil)
1160 (mail-parse-ignored-charsets nil)
1161 region buffer-read-only)
1162 (save-excursion
1163 (unwind-protect
1164 (let ((win (get-buffer-window (current-buffer) t)))
1165 (when win
1166 (select-window win))
1167 (goto-char point)
1169 (if (mm-handle-displayed-p handle)
1170 ;; This will remove the part.
1171 (progn
1172 ;; Delete the button and displayed part (if any)
1173 (let ((region (get-text-property point 'mh-region)))
1174 (when region
1175 (mh-funcall-if-exists
1176 remove-images (car region) (cdr region)))
1177 (mm-display-part handle)
1178 (when region
1179 (delete-region (car region) (cdr region))))
1180 ;; Delete button (if it still remains). This happens for
1181 ;; externally displayed parts where the previous step does
1182 ;; nothing.
1183 (unless (eolp)
1184 (delete-region (point) (progn (forward-line) (point)))))
1185 (save-restriction
1186 (delete-region (point) (progn (forward-line 1) (point)))
1187 (narrow-to-region (point) (point))
1188 ;; Maybe we need another unwind-protect here.
1189 (when (equal (mm-handle-media-supertype handle) "image")
1190 (insert "\n"))
1191 (when (and (not (eq (ignore-errors (mm-display-part handle))
1192 'inline))
1193 (equal (mm-handle-media-supertype handle)
1194 "image"))
1195 (goto-char (point-min))
1196 (delete-char 1))
1197 (when (equal (mm-handle-media-supertype handle) "text")
1198 (when (eq mh-highlight-citation-style 'gnus)
1199 (mh-gnus-article-highlight-citation))
1200 (mh-display-smileys)
1201 (mh-display-emphasis)
1202 (mh-signature-highlight handle))
1203 (setq region (cons (progn (goto-char (point-min))
1204 (point-marker))
1205 (progn (goto-char (point-max))
1206 (point-marker)))))))
1207 (when (window-live-p window)
1208 (select-window window))
1209 (goto-char point)
1210 (beginning-of-line)
1211 (mh-insert-mime-button handle id (mm-handle-displayed-p handle))
1212 (goto-char point)
1213 (when region
1214 (add-text-properties (line-beginning-position) (line-end-position)
1215 `(mh-region ,region)))))))
1217 ;;;###mh-autoload
1218 (defun mh-press-button ()
1219 "View contents of button.
1221 This command is a toggle so if you use it again on the same
1222 attachment, the attachment is hidden."
1223 (interactive)
1224 (let ((mm-inline-media-tests mh-mm-inline-media-tests)
1225 (data (get-text-property (point) 'mh-data))
1226 (function (get-text-property (point) 'mh-callback))
1227 (buffer-read-only nil)
1228 (folder mh-show-folder-buffer))
1229 (flet ((mm-handle-set-external-undisplayer
1230 (handle function)
1231 (mh-handle-set-external-undisplayer folder handle function)))
1232 (when (and function (eolp))
1233 (backward-char))
1234 (unwind-protect (and function (funcall function data))
1235 (set-buffer-modified-p nil)))))
1237 ;;;###mh-autoload
1238 (defun mh-push-button (event)
1239 "Click MIME button for EVENT.
1241 If the MIME part is visible then it is removed. Otherwise the
1242 part is displayed. This function is called when the mouse is used
1243 to click the MIME button."
1244 (interactive "e")
1245 (mh-do-at-event-location event
1246 (let ((folder mh-show-folder-buffer)
1247 (mm-inline-media-tests mh-mm-inline-media-tests)
1248 (data (get-text-property (point) 'mh-data))
1249 (function (get-text-property (point) 'mh-callback)))
1250 (flet ((mm-handle-set-external-undisplayer (handle func)
1251 (mh-handle-set-external-undisplayer folder handle func)))
1252 (and function (funcall function data))))))
1254 ;;;###mh-autoload
1255 (defun mh-mime-save-part ()
1256 "Save MIME part at point."
1257 (interactive)
1258 (let ((data (get-text-property (point) 'mh-data)))
1259 (when data
1260 (let ((mm-default-directory
1261 (file-name-as-directory (or mh-mime-save-parts-directory
1262 default-directory))))
1263 (mh-mm-save-part data)
1264 (setq mh-mime-save-parts-directory mm-default-directory)))))
1266 ;;;###mh-autoload
1267 (defun mh-mime-inline-part ()
1268 "Toggle display of the raw MIME part."
1269 (interactive)
1270 (let* ((buffer-read-only nil)
1271 (data (get-text-property (point) 'mh-data))
1272 (inserted-flag (get-text-property (point) 'mh-mime-inserted))
1273 (displayed-flag (mm-handle-displayed-p data))
1274 (point (point))
1275 start end)
1276 (cond ((and data (not inserted-flag) (not displayed-flag))
1277 (let ((contents (mm-get-part data)))
1278 (add-text-properties (line-beginning-position) (line-end-position)
1279 '(mh-mime-inserted t))
1280 (setq start (point-marker))
1281 (forward-line 1)
1282 (mm-insert-inline data contents)
1283 (setq end (point-marker))
1284 (add-text-properties
1285 start (progn (goto-char start) (line-end-position))
1286 `(mh-region (,start . ,end)))))
1287 ((and data (or inserted-flag displayed-flag))
1288 (mh-press-button)
1289 (message "MIME part already inserted")))
1290 (goto-char point)
1291 (set-buffer-modified-p nil)))
1293 ;;;###mh-autoload
1294 (defun mh-display-with-external-viewer (part-index)
1295 "View attachment externally.
1297 If Emacs does not know how to view an attachment, you could save
1298 it into a file and then run some program to open it. It is
1299 easier, however, to launch the program directly from MH-E with
1300 this command. While you'll most likely use this to view
1301 spreadsheets and documents, it is also useful to use your browser
1302 to view HTML attachments with higher fidelity than what Emacs can
1303 provide.
1305 This command displays the attachment associated with the button
1306 under the cursor. If the cursor is not located over a button,
1307 then the cursor first moves to the next button, wrapping to the
1308 beginning of the message if necessary. You can provide a numeric
1309 prefix argument PART-INDEX to view the attachment labeled with
1310 that number.
1312 This command tries to provide a reasonable default for the viewer
1313 by calling the Emacs function `mailcap-mime-info'. This function
1314 usually reads the file \"/etc/mailcap\"."
1315 (interactive "P")
1316 (when (consp part-index) (setq part-index (car part-index)))
1317 (mh-folder-mime-action
1318 part-index
1319 #'(lambda ()
1320 (let* ((part (get-text-property (point) 'mh-data))
1321 (type (mm-handle-media-type part))
1322 (methods (mapcar (lambda (x) (list (cdr (assoc 'viewer x))))
1323 (mailcap-mime-info type 'all)))
1324 (def (caar methods))
1325 (prompt (format "Viewer%s: " (if def
1326 (format " (default %s)" def)
1327 "")))
1328 (method (completing-read prompt methods nil nil nil nil def))
1329 (folder mh-show-folder-buffer)
1330 (buffer-read-only nil))
1331 (when (string-match "^[^% \t]+$" method)
1332 (setq method (concat method " %s")))
1333 (flet ((mm-handle-set-external-undisplayer (handle function)
1334 (mh-handle-set-external-undisplayer folder handle function)))
1335 (unwind-protect (mm-display-external part method)
1336 (set-buffer-modified-p nil)))))
1337 nil))
1339 (defun mh-widget-press-button (widget el)
1340 "Callback for widget, WIDGET.
1341 Parameter EL is unused."
1342 (goto-char (widget-get widget :from))
1343 (mh-press-button))
1345 (defun mh-mime-display-security (handle)
1346 "Display PGP encrypted/signed message, HANDLE."
1347 (save-restriction
1348 (narrow-to-region (point) (point))
1349 (insert "\n")
1350 (mh-insert-mime-security-button handle)
1351 (mh-mime-display-mixed (cdr handle))
1352 (insert "\n")
1353 (let ((mh-mime-security-button-line-format
1354 mh-mime-security-button-end-line-format))
1355 (mh-insert-mime-security-button handle))
1356 (mm-set-handle-multipart-parameter
1357 handle 'mh-region (cons (point-min-marker) (point-max-marker)))))
1359 ;; I rewrote the security part because Gnus doesn't seem to ever minimize
1360 ;; the button. That is once the mime-security button is pressed there seems
1361 ;; to be no way of getting rid of the inserted text.
1362 (defun mh-mime-security-show-details (handle)
1363 "Toggle display of detailed security info for HANDLE."
1364 (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
1365 (when details
1366 (let ((mh-mime-security-button-pressed
1367 (not (get-text-property (point) 'mh-button-pressed)))
1368 (mh-mime-security-button-line-format
1369 (get-text-property (point) 'mh-line-format)))
1370 (forward-char -1)
1371 (while (eq (get-text-property (point) 'mh-line-format)
1372 mh-mime-security-button-line-format)
1373 (forward-char -1))
1374 (forward-char)
1375 (save-restriction
1376 (narrow-to-region (point) (point))
1377 (mh-insert-mime-security-button handle))
1378 (delete-region
1379 (point)
1380 (or (text-property-not-all
1381 (point) (point-max)
1382 'mh-line-format mh-mime-security-button-line-format)
1383 (point-max)))
1384 (forward-line -1)))))
1386 (defun mh-mime-security-button-face (info)
1387 "Return the button face to use for encrypted/signed mail based on INFO."
1388 (cond ((string-match "OK" info) ;Decrypted mail
1389 'mh-show-pgg-good)
1390 ((string-match "Failed" info) ;Decryption failed or signature invalid
1391 'mh-show-pgg-bad)
1392 ((string-match "Undecided" info);Unprocessed mail
1393 'mh-show-pgg-unknown)
1394 ((string-match "Untrusted" info);Key not trusted
1395 'mh-show-pgg-unknown)
1397 'mh-show-pgg-good)))
1399 (defun mh-mime-security-press-button (handle)
1400 "Callback from security button for part HANDLE."
1401 (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
1402 (mh-mime-security-show-details handle)
1403 (let ((region (mm-handle-multipart-ctl-parameter handle 'mh-region))
1404 point)
1405 (setq point (point))
1406 (goto-char (car region))
1407 (delete-region (car region) (cdr region))
1408 (with-current-buffer (mm-handle-multipart-ctl-parameter handle 'buffer)
1409 (let* ((mm-verify-option 'known)
1410 (mm-decrypt-option 'known)
1411 (new (mm-possibly-verify-or-decrypt (cdr handle) handle)))
1412 (unless (eq new (cdr handle))
1413 (mm-destroy-parts (cdr handle))
1414 (setcdr handle new))))
1415 (mh-mime-display-security handle)
1416 (goto-char point))))
1418 ;; Shush compiler.
1419 (eval-when-compile
1420 (defvar mm-verify-function-alist nil)
1421 (defvar mm-decrypt-function-alist nil))
1423 (defvar pressed-details)
1425 (defun mh-insert-mime-security-button (handle)
1426 "Display buttons for PGP message, HANDLE."
1427 (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
1428 (crypto-type (or (nth 2 (assoc protocol mm-verify-function-alist))
1429 (nth 2 (assoc protocol mm-decrypt-function-alist))
1430 "Unknown"))
1431 (type (concat crypto-type
1432 (if (equal (car handle) "multipart/signed")
1433 " Signed" " Encrypted")
1434 " Part"))
1435 (info (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
1436 "Undecided"))
1437 (details (mm-handle-multipart-ctl-parameter handle 'gnus-details))
1438 pressed-details begin end face)
1439 (setq details (if details (concat "\n" details) ""))
1440 (setq pressed-details (if mh-mime-security-button-pressed details ""))
1441 (setq face (mh-mime-security-button-face info))
1442 (unless (bolp) (insert "\n"))
1443 (setq begin (point))
1444 (gnus-eval-format
1445 mh-mime-security-button-line-format
1446 mh-mime-security-button-line-format-alist
1447 `(,@(gnus-local-map-property mh-mime-security-button-map)
1448 mh-button-pressed ,mh-mime-security-button-pressed
1449 mh-callback mh-mime-security-press-button
1450 mh-line-format ,mh-mime-security-button-line-format
1451 mh-data ,handle))
1452 (setq end (point))
1453 (widget-convert-button 'link begin end
1454 :mime-handle handle
1455 :action 'mh-widget-press-button
1456 :button-keymap mh-mime-security-button-map
1457 :button-face face
1458 :help-echo "Mouse-2 click or press RET (in show buffer) to see security details.")
1459 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
1460 (mh-funcall-if-exists overlay-put ov 'evaporate t))
1461 (when (equal info "Failed")
1462 (let* ((type (if (equal (car handle) "multipart/signed")
1463 "verification" "decryption"))
1464 (warning (if (equal type "decryption")
1465 "(passphrase may be incorrect)" "")))
1466 (message "%s %s failed %s" crypto-type type warning)))))
1468 (defun mh-mm-inline-message (handle)
1469 "Display message, HANDLE.
1470 The function decodes the message and displays it. It avoids
1471 decoding the same message multiple times."
1472 (let ((b (point))
1473 (clean-message-header mh-clean-message-header-flag)
1474 (invisible-headers mh-invisible-header-fields-compiled)
1475 (visible-headers nil))
1476 (save-excursion
1477 (save-restriction
1478 (narrow-to-region b b)
1479 (mm-insert-part handle)
1480 (mh-mime-display
1481 (or (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
1482 (setf (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
1483 (let ((handles (mm-dissect-buffer nil)))
1484 (if handles
1485 (when (fboundp 'mm-uu-dissect-text-parts)
1486 (mm-uu-dissect-text-parts handles))
1487 (setq handles (mm-uu-dissect)))
1488 (setf (mh-mime-handles (mh-buffer-data))
1489 (mm-merge-handles
1490 handles (mh-mime-handles (mh-buffer-data))))
1491 handles))))
1493 (goto-char (point-min))
1494 (mh-show-xface)
1495 (cond (clean-message-header
1496 (mh-clean-msg-header (point-min)
1497 invisible-headers
1498 visible-headers)
1499 (goto-char (point-min)))
1501 (mh-start-of-uncleaned-message)))
1502 (mh-decode-message-header)
1503 (mh-show-addr)
1504 ;; The other highlighting types don't need anything special
1505 (when (eq mh-highlight-citation-style 'gnus)
1506 (mh-gnus-article-highlight-citation))
1507 (goto-char (point-min))
1508 (insert "\n------- Forwarded Message\n\n")
1509 (mh-display-smileys)
1510 (mh-display-emphasis)
1511 (mm-handle-set-undisplayer
1512 handle
1513 `(lambda ()
1514 (let (buffer-read-only)
1515 (if (fboundp 'remove-specifier)
1516 ;; This is only valid on XEmacs.
1517 (mapcar (lambda (prop)
1518 (remove-specifier
1519 (face-property 'default prop) (current-buffer)))
1520 '(background background-pixmap foreground)))
1521 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
1523 (provide 'mh-mime)
1525 ;; Local Variables:
1526 ;; indent-tabs-mode: nil
1527 ;; sentence-end-double-space: nil
1528 ;; End:
1530 ;; arch-tag: 0dd36518-1b64-4a84-8f4e-59f422d3f002
1531 ;;; mh-mime.el ends here