53d7b2fc9ffe46359fcf0c0c550cf95b3d113b72
[elisp/wanderlust.git] / elmo / modb-standard.el
1 ;;; modb-standard.el --- Standartd Implement of MODB.
2
3 ;; Copyright (C) 2003 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;;      Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
7 ;; Keywords: mail, net news
8
9 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10
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)
14 ;; any later version.
15 ;;
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.
20 ;;
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.
25 ;;
26
27 ;;; Commentary:
28 ;;
29
30 ;;; Code:
31 ;;
32 (eval-when-compile (require 'cl))
33
34 (require 'elmo-util)
35 (require 'modb)
36
37 (defcustom modb-standard-divide-number 500
38   "*Standard modb divide entity number."
39   :type '(choice (const :tag "Not divide" nil)
40                  number)
41   :group 'elmo)
42
43 (defcustom modb-standard-economize-entity-size nil
44   "*Economize message entity size.
45 When non-nil, redundunt message-id string are not saved."
46   :type 'boolean
47   :group 'elmo)
48
49 (defvar modb-standard-entity-filename "entity"
50   "Message entity database.")
51
52 (defvar modb-standard-flag-filename "flag"
53   "Message number <=> Flag status database.")
54
55 (defvar modb-standard-msgid-filename "msgid"
56   "Message number <=> Message-Id database.")
57
58 (eval-and-compile
59   (luna-define-class modb-standard (modb-generic)
60                      (number-list       ; sorted list of message numbers.
61                       entity-map        ; number, msg-id -> entity mapping.
62                       flag-map          ; number -> flag-list mapping
63                       flag-count        ; list of (FLAG . COUNT)
64                       ))
65   (luna-define-internal-accessors 'modb-standard))
66
67 ;; for internal use only
68 (defsubst modb-standard-key (number)
69   (concat "#" (number-to-string number)))
70
71 (defsubst modb-standard-entity-id (entity)
72   (if (eq 'autoload (car-safe entity))
73       (cddr entity)
74     (elmo-msgdb-message-entity-field
75      (elmo-message-entity-handler entity)
76      entity 'message-id)))
77
78 (defsubst modb-standard-entity-map (modb)
79   (or (modb-standard-entity-map-internal modb)
80       (modb-standard-set-entity-map-internal
81        modb
82        (elmo-make-hash (elmo-msgdb-length modb)))))
83
84 (defsubst modb-standard-flag-map (modb)
85   (or (modb-standard-flag-map-internal modb)
86       (modb-standard-set-flag-map-internal
87        modb
88        (elmo-make-hash (elmo-msgdb-length modb)))))
89
90 (defsubst modb-standard-set-message-modified (modb number)
91   (if modb-standard-divide-number
92       (let ((section (/ number modb-standard-divide-number))
93             (modified (modb-generic-message-modified-internal modb)))
94         (unless (memq section modified)
95           (modb-generic-set-message-modified-internal
96            modb (cons section modified))))
97     (modb-generic-set-message-modified-internal modb t)))
98
99 (defsubst modb-standard-set-flag-modified (modb number)
100   (modb-generic-set-flag-modified-internal modb t))
101
102 (defsubst modb-standard-message-flags (modb number)
103   (cdr (elmo-get-hash-val (modb-standard-key number)
104                           (modb-standard-flag-map-internal modb))))
105
106 (defsubst modb-standard-match-flags (check-flags flags)
107   (catch 'done
108     (while check-flags
109       (when (memq (car check-flags) flags)
110         (throw 'done t))
111       (setq check-flags (cdr check-flags)))))
112
113 (defsubst modb-standard-countup-flags (modb flags &optional delta)
114   (let ((flag-count (modb-standard-flag-count-internal modb))
115         (delta (or delta 1))
116         elem)
117     (dolist (flag flags)
118       (if (setq elem (assq flag flag-count))
119           (setcdr elem (+ (cdr elem) delta))
120         (setq flag-count (cons (cons flag delta) flag-count))))
121     (modb-standard-set-flag-count-internal modb flag-count)))
122
123 ;; save and load functions
124 (defun modb-standard-load-msgid (modb path)
125   (let* ((alist (elmo-object-load
126                  (expand-file-name modb-standard-msgid-filename path)))
127          (table (or (modb-standard-entity-map-internal modb)
128                     (elmo-make-hash (length alist))))
129          numbers info)
130     (dolist (pair alist)
131       (setq info (cons 'autoload pair))
132       (elmo-set-hash-val (modb-standard-key (car pair)) info table)
133       (elmo-set-hash-val (cdr pair) info table)
134       (setq numbers (cons (car pair) numbers)))
135     (modb-standard-set-number-list-internal modb (nreverse numbers))
136     (modb-standard-set-entity-map-internal modb table)))
137
138 (defun modb-standard-save-msgid (modb path)
139   (let ((table (modb-standard-entity-map-internal modb))
140         entity alist)
141     (dolist (number (modb-standard-number-list-internal modb))
142       (setq entity (elmo-get-hash-val (modb-standard-key number) table))
143       (setq alist (cons (cons number (modb-standard-entity-id entity))
144                         alist)))
145     (elmo-object-save
146      (expand-file-name modb-standard-msgid-filename path)
147      (nreverse alist))))
148
149 (defun modb-standard-load-flag (modb path)
150   (let ((table (or (modb-standard-flag-map-internal modb)
151                    (elmo-make-hash (elmo-msgdb-length modb)))))
152     (dolist (info (elmo-object-load
153                    (expand-file-name modb-standard-flag-filename path)))
154       (modb-standard-countup-flags modb (cdr info))
155       (elmo-set-hash-val (modb-standard-key (car info)) info table))
156     (modb-standard-set-flag-map-internal modb table)))
157
158 (defun modb-standard-save-flag (modb path)
159   (let (table flist info)
160     (when (setq table (modb-standard-flag-map-internal modb))
161       (mapatoms
162        (lambda (atom)
163          (setq info (symbol-value atom))
164          (when (cdr info)
165            (setq flist (cons info flist))))
166        table))
167     (elmo-object-save
168      (expand-file-name modb-standard-flag-filename path)
169      flist)))
170
171 (defsubst modb-standard-entity-filename (section)
172   (if section
173       (concat modb-standard-entity-filename
174               "-"
175               (number-to-string section))
176     modb-standard-entity-filename))
177
178 (defsubst modb-standard-loaded-message-id (msgdb number)
179   "Get message-id for autoloaded entity."
180   (let ((ret (elmo-get-hash-val
181               (modb-standard-key number)
182               (modb-standard-entity-map-internal msgdb))))
183     (cond
184      ((and ret (eq (car-safe ret) 'autoload))
185       (cdr (cdr ret))) ; message-id.
186      ((and ret (stringp (car-safe ret)))
187       ;; Already loaded.
188       (car ret))
189      ((null ret)
190       ;; Garbage entity.
191       (elmo-clear-hash-val (modb-standard-key number)
192                            (modb-standard-entity-map-internal msgdb)))
193      (t (error "Internal error: invalid msgdb status")))))
194
195 (defun modb-standard-load-entity (modb path &optional section)
196   (let ((table (or (modb-standard-entity-map-internal modb)
197                    (elmo-make-hash (elmo-msgdb-length modb))))
198         (inhibit-quit t)
199         number msgid)
200     (dolist (entity (elmo-object-load
201                      (expand-file-name
202                       (modb-standard-entity-filename section)
203                       path)))
204       (setq number (elmo-msgdb-message-entity-number
205                     (elmo-message-entity-handler entity)
206                     entity)
207             msgid (modb-standard-loaded-message-id modb number))
208       (when msgid
209         (setcar entity msgid)
210         (elmo-set-hash-val msgid entity table)
211         (elmo-set-hash-val (modb-standard-key number) entity table)))
212     (modb-standard-set-entity-map-internal modb table)))
213
214 (defsubst modb-standard-save-entity-1 (modb path &optional section)
215   (let ((table (modb-standard-entity-map-internal modb))
216         (filename (expand-file-name
217                    (modb-standard-entity-filename section) path))
218         entity entities)
219     (dolist (number (modb-standard-number-list-internal modb))
220       (when (and (or (null section)
221                      (= section (/ number modb-standard-divide-number)))
222                  (setq entity (elmo-msgdb-message-entity modb number)))
223         (when modb-standard-economize-entity-size
224           (when (stringp (car entity)) (setcar entity t)))
225         (setq entities (cons entity entities))))
226     (if entities
227         (elmo-object-save filename entities)
228       (ignore-errors (delete-file filename)))))
229
230 (defun modb-standard-save-entity (modb path)
231   (let ((sections (modb-generic-message-modified-internal modb)))
232     (cond ((listp sections)
233            (dolist (section sections)
234              (modb-standard-save-entity-1 modb path section)))
235           (sections
236            (modb-standard-save-entity-1 modb path)))))
237
238 ;;; Implement
239 ;;
240 (luna-define-method elmo-msgdb-load ((msgdb modb-standard))
241   (let ((inhibit-quit t)
242         (path (elmo-msgdb-location msgdb)))
243     (when (file-exists-p (expand-file-name modb-standard-flag-filename path))
244       (modb-standard-load-msgid msgdb path)
245       (modb-standard-load-flag msgdb path)
246       (unless modb-standard-divide-number
247         (modb-standard-load-entity msgdb path))
248       t)))
249
250 (luna-define-method elmo-msgdb-save ((msgdb modb-standard))
251   (let ((path (elmo-msgdb-location msgdb)))
252     (when (elmo-msgdb-message-modified-p msgdb)
253       (modb-standard-save-msgid  msgdb path)
254       (modb-standard-save-entity msgdb path)
255       (modb-generic-set-message-modified-internal msgdb nil))
256     (when (elmo-msgdb-flag-modified-p msgdb)
257       (modb-standard-save-flag msgdb path)
258       (modb-generic-set-flag-modified-internal msgdb nil))))
259
260 (luna-define-method elmo-msgdb-append :around ((msgdb modb-standard)
261                                                msgdb-append)
262   (when (> (elmo-msgdb-length msgdb-append) 0)
263     (if (eq (luna-class-name msgdb-append) 'modb-standard)
264         (let ((numbers (modb-standard-number-list-internal msgdb-append))
265               duplicates)
266           ;; number-list
267           (modb-standard-set-number-list-internal
268            msgdb
269            (nconc (modb-standard-number-list-internal msgdb)
270                   numbers))
271           ;; entity-map
272           (let ((table (modb-standard-entity-map msgdb))
273                 entity msg-id)
274             (dolist (number numbers)
275               (setq entity (elmo-msgdb-message-entity msgdb-append number)
276                     msg-id (modb-standard-entity-id entity))
277               (if (elmo-get-hash-val msg-id table)
278                   (setq duplicates (cons number duplicates))
279                 (elmo-set-hash-val msg-id entity table))
280               (elmo-set-hash-val (modb-standard-key number)
281                                  entity
282                                  table)))
283           ;; flag-map
284           (let ((table (modb-standard-flag-map msgdb)))
285             (mapatoms
286              (lambda (atom)
287                (elmo-set-hash-val (symbol-name atom)
288                                   (symbol-value atom)
289                                   table))
290              (modb-standard-flag-map msgdb-append)))
291           ;; flag-count
292           (dolist (pair (modb-standard-flag-count-internal msgdb-append))
293             (modb-standard-countup-flags msgdb (list (car pair)) (cdr pair)))
294           ;; modification flags
295           (dolist (number (modb-standard-number-list-internal msgdb-append))
296             (modb-standard-set-message-modified msgdb number)
297             (modb-standard-set-flag-modified msgdb number))
298           duplicates)
299       (luna-call-next-method))))
300
301 (luna-define-method elmo-msgdb-clear :after ((msgdb modb-standard))
302   (modb-standard-set-number-list-internal msgdb nil)
303   (modb-standard-set-entity-map-internal msgdb nil)
304   (modb-standard-set-flag-map-internal msgdb nil)
305   (modb-standard-set-flag-count-internal msgdb nil))
306
307 (luna-define-method elmo-msgdb-length ((msgdb modb-standard))
308   (length (modb-standard-number-list-internal msgdb)))
309
310 (luna-define-method elmo-msgdb-flag-available-p ((msgdb modb-standard) flag)
311   t)
312
313 (luna-define-method elmo-msgdb-flags ((msgdb modb-standard) number)
314   (modb-standard-message-flags msgdb number))
315
316 (luna-define-method elmo-msgdb-set-flag ((msgdb modb-standard)
317                                          number flag)
318   (case flag
319     (read
320      (elmo-msgdb-unset-flag msgdb number 'unread))
321     (uncached
322      (elmo-msgdb-unset-flag msgdb number 'cached))
323     (t
324      (let ((cur-flags (modb-standard-message-flags msgdb number))
325            new-flags diff)
326        (unless (memq flag cur-flags)
327          (setq new-flags (cons flag cur-flags))
328          (setq diff (elmo-list-diff new-flags cur-flags))
329          (modb-standard-countup-flags msgdb (car diff))
330          (modb-standard-countup-flags msgdb (cadr diff) -1)
331          (elmo-set-hash-val (modb-standard-key number)
332                             (cons number new-flags)
333                             (modb-standard-flag-map msgdb))
334          (modb-standard-set-flag-modified msgdb number))))))
335
336 (luna-define-method elmo-msgdb-unset-flag ((msgdb modb-standard)
337                                            number flag)
338   (case flag
339     (read
340      (elmo-msgdb-set-flag msgdb number 'unread))
341     (uncached
342      (elmo-msgdb-set-flag msgdb number 'cached))
343     (all
344      (modb-standard-countup-flags msgdb
345                                   (modb-standard-message-flags msgdb number)
346                                   -1)
347      (elmo-clear-hash-val (modb-standard-key number)
348                           (modb-standard-flag-map msgdb)))
349     (t
350      (let ((cur-flags (modb-standard-message-flags msgdb number))
351            new-flags diff)
352        (when (memq flag cur-flags)
353          (setq new-flags (delq flag (copy-sequence cur-flags)))
354          (setq diff (elmo-list-diff new-flags cur-flags))
355          (modb-standard-countup-flags msgdb (car diff))
356          (modb-standard-countup-flags msgdb (cadr diff) -1)
357          (elmo-set-hash-val (modb-standard-key number)
358                             (cons number new-flags)
359                             (modb-standard-flag-map msgdb))
360          (modb-standard-set-flag-modified msgdb number))
361        (when (eq flag 'unread)
362          (elmo-msgdb-unset-flag msgdb number 'new))))))
363
364 (luna-define-method elmo-msgdb-flag-count ((msgdb modb-standard))
365   (modb-standard-flag-count-internal msgdb))
366
367 (luna-define-method elmo-msgdb-list-messages ((msgdb modb-standard))
368   (copy-sequence
369    (modb-standard-number-list-internal msgdb)))
370
371 (luna-define-method elmo-msgdb-list-flagged ((msgdb modb-standard) flag)
372   (let (entry matched)
373     (case flag
374       (read
375        (dolist (number (modb-standard-number-list-internal msgdb))
376          (unless (memq 'unread (modb-standard-message-flags msgdb number))
377            (setq matched (cons number matched)))))
378       (uncached
379        (dolist (number (modb-standard-number-list-internal msgdb))
380          (unless (memq 'cached (modb-standard-message-flags msgdb number))
381            (setq matched (cons number matched)))))
382       (any
383        (mapatoms
384         (lambda (atom)
385           (setq entry (symbol-value atom))
386           (unless (and (eq (length (cdr entry)) 1)
387                        (eq (car (cdr entry)) 'cached))
388             ;; If there is a flag other than cached, then the message
389             ;; matches to `any'.
390             (setq matched (cons (car entry) matched))))
391         (modb-standard-flag-map msgdb)))
392       (digest
393        (let ((flags (append elmo-digest-flags
394                             (elmo-get-global-flags t t))))
395          (mapatoms
396           (lambda (atom)
397             (setq entry (symbol-value atom))
398             (when (modb-standard-match-flags flags (cdr entry))
399               (setq matched (cons (car entry) matched))))
400           (modb-standard-flag-map msgdb))))
401       (t
402        (mapatoms
403         (lambda (atom)
404           (setq entry (symbol-value atom))
405           (when (memq flag (cdr entry))
406             (setq matched (cons (car entry) matched))))
407         (modb-standard-flag-map msgdb))))
408     matched))
409
410 (luna-define-method elmo-msgdb-search ((msgdb modb-standard)
411                                        condition &optional numbers)
412   (if (vectorp condition)
413       (let ((key (elmo-filter-key condition))
414             results)
415         (cond
416          ((and (string= key "flag")
417                (eq (elmo-filter-type condition) 'match))
418           (setq results (elmo-msgdb-list-flagged
419                          msgdb
420                          (intern (elmo-filter-value condition))))
421           (if numbers
422               (elmo-list-filter numbers results)
423             results))
424          ((member key '("first" "last"))
425           (let* ((numbers (or numbers
426                               (modb-standard-number-list-internal msgdb)))
427                  (len (length numbers))
428                  (lastp (string= key "last"))
429                  (value (string-to-number (elmo-filter-value condition))))
430             (when (eq (elmo-filter-type condition) 'unmatch)
431               (setq lastp (not lastp)
432                     value (- len value)))
433             (if lastp
434                 (nthcdr (max (- len value) 0) numbers)
435               (when (> value 0)
436                 (let* ((numbers (copy-sequence numbers))
437                        (last (nthcdr (1- value) numbers)))
438                   (when last
439                     (setcdr last nil))
440                   numbers)))))
441          (t
442           t)))
443     t))
444
445 (luna-define-method elmo-msgdb-append-entity ((msgdb modb-standard)
446                                               entity &optional flags)
447   (when entity
448     (let ((number (elmo-msgdb-message-entity-number
449                    (elmo-message-entity-handler entity) entity))
450           (msg-id (elmo-msgdb-message-entity-field
451                    (elmo-message-entity-handler entity) entity 'message-id))
452           duplicate)
453       (when msg-id
454         ;; number-list
455         (modb-standard-set-number-list-internal
456          msgdb
457          (nconc (modb-standard-number-list-internal msgdb)
458                 (list number)))
459         ;; entity-map
460         (let ((table (modb-standard-entity-map msgdb)))
461           (setq duplicate (elmo-get-hash-val msg-id table))
462           (elmo-set-hash-val (modb-standard-key number) entity table)
463           (elmo-set-hash-val msg-id entity table))
464         ;; modification flags
465         (modb-standard-set-message-modified msgdb number)
466         ;; flag-map
467         (when flags
468           (elmo-set-hash-val
469            (modb-standard-key number)
470            (cons number flags)
471            (modb-standard-flag-map msgdb))
472           (modb-standard-countup-flags msgdb flags)
473           (modb-standard-set-flag-modified msgdb number))
474         duplicate))))
475
476 (luna-define-method elmo-msgdb-delete-messages ((msgdb modb-standard)
477                                                 numbers)
478   (let ((number-list (modb-standard-number-list-internal msgdb))
479         (entity-map (modb-standard-entity-map-internal msgdb))
480         (flag-map (modb-standard-flag-map-internal msgdb))
481         key entity)
482     (dolist (number numbers)
483       (setq key (modb-standard-key number)
484             entity (elmo-get-hash-val key entity-map))
485       (when entity
486         ;; number-list
487         (setq number-list (delq number number-list))
488         ;; entity-map
489         (elmo-clear-hash-val key entity-map)
490         (elmo-clear-hash-val (modb-standard-entity-id entity) entity-map)
491         ;; flag-count (must be BEFORE flag-map)
492         (modb-standard-countup-flags
493          msgdb
494          (modb-standard-message-flags msgdb number)
495          -1)
496         ;; flag-map
497         (elmo-clear-hash-val key flag-map)
498         (modb-standard-set-message-modified msgdb number)
499         (modb-standard-set-flag-modified msgdb number)))
500     (modb-standard-set-number-list-internal msgdb number-list)
501     (modb-standard-set-entity-map-internal msgdb entity-map)
502     (modb-standard-set-flag-map-internal msgdb flag-map)
503     t))
504
505 (luna-define-method elmo-msgdb-sort-entities ((msgdb modb-standard)
506                                               predicate &optional app-data)
507   (message "Sorting...")
508   (let ((numbers (modb-standard-number-list-internal msgdb)))
509     (modb-standard-set-number-list-internal
510      msgdb
511      (sort numbers (lambda (a b)
512                      (funcall predicate
513                               (elmo-msgdb-message-entity msgdb a)
514                               (elmo-msgdb-message-entity msgdb b)
515                               app-data))))
516     (message "Sorting...done")
517     msgdb))
518
519 (defun modb-standard-message-entity (msgdb key load)
520   (let ((ret (elmo-get-hash-val
521               key
522               (modb-standard-entity-map-internal msgdb))))
523     (if (eq 'autoload (car-safe ret))
524         (when (and load modb-standard-divide-number)
525           (modb-standard-load-entity
526            msgdb
527            (elmo-msgdb-location msgdb)
528            (/ (nth 1 ret) modb-standard-divide-number))
529           (modb-standard-message-entity msgdb key nil))
530       ret)))
531
532 (luna-define-method elmo-msgdb-message-number ((msgdb modb-standard)
533                                                message-id)
534   (let ((ret (elmo-get-hash-val
535               message-id
536               (modb-standard-entity-map-internal msgdb))))
537     (if (eq 'autoload (car-safe ret))
538         ;; Not loaded yet but can return number.
539         (nth 1 ret)
540       (elmo-message-entity-number ret))))
541
542 (luna-define-method elmo-msgdb-message-field ((msgdb modb-standard)
543                                               number field)
544   (let ((ret (elmo-get-hash-val
545               (modb-standard-key number)
546               (modb-standard-entity-map-internal msgdb))))
547     (if (and (eq 'autoload (car-safe ret)) (eq field 'message-id))
548         ;; Not loaded yet but can return message-id
549         (cdr (cdr ret))
550       (elmo-message-entity-field (elmo-msgdb-message-entity
551                                   msgdb (modb-standard-key number))
552                                  field))))
553
554 (luna-define-method elmo-msgdb-message-entity ((msgdb modb-standard) key)
555   (when key
556     (modb-standard-message-entity
557      msgdb
558      (cond ((stringp key) key)
559            ((numberp key) (modb-standard-key key)))
560      'autoload)))
561
562 (require 'product)
563 (product-provide (provide 'modb-standard) (require 'elmo-version))
564
565 ;;; modb-standard.el ends here