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.
37 (defun-maybe shimbun-servers-list ()))
39 (defcustom elmo-shimbun-check-interval 60
40 "*Check interval for shimbun."
44 (defcustom elmo-shimbun-default-index-range 2
45 "*Default value for the range of header indices."
46 :type '(choice (const :tag "all" all)
47 (const :tag "last" last)
48 (integer :tag "number"))
51 (defcustom elmo-shimbun-use-cache t
52 "*If non-nil, use cache for each article."
56 (defcustom elmo-shimbun-index-range-alist nil
57 "*Alist of FOLDER-REGEXP and RANGE.
58 FOLDER-REGEXP is the regexp for shimbun folder name.
59 RANGE is the range of the header indices .
60 See `shimbun-headers' for more detail about RANGE."
61 :type '(repeat (cons (regexp :tag "Folder Regexp")
62 (choice (const :tag "all" all)
63 (const :tag "last" last)
64 (integer :tag "number"))))
67 (defcustom elmo-shimbun-update-overview-folder-list 'all
68 "*List of FOLDER-REGEXP.
69 FOLDER-REGEXP is the regexp of shimbun folder name which should be
70 update overview when message is fetched.
71 If it is the symbol `all', update overview for all shimbun folders."
72 :type '(choice (const :tag "All shimbun folders" all)
73 (repeat (regexp :tag "Folder Regexp")))
77 (defsubst elmo-shimbun-header-extra-field (header field-name)
78 (let ((extra (and header (shimbun-header-extra header))))
80 (cdr (assoc field-name extra)))))
82 (defsubst elmo-shimbun-header-set-extra-field (header field-name value)
83 (let ((extras (and header (shimbun-header-extra header)))
85 (if (setq extra (assoc field-name extras))
87 (shimbun-header-set-extra
89 (cons (cons field-name value) extras)))))
93 (luna-define-class shimbun-elmo-mua (shimbun-mua) (folder))
94 (luna-define-internal-accessors 'shimbun-elmo-mua))
96 (luna-define-method shimbun-mua-search-id ((mua shimbun-elmo-mua) id)
97 (elmo-msgdb-message-entity (elmo-folder-msgdb
98 (shimbun-elmo-mua-folder-internal mua))
102 (luna-define-class elmo-shimbun-folder
103 (elmo-map-folder) (shimbun headers header-hash
105 group range last-check))
106 (luna-define-internal-accessors 'elmo-shimbun-folder))
108 (defun elmo-shimbun-folder-entity-hash (folder)
109 (or (elmo-shimbun-folder-entity-hash-internal folder)
110 (let ((overviews (elmo-folder-list-message-entities folder))
113 (setq hash (elmo-make-hash (length overviews)))
114 (dolist (entity overviews)
115 (elmo-set-hash-val (elmo-message-entity-field entity 'message-id)
117 (when (setq id (elmo-message-entity-field entity 'x-original-id))
118 (elmo-set-hash-val id entity hash)))
119 (elmo-shimbun-folder-set-entity-hash-internal folder hash)))))
121 (defsubst elmo-shimbun-folder-shimbun-header (folder location)
122 (let ((hash (elmo-shimbun-folder-header-hash-internal folder)))
123 (or (and hash (elmo-get-hash-val location hash))
124 (let ((entity (elmo-msgdb-message-entity
125 (elmo-folder-msgdb folder)
127 (elmo-hash-minimum-size 63)
130 (setq header (elmo-shimbun-entity-to-header entity))
132 (elmo-shimbun-folder-set-header-hash-internal
134 (setq hash (elmo-make-hash))))
135 (elmo-set-hash-val (elmo-message-entity-field entity
141 (defsubst elmo-shimbun-lapse-seconds (time)
142 (let ((now (current-time)))
143 (+ (* (- (car now) (car time)) 65536)
144 (- (nth 1 now) (nth 1 time)))))
146 (defun elmo-shimbun-parse-time-string (string)
147 "Parse the time-string STRING and return its time as Emacs style."
149 (let ((x (timezone-fix-time string nil nil)))
150 (encode-time (aref x 5) (aref x 4) (aref x 3)
151 (aref x 2) (aref x 1) (aref x 0)
154 (defsubst elmo-shimbun-headers-check-p (folder)
155 (or (null (elmo-shimbun-folder-last-check-internal folder))
156 (and (elmo-shimbun-folder-last-check-internal folder)
157 (> (elmo-shimbun-lapse-seconds
158 (elmo-shimbun-folder-last-check-internal folder))
159 elmo-shimbun-check-interval))))
161 (defun elmo-shimbun-entity-to-header (entity)
162 (let (message-id shimbun-id)
163 (if (setq message-id (elmo-message-entity-field
164 entity 'x-original-id))
165 (setq shimbun-id (elmo-message-entity-field entity 'message-id))
166 (setq message-id (elmo-message-entity-field entity 'message-id)
169 (set-buffer-multibyte t)
171 (elmo-message-entity-number entity)
172 (shimbun-mime-encode-string
173 (elmo-message-entity-field entity 'subject 'decode))
174 (shimbun-mime-encode-string
175 (elmo-message-entity-field entity 'from 'decode))
176 (elmo-message-entity-field entity 'date)
178 (elmo-message-entity-field entity 'references)
181 (elmo-message-entity-field entity 'xref)
183 (list (cons "x-shimbun-id" shimbun-id)))))))
185 (defsubst elmo-shimbun-folder-header-hash-setup (folder headers)
186 (let ((hash (or (elmo-shimbun-folder-header-hash-internal folder)
187 (elmo-make-hash (length headers)))))
188 (dolist (header headers)
189 (elmo-set-hash-val (shimbun-header-id header) header hash))
190 (elmo-shimbun-folder-set-header-hash-internal folder hash)))
192 (defun elmo-shimbun-get-headers (folder)
193 (let* ((shimbun (elmo-shimbun-folder-shimbun-internal folder))
194 (key (concat (shimbun-server shimbun)
195 "." (shimbun-current-group shimbun)))
196 (elmo-hash-minimum-size 63)
203 (unless (elmo-msgdb-message-entity
204 (elmo-folder-msgdb folder)
205 (shimbun-header-id x))
207 ;; This takes much time.
209 (elmo-shimbun-folder-shimbun-internal folder)
210 (elmo-shimbun-folder-range-internal folder)))))
211 (elmo-shimbun-folder-set-headers-internal folder headers)
213 (elmo-shimbun-folder-header-hash-setup folder headers))
214 (elmo-shimbun-folder-set-last-check-internal folder (current-time))))
216 (luna-define-method elmo-folder-initialize ((folder
219 (if (string= name "")
221 (let ((server-group (if (string-match "\\([^.]+\\)\\." name)
222 (list (elmo-match-string 1 name)
223 (substring name (match-end 0)))
225 (when (nth 0 server-group) ; server
226 (elmo-shimbun-folder-set-shimbun-internal
229 (shimbun-open (nth 0 server-group)
230 (luna-make-entity 'shimbun-elmo-mua :folder folder))
232 (luna-make-entity 'shimbun :server (nth 0 server-group))))))
233 (when (nth 1 server-group)
234 (elmo-shimbun-folder-set-group-internal
236 (nth 1 server-group)))
237 (elmo-shimbun-folder-set-range-internal
239 (or (cdr (elmo-string-matched-assoc (elmo-folder-name-internal folder)
240 elmo-shimbun-index-range-alist))
241 elmo-shimbun-default-index-range))
244 (luna-define-method elmo-folder-open-internal ((folder elmo-shimbun-folder))
245 (when (elmo-shimbun-folder-shimbun-internal folder)
247 (elmo-shimbun-folder-shimbun-internal folder)
248 (elmo-shimbun-folder-group-internal folder))
249 (let ((inhibit-quit t))
250 (unless (elmo-map-folder-location-alist-internal folder)
251 (elmo-map-folder-location-setup
253 (elmo-msgdb-location-load (elmo-folder-msgdb-path folder))))
254 (when (and (elmo-folder-plugged-p folder)
255 (elmo-shimbun-headers-check-p folder))
256 (elmo-shimbun-get-headers folder)
257 (elmo-map-folder-update-locations
259 (elmo-map-folder-list-message-locations folder))))))
261 (luna-define-method elmo-folder-reserve-status-p ((folder elmo-shimbun-folder))
264 (luna-define-method elmo-folder-local-p ((folder elmo-shimbun-folder))
267 (luna-define-method elmo-message-use-cache-p ((folder elmo-shimbun-folder)
269 elmo-shimbun-use-cache)
271 (luna-define-method elmo-folder-close-internal :after ((folder
272 elmo-shimbun-folder))
274 (elmo-shimbun-folder-shimbun-internal folder))
275 (elmo-shimbun-folder-set-headers-internal
277 (elmo-shimbun-folder-set-header-hash-internal
279 (elmo-shimbun-folder-set-entity-hash-internal
281 (elmo-shimbun-folder-set-last-check-internal
284 (luna-define-method elmo-folder-plugged-p ((folder elmo-shimbun-folder))
287 (and (elmo-shimbun-folder-shimbun-internal folder)
288 (shimbun-server (elmo-shimbun-folder-shimbun-internal folder)))
290 (and (elmo-shimbun-folder-shimbun-internal folder)
291 (shimbun-server (elmo-shimbun-folder-shimbun-internal folder)))))
293 (luna-define-method elmo-folder-set-plugged ((folder elmo-shimbun-folder)
294 plugged &optional add)
295 (elmo-set-plugged plugged
298 (elmo-shimbun-folder-shimbun-internal folder))
301 (elmo-shimbun-folder-shimbun-internal folder))
304 (luna-define-method elmo-net-port-info ((folder elmo-shimbun-folder))
307 (elmo-shimbun-folder-shimbun-internal folder))
310 (luna-define-method elmo-folder-check :around ((folder elmo-shimbun-folder))
311 (when (shimbun-current-group
312 (elmo-shimbun-folder-shimbun-internal folder))
313 (when (and (elmo-folder-plugged-p folder)
314 (elmo-shimbun-headers-check-p folder))
315 (elmo-shimbun-get-headers folder)
316 (luna-call-next-method))))
318 (luna-define-method elmo-folder-clear :around ((folder elmo-shimbun-folder)
319 &optional keep-killed)
320 (elmo-shimbun-folder-set-headers-internal folder nil)
321 (elmo-shimbun-folder-set-header-hash-internal folder nil)
322 (elmo-shimbun-folder-set-entity-hash-internal folder nil)
323 (elmo-shimbun-folder-set-last-check-internal folder nil)
324 (luna-call-next-method))
326 (luna-define-method elmo-folder-expand-msgdb-path ((folder
327 elmo-shimbun-folder))
329 (concat (shimbun-server
330 (elmo-shimbun-folder-shimbun-internal folder))
332 (elmo-shimbun-folder-group-internal folder))
333 (expand-file-name "shimbun" elmo-msgdb-directory)))
335 (defun elmo-shimbun-msgdb-create-entity (folder number)
336 (let ((header (elmo-shimbun-folder-shimbun-header
338 (elmo-map-message-location folder number)))
342 (shimbun-header-insert
343 (elmo-shimbun-folder-shimbun-internal folder)
345 (setq ov (elmo-msgdb-create-message-entity-from-buffer
346 (elmo-msgdb-message-entity-handler
347 (elmo-folder-msgdb-internal folder)) number))
348 (elmo-message-entity-set-field
350 'xref (shimbun-header-xref header)))
353 (luna-define-method elmo-folder-msgdb-create ((folder elmo-shimbun-folder)
355 (let ((new-msgdb (elmo-make-msgdb))
356 entity i percent length msgid flags)
357 (setq length (length numlist))
359 (message "Creating msgdb...")
362 (elmo-shimbun-msgdb-create-entity
363 folder (car numlist)))
365 (setq msgid (elmo-message-entity-field entity 'message-id)
366 flags (elmo-flag-table-get flag-table msgid))
367 (elmo-global-flags-set flags folder (car numlist) msgid)
368 (elmo-msgdb-append-entity new-msgdb entity flags))
369 (when (> length elmo-display-progress-threshold)
371 (setq percent (/ (* i 100) length))
372 (elmo-display-progress
373 'elmo-folder-msgdb-create "Creating msgdb..."
375 (setq numlist (cdr numlist)))
376 (message "Creating msgdb...done")
377 (elmo-msgdb-sort-by-date new-msgdb)))
379 (luna-define-method elmo-folder-message-file-p ((folder elmo-shimbun-folder))
382 (defsubst elmo-shimbun-update-overview (folder shimbun-id header)
383 (let ((entity (elmo-msgdb-message-entity (elmo-folder-msgdb folder)
385 (message-id (shimbun-header-id header))
387 (unless (string= shimbun-id message-id)
388 (elmo-message-entity-set-field
389 entity 'x-original-id message-id)
390 (elmo-shimbun-header-set-extra-field
391 header "x-shimbun-id" shimbun-id)
392 (elmo-set-hash-val message-id
394 (elmo-shimbun-folder-entity-hash folder))
395 (elmo-set-hash-val shimbun-id
397 (elmo-shimbun-folder-entity-hash folder)))
398 (elmo-message-entity-set-field
401 (elmo-mime-string (shimbun-header-from header)))
402 (elmo-message-entity-set-field
405 (elmo-mime-string (shimbun-header-subject header)))
406 (elmo-message-entity-set-field
409 (shimbun-header-date header))
410 (when (setq references
411 (or (elmo-msgdb-get-last-message-id
412 (elmo-field-body "in-reply-to"))
413 (elmo-msgdb-get-last-message-id
414 (elmo-field-body "references"))))
415 (elmo-message-entity-set-field
418 (or (elmo-message-entity-field
421 (elmo-shimbun-folder-entity-hash folder))
425 (luna-define-method elmo-map-message-fetch ((folder elmo-shimbun-folder)
427 &optional section unseen)
428 (if (elmo-folder-plugged-p folder)
429 (let ((header (elmo-shimbun-folder-shimbun-header
433 (shimbun-article (elmo-shimbun-folder-shimbun-internal folder)
435 (when (or (eq elmo-shimbun-update-overview-folder-list 'all)
436 (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-message-entity-field ov 'xref)
473 (< (elmo-shimbun-lapse-seconds
474 (elmo-shimbun-parse-time-string
475 (elmo-message-entity-field ov 'date)))
476 (* expire-days 86400 ; seconds per day
479 (elmo-message-entity-field ov 'message-id)))
480 (elmo-folder-list-message-entities folder)))
483 (or (elmo-shimbun-header-extra-field header "x-shimbun-id")
484 (shimbun-header-id header)))
485 (elmo-shimbun-folder-headers-internal folder))))))
487 (luna-define-method elmo-folder-list-subfolders ((folder elmo-shimbun-folder)
489 (let ((prefix (elmo-folder-prefix-internal folder)))
490 (cond ((elmo-shimbun-folder-shimbun-internal folder)
491 (unless (elmo-shimbun-folder-group-internal folder)
496 (elmo-shimbun-folder-shimbun-internal folder))
498 (shimbun-groups (elmo-shimbun-folder-shimbun-internal folder)))))
499 ;; the rest are for "@/" group
502 (lambda (server) (list (concat prefix server)))
503 (shimbun-servers-list)))
506 (dolist (server (shimbun-servers-list))
510 (lambda (fld) (concat prefix server "." fld))
515 (concat prefix server))))
521 (luna-define-method elmo-folder-exists-p ((folder elmo-shimbun-folder))
522 (if (elmo-shimbun-folder-group-internal folder)
525 (elmo-shimbun-folder-group-internal folder)
526 (shimbun-groups (elmo-shimbun-folder-shimbun-internal
531 (product-provide (provide 'elmo-shimbun) (require 'elmo-version))
533 ;;; elmo-shimbun.el ends here