Debian Codegraph @

mew-cache.el

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