Fixed typo.
[elisp/wanderlust.git] / elmo / elmo-archive.el
1 ;;; elmo-archive.el -- Archive folder of ELMO.
2
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>
5
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
10
11 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
12
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)
16 ;; any later version.
17 ;;
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.
22 ;;
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.
27 ;;
28
29 ;;; Commentary:
30 ;; 
31 ;; TODO:
32 ;; [¥Ü¥½] append-msgs() ¤¬Íߤ·¤¤¡Ê¤±¤É multi-refile ÉԲġˡ£
33 ;; Info-Zip ÀìÍÑ¥¨¡¼¥¸¥§¥ó¥È¤òÍѤ¤¤¿ÆüËܸ측º÷¡ÊOS/2 ÀìÍÑ¡Ë¡£
34
35 ;;; Code:
36 ;; 
37
38 (require 'elmo-msgdb)
39 (require 'emu)
40 (require 'std11)
41 (eval-when-compile (require 'elmo-localdir))
42
43 ;;; Const
44 (defconst elmo-archive-version "v0.18 [990729/alpha]")
45
46 ;;; User vars.
47 (defvar elmo-archive-lha-dos-compatible
48   (memq system-type '(OS/2 emx windows-nt))
49   "*If non-nil, regard your LHA as compatible to DOS version.")
50
51 (defvar elmo-archive-use-izip-agent (memq system-type '(OS/2 emx))
52   "*If non-nil, use the special agent in fetching headers.")
53
54 (defvar elmo-archive-folder-path "~/Mail"
55   "*Base directory for archive folders.")
56
57 (defvar elmo-archive-basename "elmo-archive"
58   "*Common basename of archive folder file, w/o suffix.")
59
60 (defvar elmo-archive-cmdstr-max-length 8000 ; SASAKI Osamu's suggestion
61   "*Command line string limitation under OS/2, exactly 8190 bytes.")
62
63 (defvar elmo-archive-fetch-headers-volume 50
64   "*Quantity of article headers to fetch per once.")
65
66 (defvar elmo-archive-dummy-file ".elmo-archive"
67   "*Name of dummy file that will be appended when the folder is null.")
68
69 (defvar elmo-archive-check-existance-strict t
70   "*Check existance of archive contents if non-nil.")
71
72 (defvar elmo-archive-load-hook nil
73   "*Hook called after loading elmo-archive.el.")
74
75 (defvar elmo-archive-treat-file nil
76   "*Treat archive folder as a file if non-nil.")
77
78 ;;; MMDF parser -- info-zip agent w/ REXX
79 (defvar elmo-mmdf-delimiter "^\01\01\01\01$"
80   "*Regular expression of MMDF delimiter.")
81
82 (defvar elmo-unixmail-delimiter "^From \\([^ \t]+\\) \\(.+\\)"
83   "*Regular expression of UNIX Mail delimiter.")
84
85 (defvar elmo-archive-header-regexp "^[ \t]*[-=][-=][-=][-=]"
86   "*Common regexp of the delimiter in listing archive.") ; marche
87
88 (defvar elmo-archive-file-regexp-alist
89   (append
90    (if elmo-archive-lha-dos-compatible
91        '((lha . "^%s\\([0-9]+\\)$"))    ; OS/2,DOS w/  "-x"
92      '((lha . "^.*[ \t]%s\\([0-9]+\\)$")))
93    '((zip . "^.*[ \t]%s\\([0-9]+\\)$")
94      (zoo . "^.*[ \t]%s\\([0-9]+\\)$")
95      (tar . "^%s\\([0-9]+\\)$")         ; ok
96      (tgz . "^%s\\([0-9]+\\)$")         ; ok
97      (rar . "^[ \t]%s\\([0-9]+\\)$"))))
98
99 (defvar elmo-archive-suffix-alist
100    '((lha . ".lzh")  ; default
101 ;;;  (lha . ".lzs")
102      (zip . ".zip")
103      (zoo . ".zoo")
104 ;;;  (arc . ".arc")
105 ;;;  (arj . ".arj")
106      (rar . ".rar")
107      (tar . ".tar")
108      (tgz . ".tar.gz")))
109
110 ;;; lha
111 (defvar elmo-archive-lha-method-alist
112   (if elmo-archive-lha-dos-compatible
113       ;; OS/2
114       '((cp  . ("lha" "u" "-x"))
115         (mv  . ("lha" "m" "-x"))
116         (rm  . ("lha" "d"))
117         (ls  . ("lha" "l" "-x"))
118         (cat . ("lha" "p" "-n"))
119         (ext . ("lha" "x"))             ; "-x"
120         )
121     ;; some UN|X
122     '((cp  . ("lha" "u"))
123       (mv  . ("lha" "m"))
124       (rm  . ("lha" "d"))
125       (ls  . ("lha" "l"))
126       (cat . ("lha" "pq"))
127       (ext . ("lha" "x")))))
128
129 ;;; info-zip/unzip
130 (defvar elmo-archive-zip-method-alist
131   '((cp       . ("zip" "-9q"))
132     (cp-pipe  . ("zip" "-9q@"))
133     (mv       . ("zip" "-mDq9"))
134     (mv-pipe  . ("zip" "-mDq9@"))
135     (rm       . ("zip" "-dq"))
136     (rm-pipe  . ("zip" "-dq@"))
137     (ls       . ("unzip" "-lq"))
138     (cat      . ("unzip" "-pq"))
139     (ext      . ("unzip"))
140     (cat-headers . ("izwlagent" "--cat"))))
141
142 ;;; zoo
143 (defvar elmo-archive-zoo-method-alist
144   '((cp       . ("zoo" "aq"))
145     (cp-pipe  . ("zoo" "aqI"))
146     (mv       . ("zoo" "aMq"))
147     (mv-pipe  . ("zoo" "aMqI"))
148     (rm       . ("zoo" "Dq"))
149     (ls       . ("zoo" "l"))            ; normal
150     (cat      . ("zoo" "xpq"))
151     (ext      . ("zoo" "xq"))))
152
153 ;;; rar
154 (defvar elmo-archive-rar-method-alist
155   '((cp       . ("rar" "u" "-m5"))
156     (mv       . ("rar" "m" "-m5"))
157     (rm       . ("rar" "d"))
158     (ls       . ("rar" "v"))
159     (cat      . ("rar" "p" "-inul"))
160     (ext      . ("rar" "x"))))
161
162 ;;; GNU tar (*.tar)
163 (defvar elmo-archive-tar-method-alist
164   (if elmo-archive-lha-dos-compatible
165       '((ls   . ("gtar" "-tf"))
166         (cat  . ("gtar" "--posix Oxf"))
167         (ext  . ("gtar" "-xf"))
168 ;;;     (rm   . ("gtar" "--posix" "--delete" "-f")) ; well not work
169         )
170     '((ls    . ("gtar" "-tf"))
171       (cat   . ("gtar" "-Oxf"))
172       (ext   . ("gtar" "-xf"))
173 ;;;     (rm    . ("gtar" "--delete" "-f")) ;; well not work
174       )))
175
176 ;;; GNU tar (*.tar.gz, *.tar.Z, *.tar.bz2)
177 (defvar elmo-archive-tgz-method-alist
178   '((ls         . ("gtar" "-ztf"))
179     (cat        . ("gtar" "-Ozxf"))
180     (create     . ("gtar" "-zcf"))
181 ;;; (rm         . elmo-archive-tgz-rm-func)
182     (cp         . elmo-archive-tgz-cp-func)
183     (mv         . elmo-archive-tgz-mv-func)
184     (ext        . ("gtar" "-zxf"))
185     ;; tgz special method
186     (decompress . ("gzip" "-d"))
187     (compress   . ("gzip"))
188     (append     . ("gtar" "-uf"))
189 ;;; (delete     . ("gtar" "--delete" "-f")) ; well not work
190     ))
191
192 (defvar elmo-archive-method-list
193   '(elmo-archive-lha-method-alist
194     elmo-archive-zip-method-alist
195     elmo-archive-zoo-method-alist
196 ;;; elmo-archive-tar-method-alist
197     elmo-archive-tgz-method-alist
198 ;;; elmo-archive-arc-method-alist
199 ;;; elmo-archive-arj-method-alist
200     elmo-archive-rar-method-alist))
201
202 ;;; Internal vars.
203 (defvar elmo-archive-method-alist nil)
204 (defvar elmo-archive-suffixes nil)
205
206
207 ;;; Macro
208 (defmacro elmo-archive-get-method (type action)
209   (` (cdr (assq (, action) (cdr (assq (, type)
210                                       elmo-archive-method-alist))))))
211
212 (defmacro elmo-archive-get-suffix (type)
213   (` (cdr (assq (, type)
214                 elmo-archive-suffix-alist))))
215
216 (defmacro elmo-archive-get-regexp (type)
217   (` (cdr (assq (, type)
218                 elmo-archive-file-regexp-alist))))
219
220 (static-if (boundp 'NEMACS)
221     (defsubst elmo-archive-call-process (prog args &optional output)
222       (apply 'call-process prog nil output nil args)
223       0)
224   (defsubst elmo-archive-call-process (prog args &optional output)
225     (= (apply 'call-process prog nil output nil args) 0)))
226
227 (defsubst elmo-archive-call-method (method args &optional output)
228   (cond
229    ((functionp method)
230     (funcall method args output))
231    (t
232     (elmo-archive-call-process
233      (car method) (append (cdr method) args) output))))
234
235 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 ;;; Scan Folder
237
238 (defsubst elmo-archive-list-folder-subr (spec &optional nonsort)
239   "*Returns list of number-file(int, not string) in archive FILE.
240 TYPE specifies the archiver's symbol."
241   (let* ((type (nth 2 spec))
242          (prefix (nth 3 spec))
243          (file (elmo-archive-get-archive-name (nth 1 spec) type spec))
244          (method (elmo-archive-get-method type 'ls))
245          (args (list file))
246          (file-regexp (format (elmo-archive-get-regexp type)
247                               (elmo-concat-path (regexp-quote prefix) "")))
248          (killed (and elmo-use-killed-list
249                       (elmo-msgdb-killed-list-load
250                        (elmo-msgdb-expand-path spec))))
251          numbers buf file-list header-end)
252     (when (file-exists-p file)
253       (save-excursion
254         (set-buffer (setq buf (get-buffer-create " *ELMO ARCHIVE ls*")))
255         (unless (elmo-archive-call-method method args t)
256           (error "%s exited abnormally!" method))
257         (goto-char (point-min))
258         (when (re-search-forward elmo-archive-header-regexp nil t)
259           (forward-line 1)
260           (setq header-end (point))
261           (when (re-search-forward elmo-archive-header-regexp nil t)
262               (beginning-of-line)
263               (narrow-to-region header-end (point))
264               (goto-char (point-min))))
265         (while (and (re-search-forward file-regexp nil t)
266                     (not (eobp)))  ; for GNU tar 981010
267           (setq file-list (nconc file-list (list (string-to-int
268                                                   (match-string 1))))))
269         (kill-buffer buf)))
270     (if nonsort
271         (cons (or (elmo-max-of-list file-list) 0)
272               (if killed
273                   (- (length file-list)
274                      (elmo-msgdb-killed-list-length killed))
275                 (length file-list)))
276       (setq numbers (sort file-list '<))
277       (elmo-living-messages numbers killed))))
278
279 (defun elmo-archive-list-folder (spec &optional nohide)
280   (elmo-archive-list-folder-subr spec))
281
282 (defun elmo-archive-max-of-folder (spec)
283   (elmo-archive-list-folder-subr spec t))
284
285
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 ;;; Folder related functions
288
289 (defsubst elmo-archive-get-archive-directory (name)
290   ;; allow fullpath. return format is "/foo/bar/".
291   (if (file-name-absolute-p name)
292       (if (find-file-name-handler name 'copy-file)
293           name
294         (expand-file-name name))
295     (expand-file-name name elmo-archive-folder-path)))
296
297 (defun elmo-archive-get-archive-name (folder type &optional spec)
298   (let ((dir (elmo-archive-get-archive-directory folder))
299         (suffix (elmo-archive-get-suffix type))
300         filename dbdir)
301     (if elmo-archive-treat-file
302         (if (string-match (concat (regexp-quote suffix) "$") folder)
303             (expand-file-name
304              folder
305              elmo-archive-folder-path)
306           (expand-file-name
307            (concat folder suffix)
308            elmo-archive-folder-path))
309       (if (and (let ((handler (find-file-name-handler dir 'copy-file))) ; dir is local.
310                  (or (not handler)
311                      (if (featurep 'xemacs)
312                          (eq handler 'dired-handler-fn))))
313                (or (not (file-exists-p dir))
314                    (file-directory-p dir)))
315           (expand-file-name
316            (concat elmo-archive-basename suffix)
317            dir)
318         ;; for full-path specification.
319         (if (and (find-file-name-handler dir 'copy-file) ; ange-ftp, efs
320                  spec)
321             (progn
322               (setq filename (expand-file-name
323                               (concat elmo-archive-basename suffix)
324                               (setq dbdir (elmo-msgdb-expand-path spec))))
325               (if (file-directory-p dbdir)
326                   (); ok.
327                 (if (file-exists-p dbdir)
328                     (error "File %s already exists" dbdir)
329                   (elmo-make-directory dbdir)))
330               (if (not (file-exists-p filename))
331                   (copy-file
332                    (if (file-directory-p dir)
333                        (expand-file-name
334                         (concat elmo-archive-basename suffix)
335                         dir)
336                      dir)
337                    filename))
338               filename)
339           dir)))))
340
341 (defun elmo-archive-folder-exists-p (spec)
342   (file-exists-p
343    (elmo-archive-get-archive-name (nth 1 spec) (nth 2 spec) spec)))
344
345 (defun elmo-archive-folder-creatable-p (spec)
346   t)
347
348 (defun elmo-archive-create-folder (spec)
349   (let* ((dir (directory-file-name      ; remove tail slash.
350                (elmo-archive-get-archive-directory (nth 1 spec))))
351          (type (nth 2 spec))
352          (arc (elmo-archive-get-archive-name (nth 1 spec) type)))
353     (if elmo-archive-treat-file
354         (setq dir (directory-file-name (file-name-directory dir))))
355     (cond ((and (file-exists-p dir)
356                 (not (file-directory-p dir)))
357            ;; file exists
358            (error "Create folder failed; File \"%s\" exists" dir))
359           ((file-directory-p dir)
360            (if (file-exists-p arc)
361                t                        ; return value
362              (elmo-archive-create-file arc type spec)))
363           (t
364            (elmo-make-directory dir)
365            (elmo-archive-create-file arc type spec)
366            t))))
367
368 (defun elmo-archive-create-file (archive type spec)
369   (save-excursion
370     (let* ((tmp-dir (directory-file-name
371                      (elmo-msgdb-expand-path spec)))
372            (dummy elmo-archive-dummy-file)
373            (method (or (elmo-archive-get-method type 'create)
374                        (elmo-archive-get-method type 'mv)))
375            (args (list archive dummy)))
376       (when (null method)
377         (ding)
378         (error "WARNING: read-only mode: %s (method undefined)" type))
379       (cond
380        ((file-directory-p tmp-dir)
381         ()) ;nop
382        ((file-exists-p tmp-dir)
383         ;; file exists
384         (error "Create directory failed; File \"%s\" exists" tmp-dir))
385        (t
386         (elmo-make-directory tmp-dir)))
387       (elmo-bind-directory
388        tmp-dir
389        (write-region (point) (point) dummy nil 'no-msg)
390        (prog1
391            (elmo-archive-call-method method args)
392          (if (file-exists-p dummy)
393              (delete-file dummy)))
394        ))))
395
396 (defun elmo-archive-delete-folder (spec)
397   (let* ((arc (elmo-archive-get-archive-name (nth 1 spec) (nth 2 spec))))
398     (if (not (file-exists-p arc))
399         (error "No such file: %s" arc)
400       (delete-file arc)
401       t)))
402
403 (defun elmo-archive-rename-folder (old-spec new-spec)
404   (let* ((old-arc (elmo-archive-get-archive-name
405                    (nth 1 old-spec) (nth 2 old-spec)))
406          (new-arc (elmo-archive-get-archive-name
407                    (nth 1 new-spec) (nth 2 new-spec))))
408     (unless (and (eq (nth 2 old-spec) (nth 2 new-spec))
409                  (equal (nth 3 old-spec) (nth 3 new-spec)))
410       (error "Not same archive type and prefix"))
411     (if (not (file-exists-p old-arc))
412         (error "No such file: %s" old-arc)
413       (if (file-exists-p new-arc)
414           (error "Already exists: %s" new-arc)
415         (rename-file old-arc new-arc)
416         t))))
417
418 (defun elmo-archive-list-folders (spec &optional hierarchy)
419   (let ((folder (concat "$" (nth 1 spec)))
420         (elmo-localdir-folder-path elmo-archive-folder-path))
421     (if elmo-archive-treat-file
422         (let* ((path (elmo-localdir-get-folder-directory spec))
423                (base-folder (or (nth 1 spec) ""))
424                (suffix (nth 2 spec))
425                (prefix (if (string= (nth 3 spec) "")
426                            "" (concat ";" (nth 3 spec))))
427                (dir (if (file-directory-p path)
428                         path (file-name-directory path)))
429                (name (if (file-directory-p path)
430                          "" (file-name-nondirectory path)))
431                (flist (and (file-directory-p dir)
432                            (directory-files dir nil name nil)))
433                (regexp (format "^\\(.*\\)\\(%s\\)$"
434                                (mapconcat
435                                 '(lambda (x) (regexp-quote (cdr x)))
436                                 elmo-archive-suffix-alist
437                                 "\\|"))))
438           (if (string-match "\\(.*\\)/$" base-folder) ; ends with '/'.
439               (setq base-folder (elmo-match-string 1 base-folder))
440             (unless (file-directory-p path)
441               (setq base-folder (or (file-name-directory base-folder)
442                                     base-folder))))
443           (delq
444            nil
445            (mapcar
446             '(lambda (x)
447                (when (and (string-match regexp x)
448                           (eq suffix
449                               (car
450                                (rassoc (elmo-match-string 2 x)
451                                        elmo-archive-suffix-alist))))
452                  (format "$%s;%s%s"
453                          (elmo-concat-path base-folder (elmo-match-string 1 x))
454                          suffix prefix)))
455             flist)))
456       (elmo-localdir-list-folders-subr folder hierarchy))))
457
458
459 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
460 ;;; Article file related functions
461 ;;; read(extract) / append(move) / delete(delete) / query(list)
462
463 (defun elmo-archive-read-msg (spec number outbuf &optional msgdb unread)
464   (save-excursion
465     (let* ((type (nth 2 spec))
466            (arc (elmo-archive-get-archive-name (nth 1 spec) type spec))
467            (prefix (nth 3 spec))
468            (method (elmo-archive-get-method type 'cat))
469            (args (list arc (elmo-concat-path
470                             prefix (int-to-string number)))))
471       (set-buffer outbuf)
472       (erase-buffer)
473       (when (file-exists-p arc)
474         (and
475          (as-binary-process
476           (elmo-archive-call-method method args t))
477          (elmo-delete-cr-get-content-type))))))
478
479 ;; verrrrrry slow!!
480 (defun elmo-archive-append-msg (spec string &optional msg no-see)
481   (let* ((type (nth 2 spec))
482          (prefix (nth 3 spec))
483          (arc (elmo-archive-get-archive-name (nth 1 spec) type))
484          (method (elmo-archive-get-method type 'mv))
485          (tmp-buffer (get-buffer-create " *ELMO ARCHIVE mv*"))
486          (next-num (or msg
487                        (1+ (if (file-exists-p arc)
488                                (car (elmo-archive-max-of-folder spec)) 0))))
489          (tmp-dir (elmo-msgdb-expand-path spec))
490          newfile)
491     (when (null method)
492       (ding)
493       (error "WARNING: read-only mode: %s (method undefined)" type))
494     (save-excursion
495       (set-buffer tmp-buffer)
496       (erase-buffer)
497       (let ((tmp-dir (expand-file-name prefix tmp-dir)))
498         (when (not (file-directory-p tmp-dir))
499           (elmo-make-directory (directory-file-name tmp-dir))))
500       (setq newfile (elmo-concat-path
501                      prefix
502                      (int-to-string next-num)))
503       (unwind-protect
504           (elmo-bind-directory
505            tmp-dir
506            (if (and (or (functionp method) (car method))
507                     (file-writable-p newfile))
508                (progn
509                  (insert string)
510                  (as-binary-output-file
511                   (write-region (point-min) (point-max) newfile nil 'no-msg))
512                  (elmo-archive-call-method method (list arc newfile)))
513              nil))
514         (kill-buffer tmp-buffer)))))
515
516 ;; (localdir, maildir, localnews, archive) -> archive
517 (defun elmo-archive-copy-msgs (dst-spec msgs src-spec
518                                         &optional loc-alist same-number)
519   (let* ((dst-type (nth 2 dst-spec))
520          (arc (elmo-archive-get-archive-name (nth 1 dst-spec) dst-type))
521          (prefix (nth 3 dst-spec))
522          (p-method (elmo-archive-get-method dst-type 'mv-pipe))
523          (n-method (elmo-archive-get-method dst-type 'mv))
524          (new (unless same-number
525                 (1+ (car (elmo-archive-max-of-folder dst-spec)))))
526          (src-dir (elmo-localdir-get-folder-directory src-spec))
527          (tmp-dir
528           (file-name-as-directory (elmo-msgdb-expand-path dst-spec)))
529          (do-link t)
530          src tmp newfile tmp-msgs)
531     (when (not (elmo-archive-folder-exists-p dst-spec))
532       (elmo-archive-create-folder dst-spec))
533     (when (null (or p-method n-method))
534       (ding)
535       (error "WARNING: read-only mode: %s (method undefined)" dst-type))
536     (when (and same-number
537                (not (eq (car src-spec) 'maildir))
538                (string-match (concat prefix "$") src-dir)
539                (or
540                 (elmo-archive-get-method dst-type 'cp-pipe)
541                 (elmo-archive-get-method dst-type 'cp)))
542       (setq tmp-dir (substring src-dir 0 (match-beginning 0)))
543       (setq p-method (elmo-archive-get-method dst-type 'cp-pipe)
544             n-method (elmo-archive-get-method dst-type 'cp))
545       (setq tmp-msgs (mapcar '(lambda (x)
546                                 (elmo-concat-path prefix (int-to-string x)))
547                              msgs))
548       (setq do-link nil))
549     (when do-link
550       (let ((tmp-dir (expand-file-name prefix tmp-dir)))
551         (when (not (file-directory-p tmp-dir))
552           (elmo-make-directory (directory-file-name tmp-dir))))
553       (while msgs
554         (setq newfile (elmo-concat-path prefix (int-to-string
555                                                 (if same-number
556                                                     (car msgs)
557                                                   new))))
558         (setq tmp-msgs (nconc tmp-msgs (list newfile)))
559         (elmo-copy-file
560          ;; src file
561          (elmo-call-func src-spec "get-msg-filename" (car msgs) loc-alist)
562          ;; tmp file
563          (expand-file-name newfile tmp-dir))
564         (setq msgs (cdr msgs))
565         (unless same-number (setq new (1+ new)))))
566     (save-excursion
567       (elmo-bind-directory
568        tmp-dir
569        (cond
570         ((functionp n-method)
571          (funcall n-method (cons arc tmp-msgs)))
572         (p-method
573          (let ((p-prog (car p-method))
574                (p-prog-arg (cdr p-method)))
575            (elmo-archive-exec-msgs-subr1
576             p-prog (append p-prog-arg (list arc)) tmp-msgs)))
577         (t
578          (let ((n-prog (car n-method))
579                (n-prog-arg (cdr n-method)))
580            (elmo-archive-exec-msgs-subr2
581             n-prog (append n-prog-arg (list arc)) tmp-msgs (length arc)))))))))
582
583 ;;; archive -> (localdir, localnews, archive)
584 (defun elmo-archive-copy-msgs-froms (dst-spec msgs src-spec
585                                               &optional loc-alist same-number)
586   (let* ((src-type (nth 2 src-spec))
587          (arc (elmo-archive-get-archive-name (nth 1 src-spec) src-type))
588          (prefix (nth 3 src-spec))
589          (p-method (elmo-archive-get-method src-type 'ext-pipe))
590          (n-method (elmo-archive-get-method src-type 'ext))
591          (tmp-dir
592           (file-name-as-directory (elmo-msgdb-expand-path src-spec)))
593          (tmp-msgs (mapcar '(lambda (x) (elmo-concat-path
594                                          prefix
595                                          (int-to-string x)))
596                            msgs))
597          result)
598     (unwind-protect
599         (setq result
600               (and
601                ;; extract messages
602                (save-excursion
603                  (elmo-bind-directory
604                   tmp-dir
605                   (cond
606                    ((functionp n-method)
607                     (funcall n-method (cons arc tmp-msgs)))
608                    (p-method
609                     (let ((p-prog (car p-method))
610                           (p-prog-arg (cdr p-method)))
611                       (elmo-archive-exec-msgs-subr1
612                        p-prog (append p-prog-arg (list arc)) tmp-msgs)))
613                    (t
614                     (let ((n-prog (car n-method))
615                           (n-prog-arg (cdr n-method)))
616                       (elmo-archive-exec-msgs-subr2
617                        n-prog (append n-prog-arg (list arc)) tmp-msgs (length arc)))))))
618                ;; call elmo-*-copy-msgs of destination folder
619                (elmo-call-func dst-spec "copy-msgs"
620                                msgs src-spec loc-alist same-number)))
621       ;; clean up tmp-dir
622       (elmo-bind-directory
623        tmp-dir
624        (while tmp-msgs
625          (if (file-exists-p (car tmp-msgs))
626              (delete-file (car tmp-msgs)))
627          (setq tmp-msgs (cdr tmp-msgs))))
628       result)))
629
630 (defun elmo-archive-delete-msgs (spec msgs)
631   (save-excursion
632     (let* ((type (nth 2 spec))
633            (prefix (nth 3 spec))
634            (arc (elmo-archive-get-archive-name (nth 1 spec) type))
635            (p-method (elmo-archive-get-method type 'rm-pipe))
636            (n-method (elmo-archive-get-method type 'rm))
637            (msgs (mapcar '(lambda (x) (elmo-concat-path
638                                        prefix
639                                        (int-to-string x)))
640                          msgs)))
641       (cond ((functionp n-method)
642              (funcall n-method (cons arc msgs)))
643             (p-method
644              (let ((p-prog (car p-method))
645                    (p-prog-arg (cdr p-method)))
646                (elmo-archive-exec-msgs-subr1
647                 p-prog (append p-prog-arg (list arc)) msgs)))
648             (n-method
649              (let ((n-prog (car n-method))
650                    (n-prog-arg (cdr n-method)))
651                (elmo-archive-exec-msgs-subr2
652                 n-prog (append n-prog-arg (list arc)) msgs (length arc))))
653             (t
654              (ding)
655              (error "WARNING: not delete: %s (method undefined)" type))) )))
656
657 (defun elmo-archive-exec-msgs-subr1 (prog args msgs)
658   (let ((buf (get-buffer-create " *ELMO ARCHIVE exec*")))
659     (set-buffer buf)
660     (insert (mapconcat 'concat msgs "\n")) ;string
661     (unwind-protect
662         (= 0
663            (apply 'call-process-region (point-min) (point-max)
664                   prog nil nil nil args))
665       (kill-buffer buf))))
666
667 (defun elmo-archive-exec-msgs-subr2 (prog args msgs arc-length)
668   (let ((max-len (- elmo-archive-cmdstr-max-length arc-length))
669         (n (length msgs))
670         rest i sum)
671     (setq rest msgs) ;string
672     (setq i 1)
673     (setq sum 0)
674     (catch 'done
675       (while (and rest (<= i n))
676         (mapcar '(lambda (x)
677                    (let* ((len (length x))
678                           (files (member x (reverse rest))))
679                      ;; total(previous) + current + white space
680                      (if (<= max-len (+ sum len 1))
681                          (progn
682                            (unless
683                                (elmo-archive-call-process
684                                 prog (append args files))
685                              (throw 'done nil))
686                            (setq sum 0) ;; reset
687                            (setq rest (nthcdr i rest)))
688                        (setq sum (+ sum len 1)))
689                      (setq i (1+ i)))) msgs))
690       (throw 'done
691              (or (not rest)
692                  (elmo-archive-call-process prog (append args rest))))
693       )))
694
695 (defsubst elmo-archive-article-exists-p (arc msg type)
696   (if (not elmo-archive-check-existance-strict)
697       t ; nop
698     (save-excursion ; added 980915
699       (let* ((method (elmo-archive-get-method type 'ls))
700              (args (list arc msg))
701              (buf (get-buffer-create " *ELMO ARCHIVE query*"))
702              (error-msg "\\(no file\\|0 files\\)")
703              ret-val)
704         (set-buffer buf)
705         (erase-buffer)
706         (elmo-archive-call-method method args t)
707         ;; pointer: point-max
708         (setq ret-val (not (re-search-backward error-msg nil t)))
709         (kill-buffer buf)
710         ret-val))))
711
712 (defun elmo-archive-tgz-common-func (args exec-type &optional copy)
713   (let* ((arc (car args))
714          (tmp-msgs (cdr args))
715          (decompress (elmo-archive-get-method 'tgz 'decompress))
716          (compress (elmo-archive-get-method 'tgz 'compress))
717          (exec (elmo-archive-get-method 'tgz exec-type))
718          (suffix (elmo-archive-get-suffix 'tgz))
719          (tar-suffix (elmo-archive-get-suffix 'tar))
720          arc-tar ret-val
721          )
722     (when (null (and decompress compress exec))
723       (ding)
724       (error "WARNING: special method undefined: %s of %s"
725              (or (if (null decompress) 'decompress)
726                  (if (null compress) 'compress)
727                  (if (null exec) exec-type))
728              'tgz))
729     (unless tar-suffix
730       (ding)
731       (error "WARNING: `tar' suffix undefined"))
732     (if (string-match (concat (regexp-quote suffix) "$") arc)
733         (setq arc-tar
734               (concat (substring arc 0 (match-beginning 0)) tar-suffix))
735       (error "%s: not match suffix [%s]" arc suffix))
736     (and
737      ;; decompress
738      (elmo-archive-call-process
739       (car decompress) (append (cdr decompress) (list arc)))
740      ;; append (or delete)
741      (elmo-archive-exec-msgs-subr2
742       (car exec) (append (cdr exec) (list arc-tar)) tmp-msgs (length arc-tar))
743      ;; compress
744      (setq ret-val
745            (elmo-archive-call-process
746             (car compress) (append (cdr compress) (list arc-tar)))))
747     ;; delete tmporary messages
748     (if (and (not copy)
749              (eq exec-type 'append))
750         (while tmp-msgs
751           (if (file-exists-p (car tmp-msgs))
752               (delete-file (car tmp-msgs)))
753           (setq tmp-msgs (cdr tmp-msgs))))
754     ret-val))
755
756 (defun elmo-archive-tgz-cp-func (args &optional output)
757   (elmo-archive-tgz-common-func args 'append t))
758
759 (defun elmo-archive-tgz-mv-func (args &optional output)
760   (elmo-archive-tgz-common-func args 'append))
761
762 (defun elmo-archive-tgz-rm-func (args &optional output)
763   (elmo-archive-tgz-common-func args 'delete))
764
765 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
766 ;;; MessageDB functions (from elmo-localdir.el)
767
768 (defsubst elmo-archive-msgdb-create-entity-subr (number)
769   (let (header-end)
770     (elmo-set-buffer-multibyte default-enable-multibyte-characters)
771     (goto-char (point-min))
772     (if (re-search-forward "\\(^--.*$\\)\\|\\(\n\n\\)" nil t)
773         (setq header-end (point))
774       (setq header-end (point-max)))
775     (narrow-to-region (point-min) header-end)
776     (elmo-msgdb-create-overview-from-buffer number)))
777
778 ;; verrrry slow!!
779 (defsubst elmo-archive-msgdb-create-entity (method archive number type &optional prefix)
780   (let* ((msg (elmo-concat-path prefix (int-to-string number)))
781          (arg-list (list archive msg)))
782     (when (elmo-archive-article-exists-p archive msg type)
783       ;; insert article.
784       (as-binary-process
785        (elmo-archive-call-method method arg-list t))
786       (elmo-archive-msgdb-create-entity-subr number))))
787
788 (defun elmo-archive-msgdb-create-as-numlist (spec numlist new-mark
789                                                   already-mark seen-mark
790                                                   important-mark seen-list)
791   (when numlist
792     (save-excursion ;; 981005
793       (if (and elmo-archive-use-izip-agent
794                (elmo-archive-get-method (nth 2 spec) 'cat-headers))
795           (elmo-archive-msgdb-create-as-numlist-subr2
796            spec numlist new-mark already-mark seen-mark important-mark
797            seen-list)
798         (elmo-archive-msgdb-create-as-numlist-subr1
799          spec numlist new-mark already-mark seen-mark important-mark
800          seen-list)))))
801
802 (defalias 'elmo-archive-msgdb-create 'elmo-archive-msgdb-create-as-numlist)
803
804
805 (defun elmo-archive-msgdb-create-as-numlist-subr1 (spec numlist new-mark
806                                                         already-mark seen-mark
807                                                         important-mark
808                                                         seen-list)
809   (let* ((type (nth 2 spec))
810          (file (elmo-archive-get-archive-name (nth 1 spec) type spec))
811          (method (elmo-archive-get-method type 'cat))
812          (tmp-buf (get-buffer-create " *ELMO ARCHIVE msgdb*"))
813          overview number-alist mark-alist entity
814          i percent num message-id seen gmark)
815     (save-excursion
816       (set-buffer tmp-buf)
817       (setq num (length numlist))
818       (setq i 0)
819       (message "Creating msgdb...")
820       (while numlist
821         (erase-buffer)
822         (setq entity
823               (elmo-archive-msgdb-create-entity
824                method file (car numlist) type (nth 3 spec)))
825         (when entity
826           (setq overview
827                 (elmo-msgdb-append-element
828                  overview entity))
829           (setq number-alist
830                 (elmo-msgdb-number-add
831                  number-alist
832                  (elmo-msgdb-overview-entity-get-number entity)
833                  (car entity)))
834           (setq message-id (car entity))
835           (setq seen (member message-id seen-list))
836           (if (setq gmark
837                     (or (elmo-msgdb-global-mark-get message-id)
838                         (if (elmo-cache-exists-p message-id) ; XXX
839                             (if seen
840                                 nil
841                               already-mark)
842                           (if seen
843                               seen-mark
844                             new-mark))))
845               (setq mark-alist
846                     (elmo-msgdb-mark-append
847                      mark-alist
848                      (elmo-msgdb-overview-entity-get-number entity)
849                      gmark))))
850         (when (> num elmo-display-progress-threshold)
851           (setq i (1+ i))
852           (setq percent (/ (* i 100) num))
853           (elmo-display-progress
854            'elmo-archive-msgdb-create-as-numlist-subr1 "Creating msgdb..."
855            percent))
856         (setq numlist (cdr numlist)))
857       (kill-buffer tmp-buf)
858       (message "Creating msgdb...done")
859       (list overview number-alist mark-alist)) ))
860
861 ;;; info-zip agent
862 (defun elmo-archive-msgdb-create-as-numlist-subr2 (spec numlist new-mark
863                                                         already-mark seen-mark
864                                                         important-mark
865                                                         seen-list)
866   (let* ((buf (get-buffer-create " *ELMO ARCHIVE headers*"))
867          (delim1 elmo-mmdf-delimiter)           ;; MMDF
868          (delim2 elmo-unixmail-delimiter)       ;; UNIX Mail
869          (type (nth 2 spec))
870          (prefix (nth 3 spec))
871          (method (elmo-archive-get-method type 'cat-headers))
872          (prog (car method))
873          (args (cdr method))
874          (arc (elmo-archive-get-archive-name (nth 1 spec) type))
875          n i percent num result overview number-alist mark-alist
876          msgs case-fold-search)
877     (set-buffer buf)
878     (setq num (length numlist))
879     (setq i 0)
880     (message "Creating msgdb...")
881     (while numlist
882       (setq n (min (1- elmo-archive-fetch-headers-volume)
883                    (1- (length numlist))))
884       (setq msgs (reverse (memq (nth n numlist) (reverse numlist))))
885       (setq numlist (nthcdr (1+ n) numlist))
886       (erase-buffer)
887       (insert
888        (mapconcat
889         'concat
890         (mapcar '(lambda (x) (elmo-concat-path prefix (int-to-string x))) msgs)
891         "\n"))
892       (message "Fetching headers...")
893       (as-binary-process (apply 'call-process-region
894                                 (point-min) (point-max)
895                                 prog t t nil (append args (list arc))))
896       (goto-char (point-min))
897       (cond
898        ((looking-at delim1)     ;; MMDF
899         (setq result (elmo-archive-parse-mmdf msgs
900                                               new-mark
901                                               already-mark seen-mark
902                                               seen-list))
903         (setq overview (append overview (nth 0 result)))
904         (setq number-alist (append number-alist (nth 1 result)))
905         (setq mark-alist (append mark-alist (nth 2 result))))
906 ;;;    ((looking-at delim2)     ;; UNIX MAIL
907 ;;;     (setq result (elmo-archive-parse-unixmail msgs))
908 ;;;     (setq overview (append overview (nth 0 result)))
909 ;;;     (setq number-alist (append number-alist (nth 1 result)))
910 ;;;     (setq mark-alist (append mark-alist (nth 2 result))))
911        (t                       ;; unknown format
912         (error "Unknown format!")))
913       (when (> num elmo-display-progress-threshold)
914         (setq i (+ n i))
915         (setq percent (/ (* i 100) num))
916         (elmo-display-progress
917          'elmo-archive-msgdb-create-as-numlist-subr2 "Creating msgdb..."
918          percent)))
919     (kill-buffer buf)
920     (list overview number-alist mark-alist)) )
921
922 (defun elmo-archive-parse-mmdf (msgs new-mark
923                                      already-mark
924                                      seen-mark
925                                      seen-list)
926   (let ((delim elmo-mmdf-delimiter)
927         number sp ep rest entity overview number-alist mark-alist ret-val
928         message-id seen gmark)
929     (goto-char (point-min))
930     (setq rest msgs)
931     (while (and rest (re-search-forward delim nil t)
932                 (not (eobp)))
933       (setq number (car rest))
934       (setq sp (1+ (point)))
935       (setq ep (prog2 (re-search-forward delim)
936                    (1+ (- (point) (length delim)))))
937       (if (>= sp ep)                    ; no article!
938           ()                            ; nop
939         (save-excursion
940           (narrow-to-region sp ep)
941           (setq entity (elmo-archive-msgdb-create-entity-subr number))
942           (setq overview
943                 (elmo-msgdb-append-element
944                  overview entity))
945           (setq number-alist
946                 (elmo-msgdb-number-add
947                  number-alist
948                  (elmo-msgdb-overview-entity-get-number entity)
949                  (car entity)))
950           (setq message-id (car entity))
951           (setq seen (member message-id seen-list))
952           (if (setq gmark
953                     (or (elmo-msgdb-global-mark-get message-id)
954                         (if (elmo-cache-exists-p message-id) ; XXX
955                             (if seen
956                                 nil
957                               already-mark)
958                           (if seen
959                               seen-mark
960                             new-mark))))
961               (setq mark-alist
962                     (elmo-msgdb-mark-append
963                      mark-alist
964                      (elmo-msgdb-overview-entity-get-number entity)
965                      gmark)))
966           (setq ret-val (append ret-val (list overview number-alist mark-alist)))
967           (widen)))
968       (forward-line 1)
969       (setq rest (cdr rest)))
970     ret-val))
971
972
973 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
974 ;;; Search functions
975
976 (defsubst elmo-archive-field-condition-match (spec number number-list
977                                                    condition prefix)
978   (save-excursion
979     (let* ((type (nth 2 spec))
980            (arc (elmo-archive-get-archive-name (nth 1 spec) type spec))
981            (method (elmo-archive-get-method type 'cat))
982            (args (list arc (elmo-concat-path prefix (int-to-string number)))))
983       (elmo-set-work-buf
984        (when (file-exists-p arc)
985          (as-binary-process
986           (elmo-archive-call-method method args t))
987          (elmo-set-buffer-multibyte default-enable-multibyte-characters)
988          (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
989          (elmo-buffer-field-condition-match condition number number-list))))))
990
991 (defun elmo-archive-search (spec condition &optional from-msgs)
992   (let* (;;(args (elmo-string-to-list key))
993          ;; XXX: I don't know whether `elmo-archive-list-folder'
994          ;;      updates match-data.
995          ;; (msgs (or from-msgs (elmo-archive-list-folder spec)))
996          (msgs (or from-msgs (elmo-archive-list-folder spec)))
997          (num (length msgs))
998          (i 0)
999          (case-fold-search nil)
1000          number-list ret-val)
1001     (setq number-list msgs)
1002     (while msgs
1003       (if (elmo-archive-field-condition-match spec (car msgs) number-list
1004                                               condition
1005                                               (nth 3 spec))
1006           (setq ret-val (cons (car msgs) ret-val)))
1007       (when (> num elmo-display-progress-threshold)
1008         (setq i (1+ i))
1009         (elmo-display-progress
1010          'elmo-archive-search "Searching..."
1011          (/ (* i 100) num)))
1012       (setq msgs (cdr msgs)))
1013     (nreverse ret-val)))
1014
1015
1016 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1017 ;;; Misc functions
1018
1019 (defun elmo-archive-check-validity (spec validity-file)
1020   t) ; ok.
1021
1022 (defun elmo-archive-sync-validity (spec validity-file)
1023   t) ; ok.
1024
1025 \f
1026 ;;; method(alist)
1027 (if (null elmo-archive-method-alist)
1028     (let ((mlist elmo-archive-method-list) ; from mew-highlight.el
1029           method type str)
1030       (while mlist
1031         (setq method (car mlist))
1032         (setq mlist (cdr mlist))
1033         (setq str (symbol-name method))
1034         (string-match "elmo-archive-\\([^-].*\\)-method-alist$" str)
1035         (setq type (intern-soft
1036                     (elmo-match-string 1 str)))
1037         (setq elmo-archive-method-alist
1038               (cons (cons type
1039                           (symbol-value method))
1040                     elmo-archive-method-alist)))))
1041
1042 ;;; valid suffix(list)
1043 (if (null elmo-archive-suffixes)
1044     (let ((slist elmo-archive-suffix-alist)
1045           tmp)
1046       (while slist
1047         (setq tmp (car slist))
1048         (setq elmo-archive-suffixes
1049               (nconc elmo-archive-suffixes (list (cdr tmp))))
1050         (setq slist (cdr slist)))))
1051
1052 (defun elmo-archive-use-cache-p (spec number)
1053   elmo-archive-use-cache)
1054
1055 (defun elmo-archive-local-file-p (spec number)
1056   nil)
1057
1058 (defun elmo-archive-get-msg-filename (spec number &optional loc-alist)
1059   (let ((tmp-dir (file-name-as-directory (elmo-msgdb-expand-path spec)))
1060         (prefix (nth 3 spec)))
1061     (expand-file-name
1062      (elmo-concat-path prefix (int-to-string number))
1063      tmp-dir)))
1064
1065 (defalias 'elmo-archive-sync-number-alist
1066   'elmo-generic-sync-number-alist)
1067 (defalias 'elmo-archive-list-folder-unread
1068   'elmo-generic-list-folder-unread)
1069 (defalias 'elmo-archive-list-folder-important
1070   'elmo-generic-list-folder-important)
1071 (defalias 'elmo-archive-commit 'elmo-generic-commit)
1072 (defalias 'elmo-archive-folder-diff 'elmo-generic-folder-diff)
1073
1074 ;;; End
1075 (run-hooks 'elmo-archive-load-hook)
1076
1077 (require 'product)
1078 (product-provide (provide 'elmo-archive) (require 'elmo-version))
1079
1080 ;;; elmo-archive.el ends here