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 (message-buffer (wl-current-message-buffer)))
64 (unless message-buffer
65 (error "No message."))
66 (if (get-buffer message-buffer)
68 (set-buffer (wl-current-message-buffer))
71 (if (wl-region-exists-p)
72 (wl-mime-preview-follow-current-region)
73 (mime-preview-follow-current-entity)))))))
75 ;; modified mime-preview-follow-current-entity from mime-view.el
76 (defun wl-mime-preview-follow-current-region ()
77 "Write follow message to current region.
78 It calls following-method selected from variable
79 `mime-preview-following-method-alist'."
81 (let ((r-beg (region-beginning))
83 (entity (get-text-property (point-min)
85 (let* ((mode (mime-preview-original-major-mode 'recursive))
87 (format "%s-active-region" (buffer-name)))
89 (the-buf (current-buffer))
92 (set-buffer (setq new-buf (get-buffer-create new-name)))
95 (insert-buffer-substring the-buf r-beg r-end)
96 (goto-char (point-min))
98 (if (and (eq (mime-entity-media-type entity) 'message)
99 (eq (mime-entity-media-subtype entity) 'rfc822))
100 (car (mime-entity-children entity))
102 (while (and current-entity
103 (if (and (eq (mime-entity-media-type
104 current-entity) 'message)
105 (eq (mime-entity-media-subtype
106 current-entity) 'rfc822))
108 (mime-insert-header current-entity fields)
110 (setq fields (std11-collect-field-names)
111 current-entity (mime-entity-parent current-entity))
113 (let ((rest mime-view-following-required-fields-list)
116 (setq field-name (car rest))
117 (or (std11-field-body field-name)
121 (let ((entity (when mime-mother-buffer
122 (set-buffer mime-mother-buffer)
123 (get-text-property (point)
124 'mime-view-entity))))
126 (null (setq ret (mime-entity-fetch-field
127 entity field-name))))
128 (setq entity (mime-entity-parent entity)))))
130 (insert (concat field-name ": " ret "\n"))
132 (setq rest (cdr rest))
135 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
139 "Sorry, following method for %s is not implemented yet."
144 (defalias 'wl-draft-enclose-digest-region 'mime-edit-enclose-digest-region)
146 (defun wl-draft-preview-message ()
147 "Preview editing message."
150 (orig-buffer (current-buffer))
151 (current-point (point))
152 (config-exec-flag wl-draft-config-exec-flag)
153 (parent-folder wl-draft-parent-folder)
154 (mime-display-header-hook 'wl-highlight-headers)
155 (mime-header-encode-method-alist
157 '((wl-draft-eword-encode-address-list
158 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
159 (if (boundp 'mime-header-encode-method-alist)
160 (symbol-value 'mime-header-encode-method-alist))))
161 mime-view-ignored-field-list ; all header.
162 (mime-edit-translate-buffer-hook
167 (let ((wl-draft-config-exec-flag config-exec-flag)
168 (wl-draft-parent-folder parent-folder))
169 (goto-char current-point)
170 (run-hooks 'wl-draft-send-hook)
175 (wl-draft-deduce-address-list
179 (goto-char (point-min))
183 (regexp-quote mail-header-separator)
189 (kill-buffer (current-buffer))
190 (signal (car err) (cdr err)))))))))
191 mime-edit-translate-buffer-hook)))
192 (mime-edit-preview-message)
193 (let ((buffer-read-only nil))
194 (when wl-highlight-body-too
196 (run-hooks 'wl-draft-preview-message-hook))
197 (make-local-variable 'kill-buffer-hook)
198 (add-hook 'kill-buffer-hook
200 (when (get-buffer-window
201 wl-draft-preview-attributes-buffer-name)
202 (select-window (get-buffer-window
203 wl-draft-preview-attributes-buffer-name))
205 (when (get-buffer wl-draft-preview-attributes-buffer-name)
206 (kill-buffer (get-buffer
207 wl-draft-preview-attributes-buffer-name)))))
208 (if (not wl-draft-preview-attributes)
209 (message (concat "Recipients: " wl-recipients))
210 ; (ignore-errors ; in case when the window is too small
211 (let* ((cur-win (selected-window))
213 (- (window-height cur-win)
215 (- (window-height cur-win)
218 (1+ wl-draft-preview-attributes-buffer-lines))))))
219 (split-window cur-win (if (> size 0) size window-min-height))
220 (select-window (next-window))
221 (let ((pop-up-windows nil))
222 (switch-to-buffer (get-buffer-create
223 wl-draft-preview-attributes-buffer-name)))
225 (get-buffer wl-draft-preview-attributes-buffer-name)
226 (setq buffer-read-only t)
227 (let (buffer-read-only)
229 (dolist (attr wl-draft-preview-attributes-list)
230 (insert (capitalize (symbol-name attr)) ": "
232 (with-current-buffer orig-buffer
236 (concat "wl-" (symbol-name attr))))))
239 (goto-char (point-min))
240 (wl-highlight-headers)))
241 (select-window cur-win)))));)
243 (defalias 'wl-draft-caesar-region 'mule-caesar-region)
245 (defalias 'wl-draft-insert-message 'mime-edit-insert-message)
247 (defalias 'wl-draft-insert-mail 'mime-edit-insert-mail)
251 (defun wl-message-decode-mode (outbuf inbuf)
252 (let ((mime-view-content-header-filter-hook 'wl-highlight-headers)
253 (mime-display-header-hook 'wl-highlight-headers))
254 (mime-view-mode nil nil nil inbuf outbuf)))
256 (defun wl-message-decode-with-all-header (outbuf inbuf)
257 (let ((mime-view-ignored-field-regexp "^:$")
258 (mime-view-content-header-filter-hook 'wl-highlight-headers)
259 (mime-display-header-hook 'wl-highlight-headers)
260 mime-view-ignored-field-list)
261 (mime-view-mode nil nil nil inbuf outbuf)))
263 (defun wl-message-delete-mime-out-buf ()
264 (let (mime-out-buf mime-out-win)
265 (if (setq mime-out-buf (get-buffer mime-echo-buffer-name))
266 (if (setq mime-out-win (get-buffer-window mime-out-buf))
267 (delete-window mime-out-win)))))
269 (defun wl-message-request-partial (folder number)
271 (elmo-message-fetch (wl-folder-get-elmo-folder folder)
273 (elmo-make-fetch-strategy 'entire)
277 (mime-parse-buffer nil)))
279 (defalias 'wl-message-read 'mime-preview-scroll-up-entity)
280 (defalias 'wl-message-next-content 'mime-preview-move-to-next)
281 (defalias 'wl-message-prev-content 'mime-preview-move-to-previous)
282 (defalias 'wl-message-play-content 'mime-preview-play-current-entity)
283 (defalias 'wl-message-extract-content 'mime-preview-extract-current-entity)
284 (defalias 'wl-message-quit 'mime-preview-quit)
285 (defalias 'wl-message-button-dispatcher-internal
286 'mime-button-dispatcher)
288 (defsubst wl-mime-node-id-to-string (node-id)
290 (mapconcat (function (lambda (num) (format "%s" (1+ num))))
295 (defun wl-message-delete-current-part ()
296 "Delete a part under the cursor from the multipart message."
300 (let* ((entity (get-text-property (point) 'mime-view-entity))
301 (node-id (mime-entity-node-id entity))
302 (header-start (mime-buffer-entity-header-start-internal entity))
303 (body-end (mime-buffer-entity-body-end-internal entity))
304 (folder (wl-folder-get-elmo-folder wl-message-buffer-cur-folder))
305 (number wl-message-buffer-cur-number)
306 (msgid (elmo-message-field folder number 'message-id))
307 (orig-buf wl-message-buffer-original-buffer))
308 (if (eq (luna-class-name entity) 'mime-elmo-imap-entity)
309 (error "Please fetch the entire message (by typing 'C-u .') and try again"))
310 (with-current-buffer orig-buf
311 (unless (string-equal
313 (elmo-message-fetch folder number
314 (elmo-make-fetch-strategy 'entire)))
315 (error "Buffer content differs from actual message")))
316 (when (and (elmo-folder-writable-p folder)
317 (buffer-live-p orig-buf)
320 (format "Do you really want to delete part %s? "
321 (wl-mime-node-id-to-string node-id))))
322 (when (with-temp-buffer
323 (insert-buffer orig-buf)
324 (delete-region header-start body-end)
325 (goto-char header-start)
326 (insert "Content-Type: text/plain; charset=US-ASCII\n\n")
327 (insert "** This part has been removed by Wanderlust **\n\n")
328 (elmo-folder-append-buffer folder))
330 (elmo-folder-move-messages
332 (wl-folder-get-elmo-folder wl-trash-folder))
333 (when (and (elmo-cache-get-path msgid)
334 (file-exists-p (elmo-cache-get-path msgid)))
335 (delete-file (elmo-cache-get-path msgid)))
338 (wl-summary-delete-messages-on-buffer (list number))
339 (wl-summary-toggle-disp-msg 'off)
340 (setq wl-message-buffer nil)
341 (wl-summary-sync nil "update"))))))
343 (defun wl-message-decrypt-pgp-nonmime ()
344 "Decrypt PGP encrypted region"
349 (if (or (re-search-forward "^-+END PGP MESSAGE-+$" nil t)
350 (re-search-backward "^-+END PGP MESSAGE-+$" nil t))
351 (let (beg end status)
352 (setq end (match-end 0))
353 (if (setq beg (re-search-backward "^-+BEGIN PGP MESSAGE-+$" nil t))
354 (let ((inhibit-read-only t)
355 (buffer-file-coding-system wl-cs-autoconv))
356 (setq status (pgg-decrypt-region beg end))
357 (pgg-display-output-buffer beg end status))
358 (message "Cannot find pgp encrypted region")))
359 (message "Cannot find pgp encrypted region"))))
361 (defun wl-message-verify-pgp-nonmime ()
362 "Verify PGP signed region"
367 (if (and (or (re-search-forward "^-+END PGP SIGNATURE-+$" nil t)
368 (re-search-backward "^-+END PGP SIGNATURE-+$" nil t))
369 (re-search-backward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
372 (situation (mime-preview-find-boundary-info))
373 (p-end (aref situation 1))
374 (entity (aref situation 2))
377 (while (< beg (point))
378 (if (re-search-backward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
379 (setq count (+ count 1))
382 (set-buffer-multibyte nil)
383 (insert (mime-entity-body entity))
384 (goto-char (point-max))
386 (if (re-search-backward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
387 (setq count (- count 1))
389 (let ((r-beg (point))
390 (r-end (re-search-forward "^-+END PGP SIGNATURE-+$" nil t)))
392 (setq status (pgg-verify-region r-beg r-end nil 'fetch))
394 (mime-show-echo-buffer)
395 (set-buffer mime-echo-buffer-name)
397 (get-buffer-window mime-echo-buffer-name)
400 (insert-buffer-substring
401 (if status pgg-output-buffer pgg-errors-buffer))
402 (decode-coding-region m-beg (point) wl-cs-autoconv))
403 (message "Cannot find pgp signed region"))))
405 ;; XXX: encrypted multipart isn't represented as multipart
406 (defun wl-mime-preview-application/pgp (parent-entity entity situation)
408 (goto-char (point-max))
410 raw-buf to-buf representation-type child-entity)
413 (narrow-to-region p p)
414 (setq to-buf (current-buffer))
416 (setq raw-buf (current-buffer))
417 (mime-insert-entity entity)
419 (goto-char (point-min))
420 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
421 (pgg-decrypt-region (point-min)(point-max))
422 (delete-region (point-min) (point-max))
423 (insert-buffer pgg-output-buffer)
424 (setq representation-type 'elmo-buffer))
425 (setq child-entity (mime-parse-message
426 (mm-expand-class-name representation-type)
429 (mime-entity-node-id-internal parent-entity)))
435 (entity-button . invisible))
438 (defun wl-mime-preview-application/pgp-encrypted (entity situation)
439 (let* ((entity-node-id (mime-entity-node-id entity))
440 (mother (mime-entity-parent entity))
441 (knum (car entity-node-id))
445 (orig-entity (nth onum (mime-entity-children mother))))
446 (wl-mime-preview-application/pgp entity orig-entity situation)))
449 (defun wl-summary-burst-subr (message-entity target number)
450 ;; returns new number.
451 (let (content-type entity)
452 (setq content-type (mime-entity-content-type message-entity))
453 (cond ((eq (cdr (assq 'type content-type)) 'multipart)
454 (dolist (entity (mime-entity-children message-entity))
455 (setq number (wl-summary-burst-subr
459 ((and (eq (cdr (assq 'type content-type)) 'message)
460 (eq (cdr (assq 'subtype content-type)) 'rfc822))
461 (message "Bursting...%s" (setq number (+ 1 number)))
463 (car (mime-entity-children message-entity)))
465 (insert (mime-entity-body message-entity))
466 (elmo-folder-append-buffer target))))
469 (defun wl-summary-burst (&optional arg)
470 "De-capsulate embedded messages in MIME format.
471 With ARG, ask destination folder."
473 (let ((raw-buf (wl-summary-get-original-buffer))
474 (view-buf wl-message-buffer)
475 children message-entity content-type target-name target)
477 (setq target wl-summary-buffer-elmo-folder)
478 (when (or arg (not (elmo-folder-writable-p target)))
479 (setq target-name (wl-summary-read-folder wl-default-folder "to extract to"))
480 (setq target (wl-folder-get-elmo-folder target-name)))
481 (wl-summary-set-message-buffer-or-redisplay)
482 (with-current-buffer view-buf
483 (setq message-entity (get-text-property (point-min) 'mime-view-entity)))
485 (message "Bursting...")
486 (with-current-buffer raw-buf
487 (wl-summary-burst-subr message-entity target 0))
488 (message "Bursting...done"))
489 (if (elmo-folder-plugged-p target)
490 (elmo-folder-check target)))
491 (when (or (not target-name)
492 (string= wl-summary-buffer-folder-name target-name))
493 (save-excursion (wl-summary-sync-update)))))
495 ;; internal variable.
496 (defvar wl-mime-save-directory nil "Last saved directory.")
497 ;;; Yet another save method.
498 (defun wl-mime-save-content (entity situation)
499 (let ((filename (read-file-name "Save to file: "
501 (or (mime-entity-safe-filename entity)
503 (or wl-mime-save-directory
504 wl-temporary-file-directory)))))
505 (while (file-directory-p filename)
506 (setq filename (read-file-name "Please set filename (not directory): "
508 (if (and (file-exists-p filename)
509 (not (yes-or-no-p (format "File %s exists. Save anyway? "
511 (message "Not saved")
512 (setq wl-mime-save-directory (file-name-directory filename))
513 (mime-write-entity-content entity filename))))
515 ;;; Yet another combine method.
516 (defun wl-mime-combine-message/partial-pieces (entity situation)
517 "Internal method for wl to combine message/partial messages automatically."
519 (let* ((folder (save-excursion
520 (set-buffer wl-message-buffer-cur-summary-buffer)
521 wl-summary-buffer-elmo-folder))
522 (mime-display-header-hook 'wl-highlight-headers)
523 (folder wl-message-buffer-cur-folder)
524 (id (or (cdr (assoc "id" situation)) ""))
525 (mother (current-buffer))
526 (summary-buf wl-message-buffer-cur-summary-buffer)
528 (root-dir (expand-file-name
529 (concat "m-prts-" (user-login-name))
530 temporary-file-directory))
532 (setq root-dir (concat root-dir "/" (replace-as-filename id)))
533 (setq full-file (concat root-dir "/FULL"))
534 (if (or (file-exists-p full-file)
535 (not (y-or-n-p "Merge partials? ")))
536 (with-current-buffer mother
537 (mime-store-message/partial-piece entity situation)
538 (setq wl-message-buffer-cur-summary-buffer summary-buf)
539 (make-variable-buffer-local 'mime-preview-over-to-next-method-alist)
540 (setq mime-preview-over-to-next-method-alist
541 (cons (cons 'mime-show-message-mode 'wl-message-exit)
542 mime-preview-over-to-next-method-alist))
543 (make-variable-buffer-local 'mime-preview-over-to-previous-method-alist)
544 (setq mime-preview-over-to-previous-method-alist
545 (cons (cons 'mime-show-message-mode 'wl-message-exit)
546 mime-preview-over-to-previous-method-alist)))
549 (decode-mime-charset-string
550 (mime-entity-read-field entity 'Subject)
551 wl-summary-buffer-mime-charset)))
552 (if (string-match "[0-9\n]+" subject-id)
553 (setq subject-id (substring subject-id 0 (match-beginning 0))))
555 (elmo-folder-do-each-message-entity (entity folder)
557 (regexp-quote subject-id)
558 (elmo-message-entity-field entity 'subject))
560 ;; request message at the cursor in Subject buffer.
561 (wl-message-request-partial
563 (elmo-message-entity-number entity)))
564 (situation (mime-entity-situation message))
565 (the-id (or (cdr (assoc "id" situation)) "")))
566 (when (string= (downcase the-id)
568 (with-current-buffer mother
569 (mime-store-message/partial-piece message situation))
570 (if (file-exists-p full-file)
571 (throw 'tag nil))))))
572 (message "Not all partials found.")))))
574 (defun wl-mime-display-text/plain (entity situation)
576 (mime-display-text/plain entity situation)
577 (wl-highlight-message beg (point-max) t t)))
579 (defun wl-mime-display-header (entity situation)
580 (let ((elmo-message-ignored-field-list
581 (if wl-message-buffer-all-header-flag
583 wl-message-ignored-field-list))
584 (elmo-message-visible-field-list wl-message-visible-field-list)
585 (elmo-message-sorted-field-list wl-message-sort-field-list))
586 (elmo-mime-insert-header entity situation)
587 (wl-highlight-headers)))
589 (defun wl-mime-decrypt-application/pgp-encrypted (entity situation)
590 (let ((summary-buffer wl-message-buffer-cur-summary-buffer)
591 (original-buffer wl-message-buffer-original-buffer))
592 (mime-decrypt-application/pgp-encrypted entity situation)
593 (setq wl-message-buffer-cur-summary-buffer summary-buffer)
594 (setq wl-message-buffer-original-buffer original-buffer)))
598 (defun wl-mime-setup ()
599 (set-alist 'mime-preview-quitting-method-alist
600 'wl-original-message-mode 'wl-message-exit)
601 (set-alist 'mime-view-over-to-previous-method-alist
602 'wl-original-message-mode 'wl-message-exit)
603 (set-alist 'mime-view-over-to-next-method-alist
604 'wl-original-message-mode 'wl-message-exit)
605 (set-alist 'mime-preview-over-to-previous-method-alist
606 'wl-original-message-mode 'wl-message-exit)
607 (set-alist 'mime-preview-over-to-next-method-alist
608 'wl-original-message-mode 'wl-message-exit)
609 (add-hook 'wl-summary-redisplay-hook 'wl-message-delete-mime-out-buf)
610 (add-hook 'wl-message-exit-hook 'wl-message-delete-mime-out-buf)
612 (ctree-set-calist-strictly
613 'mime-preview-condition
614 '((type . text) (subtype . plain)
616 (body-presentation-method . wl-mime-display-text/plain)
617 (major-mode . wl-original-message-mode)))
619 (ctree-set-calist-strictly
620 'mime-preview-condition
621 '((type . application)(subtype . pgp-encrypted)
624 (body-presentation-method . wl-mime-preview-application/pgp-encrypted)
625 (major-mode . wl-original-message-mode)))
627 (ctree-set-calist-strictly
628 'mime-acting-condition
629 '((type . message) (subtype . partial)
630 (method . wl-mime-combine-message/partial-pieces)
631 (request-partial-message-method . wl-message-request-partial)
632 (major-mode . wl-original-message-mode)))
634 (ctree-set-calist-strictly
635 'mime-acting-condition
636 '((type . application) (subtype . pgp-encrypted)
637 (method . wl-mime-decrypt-application/pgp-encrypted)
638 (major-mode . wl-original-message-mode)))
640 (ctree-set-calist-strictly
641 'mime-acting-condition
643 (major-mode . wl-original-message-mode)
644 (method . wl-mime-save-content)))
645 (set-alist 'mime-preview-following-method-alist
646 'wl-original-message-mode
647 (function wl-message-follow-current-entity))
648 (set-alist 'mime-view-following-method-alist
649 'wl-original-message-mode
650 (function wl-message-follow-current-entity))
651 (set-alist 'mime-edit-message-inserter-alist
652 'wl-draft-mode (function wl-draft-insert-current-message))
653 (set-alist 'mime-edit-mail-inserter-alist
654 'wl-draft-mode (function wl-draft-insert-get-message))
655 (set-alist 'mime-edit-split-message-sender-alist
657 (cdr (assq 'mail-mode mime-edit-split-message-sender-alist)))
658 (set-alist 'mime-raw-representation-type-alist
659 'wl-original-message-mode 'binary)
660 ;; Sort and highlight header fields.
661 (or wl-message-ignored-field-list
662 (setq wl-message-ignored-field-list
663 mime-view-ignored-field-list))
664 (or wl-message-visible-field-list
665 (setq wl-message-visible-field-list
666 mime-view-visible-field-list))
667 (set-alist 'mime-header-presentation-method-alist
668 'wl-original-message-mode
669 'wl-mime-display-header)
670 ;; To avoid overriding wl-draft-mode-map.
671 (when (boundp 'mime-setup-signature-key-alist)
672 (unless (assq 'wl-draft-mode mime-setup-signature-key-alist)
673 (setq mime-setup-signature-key-alist
674 (cons '(wl-draft-mode . "\C-c\C-w")
675 mime-setup-signature-key-alist)))))
678 (product-provide (provide 'wl-mime) (require 'wl-version))
680 ;;; wl-mime.el ends here