* wl-expire.el (wl-summary-archive): Fixed;
[elisp/wanderlust.git] / elmo / elmo-shimbun.el
1 ;;; elmo-shimbun.el -- Shimbun interface for ELMO.
2
3 ;; Copyright (C) 2001 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)
32 (require 'elmo-map)
33 (require 'elmo-dop)
34 (require 'shimbun)
35
36 (defcustom elmo-shimbun-check-interval 60
37   "*Check interval for shimbun."
38   :type 'integer
39   :group 'elmo)
40
41 (defcustom elmo-shimbun-default-index-range 2
42   "*Default value for the range of header indices."
43   :type '(choice (const :tag "all" all)
44                  (const :tag "last" last)
45                  (integer :tag "number"))
46   :group 'elmo)
47
48 (defcustom elmo-shimbun-use-cache t
49   "*If non-nil, use cache for each article."
50   :type :boolean
51   :group 'elmo)
52
53 (defcustom elmo-shimbun-index-range-alist nil
54   "*Alist of FOLDER-REGEXP and RANGE.
55 FOLDER-REGEXP is the regexp for shimbun folder name.
56 RANGE is the range of the header indices .
57 See `shimbun-headers' for more detail about RANGE."
58   :type '(repeat (cons (regexp :tag "Folder Regexp")
59                        (choice (const :tag "all" all)
60                                (const :tag "last" last)
61                                (integer :tag "number"))))
62   :group 'elmo)
63
64 ;; Shimbun mua.
65 (eval-and-compile 
66   (luna-define-class shimbun-elmo-mua (shimbun-mua) (folder))
67   (luna-define-internal-accessors 'shimbun-elmo-mua))
68
69 (luna-define-method shimbun-mua-search-id ((mua shimbun-elmo-mua) id)
70   (elmo-msgdb-overview-get-entity id 
71                                   (elmo-folder-msgdb
72                                    (shimbun-elmo-mua-folder-internal mua))))
73
74 (eval-and-compile
75   (luna-define-class elmo-shimbun-folder
76                      (elmo-map-folder) (shimbun headers header-hash
77                                                 group range last-check))
78   (luna-define-internal-accessors 'elmo-shimbun-folder))
79
80 (defsubst elmo-shimbun-lapse-seconds (time)
81   (let ((now (current-time)))
82     (+ (* (- (car now) (car time)) 65536)
83        (- (nth 1 now) (nth 1 time)))))
84
85 (defun elmo-shimbun-parse-time-string (string)
86   "Parse the time-string STRING and return its time as Emacs style."
87   (ignore-errors
88     (let ((x (timezone-fix-time string nil nil)))
89       (encode-time (aref x 5) (aref x 4) (aref x 3)
90                    (aref x 2) (aref x 1) (aref x 0)
91                    (aref x 6)))))
92
93 (defsubst elmo-shimbun-headers-check-p (folder)
94   (or (null (elmo-shimbun-folder-last-check-internal folder))
95       (and (elmo-shimbun-folder-last-check-internal folder)
96            (> (elmo-shimbun-lapse-seconds
97                (elmo-shimbun-folder-last-check-internal folder))
98               elmo-shimbun-check-interval))))
99
100 (defun elmo-shimbun-msgdb-to-headers (folder expire-days)
101   (let (headers)
102     (dolist (ov (elmo-msgdb-get-overview (elmo-folder-msgdb folder)))
103       (when (and (elmo-msgdb-overview-entity-get-extra-field ov "xref")
104                  (if expire-days
105                      (< (elmo-shimbun-lapse-seconds
106                          (elmo-shimbun-parse-time-string
107                           (elmo-msgdb-overview-entity-get-date ov)))
108                         (* expire-days 86400 ; seconds per day
109                            ))
110                    t))
111         (setq headers
112               (cons (shimbun-make-header
113                      (elmo-msgdb-overview-entity-get-number ov)
114                      (shimbun-mime-encode-string
115                       (elmo-msgdb-overview-entity-get-subject ov))
116                      (shimbun-mime-encode-string
117                       (elmo-msgdb-overview-entity-get-from ov))
118                      (elmo-msgdb-overview-entity-get-date ov)
119                      (elmo-msgdb-overview-entity-get-id ov)
120                      (elmo-msgdb-overview-entity-get-references ov)
121                      0
122                      0
123                      (elmo-msgdb-overview-entity-get-extra-field ov "xref"))
124                     headers))))
125     (nreverse headers)))
126
127 (defun elmo-shimbun-folder-setup (folder)
128   ;; Resume headers from existing msgdb.
129   (elmo-shimbun-folder-set-headers-internal
130    folder
131    (elmo-shimbun-msgdb-to-headers folder nil))
132   (elmo-shimbun-folder-set-header-hash-internal
133    folder
134    (elmo-make-hash
135     (length (elmo-shimbun-folder-headers-internal folder))))
136   (dolist (header (elmo-shimbun-folder-headers-internal folder))
137     (elmo-set-hash-val
138      (shimbun-header-id header) header
139      (elmo-shimbun-folder-header-hash-internal folder))))
140
141 (defun elmo-shimbun-get-headers (folder)
142   (shimbun-open-group
143    (elmo-shimbun-folder-shimbun-internal folder)
144    (elmo-shimbun-folder-group-internal folder))
145   (let* ((shimbun (elmo-shimbun-folder-shimbun-internal folder))
146          (key (concat (shimbun-server-internal shimbun)
147                       "." (shimbun-current-group-internal shimbun)))
148          (elmo-hash-minimum-size 0)
149          entry headers hash)
150     ;; new headers.
151     (setq headers
152           (delq nil
153                 (mapcar
154                  (lambda (x)
155                    (unless (elmo-msgdb-overview-get-entity 
156                             (shimbun-header-id x)
157                             (elmo-folder-msgdb folder))
158                      x))
159                  ;; This takes much time.
160                  (shimbun-headers
161                   (elmo-shimbun-folder-shimbun-internal folder)
162                   (elmo-shimbun-folder-range-internal folder)))))
163     (elmo-shimbun-folder-set-headers-internal
164      folder
165      (nconc (elmo-shimbun-msgdb-to-headers
166              folder (shimbun-article-expiration-days
167                      (elmo-shimbun-folder-shimbun-internal folder)))
168             headers))
169     (setq hash
170           (elmo-shimbun-folder-set-header-hash-internal
171            folder
172            (elmo-make-hash
173             (length (elmo-shimbun-folder-headers-internal folder)))))
174     ;; Set up header hash.
175     (dolist (header (elmo-shimbun-folder-headers-internal folder))
176       (elmo-set-hash-val
177        (shimbun-header-id header) header
178        (elmo-shimbun-folder-header-hash-internal folder)))
179     (elmo-shimbun-folder-set-last-check-internal folder (current-time))))
180
181 (luna-define-method elmo-folder-initialize ((folder
182                                              elmo-shimbun-folder)
183                                             name)
184   (let ((server-group (if (string-match "\\([^.]+\\)\\." name)
185                           (list (elmo-match-string 1 name)
186                                 (substring name (match-end 0)))
187                         (list name))))
188     (when (nth 0 server-group) ; server
189       (elmo-shimbun-folder-set-shimbun-internal
190        folder
191        (shimbun-open (nth 0 server-group)
192                      (luna-make-entity 'shimbun-elmo-mua :folder folder))))
193     (when (nth 1 server-group)
194       (elmo-shimbun-folder-set-group-internal
195        folder
196        (nth 1 server-group)))
197     (elmo-shimbun-folder-set-range-internal
198      folder
199      (or (cdr (elmo-string-matched-assoc (elmo-folder-name-internal folder)
200                                          elmo-shimbun-index-range-alist))
201          elmo-shimbun-default-index-range))
202     folder))
203
204 (luna-define-method elmo-folder-open-internal ((folder elmo-shimbun-folder))
205   (when (elmo-folder-plugged-p folder)
206     (when (elmo-shimbun-headers-check-p folder)
207       (let ((inhibit-quit t))
208         (elmo-map-folder-location-setup
209          folder 
210          (elmo-msgdb-location-load (elmo-folder-msgdb-path folder)))
211         ;; Resume headers from existing msgdb.
212         (elmo-shimbun-folder-setup folder))
213       (elmo-shimbun-get-headers folder))
214     (elmo-map-folder-update-locations
215      folder
216      (elmo-map-folder-list-message-locations folder))))
217
218 (luna-define-method elmo-folder-reserve-status-p ((folder elmo-shimbun-folder))
219   t)
220
221 (luna-define-method elmo-message-use-cache-p ((folder elmo-shimbun-folder)
222                                               number)
223   elmo-shimbun-use-cache)
224
225 (luna-define-method elmo-folder-close-internal :after ((folder
226                                                         elmo-shimbun-folder))
227   (shimbun-close-group
228    (elmo-shimbun-folder-shimbun-internal folder))
229   (elmo-shimbun-folder-set-headers-internal
230    folder nil)
231   (elmo-shimbun-folder-set-header-hash-internal
232    folder nil)
233   (elmo-shimbun-folder-set-last-check-internal
234    folder nil))
235
236 (luna-define-method elmo-folder-plugged-p ((folder elmo-shimbun-folder))
237   (elmo-plugged-p
238    "shimbun" 
239    (shimbun-server-internal (elmo-shimbun-folder-shimbun-internal folder))
240    nil nil
241    (shimbun-server-internal (elmo-shimbun-folder-shimbun-internal folder))))
242                             
243 (luna-define-method elmo-folder-set-plugged ((folder elmo-shimbun-folder)
244                                              plugged &optional add)
245   (elmo-set-plugged plugged
246                     "shimbun"
247                     (shimbun-server-internal
248                      (elmo-shimbun-folder-shimbun-internal folder))
249                     nil nil nil
250                     (shimbun-server-internal
251                      (elmo-shimbun-folder-shimbun-internal folder))
252                     add))
253
254 (luna-define-method elmo-net-port-info ((folder elmo-shimbun-folder))
255   (list "shimbun"
256         (shimbun-server-internal
257          (elmo-shimbun-folder-shimbun-internal folder))
258         nil))
259
260 (luna-define-method elmo-folder-check :around ((folder elmo-shimbun-folder))
261   (when (shimbun-current-group-internal 
262          (elmo-shimbun-folder-shimbun-internal folder))
263     (when (and (elmo-folder-plugged-p folder)
264                (elmo-shimbun-headers-check-p folder))
265       (elmo-shimbun-get-headers folder)
266       (luna-call-next-method))))
267
268 (luna-define-method elmo-folder-clear :around ((folder elmo-shimbun-folder)
269                                                &optional keep-killed)
270   (elmo-shimbun-folder-set-headers-internal folder nil)
271   (elmo-shimbun-folder-set-header-hash-internal folder nil)
272   (elmo-shimbun-folder-set-last-check-internal folder nil)
273   (luna-call-next-method))
274
275 (luna-define-method elmo-folder-expand-msgdb-path ((folder
276                                                     elmo-shimbun-folder))
277   (expand-file-name
278    (concat (shimbun-server-internal
279             (elmo-shimbun-folder-shimbun-internal folder))
280            "/"
281            (elmo-shimbun-folder-group-internal folder))
282    (expand-file-name "shimbun" elmo-msgdb-dir)))
283                      
284 (defun elmo-shimbun-msgdb-create-entity (folder number)
285   (let ((header (elmo-get-hash-val
286                  (elmo-map-message-location folder number)
287                  (elmo-shimbun-folder-header-hash-internal folder)))
288         ov)
289     (when header
290       (with-temp-buffer
291         (shimbun-header-insert
292          (elmo-shimbun-folder-shimbun-internal folder)
293          header)
294         (setq ov (elmo-msgdb-create-overview-from-buffer number))
295         (elmo-msgdb-overview-entity-set-extra
296          ov
297          (nconc
298           (elmo-msgdb-overview-entity-get-extra ov)
299           (list (cons "xref" (shimbun-header-xref header)))))))))
300
301 (luna-define-method elmo-folder-msgdb-create ((folder elmo-shimbun-folder)
302                                               numlist new-mark
303                                               already-mark seen-mark
304                                               important-mark
305                                               seen-list)
306   (let* (overview number-alist mark-alist entity
307                   i percent number length pair msgid gmark seen)
308     (setq length (length numlist))
309     (setq i 0)
310     (message "Creating msgdb...")
311     (while numlist
312       (setq entity
313             (elmo-shimbun-msgdb-create-entity
314              folder (car numlist)))
315       (when entity
316         (setq overview
317               (elmo-msgdb-append-element
318                overview entity))
319         (setq number (elmo-msgdb-overview-entity-get-number entity))
320         (setq msgid (elmo-msgdb-overview-entity-get-id entity))
321         (setq number-alist
322               (elmo-msgdb-number-add number-alist
323                                      number msgid))
324         (setq seen (member msgid seen-list))
325         (if (setq gmark (or (elmo-msgdb-global-mark-get msgid)
326                             (if (elmo-file-cache-status
327                                  (elmo-file-cache-get msgid))
328                                 (if seen nil already-mark)
329                               (if seen
330                                   (if elmo-shimbun-use-cache
331                                       seen-mark)
332                                 new-mark))))
333             (setq mark-alist
334                   (elmo-msgdb-mark-append mark-alist
335                                           number gmark))))
336       (when (> length elmo-display-progress-threshold)
337         (setq i (1+ i))
338         (setq percent (/ (* i 100) length))
339         (elmo-display-progress
340          'elmo-folder-msgdb-create "Creating msgdb..."
341          percent))
342       (setq numlist (cdr numlist)))
343     (message "Creating msgdb...done.")
344     (elmo-msgdb-sort-by-date
345      (list overview number-alist mark-alist))))
346
347 (luna-define-method elmo-folder-message-file-p ((folder elmo-shimbun-folder))
348   nil)
349
350 (luna-define-method elmo-map-message-fetch ((folder elmo-shimbun-folder)
351                                             location strategy
352                                             &optional section unseen)
353   (shimbun-article (elmo-shimbun-folder-shimbun-internal folder)
354                    (elmo-get-hash-val
355                     location
356                     (elmo-shimbun-folder-header-hash-internal folder))))
357
358 (luna-define-method elmo-message-encache :around ((folder
359                                                    elmo-shimbun-folder)
360                                                   number &optional read)
361   (if (elmo-folder-plugged-p folder)
362       (luna-call-next-method)
363     (if elmo-enable-disconnected-operation
364         (elmo-message-encache-dop folder number read)
365       (error "Unplugged"))))
366
367 (luna-define-method elmo-folder-list-messages-internal :around
368   ((folder elmo-shimbun-folder) &optional nohide)
369   (if (elmo-folder-plugged-p folder)
370       (luna-call-next-method)
371     t))
372
373 (luna-define-method elmo-map-folder-list-message-locations
374   ((folder elmo-shimbun-folder))
375   (mapcar
376    (function shimbun-header-id)
377    (elmo-shimbun-folder-headers-internal folder)))
378
379 (luna-define-method elmo-folder-list-subfolders ((folder elmo-shimbun-folder)
380                                                  &optional one-level)
381   (unless (elmo-shimbun-folder-group-internal folder)
382     (mapcar
383      (lambda (x)
384        (concat (elmo-folder-prefix-internal folder)
385                (shimbun-server-internal
386                 (elmo-shimbun-folder-shimbun-internal folder))
387                "."
388                x))
389      (shimbun-groups (elmo-shimbun-folder-shimbun-internal folder)))))
390
391 (luna-define-method elmo-folder-exists-p ((folder elmo-shimbun-folder))
392   (if (elmo-shimbun-folder-group-internal folder)
393       (progn
394         (member 
395          (elmo-shimbun-folder-group-internal folder)
396          (shimbun-groups (elmo-shimbun-folder-shimbun-internal
397                           folder))))
398     t))
399
400 (luna-define-method elmo-folder-search ((folder elmo-shimbun-folder)
401                                         condition &optional from-msgs)
402   nil)
403
404 ;;; To override elmo-map-folder methods.
405 (luna-define-method elmo-folder-list-unreads-internal
406   ((folder elmo-shimbun-folder) unread-marks &optional mark-alist)
407   t)
408
409 (luna-define-method elmo-folder-unmark-important ((folder elmo-shimbun-folder)
410                                                   numbers)
411   t)
412
413 (luna-define-method elmo-folder-mark-as-important ((folder elmo-shimbun-folder)
414                                                    numbers)
415   t)
416
417 (luna-define-method elmo-folder-unmark-read ((folder elmo-shimbun-folder)
418                                              numbers)
419   t)
420
421 (luna-define-method elmo-folder-mark-as-read ((folder elmo-shimbun-folder)
422                                               numbers)
423   t)
424
425 (require 'product)
426 (product-provide (provide 'elmo-shimbun) (require 'elmo-version))
427
428 ;;; elmo-shimbun.el ends here