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