New feature: toggle visibility of mime buttons.
[more-wl.git] / wl / wl-mime.el
blob52cfad2e84ae3fd1ba5cc791f36f8bee32737813
1 ;;; wl-mime.el --- SEMI implementations of MIME processing on Wanderlust.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
29 ;;; Code:
31 (require 'mime-view)
32 (require 'mime-edit)
33 (require 'mime-play)
34 (require 'mime-parse)
35 (eval-when-compile (require 'mmbuffer))
36 (require 'elmo)
37 (require 'elmo-mime)
38 (require 'wl-vars)
39 (require 'wl-util)
40 (eval-when-compile (require 'cl))
42 ;;; Draft
44 (defalias 'wl-draft-editor-mode 'mime-edit-mode)
46 (defalias 'wl-draft-decode-message-in-buffer
47 'mime-edit-decode-message-in-buffer)
49 (defun wl-draft-yank-current-message-entity ()
50 "Yank currently displayed message entity.
51 By setting following-method as yank-content.
53 If region is active, yank region contents instead. \(this feature is available
54 if and only if `transient-mark-mode' \(GNU Emacs\) or `zmacs-regions' \(XEmacs\)
55 has Non-nil value\)"
56 (let ((wl-draft-buffer (current-buffer))
57 (mime-view-following-method-alist
58 (list (cons 'wl-original-message-mode
59 (function wl-draft-yank-to-draft-buffer))))
60 (mime-preview-following-method-alist
61 (list (cons 'wl-original-message-mode
62 (function wl-draft-yank-to-draft-buffer))))
63 (message-buffer (wl-current-message-buffer)))
64 (if message-buffer
65 (save-excursion
66 (set-buffer message-buffer)
67 (save-restriction
68 (widen)
69 (cond
70 ((wl-region-exists-p)
71 (wl-mime-preview-follow-current-region))
72 ((not (wl-message-mime-analysis-p
73 (wl-message-buffer-display-type)))
74 (wl-mime-preview-follow-no-mime
75 (wl-message-buffer-display-type)))
77 (mime-preview-follow-current-entity)))))
78 (error "No message."))))
80 ;; modified mime-preview-follow-current-entity from mime-view.el
81 (defun wl-mime-preview-follow-no-mime (display-type)
82 "Write follow message to current message, without mime.
83 It calls following-method selected from variable
84 `mime-preview-following-method-alist'."
85 (interactive)
86 (let* ((mode (mime-preview-original-major-mode 'recursive))
87 (new-name (format "%s-no-mime" (buffer-name)))
88 new-buf min beg end
89 (entity (get-text-property (point-min) 'elmo-as-is-entity))
90 (the-buf (current-buffer))
91 fields)
92 (save-excursion
93 (goto-char (point-min))
94 (setq min (point-min)
95 beg (re-search-forward "^$" nil t)
96 end (point-max)))
97 (save-excursion
98 (set-buffer (setq new-buf (get-buffer-create new-name)))
99 (erase-buffer)
100 (insert-buffer-substring the-buf beg end)
101 (goto-char (point-min))
102 ;; Insert all headers.
103 (let ((elmo-mime-display-header-analysis
104 (wl-message-mime-analysis-p display-type 'header)))
105 (elmo-mime-insert-sorted-header entity))
106 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
107 (if (functionp f)
108 (funcall f new-buf)
109 (message
110 "Sorry, following method for %s is not implemented yet."
111 mode))))))
113 ;; modified mime-preview-follow-current-entity from mime-view.el
114 (defun wl-mime-preview-follow-current-region ()
115 "Write follow message to current region.
116 It calls following-method selected from variable
117 `mime-preview-following-method-alist'."
118 (interactive)
119 (let ((r-beg (region-beginning))
120 (r-end (region-end))
121 (entity (get-text-property (point-min)
122 'mime-view-entity)))
123 (let* ((mode (mime-preview-original-major-mode 'recursive))
124 (new-name
125 (format "%s-active-region" (buffer-name)))
126 new-buf
127 (the-buf (current-buffer))
128 fields)
129 (save-excursion
130 (set-buffer (setq new-buf (get-buffer-create new-name)))
131 (erase-buffer)
132 (insert ?\n)
133 (insert-buffer-substring the-buf r-beg r-end)
134 (goto-char (point-min))
135 (let ((current-entity
136 (if (and entity
137 (eq (mime-entity-media-type entity) 'message)
138 (eq (mime-entity-media-subtype entity) 'rfc822))
139 (car (mime-entity-children entity))
140 entity)))
141 (while (and current-entity
142 (if (and (eq (mime-entity-media-type
143 current-entity) 'message)
144 (eq (mime-entity-media-subtype
145 current-entity) 'rfc822))
147 (mime-insert-header current-entity fields)
149 (setq fields (std11-collect-field-names)
150 current-entity (mime-entity-parent current-entity))))
151 (let ((rest mime-view-following-required-fields-list)
152 field-name ret)
153 (while rest
154 (setq field-name (car rest))
155 (or (std11-field-body field-name)
156 (progn
157 (save-excursion
158 (set-buffer the-buf)
159 (let ((entity (when mime-mother-buffer
160 (set-buffer mime-mother-buffer)
161 (get-text-property (point)
162 'mime-view-entity))))
163 (while (and entity
164 (null (setq ret (mime-entity-fetch-field
165 entity field-name))))
166 (setq entity (mime-entity-parent entity)))))
167 (if ret
168 (insert (concat field-name ": " ret "\n")))))
169 (setq rest (cdr rest)))))
170 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
171 (if (functionp f)
172 (funcall f new-buf)
173 (message
174 "Sorry, following method for %s is not implemented yet."
175 mode))))))
177 (defalias 'wl-draft-enclose-digest-region 'mime-edit-enclose-digest-region)
179 (defun wl-draft-preview-attributes-list ()
180 (if (listp (car wl-draft-preview-attributes-list))
181 (elmo-uniq-list
182 (nconc (and (wl-message-mail-p)
183 (copy-sequence
184 (cdr (assq 'mail wl-draft-preview-attributes-list))))
185 (and (wl-message-news-p)
186 (copy-sequence
187 (cdr (assq 'news wl-draft-preview-attributes-list))))))
188 wl-draft-preview-attributes-list))
190 (defun wl-draft-show-attributes-buffer (attribute-values)
191 (let* ((cur-win (selected-window))
192 (size (min
193 (- (window-height cur-win)
194 window-min-height 1)
195 (- (window-height cur-win)
196 (max
197 window-min-height
198 (1+ wl-draft-preview-attributes-buffer-lines))))))
199 (split-window cur-win (if (> size 0) size window-min-height))
200 (select-window (next-window))
201 (let ((pop-up-windows nil))
202 (switch-to-buffer (get-buffer-create
203 wl-draft-preview-attributes-buffer-name)))
204 (with-current-buffer
205 (get-buffer wl-draft-preview-attributes-buffer-name)
206 (setq buffer-read-only t)
207 (let (buffer-read-only)
208 (erase-buffer)
209 (dolist (pair attribute-values)
210 (insert (capitalize (symbol-name (car pair))) ": "
211 (format "%s" (or (cdr pair) ""))
212 "\n"))
213 (goto-char (point-min))
214 (wl-highlight-headers)))
215 (select-window cur-win)))
217 (defun wl-draft-hide-attributes-buffer ()
218 (let (window buffer)
219 (when (setq window (get-buffer-window
220 wl-draft-preview-attributes-buffer-name))
221 (select-window window)
222 (delete-window))
223 (when (setq buffer (get-buffer wl-draft-preview-attributes-buffer-name))
224 (kill-buffer buffer))))
226 (defun wl-draft-attribute-recipients ()
227 (let (wl-draft-remove-group-list-contents)
228 (mapconcat
229 'identity
230 (nconc
231 (wl-draft-deduce-address-list
232 (current-buffer)
233 (point-min)
234 (save-excursion
235 (goto-char (point-min))
236 (re-search-forward
237 (concat
239 (regexp-quote mail-header-separator)
240 "$")
241 nil t)
242 (point)))
243 (wl-draft-parse-mailbox-list wl-draft-mime-bcc-field-name))
244 ", ")))
246 (defun wl-draft-attribute-envelope-from ()
247 (or wl-envelope-from
248 (wl-address-header-extract-address wl-from)))
250 (defun wl-draft-attribute-smtp-posting-server ()
251 (or wl-smtp-posting-server
252 (progn (require 'smtp) smtp-server)
253 "localhost"))
255 (defun wl-draft-attribute-smtp-posting-port ()
256 (or wl-smtp-posting-port
257 (progn (require 'smtp) smtp-service)))
259 (defun wl-draft-attribute-newsgroups ()
260 (std11-field-body "Newsgroups"))
262 (defun wl-draft-nntp-attribute (attribute &optional alternatives)
263 (let ((config (cdr (elmo-string-matched-assoc
264 (std11-field-body "newsgroups")
265 wl-nntp-posting-config-alist)))
266 entry)
267 (when (stringp config)
268 (setq config (list (cons 'server config))))
269 (if (setq entry (assq attribute config))
270 ;; maybe nil
271 (cdr entry)
272 (let (value)
273 (while alternatives
274 (if (setq value (symbol-value (car alternatives)))
275 (setq alternatives nil)
276 (setq alternatives (cdr alternatives))))
277 value))))
279 (defun wl-draft-attribute-nntp-posting-server ()
280 (wl-draft-nntp-attribute
281 'server
282 '(wl-nntp-posting-server elmo-nntp-default-server)))
284 (defun wl-draft-attribute-nntp-posting-port ()
285 (wl-draft-nntp-attribute
286 'port
287 '(wl-nntp-posting-port elmo-nntp-default-port)))
289 (defun wl-draft-attribute-value (attr)
290 (let ((name (symbol-name attr))
291 fsymbol symbol)
292 (cond ((and (setq fsymbol (intern-soft
293 (format "wl-draft-attribute-%s" name)))
294 (fboundp fsymbol))
295 (funcall fsymbol))
296 ((and (setq symbol (intern-soft (format "wl-%s" name)))
297 (boundp symbol))
298 (symbol-value symbol))
299 ((boundp attr)
300 (symbol-value attr)))))
302 (defun wl-mime-quit-preview ()
303 "Quitting method for mime-view."
304 (let* ((temp (and (boundp 'mime-edit-temp-message-buffer) ;; for SEMI <= 1.14.6
305 mime-edit-temp-message-buffer))
306 (window (selected-window))
307 buf)
308 (mime-preview-kill-buffer)
309 (set-buffer temp)
310 (setq buf mime-edit-buffer)
311 (kill-buffer temp)
312 (select-window window)
313 (switch-to-buffer buf)))
315 (defun wl-draft-preview-message ()
316 "Preview editing message."
317 (interactive)
318 (let* (attribute-values
319 (orig-buffer (current-buffer))
320 (current-point (point))
321 (config-exec-flag wl-draft-config-exec-flag)
322 (parent-folder wl-draft-parent-folder)
323 (mime-display-header-hook 'wl-highlight-headers)
324 (mime-header-encode-method-alist
325 (append
326 '((wl-draft-eword-encode-address-list
327 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
328 (if (boundp 'mime-header-encode-method-alist)
329 (symbol-value 'mime-header-encode-method-alist))))
330 mime-view-ignored-field-list ; all header.
331 (mime-edit-translate-buffer-hook
332 (cons
333 (lambda ()
334 (let ((wl-draft-config-exec-flag config-exec-flag)
335 (wl-draft-parent-folder parent-folder)
336 (copy-buffer (current-buffer)))
337 (wl-copy-local-variables
338 orig-buffer
339 copy-buffer
340 (with-current-buffer orig-buffer
341 (append wl-draft-config-variables
342 (wl-draft-clone-local-variables))))
343 (goto-char current-point)
344 (run-hooks 'wl-draft-send-hook)
345 (condition-case err
346 (setq attribute-values
347 (mapcar
348 (lambda (attr)
349 (cons attr (wl-draft-attribute-value attr)))
350 (if wl-draft-preview-attributes
351 (wl-draft-preview-attributes-list)
352 '(recipients))))
353 (error
354 (kill-buffer (current-buffer))
355 (signal (car err) (cdr err))))))
356 mime-edit-translate-buffer-hook)))
357 (mime-edit-preview-message)
358 (make-local-variable 'mime-preview-quitting-method-alist)
359 (setq mime-preview-quitting-method-alist
360 '((mime-temp-message-mode . wl-mime-quit-preview)))
361 (let ((buffer-read-only nil))
362 (when wl-highlight-body-too
363 (wl-highlight-body))
364 (run-hooks 'wl-draft-preview-message-hook))
365 (make-local-hook 'kill-buffer-hook)
366 (add-hook 'kill-buffer-hook #'wl-draft-hide-attributes-buffer nil t)
367 (if wl-draft-preview-attributes
368 (ignore-errors ; in case when the window is too small
369 (wl-draft-show-attributes-buffer attribute-values))
370 (message (concat "Recipients: "
371 (cdr (assq 'recipients attribute-values)))))))
373 (defalias 'wl-draft-caesar-region 'mule-caesar-region)
375 (defalias 'wl-draft-insert-message 'mime-edit-insert-message)
377 (defalias 'wl-draft-insert-mail 'mime-edit-insert-mail)
379 ;;; Message
381 (defun wl-message-decode-mode (outbuf inbuf)
382 (let ((mime-view-content-header-filter-hook 'wl-highlight-headers)
383 (mime-display-header-hook 'wl-highlight-headers))
384 (mime-view-mode nil nil nil inbuf outbuf)))
386 (defun wl-message-decode-with-all-header (outbuf inbuf)
387 (let ((mime-view-ignored-field-regexp "^:$")
388 (mime-view-content-header-filter-hook 'wl-highlight-headers)
389 (mime-display-header-hook 'wl-highlight-headers)
390 mime-view-ignored-field-list)
391 (mime-view-mode nil nil nil inbuf outbuf)))
393 (defun wl-message-delete-popup-windows ()
394 (dolist (buffer wl-message-popup-buffers)
395 (when (or (stringp buffer)
396 (and (symbolp buffer)
397 (boundp buffer)
398 (setq buffer (symbol-value buffer))))
399 (let ((window (get-buffer-window buffer)))
400 (when window
401 (delete-window window))))))
403 (defun wl-message-request-partial (folder number)
404 (elmo-set-work-buf
405 (elmo-message-fetch (wl-folder-get-elmo-folder folder)
406 number
407 (elmo-make-fetch-strategy 'entire)
408 'unread)
409 (mime-parse-buffer nil)))
411 (defalias 'wl-message-read 'mime-preview-scroll-up-entity)
412 (defalias 'wl-message-next-content 'mime-preview-move-to-next)
413 (defalias 'wl-message-prev-content 'mime-preview-move-to-previous)
414 (defalias 'wl-message-play-content 'mime-preview-play-current-entity)
415 (defalias 'wl-message-extract-content 'mime-preview-extract-current-entity)
416 (defalias 'wl-message-quit 'mime-preview-quit)
417 (defalias 'wl-message-button-dispatcher-internal
418 'mime-button-dispatcher)
420 (defsubst wl-mime-node-id-to-string (node-id)
421 (if (consp node-id)
422 (mapconcat (function (lambda (num) (format "%s" (1+ num))))
423 (reverse node-id)
424 ".")
425 "0"))
427 (defun wl-message-delete-current-part ()
428 "Delete a part under the cursor from the multipart message."
429 (interactive)
430 (save-restriction
431 (widen)
432 (let* ((entity (get-text-property (point) 'mime-view-entity))
433 (node-id (mime-entity-node-id entity))
434 (filename (mime-entity-safe-filename entity))
435 (header-start (mime-buffer-entity-header-start-internal entity))
436 (body-end (mime-buffer-entity-body-end-internal entity))
437 (folder (wl-folder-get-elmo-folder wl-message-buffer-cur-folder))
438 (number wl-message-buffer-cur-number)
439 (msgid (elmo-message-field folder number 'message-id))
440 (orig-buf wl-message-buffer-original-buffer))
441 (if (eq (luna-class-name entity) 'mime-elmo-imap-entity)
442 (error "Please fetch the entire message (by typing 'C-u .') and try again"))
443 (with-current-buffer orig-buf
444 (unless (string-equal
445 (buffer-string)
446 (elmo-message-fetch-string
447 folder number
448 (elmo-make-fetch-strategy 'entire)))
449 (error "Buffer content differs from actual message")))
450 (when (and (elmo-folder-writable-p folder)
451 (buffer-live-p orig-buf)
452 node-id
453 (yes-or-no-p
454 (format "Do you really want to delete part %s? "
455 (wl-mime-node-id-to-string node-id))))
456 (when (with-temp-buffer
457 (insert-buffer-substring orig-buf)
458 (delete-region header-start body-end)
459 (goto-char header-start)
460 (insert "Content-Type: text/plain; charset=US-ASCII\n")
461 (when filename
462 (insert
463 "Content-Disposition:"
464 (mime-encode-field-body
465 (concat ""
466 (and filename
467 (concat " inline; filename=" (std11-wrap-as-quoted-string filename))))
468 "Content-Disposition")
469 "\n"))
470 (insert "\n")
471 (insert "** This part has been removed by Wanderlust **\n\n")
472 (elmo-folder-append-buffer folder))
474 (elmo-folder-move-messages
475 folder (list number)
476 (wl-folder-get-elmo-folder wl-trash-folder))
477 (when (and (elmo-cache-get-path msgid)
478 (file-exists-p (elmo-cache-get-path msgid)))
479 (delete-file (elmo-cache-get-path msgid)))
481 (mime-preview-quit)
482 (wl-summary-delete-messages-on-buffer (list number))
483 (wl-summary-toggle-disp-msg 'off)
484 (setq wl-message-buffer nil)
485 (wl-summary-sync nil "update"))))))
487 ;; PGP
488 (eval-when-compile
489 (defmacro wl-define-dummy-functions (&rest symbols)
490 `(dolist (symbol (quote ,symbols))
491 (defalias symbol 'ignore))))
493 (eval-when-compile
494 ;; split eval-when-compile form for avoid error on `make compile-strict'
495 (require 'mime-pgp)
496 (condition-case nil
497 (require 'epa)
498 (error
499 (wl-define-dummy-functions epg-make-context
500 epg-decrypt-string
501 epg-verify-string
502 epg-context-set-progress-callback
503 epg-context-result-for
504 epg-verify-result-to-string
505 epa-display-info)))
506 (condition-case nil
507 (require 'pgg)
508 (error
509 (wl-define-dummy-functions pgg-decrypt-region
510 pgg-verify-region
511 pgg-display-output-buffer))))
513 (defun wl-epg-progress-callback (context what char current total reporter)
514 (let ((label (elmo-progress-counter-label reporter)))
515 (when label
516 (elmo-progress-notify label :set current :total total))))
518 (defun wl-mime-pgp-decrypt-region-with-epg (beg end &optional no-decode)
519 (require 'epg)
520 (let ((context (epg-make-context)))
521 (elmo-with-progress-display (epg-decript nil reporter)
522 "Decrypting"
523 (epg-context-set-progress-callback context
524 (cons #'wl-epg-progress-callback
525 reporter))
526 (insert (prog1
527 (decode-coding-string
528 (epg-decrypt-string
529 context
530 (buffer-substring beg end))
531 (if no-decode 'raw-text wl-cs-autoconv))
532 (delete-region beg end)))))
533 last-coding-system-used)
535 (defun wl-mime-pgp-verify-region-with-epg (beg end &optional coding-system)
536 (require 'epa)
537 (let ((context (epg-make-context)))
538 (elmo-with-progress-display (epg-verify nil reporter)
539 "Verifying"
540 (epg-context-set-progress-callback context
541 (cons #'wl-epg-progress-callback
542 reporter))
543 (epg-verify-string
544 context
545 (encode-coding-string
546 (buffer-substring beg end)
547 (if coding-system
548 (coding-system-change-eol-conversion coding-system 'dos)
549 'raw-text-dos))))
550 (when (epg-context-result-for context 'verify)
551 (epa-display-info (epg-verify-result-to-string
552 (epg-context-result-for context 'verify))))))
554 (defun wl-mime-pgp-decrypt-region-with-pgg (beg end &optional no-decode)
555 (require 'pgg)
556 (let ((buffer-file-coding-system wl-cs-autoconv)
557 status)
558 (setq status (pgg-decrypt-region beg end))
559 (if no-decode
560 (when status
561 (delete-region beg end)
562 (insert-buffer-substring pgg-output-buffer))
563 (pgg-display-output-buffer beg end status))
564 (unless status
565 (error "Decryption is failed"))
566 last-coding-system-used))
568 (defun wl-mime-pgp-verify-region-with-pgg (beg end &optional coding-system)
569 (require 'pgg)
570 (let ((message-buffer (current-buffer))
571 success)
572 (with-temp-buffer
573 (insert-buffer-substring message-buffer beg end)
574 (when coding-system
575 (encode-coding-region (point-min) (point-max) coding-system))
576 (setq success (pgg-verify-region (point-min) (point-max) nil 'fetch)))
577 (mime-show-echo-buffer)
578 (set-buffer mime-echo-buffer-name)
579 (set-window-start
580 (get-buffer-window mime-echo-buffer-name)
581 (point-max))
582 (insert-buffer-substring
583 (if success
584 pgg-output-buffer
585 pgg-errors-buffer))))
587 (defsubst wl-mime-pgp-decrypt-region (beg end &optional no-decode)
588 (case wl-use-pgp-module
589 (epg
590 (wl-mime-pgp-decrypt-region-with-epg beg end no-decode))
591 (pgg
592 (wl-mime-pgp-decrypt-region-with-pgg beg end no-decode))
594 (error "Don't support PGP decryption"))))
596 (defsubst wl-mime-pgp-verify-region (beg end &optional coding-system)
597 (case wl-use-pgp-module
598 (epg
599 (wl-mime-pgp-verify-region-with-epg beg end coding-system))
600 (pgg
601 (wl-mime-pgp-verify-region-with-pgg beg end coding-system))
603 (error "Don't support PGP decryption"))))
605 (defun wl-message-decrypt-pgp-nonmime ()
606 "Decrypt PGP encrypted region"
607 (interactive)
608 (save-excursion
609 (beginning-of-line)
610 (let ((region (wl-find-region "^-+BEGIN PGP MESSAGE-+$"
611 "^-+END PGP MESSAGE-+$"))
612 (inhibit-read-only t)
613 coding-system)
614 (unless region
615 (error "Cannot find PGP encrypted region"))
616 (save-restriction
617 (let ((props (text-properties-at (car region))))
618 (narrow-to-region (car region) (cdr region))
619 (when (setq coding-system
620 (wl-mime-pgp-decrypt-region (point-min) (point-max)))
621 (setq props (plist-put props
622 'wl-mime-decoded-coding-system
623 coding-system)))
624 (set-text-properties (point-min) (point-max) props))))))
626 (defun wl-message-verify-pgp-nonmime (&optional arg)
627 "Verify PGP signed region.
628 With ARG, ask coding system and encode the region with it before verifying."
629 (interactive "P")
630 (save-excursion
631 (beginning-of-line)
632 (let ((region (wl-find-region "^-+BEGIN PGP SIGNED MESSAGE-+$"
633 "^-+END PGP SIGNATURE-+$"))
634 coding-system)
635 (unless region
636 (error "Cannot find PGP signed region"))
637 (setq coding-system
638 (or (get-text-property (car region) 'wl-mime-decoded-coding-system)
639 (let* ((situation (mime-preview-find-boundary-info))
640 (entity (aref situation 2)))
641 (mime-charset-to-coding-system
642 (mime-content-type-parameter
643 (mime-entity-content-type entity)
644 "charset")))
645 buffer-file-coding-system))
646 (when arg
647 (setq coding-system (read-coding-system
648 (format "Coding system (%S): " coding-system)
649 coding-system)))
650 (wl-mime-pgp-verify-region (car region) (cdr region) coding-system))))
652 ;; XXX: encrypted multipart isn't represented as multipart
653 (defun wl-mime-preview-application/pgp (parent-entity entity situation)
654 (goto-char (point-max))
655 (let ((p (point))
656 raw-buf to-buf representation-type child-entity)
657 (goto-char p)
658 (save-restriction
659 (narrow-to-region p p)
660 (setq to-buf (current-buffer))
661 (with-temp-buffer
662 (setq raw-buf (current-buffer))
663 (mime-insert-entity entity)
664 (when (progn
665 (goto-char (point-min))
666 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
667 (wl-mime-pgp-decrypt-region (point-min) (point-max) 'no-decode)
668 (setq representation-type 'elmo-buffer))
669 (setq child-entity (mime-parse-message
670 (mm-expand-class-name representation-type)
672 parent-entity
673 (mime-entity-node-id-internal parent-entity)))
674 (mime-display-entity
675 child-entity
677 `((header . visible)
678 (body . visible)
679 (entity-button . invisible))
680 to-buf)))))
682 (defun wl-mime-preview-application/pgp-encrypted (entity situation)
683 (let* ((entity-node-id (mime-entity-node-id entity))
684 (mother (mime-entity-parent entity))
685 (knum (car entity-node-id))
686 (onum (if (> knum 0)
687 (1- knum)
688 (1+ knum)))
689 (orig-entity (nth onum (mime-entity-children mother))))
690 (wl-mime-preview-application/pgp entity orig-entity situation)))
692 ;;; Summary
693 (defun wl-summary-burst-subr (message-entity target number)
694 ;; returns new number.
695 (let (content-type entity)
696 (setq content-type (mime-entity-content-type message-entity))
697 (cond ((eq (cdr (assq 'type content-type)) 'multipart)
698 (dolist (entity (mime-entity-children message-entity))
699 (setq number (wl-summary-burst-subr
700 entity
701 target
702 number))))
703 ((and (eq (cdr (assq 'type content-type)) 'message)
704 (eq (cdr (assq 'subtype content-type)) 'rfc822))
705 (message "Bursting...%s" (setq number (+ 1 number)))
706 (setq entity
707 (car (mime-entity-children message-entity)))
708 (with-temp-buffer
709 (set-buffer-multibyte nil)
710 (insert (mime-entity-body message-entity))
711 (elmo-folder-append-buffer target))))
712 number))
714 (defun wl-summary-burst (&optional arg)
715 "De-capsulate embedded messages in MIME format.
716 With ARG, ask destination folder."
717 (interactive "P")
718 (let ((raw-buf (wl-summary-get-original-buffer))
719 (view-buf wl-message-buffer)
720 message-entity target)
721 (save-excursion
722 (when (and (null arg)
723 (elmo-folder-writable-p wl-summary-buffer-elmo-folder))
724 (setq target wl-summary-buffer-elmo-folder))
725 (while (null target)
726 (let ((name (wl-summary-read-folder wl-default-folder
727 "to extract to")))
728 (setq target (wl-folder-get-elmo-folder name))
729 (unless (elmo-folder-writable-p target)
730 (message "%s is not writable" name)
731 (setq target nil)
732 (sit-for 1))))
733 (wl-summary-set-message-buffer-or-redisplay)
734 (with-current-buffer view-buf
735 (setq message-entity
736 (get-text-property (point-min) 'mime-view-entity)))
737 (when message-entity
738 (message "Bursting...")
739 (with-current-buffer raw-buf
740 (wl-summary-burst-subr message-entity target 0))
741 (message "Bursting...done"))
742 (if (elmo-folder-plugged-p target)
743 (elmo-folder-check target)))
744 (when (and target
745 (string= wl-summary-buffer-folder-name
746 (elmo-folder-name-internal target)))
747 (save-excursion (wl-summary-sync-update)))))
749 ;; internal variable.
750 (defvar wl-mime-save-directory nil "Last saved directory.")
751 ;;; Yet another save method.
752 (defun wl-mime-save-content (entity situation)
753 (let ((filename (expand-file-name
754 (read-file-name "Save to file: "
755 (expand-file-name
756 (or (mime-entity-safe-filename entity)
757 ".")
758 (or wl-mime-save-directory
759 wl-temporary-file-directory))))))
760 (while (file-directory-p filename)
761 (setq filename (read-file-name "Please set filename (not directory): "
762 filename)))
763 (if (and (file-exists-p filename)
764 (not (yes-or-no-p (format "File %s exists. Save anyway? "
765 filename))))
766 (message "Not saved")
767 (setq wl-mime-save-directory (file-name-directory filename))
768 (mime-write-entity-content entity filename))))
770 (defun wl-summary-extract-attachments-1 (message-entity directory number)
771 ;; returns new number.
772 (let (children filename)
773 (cond
774 ((setq children (mime-entity-children message-entity))
775 (dolist (entity children)
776 (setq number
777 (wl-summary-extract-attachments-1 entity directory number))))
778 ((and (eq (mime-content-disposition-type
779 (mime-entity-content-disposition message-entity))
780 'attachment)
781 (setq filename (mime-entity-safe-filename message-entity)))
782 (let ((full (expand-file-name filename directory)))
783 (when (or (not (file-exists-p full))
784 (yes-or-no-p
785 (format "File %s exists. Save anyway? " filename)))
786 (message "Extracting...%s" (setq number (+ 1 number)))
787 (mime-write-entity-content message-entity full)))))
788 number))
790 (defun wl-summary-extract-attachments (directory)
791 "Extract attachment parts in MIME format into the DIRECTORY."
792 (interactive
793 (let* ((default (or wl-mime-save-directory
794 wl-temporary-file-directory))
795 (directory (read-directory-name "Extract to " default default t)))
796 (list (if (> (length directory) 0) directory default))))
797 (unless (and (file-writable-p directory)
798 (file-directory-p directory))
799 (error "%s is not writable" directory))
800 (save-excursion
801 (wl-summary-set-message-buffer-or-redisplay)
802 (let ((entity (get-text-property (point-min) 'mime-view-entity)))
803 (when entity
804 (message "Extracting...")
805 (wl-summary-extract-attachments-1 entity directory 0)
806 (message "Extracting...done")))))
808 ;;; Yet another combine method.
809 (defun wl-mime-combine-message/partial-pieces (entity situation)
810 "Internal method for wl to combine message/partial messages automatically."
811 (interactive)
812 (let* ((folder (with-current-buffer wl-message-buffer-cur-summary-buffer
813 wl-summary-buffer-elmo-folder))
814 (mime-display-header-hook 'wl-highlight-headers)
815 (id (or (cdr (assoc "id" situation)) ""))
816 (mother (current-buffer))
817 (summary-buf wl-message-buffer-cur-summary-buffer)
818 subject-id overviews
819 (root-dir (expand-file-name
820 (concat "m-prts-" (user-login-name))
821 temporary-file-directory))
822 full-file point)
823 (setq root-dir (concat root-dir "/" (replace-as-filename id)))
824 (setq full-file (concat root-dir "/FULL"))
825 (if (or (file-exists-p full-file)
826 (not (y-or-n-p "Merge partials? ")))
827 (with-current-buffer mother
828 (mime-store-message/partial-piece entity situation)
829 (setq wl-message-buffer-cur-summary-buffer summary-buf)
830 (make-local-variable 'mime-preview-over-to-next-method-alist)
831 (setq mime-preview-over-to-next-method-alist
832 (cons (cons 'mime-show-message-mode 'wl-message-exit)
833 mime-preview-over-to-next-method-alist))
834 (make-local-variable 'mime-preview-over-to-previous-method-alist)
835 (setq mime-preview-over-to-previous-method-alist
836 (cons (cons 'mime-show-message-mode 'wl-message-exit)
837 mime-preview-over-to-previous-method-alist)))
838 (setq subject-id
839 (eword-decode-string
840 (decode-mime-charset-string
841 (mime-entity-read-field entity 'Subject)
842 wl-summary-buffer-mime-charset)))
843 (if (string-match "[0-9\n]+" subject-id)
844 (setq subject-id (substring subject-id 0 (match-beginning 0))))
845 (catch 'tag
846 (elmo-folder-do-each-message-entity (entity folder)
847 (when (string-match
848 (regexp-quote subject-id)
849 (elmo-message-entity-field entity 'subject))
850 (let* ((message
851 ;; request message at the cursor in Subject buffer.
852 (wl-message-request-partial
853 (elmo-folder-name-internal folder)
854 (elmo-message-entity-number entity)))
855 (situation (mime-entity-situation message))
856 (the-id (or (cdr (assoc "id" situation)) "")))
857 (when (string= (downcase the-id)
858 (downcase id))
859 (with-current-buffer mother
860 (mime-store-message/partial-piece message situation))
861 (if (file-exists-p full-file)
862 (throw 'tag nil))))))
863 (message "Not all partials found.")))))
865 (defun wl-mime-display-text/plain (entity situation)
866 (let ((beg (point)))
867 (mime-display-text/plain entity situation)
868 (wl-highlight-message beg (point-max) t t)))
870 (defun wl-mime-display-header (entity situation)
871 (let ((elmo-message-ignored-field-list
872 (if wl-message-buffer-require-all-header
874 wl-message-ignored-field-list))
875 (elmo-message-visible-field-list wl-message-visible-field-list)
876 (elmo-message-sorted-field-list wl-message-sort-field-list))
877 (elmo-mime-insert-header entity situation)
878 (wl-highlight-headers)))
880 (defun wl-mime-decrypt-application/pgp-encrypted (entity situation)
881 (let ((summary-buffer wl-message-buffer-cur-summary-buffer)
882 (original-buffer wl-message-buffer-original-buffer))
883 (mime-decrypt-application/pgp-encrypted entity situation)
884 (setq wl-message-buffer-cur-summary-buffer summary-buffer)
885 (setq wl-message-buffer-original-buffer original-buffer)))
888 ;;; Setup methods.
889 (defun wl-mime-setup ()
890 (set-alist 'mime-preview-quitting-method-alist
891 'wl-original-message-mode 'wl-message-exit)
892 (set-alist 'mime-view-over-to-previous-method-alist
893 'wl-original-message-mode 'wl-message-exit)
894 (set-alist 'mime-view-over-to-next-method-alist
895 'wl-original-message-mode 'wl-message-exit)
896 (set-alist 'mime-preview-over-to-previous-method-alist
897 'wl-original-message-mode 'wl-message-exit)
898 (set-alist 'mime-preview-over-to-next-method-alist
899 'wl-original-message-mode 'wl-message-exit)
900 (add-hook 'wl-summary-toggle-disp-off-hook 'wl-message-delete-popup-windows)
901 (add-hook 'wl-summary-redisplay-hook 'wl-message-delete-popup-windows)
902 (add-hook 'wl-message-exit-hook 'wl-message-delete-popup-windows)
904 (ctree-set-calist-strictly
905 'mime-preview-condition
906 '((type . text) (subtype . plain)
907 (body . visible)
908 (body-presentation-method . wl-mime-display-text/plain)
909 (major-mode . wl-original-message-mode)))
911 (ctree-set-calist-strictly
912 'mime-preview-condition
913 '((type . application)(subtype . pgp-encrypted)
914 (encoding . t)
915 (body . invisible)
916 (body-presentation-method . wl-mime-preview-application/pgp-encrypted)
917 (major-mode . wl-original-message-mode)))
919 (ctree-set-calist-strictly
920 'mime-acting-condition
921 '((type . message) (subtype . partial)
922 (method . wl-mime-combine-message/partial-pieces)
923 (request-partial-message-method . wl-message-request-partial)
924 (major-mode . wl-original-message-mode)))
926 (ctree-set-calist-strictly
927 'mime-acting-condition
928 '((type . application) (subtype . pgp-encrypted)
929 (method . wl-mime-decrypt-application/pgp-encrypted)
930 (major-mode . wl-original-message-mode)))
932 (ctree-set-calist-strictly
933 'mime-acting-condition
934 '((mode . "extract")
935 (major-mode . wl-original-message-mode)
936 (method . wl-mime-save-content)))
937 (set-alist 'mime-preview-following-method-alist
938 'wl-original-message-mode
939 (function wl-message-follow-current-entity))
940 (set-alist 'mime-view-following-method-alist
941 'wl-original-message-mode
942 (function wl-message-follow-current-entity))
943 (set-alist 'mime-edit-message-inserter-alist
944 'wl-draft-mode (function wl-draft-insert-current-message))
945 (set-alist 'mime-edit-mail-inserter-alist
946 'wl-draft-mode (function wl-draft-insert-get-message))
947 (set-alist 'mime-edit-split-message-sender-alist
948 'wl-draft-mode
949 (cdr (assq 'mail-mode mime-edit-split-message-sender-alist)))
950 (set-alist 'mime-raw-representation-type-alist
951 'wl-original-message-mode 'binary)
952 ;; Sort and highlight header fields.
953 (or wl-message-ignored-field-list
954 (setq wl-message-ignored-field-list
955 mime-view-ignored-field-list))
956 (or wl-message-visible-field-list
957 (setq wl-message-visible-field-list
958 mime-view-visible-field-list))
959 (set-alist 'mime-header-presentation-method-alist
960 'wl-original-message-mode
961 'wl-mime-display-header)
962 ;; To avoid overriding wl-draft-mode-map.
963 (when (boundp 'mime-setup-signature-key-alist)
964 (unless (assq 'wl-draft-mode mime-setup-signature-key-alist)
965 (setq mime-setup-signature-key-alist
966 (cons '(wl-draft-mode . "\C-c\C-w")
967 mime-setup-signature-key-alist)))))
969 (require 'product)
970 (product-provide (provide 'wl-mime) (require 'wl-version))
972 ;;; wl-mime.el ends here