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