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