5ca53e19d6b2abd89881c015ccc6caf222b7cbfb
[elisp/wanderlust.git] / wl / tm-wl.el
1 ;;; wl-mime.el -- tm implementations of MIME processing on Wanderlust.
2
3 ;; Copyright 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 (autoload 'mime/editor-mode "tm-edit" nil t)
33 (autoload 'mime/edit-again "tm-edit" nil t)
34
35 (eval-when-compile (require 'tm-edit))
36
37 (defalias 'wl-draft-editor-mode 'mime/editor-mode)
38
39 (defun wl-draft-decode-message-in-buffer (&optional default-content-type)
40   (when default-content-type
41     (insert "Content-type: " default-content-type "\n")
42     (insert "\n"))
43   (mime-editor::edit-again 'code-conversion))
44
45 (defun wl-draft-yank-current-message-entity ()
46   "Yank currently displayed message entity.
47 By setting following-method as yank-content."
48   (let ((wl-draft-buffer (current-buffer))
49         (mime-viewer/following-method-alist
50          (list (cons 'wl-message-original-mode
51                      (function wl-draft-yank-to-draft-buffer)))))
52     (if (get-buffer (wl-current-message-buffer))
53         (save-excursion
54           (save-restriction
55             (set-buffer (wl-current-message-buffer))
56             (setq mime::preview/mother-buffer nil)
57             (widen)
58             (mime-viewer/follow-content))))))
59
60 (defmacro wl-draft-enclose-digest-region (beg end)
61   (` (mime-editor/enclose-region "digest" (, beg) (, end))))
62
63 (defun wl-draft-preview-message ()
64   (interactive)
65   (let ((mime-viewer/content-header-filter-hook 'wl-highlight-headers)
66         (mime-viewer/ignored-field-regexp "^:$")
67         (mime-editor/translate-buffer-hook
68          (append
69           (list 'wl-draft-config-exec)
70           mime-editor/translate-buffer-hook)))
71     (mime-editor/preview-message)
72     (let ((buffer-read-only nil))
73     (let ((buffer-read-only nil))
74       (when wl-highlight-body-too
75         (wl-highlight-body))
76       (run-hooks 'wl-draft-preview-message-hook)))))
77
78 (defmacro wl-draft-caesar-region (beg end)
79   (` (tm:caesar-region)))
80
81 (defalias 'wl-draft-insert-message 'mime-editor/insert-message)
82
83 (defalias 'wl-draft-insert-mail 'mime-editor/insert-mail)
84
85 ;;; Message
86
87 (defun wl-message-decode-mode (outbuf inbuf)
88   (let ((mime-viewer/content-header-filter-hook 'wl-highlight-headers))
89     (mime/viewer-mode nil nil nil inbuf outbuf)))
90
91 (defun wl-message-decode-with-all-header (outbuf inbuf)
92   (let ((mime-viewer/ignored-field-regexp "^:$")
93         (mime-viewer/content-header-filter-hook 'wl-highlight-headers))
94     (mime/viewer-mode nil nil nil inbuf outbuf)))
95
96 (defun wl-message-delete-mime-out-buf ()
97   (let (mime-out-buf mime-out-win)
98     (if (setq mime-out-buf (get-buffer mime/output-buffer-name))
99         (if (setq mime-out-win (get-buffer-window mime-out-buf))
100             (delete-window mime-out-win)))))
101
102 (defun wl-message-request-partial (folder number)
103   (elmo-set-work-buf
104    (elmo-read-msg-no-cache folder number (current-buffer))
105    (mime/parse-message nil nil)))
106
107 (defalias 'wl-message-read            'mime-viewer/scroll-up-content)
108 (defalias 'wl-message-next-content    'mime-viewer/next-content)
109 (defalias 'wl-message-prev-content    'mime-viewer/previous-content)
110 (defalias 'wl-message-play-content    'mime-viewer/play-content)
111 (defalias 'wl-message-extract-content 'mime-viewer/extract-content)
112 (defalias 'wl-message-quit            'mime-viewer/quit)
113 (defalias 'wl-message-button-dispatcher-internal
114   'tm:button-dispatcher)
115
116 ;;; Summary
117 (defun wl-summary-burst-subr (children target number)
118   ;; returns new number.
119   (let (content-type message-entity granch)
120     (while children
121       (setq content-type (mime::content-info/type (car children)))
122       (if (string-match "multipart" content-type)
123           (setq number (wl-summary-burst-subr
124                         (mime::content-info/children (car children))
125                         target
126                         number))
127         (when (string= "message/rfc822" (downcase content-type))
128           (message (format "Bursting...%s" (setq number (+ 1 number))))
129           (setq message-entity
130                 (car (mime::content-info/children (car children))))
131           (save-restriction
132             (narrow-to-region (mime::content-info/point-min message-entity)
133                               (mime::content-info/point-max message-entity))
134             (elmo-append-msg target
135                              (buffer-substring (point-min) (point-max))
136                              (std11-field-body "Message-ID")))))
137       (setq children (cdr children)))))
138
139 (defun wl-summary-burst ()
140   (interactive)
141   (let ((raw-buf (wl-message-get-original-buffer))
142         target
143         children message-entity content-type)
144     (save-excursion
145       (setq target wl-summary-buffer-folder-name)
146       (while (not (elmo-folder-writable-p target))
147         (setq target
148               (wl-summary-read-folder wl-default-folder "to extract to ")))
149       (wl-summary-set-message-buffer-or-redisplay)
150       (set-buffer raw-buf)
151       (setq children (mime::content-info/children mime::article/content-info))
152       (message "Bursting...")
153       (when children
154         (wl-summary-burst-subr children target 0))
155       (message "Bursting...done."))
156     (if (elmo-folder-plugged-p target)
157         (elmo-commit target))
158     (wl-summary-sync-update3)))
159
160 ;; internal variable.
161 (defvar wl-mime-save-dir nil "Last saved directory.")
162 ;;; Yet another save method.
163 (defun wl-mime-save-content (beg end cal)
164   (goto-char beg)
165   (let* ((name
166           (save-restriction
167             (narrow-to-region beg end)
168             (mime-article/get-filename cal)))
169          (encoding (cdr (assq 'encoding cal)))
170          (filename (read-file-name "Save to file: "
171                                    (expand-file-name
172                                     (or name ".")
173                                     (or wl-mime-save-dir
174                                         wl-tmp-dir))))
175          tmp-buf)
176     (while (file-directory-p filename)
177       (setq filename (read-file-name "Please set filename (not directory): "
178                                      filename)))
179     (if (file-exists-p filename)
180         (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
181             (error "Not saved")))
182     (setq wl-mime-save-dir (file-name-directory filename))
183     (setq tmp-buf (generate-new-buffer (file-name-nondirectory filename)))
184     (re-search-forward "\n\n")
185     (append-to-buffer tmp-buf (match-end 0) end)
186     (save-excursion
187       (set-buffer tmp-buf)
188       (mime-decode-region (point-min)(point-max) encoding)
189       (as-binary-output-file (write-file filename))
190       (kill-buffer tmp-buf))))
191
192 ;;; Yet another combine method.
193 (defun wl-mime-combine-message/partial-pieces (beg end cal)
194   (interactive)
195   (let* (folder
196          (msgdb (save-excursion
197                   (set-buffer wl-message-buffer-cur-summary-buffer)
198                   (setq folder wl-summary-buffer-folder-name)
199                   wl-summary-buffer-msgdb))
200          (mime-viewer/content-header-filter-hook 'wl-highlight-headers)
201          (id (cdr (assoc "id" cal)))
202          (mother mime::article/preview-buffer)
203          (target (cdr (assq 'major-mode cal)))
204          (article-buffer (buffer-name (current-buffer)))
205          (subject-buf (eval (cdr (assq 'summary-buffer-exp cal))))
206          subject-id overviews
207          (root-dir (expand-file-name
208                     (concat "m-prts-" (user-login-name)) mime/tmp-dir))
209          full-file)
210     (setq root-dir (concat root-dir "/" (replace-as-filename id)))
211     (setq full-file (concat root-dir "/FULL"))
212     (if (null target)
213         (error "%s is not supported. Sorry." target))
214     (if (or (file-exists-p full-file)
215             (not (y-or-n-p "Merge partials?")))
216         (mime-article/decode-message/partial beg end cal)
217       (message "Merging...")
218       (let (cinfo the-id parameters)
219         (setq subject-id
220               (eword-decode-string
221                (decode-mime-charset-string
222                 (std11-field-body "Subject")
223                 wl-summary-buffer-mime-charset)))
224         (if (string-match "[0-9\n]+" subject-id)
225             (setq subject-id (substring subject-id 0 (match-beginning 0))))
226         (setq overviews (elmo-msgdb-get-overview msgdb))
227         (catch 'tag
228           (while overviews
229             (when (string-match
230                    (regexp-quote subject-id)
231                    (elmo-msgdb-overview-entity-get-subject
232                     (car overviews)))
233               (setq cinfo
234                     (wl-message-request-partial
235                      folder
236                      (elmo-msgdb-overview-entity-get-number (car overviews))))
237               (setq parameters (mime::content-info/parameters cinfo))
238               (setq the-id (assoc-value "id" parameters))
239               (if (string= the-id id)
240                   (progn
241                     (set-buffer elmo-work-buf-name)
242                     (setq mime::article/preview-buffer
243                           (get-buffer wl-message-buf-name))
244                     (mime-article/decode-message/partial
245                      (point-min)(point-max) parameters)
246                     (if (file-exists-p full-file)
247                         (throw 'tag nil)))))
248             (setq overviews (cdr overviews)))
249           (message "Not all partials found."))))))
250
251 (defun wl-mime-setup ()
252   (require 'tm-view)
253   (set-alist 'mime-viewer/quitting-method-alist
254              'wl-message-original-mode 'wl-message-exit)
255   (set-alist 'mime-viewer/over-to-previous-method-alist
256              'wl-message-original-mode 'wl-message-exit)
257   (set-alist 'mime-viewer/over-to-next-method-alist
258              'wl-message-original-mode  'wl-message-exit)
259   (add-hook 'wl-summary-redisplay-hook 'wl-message-delete-mime-out-buf)
260   (add-hook 'wl-message-exit-hook      'wl-message-delete-mime-out-buf)
261   (set-atype 'mime/content-decoding-condition
262              '((type . "message/partial")
263                (method . wl-message-combine-message/partial-pieces)
264                (major-mode . wl-message-original-mode)
265                (summary-buffer-exp . wl-summary-buffer-name)))
266   (set-atype 'mime/content-decoding-condition
267              '((mode . "extract")
268                (method . wl-mime-save-content)
269                (major-mode . wl-message-original-mode))
270              'remove
271              '((method "tm-file"  nil 'file 'type 'encoding 'mode 'name)
272                (mode . "extract"))
273              'replacement)
274   (set-alist 'mime-viewer/following-method-alist
275              'wl-message-original-mode
276              (function wl-message-follow-current-entity))
277
278   (set-alist 'mime-editor/message-inserter-alist
279              'wl-draft-mode (function wl-draft-insert-current-message))
280   (set-alist 'mime-editor/mail-inserter-alist
281              'wl-draft-mode (function wl-draft-insert-get-message))
282   (set-alist 'mime-editor/split-message-sender-alist
283              'wl-draft-mode
284              (cdr (assq 'mail-mode
285                         mime-editor/split-message-sender-alist)))
286   (setq mime-viewer/code-converter-alist
287         (append
288          (list (cons 'wl-message-original-mode 'mime-charset/decode-buffer))
289          mime-viewer/code-converter-alist))
290   (defvar-maybe mime::preview/mother-buffer nil))
291   
292 (provide 'tm-wl)
293
294 ;;; tm-wl.el ends here