remove extraneous calls to shell-quote-argument
[bpt/emacs.git] / lisp / mh-e / mh-mime.el
CommitLineData
bdcfe844 1;;; mh-mime.el --- MH-E support for composing MIME messages
c26cf6c8 2
632146e3 3;; Copyright (C) 1993, 1995, 2005 Free Software Foundation, Inc.
a1b4049d
BW
4
5;; Author: Bill Wohler <wohler@newt.com>
6;; Maintainer: Bill Wohler <wohler@newt.com>
7;; Keywords: mail
8;; See: mh-e.el
c26cf6c8 9
60370d40 10;; This file is part of GNU Emacs.
c26cf6c8 11
9b7bc076 12;; GNU Emacs is free software; you can redistribute it and/or modify
c26cf6c8
RS
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
9b7bc076 17;; GNU Emacs is distributed in the hope that it will be useful,
c26cf6c8
RS
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
b578f267
EN
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.
c26cf6c8
RS
26
27;;; Commentary:
28
bdcfe844 29;; Internal support for MH-E package.
b578f267
EN
30;; Support for generating an mhn composition file.
31;; MIME is supported only by MH 6.8 or later.
c26cf6c8 32
847b8219
KH
33;;; Change Log:
34
c26cf6c8
RS
35;;; Code:
36
f0d73c14 37(eval-when-compile (require 'mh-acros))
a66894d8
BW
38(mh-require-cl)
39(require 'mh-comp)
bdcfe844 40(require 'gnus-util)
a66894d8 41(require 'mh-gnus)
bdcfe844
BW
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")
f0d73c14 49(autoload 'mml-unsecure-message "mml-sec")
bdcfe844
BW
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")
924df208 55(autoload 'rfc2047-decode-region "rfc2047")
bdcfe844 56
c3d9274a 57;;;###mh-autoload
bdcfe844
BW
58(defun mh-compose-insertion (&optional inline)
59 "Add a directive to insert a MIME part from a file, using mhn or gnus.
60If the variable `mh-compose-insertion' is set to 'mhn, then that will be used.
61If it is set to 'gnus, then that will be used instead.
62Optional 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
c3d9274a 70;;;###mh-autoload
bdcfe844
BW
71(defun mh-compose-forward (&optional description folder message)
72 "Add a MIME directive to forward a message, using mhn or gnus.
73If the variable `mh-compose-insertion' is set to 'mhn, then that will be used.
74If it is set to 'gnus, then that will be used instead.
75Optional argument DESCRIPTION is a description of the attachment.
76Optional argument FOLDER is the folder from which the forwarded message should
77come.
78Optional argument MESSAGE is the message to forward.
79If any of the optional arguments are absent, they are prompted for."
80 (interactive (list
c3d9274a
BW
81 (read-string "Forw Content-description: ")
82 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
83 (read-string (format "Messages%s: "
f0d73c14 84 (if (numberp mh-sent-from-msg)
bdcfe844 85 (format " [%d]" mh-sent-from-msg)
c3d9274a 86 "")))))
bdcfe844
BW
87 (if (equal mh-compose-insertion 'gnus)
88 (mh-mml-forward-message description folder message)
89 (mh-mhn-compose-forw description folder message)))
c26cf6c8 90
c26cf6c8
RS
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]
c3d9274a 94;; invokes mh-edit-mhn automatically before sending.)
c26cf6c8
RS
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
847b8219
KH
100(defvar mh-mhn-args nil
101 "Extra arguments to have \\[mh-edit-mhn] pass to the \"mhn\" command.
102The arguments are passed to mhn if \\[mh-edit-mhn] is given a
103prefix argument. Normally default arguments to mhn are specified in the
104MH profile.")
105
bdcfe844
BW
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)
847b8219 115
f0d73c14 116;;;###mh-autoload
a1b4049d
BW
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))
c3d9274a 121 (load "executable" t t) ; executable-find not autoloaded in emacs20
a1b4049d
BW
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
bdcfe844
BW
130(defvar mh-file-mime-type-substitutions
131 '(("application/msword" "\.xls" "application/ms-excel")
f0d73c14
BW
132 ("application/msword" "\.ppt" "application/ms-powerpoint")
133 ("text/plain" "\.vcf" "text/x-vcard"))
bdcfe844
BW
134 "Substitutions to make for Content-Type returned from file command.
135The first element is the Content-Type returned by the file command.
136The second element is a regexp matching the file name, usually the extension.
137The 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.
141Substitutions 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
f0d73c14 155;;;###mh-autoload
a1b4049d
BW
156(defun mh-file-mime-type (filename)
157 "Return MIME type of FILENAME from file command.
158Returns 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
bdcfe844 175 (mh-file-mime-type-substitute (match-string 0) filename)))
a1b4049d
BW
176 (kill-buffer tmp-buffer)))))))
177
a1b4049d 178;;; This is needed for Emacs20 which doesn't have mailcap-mime-types.
c26cf6c8 179(defvar mh-mime-content-types
a1b4049d
BW
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")
f0d73c14 197 ("text/richtext") ("text/x-vcard") ("text/xml")
a1b4049d
BW
198
199 ("video/mpeg") ("video/quicktime"))
88a34f43 200 "Valid MIME content types.
a1b4049d
BW
201See documentation for \\[mh-edit-mhn].")
202
f0d73c14
BW
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
88a34f43 227 "Valid MIME access-type values.")
f0d73c14 228
c3d9274a 229;;;###mh-autoload
3fda54a2 230(defun mh-mhn-compose-insertion (filename type description attributes)
42c21202 231 "Add a directive to insert a MIME message part from a file.
bdcfe844
BW
232This is the typical way to insert non-text parts in a message.
233
234Arguments are FILENAME, which tells where to find the file, TYPE, the MIME
235content type, DESCRIPTION, a line of text for the Content-Description field.
236ATTRIBUTES is a comma separated list of name=value pairs that is appended to
237the Content-Type field of the attachment.
238
a1b4049d
BW
239See also \\[mh-edit-mhn]."
240 (interactive (let ((filename (read-file-name "Insert contents of: ")))
c3d9274a
BW
241 (list
242 filename
a1b4049d 243 (or (mh-file-mime-type filename)
c3d9274a
BW
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 "\"")))))
3fda54a2 253 (mh-mhn-compose-type filename type description attributes ))
c26cf6c8 254
3fda54a2 255(defun mh-mhn-compose-type (filename type
c3d9274a 256 &optional description attributes comment)
bdcfe844
BW
257 "Insert a mhn directive to insert a file.
258
259The file specified by FILENAME is encoded as TYPE. An optional DESCRIPTION is
260used as the Content-Description field, optional set of ATTRIBUTES and an
261optional COMMENT can also be included."
c26cf6c8
RS
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))
3fda54a2 271 (insert "] " (expand-file-name filename))
c26cf6c8
RS
272 (insert "\n"))
273
274
c3d9274a 275;;;###mh-autoload
3fda54a2 276(defun mh-mhn-compose-anon-ftp (host filename type description)
42c21202 277 "Add a directive for a MIME anonymous ftp external body part.
bdcfe844
BW
278This directive tells MH to include a reference to a message/external-body part
279retrievable by anonymous FTP.
280
281Arguments are HOST and FILENAME, which tell where to find the file, TYPE, the
282MIME content type, and DESCRIPTION, a line of text for the Content-description
283header.
284
285See also \\[mh-edit-mhn]."
c26cf6c8 286 (interactive (list
c3d9274a
BW
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: ")))
3fda54a2 294 (mh-mhn-compose-external-type "anon-ftp" host filename
c3d9274a 295 type description))
c26cf6c8 296
c3d9274a 297;;;###mh-autoload
3fda54a2 298(defun mh-mhn-compose-external-compressed-tar (host filename description)
42c21202 299 "Add a directive to include a MIME reference to a compressed tar file.
bdcfe844
BW
300The file should be available via anonymous ftp. This directive tells MH to
301include a reference to a message/external-body part.
302
3fda54a2 303Arguments are HOST and FILENAME, which tell where to find the file, and
847b8219 304DESCRIPTION, a line of text for the Content-description header.
bdcfe844 305
c26cf6c8
RS
306See also \\[mh-edit-mhn]."
307 (interactive (list
c3d9274a
BW
308 (read-string "Remote host: ")
309 (read-string "Remote filename: ")
310 (read-string "Tar file Content-description: ")))
3fda54a2 311 (mh-mhn-compose-external-type "anon-ftp" host filename
c3d9274a
BW
312 "application/octet-stream"
313 description
314 "type=tar; conversions=x-compress"
315 "mode=image"))
c26cf6c8 316
f0d73c14 317;;;###mh-autoload
3fda54a2 318(defun mh-mhn-compose-external-type (access-type host filename type
c3d9274a
BW
319 &optional description
320 attributes extra-params
321 comment)
bdcfe844
BW
322 "Add a directive to include a MIME reference to a remote file.
323The file should be available via anonymous ftp. This directive tells MH to
324include a reference to a message/external-body part.
325
326Arguments are ACCESS-TYPE, HOST and FILENAME, which tell where to find the
327file and TYPE which is the MIME Content-Type. Optional arguments include
328DESCRIPTION, a line of text for the Content-description header, ATTRIBUTES,
329EXTRA-PARAMS, and COMMENT.
330
331See also \\[mh-edit-mhn]."
f0d73c14
BW
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: "))))
c26cf6c8
RS
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)
3fda54a2 356 (insert "; name=" (file-name-nondirectory filename))
f0d73c14
BW
357 (let ((directory (file-name-directory filename)))
358 (and directory
359 (insert "; directory=\"" directory "\"")))
c26cf6c8
RS
360 (and extra-params
361 (insert "; " extra-params))
362 (insert "\n"))
363
c3d9274a 364;;;###mh-autoload
847b8219 365(defun mh-mhn-compose-forw (&optional description folder messages)
42c21202 366 "Add a forw directive to this message, to forward a message with MIME.
c26cf6c8 367This directive tells MH to include the named messages in this one.
bdcfe844 368
c26cf6c8 369Arguments are DESCRIPTION, a line of text for the Content-description header,
42c21202 370and FOLDER and MESSAGES, which name the message(s) to be forwarded.
bdcfe844 371
c26cf6c8
RS
372See also \\[mh-edit-mhn]."
373 (interactive (list
c3d9274a
BW
374 (read-string "Forw Content-description: ")
375 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
376 (read-string (format "Messages%s: "
f0d73c14 377 (if (numberp mh-sent-from-msg)
c3d9274a
BW
378 (format " [%d]" mh-sent-from-msg)
379 "")))))
c26cf6c8
RS
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))
847b8219 389 (if (and messages
c3d9274a 390 (not (string= messages "")))
c26cf6c8 391 (let ((start (point)))
c3d9274a
BW
392 (insert " " messages)
393 (subst-char-in-region start (point) ?, ? ))
f0d73c14 394 (if (numberp mh-sent-from-msg)
c3d9274a 395 (insert " " (int-to-string mh-sent-from-msg))))
c26cf6c8
RS
396 (insert "\n"))
397
c3d9274a 398;;;###mh-autoload
847b8219
KH
399(defun mh-edit-mhn (&optional extra-args)
400 "Format the current draft for MIME, expanding any mhn directives.
a1b4049d
BW
401
402Process the current draft with the mhn program, which, using directives
403already inserted in the draft, fills in all the MIME components and header
404fields.
405
924df208
BW
406This step is performed automatically when sending the message, but this
407function may be called manually before sending the draft as well.
a1b4049d
BW
408
409The `\\[mh-revert-mhn-edit]' command undoes this command. The arguments in the
410list `mh-mhn-args' are passed to mhn if this function is passed an optional
411prefix argument EXTRA-ARGS.
412
413For assistance with creating mhn directives to insert various types of
414components in a message, see \\[mh-mhn-compose-insertion] (generic insertion
415from a file), \\[mh-mhn-compose-anon-ftp] (external reference to file via
416anonymous ftp), \\[mh-mhn-compose-external-compressed-tar] \ \(reference to
417compressed tar file via anonymous ftp), and \\[mh-mhn-compose-forw] (forward
924df208 418message).
a1b4049d 419
bdcfe844
BW
420The value of `mh-edit-mhn-hook' is a list of functions to be called, with no
421arguments, after performing the conversion.
422
a1b4049d 423The mhn program is part of MH version 6.8 or later."
847b8219 424 (interactive "*P")
f0d73c14 425 (mh-mhn-quote-unescaped-sharp)
c26cf6c8
RS
426 (save-buffer)
427 (message "mhn editing...")
a1b4049d 428 (cond
f0d73c14 429 ((mh-variant-p 'nmh)
a1b4049d
BW
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)))
c26cf6c8 435 (revert-buffer t t)
847b8219
KH
436 (message "mhn editing...done")
437 (run-hooks 'mh-edit-mhn-hook))
c26cf6c8 438
f0d73c14
BW
439(defun mh-mhn-quote-unescaped-sharp ()
440 "Quote `#' characters that haven't been quoted for `mhbuild'.
441If the `#' character is present in the first column, but it isn't part of a
442MHN directive then `mhbuild' gives an error. This function will quote all such
443characters."
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
c3d9274a 452;;;###mh-autoload
c26cf6c8 453(defun mh-revert-mhn-edit (noconfirm)
a1b4049d
BW
454 "Undo the effect of \\[mh-edit-mhn] by reverting to the backup file.
455Optional non-nil argument NOCONFIRM means don't ask for confirmation."
c26cf6c8
RS
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 '("," "#"))
c3d9274a 460 backup-file)
c26cf6c8 461 (while (and backup-strings
c3d9274a
BW
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")))))
c26cf6c8
RS
468 (setq backup-strings (cdr backup-strings)))
469 (or backup-strings
c3d9274a 470 (error "Backup file for %s no longer exists!" buffer-file-name))
c26cf6c8 471 (or noconfirm
c3d9274a
BW
472 (yes-or-no-p (format "Revert buffer from file %s? "
473 backup-file))
474 (error "Revert not confirmed"))
c26cf6c8
RS
475 (let ((buffer-read-only nil))
476 (erase-buffer)
477 (insert-file-contents backup-file))
478 (after-find-file nil)))
60370d40 479
924df208 480;;;###mh-autoload
f0d73c14
BW
481(defun mh-mhn-directive-present-p (&optional begin end)
482 "Check if the text between BEGIN and END might be a MHN directive.
483The optional argument BEGIN defaults to the beginning of the buffer, while END
484defaults to the the end of the buffer."
485 (unless begin (setq begin (point-min)))
486 (unless end (setq end (point-max)))
924df208
BW
487 (save-excursion
488 (block 'search-for-mhn-directive
f0d73c14
BW
489 (goto-char begin)
490 (while (re-search-forward "^#" end t)
924df208
BW
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;@]"))))
f0d73c14
BW
496 (when (and first-token
497 (string-match mh-media-type-regexp
498 first-token))
924df208
BW
499 (return-from 'search-for-mhn-directive t)))))))
500 nil)))
501
bdcfe844
BW
502\f
503
504;;; MIME composition functions
505
c3d9274a 506;;;###mh-autoload
bdcfe844 507(defun mh-mml-to-mime ()
924df208
BW
508 "Compose MIME message from mml directives.
509This step is performed automatically when sending the message, but this
510function may be called manually before sending the draft as well."
bdcfe844 511 (interactive)
a66894d8 512 (require 'message)
bdcfe844
BW
513 (when mh-gnus-pgp-support-flag ;; This is only needed for PGP
514 (message-options-set-recipient))
f0d73c14
BW
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))))))
bdcfe844 525
c3d9274a 526;;;###mh-autoload
bdcfe844
BW
527(defun mh-mml-forward-message (description folder message)
528 "Forward a message as attachment.
529The function will prompt the user for a DESCRIPTION, a FOLDER and MESSAGE
530number."
f0d73c14 531 (let ((msg (if (and (equal message "") (numberp mh-sent-from-msg))
bdcfe844
BW
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"
924df208 544 description)))
bdcfe844
BW
545 (t (error "The message number, %s is not a integer!" msg)))))
546
f0d73c14
BW
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
c3d9274a 560;;;###mh-autoload
bdcfe844
BW
561(defun mh-mml-attach-file (&optional disposition)
562 "Attach a file to the outgoing MIME message.
563The file is not inserted or encoded until you send the message with
564`\\[mh-send-letter]'.
565Message disposition is \"inline\" or \"attachment\" and is prompted for if
566DISPOSITION is nil.
567
568This is basically `mml-attach-file' from gnus, modified such that a prefix
569argument yields an `inline' disposition and Content-Type is determined
570automatically."
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
924df208 584 'disposition dispos 'description description)))
bdcfe844 585
eccf9613
BW
586(defvar mh-identity-pgg-default-user-id)
587
f0d73c14
BW
588(defun mh-secure-message (method mode &optional identity)
589 "Add directive to Encrypt/Sign an entire message.
590METHOD should be one of: \"pgpmime\", \"pgp\", \"smime\".
591MODE should be one of: \"sign\", \"encrypt\", \"signencrypt\", \"none\".
592IDENTITY is optionally the default-user-id to use."
bdcfe844
BW
593 (if (not mh-gnus-pgp-support-flag)
594 (error "Sorry. Your version of gnus does not support PGP/GPG")
f0d73c14
BW
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)))))))
bdcfe844 611
c3d9274a 612;;;###mh-autoload
f0d73c14
BW
613(defun mh-mml-unsecure-message (&optional ignore)
614 "Remove any secure message directives.
615The IGNORE argument is not used."
bdcfe844
BW
616 (interactive "P")
617 (if (not mh-gnus-pgp-support-flag)
618 (error "Sorry. Your version of gnus does not support PGP/GPG")
f0d73c14
BW
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))
924df208
BW
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)))
bdcfe844
BW
647
648\f
649
bdcfe844
BW
650;;; MIME cleanup
651
c3d9274a 652;;;###mh-autoload
bdcfe844
BW
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?
924df208 657 (mh-funcall-if-exists remove-images (point-min) (point-max))
bdcfe844
BW
658 (when mime-data
659 (mm-destroy-parts (mh-mime-handles mime-data))
660 (remhash (current-buffer) mh-globals-hash))))
661
c3d9274a 662;;;###mh-autoload
bdcfe844
BW
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'.
672This is only called in recent versions of Gnus. The MIME handles are stored
673in data structures corresponding to MH-E folder buffer FOLDER instead of in
674Gnus (as in the original). The MIME part, HANDLE is associated with the
675undisplayer FUNCTION."
676 (if (mm-keep-viewer-alive-p handle)
677 (let ((new-handle (copy-sequence handle)))
c3d9274a
BW
678 (mm-handle-set-undisplayer new-handle function)
679 (mm-handle-set-undisplayer handle nil)
bdcfe844
BW
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
c3d9274a 688(eval-when-compile (require 'font-lock))
bdcfe844 689
c3d9274a 690;;;###mh-autoload
bdcfe844
BW
691(defun mh-add-missing-mime-version-header ()
692 "Some mail programs don't put a MIME-Version header.
693I have seen this only in spam, so maybe we shouldn't fix this ;-)"
694 (save-excursion
695 (goto-char (point-min))
a66894d8
BW
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))
bdcfe844
BW
702 (insert "MIME-Version: 1.0\n")))))
703
a66894d8
BW
704(defun mh-small-show-buffer-p ()
705 "Check if show buffer is small.
706This 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
c3d9274a 716;;;###mh-autoload
bdcfe844
BW
717(defun mh-display-smileys ()
718 "Function to display smileys."
a66894d8
BW
719 (when (and mh-graphical-smileys-flag (mh-small-show-buffer-p))
720 (mh-funcall-if-exists smiley-region (point-min) (point-max))))
bdcfe844 721
c3d9274a 722;;;###mh-autoload
bdcfe844
BW
723(defun mh-display-emphasis ()
724 "Function to display graphical emphasis."
a66894d8 725 (when (and mh-graphical-emphasis-flag (mh-small-show-buffer-p))
c3d9274a 726 (flet ((article-goto-body ())) ; shadow this function to do nothing
bdcfe844
BW
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))
924df208
BW
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))
bdcfe844
BW
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)
924df208
BW
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))
bdcfe844
BW
770 map))
771
772(defvar mh-mime-save-parts-directory nil
773 "Default to use for `mh-mime-save-parts-default-directory'.
774Set from last use.")
775
c3d9274a 776;;;###mh-autoload
bdcfe844
BW
777(defun mh-mime-save-parts (arg)
778 "Store the MIME parts of the current message.
779If ARG, prompt for directory, else use that specified by the variable
780`mh-mime-save-parts-default-directory'. These directories may be superseded by
781mh_profile directives, since this function calls on mhstore or mhn to do the
782actual 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))
f0d73c14 790 (command (if (mh-variant-p 'nmh) "mhstore" "mhn"))
bdcfe844
BW
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))
f0d73c14 797 (read-file-name "Store in directory: " nil nil t nil))
bdcfe844
BW
798 ((and (or arg
799 (equal t mh-mime-save-parts-default-directory))
800 mh-mime-save-parts-directory)
801 (read-file-name (format
f0d73c14 802 "Store in directory: [%s] "
bdcfe844
BW
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))
f0d73c14 812 (message "No directory specified")
bdcfe844
BW
813 (if (equal nil mh-mime-save-parts-default-directory)
814 (setq mh-mime-save-parts-directory directory))
815 (save-excursion
924df208 816 (set-buffer (get-buffer-create mh-log-buffer))
bdcfe844
BW
817 (cd directory)
818 (setq mh-mime-save-parts-directory directory)
924df208
BW
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))))))))
bdcfe844
BW
827
828;; Avoid errors if gnus-sum isn't loaded yet...
829(defvar gnus-newsgroup-charset nil)
830(defvar gnus-newsgroup-name nil)
831
924df208
BW
832(defun mh-decode-message-body ()
833 "Decode message based on charset.
834If message has been encoded for transfer take that into account."
a66894d8
BW
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")))
924df208
BW
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
d103d8b3
BW
854;;;###mh-autoload
855(defun mh-toggle-mh-decode-mime-flag ()
856 "Toggle whether MH-E should decode MIME or not."
eccf9613 857 (interactive)
d103d8b3
BW
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
924df208
BW
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
c3d9274a 869;;;###mh-autoload
bdcfe844
BW
870(defun mh-mime-display (&optional pre-dissected-handles)
871 "Display (and possibly decode) MIME handles.
872Optional argument, PRE-DISSECTED-HANDLES is a list of MIME handles. If
873present they are displayed otherwise the buffer is parsed and then
874displayed."
875 (let ((handles ())
924df208
BW
876 (folder mh-show-folder-buffer)
877 (raw-message-data (buffer-string)))
c3d9274a
BW
878 (flet ((mm-handle-set-external-undisplayer
879 (handle function)
880 (mh-handle-set-external-undisplayer folder handle function)))
924df208
BW
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
f0d73c14
BW
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)))
bdcfe844 902
f0d73c14
BW
903 ;; Delete the body
904 (delete-region (point) (point-max))
bdcfe844 905
f0d73c14
BW
906 ;; Display the MIME handles
907 (mh-mime-display-part handles))
908 (t (mh-signature-highlight))))
924df208
BW
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))))))
bdcfe844
BW
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.
930If no part is preferred then all the parts are displayed."
a66894d8
BW
931 (let* ((preferred (mm-preferred-alternative handles))
932 (others (loop for x in handles unless (eq x preferred) collect x)))
bdcfe844 933 (cond ((and preferred (stringp (car preferred)))
a66894d8
BW
934 (mh-mime-display-part preferred)
935 (mh-mime-maybe-display-alternatives others))
bdcfe844
BW
936 (preferred
937 (save-restriction
938 (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
c3d9274a 939 (mh-mime-display-single preferred)
a66894d8 940 (mh-mime-maybe-display-alternatives others)
bdcfe844
BW
941 (goto-char (point-max))))
942 (t (mh-mime-display-mixed handles)))))
943
a66894d8
BW
944(defun mh-mime-maybe-display-alternatives (alternatives)
945 "Show buttons for ALTERNATIVES.
946If `mh-mime-display-alternatives-flag' is non-nil then display buttons for
947alternative 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
bdcfe844
BW
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.
962Notice that a hash table is used to display the same number when buttons need
963to be displayed multiple times (for instance when nested messages are
964opened)."
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
bdcfe844
BW
969(defun mh-small-image-p (handle)
970 "Decide whether HANDLE is a \"small\" image that can be displayed inline.
971This 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")
c3d9274a
BW
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
bdcfe844 980 (let* ((image (mm-get-image handle)))
a66894d8
BW
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)))))))))))
bdcfe844 995
c3d9274a
BW
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)))
924df208
BW
999 (and (or (featurep 'vcard) (fboundp 'vcard-pretty-print))
1000 (consp type)
c3d9274a
BW
1001 (equal (car type) "text/x-vcard")
1002 (save-excursion
1003 (save-restriction
1004 (widen)
1005 (goto-char (point-min))
f0d73c14 1006 (not (mh-signature-separator-p)))))))
c3d9274a 1007
bdcfe844
BW
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)))
c3d9274a
BW
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
bdcfe844
BW
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"))
c3d9274a 1031 nil) ; skip signatures as they are already handled...
bdcfe844
BW
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))
f0d73c14
BW
1036 (or (mm-display-part handle) (mm-display-part handle))
1037 (mh-signature-highlight handle))
bdcfe844
BW
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
f0d73c14
BW
1045(defun mh-signature-highlight (&optional handle)
1046 "Highlight message signature in HANDLE.
1047The optional argument, HANDLE is a MIME handle if the function is being used
1048to 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
924df208
BW
1067(mh-do-in-xemacs
1068 (defvar dots)
1069 (defvar type))
1070
bdcfe844
BW
1071(defun mh-insert-mime-button (handle index displayed)
1072 "Insert MIME button for HANDLE.
1073INDEX is the part number that will be DISPLAYED. It is also used by commands
1074like \"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)
c3d9274a
BW
1099 mh-callback mh-mm-display-part
1100 mh-part ,index
1101 mh-data ,handle))
bdcfe844
BW
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
f0d73c14
BW
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))))
bdcfe844
BW
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)))
a66894d8 1139 (when region
924df208
BW
1140 (mh-funcall-if-exists
1141 remove-images (car region) (cdr region)))
bdcfe844
BW
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)
f0d73c14
BW
1166 (mh-display-emphasis)
1167 (mh-signature-highlight handle))
bdcfe844
BW
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
c3d9274a 1182;;;###mh-autoload
bdcfe844
BW
1183(defun mh-press-button ()
1184 "Press MIME button.
1185If the MIME part is visible then it is removed. Otherwise the part is
1186displayed."
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))
c3d9274a
BW
1193 (flet ((mm-handle-set-external-undisplayer
1194 (handle function)
1195 (mh-handle-set-external-undisplayer folder handle function)))
bdcfe844
BW
1196 (when (and function (eolp))
1197 (backward-char))
1198 (unwind-protect (and function (funcall function data))
1199 (set-buffer-modified-p nil)))))
1200
c3d9274a 1201;;;###mh-autoload
bdcfe844
BW
1202(defun mh-push-button (event)
1203 "Click MIME button for EVENT.
1204If the MIME part is visible then it is removed. Otherwise the part is
1205displayed. This function is called when the mouse is used to click the MIME
1206button."
1207 (interactive "e")
a66894d8
BW
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))))))
bdcfe844 1216
c3d9274a 1217;;;###mh-autoload
bdcfe844
BW
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
a66894d8
BW
1223 (let ((mm-default-directory
1224 (file-name-as-directory (or mh-mime-save-parts-directory
1225 default-directory))))
c3d9274a
BW
1226 (mh-mm-save-part data)
1227 (setq mh-mime-save-parts-directory mm-default-directory)))))
bdcfe844 1228
c3d9274a 1229;;;###mh-autoload
bdcfe844
BW
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
f0d73c14
BW
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
bdcfe844
BW
1281(defun mh-widget-press-button (widget el)
1282 "Callback for widget, WIDGET.
1283Parameter 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."
bdcfe844
BW
1289 (save-restriction
1290 (narrow-to-region (point) (point))
f0d73c14 1291 (insert "\n")
bdcfe844
BW
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
c3d9274a 1296 mh-mime-security-button-end-line-format))
bdcfe844
BW
1297 (mh-insert-mime-security-button handle))
1298 (mm-set-handle-multipart-parameter
f0d73c14 1299 handle 'mh-region (cons (point-min-marker) (point-max-marker)))))
bdcfe844
BW
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
c3d9274a 1309 (not (get-text-property (point) 'mh-button-pressed)))
bdcfe844 1310 (mh-mime-security-button-line-format
c3d9274a 1311 (get-text-property (point) 'mh-line-format)))
bdcfe844
BW
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."
f0d73c14
BW
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))))
bdcfe844
BW
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)
c3d9274a
BW
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))
bdcfe844
BW
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.")
f0d73c14
BW
1386 (dolist (ov (mh-funcall-if-exists overlays-in begin end))
1387 (mh-funcall-if-exists overlay-put ov 'evaporate t))
bdcfe844
BW
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.
1397The function decodes the message and displays it. It avoids decoding the same
1398message multiple times."
1399 (let ((b (point))
bdcfe844 1400 (clean-message-header mh-clean-message-header-flag)
f0d73c14
BW
1401 (invisible-headers mh-invisible-header-fields-compiled)
1402 (visible-headers nil))
bdcfe844
BW
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))
924df208 1418 (mh-show-xface)
bdcfe844
BW
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)))
924df208 1426 (mh-decode-message-header)
bdcfe844
BW
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:
c3d9274a 1450;;; indent-tabs-mode: nil
bdcfe844
BW
1451;;; sentence-end-double-space: nil
1452;;; End:
1453
ab5796a9 1454;;; arch-tag: 0dd36518-1b64-4a84-8f4e-59f422d3f002
60370d40 1455;;; mh-mime.el ends here