* wl-expire.el (wl-summary-archive): Fixed;
[elisp/wanderlust.git] / wl / wl-mime.el
1 ;;; wl-mime.el -- SEMI implementations of MIME processing on Wanderlust.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7
8 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
9
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.
14 ;;
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.
19 ;;
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.
24 ;;
25
26 ;;; Commentary:
27 ;; 
28
29 ;;; Code:
30 ;; 
31
32 (require 'mime-view)
33 (require 'mime-edit)
34 (require 'mime-play)
35 (require 'elmo)
36
37 (eval-when-compile
38   (defalias-maybe 'Meadow-version 'ignore))
39
40 (defvar xemacs-betaname)
41 (defvar xemacs-codename)
42 (defvar enable-multibyte-characters)
43 (defvar mule-version)
44
45 ;;; Draft
46
47 (defalias 'wl-draft-editor-mode 'mime-edit-mode)
48
49 (defalias 'wl-draft-decode-message-in-buffer
50   'mime-edit-decode-message-in-buffer)
51
52 (defun wl-draft-yank-current-message-entity ()
53   "Yank currently displayed message entity.
54 By setting following-method as yank-content."
55   (let ((wl-draft-buffer (current-buffer))
56         (mime-view-following-method-alist
57          (list (cons 'wl-original-message-mode
58                      (function wl-draft-yank-to-draft-buffer))))
59         (mime-preview-following-method-alist
60          (list (cons 'wl-original-message-mode
61                      (function wl-draft-yank-to-draft-buffer)))))
62     (if (get-buffer (wl-current-message-buffer))
63         (save-excursion
64           (save-restriction
65             (set-buffer (wl-current-message-buffer))
66             (widen)
67             (mime-preview-follow-current-entity))))))
68
69 (defalias 'wl-draft-enclose-digest-region 'mime-edit-enclose-digest-region)
70
71 (defun wl-draft-preview-message ()
72   ""
73   (interactive)
74   (let* (recipients-message
75          (config-exec-flag wl-draft-config-exec-flag)
76          (mime-display-header-hook 'wl-highlight-headers)
77          mime-view-ignored-field-list   ; all header.
78          (mime-edit-translate-buffer-hook
79           (append
80            (list
81             (function
82              (lambda ()
83                (let ((wl-draft-config-exec-flag config-exec-flag))
84                  (run-hooks 'wl-draft-send-hook)
85                  (setq recipients-message
86                        (condition-case err
87                            (concat "Recipients: "
88                                    (mapconcat
89                                     'identity
90                                     (wl-draft-deduce-address-list
91                                      (current-buffer)
92                                      (point-min)
93                                      (save-excursion
94                                        (goto-char (point-min))
95                                        (re-search-forward
96                                         (concat
97                                          "^"
98                                          (regexp-quote mail-header-separator)
99                                          "$")
100                                         nil t)
101                                        (point)))
102                                     ", "))
103                          (error
104                           (kill-buffer (current-buffer))
105                           (signal (car err) (cdr err)))))))))
106            mime-edit-translate-buffer-hook)))
107     (mime-edit-preview-message)
108     (let ((buffer-read-only nil))
109       (when wl-highlight-body-too
110         (wl-highlight-body))
111       (run-hooks 'wl-draft-preview-message-hook))
112     (message recipients-message)))
113
114 (defalias 'wl-draft-caesar-region  'mule-caesar-region)
115
116 (defalias 'wl-draft-insert-message 'mime-edit-insert-message)
117
118 (defalias 'wl-draft-insert-mail 'mime-edit-insert-mail)
119
120 ;;; Message
121
122 (defun wl-message-decode-mode (outbuf inbuf)
123   (let ((mime-view-content-header-filter-hook 'wl-highlight-headers)
124         (mime-display-header-hook 'wl-highlight-headers))
125     (mime-view-mode nil nil nil inbuf outbuf)))
126
127 (defun wl-message-decode-with-all-header (outbuf inbuf)
128   (let ((mime-view-ignored-field-regexp "^:$")
129         (mime-view-content-header-filter-hook 'wl-highlight-headers)
130         (mime-display-header-hook 'wl-highlight-headers)
131         mime-view-ignored-field-list)
132     (mime-view-mode nil nil nil inbuf outbuf)))
133
134 (defun wl-message-delete-mime-out-buf ()
135   (let (mime-out-buf mime-out-win)
136     (if (setq mime-out-buf (get-buffer mime-echo-buffer-name))
137         (if (setq mime-out-win (get-buffer-window mime-out-buf))
138             (delete-window mime-out-win)))))
139
140 (defun wl-message-request-partial (folder number)
141   (elmo-set-work-buf
142    (elmo-message-fetch (wl-folder-get-elmo-folder folder)
143                        number 
144                        (elmo-make-fetch-strategy 'entire)
145                        nil
146                        (current-buffer)
147                        'unread)
148    (mime-parse-buffer nil)))
149
150 (defalias 'wl-message-read            'mime-preview-scroll-up-entity)
151 (defalias 'wl-message-next-content    'mime-preview-move-to-next)
152 (defalias 'wl-message-prev-content    'mime-preview-move-to-previous)
153 (defalias 'wl-message-play-content    'mime-preview-play-current-entity)
154 (defalias 'wl-message-extract-content 'mime-preview-extract-current-entity)
155 (defalias 'wl-message-quit            'mime-preview-quit)
156 (defalias 'wl-message-button-dispatcher-internal
157   'mime-button-dispatcher)
158
159 ;;; Summary
160 (defun wl-summary-burst-subr (message-entity target number)
161   ;; returns new number.
162   (let (content-type entity)
163     (setq content-type (mime-entity-content-type message-entity))
164     (cond ((eq (cdr (assq 'type content-type)) 'multipart)
165            (dolist (entity (mime-entity-children message-entity))
166              (setq number (wl-summary-burst-subr
167                            entity
168                            target
169                            number))))
170           ((and (eq (cdr (assq 'type content-type)) 'message)
171                 (eq (cdr (assq 'subtype content-type)) 'rfc822))
172            (message (format "Bursting...%s" (setq number (+ 1 number))))
173            (setq entity
174                  (car (mime-entity-children message-entity)))
175            (with-temp-buffer
176              (insert (mime-entity-body message-entity))
177              (elmo-folder-append-buffer
178               target
179               (mime-entity-fetch-field entity "Message-ID")))))
180     number))
181
182 (defun wl-summary-burst ()
183   ""
184   (interactive)
185   (let ((raw-buf (wl-summary-get-original-buffer))
186         (view-buf wl-message-buffer)
187         children message-entity content-type target)
188     (save-excursion
189       (setq target wl-summary-buffer-elmo-folder)
190       (while (not (elmo-folder-writable-p target))
191         (setq target
192               (wl-summary-read-folder wl-default-folder "to extract to")))
193       (wl-summary-set-message-buffer-or-redisplay)
194       (with-current-buffer view-buf
195         (setq message-entity (get-text-property (point-min) 'mime-view-entity)))
196       (when message-entity
197         (message "Bursting...")
198         (with-current-buffer raw-buf
199           (wl-summary-burst-subr message-entity target 0))
200         (message "Bursting...done"))
201       (if (elmo-folder-plugged-p target)
202           (elmo-folder-check target)))
203     (wl-summary-sync-update)))
204
205 ;; internal variable.
206 (defvar wl-mime-save-dir nil "Last saved directory.")
207 ;;; Yet another save method.
208 (defun wl-mime-save-content (entity situation)
209   (let ((filename (read-file-name "Save to file: "
210                                   (expand-file-name
211                                    (or (mime-entity-safe-filename entity)
212                                        ".")
213                                    (or wl-mime-save-dir
214                                        wl-tmp-dir)))))
215     (while (file-directory-p filename)
216       (setq filename (read-file-name "Please set filename (not directory): "
217                                      filename)))
218     (if (file-exists-p filename)
219         (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
220             (error "Not saved")))
221     (setq wl-mime-save-dir (file-name-directory filename))
222     (mime-write-entity-content entity filename)))
223
224 ;;; Yet another combine method.
225 (defun wl-mime-combine-message/partial-pieces (entity situation)
226   "Internal method for wl to combine message/partial messages automatically."
227   (interactive)
228   (let* ((msgdb (save-excursion
229                   (set-buffer wl-message-buffer-cur-summary-buffer)
230                   (wl-summary-buffer-msgdb)))
231          (mime-display-header-hook 'wl-highlight-headers)
232          (folder wl-message-buffer-cur-folder)
233          (id (or (cdr (assoc "id" situation)) ""))
234          (mother (current-buffer))
235          (summary-buf wl-message-buffer-cur-summary-buffer)
236          subject-id overviews
237          (root-dir (expand-file-name
238                     (concat "m-prts-" (user-login-name))
239                     temporary-file-directory))
240          full-file point)
241     (setq root-dir (concat root-dir "/" (replace-as-filename id)))
242     (setq full-file (concat root-dir "/FULL"))
243     (if (or (file-exists-p full-file)
244             (not (y-or-n-p "Merge partials? ")))
245         (with-current-buffer mother
246           (mime-store-message/partial-piece entity situation)
247           (setq wl-message-buffer-cur-summary-buffer summary-buf)
248           (make-variable-buffer-local 'mime-preview-over-to-next-method-alist)
249           (setq mime-preview-over-to-next-method-alist
250                 (cons (cons 'mime-show-message-mode 'wl-message-exit)
251                       mime-preview-over-to-next-method-alist))
252           (make-variable-buffer-local 'mime-preview-over-to-previous-method-alist)
253           (setq mime-preview-over-to-previous-method-alist
254                 (cons (cons 'mime-show-message-mode 'wl-message-exit)
255                       mime-preview-over-to-previous-method-alist)))
256       (setq subject-id
257             (eword-decode-string
258              (decode-mime-charset-string
259               (mime-entity-read-field entity 'Subject)
260               wl-summary-buffer-mime-charset)))
261       (if (string-match "[0-9\n]+" subject-id)
262           (setq subject-id (substring subject-id 0 (match-beginning 0))))
263       (setq overviews (elmo-msgdb-get-overview msgdb))
264       (catch 'tag
265         (while overviews
266           (when (string-match
267                  (regexp-quote subject-id)
268                  (elmo-msgdb-overview-entity-get-subject (car overviews)))
269             (let* ((message
270                     ;; request message at the cursor in Subject buffer.
271                     (wl-message-request-partial
272                      folder
273                      (elmo-msgdb-overview-entity-get-number
274                       (car overviews))))
275                    (situation (mime-entity-situation message))
276                    (the-id (or (cdr (assoc "id" situation)) "")))
277               (when (string= (downcase the-id)
278                              (downcase id))
279                 (with-current-buffer mother
280                   (mime-store-message/partial-piece message situation))
281                 (if (file-exists-p full-file)
282                     (throw 'tag nil)))))
283           (setq overviews (cdr overviews)))
284         (message "Not all partials found.")))))
285
286 (defun wl-mime-display-text/plain (entity situation)
287   (let ((beg (point)))
288     (mime-display-text/plain entity situation)
289     (wl-highlight-message beg (point-max) t t)))
290
291 (defun wl-mime-display-header (entity situation)
292   (let ((elmo-message-ignored-field-list
293          (if wl-message-buffer-all-header-flag
294              nil
295            wl-message-ignored-field-list))
296         (elmo-message-visible-field-list wl-message-visible-field-list)
297         (elmo-message-sorted-field-list wl-message-sort-field-list))
298     (elmo-mime-insert-header entity situation)
299     (wl-highlight-headers)))
300
301 ;;; Setup methods.
302 (defun wl-mime-setup ()
303   (set-alist 'mime-preview-quitting-method-alist
304              'wl-original-message-mode 'wl-message-exit)
305   (set-alist 'mime-view-over-to-previous-method-alist
306              'wl-original-message-mode 'wl-message-exit)
307   (set-alist 'mime-view-over-to-next-method-alist
308              'wl-original-message-mode 'wl-message-exit)
309   (set-alist 'mime-preview-over-to-previous-method-alist
310              'wl-original-message-mode 'wl-message-exit)
311   (set-alist 'mime-preview-over-to-next-method-alist
312              'wl-original-message-mode 'wl-message-exit)
313   (add-hook 'wl-summary-redisplay-hook 'wl-message-delete-mime-out-buf)
314   (add-hook 'wl-message-exit-hook 'wl-message-delete-mime-out-buf)
315
316   (ctree-set-calist-strictly
317    'mime-preview-condition
318    '((type . text) (subtype . plain)
319      (body . visible)
320      (body-presentation-method . wl-mime-display-text/plain)
321      (major-mode . wl-original-message-mode)))
322
323   (ctree-set-calist-strictly
324    'mime-acting-condition
325    '((type . message) (subtype . partial)
326      (method .  wl-mime-combine-message/partial-pieces)
327      (request-partial-message-method . wl-message-request-partial)
328      (major-mode . wl-original-message-mode)))
329
330   (ctree-set-calist-strictly
331    'mime-acting-condition
332    '((mode . "extract")
333      (major-mode . wl-original-message-mode)
334      (method . wl-mime-save-content)))
335   (set-alist 'mime-preview-following-method-alist
336              'wl-original-message-mode
337              (function wl-message-follow-current-entity))
338   (set-alist 'mime-view-following-method-alist
339              'wl-original-message-mode
340              (function wl-message-follow-current-entity))
341   (set-alist 'mime-edit-message-inserter-alist
342              'wl-draft-mode (function wl-draft-insert-current-message))
343   (set-alist 'mime-edit-mail-inserter-alist
344              'wl-draft-mode (function wl-draft-insert-get-message))
345   (set-alist 'mime-edit-split-message-sender-alist
346              'wl-draft-mode
347              (cdr (assq 'mail-mode mime-edit-split-message-sender-alist)))
348   (set-alist 'mime-raw-representation-type-alist
349              'wl-original-message-mode 'binary)
350   ;; Sort and highlight header fields.
351   (or wl-message-ignored-field-list
352       (setq wl-message-ignored-field-list
353             mime-view-ignored-field-list))
354   (or wl-message-visible-field-list
355       (setq wl-message-visible-field-list
356             mime-view-visible-field-list))
357   (set-alist 'mime-header-presentation-method-alist
358              'wl-original-message-mode
359              'wl-mime-display-header)
360   ;; To avoid overriding wl-draft-mode-map.
361   (when (boundp 'mime-setup-signature-key-alist)
362     (unless (assq 'wl-draft-mode mime-setup-signature-key-alist)
363       (setq mime-setup-signature-key-alist
364             (cons '(wl-draft-mode . "\C-c\C-w")
365                   mime-setup-signature-key-alist)))))
366
367 (require 'product)
368 (product-provide (provide 'wl-mime) (require 'wl-version))
369
370 ;;; wl-mime.el ends here