1 ;;; elmo-shimbun.el --- Shimbun interface for ELMO.
3 ;; Copyright (C) 2001 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
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)
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.
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.
36 (defcustom elmo-shimbun-check-interval 60
37 "*Check interval for shimbun."
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"))
48 (defcustom elmo-shimbun-use-cache t
49 "*If non-nil, use cache for each article."
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"))))
64 (defcustom elmo-shimbun-update-overview-folder-list nil
65 "*List of FOLDER-REGEXP.
66 FOLDER-REGEXP is the regexp of shimbun folder name which should be
67 update overview when message is fetched."
68 :type '(repeat (regexp :tag "Folder Regexp"))
72 (defsubst elmo-shimbun-header-extra-field (header field-name)
73 (let ((extra (and header (shimbun-header-extra header))))
75 (cdr (assoc field-name extra)))))
77 (defsubst elmo-shimbun-header-set-extra-field (header field-name value)
78 (let ((extras (and header (shimbun-header-extra header)))
80 (if (setq extra (assoc field-name extras))
82 (shimbun-header-set-extra
84 (cons (cons field-name value) extras)))))
88 (luna-define-class shimbun-elmo-mua (shimbun-mua) (folder))
89 (luna-define-internal-accessors 'shimbun-elmo-mua))
91 (luna-define-method shimbun-mua-search-id ((mua shimbun-elmo-mua) id)
92 (elmo-msgdb-overview-get-entity id
94 (shimbun-elmo-mua-folder-internal mua))))
97 (luna-define-class elmo-shimbun-folder
98 (elmo-map-folder) (shimbun headers header-hash
100 group range last-check))
101 (luna-define-internal-accessors 'elmo-shimbun-folder))
103 (defun elmo-shimbun-folder-entity-hash (folder)
104 (or (elmo-shimbun-folder-entity-hash-internal folder)
105 (let ((overviews (elmo-msgdb-get-overview (elmo-folder-msgdb folder)))
108 (setq hash (elmo-make-hash (length overviews)))
109 (dolist (entity overviews)
110 (elmo-set-hash-val (elmo-msgdb-overview-entity-get-id entity)
112 (when (setq id (elmo-msgdb-overview-entity-get-extra-field
113 entity "x-original-id"))
114 (elmo-set-hash-val id entity hash)))
115 (elmo-shimbun-folder-set-entity-hash-internal folder hash)))))
117 (defsubst elmo-shimbun-folder-shimbun-header (folder location)
118 (let ((hash (elmo-shimbun-folder-header-hash-internal folder)))
119 (or (and hash (elmo-get-hash-val location hash))
120 (let ((entity (elmo-msgdb-overview-get-entity
122 (elmo-folder-msgdb folder)))
123 (elmo-hash-minimum-size 63)
126 (setq header (elmo-shimbun-entity-to-header entity))
128 (elmo-shimbun-folder-set-header-hash-internal
130 (setq hash (elmo-make-hash))))
131 (elmo-set-hash-val (elmo-msgdb-overview-entity-get-id entity)
136 (defsubst elmo-shimbun-lapse-seconds (time)
137 (let ((now (current-time)))
138 (+ (* (- (car now) (car time)) 65536)
139 (- (nth 1 now) (nth 1 time)))))
141 (defun elmo-shimbun-parse-time-string (string)
142 "Parse the time-string STRING and return its time as Emacs style."
144 (let ((x (timezone-fix-time string nil nil)))
145 (encode-time (aref x 5) (aref x 4) (aref x 3)
146 (aref x 2) (aref x 1) (aref x 0)
149 (defsubst elmo-shimbun-headers-check-p (folder)
150 (or (null (elmo-shimbun-folder-last-check-internal folder))
151 (and (elmo-shimbun-folder-last-check-internal folder)
152 (> (elmo-shimbun-lapse-seconds
153 (elmo-shimbun-folder-last-check-internal folder))
154 elmo-shimbun-check-interval))))
156 (defun elmo-shimbun-entity-to-header (entity)
157 (let (message-id shimbun-id)
158 (if (setq message-id (elmo-msgdb-overview-entity-get-extra-field
159 entity "x-original-id"))
160 (setq shimbun-id (elmo-msgdb-overview-entity-get-id entity))
161 (setq message-id (elmo-msgdb-overview-entity-get-id entity)
164 (set-buffer-multibyte t)
166 (elmo-msgdb-overview-entity-get-number entity)
167 (shimbun-mime-encode-string
168 (decode-mime-charset-string
169 (elmo-msgdb-overview-entity-get-subject-no-decode entity)
171 (shimbun-mime-encode-string
172 (decode-mime-charset-string
173 (elmo-msgdb-overview-entity-get-from-no-decode entity)
175 (elmo-msgdb-overview-entity-get-date entity)
177 (elmo-msgdb-overview-entity-get-references entity)
180 (elmo-msgdb-overview-entity-get-extra-field entity "xref")
182 (list (cons "x-shimbun-id" shimbun-id)))))))
184 (defsubst elmo-shimbun-folder-header-hash-setup (folder headers)
185 (let ((hash (or (elmo-shimbun-folder-header-hash-internal folder)
186 (elmo-make-hash (length headers)))))
187 (dolist (header headers)
188 (elmo-set-hash-val (shimbun-header-id header) header hash))
189 (elmo-shimbun-folder-set-header-hash-internal folder hash)))
191 (defun elmo-shimbun-get-headers (folder)
192 (let* ((shimbun (elmo-shimbun-folder-shimbun-internal folder))
193 (key (concat (shimbun-server-internal shimbun)
194 "." (shimbun-current-group-internal shimbun)))
195 (elmo-hash-minimum-size 63)
202 (unless (elmo-msgdb-overview-get-entity
203 (shimbun-header-id x)
204 (elmo-folder-msgdb folder))
206 ;; This takes much time.
208 (elmo-shimbun-folder-shimbun-internal folder)
209 (elmo-shimbun-folder-range-internal folder)))))
210 (elmo-shimbun-folder-set-headers-internal folder headers)
212 (elmo-shimbun-folder-header-hash-setup folder headers))
213 (elmo-shimbun-folder-set-last-check-internal folder (current-time))))
215 (luna-define-method elmo-folder-initialize ((folder
218 (let ((server-group (if (string-match "\\([^.]+\\)\\." name)
219 (list (elmo-match-string 1 name)
220 (substring name (match-end 0)))
222 (when (nth 0 server-group) ; server
223 (elmo-shimbun-folder-set-shimbun-internal
225 (shimbun-open (nth 0 server-group)
226 (luna-make-entity 'shimbun-elmo-mua :folder folder))))
227 (when (nth 1 server-group)
228 (elmo-shimbun-folder-set-group-internal
230 (nth 1 server-group)))
231 (elmo-shimbun-folder-set-range-internal
233 (or (cdr (elmo-string-matched-assoc (elmo-folder-name-internal folder)
234 elmo-shimbun-index-range-alist))
235 elmo-shimbun-default-index-range))
238 (luna-define-method elmo-folder-open-internal ((folder elmo-shimbun-folder))
240 (elmo-shimbun-folder-shimbun-internal folder)
241 (elmo-shimbun-folder-group-internal folder))
242 (let ((inhibit-quit t))
243 (unless (elmo-map-folder-location-alist-internal folder)
244 (elmo-map-folder-location-setup
246 (elmo-msgdb-location-load (elmo-folder-msgdb-path folder))))
247 (when (and (elmo-folder-plugged-p folder)
248 (elmo-shimbun-headers-check-p folder))
249 (elmo-shimbun-get-headers folder)
250 (elmo-map-folder-update-locations
252 (elmo-map-folder-list-message-locations folder)))))
254 (luna-define-method elmo-folder-reserve-status-p ((folder elmo-shimbun-folder))
257 (luna-define-method elmo-folder-local-p ((folder elmo-shimbun-folder))
260 (luna-define-method elmo-message-use-cache-p ((folder elmo-shimbun-folder)
262 elmo-shimbun-use-cache)
264 (luna-define-method elmo-folder-close-internal :after ((folder
265 elmo-shimbun-folder))
267 (elmo-shimbun-folder-shimbun-internal folder))
268 (elmo-shimbun-folder-set-headers-internal
270 (elmo-shimbun-folder-set-header-hash-internal
272 (elmo-shimbun-folder-set-entity-hash-internal
274 (elmo-shimbun-folder-set-last-check-internal
277 (luna-define-method elmo-folder-plugged-p ((folder elmo-shimbun-folder))
280 (shimbun-server-internal (elmo-shimbun-folder-shimbun-internal folder))
282 (shimbun-server-internal (elmo-shimbun-folder-shimbun-internal folder))))
284 (luna-define-method elmo-folder-set-plugged ((folder elmo-shimbun-folder)
285 plugged &optional add)
286 (elmo-set-plugged plugged
288 (shimbun-server-internal
289 (elmo-shimbun-folder-shimbun-internal folder))
291 (shimbun-server-internal
292 (elmo-shimbun-folder-shimbun-internal folder))
295 (luna-define-method elmo-net-port-info ((folder elmo-shimbun-folder))
297 (shimbun-server-internal
298 (elmo-shimbun-folder-shimbun-internal folder))
301 (luna-define-method elmo-folder-check :around ((folder elmo-shimbun-folder))
302 (when (shimbun-current-group-internal
303 (elmo-shimbun-folder-shimbun-internal folder))
304 (when (and (elmo-folder-plugged-p folder)
305 (elmo-shimbun-headers-check-p folder))
306 (elmo-shimbun-get-headers folder)
307 (luna-call-next-method))))
309 (luna-define-method elmo-folder-clear :around ((folder elmo-shimbun-folder)
310 &optional keep-killed)
311 (elmo-shimbun-folder-set-headers-internal folder nil)
312 (elmo-shimbun-folder-set-header-hash-internal folder nil)
313 (elmo-shimbun-folder-set-entity-hash-internal folder nil)
314 (elmo-shimbun-folder-set-last-check-internal folder nil)
315 (luna-call-next-method))
317 (luna-define-method elmo-folder-expand-msgdb-path ((folder
318 elmo-shimbun-folder))
320 (concat (shimbun-server-internal
321 (elmo-shimbun-folder-shimbun-internal folder))
323 (elmo-shimbun-folder-group-internal folder))
324 (expand-file-name "shimbun" elmo-msgdb-directory)))
326 (defun elmo-shimbun-msgdb-create-entity (folder number)
327 (let ((header (elmo-shimbun-folder-shimbun-header
329 (elmo-map-message-location folder number)))
333 (shimbun-header-insert
334 (elmo-shimbun-folder-shimbun-internal folder)
336 (setq ov (elmo-msgdb-create-overview-from-buffer number))
337 (elmo-msgdb-overview-entity-set-extra
340 (elmo-msgdb-overview-entity-get-extra ov)
341 (list (cons "xref" (shimbun-header-xref header)))))))))
343 (luna-define-method elmo-folder-msgdb-create ((folder elmo-shimbun-folder)
345 (let* (overview number-alist mark-alist entity
346 i percent number length pair msgid gmark seen)
347 (setq length (length numlist))
349 (message "Creating msgdb...")
352 (elmo-shimbun-msgdb-create-entity
353 folder (car numlist)))
356 (elmo-msgdb-append-element
358 (setq number (elmo-msgdb-overview-entity-get-number entity))
359 (setq msgid (elmo-msgdb-overview-entity-get-id entity))
361 (elmo-msgdb-number-add number-alist
363 (setq seen (member msgid seen-list))
364 (if (setq gmark (or (elmo-msgdb-global-mark-get msgid)
365 (if (elmo-file-cache-status
366 (elmo-file-cache-get msgid))
367 (if seen nil elmo-msgdb-unread-cached-mark)
369 (if elmo-shimbun-use-cache
370 elmo-msgdb-read-uncached-mark)
371 elmo-msgdb-new-mark))))
373 (elmo-msgdb-mark-append mark-alist
375 (when (> length elmo-display-progress-threshold)
377 (setq percent (/ (* i 100) length))
378 (elmo-display-progress
379 'elmo-folder-msgdb-create "Creating msgdb..."
381 (setq numlist (cdr numlist)))
382 (message "Creating msgdb...done")
383 (elmo-msgdb-sort-by-date
384 (list overview number-alist mark-alist))))
386 (luna-define-method elmo-folder-message-file-p ((folder elmo-shimbun-folder))
389 (defsubst elmo-shimbun-update-overview (folder shimbun-id header)
390 (let ((entity (elmo-msgdb-overview-get-entity shimbun-id
391 (elmo-folder-msgdb folder)))
392 (message-id (shimbun-header-id header))
394 (unless (string= shimbun-id message-id)
395 (elmo-msgdb-overview-entity-set-extra-field
396 entity "x-original-id" message-id)
397 (elmo-shimbun-header-set-extra-field
398 header "x-shimbun-id" shimbun-id)
399 (elmo-set-hash-val message-id
401 (elmo-shimbun-folder-entity-hash folder))
402 (elmo-set-hash-val shimbun-id
404 (elmo-shimbun-folder-entity-hash folder)))
405 (elmo-msgdb-overview-entity-set-from
407 (elmo-mime-string (shimbun-header-from header)))
408 (elmo-msgdb-overview-entity-set-subject
410 (elmo-mime-string (shimbun-header-subject header)))
411 (elmo-msgdb-overview-entity-set-date
412 entity (shimbun-header-date header))
413 (when (setq references
414 (or (elmo-msgdb-get-last-message-id
415 (elmo-field-body "in-reply-to"))
416 (elmo-msgdb-get-last-message-id
417 (elmo-field-body "references"))))
418 (elmo-msgdb-overview-entity-set-references
420 (or (elmo-msgdb-overview-entity-get-id
423 (elmo-shimbun-folder-entity-hash folder)))
426 (luna-define-method elmo-map-message-fetch ((folder elmo-shimbun-folder)
428 &optional section unseen)
429 (if (elmo-folder-plugged-p folder)
430 (let ((header (elmo-shimbun-folder-shimbun-header
434 (shimbun-article (elmo-shimbun-folder-shimbun-internal folder)
436 (when (elmo-string-match-member
437 (elmo-folder-name-internal folder)
438 elmo-shimbun-update-overview-folder-list)
439 (elmo-shimbun-update-overview folder location header))
440 (when (setq shimbun-id
441 (elmo-shimbun-header-extra-field header "x-shimbun-id"))
442 (goto-char (point-min))
443 (insert (format "X-Shimbun-Id: %s\n" shimbun-id)))
445 (error "Unplugged")))
447 (luna-define-method elmo-message-encache :around ((folder
449 number &optional read)
450 (if (elmo-folder-plugged-p folder)
451 (luna-call-next-method)
452 (if elmo-enable-disconnected-operation
453 (elmo-message-encache-dop folder number read)
454 (error "Unplugged"))))
456 (luna-define-method elmo-folder-list-messages-internal :around
457 ((folder elmo-shimbun-folder) &optional nohide)
458 (if (elmo-folder-plugged-p folder)
459 (luna-call-next-method)
462 (luna-define-method elmo-map-folder-list-message-locations
463 ((folder elmo-shimbun-folder))
464 (let ((expire-days (shimbun-article-expiration-days
465 (elmo-shimbun-folder-shimbun-internal folder))))
471 (when (and (elmo-msgdb-overview-entity-get-extra-field
474 (< (elmo-shimbun-lapse-seconds
475 (elmo-shimbun-parse-time-string
476 (elmo-msgdb-overview-entity-get-date ov)))
477 (* expire-days 86400 ; seconds per day
480 (elmo-msgdb-overview-entity-get-id ov)))
481 (elmo-msgdb-get-overview (elmo-folder-msgdb folder))))
484 (or (elmo-shimbun-header-extra-field header "x-shimbun-id")
485 (shimbun-header-id header)))
486 (elmo-shimbun-folder-headers-internal folder))))))
488 (luna-define-method elmo-folder-list-subfolders ((folder elmo-shimbun-folder)
490 (unless (elmo-shimbun-folder-group-internal folder)
493 (concat (elmo-folder-prefix-internal folder)
494 (shimbun-server-internal
495 (elmo-shimbun-folder-shimbun-internal folder))
498 (shimbun-groups (elmo-shimbun-folder-shimbun-internal folder)))))
500 (luna-define-method elmo-folder-exists-p ((folder elmo-shimbun-folder))
501 (if (elmo-shimbun-folder-group-internal folder)
504 (elmo-shimbun-folder-group-internal folder)
505 (shimbun-groups (elmo-shimbun-folder-shimbun-internal
510 (product-provide (provide 'elmo-shimbun) (require 'elmo-version))
512 ;;; elmo-shimbun.el ends here