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