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