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