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