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