* mmelmo.el (mime-parse-parameters-from-list): Don't downcase
[elisp/wanderlust.git] / elmo / mmelmo.el
1 ;;; mmelmo.el -- mm-backend by ELMO.
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 ELMO (Elisp Library for Message Orchestration).
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 (require 'elmo-vars)
32 (require 'elmo-util)
33 (require 'mime-parse)
34 (require 'mmbuffer)
35
36 (require 'product)
37 (product-provide (provide 'mmelmo) (require 'elmo-version))
38 (require 'mmelmo-imap4)
39
40 (eval-and-compile
41   (luna-define-class mime-elmo-entity (mime-buffer-entity)
42                      (imap folder number msgdb size))
43   (luna-define-internal-accessors 'mime-elmo-entity))
44
45 (defvar mmelmo-force-reload nil)
46 (defvar mmelmo-sort-field-list nil)
47
48 (defvar mmelmo-header-max-column fill-column
49   "*Inserted header is folded with this value.
50 If function is specified, its return value is used.")
51
52 (defvar mmelmo-header-inserted-hook nil
53   "*A hook called when header is inserted.")
54
55 (defvar mmelmo-entity-content-inserted-hook nil
56   "*A hook called when entity-content is inserted.")
57
58 (defun mmelmo-get-original-buffer ()
59   (let ((ret-val (get-buffer (concat mmelmo-entity-buffer-name "0"))))
60     (if (not ret-val)
61         (save-excursion
62           (set-buffer (setq ret-val
63                             (get-buffer-create
64                              (concat mmelmo-entity-buffer-name "0"))))
65           (mmelmo-original-mode)))
66     ret-val))
67
68 (defun mmelmo-cleanup-entity-buffers ()
69   "Cleanup entity buffers of mmelmo."
70   (mapcar (lambda (x)
71             (if (string-match mmelmo-entity-buffer-name x)
72                 (kill-buffer x)))
73           (mapcar 'buffer-name (buffer-list))))
74
75 ;; For FLIM 1-13.x
76 (defun-maybe mime-entity-body (entity)
77   (luna-send entity 'mime-entity-body))
78
79 (defun mmelmo-insert-sorted-header-from-buffer (buffer
80                                                 start end
81                                                 &optional invisible-fields
82                                                 visible-fields
83                                                 sorted-fields)
84   (let ((the-buf (current-buffer))
85         (mode-obj (mime-find-field-presentation-method 'wide))
86         field-decoder
87         f-b p f-e field-name field field-body
88         vf-alist (sl sorted-fields))
89     (save-excursion
90       (set-buffer buffer)
91       (save-restriction
92         (narrow-to-region start end)
93         (goto-char start)
94         (while (re-search-forward std11-field-head-regexp nil t)
95           (setq f-b (match-beginning 0)
96                 p (match-end 0)
97                 field-name (buffer-substring f-b p)
98                 f-e (std11-field-end))
99           (when (mime-visible-field-p field-name
100                                       visible-fields invisible-fields)
101             (setq field (intern
102                          (capitalize (buffer-substring f-b (1- p))))
103                   field-body (buffer-substring p f-e)
104                   field-decoder (inline (mime-find-field-decoder-internal
105                                          field mode-obj)))
106             (setq vf-alist (append (list
107                                     (cons field-name
108                                           (list field-body field-decoder)))
109                                    vf-alist))))
110         (and vf-alist
111              (setq vf-alist
112                    (sort vf-alist
113                          (function (lambda (s d)
114                                      (let ((n 0) re
115                                            (sf (car s))
116                                            (df (car d)))
117                                        (catch 'done
118                                          (while (setq re (nth n sl))
119                                            (setq n (1+ n))
120                                            (and (string-match re sf)
121                                                 (throw 'done t))
122                                            (and (string-match re df)
123                                                 (throw 'done nil)))
124                                          t)))))))
125         (with-current-buffer the-buf
126           (while vf-alist
127             (let* ((vf (car vf-alist))
128                    (field-name (car vf))
129                    (field-body (car (cdr vf)))
130                    (field-decoder (car (cdr (cdr vf)))))
131               (insert field-name)
132               (insert (if field-decoder
133                           (funcall field-decoder field-body
134                                    (string-width field-name)
135                                    (if (functionp mmelmo-header-max-column)
136                                        (funcall mmelmo-header-max-column)
137                                      mmelmo-header-max-column))
138                         ;; Don't decode
139                         field-body))
140               (insert "\n"))
141             (setq vf-alist (cdr vf-alist)))
142           (run-hooks 'mmelmo-header-inserted-hook))))))
143
144 (defun mmelmo-original-mode ()
145   (setq major-mode 'mmelmo-original-mode)
146   (setq buffer-read-only t)
147   (elmo-set-buffer-multibyte nil)
148   (setq mode-name "MMELMO-Original"))
149
150 ;; For FLIMs without rfc2231 feature .
151 (if (not (fboundp 'mime-parse-parameters-from-list))
152     (defun mime-parse-parameters-from-list (attrlist)
153       (let (ret-val)
154         (if (not (eq (% (length attrlist) 2) 0))
155             (message "Invalid attributes."))
156         (while attrlist
157           (setq ret-val (append ret-val
158                                 (list (cons (downcase (car attrlist))
159                                             (car (cdr attrlist))))))
160           (setq attrlist (cdr (cdr attrlist))))
161         ret-val)))
162
163 (luna-define-method initialize-instance :after ((entity mime-elmo-entity)
164                                                 &rest init-args)
165   "The initialization method for elmo.
166 mime-elmo-entity has its own member variable,
167 `imap', `folder', `msgdb' and `size'.
168 imap:   boolean. if non-nil, entity becomes mime-elmo-imap4-entity class.
169 folder: string.  folder name.
170 msgdb:  msgdb of elmo.
171 size:   size of the entity."
172   (if (mime-elmo-entity-imap-internal entity)
173       ;; use imap part fetching.
174       ;; child mime-entity's class becomes `mime-elmo-imap4-entity'
175       ;; which implements `entity-buffer' method.
176       (progn
177         (let (new-entity)
178           (mime-buffer-entity-set-buffer-internal entity nil)
179           (setq new-entity
180                 (mmelmo-imap4-get-mime-entity
181                  (mime-elmo-entity-folder-internal entity) ; folder
182                  (mime-elmo-entity-number-internal entity) ; number
183                  (mime-elmo-entity-msgdb-internal entity)  ; msgdb
184                  ))
185           (mime-entity-set-content-type-internal
186            entity
187            (mime-entity-content-type-internal new-entity))
188           (mime-entity-set-encoding-internal
189            entity
190            (mime-entity-encoding-internal new-entity))
191           (mime-entity-set-children-internal
192            entity
193            (mime-entity-children-internal new-entity))
194           (mime-elmo-entity-set-size-internal
195            entity
196            (mime-elmo-entity-size-internal new-entity))
197           (mime-entity-set-representation-type-internal
198            entity 'mime-elmo-imap4-entity)
199           entity))
200     (set-buffer (mime-buffer-entity-buffer-internal entity))
201     (mmelmo-original-mode)
202     (when (mime-root-entity-p entity)
203       (let ((buffer-read-only nil)
204             header-end body-start)
205         (erase-buffer)
206         (elmo-read-msg-with-buffer-cache
207          (mime-elmo-entity-folder-internal entity)
208          (mime-elmo-entity-number-internal entity)
209          (current-buffer)
210          (mime-elmo-entity-msgdb-internal entity)
211          mmelmo-force-reload)
212         (goto-char (point-min))
213         (if (re-search-forward
214              (concat "^" (regexp-quote mail-header-separator) "$\\|^$" )
215              nil t)
216             (setq header-end (match-beginning 0)
217                   body-start (if (= (match-end 0) (point-max))
218                                  (point-max)
219                                (1+ (match-end 0))))
220           (setq header-end (point-min)
221                 body-start (point-min)))
222         (mime-buffer-entity-set-header-start-internal entity (point-min))
223         (mime-buffer-entity-set-header-end-internal entity header-end)
224         (mime-buffer-entity-set-body-start-internal entity body-start)
225         (mime-buffer-entity-set-body-end-internal entity (point-max))
226         (save-restriction
227           (narrow-to-region (mime-buffer-entity-header-start-internal entity)
228                             (mime-buffer-entity-header-end-internal entity))
229           (mime-entity-set-content-type-internal
230            entity
231            (let ((str (std11-fetch-field "Content-Type")))
232              (if str
233                  (mime-parse-Content-Type str)
234                ))))))
235     entity))
236
237 (luna-define-method mime-insert-header ((entity mime-elmo-entity)
238                                         &optional invisible-fields
239                                         visible-fields)
240   (mmelmo-insert-sorted-header-from-buffer
241    (mime-buffer-entity-buffer-internal entity)
242    (mime-buffer-entity-header-start-internal entity)
243    (mime-buffer-entity-header-end-internal entity)
244    invisible-fields visible-fields mmelmo-sort-field-list))
245
246 (luna-define-method mime-insert-text-content :around ((entity
247                                                        mime-elmo-entity))
248   (luna-call-next-method)
249   (run-hooks 'mmelmo-entity-content-inserted-hook))
250
251 (luna-define-method mime-entity-body ((entity mime-elmo-entity))
252   (with-current-buffer (mime-buffer-entity-buffer-internal entity)
253     (buffer-substring (mime-buffer-entity-body-start-internal entity)
254                       (mime-buffer-entity-body-end-internal entity))))
255
256 ;;(luna-define-method mime-entity-content ((entity mime-elmo-entity))
257 ;;  (mime-decode-string
258 ;;   (with-current-buffer (mime-buffer-entity-buffer-internal entity)
259 ;;     (buffer-substring (mime-buffer-entity-body-start-internal entity)
260 ;;                     (mime-buffer-entity-body-end-internal entity)))
261 ;;   (mime-entity-encoding entity)))
262
263 ;;; mmelmo.el ends here