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