1 ;;; elmo-filter.el --- Filtered Folder Interface for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
7 ;; Keywords: mail, net news
9 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
33 (require 'elmo-signal
)
36 (defvar elmo-filter-number-filename
"number-list"
37 "File name for message number database.")
39 ;;; ELMO filter folder
41 (luna-define-class elmo-filter-folder
(elmo-folder)
42 (condition target require-msgdb number-list flag-count
))
43 (luna-define-internal-accessors 'elmo-filter-folder
))
45 (luna-define-method elmo-folder-initialize
((folder elmo-filter-folder
)
48 (setq pair
(elmo-parse-search-condition name
))
49 (elmo-filter-folder-set-condition-internal folder
(car pair
))
50 (if (string-match "^ */\\(.*\\)$" (cdr pair
))
51 (elmo-filter-folder-set-target-internal
53 (elmo-get-folder (elmo-match-string 1 (cdr pair
))))
54 (error "Folder syntax error `%s'" (elmo-folder-name-internal folder
)))
55 (elmo-filter-folder-set-require-msgdb-internal
57 (elmo-folder-search-requires-msgdb-p
58 (elmo-filter-folder-target-internal folder
)
59 (elmo-filter-folder-condition-internal folder
)))
60 (elmo-filter-folder-set-number-list-internal folder
'not-loaded
)
61 (elmo-filter-connect-signals
63 (elmo-filter-folder-target-internal folder
))
66 (defun elmo-filter-connect-signals (folder target
)
68 target
'flag-changing folder
69 (elmo-define-signal-handler (folder target number old-flags new-flags
)
70 (elmo-filter-add-flag-count folder old-flags -
1)
71 (elmo-filter-add-flag-count folder new-flags
)
72 (elmo-emit-signal 'flag-changing folder number old-flags new-flags
))
73 (elmo-define-signal-filter (folder target number
)
74 (memq number
(elmo-folder-list-messages folder nil t
))))
76 target
'flag-changed folder
77 (elmo-define-signal-handler (folder target numbers
)
78 (let ((filterd (elmo-list-filter
79 (elmo-folder-list-messages folder nil t
)
82 (elmo-emit-signal 'flag-changed folder filterd
)))))
84 target
'status-changed folder
85 (elmo-define-signal-handler (folder target numbers
)
86 (let ((filterd (elmo-list-filter
87 (elmo-folder-list-messages folder nil t
)
90 (elmo-emit-signal 'status-changed folder filterd
)))))
92 target
'update-overview folder
93 (elmo-define-signal-handler (folder target number
)
94 (elmo-emit-signal 'update-overview folder number
))
95 (elmo-define-signal-filter (folder target number
)
96 (memq number
(elmo-folder-list-messages folder nil t
)))))
98 (defun elmo-filter-number-list-load (dir)
100 (expand-file-name elmo-filter-number-filename dir
)))
102 (defun elmo-filter-number-list-save (dir number-list
)
104 (expand-file-name elmo-filter-number-filename dir
)
107 (defun elmo-filter-folder-number-list-loaded-p (folder)
108 (listp (elmo-filter-folder-number-list-internal folder
)))
110 (defun elmo-filter-folder-number-list (folder)
111 (let ((numbers (elmo-filter-folder-number-list-internal folder
)))
114 (elmo-filter-folder-set-number-list-internal
116 (elmo-filter-number-list-load (elmo-folder-msgdb-path folder
))))))
118 (defsubst elmo-filter-folder-countup-message-flags
(folder numbers
120 (let ((flag-count (elmo-filter-folder-flag-count-internal folder
))
123 (dolist (number numbers
)
124 (dolist (flag (elmo-message-flags folder number
))
125 (if (setq elem
(assq flag flag-count
))
126 (setcdr elem
(+ (cdr elem
) delta
))
127 (setq flag-count
(cons (cons flag delta
) flag-count
)))))
128 (elmo-filter-folder-set-flag-count-internal folder flag-count
)))
130 (defun elmo-filter-add-flag-count (folder flags
&optional delta
)
131 (let ((flag-count (elmo-filter-folder-flag-count-internal folder
))
135 (if (setq elem
(assq flag flag-count
))
136 (setcdr elem
(+ (cdr elem
) delta
))
137 (setq flag-count
(cons (cons flag delta
) flag-count
))))
138 (elmo-filter-folder-set-flag-count-internal folder flag-count
)))
140 (defun elmo-filter-folder-flag-count (folder)
141 (or (elmo-filter-folder-flag-count-internal folder
)
142 (elmo-filter-folder-countup-message-flags
144 (elmo-folder-list-messages folder t t
))))
146 (defun elmo-filter-folder-copy-flag-count (flag-counts)
147 (mapcar (lambda (pair) (cons (car pair
) (cdr pair
))) flag-counts
))
149 (luna-define-method elmo-folder-open
:after
((folder elmo-filter-folder
)
150 &optional load-msgdb
)
152 (elmo-filter-folder-number-list folder
)
153 (elmo-filter-folder-flag-count folder
)
154 (elmo-folder-msgdb (elmo-filter-folder-target-internal folder
))))
156 (luna-define-method elmo-folder-open-internal
((folder elmo-filter-folder
))
157 (elmo-folder-open-internal (elmo-filter-folder-target-internal folder
)))
159 (luna-define-method elmo-folder-check
((folder elmo-filter-folder
))
160 (when (elmo-filter-folder-require-msgdb-internal folder
)
161 (elmo-folder-synchronize (elmo-filter-folder-target-internal folder
))))
163 (luna-define-method elmo-folder-close-internal
((folder elmo-filter-folder
))
164 (elmo-folder-close-internal (elmo-filter-folder-target-internal folder
)))
166 (luna-define-method elmo-folder-close
((folder elmo-filter-folder
))
167 (elmo-generic-folder-close folder
)
168 (elmo-filter-folder-set-number-list-internal folder
'not-loaded
)
169 (elmo-filter-folder-set-flag-count-internal folder nil
)
170 (elmo-folder-close (elmo-filter-folder-target-internal folder
)))
172 (luna-define-method elmo-folder-commit
((folder elmo-filter-folder
))
173 (elmo-folder-commit (elmo-filter-folder-target-internal folder
))
174 (when (elmo-folder-persistent-p folder
)
175 (elmo-folder-set-info-max-by-numdb
177 (elmo-folder-list-messages folder nil
'in-msgdb
))
178 (elmo-msgdb-killed-list-save
179 (elmo-folder-msgdb-path folder
)
180 (elmo-folder-killed-list-internal folder
))
181 (when (elmo-filter-folder-number-list-loaded-p folder
)
182 (elmo-filter-number-list-save
183 (elmo-folder-msgdb-path folder
)
184 (elmo-filter-folder-number-list folder
)))))
186 (luna-define-method elmo-folder-expand-msgdb-path
((folder
189 (elmo-replace-string-as-filename (elmo-folder-name-internal folder
))
190 (expand-file-name "filter" elmo-msgdb-directory
)))
192 (luna-define-method elmo-folder-search-requires-msgdb-p
((folder
195 (elmo-folder-search-requires-msgdb-p
196 (elmo-filter-folder-target-internal folder
) condition
))
198 (luna-define-method elmo-folder-newsgroups
((folder elmo-filter-folder
))
199 (elmo-folder-newsgroups (elmo-filter-folder-target-internal folder
)))
201 (luna-define-method elmo-find-fetch-strategy
((folder elmo-filter-folder
)
206 (elmo-find-fetch-strategy
207 (elmo-filter-folder-target-internal folder
)
208 number ignore-cache require-entireness
))
210 (luna-define-method elmo-folder-get-primitive-list
((folder
212 (elmo-folder-get-primitive-list (elmo-filter-folder-target-internal folder
)))
214 (luna-define-method elmo-folder-contains-type
((folder elmo-filter-folder
)
216 (elmo-folder-contains-type
217 (elmo-filter-folder-target-internal folder
)
220 (luna-define-method elmo-folder-append-buffer
((folder elmo-filter-folder
)
221 &optional flag number
)
222 (elmo-folder-append-buffer
223 (elmo-filter-folder-target-internal folder
)
226 (defun elmo-folder-append-messages-filter-* (dst-folder
230 (elmo-folder-append-messages dst-folder
231 (elmo-filter-folder-target-internal src-folder
)
235 (defun elmo-folder-append-messages-*-filter
(dst-folder
239 (elmo-folder-append-messages (elmo-filter-folder-target-internal dst-folder
)
244 (luna-define-method elmo-message-fetch-bodystructure
((folder
247 (elmo-message-fetch-bodystructure
248 (elmo-filter-folder-target-internal folder
)
251 (luna-define-method elmo-message-fetch
((folder elmo-filter-folder
)
253 &optional unseen section
)
254 (elmo-message-fetch (elmo-filter-folder-target-internal folder
)
255 number strategy unseen section
))
257 (luna-define-method elmo-folder-delete-messages
((folder elmo-filter-folder
)
259 (let ((flag-count (elmo-filter-folder-copy-flag-count
260 (elmo-filter-folder-flag-count-internal folder
)))
261 (messages (copy-sequence
262 (elmo-filter-folder-number-list folder
)))
264 (elmo-folder-detach-messages folder numbers
)
265 (unless (setq success
266 (elmo-folder-delete-messages
267 (elmo-filter-folder-target-internal folder
) numbers
))
268 (elmo-filter-folder-set-flag-count-internal folder flag-count
)
269 (elmo-filter-folder-set-number-list-internal folder messages
))
272 (luna-define-method elmo-folder-list-messages
((folder elmo-filter-folder
)
273 &optional visible-only in-msgdb
)
274 (let ((list (if in-msgdb
276 (elmo-folder-list-messages-internal folder visible-only
)))
277 (killed-list (elmo-folder-killed-list-internal folder
)))
280 (setq list
(elmo-filter-folder-number-list folder
)))
282 (elmo-living-messages list killed-list
)
283 (if (and in-msgdb killed-list list
)
284 (elmo-uniq-sorted-list
285 (sort (nconc (elmo-number-set-to-number-list killed-list
) list
) #'<)
289 (luna-define-method elmo-folder-list-messages-internal
290 ((folder elmo-filter-folder
) &optional nohide
)
291 (let ((target (elmo-filter-folder-target-internal folder
)))
292 (if (or (elmo-folder-plugged-p target
)
293 (not (elmo-folder-persistent-p folder
)))
294 ;; search target folder
297 (elmo-filter-folder-condition-internal folder
))
301 (luna-define-method elmo-folder-list-flagged
((folder elmo-filter-folder
)
305 (elmo-folder-list-messages folder nil t
)
306 (elmo-folder-list-flagged
307 (elmo-filter-folder-target-internal folder
) flag in-msgdb
)))
309 (luna-define-method elmo-folder-list-subfolders
((folder elmo-filter-folder
)
311 (let* ((target (elmo-filter-folder-target-internal folder
))
312 (prefix (and (string-match
315 (elmo-folder-name-internal
318 (elmo-folder-name-internal folder
))
319 (match-string 1 (elmo-folder-name-internal
321 (elmo-mapcar-list-of-list
322 (lambda (x) (concat prefix x
))
323 (elmo-folder-list-subfolders target one-level
))))
325 (luna-define-method elmo-folder-diff
:around
((folder elmo-filter-folder
))
326 (let ((condition (elmo-filter-folder-condition-internal folder
))
328 (if (vectorp condition
)
330 ((and (string= (elmo-filter-key condition
) "flag")
331 (or (string= (elmo-filter-value condition
) "any")
332 (string= (elmo-filter-value condition
) "digest")
333 (string= (elmo-filter-value condition
) "unread")))
334 (setq diff
(elmo-folder-diff (elmo-filter-folder-target-internal
336 (if (consp (cdr diff
))
338 (list (car diff
) (nth 1 diff
) (nth 1 diff
))
339 (cons (car diff
) (car diff
))))
340 ((string= "last" (elmo-filter-key condition
))
341 (luna-call-next-method))
343 (cons nil
(cdr (elmo-folder-diff (elmo-filter-folder-target-internal
345 (luna-call-next-method))))
347 (luna-define-method elmo-folder-status
((folder elmo-filter-folder
))
349 (elmo-filter-folder-target-internal folder
)))
351 (luna-define-method elmo-folder-exists-p
((folder elmo-filter-folder
))
352 (elmo-folder-exists-p (elmo-filter-folder-target-internal folder
)))
354 (luna-define-method elmo-folder-creatable-p
((folder elmo-filter-folder
))
355 (elmo-folder-creatable-p (elmo-filter-folder-target-internal folder
)))
357 (luna-define-method elmo-folder-writable-p
((folder elmo-filter-folder
))
358 (elmo-folder-writable-p (elmo-filter-folder-target-internal folder
)))
360 (luna-define-method elmo-folder-create
((folder elmo-filter-folder
))
361 (elmo-folder-create (elmo-filter-folder-target-internal folder
)))
363 (luna-define-method elmo-folder-search
((folder elmo-filter-folder
)
364 condition
&optional numbers
)
365 ;; search from messages in this folder
366 (let ((result (elmo-folder-search
367 (elmo-filter-folder-target-internal folder
)
369 (elmo-folder-list-messages folder
))))
371 (elmo-list-filter numbers result
)
374 (luna-define-method elmo-message-use-cache-p
((folder elmo-filter-folder
)
376 (elmo-message-use-cache-p (elmo-filter-folder-target-internal folder
)
379 (luna-define-method elmo-folder-message-file-p
((folder elmo-filter-folder
))
380 (elmo-folder-message-file-p (elmo-filter-folder-target-internal folder
)))
382 (luna-define-method elmo-folder-local-p
((folder elmo-filter-folder
))
383 (elmo-folder-local-p (elmo-filter-folder-target-internal folder
)))
385 (luna-define-method elmo-folder-plugged-p
((folder elmo-filter-folder
))
386 (elmo-folder-plugged-p (elmo-filter-folder-target-internal folder
)))
388 (luna-define-method elmo-folder-set-plugged
((folder elmo-filter-folder
)
389 plugged
&optional add
)
390 (elmo-folder-set-plugged (elmo-filter-folder-target-internal folder
)
393 (luna-define-method elmo-message-file-name
((folder elmo-filter-folder
)
395 (elmo-message-file-name (elmo-filter-folder-target-internal folder
)
398 (luna-define-method elmo-message-flag-available-p
((folder
399 elmo-filter-folder
) number
401 (elmo-message-flag-available-p
402 (elmo-filter-folder-target-internal folder
)
405 (luna-define-method elmo-message-flags
((folder elmo-filter-folder
) number
)
406 (elmo-message-flags (elmo-filter-folder-target-internal folder
)
409 (luna-define-method elmo-message-set-cached
((folder elmo-filter-folder
)
411 (elmo-message-set-cached
412 (elmo-filter-folder-target-internal folder
) number cached
))
414 (luna-define-method elmo-message-number
((folder elmo-filter-folder
)
416 (elmo-message-number (elmo-filter-folder-target-internal folder
)
419 (luna-define-method elmo-message-entity
((folder elmo-filter-folder
) key
)
420 (elmo-message-entity (elmo-filter-folder-target-internal folder
) key
))
422 (luna-define-method elmo-message-entity-parent
((folder elmo-filter-folder
)
424 (let ((parent (elmo-message-entity-parent
425 (elmo-filter-folder-target-internal folder
)
427 (when (memq (elmo-message-entity-number parent
)
428 (elmo-filter-folder-number-list folder
))
431 (luna-define-method elmo-folder-flag-table
((folder elmo-filter-folder
)
433 (elmo-folder-flag-table (elmo-filter-folder-target-internal folder
)
436 (luna-define-method elmo-folder-close-flag-table
((folder elmo-filter-folder
))
437 (elmo-folder-close-flag-table (elmo-filter-folder-target-internal folder
)))
439 (luna-define-method elmo-folder-count-flags
((folder elmo-filter-folder
))
440 (elmo-filter-folder-flag-count folder
))
442 (luna-define-method elmo-folder-set-flag
((folder elmo-filter-folder
)
446 (elmo-folder-set-flag (elmo-filter-folder-target-internal folder
)
447 numbers flag is-local
))
449 (luna-define-method elmo-folder-unset-flag
((folder elmo-filter-folder
)
453 (elmo-folder-unset-flag (elmo-filter-folder-target-internal folder
)
454 numbers flag is-local
))
456 (luna-define-method elmo-message-folder
((folder elmo-filter-folder
)
458 (elmo-message-folder (elmo-filter-folder-target-internal folder
) number
))
460 (luna-define-method elmo-message-field
((folder elmo-filter-folder
)
461 number field
&optional type
)
463 (elmo-filter-folder-target-internal folder
) number field type
))
465 (luna-define-method elmo-message-set-field
((folder elmo-filter-folder
)
467 (elmo-message-set-field
468 (elmo-filter-folder-target-internal folder
) number field value
))
470 (luna-define-method elmo-folder-clear
((folder elmo-filter-folder
)
471 &optional keep-killed
)
473 (elmo-folder-set-killed-list-internal folder nil
))
474 (elmo-filter-folder-set-number-list-internal folder nil
)
475 (elmo-filter-folder-set-flag-count-internal folder nil
))
477 (luna-define-method elmo-folder-synchronize
((folder elmo-filter-folder
)
483 (let ((killed-list (elmo-folder-killed-list-internal folder
))
486 (when (elmo-filter-folder-require-msgdb-internal folder
)
487 (elmo-folder-synchronize (elmo-filter-folder-target-internal folder
)
492 (setq numbers
(elmo-folder-list-messages folder
(not disable-killed
)))
494 (not (elmo-filter-folder-require-msgdb-internal folder
)))
495 (elmo-folder-synchronize (elmo-filter-folder-target-internal folder
)
500 (elmo-list-filter mask numbers
)
502 (when (and disable-killed ignore-msgdb
)
503 (elmo-folder-set-killed-list-internal folder nil
))
504 (elmo-filter-folder-set-number-list-internal folder numbers
)
505 (elmo-filter-folder-set-flag-count-internal folder nil
)
508 (luna-define-method elmo-folder-detach-messages
((folder elmo-filter-folder
)
510 (elmo-filter-folder-countup-message-flags folder numbers -
1)
511 (elmo-filter-folder-set-number-list-internal
513 (elmo-list-delete numbers
(elmo-filter-folder-number-list folder
) #'delq
))
516 (luna-define-method elmo-folder-length
((folder elmo-filter-folder
))
517 (and (elmo-filter-folder-number-list-loaded-p folder
)
518 (length (elmo-filter-folder-number-list-internal folder
))))
521 (product-provide (provide 'elmo-filter
) (require 'elmo-version
))
523 ;;; elmo-filter.el ends here