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