~ubuntu-branches/ubuntu/lucid/mew-beta/lucid

1 by Tatsuya Kinoshita
Import upstream version 4.0.65
1
;;; mew-cache.el --- Cache management for Mew
2
3
;; Author:  Kazu Yamamoto <Kazu@Mew.org>
4
;; Created: Mar 23, 1997
5
6
;;; Code:
7
8
(require 'mew)
9
10
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11
;;;
12
;;; Cache info
13
;;;
14
15
(defvar mew-cinfo-list '("fld" "msg" "time" "size" "decode-broken"))
16
17
(mew-blinfo-defun 'mew-cinfo mew-cinfo-list)
18
19
(defun mew-cinfo-set (fld msg time size decode-broken)
20
  (mew-cinfo-set-fld fld)
21
  (mew-cinfo-set-msg msg)
22
  (mew-cinfo-set-time time)
23
  (mew-cinfo-set-size size)
24
  (mew-cinfo-set-decode-broken decode-broken))
25
26
(defun mew-cinfo-equal (fld msg time size)
27
  (and (string= (mew-cinfo-get-fld) fld)
28
       (string= (mew-cinfo-get-msg) msg)
29
       (equal (mew-cinfo-get-time) time)
30
       (eq (mew-cinfo-get-size) size)))
31
32
(defun mew-cache-dinfo-get-decode-broken (buf)
33
  (when buf
34
    (save-excursion
35
      (set-buffer buf)
36
      (mew-cinfo-get-decode-broken))))
37
38
(defvar mew-xinfo-list
39
  '("decode-err" "warning" "info" "action" "multi-form" "icon-spec"
40
    "pri-result" "not-decrypted" "text-body"))
41
42
(mew-blinfo-defun 'mew-xinfo mew-xinfo-list)
43
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
44
(defun mew-xinfo-copy (buf)
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
45
  (set 'mew-xinfo (save-excursion (set-buffer buf) (symbol-value 'mew-xinfo))))
46
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
47
(defun mew-xinfo-clear ()
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
48
  (set 'mew-xinfo nil))
49
50
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51
;;
52
;;  Prepare new message --- caching
53
;;
54
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
55
(defun mew-cache-decode-syntax (buf)
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
56
  (save-excursion (set-buffer buf) mew-decode-syntax))
57
58
(defvar mew-cache nil
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
59
  "A list of decoded messages cache.
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
60
The (new ... old) order of ((\"+folder\" . \"message\") . cache-buffer)")
61
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
62
(defun mew-cache-key (fld msg)
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
63
  (cons fld msg))
64
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
65
(defun mew-cache-buffer-get (entry)
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
66
  (cdr entry))
67
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
68
(defun mew-cache-entry-make (fld msg buf)
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
69
  (cons (mew-cache-key fld msg) buf))
70
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
71
(defun mew-cache-get (fld msg)
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
72
  (assoc (mew-cache-key fld msg) mew-cache))
73
74
(defun mew-cache-hit (fld msg &optional must-hit)
75
  "Return the buffer associated with FLD and MSG.
76
If no cache entry is found, nil is returned.
77
If a cache entry is invalid, the entry is removed and nil is returned.
78
If MUST-HIT is non-nil and no valid cache entry is found, an error occurs."
79
  (let ((entry (mew-cache-get fld msg))
80
	cache file time size ok)
81
    (if (null entry)
82
	(if must-hit
83
	    (error "Cache not found")
84
	  nil)
85
      (setq cache (mew-cache-buffer-get entry))
1.1.2 by Tatsuya Kinoshita
Import upstream version 5.0.53
86
      (setq file (mew-expand-msg fld msg))
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
87
      (unless (file-readable-p file)
88
	(error "%s does not exist" (mew-concat-folder fld msg)))
89
      (setq time (mew-file-get-time file))
90
      (setq size (mew-file-get-size file))
91
      (save-excursion
92
	(set-buffer cache)
93
	(setq ok (mew-cinfo-equal fld msg time size)))
94
      (if ok
95
	  (progn
96
	    (mew-cache-sort entry)
97
	    cache)
98
	(if must-hit
99
	    (error "Cache not found")
100
	  (mew-cache-delete2 fld msg)
101
	  nil)))))
102
103
(defun mew-cache-sort (entry)
104
  (setq mew-cache (cons entry (delq entry mew-cache))))
105
106
(defun mew-cache-add (fld msg)
107
  (let ((len (length mew-cache))
108
	buf)
109
    (if (< len mew-cache-size)
110
	(setq buf (get-buffer-create (format "%s%d" mew-buffer-cache len)))
111
      (setq buf (mew-cache-buffer-get (nth (1- len) mew-cache)))
112
      (setcdr (nthcdr (- len 2) mew-cache) nil))
113
    (setq mew-cache (cons (mew-cache-entry-make fld msg buf) mew-cache))
114
    buf))
115
116
(defun mew-cache-delete ()
117
  "Delete the most recent cache entry."
118
  (let ((buf (mew-cache-buffer-get (car mew-cache))))
119
    ;; must preserve the buffer itself because the buffer creation
120
    ;; depends on the length of mew-cache.
121
    (setq mew-cache (nconc (cdr mew-cache)
122
			   (list (mew-cache-entry-make nil nil buf))))))
123
124
(defun mew-cache-delete2 (fld msg)
125
  "Delete the specific cache entry."
126
  (let ((entry (mew-cache-get fld msg)) buf)
127
    (if (null entry)
128
	()
129
      (setq buf (mew-cache-buffer-get entry))
130
      (setq mew-cache (delq entry mew-cache))
131
      (setq mew-cache (nconc mew-cache
132
			     (list (mew-cache-entry-make nil nil buf)))))))
133
1.1.1 by Tatsuya Kinoshita
Import upstream version 4.2.52
134
(defvar mew-fields-xcc
135
  (list mew-fcc: mew-dcc: mew-bcc:
136
	mew-resent-fcc: mew-resent-bcc: mew-resent-dcc:))
137
138
(defun mew-xinfo-get-xcc (fld msg)
139
  (when (or (mew-folder-queuep fld) (mew-folder-postqp fld))
1.1.4 by Tatsuya Kinoshita
Import upstream version 5.0.53+5.1rc2
140
    (let ((info (concat (mew-expand-msg fld msg) mew-queue-info-suffix))
1.1.1 by Tatsuya Kinoshita
Import upstream version 4.2.52
141
	  (headers mew-fields-xcc)
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
142
	  data addr xinfo)
1.1.1 by Tatsuya Kinoshita
Import upstream version 4.2.52
143
      (when (and (file-readable-p info)
144
		 (setq data (aref (mew-lisp-load info) 0)))
145
	(with-temp-buffer
146
	  (insert data)
147
	  (goto-char (point-min))
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
148
	  (dolist (header headers)
1.1.1 by Tatsuya Kinoshita
Import upstream version 4.2.52
149
	    (when (setq addr (mew-header-get-value header))
150
	      (setq xinfo (cons (format "%s %s\n" header addr) xinfo)))))
151
	(nreverse xinfo)))))
152
1.2.3 by Tatsuya Kinoshita
Import upstream version 6.2.51
153
(defun mew-disable-alternative-check ()
154
  (let ((xmailer (mew-header-get-value mew-x-mailer:))
155
	(regexs mew-disable-alternative-regex-list))
156
    (if (null xmailer)
157
	t
158
      (catch 'loop
159
	(dolist (regex regexs t)
160
	  (if (string-match regex xmailer)
161
	      (throw 'loop nil)))))))
162
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
163
(defun mew-cache-message (fld msg &optional unlimit no-err)
164
  "Cache the message specified by FLD and MSG.
165
If an invalid message are cached, deletes it and caches the message again.
166
If UNLIMIT is non-nil, decodes the message to be cached without
167
the limitations. If NO-ERR is non-nil, an error is caused
168
if decode fails."
169
  (let* ((cbuf (current-buffer))
170
	 (cache (mew-cache-hit fld msg))
1.2.3 by Tatsuya Kinoshita
Import upstream version 6.2.51
171
	 (use-alternative mew-use-alternative)
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
172
	 tim-siz decode errormsg)
173
    (catch 'return
174
      (if cache
175
	  (progn
176
	    (set-buffer cache)
1.1.1 by Tatsuya Kinoshita
Import upstream version 4.2.52
177
	    ;; Decryption may fail if password is wrong. So, try
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
178
	    ;; to decode this again.
179
	    (if (or (and unlimit (mew-xinfo-get-not-decrypted))
180
		    (and unlimit (mew-xinfo-get-decode-err)))
181
		;; cache is invalid
182
		(setq decode t)))
183
	(setq cache (mew-cache-add fld msg))
184
	(setq decode t))
185
      (if (not decode) (throw 'return nil))
186
      ;;
187
      (set-buffer cache)
188
      ;; in cache buffer
189
      (mew-erase-buffer)
190
      (condition-case errmsg
191
	  (setq tim-siz	(mew-insert-message fld msg mew-cs-text-for-read nil))
192
	(error
193
	 ;; file not exist
194
	 (mew-cache-delete)
195
	 (setq errormsg (nth 1 errmsg))
196
	 (throw 'return (setq cache nil))))
197
      (mew-cinfo-set fld msg (car tim-siz) (cdr tim-siz) mew-decode-broken)
1.2.3 by Tatsuya Kinoshita
Import upstream version 6.2.51
198
      (if (and use-alternative mew-disable-alternative-regex-list)
199
	  (setq use-alternative (mew-disable-alternative-check)))
200
      (mew-dinfo-set nil t t use-alternative)
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
201
      (mew-decode-syntax-clear)
202
      (mew-xinfo-set-text-body mew-use-text-body)
203
      (condition-case nil
204
	  (if unlimit
205
	      (let ((mew-header-max-length nil)
206
		    (mew-header-max-depth nil))
207
		(mew-decode))
208
	    (mew-decode))
209
	;; Don't put error handing here. Because (mew-decode) would
210
	;; set debug-on-error to t.
211
	(quit
212
	 ;; prefetching an encrypted message
213
	 (mew-cache-delete)
1.1.4 by Tatsuya Kinoshita
Import upstream version 5.0.53+5.1rc2
214
	 ;; The following message is not friendly to users.
215
	 ;; (message "MIME decoding for %s/%s aborted" fld msg)
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
216
	 (throw 'return (setq cache nil))))
217
      (mew-ainfo-set-icon msg)
1.1.1 by Tatsuya Kinoshita
Import upstream version 4.2.52
218
      (mew-xinfo-set-info (append (mew-xinfo-get-info) (mew-xinfo-get-xcc fld msg)))
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
219
      (mew-decode-syntax-set))
220
    ;;
221
    (set-buffer cbuf)
222
    (if errormsg (if no-err (message "%s" errormsg) (error "%s" errormsg)))
1.1.1 by Tatsuya Kinoshita
Import upstream version 4.2.52
223
    cache)) ;; return value
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
224
225
(defun mew-cache-clean-up ()
226
  "A function to flush all decoded messages in cache list."
227
  (interactive)
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
228
  (dotimes (n mew-cache-size)
229
    (mew-kill-buffer (format "%s%d" mew-buffer-cache n)))
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
230
  (mew-summary-reset)
231
  (setq mew-cache nil))
232
233
(defalias 'mew-cache-flush 'mew-cache-clean-up)
234
235
(provide 'mew-cache)
236
237
;;; Copyright Notice:
238
1.2.3 by Tatsuya Kinoshita
Import upstream version 6.2.51
239
;; Copyright (C) 1997-2009 Mew developing team.
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
240
;; All rights reserved.
241
242
;; Redistribution and use in source and binary forms, with or without
243
;; modification, are permitted provided that the following conditions
244
;; are met:
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
245
;;
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
246
;; 1. Redistributions of source code must retain the above copyright
247
;;    notice, this list of conditions and the following disclaimer.
248
;; 2. Redistributions in binary form must reproduce the above copyright
249
;;    notice, this list of conditions and the following disclaimer in the
250
;;    documentation and/or other materials provided with the distribution.
251
;; 3. Neither the name of the team nor the names of its contributors
252
;;    may be used to endorse or promote products derived from this software
253
;;    without specific prior written permission.
1.1.12 by Tatsuya Kinoshita
Import upstream version 6.0.51~0.20080421
254
;;
1 by Tatsuya Kinoshita
Import upstream version 4.0.65
255
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
256
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
257
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
258
;; PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
259
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
260
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
261
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
262
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
263
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
264
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
265
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266
267
;;; mew-cache.el ends here