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 &optional same-number)
611 (let ((prefix (elmo-archive-folder-archive-prefix-internal folder)))
615 (elmo-folder-message-file-p src-folder)
616 (elmo-folder-message-file-number-p src-folder))
617 ;; same-number(localdir, localnews) -> archive
618 (elmo-archive-append-files folder
619 (elmo-folder-message-file-directory src-folder)
621 (elmo-progress-notify 'elmo-folder-move-messages (length numbers))
623 ((elmo-folder-message-make-temp-file-p src-folder)
624 ;; not-same-number (localdir, localnews), (archive maildir) -> archive
625 (let ((temp-dir (elmo-folder-message-make-temp-files
629 (1+ (if (file-exists-p (elmo-archive-get-archive-name
631 (car (elmo-folder-status folder)) 0)))))
632 new-dir base-dir files)
633 (setq base-dir temp-dir)
634 (when (> (length prefix) 0)
635 (when (file-name-directory prefix)
636 (elmo-make-directory (file-name-directory prefix)))
642 ;; parent of temp-dir..(works in windows?)
643 (expand-file-name ".." temp-dir))))
644 ;; now temp-dir has name prefix.
645 (setq temp-dir new-dir)
646 ;; parent of prefix becomes base-dir.
647 (setq base-dir (expand-file-name ".." temp-dir)))
650 '(lambda (x) (elmo-concat-path prefix x))
651 (directory-files temp-dir nil "^[^\\.]")))
652 (if (elmo-archive-append-files folder
655 (elmo-delete-directory temp-dir)))
656 (elmo-progress-notify 'elmo-folder-move-messages (length numbers))
658 (t (luna-call-next-method)))))
660 (luna-define-method elmo-folder-message-make-temp-file-p
661 ((folder elmo-archive-folder))
662 (let ((type (elmo-archive-folder-archive-type-internal folder)))
663 (or (elmo-archive-get-method type 'ext-pipe)
664 (elmo-archive-get-method type 'ext))))
666 (luna-define-method elmo-folder-message-make-temp-files
667 ((folder elmo-archive-folder) numbers
668 &optional start-number)
669 (elmo-archive-folder-message-make-temp-files folder numbers start-number))
671 (defun elmo-archive-folder-message-make-temp-files (folder
674 (let* ((tmp-dir-src (elmo-folder-make-temporary-directory folder))
675 (tmp-dir-dst (elmo-folder-make-temporary-directory folder))
676 (arc (elmo-archive-get-archive-name folder))
677 (type (elmo-archive-folder-archive-type-internal folder))
678 (prefix (elmo-archive-folder-archive-prefix-internal folder))
679 (p-method (elmo-archive-get-method type 'ext-pipe))
680 (n-method (elmo-archive-get-method type 'ext))
681 (tmp-msgs (mapcar (lambda (x) (elmo-concat-path
683 (int-to-string x))) numbers))
685 ;; Expand files in the tmp-dir-src.
689 ((functionp n-method)
690 (funcall n-method (cons arc tmp-msgs)))
692 (let ((p-prog (car p-method))
693 (p-prog-arg (cdr p-method)))
694 (elmo-archive-exec-msgs-subr1
695 p-prog (append p-prog-arg (list arc)) tmp-msgs)))
697 (let ((n-prog (car n-method))
698 (n-prog-arg (cdr n-method)))
699 (elmo-archive-exec-msgs-subr2
700 n-prog (append n-prog-arg (list arc)) tmp-msgs
702 ;; Move files to the tmp-dir-dst.
703 (setq number start-number)
704 (dolist (tmp-file tmp-msgs)
705 (rename-file (expand-file-name
710 (int-to-string number)
711 (file-name-nondirectory tmp-file))
713 (if start-number (incf number)))
714 ;; Remove tmp-dir-src.
715 (elmo-delete-directory tmp-dir-src)
716 ;; tmp-dir-dst is the return directory.
719 (defun elmo-archive-append-files (folder dir &optional files)
720 (let* ((dst-type (elmo-archive-folder-archive-type-internal folder))
721 (arc (elmo-archive-get-archive-name folder))
722 (prefix (elmo-archive-folder-archive-prefix-internal folder))
723 (p-method (elmo-archive-get-method dst-type 'cp-pipe))
724 (n-method (elmo-archive-get-method dst-type 'cp))
726 (unless (elmo-folder-exists-p folder) (elmo-folder-create folder))
727 (unless files (setq files (directory-files dir nil "^[^\\.]")))
728 (when (null (or p-method n-method))
730 (error "WARNING: read-only mode: %s (method undefined)" dst-type))
735 ((functionp n-method)
736 (funcall n-method (cons arc files)))
738 (let ((p-prog (car p-method))
739 (p-prog-arg (cdr p-method)))
740 (elmo-archive-exec-msgs-subr1
741 p-prog (append p-prog-arg (list arc)) files)))
743 (let ((n-prog (car n-method))
744 (n-prog-arg (cdr n-method)))
745 (elmo-archive-exec-msgs-subr2
746 n-prog (append n-prog-arg (list arc)) files (length arc)))))))))
748 (luna-define-method elmo-folder-delete-messages ((folder elmo-archive-folder)
750 (let* ((type (elmo-archive-folder-archive-type-internal folder))
751 (prefix (elmo-archive-folder-archive-prefix-internal folder))
752 (arc (elmo-archive-get-archive-name folder))
753 (p-method (elmo-archive-get-method type 'rm-pipe))
754 (n-method (elmo-archive-get-method type 'rm))
755 (numbers (mapcar '(lambda (x) (elmo-concat-path
759 (cond ((functionp n-method)
760 (funcall n-method (cons arc numbers)))
762 (let ((p-prog (car p-method))
763 (p-prog-arg (cdr p-method)))
764 (elmo-archive-exec-msgs-subr1
765 p-prog (append p-prog-arg (list arc)) numbers)))
767 (let ((n-prog (car n-method))
768 (n-prog-arg (cdr n-method)))
769 (elmo-archive-exec-msgs-subr2
770 n-prog (append n-prog-arg (list arc)) numbers (length arc))))
773 (error "WARNING: not delete: %s (method undefined)" type)))))
775 (defun elmo-archive-exec-msgs-subr1 (prog args msgs)
777 (insert (mapconcat 'concat msgs "\n")) ;string
778 (= 0 (apply 'call-process-region (point-min) (point-max)
779 prog nil nil nil args))))
781 (defun elmo-archive-exec-msgs-subr2 (prog args msgs arc-length)
782 (let ((max-len (- elmo-archive-cmdstr-max-length arc-length))
785 (setq rest msgs) ;string
789 (while (and rest (<= i n))
791 (let* ((len (length x))
792 (files (member x (reverse rest))))
793 ;; total(previous) + current + white space
794 (if (<= max-len (+ sum len 1))
797 (elmo-archive-call-process
798 prog (append args files))
800 (setq sum 0) ;; reset
801 (setq rest (nthcdr i rest)))
802 (setq sum (+ sum len 1)))
803 (setq i (1+ i)))) msgs))
806 (elmo-archive-call-process prog (append args rest))))
809 (defsubst elmo-archive-article-exists-p (arc msg type)
810 (if (not elmo-archive-check-existance-strict)
812 (save-excursion ; added 980915
813 (let* ((method (elmo-archive-get-method type 'ls))
814 (args (list arc msg))
815 (buf (get-buffer-create " *ELMO ARCHIVE query*"))
816 (error-msg "\\(no file\\|0 files\\)")
820 (elmo-archive-call-method method args t)
821 ;; pointer: point-max
822 (setq ret-val (not (re-search-backward error-msg nil t)))
826 (defun elmo-archive-tgz-common-func (args exec-type &optional copy)
827 (let* ((arc (car args))
828 (tmp-msgs (cdr args))
829 (decompress (elmo-archive-get-method 'tgz 'decompress))
830 (compress (elmo-archive-get-method 'tgz 'compress))
831 (exec (elmo-archive-get-method 'tgz exec-type))
832 (suffix (elmo-archive-get-suffix 'tgz))
833 (tar-suffix (elmo-archive-get-suffix 'tar))
836 (when (null (and decompress compress exec))
838 (error "WARNING: special method undefined: %s of %s"
839 (or (if (null decompress) 'decompress)
840 (if (null compress) 'compress)
841 (if (null exec) exec-type))
845 (error "WARNING: `tar' suffix undefined"))
846 (if (string-match (concat (regexp-quote suffix) "$") arc)
848 (concat (substring arc 0 (match-beginning 0)) tar-suffix))
849 (error "%s: not match suffix [%s]" arc suffix))
852 (elmo-archive-call-process
853 (car decompress) (append (cdr decompress) (list arc)))
854 ;; append (or delete)
855 (elmo-archive-exec-msgs-subr2
856 (car exec) (append (cdr exec) (list arc-tar)) tmp-msgs (length arc-tar))
859 (elmo-archive-call-process
860 (car compress) (append (cdr compress) (list arc-tar)))))
861 ;; delete temporary messages
863 (eq exec-type 'append))
865 (if (file-exists-p (car tmp-msgs))
866 (delete-file (car tmp-msgs)))
867 (setq tmp-msgs (cdr tmp-msgs))))
870 (defun elmo-archive-tgz-cp-func (args &optional output)
871 (elmo-archive-tgz-common-func args 'append t))
873 (defun elmo-archive-tgz-mv-func (args &optional output)
874 (elmo-archive-tgz-common-func args 'append))
876 (defun elmo-archive-tgz-rm-func (args &optional output)
877 (elmo-archive-tgz-common-func args 'delete))
879 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
880 ;;; MessageDB functions (from elmo-localdir.el)
882 (defsubst elmo-archive-msgdb-create-entity-subr (number)
884 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
885 (goto-char (point-min))
886 (if (re-search-forward "\\(^--.*$\\)\\|\\(\n\n\\)" nil t)
887 (setq header-end (point))
888 (setq header-end (point-max)))
889 (narrow-to-region (point-min) header-end)
890 (elmo-msgdb-create-overview-from-buffer number)))
893 (defsubst elmo-archive-msgdb-create-entity (method archive number type &optional prefix)
894 (let* ((msg (elmo-concat-path prefix (int-to-string number)))
895 (arg-list (list archive msg)))
896 (when (elmo-archive-article-exists-p archive msg type)
899 (elmo-archive-call-method method arg-list t))
900 (elmo-archive-msgdb-create-entity-subr number))))
902 (luna-define-method elmo-folder-msgdb-create ((folder elmo-archive-folder)
905 (save-excursion ;; 981005
906 (if (and elmo-archive-use-izip-agent
907 (elmo-archive-get-method
908 (elmo-archive-folder-archive-type-internal folder)
910 (elmo-archive-msgdb-create-as-numlist-subr2
911 folder numbers seen-list)
912 (elmo-archive-msgdb-create-as-numlist-subr1
913 folder numbers seen-list)))))
915 (defun elmo-archive-msgdb-create-as-numlist-subr1 (folder numlist seen-list)
916 (let* ((type (elmo-archive-folder-archive-type-internal folder))
917 (file (elmo-archive-get-archive-name folder))
918 (method (elmo-archive-get-method type 'cat))
919 overview number-alist mark-alist entity
920 i percent num message-id seen gmark)
922 (setq num (length numlist))
924 (message "Creating msgdb...")
928 (elmo-archive-msgdb-create-entity
929 method file (car numlist) type
930 (elmo-archive-folder-archive-prefix-internal folder)))
933 (elmo-msgdb-append-element
936 (elmo-msgdb-number-add
938 (elmo-msgdb-overview-entity-get-number entity)
940 (setq message-id (car entity))
941 (setq seen (member message-id seen-list))
943 (or (elmo-msgdb-global-mark-get message-id)
944 (if (elmo-file-cache-status
945 (elmo-file-cache-get message-id))
948 elmo-msgdb-unread-cached-mark)
950 elmo-msgdb-read-uncached-mark
951 elmo-msgdb-new-mark))))
953 (elmo-msgdb-mark-append
955 (elmo-msgdb-overview-entity-get-number entity)
957 (when (> num elmo-display-progress-threshold)
959 (setq percent (/ (* i 100) num))
960 (elmo-display-progress
961 'elmo-archive-msgdb-create-as-numlist-subr1 "Creating msgdb..."
963 (setq numlist (cdr numlist)))
964 (message "Creating msgdb...done")
965 (list overview number-alist mark-alist))))
968 (defun elmo-archive-msgdb-create-as-numlist-subr2 (folder
971 (let* ((delim1 elmo-mmdf-delimiter) ;; MMDF
972 (delim2 elmo-unixmail-delimiter) ;; UNIX Mail
973 (type (elmo-archive-folder-archive-type-internal folder))
974 (prefix (elmo-archive-folder-archive-prefix-internal folder))
975 (method (elmo-archive-get-method type 'cat-headers))
978 (arc (elmo-archive-get-archive-name folder))
979 n i percent num result overview number-alist mark-alist
980 msgs case-fold-search)
982 (setq num (length numlist))
984 (message "Creating msgdb...")
986 (setq n (min (1- elmo-archive-fetch-headers-volume)
987 (1- (length numlist))))
988 (setq msgs (reverse (memq (nth n numlist) (reverse numlist))))
989 (setq numlist (nthcdr (1+ n) numlist))
994 (mapcar '(lambda (x) (elmo-concat-path prefix (int-to-string x))) msgs)
996 (message "Fetching headers...")
997 (as-binary-process (apply 'call-process-region
998 (point-min) (point-max)
999 prog t t nil (append args (list arc))))
1000 (goto-char (point-min))
1002 ((looking-at delim1) ;; MMDF
1003 (setq result (elmo-archive-parse-mmdf msgs seen-list))
1004 (setq overview (append overview (nth 0 result)))
1005 (setq number-alist (append number-alist (nth 1 result)))
1006 (setq mark-alist (append mark-alist (nth 2 result))))
1007 ;;; ((looking-at delim2) ;; UNIX MAIL
1008 ;;; (setq result (elmo-archive-parse-unixmail msgs))
1009 ;;; (setq overview (append overview (nth 0 result)))
1010 ;;; (setq number-alist (append number-alist (nth 1 result)))
1011 ;;; (setq mark-alist (append mark-alist (nth 2 result))))
1012 (t ;; unknown format
1013 (error "Unknown format!")))
1014 (when (> num elmo-display-progress-threshold)
1016 (setq percent (/ (* i 100) num))
1017 (elmo-display-progress
1018 'elmo-archive-msgdb-create-as-numlist-subr2 "Creating msgdb..."
1020 (list overview number-alist mark-alist)))
1022 (defun elmo-archive-parse-mmdf (msgs seen-list)
1023 (let ((delim elmo-mmdf-delimiter)
1024 number sp ep rest entity overview number-alist mark-alist ret-val
1025 message-id seen gmark)
1026 (goto-char (point-min))
1028 (while (and rest (re-search-forward delim nil t)
1030 (setq number (car rest))
1031 (setq sp (1+ (point)))
1032 (setq ep (prog2 (re-search-forward delim)
1033 (1+ (- (point) (length delim)))))
1034 (if (>= sp ep) ; no article!
1037 (narrow-to-region sp ep)
1038 (setq entity (elmo-archive-msgdb-create-entity-subr number))
1040 (elmo-msgdb-append-element
1043 (elmo-msgdb-number-add
1045 (elmo-msgdb-overview-entity-get-number entity)
1047 (setq message-id (car entity))
1048 (setq seen (member message-id seen-list))
1050 (or (elmo-msgdb-global-mark-get message-id)
1051 (if (elmo-file-cache-status
1052 (elmo-file-cache-get message-id))
1055 elmo-msgdb-unread-cached-mark)
1057 elmo-msgdb-read-uncached-mark
1058 elmo-msgdb-new-mark))))
1060 (elmo-msgdb-mark-append
1062 (elmo-msgdb-overview-entity-get-number entity)
1064 (setq ret-val (append ret-val (list overview number-alist
1068 (setq rest (cdr rest)))
1072 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1073 ;;; Search functions
1075 (defsubst elmo-archive-field-condition-match (folder number number-list
1078 (let* ((type (elmo-archive-folder-archive-type-internal folder))
1079 (arc (elmo-archive-get-archive-name folder))
1080 (method (elmo-archive-get-method type 'cat))
1081 (args (list arc (elmo-concat-path prefix (int-to-string number)))))
1083 (when (file-exists-p arc)
1085 (elmo-archive-call-method method args t))
1086 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
1087 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
1088 (elmo-buffer-field-condition-match condition number number-list))))))
1090 (luna-define-method elmo-folder-search ((folder elmo-archive-folder)
1091 condition &optional from-msgs)
1092 (let* (;;(args (elmo-string-to-list key))
1093 ;; XXX: I don't know whether `elmo-archive-list-folder'
1094 ;; updates match-data.
1095 ;; (msgs (or from-msgs (elmo-archive-list-folder spec)))
1096 (msgs (or from-msgs (elmo-folder-list-messages folder)))
1099 (case-fold-search nil)
1100 number-list ret-val)
1101 (setq number-list msgs)
1103 (if (elmo-archive-field-condition-match
1104 folder (car msgs) number-list
1106 (elmo-archive-folder-archive-prefix-internal folder))
1107 (setq ret-val (cons (car msgs) ret-val)))
1108 (when (> num elmo-display-progress-threshold)
1110 (elmo-display-progress
1111 'elmo-archive-search "Searching..."
1113 (setq msgs (cdr msgs)))
1114 (nreverse ret-val)))
1117 (if (null elmo-archive-method-alist)
1118 (let ((mlist elmo-archive-method-list) ; from mew-highlight.el
1121 (setq method (car mlist))
1122 (setq mlist (cdr mlist))
1123 (setq str (symbol-name method))
1124 (string-match "elmo-archive-\\([^-].*\\)-method-alist$" str)
1125 (setq type (intern-soft
1126 (elmo-match-string 1 str)))
1127 (setq elmo-archive-method-alist
1129 (symbol-value method))
1130 elmo-archive-method-alist)))))
1132 ;;; valid suffix(list)
1133 (if (null elmo-archive-suffixes)
1134 (let ((slist elmo-archive-suffix-alist)
1137 (setq tmp (car slist))
1138 (setq elmo-archive-suffixes
1139 (nconc elmo-archive-suffixes (list (cdr tmp))))
1140 (setq slist (cdr slist)))))
1142 (luna-define-method elmo-message-use-cache-p ((folder elmo-archive-folder)
1144 elmo-archive-use-cache)
1147 (run-hooks 'elmo-archive-load-hook)
1150 (product-provide (provide 'elmo-archive) (require 'elmo-version))
1152 ;;; elmo-archive.el ends here