1 ;;; elmo-archive.el --- Archive folder of ELMO.
3 ;; Copyright (C) 1998,1999,2000 OKUNISHI Fujikazu <fuji0924@mbox.kyoto-inet.or.jp>
4 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Author: OKUNISHI Fujikazu <fuji0924@mbox.kyoto-inet.or.jp>
7 ;; Yuuichi Teranishi <teranisi@gohome.org>
8 ;; Keywords: mail, net news
9 ;; Created: Sep 13, 1998
11 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
32 ;; Info-Zip ÀìÍÑ¥¨¡¼¥¸¥§¥ó¥È¤òÍѤ¤¤¿ÆüËܸ측º÷¡ÊOS/2 ÀìÍÑ¡Ë¡£
40 (eval-when-compile (require 'elmo-localdir))
43 (defvar elmo-archive-lha-dos-compatible
44 (memq system-type '(OS/2 emx windows-nt))
45 "*If non-nil, regard your LHA as compatible to DOS version.")
47 (defvar elmo-archive-use-izip-agent (memq system-type '(OS/2 emx))
48 "*If non-nil, use the special agent in fetching headers.")
50 (defvar elmo-archive-folder-path "~/Mail"
51 "*Base directory for archive folders.")
53 (defvar elmo-archive-basename "elmo-archive"
54 "*Common basename of archive folder file, w/o suffix.")
56 (defvar elmo-archive-cmdstr-max-length 8000 ; SASAKI Osamu's suggestion
57 "*Command line string limitation under OS/2, exactly 8190 bytes.")
59 (defvar elmo-archive-fetch-headers-volume 50
60 "*Quantity of article headers to fetch per once.")
62 (defvar elmo-archive-dummy-file ".elmo-archive"
63 "*Name of dummy file that will be appended when the folder is null.")
65 (defvar elmo-archive-check-existance-strict t
66 "*Check existance of archive contents if non-nil.")
68 (defvar elmo-archive-load-hook nil
69 "*Hook called after loading elmo-archive.el.")
71 (defvar elmo-archive-treat-file nil
72 "*Treat archive folder as a file if non-nil.")
74 ;;; User variables for elmo-archive.
75 (defvar elmo-archive-default-type 'zip
76 "*Default archiver type. The value must be a symbol.")
78 (defvar elmo-archive-use-cache nil
79 "Use cache in archive folder.")
81 ;;; ELMO Local directory folder
83 (luna-define-class elmo-archive-folder (elmo-folder)
84 (archive-name archive-type archive-prefix dir-name))
85 (luna-define-internal-accessors 'elmo-archive-folder))
87 (luna-define-generic elmo-archive-folder-path (folder)
88 "Return local directory path of the FOLDER.")
90 (luna-define-method elmo-archive-folder-path ((folder elmo-archive-folder))
91 elmo-archive-folder-path)
93 (luna-define-method elmo-folder-initialize ((folder
96 (elmo-archive-folder-set-dir-name-internal folder name)
98 "^\\([^;]*\\);?\\([^;]*\\);?\\([^;]*\\)$"
100 ;; Drive letter is OK!
101 (or (elmo-archive-folder-set-archive-name-internal
102 folder (elmo-match-string 1 name))
103 (elmo-archive-folder-set-archive-name-internal
105 (or (elmo-archive-folder-set-archive-type-internal
106 folder (intern-soft (elmo-match-string 2 name)))
107 (elmo-archive-folder-set-archive-type-internal
108 folder elmo-archive-default-type))
109 (or (elmo-archive-folder-set-archive-prefix-internal
110 folder (elmo-match-string 3 name))
111 (elmo-archive-folder-set-archive-prefix-internal
115 (luna-define-method elmo-folder-expand-msgdb-path ((folder
116 elmo-archive-folder))
119 (elmo-replace-string-as-filename
120 (elmo-folder-name-internal folder))
121 (expand-file-name (concat (symbol-name (elmo-folder-type-internal folder))
124 (elmo-archive-folder-archive-type-internal
126 elmo-msgdb-directory)))
128 ;;; MMDF parser -- info-zip agent w/ REXX
129 (defvar elmo-mmdf-delimiter "^\01\01\01\01$"
130 "*Regular expression of MMDF delimiter.")
132 (defvar elmo-unixmail-delimiter "^From \\([^ \t]+\\) \\(.+\\)"
133 "*Regular expression of UNIX Mail delimiter.")
135 (defvar elmo-archive-header-regexp "^[ \t]*[-=][-=][-=][-=]"
136 "*Common regexp of the delimiter in listing archive.") ; marche
138 (defvar elmo-archive-file-regexp-alist
140 (if elmo-archive-lha-dos-compatible
141 '((lha . "^%s\\([0-9]+\\)$")) ; OS/2,DOS w/ "-x"
142 '((lha . "^.*[ \t]%s\\([0-9]+\\)$")))
143 '((zip . "^.*[ \t]%s\\([0-9]+\\)$")
144 (zoo . "^.*[ \t]%s\\([0-9]+\\)$")
145 (tar . "^%s\\([0-9]+\\)$") ; ok
146 (tgz . "^%s\\([0-9]+\\)$") ; ok
147 (rar . "^[ \t]%s\\([0-9]+\\)$"))))
149 (defvar elmo-archive-suffix-alist
150 '((lha . ".lzh") ; default
161 (defvar elmo-archive-lha-method-alist
162 (if elmo-archive-lha-dos-compatible
164 '((cp . ("lha" "u" "-x"))
165 (mv . ("lha" "m" "-x"))
167 (ls . ("lha" "l" "-x"))
168 (cat . ("lha" "p" "-n"))
169 (ext . ("lha" "x")) ; "-x"
177 (ext . ("lha" "x")))))
180 (defvar elmo-archive-zip-method-alist
181 '((cp . ("zip" "-9q"))
182 (cp-pipe . ("zip" "-9q@"))
183 (mv . ("zip" "-mDq9"))
184 (mv-pipe . ("zip" "-mDq9@"))
186 (rm-pipe . ("zip" "-dq@"))
187 (ls . ("unzip" "-lq"))
188 (cat . ("unzip" "-pq"))
190 (cat-headers . ("izwlagent" "--cat"))))
193 (defvar elmo-archive-zoo-method-alist
194 '((cp . ("zoo" "aq"))
195 (cp-pipe . ("zoo" "aqI"))
197 (mv-pipe . ("zoo" "aMqI"))
199 (ls . ("zoo" "l")) ; normal
200 (cat . ("zoo" "xpq"))
201 (ext . ("zoo" "xq"))))
204 (defvar elmo-archive-rar-method-alist
205 '((cp . ("rar" "u" "-m5"))
206 (mv . ("rar" "m" "-m5"))
209 (cat . ("rar" "p" "-inul"))
210 (ext . ("rar" "x"))))
213 (defvar elmo-archive-tar-method-alist
214 (if elmo-archive-lha-dos-compatible
215 '((ls . ("gtar" "-tf"))
216 (cat . ("gtar" "--posix Oxf"))
217 (ext . ("gtar" "-xf"))
218 ;;; (rm . ("gtar" "--posix" "--delete" "-f")) ; well not work
220 '((ls . ("gtar" "-tf"))
221 (cat . ("gtar" "-Oxf"))
222 (ext . ("gtar" "-xf"))
223 ;;; (rm . ("gtar" "--delete" "-f")) ;; well not work
226 ;;; GNU tar (*.tar.gz, *.tar.Z, *.tar.bz2)
227 (defvar elmo-archive-tgz-method-alist
228 '((ls . ("gtar" "-ztf"))
229 (cat . ("gtar" "-Ozxf"))
230 (create . ("gtar" "-zcf"))
231 ;;; (rm . elmo-archive-tgz-rm-func)
232 (cp . elmo-archive-tgz-cp-func)
233 (mv . elmo-archive-tgz-mv-func)
234 (ext . ("gtar" "-zxf"))
235 ;; tgz special method
236 (decompress . ("gzip" "-d"))
237 (compress . ("gzip"))
238 (append . ("gtar" "-uf"))
239 ;;; (delete . ("gtar" "--delete" "-f")) ; well not work
242 (defvar elmo-archive-method-list
243 '(elmo-archive-lha-method-alist
244 elmo-archive-zip-method-alist
245 elmo-archive-zoo-method-alist
246 ;;; elmo-archive-tar-method-alist
247 elmo-archive-tgz-method-alist
248 ;;; elmo-archive-arc-method-alist
249 ;;; elmo-archive-arj-method-alist
250 elmo-archive-rar-method-alist))
253 (defvar elmo-archive-method-alist nil)
254 (defvar elmo-archive-suffixes nil)
258 (defmacro elmo-archive-get-method (type action)
259 (` (cdr (assq (, action) (cdr (assq (, type)
260 elmo-archive-method-alist))))))
262 (defmacro elmo-archive-get-suffix (type)
263 (` (cdr (assq (, type)
264 elmo-archive-suffix-alist))))
266 (defmacro elmo-archive-get-regexp (type)
267 (` (cdr (assq (, type)
268 elmo-archive-file-regexp-alist))))
270 (defsubst elmo-archive-call-process (prog args &optional output)
271 (= (apply 'call-process prog nil output nil args) 0))
273 (defsubst elmo-archive-call-method (method args &optional output)
276 (funcall method args output))
278 (elmo-archive-call-process
279 (car method) (append (cdr method) args) output))))
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 (defsubst elmo-archive-list-folder-subr (folder &optional nonsort)
285 "*Returns list of number-file(int, not string) in archive FILE.
286 TYPE specifies the archiver's symbol."
287 (let* ((type (elmo-archive-folder-archive-type-internal folder))
288 (prefix (elmo-archive-folder-archive-prefix-internal folder))
289 (file (elmo-archive-get-archive-name folder))
290 (method (elmo-archive-get-method type 'ls))
292 (file-regexp (format (elmo-archive-get-regexp type)
293 (elmo-concat-path (regexp-quote prefix) "")))
294 (killed (elmo-folder-killed-list-internal folder))
295 numbers buf file-list header-end)
296 (if (file-exists-p file)
298 (unless (elmo-archive-call-method method args t)
299 (error "%s exited abnormally!" method))
300 (goto-char (point-min))
301 (when (re-search-forward elmo-archive-header-regexp nil t)
303 (setq header-end (point))
304 (when (re-search-forward elmo-archive-header-regexp nil t)
306 (narrow-to-region header-end (point))
307 (goto-char (point-min))))
308 (while (and (re-search-forward file-regexp nil t)
309 (not (eobp))) ; for GNU tar 981010
310 (setq file-list (nconc file-list (list (string-to-int
311 (match-string 1)))))))
312 (error "%s does not exist." file))
314 (cons (or (elmo-max-of-list file-list) 0)
316 (- (length file-list)
317 (elmo-msgdb-killed-list-length killed))
319 (setq numbers (sort file-list '<))
320 (elmo-living-messages numbers killed))))
322 (luna-define-method elmo-folder-list-messages-internal ((folder
325 (elmo-archive-list-folder-subr folder))
327 (luna-define-method elmo-folder-status ((folder elmo-archive-folder))
328 (elmo-archive-list-folder-subr folder t))
330 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331 ;;; Folder related functions
333 (defsubst elmo-archive-get-archive-directory (folder)
334 ;; allow fullpath. return format is "/foo/bar/".
335 (if (file-name-absolute-p (elmo-archive-folder-archive-name-internal folder))
336 (if (find-file-name-handler
337 (elmo-archive-folder-archive-name-internal folder)
339 (elmo-archive-folder-archive-name-internal folder)
340 (expand-file-name (elmo-archive-folder-archive-name-internal folder)))
341 (expand-file-name (elmo-archive-folder-archive-name-internal folder)
342 elmo-archive-folder-path)))
344 (defun elmo-archive-get-archive-name (folder)
345 (let ((dir (elmo-archive-get-archive-directory folder))
346 (suffix (elmo-archive-get-suffix
347 (elmo-archive-folder-archive-type-internal
351 (error "Unknown archiver type: %s"
352 (elmo-archive-folder-archive-type-internal folder)))
353 (if elmo-archive-treat-file
354 (if (string-match (concat (regexp-quote suffix) "$")
355 (elmo-archive-folder-archive-name-internal folder))
356 (expand-file-name (elmo-archive-folder-archive-name-internal
358 elmo-archive-folder-path)
359 (expand-file-name (concat (elmo-archive-folder-archive-name-internal
362 elmo-archive-folder-path))
364 "^\\(ange-ftp\\|efs\\)-"
365 (symbol-name (find-file-name-handler dir 'copy-file)))
368 (setq filename (expand-file-name
369 (concat elmo-archive-basename suffix)
371 (elmo-folder-msgdb-path folder))))
372 (if (file-directory-p dbdir)
374 (if (file-exists-p dbdir)
375 (error "File %s already exists" dbdir)
376 (elmo-make-directory dbdir)))
377 (if (not (file-exists-p filename))
379 (if (file-directory-p dir)
381 (concat elmo-archive-basename suffix)
386 (if (or (not (file-exists-p dir))
387 (file-directory-p dir))
389 (concat elmo-archive-basename suffix)
393 (luna-define-method elmo-folder-exists-p ((folder elmo-archive-folder))
394 (file-exists-p (elmo-archive-get-archive-name folder)))
396 (luna-define-method elmo-folder-creatable-p ((folder elmo-archive-folder))
399 (luna-define-method elmo-folder-writable-p ((folder elmo-archive-folder))
402 (luna-define-method elmo-folder-create ((folder elmo-archive-folder))
403 (let* ((dir (directory-file-name ; remove tail slash.
404 (elmo-archive-get-archive-directory folder)))
405 (type (elmo-archive-folder-archive-type-internal folder))
406 (arc (elmo-archive-get-archive-name folder)))
407 (if elmo-archive-treat-file
408 (setq dir (directory-file-name (file-name-directory dir))))
409 (cond ((and (file-exists-p dir)
410 (not (file-directory-p dir)))
412 (error "Create folder failed; File \"%s\" exists" dir))
413 ((file-directory-p dir)
414 (if (file-exists-p arc)
416 (elmo-archive-create-file arc type folder)))
418 (elmo-make-directory dir)
419 (elmo-archive-create-file arc type folder)
422 (defun elmo-archive-create-file (archive type folder)
424 (let* ((tmp-dir (directory-file-name
425 (elmo-folder-msgdb-path folder)))
426 (dummy elmo-archive-dummy-file)
427 (method (or (elmo-archive-get-method type 'create)
428 (elmo-archive-get-method type 'mv)))
429 (args (list archive dummy)))
432 (error "WARNING: read-only mode: %s (method undefined)" type))
434 ((file-directory-p tmp-dir)
436 ((file-exists-p tmp-dir)
438 (error "Create directory failed; File \"%s\" exists" tmp-dir))
440 (elmo-make-directory tmp-dir)))
443 (write-region (point) (point) dummy nil 'no-msg)
445 (elmo-archive-call-method method args)
446 (if (file-exists-p dummy)
447 (delete-file dummy)))
450 (luna-define-method elmo-folder-delete :before ((folder elmo-archive-folder))
451 (let ((arc (elmo-archive-get-archive-name folder)))
452 (if (not (file-exists-p arc))
453 (error "No such file: %s" arc)
457 (luna-define-method elmo-folder-rename-internal ((folder elmo-archive-folder)
459 (let* ((old-arc (elmo-archive-get-archive-name folder))
460 (new-arc (elmo-archive-get-archive-name new-folder)))
461 (unless (and (eq (elmo-archive-folder-archive-type-internal folder)
462 (elmo-archive-folder-archive-type-internal new-folder))
463 (equal (elmo-archive-folder-archive-prefix-internal
465 (elmo-archive-folder-archive-prefix-internal
467 (error "Not same archive type and prefix"))
468 (if (not (file-exists-p old-arc))
469 (error "No such file: %s" old-arc)
470 (if (file-exists-p new-arc)
471 (error "Already exists: %s" new-arc)
472 (rename-file old-arc new-arc)
475 (defun elmo-archive-folder-list-subfolders (folder one-level)
476 (if elmo-archive-treat-file
477 (let* ((path (elmo-archive-get-archive-directory folder))
478 (base-folder (or (elmo-archive-folder-archive-name-internal
481 (suffix (elmo-archive-folder-archive-type-internal folder))
483 (elmo-archive-folder-archive-prefix-internal folder)
487 (elmo-archive-folder-archive-prefix-internal
489 (dir (if (file-directory-p path)
490 path (file-name-directory path)))
491 (name (if (file-directory-p path)
492 "" (file-name-nondirectory path)))
493 (flist (and (file-directory-p dir)
494 (directory-files dir nil
495 (if (> (length name) 0)
496 (concat "^" name "[^A-z][^A-z]")
499 (regexp (format "^\\(.*\\)\\(%s\\)$"
501 '(lambda (x) (regexp-quote (cdr x)))
502 elmo-archive-suffix-alist
504 (if (string-match "\\(.*\\)/$" base-folder) ; ends with '/'.
505 (setq base-folder (elmo-match-string 1 base-folder))
506 (unless (file-directory-p path)
507 (setq base-folder (or (file-name-directory base-folder) ""))))
512 (when (and (string-match regexp x)
515 (rassoc (elmo-match-string 2 x)
516 elmo-archive-suffix-alist))))
518 (elmo-folder-prefix-internal folder)
519 (elmo-concat-path base-folder (elmo-match-string 1 x))
522 (elmo-mapcar-list-of-list
523 (function (lambda (x)
526 (concat elmo-archive-basename
527 (elmo-archive-get-suffix
528 (elmo-archive-folder-archive-type-internal
532 (elmo-archive-folder-path folder))))
533 (concat (elmo-folder-prefix-internal folder) x))))
534 (elmo-list-subdirectories
535 (elmo-archive-folder-path folder)
536 (or (elmo-archive-folder-dir-name-internal folder) "")
539 (luna-define-method elmo-folder-list-subfolders ((folder elmo-archive-folder)
541 (elmo-archive-folder-list-subfolders folder one-level))
543 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
544 ;;; Article file related functions
545 ;;; read(extract) / append(move) / delete(delete) / query(list)
547 (defsubst elmo-archive-message-fetch-internal (folder number)
548 (let* ((type (elmo-archive-folder-archive-type-internal folder))
549 (arc (elmo-archive-get-archive-name folder))
550 (prefix (elmo-archive-folder-archive-prefix-internal folder))
551 (method (elmo-archive-get-method type 'cat))
552 (args (list arc (elmo-concat-path
553 prefix (int-to-string number)))))
554 (and (file-exists-p arc)
556 (elmo-archive-call-method method args t))
558 (elmo-delete-cr-buffer)
561 (luna-define-method elmo-message-fetch-internal ((folder elmo-archive-folder)
563 &optional section unseen)
564 (elmo-archive-message-fetch-internal folder number))
566 (luna-define-method elmo-folder-append-buffer ((folder elmo-archive-folder)
567 unread &optional number)
568 (elmo-archive-folder-append-buffer folder unread number))
571 (defun elmo-archive-folder-append-buffer (folder unread number)
572 (let* ((type (elmo-archive-folder-archive-type-internal folder))
573 (prefix (elmo-archive-folder-archive-prefix-internal folder))
574 (arc (elmo-archive-get-archive-name folder))
575 (method (elmo-archive-get-method type 'mv))
577 (1+ (if (file-exists-p arc)
579 (elmo-folder-status folder)) 0))))
580 (tmp-dir (elmo-folder-msgdb-path folder))
581 (src-buffer (current-buffer))
586 (error "WARNING: read-only mode: %s (method undefined)" type))
588 (let ((tmp-dir (expand-file-name prefix tmp-dir)))
589 (when (not (file-directory-p tmp-dir))
590 (elmo-make-directory (directory-file-name tmp-dir))))
591 (setq newfile (elmo-concat-path
593 (int-to-string next-num)))
597 (if (and (or (functionp method) (car method))
598 (file-writable-p newfile))
600 (setq dst-buffer (current-buffer))
601 (with-current-buffer src-buffer
602 (copy-to-buffer dst-buffer (point-min) (point-max)))
603 (as-binary-output-file
604 (write-region (point-min) (point-max) newfile nil 'no-msg))
605 (elmo-archive-call-method method (list arc newfile))
609 (luna-define-method elmo-folder-append-messages :around
610 ((folder elmo-archive-folder) src-folder numbers unread-marks
611 &optional same-number)
612 (let ((prefix (elmo-archive-folder-archive-prefix-internal folder)))
616 (elmo-folder-message-file-p src-folder)
617 (elmo-folder-message-file-number-p src-folder))
618 ;; same-number(localdir, localnews) -> archive
619 (elmo-archive-append-files folder
620 (elmo-folder-message-file-directory src-folder)
622 (elmo-progress-notify 'elmo-folder-move-messages (length numbers))
624 ((elmo-folder-message-make-temp-file-p src-folder)
625 ;; not-same-number (localdir, localnews), (archive maildir) -> archive
626 (let ((temp-dir (elmo-folder-message-make-temp-files
630 (1+ (if (file-exists-p (elmo-archive-get-archive-name
632 (car (elmo-folder-status folder)) 0)))))
633 new-dir base-dir files)
634 (setq base-dir temp-dir)
635 (when (> (length prefix) 0)
636 (when (file-name-directory prefix)
637 (elmo-make-directory (file-name-directory prefix)))
643 ;; parent of temp-dir..(works in windows?)
644 (expand-file-name ".." temp-dir))))
645 ;; now temp-dir has name prefix.
646 (setq temp-dir new-dir)
647 ;; parent of prefix becomes base-dir.
648 (setq base-dir (expand-file-name ".." temp-dir)))
651 '(lambda (x) (elmo-concat-path prefix x))
652 (directory-files temp-dir nil "^[^\\.]")))
653 (if (elmo-archive-append-files folder
656 (elmo-delete-directory temp-dir)))
657 (elmo-progress-notify 'elmo-folder-move-messages (length numbers))
659 (t (luna-call-next-method)))))
661 (luna-define-method elmo-folder-message-make-temp-file-p
662 ((folder elmo-archive-folder))
663 (let ((type (elmo-archive-folder-archive-type-internal folder)))
664 (or (elmo-archive-get-method type 'ext-pipe)
665 (elmo-archive-get-method type 'ext))))
667 (luna-define-method elmo-folder-message-make-temp-files
668 ((folder elmo-archive-folder) numbers
669 &optional start-number)
670 (elmo-archive-folder-message-make-temp-files folder numbers start-number))
672 (defun elmo-archive-folder-message-make-temp-files (folder
675 (let* ((tmp-dir-src (elmo-folder-make-temporary-directory folder))
676 (tmp-dir-dst (elmo-folder-make-temporary-directory folder))
677 (arc (elmo-archive-get-archive-name folder))
678 (type (elmo-archive-folder-archive-type-internal folder))
679 (prefix (elmo-archive-folder-archive-prefix-internal folder))
680 (p-method (elmo-archive-get-method type 'ext-pipe))
681 (n-method (elmo-archive-get-method type 'ext))
682 (tmp-msgs (mapcar (lambda (x) (elmo-concat-path
684 (int-to-string x))) numbers))
686 ;; Expand files in the tmp-dir-src.
690 ((functionp n-method)
691 (funcall n-method (cons arc tmp-msgs)))
693 (let ((p-prog (car p-method))
694 (p-prog-arg (cdr p-method)))
695 (elmo-archive-exec-msgs-subr1
696 p-prog (append p-prog-arg (list arc)) tmp-msgs)))
698 (let ((n-prog (car n-method))
699 (n-prog-arg (cdr n-method)))
700 (elmo-archive-exec-msgs-subr2
701 n-prog (append n-prog-arg (list arc)) tmp-msgs
703 ;; Move files to the tmp-dir-dst.
704 (setq number start-number)
705 (dolist (tmp-file tmp-msgs)
706 (rename-file (expand-file-name
711 (int-to-string number)
712 (file-name-nondirectory tmp-file))
714 (if start-number (incf number)))
715 ;; Remove tmp-dir-src.
716 (elmo-delete-directory tmp-dir-src)
717 ;; tmp-dir-dst is the return directory.
720 (defun elmo-archive-append-files (folder dir &optional files)
721 (let* ((dst-type (elmo-archive-folder-archive-type-internal folder))
722 (arc (elmo-archive-get-archive-name folder))
723 (prefix (elmo-archive-folder-archive-prefix-internal folder))
724 (p-method (elmo-archive-get-method dst-type 'cp-pipe))
725 (n-method (elmo-archive-get-method dst-type 'cp))
727 (unless (elmo-folder-exists-p folder) (elmo-folder-create folder))
728 (unless files (setq files (directory-files dir nil "^[^\\.]")))
729 (when (null (or p-method n-method))
731 (error "WARNING: read-only mode: %s (method undefined)" dst-type))
736 ((functionp n-method)
737 (funcall n-method (cons arc files)))
739 (let ((p-prog (car p-method))
740 (p-prog-arg (cdr p-method)))
741 (elmo-archive-exec-msgs-subr1
742 p-prog (append p-prog-arg (list arc)) files)))
744 (let ((n-prog (car n-method))
745 (n-prog-arg (cdr n-method)))
746 (elmo-archive-exec-msgs-subr2
747 n-prog (append n-prog-arg (list arc)) files (length arc)))))))))
749 (luna-define-method elmo-folder-delete-messages ((folder elmo-archive-folder)
751 (let* ((type (elmo-archive-folder-archive-type-internal folder))
752 (prefix (elmo-archive-folder-archive-prefix-internal folder))
753 (arc (elmo-archive-get-archive-name folder))
754 (p-method (elmo-archive-get-method type 'rm-pipe))
755 (n-method (elmo-archive-get-method type 'rm))
756 (numbers (mapcar '(lambda (x) (elmo-concat-path
760 (cond ((functionp n-method)
761 (funcall n-method (cons arc numbers)))
763 (let ((p-prog (car p-method))
764 (p-prog-arg (cdr p-method)))
765 (elmo-archive-exec-msgs-subr1
766 p-prog (append p-prog-arg (list arc)) numbers)))
768 (let ((n-prog (car n-method))
769 (n-prog-arg (cdr n-method)))
770 (elmo-archive-exec-msgs-subr2
771 n-prog (append n-prog-arg (list arc)) numbers (length arc))))
774 (error "WARNING: not delete: %s (method undefined)" type)))))
776 (defun elmo-archive-exec-msgs-subr1 (prog args msgs)
778 (insert (mapconcat 'concat msgs "\n")) ;string
779 (= 0 (apply 'call-process-region (point-min) (point-max)
780 prog nil nil nil args))))
782 (defun elmo-archive-exec-msgs-subr2 (prog args msgs arc-length)
783 (let ((max-len (- elmo-archive-cmdstr-max-length arc-length))
786 (setq rest msgs) ;string
790 (while (and rest (<= i n))
792 (let* ((len (length x))
793 (files (member x (reverse rest))))
794 ;; total(previous) + current + white space
795 (if (<= max-len (+ sum len 1))
798 (elmo-archive-call-process
799 prog (append args files))
801 (setq sum 0) ;; reset
802 (setq rest (nthcdr i rest)))
803 (setq sum (+ sum len 1)))
804 (setq i (1+ i)))) msgs))
807 (elmo-archive-call-process prog (append args rest))))
810 (defsubst elmo-archive-article-exists-p (arc msg type)
811 (if (not elmo-archive-check-existance-strict)
813 (save-excursion ; added 980915
814 (let* ((method (elmo-archive-get-method type 'ls))
815 (args (list arc msg))
816 (buf (get-buffer-create " *ELMO ARCHIVE query*"))
817 (error-msg "\\(no file\\|0 files\\)")
821 (elmo-archive-call-method method args t)
822 ;; pointer: point-max
823 (setq ret-val (not (re-search-backward error-msg nil t)))
827 (defun elmo-archive-tgz-common-func (args exec-type &optional copy)
828 (let* ((arc (car args))
829 (tmp-msgs (cdr args))
830 (decompress (elmo-archive-get-method 'tgz 'decompress))
831 (compress (elmo-archive-get-method 'tgz 'compress))
832 (exec (elmo-archive-get-method 'tgz exec-type))
833 (suffix (elmo-archive-get-suffix 'tgz))
834 (tar-suffix (elmo-archive-get-suffix 'tar))
837 (when (null (and decompress compress exec))
839 (error "WARNING: special method undefined: %s of %s"
840 (or (if (null decompress) 'decompress)
841 (if (null compress) 'compress)
842 (if (null exec) exec-type))
846 (error "WARNING: `tar' suffix undefined"))
847 (if (string-match (concat (regexp-quote suffix) "$") arc)
849 (concat (substring arc 0 (match-beginning 0)) tar-suffix))
850 (error "%s: not match suffix [%s]" arc suffix))
853 (elmo-archive-call-process
854 (car decompress) (append (cdr decompress) (list arc)))
855 ;; append (or delete)
856 (elmo-archive-exec-msgs-subr2
857 (car exec) (append (cdr exec) (list arc-tar)) tmp-msgs (length arc-tar))
860 (elmo-archive-call-process
861 (car compress) (append (cdr compress) (list arc-tar)))))
862 ;; delete temporary messages
864 (eq exec-type 'append))
866 (if (file-exists-p (car tmp-msgs))
867 (delete-file (car tmp-msgs)))
868 (setq tmp-msgs (cdr tmp-msgs))))
871 (defun elmo-archive-tgz-cp-func (args &optional output)
872 (elmo-archive-tgz-common-func args 'append t))
874 (defun elmo-archive-tgz-mv-func (args &optional output)
875 (elmo-archive-tgz-common-func args 'append))
877 (defun elmo-archive-tgz-rm-func (args &optional output)
878 (elmo-archive-tgz-common-func args 'delete))
880 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
881 ;;; MessageDB functions (from elmo-localdir.el)
883 (defsubst elmo-archive-msgdb-create-entity-subr (number)
885 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
886 (goto-char (point-min))
887 (if (re-search-forward "\\(^--.*$\\)\\|\\(\n\n\\)" nil t)
888 (setq header-end (point))
889 (setq header-end (point-max)))
890 (narrow-to-region (point-min) header-end)
891 (elmo-msgdb-create-overview-from-buffer number)))
894 (defsubst elmo-archive-msgdb-create-entity (method archive number type &optional prefix)
895 (let* ((msg (elmo-concat-path prefix (int-to-string number)))
896 (arg-list (list archive msg)))
897 (when (elmo-archive-article-exists-p archive msg type)
900 (elmo-archive-call-method method arg-list t))
901 (elmo-archive-msgdb-create-entity-subr number))))
903 (luna-define-method elmo-folder-msgdb-create ((folder elmo-archive-folder)
905 already-mark seen-mark
906 important-mark seen-list)
908 (save-excursion ;; 981005
909 (if (and elmo-archive-use-izip-agent
910 (elmo-archive-get-method
911 (elmo-archive-folder-archive-type-internal folder)
913 (elmo-archive-msgdb-create-as-numlist-subr2
914 folder numbers new-mark already-mark seen-mark important-mark
916 (elmo-archive-msgdb-create-as-numlist-subr1
917 folder numbers new-mark already-mark seen-mark important-mark
920 (defun elmo-archive-msgdb-create-as-numlist-subr1 (folder
922 already-mark seen-mark
925 (let* ((type (elmo-archive-folder-archive-type-internal folder))
926 (file (elmo-archive-get-archive-name folder))
927 (method (elmo-archive-get-method type 'cat))
928 overview number-alist mark-alist entity
929 i percent num message-id seen gmark)
931 (setq num (length numlist))
933 (message "Creating msgdb...")
937 (elmo-archive-msgdb-create-entity
938 method file (car numlist) type
939 (elmo-archive-folder-archive-prefix-internal folder)))
942 (elmo-msgdb-append-element
945 (elmo-msgdb-number-add
947 (elmo-msgdb-overview-entity-get-number entity)
949 (setq message-id (car entity))
950 (setq seen (member message-id seen-list))
952 (or (elmo-msgdb-global-mark-get message-id)
953 (if (elmo-file-cache-status
954 (elmo-file-cache-get message-id))
962 (elmo-msgdb-mark-append
964 (elmo-msgdb-overview-entity-get-number entity)
966 (when (> num elmo-display-progress-threshold)
968 (setq percent (/ (* i 100) num))
969 (elmo-display-progress
970 'elmo-archive-msgdb-create-as-numlist-subr1 "Creating msgdb..."
972 (setq numlist (cdr numlist)))
973 (message "Creating msgdb...done")
974 (list overview number-alist mark-alist))))
977 (defun elmo-archive-msgdb-create-as-numlist-subr2 (folder
979 already-mark seen-mark
982 (let* ((delim1 elmo-mmdf-delimiter) ;; MMDF
983 (delim2 elmo-unixmail-delimiter) ;; UNIX Mail
984 (type (elmo-archive-folder-archive-type-internal folder))
985 (prefix (elmo-archive-folder-archive-prefix-internal folder))
986 (method (elmo-archive-get-method type 'cat-headers))
989 (arc (elmo-archive-get-archive-name folder))
990 n i percent num result overview number-alist mark-alist
991 msgs case-fold-search)
993 (setq num (length numlist))
995 (message "Creating msgdb...")
997 (setq n (min (1- elmo-archive-fetch-headers-volume)
998 (1- (length numlist))))
999 (setq msgs (reverse (memq (nth n numlist) (reverse numlist))))
1000 (setq numlist (nthcdr (1+ n) numlist))
1005 (mapcar '(lambda (x) (elmo-concat-path prefix (int-to-string x))) msgs)
1007 (message "Fetching headers...")
1008 (as-binary-process (apply 'call-process-region
1009 (point-min) (point-max)
1010 prog t t nil (append args (list arc))))
1011 (goto-char (point-min))
1013 ((looking-at delim1) ;; MMDF
1014 (setq result (elmo-archive-parse-mmdf msgs
1016 already-mark seen-mark
1018 (setq overview (append overview (nth 0 result)))
1019 (setq number-alist (append number-alist (nth 1 result)))
1020 (setq mark-alist (append mark-alist (nth 2 result))))
1021 ;;; ((looking-at delim2) ;; UNIX MAIL
1022 ;;; (setq result (elmo-archive-parse-unixmail msgs))
1023 ;;; (setq overview (append overview (nth 0 result)))
1024 ;;; (setq number-alist (append number-alist (nth 1 result)))
1025 ;;; (setq mark-alist (append mark-alist (nth 2 result))))
1026 (t ;; unknown format
1027 (error "Unknown format!")))
1028 (when (> num elmo-display-progress-threshold)
1030 (setq percent (/ (* i 100) num))
1031 (elmo-display-progress
1032 'elmo-archive-msgdb-create-as-numlist-subr2 "Creating msgdb..."
1034 (list overview number-alist mark-alist)))
1036 (defun elmo-archive-parse-mmdf (msgs new-mark
1040 (let ((delim elmo-mmdf-delimiter)
1041 number sp ep rest entity overview number-alist mark-alist ret-val
1042 message-id seen gmark)
1043 (goto-char (point-min))
1045 (while (and rest (re-search-forward delim nil t)
1047 (setq number (car rest))
1048 (setq sp (1+ (point)))
1049 (setq ep (prog2 (re-search-forward delim)
1050 (1+ (- (point) (length delim)))))
1051 (if (>= sp ep) ; no article!
1054 (narrow-to-region sp ep)
1055 (setq entity (elmo-archive-msgdb-create-entity-subr number))
1057 (elmo-msgdb-append-element
1060 (elmo-msgdb-number-add
1062 (elmo-msgdb-overview-entity-get-number entity)
1064 (setq message-id (car entity))
1065 (setq seen (member message-id seen-list))
1067 (or (elmo-msgdb-global-mark-get message-id)
1068 (if (elmo-file-cache-status
1069 (elmo-file-cache-get message-id))
1077 (elmo-msgdb-mark-append
1079 (elmo-msgdb-overview-entity-get-number entity)
1081 (setq ret-val (append ret-val (list overview number-alist mark-alist)))
1084 (setq rest (cdr rest)))
1088 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1089 ;;; Search functions
1091 (defsubst elmo-archive-field-condition-match (folder number number-list
1094 (let* ((type (elmo-archive-folder-archive-type-internal folder))
1095 (arc (elmo-archive-get-archive-name folder))
1096 (method (elmo-archive-get-method type 'cat))
1097 (args (list arc (elmo-concat-path prefix (int-to-string number)))))
1099 (when (file-exists-p arc)
1101 (elmo-archive-call-method method args t))
1102 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
1103 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
1104 (elmo-buffer-field-condition-match condition number number-list))))))
1106 (luna-define-method elmo-folder-search ((folder elmo-archive-folder)
1107 condition &optional from-msgs)
1108 (let* (;;(args (elmo-string-to-list key))
1109 ;; XXX: I don't know whether `elmo-archive-list-folder'
1110 ;; updates match-data.
1111 ;; (msgs (or from-msgs (elmo-archive-list-folder spec)))
1112 (msgs (or from-msgs (elmo-folder-list-messages folder)))
1115 (case-fold-search nil)
1116 number-list ret-val)
1117 (setq number-list msgs)
1119 (if (elmo-archive-field-condition-match
1120 folder (car msgs) number-list
1122 (elmo-archive-folder-archive-prefix-internal folder))
1123 (setq ret-val (cons (car msgs) ret-val)))
1124 (when (> num elmo-display-progress-threshold)
1126 (elmo-display-progress
1127 'elmo-archive-search "Searching..."
1129 (setq msgs (cdr msgs)))
1130 (nreverse ret-val)))
1133 (if (null elmo-archive-method-alist)
1134 (let ((mlist elmo-archive-method-list) ; from mew-highlight.el
1137 (setq method (car mlist))
1138 (setq mlist (cdr mlist))
1139 (setq str (symbol-name method))
1140 (string-match "elmo-archive-\\([^-].*\\)-method-alist$" str)
1141 (setq type (intern-soft
1142 (elmo-match-string 1 str)))
1143 (setq elmo-archive-method-alist
1145 (symbol-value method))
1146 elmo-archive-method-alist)))))
1148 ;;; valid suffix(list)
1149 (if (null elmo-archive-suffixes)
1150 (let ((slist elmo-archive-suffix-alist)
1153 (setq tmp (car slist))
1154 (setq elmo-archive-suffixes
1155 (nconc elmo-archive-suffixes (list (cdr tmp))))
1156 (setq slist (cdr slist)))))
1158 (luna-define-method elmo-message-use-cache-p ((folder elmo-archive-folder)
1160 elmo-archive-use-cache)
1163 (run-hooks 'elmo-archive-load-hook)
1166 (product-provide (provide 'elmo-archive) (require 'elmo-version))
1168 ;;; elmo-archive.el ends here