]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-mime.el
Merged from miles@gnu.org--gnu-2005 (patch 677)
[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 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 (prompt)
803 "Save attachments.
804
805 You can save all of the attachments at once with this command. The attachments
806 are saved in the directory specified by the option
807 `mh-mime-save-parts-default-directory' unless you use a prefix argument PROMPT
808 in which case you are prompted for the directory. These directories may be
809 superseded by MH profile components, since this function calls on
810 \"mhstore\" (\"mhn\") to do the work."
811 (interactive "P")
812 (let ((msg (if (eq major-mode 'mh-show-mode)
813 (mh-show-buffer-message-number)
814 (mh-get-msg-num t)))
815 (folder (if (eq major-mode 'mh-show-mode)
816 mh-show-folder-buffer
817 mh-current-folder))
818 (command (if (mh-variant-p 'nmh) "mhstore" "mhn"))
819 (directory
820 (cond
821 ((and (or prompt
822 (equal nil mh-mime-save-parts-default-directory)
823 (equal t mh-mime-save-parts-default-directory))
824 (not mh-mime-save-parts-directory))
825 (read-file-name "Store in directory: " nil nil t nil))
826 ((and (or prompt
827 (equal t mh-mime-save-parts-default-directory))
828 mh-mime-save-parts-directory)
829 (read-file-name (format
830 "Store in directory: [%s] "
831 mh-mime-save-parts-directory)
832 "" mh-mime-save-parts-directory t ""))
833 ((stringp mh-mime-save-parts-default-directory)
834 mh-mime-save-parts-default-directory)
835 (t
836 mh-mime-save-parts-directory))))
837 (if (and (equal directory "") mh-mime-save-parts-directory)
838 (setq directory mh-mime-save-parts-directory))
839 (if (not (file-directory-p directory))
840 (message "No directory specified")
841 (if (equal nil mh-mime-save-parts-default-directory)
842 (setq mh-mime-save-parts-directory directory))
843 (save-excursion
844 (set-buffer (get-buffer-create mh-log-buffer))
845 (cd directory)
846 (setq mh-mime-save-parts-directory directory)
847 (let ((initial-size (mh-truncate-log-buffer)))
848 (apply 'call-process
849 (expand-file-name command mh-progs) nil t nil
850 (mh-list-to-string (list folder msg "-auto")))
851 (if (> (buffer-size) initial-size)
852 (save-window-excursion
853 (switch-to-buffer-other-window mh-log-buffer)
854 (sit-for 3))))))))
855
856 ;; Avoid errors if gnus-sum isn't loaded yet...
857 (defvar gnus-newsgroup-charset nil)
858 (defvar gnus-newsgroup-name nil)
859
860 (defun mh-decode-message-body ()
861 "Decode message based on charset.
862 If message has been encoded for transfer take that into account."
863 (let (ct charset cte)
864 (goto-char (point-min))
865 (re-search-forward "\n\n" nil t)
866 (save-restriction
867 (narrow-to-region (point-min) (point))
868 (setq ct (ignore-errors (mail-header-parse-content-type
869 (message-fetch-field "Content-Type" t)))
870 charset (mail-content-type-get ct 'charset)
871 cte (message-fetch-field "Content-Transfer-Encoding")))
872 (when (stringp cte) (setq cte (mail-header-strip cte)))
873 (when (or (not ct) (equal (car ct) "text/plain"))
874 (save-restriction
875 (narrow-to-region (min (1+ (mh-mail-header-end)) (point-max))
876 (point-max))
877 (mm-decode-body charset
878 (and cte (intern (downcase
879 (gnus-strip-whitespace cte))))
880 (car ct))))))
881
882 ;;;###mh-autoload
883 (defun mh-toggle-mh-decode-mime-flag ()
884 "Toggle the value of `mh-decode-mime-flag'."
885 (interactive)
886 (setq mh-decode-mime-flag (not mh-decode-mime-flag))
887 (mh-show nil t)
888 (message "%s" (if mh-decode-mime-flag
889 "Processing attachments normally"
890 "Displaying raw message")))
891
892 ;;;###mh-autoload
893 (defun mh-decode-message-header ()
894 "Decode RFC2047 encoded message header fields."
895 (when mh-decode-mime-flag
896 (let ((buffer-read-only nil))
897 (rfc2047-decode-region (point-min) (mh-mail-header-end)))))
898
899 ;;;###mh-autoload
900 (defun mh-mime-display (&optional pre-dissected-handles)
901 "Display (and possibly decode) MIME handles.
902 Optional argument, PRE-DISSECTED-HANDLES is a list of MIME handles. If
903 present they are displayed otherwise the buffer is parsed and then
904 displayed."
905 (let ((handles ())
906 (folder mh-show-folder-buffer)
907 (raw-message-data (buffer-string)))
908 (flet ((mm-handle-set-external-undisplayer
909 (handle function)
910 (mh-handle-set-external-undisplayer folder handle function)))
911 (goto-char (point-min))
912 (unless (search-forward "\n\n" nil t)
913 (goto-char (point-max))
914 (insert "\n\n"))
915
916 (condition-case err
917 (progn
918 ;; If needed dissect the current buffer
919 (if pre-dissected-handles
920 (setq handles pre-dissected-handles)
921 (setq handles (or (mm-dissect-buffer nil) (mm-uu-dissect)))
922 (setf (mh-mime-handles (mh-buffer-data))
923 (mm-merge-handles handles
924 (mh-mime-handles (mh-buffer-data))))
925 (unless handles (mh-decode-message-body)))
926
927 (cond ((and handles
928 (or (not (stringp (car handles))) (cdr handles)))
929 ;; Goto start of message body
930 (goto-char (point-min))
931 (or (search-forward "\n\n" nil t) (goto-char (point-max)))
932
933 ;; Delete the body
934 (delete-region (point) (point-max))
935
936 ;; Display the MIME handles
937 (mh-mime-display-part handles))
938 (t (mh-signature-highlight))))
939 (error
940 (message "Please report this error. The error message is:\n %s"
941 (error-message-string err))
942 (delete-region (point-min) (point-max))
943 (insert raw-message-data))))))
944
945 (defun mh-mime-display-part (handle)
946 "Decides the viewer to call based on the type of HANDLE."
947 (cond ((null handle) nil)
948 ((not (stringp (car handle)))
949 (mh-mime-display-single handle))
950 ((equal (car handle) "multipart/alternative")
951 (mh-mime-display-alternative (cdr handle)))
952 ((and mh-pgp-support-flag
953 (or (equal (car handle) "multipart/signed")
954 (equal (car handle) "multipart/encrypted")))
955 (mh-mime-display-security handle))
956 (t (mh-mime-display-mixed (cdr handle)))))
957
958 (defun mh-mime-display-alternative (handles)
959 "Choose among the alternatives, HANDLES the part that will be displayed.
960 If no part is preferred then all the parts are displayed."
961 (let* ((preferred (mm-preferred-alternative handles))
962 (others (loop for x in handles unless (eq x preferred) collect x)))
963 (cond ((and preferred (stringp (car preferred)))
964 (mh-mime-display-part preferred)
965 (mh-mime-maybe-display-alternatives others))
966 (preferred
967 (save-restriction
968 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
969 (mh-mime-display-single preferred)
970 (mh-mime-maybe-display-alternatives others)
971 (goto-char (point-max))))
972 (t (mh-mime-display-mixed handles)))))
973
974 (defun mh-mime-maybe-display-alternatives (alternatives)
975 "Show buttons for ALTERNATIVES.
976 If `mh-mime-display-alternatives-flag' is non-nil then display buttons for
977 alternative parts that are usually suppressed."
978 (when (and mh-display-buttons-for-alternatives-flag alternatives)
979 (insert "\n----------------------------------------------------\n")
980 (insert "Alternatives:\n")
981 (dolist (x alternatives)
982 (insert "\n")
983 (mh-insert-mime-button x (mh-mime-part-index x) nil))
984 (insert "\n----------------------------------------------------\n")))
985
986 (defun mh-mime-display-mixed (handles)
987 "Display the list of MIME parts, HANDLES recursively."
988 (mapcar #'mh-mime-display-part handles))
989
990 (defun mh-mime-part-index (handle)
991 "Generate the button number for MIME part, HANDLE.
992 Notice that a hash table is used to display the same number when buttons need
993 to be displayed multiple times (for instance when nested messages are
994 opened)."
995 (or (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
996 (setf (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
997 (incf (mh-mime-parts-count (mh-buffer-data))))))
998
999 (defun mh-small-image-p (handle)
1000 "Decide whether HANDLE is a \"small\" image that can be displayed inline.
1001 This is only useful if a Content-Disposition header is not present."
1002 (let ((media-test (caddr (assoc (car (mm-handle-type handle))
1003 mh-mm-inline-media-tests)))
1004 (mm-inline-large-images t))
1005 (and media-test
1006 (equal (mm-handle-media-supertype handle) "image")
1007 (funcall media-test handle) ; Since mm-inline-large-images is T,
1008 ; this only tells us if the image is
1009 ; something that emacs can display
1010 (let* ((image (mm-get-image handle)))
1011 (or (mh-do-in-xemacs
1012 (and (mh-funcall-if-exists glyphp image)
1013 (< (glyph-width image)
1014 (or mh-max-inline-image-width (window-pixel-width)))
1015 (< (glyph-height image)
1016 (or mh-max-inline-image-height
1017 (window-pixel-height)))))
1018 (mh-do-in-gnu-emacs
1019 (let ((size (mh-funcall-if-exists image-size image)))
1020 (and size
1021 (< (cdr size) (or mh-max-inline-image-height
1022 (1- (window-height))))
1023 (< (car size) (or mh-max-inline-image-width
1024 (window-width)))))))))))
1025
1026 (defun mh-inline-vcard-p (handle)
1027 "Decide if HANDLE is a vcard that must be displayed inline."
1028 (let ((type (mm-handle-type handle)))
1029 (and (or (featurep 'vcard) (fboundp 'vcard-pretty-print))
1030 (consp type)
1031 (equal (car type) "text/x-vcard")
1032 (save-excursion
1033 (save-restriction
1034 (widen)
1035 (goto-char (point-min))
1036 (not (mh-signature-separator-p)))))))
1037
1038 (defun mh-mime-display-single (handle)
1039 "Display a leaf node, HANDLE in the MIME tree."
1040 (let* ((type (mm-handle-media-type handle))
1041 (small-image-flag (mh-small-image-p handle))
1042 (attachmentp (equal (car (mm-handle-disposition handle))
1043 "attachment"))
1044 (inlinep (and (equal (car (mm-handle-disposition handle)) "inline")
1045 (mm-inlinable-p handle)
1046 (mm-inlined-p handle)))
1047 (displayp (or inlinep ; show if inline OR
1048 (mh-inline-vcard-p handle); inline vcard OR
1049 (and (not attachmentp) ; if not an attachment
1050 (or small-image-flag ; and small image
1051 ; and user wants inline
1052 (and (not (equal
1053 (mm-handle-media-supertype handle)
1054 "image"))
1055 (mm-inlinable-p handle)
1056 (mm-inlined-p handle)))))))
1057 (save-restriction
1058 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
1059 (cond ((and mh-pgp-support-flag
1060 (equal type "application/pgp-signature"))
1061 nil) ; skip signatures as they are already handled...
1062 ((not displayp)
1063 (insert "\n")
1064 (mh-insert-mime-button handle (mh-mime-part-index handle) nil))
1065 ((and displayp (not mh-display-buttons-for-inline-parts-flag))
1066 (or (mm-display-part handle) (mm-display-part handle))
1067 (mh-signature-highlight handle))
1068 ((and displayp mh-display-buttons-for-inline-parts-flag)
1069 (insert "\n")
1070 (mh-insert-mime-button handle (mh-mime-part-index handle) nil)
1071 (forward-line -1)
1072 (mh-mm-display-part handle)))
1073 (goto-char (point-max)))))
1074
1075 (defun mh-signature-highlight (&optional handle)
1076 "Highlight message signature in HANDLE.
1077 The optional argument, HANDLE is a MIME handle if the function is being used
1078 to highlight the signature in a MIME part."
1079 (let ((regexp
1080 (cond ((not handle) "^-- $")
1081 ((not (and (equal (mm-handle-media-supertype handle) "text")
1082 (equal (mm-handle-media-subtype handle) "html")))
1083 "^-- $")
1084 ((eq (mh-mm-text-html-renderer) 'lynx) "^ --$")
1085 (t "^--$"))))
1086 (save-excursion
1087 (goto-char (point-max))
1088 (when (re-search-backward regexp nil t)
1089 (mh-do-in-gnu-emacs
1090 (let ((ov (make-overlay (point) (point-max))))
1091 (overlay-put ov 'face 'mh-show-signature)
1092 (overlay-put ov 'evaporate t)))
1093 (mh-do-in-xemacs
1094 (set-extent-property (make-extent (point) (point-max))
1095 'face 'mh-show-signature))))))
1096
1097 (mh-do-in-xemacs
1098 (defvar dots)
1099 (defvar type))
1100
1101 (defun mh-insert-mime-button (handle index displayed)
1102 "Insert MIME button for HANDLE.
1103 INDEX is the part number that will be DISPLAYED. It is also used by commands
1104 like \"K v\" which operate on individual MIME parts."
1105 ;; The button could be displayed by a previous decode. In that case
1106 ;; undisplay it if we need a hidden button.
1107 (when (and (mm-handle-displayed-p handle) (not displayed))
1108 (mm-display-part handle))
1109 (let ((name (or (mail-content-type-get (mm-handle-type handle) 'name)
1110 (mail-content-type-get (mm-handle-disposition handle)
1111 'filename)
1112 (mail-content-type-get (mm-handle-type handle) 'url)
1113 ""))
1114 (type (mm-handle-media-type handle))
1115 (description (mail-decode-encoded-word-string
1116 (or (mm-handle-description handle) "")))
1117 (dots (if (or displayed (mm-handle-displayed-p handle)) " " "..."))
1118 long-type begin end)
1119 (if (string-match ".*/" name) (setq name (substring name (match-end 0))))
1120 (setq long-type (concat type (and (not (equal name ""))
1121 (concat "; " name))))
1122 (unless (equal description "")
1123 (setq long-type (concat " --- " long-type)))
1124 (unless (bolp) (insert "\n"))
1125 (setq begin (point))
1126 (gnus-eval-format
1127 mh-mime-button-line-format mh-mime-button-line-format-alist
1128 `(,@(gnus-local-map-property mh-mime-button-map)
1129 mh-callback mh-mm-display-part
1130 mh-part ,index
1131 mh-data ,handle))
1132 (setq end (point))
1133 (widget-convert-button
1134 'link begin end
1135 :mime-handle handle
1136 :action 'mh-widget-press-button
1137 :button-keymap mh-mime-button-map
1138 :help-echo
1139 "Mouse-2 click or press RET (in show buffer) to toggle display")
1140 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
1141 (mh-funcall-if-exists overlay-put ov 'evaporate t))))
1142
1143 ;; There is a bug in Gnus inline image display due to which an extra line
1144 ;; gets inserted every time it is viewed. To work around that problem we are
1145 ;; using an extra property 'mh-region to remember the region that is added
1146 ;; when the button is clicked. The region is then deleted to make sure that
1147 ;; no extra lines get inserted.
1148 (defun mh-mm-display-part (handle)
1149 "Toggle display of button for MIME part, HANDLE."
1150 (beginning-of-line)
1151 (let ((id (get-text-property (point) 'mh-part))
1152 (point (point))
1153 (window (selected-window))
1154 (mail-parse-charset 'nil)
1155 (mail-parse-ignored-charsets nil)
1156 region buffer-read-only)
1157 (save-excursion
1158 (unwind-protect
1159 (let ((win (get-buffer-window (current-buffer) t)))
1160 (when win
1161 (select-window win))
1162 (goto-char point)
1163
1164 (if (mm-handle-displayed-p handle)
1165 ;; This will remove the part.
1166 (progn
1167 ;; Delete the button and displayed part (if any)
1168 (let ((region (get-text-property point 'mh-region)))
1169 (when region
1170 (mh-funcall-if-exists
1171 remove-images (car region) (cdr region)))
1172 (mm-display-part handle)
1173 (when region
1174 (delete-region (car region) (cdr region))))
1175 ;; Delete button (if it still remains). This happens for
1176 ;; externally displayed parts where the previous step does
1177 ;; nothing.
1178 (unless (eolp)
1179 (delete-region (point) (progn (forward-line) (point)))))
1180 (save-restriction
1181 (delete-region (point) (progn (forward-line 1) (point)))
1182 (narrow-to-region (point) (point))
1183 ;; Maybe we need another unwind-protect here.
1184 (when (equal (mm-handle-media-supertype handle) "image")
1185 (insert "\n"))
1186 (when (and (not (eq (ignore-errors (mm-display-part handle))
1187 'inline))
1188 (equal (mm-handle-media-supertype handle)
1189 "image"))
1190 (goto-char (point-min))
1191 (delete-char 1))
1192 (when (equal (mm-handle-media-supertype handle) "text")
1193 (when (eq mh-highlight-citation-style 'gnus)
1194 (mh-gnus-article-highlight-citation))
1195 (mh-display-smileys)
1196 (mh-display-emphasis)
1197 (mh-signature-highlight handle))
1198 (setq region (cons (progn (goto-char (point-min))
1199 (point-marker))
1200 (progn (goto-char (point-max))
1201 (point-marker)))))))
1202 (when (window-live-p window)
1203 (select-window window))
1204 (goto-char point)
1205 (beginning-of-line)
1206 (mh-insert-mime-button handle id (mm-handle-displayed-p handle))
1207 (goto-char point)
1208 (when region
1209 (add-text-properties (line-beginning-position) (line-end-position)
1210 `(mh-region ,region)))))))
1211
1212 ;;;###mh-autoload
1213 (defun mh-press-button ()
1214 "View contents of button.
1215
1216 This command is a toggle so if you use it again on the same attachment, the
1217 attachment is hidden."
1218 (interactive)
1219 (let ((mm-inline-media-tests mh-mm-inline-media-tests)
1220 (data (get-text-property (point) 'mh-data))
1221 (function (get-text-property (point) 'mh-callback))
1222 (buffer-read-only nil)
1223 (folder mh-show-folder-buffer))
1224 (flet ((mm-handle-set-external-undisplayer
1225 (handle function)
1226 (mh-handle-set-external-undisplayer folder handle function)))
1227 (when (and function (eolp))
1228 (backward-char))
1229 (unwind-protect (and function (funcall function data))
1230 (set-buffer-modified-p nil)))))
1231
1232 ;;;###mh-autoload
1233 (defun mh-push-button (event)
1234 "Click MIME button for EVENT.
1235 If the MIME part is visible then it is removed. Otherwise the part is
1236 displayed. This function is called when the mouse is used to click the MIME
1237 button."
1238 (interactive "e")
1239 (mh-do-at-event-location event
1240 (let ((folder mh-show-folder-buffer)
1241 (mm-inline-media-tests mh-mm-inline-media-tests)
1242 (data (get-text-property (point) 'mh-data))
1243 (function (get-text-property (point) 'mh-callback)))
1244 (flet ((mm-handle-set-external-undisplayer (handle func)
1245 (mh-handle-set-external-undisplayer folder handle func)))
1246 (and function (funcall function data))))))
1247
1248 ;;;###mh-autoload
1249 (defun mh-mime-save-part ()
1250 "Save MIME part at point."
1251 (interactive)
1252 (let ((data (get-text-property (point) 'mh-data)))
1253 (when data
1254 (let ((mm-default-directory
1255 (file-name-as-directory (or mh-mime-save-parts-directory
1256 default-directory))))
1257 (mh-mm-save-part data)
1258 (setq mh-mime-save-parts-directory mm-default-directory)))))
1259
1260 ;;;###mh-autoload
1261 (defun mh-mime-inline-part ()
1262 "Toggle display of the raw MIME part."
1263 (interactive)
1264 (let* ((buffer-read-only nil)
1265 (data (get-text-property (point) 'mh-data))
1266 (inserted-flag (get-text-property (point) 'mh-mime-inserted))
1267 (displayed-flag (mm-handle-displayed-p data))
1268 (point (point))
1269 start end)
1270 (cond ((and data (not inserted-flag) (not displayed-flag))
1271 (let ((contents (mm-get-part data)))
1272 (add-text-properties (line-beginning-position) (line-end-position)
1273 '(mh-mime-inserted t))
1274 (setq start (point-marker))
1275 (forward-line 1)
1276 (mm-insert-inline data contents)
1277 (setq end (point-marker))
1278 (add-text-properties
1279 start (progn (goto-char start) (line-end-position))
1280 `(mh-region (,start . ,end)))))
1281 ((and data (or inserted-flag displayed-flag))
1282 (mh-press-button)
1283 (message "MIME part already inserted")))
1284 (goto-char point)
1285 (set-buffer-modified-p nil)))
1286
1287 ;;;###mh-autoload
1288 (defun mh-display-with-external-viewer (part-index)
1289 "View attachment externally.
1290
1291 If Emacs does not know how to view an attachment, you could save it into a
1292 file and then run some program to open it. It is easier, however, to launch
1293 the program directly from MH-E with this command. While you'll most likely use
1294 this to view spreadsheets and documents, it is also useful to use your browser
1295 to view HTML attachments with higher fidelity than what Emacs can provide.
1296
1297 This command displays the attachment associated with the button under the
1298 cursor. If the cursor is not located over a button, then the cursor first
1299 moves to the next button, wrapping to the beginning of the message if
1300 necessary. You can provide a numeric prefix argument PART-INDEX to view the
1301 attachment labeled with that number.
1302
1303 This command tries to provide a reasonable default for the viewer by calling
1304 the Emacs function `mailcap-mime-info'. This function usually reads the file
1305 \"/etc/mailcap\"."
1306 (interactive "P")
1307 (when (consp part-index) (setq part-index (car part-index)))
1308 (mh-folder-mime-action
1309 part-index
1310 #'(lambda ()
1311 (let* ((part (get-text-property (point) 'mh-data))
1312 (type (mm-handle-media-type part))
1313 (methods (mapcar (lambda (x) (list (cdr (assoc 'viewer x))))
1314 (mailcap-mime-info type 'all)))
1315 (def (caar methods))
1316 (prompt (format "Viewer%s: " (if def
1317 (format " (default %s)" def)
1318 "")))
1319 (method (completing-read prompt methods nil nil nil nil def))
1320 (folder mh-show-folder-buffer)
1321 (buffer-read-only nil))
1322 (when (string-match "^[^% \t]+$" method)
1323 (setq method (concat method " %s")))
1324 (flet ((mm-handle-set-external-undisplayer (handle function)
1325 (mh-handle-set-external-undisplayer folder handle function)))
1326 (unwind-protect (mm-display-external part method)
1327 (set-buffer-modified-p nil)))))
1328 nil))
1329
1330 (defun mh-widget-press-button (widget el)
1331 "Callback for widget, WIDGET.
1332 Parameter EL is unused."
1333 (goto-char (widget-get widget :from))
1334 (mh-press-button))
1335
1336 (defun mh-mime-display-security (handle)
1337 "Display PGP encrypted/signed message, HANDLE."
1338 (save-restriction
1339 (narrow-to-region (point) (point))
1340 (insert "\n")
1341 (mh-insert-mime-security-button handle)
1342 (mh-mime-display-mixed (cdr handle))
1343 (insert "\n")
1344 (let ((mh-mime-security-button-line-format
1345 mh-mime-security-button-end-line-format))
1346 (mh-insert-mime-security-button handle))
1347 (mm-set-handle-multipart-parameter
1348 handle 'mh-region (cons (point-min-marker) (point-max-marker)))))
1349
1350 ;; I rewrote the security part because Gnus doesn't seem to ever minimize
1351 ;; the button. That is once the mime-security button is pressed there seems
1352 ;; to be no way of getting rid of the inserted text.
1353 (defun mh-mime-security-show-details (handle)
1354 "Toggle display of detailed security info for HANDLE."
1355 (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
1356 (when details
1357 (let ((mh-mime-security-button-pressed
1358 (not (get-text-property (point) 'mh-button-pressed)))
1359 (mh-mime-security-button-line-format
1360 (get-text-property (point) 'mh-line-format)))
1361 (forward-char -1)
1362 (while (eq (get-text-property (point) 'mh-line-format)
1363 mh-mime-security-button-line-format)
1364 (forward-char -1))
1365 (forward-char)
1366 (save-restriction
1367 (narrow-to-region (point) (point))
1368 (mh-insert-mime-security-button handle))
1369 (delete-region
1370 (point)
1371 (or (text-property-not-all
1372 (point) (point-max)
1373 'mh-line-format mh-mime-security-button-line-format)
1374 (point-max)))
1375 (forward-line -1)))))
1376
1377 (defun mh-mime-security-button-face (info)
1378 "Return the button face to use for encrypted/signed mail based on INFO."
1379 (cond ((string-match "OK" info) ;Decrypted mail
1380 mh-show-pgg-good-face)
1381 ((string-match "Failed" info) ;Decryption failed or signature invalid
1382 mh-show-pgg-bad-face)
1383 ((string-match "Undecided" info);Unprocessed mail
1384 mh-show-pgg-unknown-face)
1385 ((string-match "Untrusted" info);Key not trusted
1386 mh-show-pgg-unknown-face)
1387 (t mh-show-pgg-good-face)))
1388
1389 (defun mh-mime-security-press-button (handle)
1390 "Callback from security button for part HANDLE."
1391 (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
1392 (mh-mime-security-show-details handle)
1393 (let ((region (mm-handle-multipart-ctl-parameter handle 'mh-region))
1394 point)
1395 (setq point (point))
1396 (goto-char (car region))
1397 (delete-region (car region) (cdr region))
1398 (with-current-buffer (mm-handle-multipart-ctl-parameter handle 'buffer)
1399 (let* ((mm-verify-option 'known)
1400 (mm-decrypt-option 'known)
1401 (new (mm-possibly-verify-or-decrypt (cdr handle) handle)))
1402 (unless (eq new (cdr handle))
1403 (mm-destroy-parts (cdr handle))
1404 (setcdr handle new))))
1405 (mh-mime-display-security handle)
1406 (goto-char point))))
1407
1408 ;; These variables should already be initialized in mm-decode.el if we have a
1409 ;; recent enough Gnus. The defvars are here to avoid compiler warnings.
1410 (defvar mm-verify-function-alist nil)
1411 (defvar mm-decrypt-function-alist nil)
1412
1413 (defvar pressed-details)
1414
1415 (defun mh-insert-mime-security-button (handle)
1416 "Display buttons for PGP message, HANDLE."
1417 (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
1418 (crypto-type (or (nth 2 (assoc protocol mm-verify-function-alist))
1419 (nth 2 (assoc protocol mm-decrypt-function-alist))
1420 "Unknown"))
1421 (type (concat crypto-type
1422 (if (equal (car handle) "multipart/signed")
1423 " Signed" " Encrypted")
1424 " Part"))
1425 (info (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
1426 "Undecided"))
1427 (details (mm-handle-multipart-ctl-parameter handle 'gnus-details))
1428 pressed-details begin end face)
1429 (setq details (if details (concat "\n" details) ""))
1430 (setq pressed-details (if mh-mime-security-button-pressed details ""))
1431 (setq face (mh-mime-security-button-face info))
1432 (unless (bolp) (insert "\n"))
1433 (setq begin (point))
1434 (gnus-eval-format
1435 mh-mime-security-button-line-format
1436 mh-mime-security-button-line-format-alist
1437 `(,@(gnus-local-map-property mh-mime-security-button-map)
1438 mh-button-pressed ,mh-mime-security-button-pressed
1439 mh-callback mh-mime-security-press-button
1440 mh-line-format ,mh-mime-security-button-line-format
1441 mh-data ,handle))
1442 (setq end (point))
1443 (widget-convert-button 'link begin end
1444 :mime-handle handle
1445 :action 'mh-widget-press-button
1446 :button-keymap mh-mime-security-button-map
1447 :button-face face
1448 :help-echo "Mouse-2 click or press RET (in show buffer) to see security details.")
1449 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
1450 (mh-funcall-if-exists overlay-put ov 'evaporate t))
1451 (when (equal info "Failed")
1452 (let* ((type (if (equal (car handle) "multipart/signed")
1453 "verification" "decryption"))
1454 (warning (if (equal type "decryption")
1455 "(passphrase may be incorrect)" "")))
1456 (message "%s %s failed %s" crypto-type type warning)))))
1457
1458 (defun mh-mm-inline-message (handle)
1459 "Display message, HANDLE.
1460 The function decodes the message and displays it. It avoids decoding the same
1461 message multiple times."
1462 (let ((b (point))
1463 (clean-message-header mh-clean-message-header-flag)
1464 (invisible-headers mh-invisible-header-fields-compiled)
1465 (visible-headers nil))
1466 (save-excursion
1467 (save-restriction
1468 (narrow-to-region b b)
1469 (mm-insert-part handle)
1470 (mh-mime-display
1471 (or (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
1472 (setf (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
1473 (let ((handles (or (mm-dissect-buffer nil)
1474 (mm-uu-dissect))))
1475 (setf (mh-mime-handles (mh-buffer-data))
1476 (mm-merge-handles
1477 handles (mh-mime-handles (mh-buffer-data))))
1478 handles))))
1479
1480 (goto-char (point-min))
1481 (mh-show-xface)
1482 (cond (clean-message-header
1483 (mh-clean-msg-header (point-min)
1484 invisible-headers
1485 visible-headers)
1486 (goto-char (point-min)))
1487 (t
1488 (mh-start-of-uncleaned-message)))
1489 (mh-decode-message-header)
1490 (mh-show-addr)
1491 ;; The other highlighting types don't need anything special
1492 (when (eq mh-highlight-citation-style 'gnus)
1493 (mh-gnus-article-highlight-citation))
1494 (goto-char (point-min))
1495 (insert "\n------- Forwarded Message\n\n")
1496 (mh-display-smileys)
1497 (mh-display-emphasis)
1498 (mm-handle-set-undisplayer
1499 handle
1500 `(lambda ()
1501 (let (buffer-read-only)
1502 (if (fboundp 'remove-specifier)
1503 ;; This is only valid on XEmacs.
1504 (mapcar (lambda (prop)
1505 (remove-specifier
1506 (face-property 'default prop) (current-buffer)))
1507 '(background background-pixmap foreground)))
1508 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
1509
1510 (provide 'mh-mime)
1511
1512 ;; Local Variables:
1513 ;; indent-tabs-mode: nil
1514 ;; sentence-end-double-space: nil
1515 ;; End:
1516
1517 ;; arch-tag: 0dd36518-1b64-4a84-8f4e-59f422d3f002
1518 ;;; mh-mime.el ends here