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