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)
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.
38 (defalias-maybe 'pgg-decrypt-region 'ignore)
39 (defalias-maybe 'pgg-display-output-buffer 'ignore)
40 (defalias-maybe 'pgg-verify-region 'ignore))
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\)
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 (if (get-buffer (wl-current-message-buffer))
65 (set-buffer (wl-current-message-buffer))
68 (if (wl-region-exists-p)
69 (wl-mime-preview-follow-current-region)
70 (mime-preview-follow-current-entity)))))))
72 ;; modified mime-preview-follow-current-entity from mime-view.el
73 (defun wl-mime-preview-follow-current-region ()
74 "Write follow message to current region.
75 It calls following-method selected from variable
76 `mime-preview-following-method-alist'."
78 (let ((r-beg (region-beginning))
80 (entity (get-text-property (point-min)
82 (let* ((mode (mime-preview-original-major-mode 'recursive))
84 (format "%s-active-region" (buffer-name)))
86 (the-buf (current-buffer))
89 (set-buffer (setq new-buf (get-buffer-create new-name)))
92 (insert-buffer-substring the-buf r-beg r-end)
93 (goto-char (point-min))
95 (if (and (eq (mime-entity-media-type entity) 'message)
96 (eq (mime-entity-media-subtype entity) 'rfc822))
97 (car (mime-entity-children entity))
99 (while (and current-entity
100 (if (and (eq (mime-entity-media-type
101 current-entity) 'message)
102 (eq (mime-entity-media-subtype
103 current-entity) 'rfc822))
105 (mime-insert-header current-entity fields)
107 (setq fields (std11-collect-field-names)
108 current-entity (mime-entity-parent current-entity))
110 (let ((rest mime-view-following-required-fields-list)
113 (setq field-name (car rest))
114 (or (std11-field-body field-name)
118 (let ((entity (when mime-mother-buffer
119 (set-buffer mime-mother-buffer)
120 (get-text-property (point)
121 'mime-view-entity))))
123 (null (setq ret (mime-entity-fetch-field
124 entity field-name))))
125 (setq entity (mime-entity-parent entity)))))
127 (insert (concat field-name ": " ret "\n"))
129 (setq rest (cdr rest))
132 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
137 "Sorry, following method for %s is not implemented yet."
142 (defalias 'wl-draft-enclose-digest-region 'mime-edit-enclose-digest-region)
144 (defun wl-draft-preview-message ()
145 "Preview editing message."
147 (let* (recipients-message
148 (current-point (point))
149 (config-exec-flag wl-draft-config-exec-flag)
150 (parent-folder wl-draft-parent-folder)
151 (mime-display-header-hook 'wl-highlight-headers)
152 (mime-header-encode-method-alist
154 '((wl-draft-eword-encode-address-list
155 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
156 (if (boundp 'mime-header-encode-method-alist)
157 (symbol-value 'mime-header-encode-method-alist))))
158 mime-view-ignored-field-list ; all header.
159 (mime-edit-translate-buffer-hook
164 (let ((wl-draft-config-exec-flag config-exec-flag)
165 (wl-draft-parent-folder parent-folder))
166 (goto-char current-point)
167 (run-hooks 'wl-draft-send-hook)
168 (setq recipients-message
170 (concat "Recipients: "
173 (wl-draft-deduce-address-list
177 (goto-char (point-min))
181 (regexp-quote mail-header-separator)
187 (kill-buffer (current-buffer))
188 (signal (car err) (cdr err)))))))))
189 mime-edit-translate-buffer-hook)))
190 (mime-edit-preview-message)
191 (let ((buffer-read-only nil))
192 (when wl-highlight-body-too
194 (run-hooks 'wl-draft-preview-message-hook))
195 (message recipients-message)))
197 (defalias 'wl-draft-caesar-region 'mule-caesar-region)
199 (defalias 'wl-draft-insert-message 'mime-edit-insert-message)
201 (defalias 'wl-draft-insert-mail 'mime-edit-insert-mail)
205 (defun wl-message-decode-mode (outbuf inbuf)
206 (let ((mime-view-content-header-filter-hook 'wl-highlight-headers)
207 (mime-display-header-hook 'wl-highlight-headers))
208 (mime-view-mode nil nil nil inbuf outbuf)))
210 (defun wl-message-decode-with-all-header (outbuf inbuf)
211 (let ((mime-view-ignored-field-regexp "^:$")
212 (mime-view-content-header-filter-hook 'wl-highlight-headers)
213 (mime-display-header-hook 'wl-highlight-headers)
214 mime-view-ignored-field-list)
215 (mime-view-mode nil nil nil inbuf outbuf)))
217 (defun wl-message-delete-mime-out-buf ()
218 (let (mime-out-buf mime-out-win)
219 (if (setq mime-out-buf (get-buffer mime-echo-buffer-name))
220 (if (setq mime-out-win (get-buffer-window mime-out-buf))
221 (delete-window mime-out-win)))))
223 (defun wl-message-request-partial (folder number)
225 (elmo-message-fetch (wl-folder-get-elmo-folder folder)
227 (elmo-make-fetch-strategy 'entire)
231 (mime-parse-buffer nil)))
233 (defalias 'wl-message-read 'mime-preview-scroll-up-entity)
234 (defalias 'wl-message-next-content 'mime-preview-move-to-next)
235 (defalias 'wl-message-prev-content 'mime-preview-move-to-previous)
236 (defalias 'wl-message-play-content 'mime-preview-play-current-entity)
237 (defalias 'wl-message-extract-content 'mime-preview-extract-current-entity)
238 (defalias 'wl-message-quit 'mime-preview-quit)
239 (defalias 'wl-message-button-dispatcher-internal
240 'mime-button-dispatcher)
242 (defsubst wl-mime-node-id-to-string (node-id)
244 (mapconcat (function (lambda (num) (format "%s" (1+ num))))
249 (defun wl-message-delete-current-part ()
250 "Delete a part under the cursor from the multipart message."
254 (let* ((entity (get-text-property (point) 'mime-view-entity))
255 (node-id (mime-entity-node-id entity))
256 (header-start (mime-buffer-entity-header-start-internal entity))
257 (body-end (mime-buffer-entity-body-end-internal entity))
258 (folder (wl-folder-get-elmo-folder wl-message-buffer-cur-folder))
259 (number wl-message-buffer-cur-number)
260 (msgid (elmo-message-field folder number 'message-id))
261 (orig-buf wl-message-buffer-original-buffer))
262 (if (eq (luna-class-name entity) 'mime-elmo-imap-entity)
263 (error "Please fetch the entire message (by typing 'C-u .') and try again"))
264 (with-current-buffer orig-buf
265 (unless (string-equal
267 (elmo-message-fetch folder number
268 (elmo-make-fetch-strategy 'entire)))
269 (error "Buffer content differs from actual message")))
270 (when (and (elmo-folder-writable-p folder)
271 (buffer-live-p orig-buf)
274 (format "Do you really want to delete part %s? "
275 (wl-mime-node-id-to-string node-id))))
276 (when (with-temp-buffer
277 (insert-buffer orig-buf)
278 (delete-region header-start body-end)
279 (goto-char header-start)
280 (insert "Content-Type: text/plain; charset=US-ASCII\n\n")
281 (insert "** This part has been removed by Wanderlust **\n\n")
282 (elmo-folder-append-buffer folder t))
284 (elmo-folder-move-messages
286 (wl-folder-get-elmo-folder wl-trash-folder))
287 (when (and (elmo-cache-get-path msgid)
288 (file-exists-p (elmo-cache-get-path msgid)))
289 (delete-file (elmo-cache-get-path msgid)))
292 (wl-summary-delete-messages-on-buffer (list number))
293 (wl-summary-toggle-disp-msg 'off)
294 (setq wl-message-buffer nil)
295 (wl-summary-sync nil "update"))))))
297 (defun wl-message-decrypt-pgp-nonmime ()
298 "Decrypt PGP encrypted region"
303 (if (or (re-search-forward "^-+END PGP MESSAGE-+$" nil t)
304 (re-search-backward "^-+END PGP MESSAGE-+$" nil t))
305 (let (beg end status)
306 (setq end (match-end 0))
307 (if (setq beg (re-search-backward "^-+BEGIN PGP MESSAGE-+$" nil t))
308 (let ((inhibit-read-only t)
309 (buffer-file-coding-system wl-cs-autoconv))
310 (setq status (pgg-decrypt-region beg end))
311 (pgg-display-output-buffer beg end status))
312 (message "Cannot find pgp encrypted region")))
313 (message "Cannot find pgp encrypted region"))))
315 (defun wl-message-verify-pgp-nonmime (&optional arg)
316 "Verify PGP signed region.
317 With ARG, ask coding system and encode the region with it before verifying."
322 (let ((msg-buf (current-buffer))
323 beg end status m-beg)
324 (if (and (when (or (re-search-forward "^-+END PGP SIGNATURE-+$" nil t)
325 (re-search-backward "^-+END PGP SIGNATURE-+$" nil t))
326 (setq end (match-end 0)))
327 (setq beg (re-search-backward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)))
331 (insert-buffer-substring msg-buf beg end)
332 (set-mark (point-min))
333 (goto-char (point-max))
334 (call-interactively 'encode-coding-region)
335 (setq status (pgg-verify-region (point-min) (point-max) nil 'fetch)))
336 (let* ((situation (mime-preview-find-boundary-info))
337 (p-end (aref situation 1))
338 (entity (aref situation 2))
341 (while (< beg (point))
342 (if (re-search-backward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
343 (setq count (+ count 1))
346 (insert (mime-entity-body entity))
347 (goto-char (point-max))
349 (if (re-search-backward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
350 (setq count (- count 1))
352 (let ((r-beg (point))
353 (r-end (re-search-forward "^-+END PGP SIGNATURE-+$" nil t)))
355 (setq status (pgg-verify-region r-beg r-end nil 'fetch))
357 (mime-show-echo-buffer)
358 (set-buffer mime-echo-buffer-name)
360 (get-buffer-window mime-echo-buffer-name)
363 (insert-buffer-substring
364 (if status pgg-output-buffer pgg-errors-buffer))
365 (encode-coding-region m-beg (point) buffer-file-coding-system)
366 (decode-coding-region m-beg (point) wl-cs-autoconv))
367 (message "Cannot find pgp signed region")))))
369 ;; XXX: encrypted multipart isn't represented as multipart
370 (defun wl-mime-preview-application/pgp (parent-entity entity situation)
372 (goto-char (point-max))
374 raw-buf to-buf representation-type child-entity)
377 (narrow-to-region p p)
378 (setq to-buf (current-buffer))
380 (setq raw-buf (current-buffer))
381 (mime-insert-entity entity)
383 (goto-char (point-min))
384 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
385 (pgg-decrypt-region (point-min)(point-max))
386 (delete-region (point-min) (point-max))
387 (insert-buffer pgg-output-buffer)
388 (setq representation-type 'elmo-buffer))
389 (setq child-entity (mime-parse-message
390 (mm-expand-class-name representation-type)
393 (mime-entity-node-id-internal parent-entity)))
399 (entity-button . invisible))
402 (defun wl-mime-preview-application/pgp-encrypted (entity situation)
403 (let* ((entity-node-id (mime-entity-node-id entity))
404 (mother (mime-entity-parent entity))
405 (knum (car entity-node-id))
409 (orig-entity (nth onum (mime-entity-children mother))))
410 (wl-mime-preview-application/pgp entity orig-entity situation)))
413 (defun wl-summary-burst-subr (message-entity target number)
414 ;; returns new number.
415 (let (content-type entity)
416 (setq content-type (mime-entity-content-type message-entity))
417 (cond ((eq (cdr (assq 'type content-type)) 'multipart)
418 (dolist (entity (mime-entity-children message-entity))
419 (setq number (wl-summary-burst-subr
423 ((and (eq (cdr (assq 'type content-type)) 'message)
424 (eq (cdr (assq 'subtype content-type)) 'rfc822))
425 (message "Bursting...%s" (setq number (+ 1 number)))
427 (car (mime-entity-children message-entity)))
429 (insert (mime-entity-body message-entity))
430 (elmo-folder-append-buffer
432 (mime-entity-fetch-field entity "Message-ID")))))
435 (defun wl-summary-burst (&optional arg)
436 "De-capsulate embedded messages in MIME format.
437 With ARG, ask destination folder."
439 (let ((raw-buf (wl-summary-get-original-buffer))
440 (view-buf wl-message-buffer)
441 children message-entity content-type target-name target)
443 (setq target wl-summary-buffer-elmo-folder)
444 (when (or arg (not (elmo-folder-writable-p target)))
445 (setq target-name (wl-summary-read-folder wl-default-folder "to extract to"))
446 (setq target (wl-folder-get-elmo-folder target-name)))
447 (wl-summary-set-message-buffer-or-redisplay)
448 (with-current-buffer view-buf
449 (setq message-entity (get-text-property (point-min) 'mime-view-entity)))
451 (message "Bursting...")
452 (with-current-buffer raw-buf
453 (wl-summary-burst-subr message-entity target 0))
454 (message "Bursting...done"))
455 (if (elmo-folder-plugged-p target)
456 (elmo-folder-check target)))
457 (when (or (not target-name)
458 (string= wl-summary-buffer-folder-name target-name))
459 (save-excursion (wl-summary-sync-update)))))
461 ;; internal variable.
462 (defvar wl-mime-save-directory nil "Last saved directory.")
463 ;;; Yet another save method.
464 (defun wl-mime-save-content (entity situation)
465 (let ((filename (read-file-name "Save to file: "
467 (or (mime-entity-safe-filename entity)
469 (or wl-mime-save-directory
470 wl-temporary-file-directory)))))
471 (while (file-directory-p filename)
472 (setq filename (read-file-name "Please set filename (not directory): "
474 (if (and (file-exists-p filename)
475 (not (yes-or-no-p (format "File %s exists. Save anyway? "
477 (message "Not saved")
478 (setq wl-mime-save-directory (file-name-directory filename))
479 (mime-write-entity-content entity filename))))
481 ;;; Yet another combine method.
482 (defun wl-mime-combine-message/partial-pieces (entity situation)
483 "Internal method for wl to combine message/partial messages automatically."
485 (let* ((msgdb (save-excursion
486 (set-buffer wl-message-buffer-cur-summary-buffer)
487 (wl-summary-buffer-msgdb)))
488 (mime-display-header-hook 'wl-highlight-headers)
489 (folder wl-message-buffer-cur-folder)
490 (id (or (cdr (assoc "id" situation)) ""))
491 (mother (current-buffer))
492 (summary-buf wl-message-buffer-cur-summary-buffer)
494 (root-dir (expand-file-name
495 (concat "m-prts-" (user-login-name))
496 temporary-file-directory))
498 (setq root-dir (concat root-dir "/" (replace-as-filename id)))
499 (setq full-file (concat root-dir "/FULL"))
500 (if (or (file-exists-p full-file)
501 (not (y-or-n-p "Merge partials? ")))
502 (with-current-buffer mother
503 (mime-store-message/partial-piece entity situation)
504 (setq wl-message-buffer-cur-summary-buffer summary-buf)
505 (make-variable-buffer-local 'mime-preview-over-to-next-method-alist)
506 (setq mime-preview-over-to-next-method-alist
507 (cons (cons 'mime-show-message-mode 'wl-message-exit)
508 mime-preview-over-to-next-method-alist))
509 (make-variable-buffer-local 'mime-preview-over-to-previous-method-alist)
510 (setq mime-preview-over-to-previous-method-alist
511 (cons (cons 'mime-show-message-mode 'wl-message-exit)
512 mime-preview-over-to-previous-method-alist)))
515 (decode-mime-charset-string
516 (mime-entity-read-field entity 'Subject)
517 wl-summary-buffer-mime-charset)))
518 (if (string-match "[0-9\n]+" subject-id)
519 (setq subject-id (substring subject-id 0 (match-beginning 0))))
520 (setq overviews (elmo-msgdb-get-overview msgdb))
524 (regexp-quote subject-id)
525 (elmo-msgdb-overview-entity-get-subject (car overviews)))
527 ;; request message at the cursor in Subject buffer.
528 (wl-message-request-partial
530 (elmo-msgdb-overview-entity-get-number
532 (situation (mime-entity-situation message))
533 (the-id (or (cdr (assoc "id" situation)) "")))
534 (when (string= (downcase the-id)
536 (with-current-buffer mother
537 (mime-store-message/partial-piece message situation))
538 (if (file-exists-p full-file)
540 (setq overviews (cdr overviews)))
541 (message "Not all partials found.")))))
543 (defun wl-mime-display-text/plain (entity situation)
545 (mime-display-text/plain entity situation)
546 (wl-highlight-message beg (point-max) t t)))
548 (defun wl-mime-display-header (entity situation)
549 (let ((elmo-message-ignored-field-list
550 (if wl-message-buffer-all-header-flag
552 wl-message-ignored-field-list))
553 (elmo-message-visible-field-list wl-message-visible-field-list)
554 (elmo-message-sorted-field-list wl-message-sort-field-list))
555 (elmo-mime-insert-header entity situation)
556 (wl-highlight-headers)))
558 (defun wl-mime-decrypt-application/pgp-encrypted (entity situation)
559 (let ((summary-buffer wl-message-buffer-cur-summary-buffer)
560 (original-buffer wl-message-buffer-original-buffer))
561 (mime-decrypt-application/pgp-encrypted entity situation)
562 (setq wl-message-buffer-cur-summary-buffer summary-buffer)
563 (setq wl-message-buffer-original-buffer original-buffer)))
567 (defun wl-mime-setup ()
568 (set-alist 'mime-preview-quitting-method-alist
569 'wl-original-message-mode 'wl-message-exit)
570 (set-alist 'mime-view-over-to-previous-method-alist
571 'wl-original-message-mode 'wl-message-exit)
572 (set-alist 'mime-view-over-to-next-method-alist
573 'wl-original-message-mode 'wl-message-exit)
574 (set-alist 'mime-preview-over-to-previous-method-alist
575 'wl-original-message-mode 'wl-message-exit)
576 (set-alist 'mime-preview-over-to-next-method-alist
577 'wl-original-message-mode 'wl-message-exit)
578 (add-hook 'wl-summary-redisplay-hook 'wl-message-delete-mime-out-buf)
579 (add-hook 'wl-message-exit-hook 'wl-message-delete-mime-out-buf)
581 (ctree-set-calist-strictly
582 'mime-preview-condition
583 '((type . text) (subtype . plain)
585 (body-presentation-method . wl-mime-display-text/plain)
586 (major-mode . wl-original-message-mode)))
588 (ctree-set-calist-strictly
589 'mime-preview-condition
590 '((type . application)(subtype . pgp-encrypted)
593 (body-presentation-method . wl-mime-preview-application/pgp-encrypted)
594 (major-mode . wl-original-message-mode)))
596 (ctree-set-calist-strictly
597 'mime-acting-condition
598 '((type . message) (subtype . partial)
599 (method . wl-mime-combine-message/partial-pieces)
600 (request-partial-message-method . wl-message-request-partial)
601 (major-mode . wl-original-message-mode)))
603 (ctree-set-calist-strictly
604 'mime-acting-condition
605 '((type . application) (subtype . pgp-encrypted)
606 (method . wl-mime-decrypt-application/pgp-encrypted)
607 (major-mode . wl-original-message-mode)))
609 (ctree-set-calist-strictly
610 'mime-acting-condition
612 (major-mode . wl-original-message-mode)
613 (method . wl-mime-save-content)))
614 (set-alist 'mime-preview-following-method-alist
615 'wl-original-message-mode
616 (function wl-message-follow-current-entity))
617 (set-alist 'mime-view-following-method-alist
618 'wl-original-message-mode
619 (function wl-message-follow-current-entity))
620 (set-alist 'mime-edit-message-inserter-alist
621 'wl-draft-mode (function wl-draft-insert-current-message))
622 (set-alist 'mime-edit-mail-inserter-alist
623 'wl-draft-mode (function wl-draft-insert-get-message))
624 (set-alist 'mime-edit-split-message-sender-alist
626 (cdr (assq 'mail-mode mime-edit-split-message-sender-alist)))
627 (set-alist 'mime-raw-representation-type-alist
628 'wl-original-message-mode 'binary)
629 ;; Sort and highlight header fields.
630 (or wl-message-ignored-field-list
631 (setq wl-message-ignored-field-list
632 mime-view-ignored-field-list))
633 (or wl-message-visible-field-list
634 (setq wl-message-visible-field-list
635 mime-view-visible-field-list))
636 (set-alist 'mime-header-presentation-method-alist
637 'wl-original-message-mode
638 'wl-mime-display-header)
639 ;; To avoid overriding wl-draft-mode-map.
640 (when (boundp 'mime-setup-signature-key-alist)
641 (unless (assq 'wl-draft-mode mime-setup-signature-key-alist)
642 (setq mime-setup-signature-key-alist
643 (cons '(wl-draft-mode . "\C-c\C-w")
644 mime-setup-signature-key-alist)))))
647 (product-provide (provide 'wl-mime) (require 'wl-version))
649 ;;; wl-mime.el ends here