* mmelmo-imap4-2.el (mmelmo-imap4-get-mime-entity):
[elisp/wanderlust.git] / elmo / elmo-archive.el
1 ;;; elmo-archive.el -- Archive folder of ELMO.
2
3 ;; Copyright 1998,1999,2000 OKUNISHI Fujikazu <fuji0924@mbox.kyoto-inet.or.jp>
4 ;;                          Yuuichi Teranishi <teranisi@gohome.org>
5
6 ;; Author:  OKUNISHI Fujikazu <fuji0924@mbox.kyoto-inet.or.jp>
7 ;; Keywords: mail, net news
8 ;; Created: Sep 13, 1998
9 ;; Revised: Dec 15, 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 ;; [\e$B%\%=\e(B] append-msgs() \e$B$,M_$7$$!J$1$I\e(B multi-refile \e$BIT2D!K!#\e(B
33 ;; Info-Zip \e$B@lMQ%(!<%8%'%s%H$rMQ$$$?F|K\8l8!:w!J\e(BOS/2 \e$B@lMQ!K!#\e(B
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 (defsubst elmo-archive-call-process (prog args &optional output)
221   (= (apply 'call-process prog nil output nil args) 0))
222
223 (defsubst elmo-archive-call-method (method args &optional output)
224   (cond
225    ((functionp method)
226     (funcall method args output))
227    (t
228     (elmo-archive-call-process
229      (car method) (append (cdr method) args) output))))
230
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232 ;;; Scan Folder
233
234 (defsubst elmo-archive-list-folder-subr (file type prefix &optional nonsort)
235   "*Returns list of number-file(int, not string) in archive FILE.
236 TYPE specifies the archiver's symbol."
237   (let* ((method (elmo-archive-get-method type 'ls))
238          (args (list file))
239          (file-regexp (format (elmo-archive-get-regexp type)
240                               (elmo-concat-path (regexp-quote prefix) "")))
241          buf file-list header-end)
242     (when (file-exists-p file)
243       (save-excursion
244         (set-buffer (setq buf (get-buffer-create " *ELMO ARCHIVE ls*")))
245         (unless (elmo-archive-call-method method args t)
246           (error "%s exited abnormally!" method))
247         (goto-char (point-min))
248         (when (re-search-forward elmo-archive-header-regexp nil t)
249           (forward-line 1)
250           (setq header-end (point))
251           (when (re-search-forward elmo-archive-header-regexp nil t)
252               (beginning-of-line)
253               (narrow-to-region header-end (point))
254               (goto-char (point-min))))
255         (while (and (re-search-forward file-regexp nil t)
256                     (not (eobp)))  ; for GNU tar 981010
257           (setq file-list (nconc file-list (list (string-to-int
258                                                   (match-string 1))))))
259         (kill-buffer buf)))
260     (if nonsort
261         (cons (or (elmo-max-of-list file-list) 0) (length file-list))
262       (sort file-list '<))))
263
264 (defun elmo-archive-list-folder (spec)
265   (let* ((type (nth 2 spec))
266          (prefix (nth 3 spec))
267          (arc (elmo-archive-get-archive-name (nth 1 spec) type spec))
268          (killed (and elmo-use-killed-list
269                       (elmo-msgdb-killed-list-load
270                        (elmo-msgdb-expand-path nil spec))))
271          numbers)
272     (setq numbers (elmo-archive-list-folder-subr arc type prefix))
273     (if killed
274         (delq nil
275               (mapcar (lambda (number)
276                         (unless (memq number killed) number))
277                       numbers))
278       numbers)))
279
280 (defun elmo-archive-max-of-folder (spec)
281   (let* ((type (nth 2 spec))
282          (prefix (nth 3 spec))
283          (arc (elmo-archive-get-archive-name (nth 1 spec) type spec)))
284     (elmo-archive-list-folder-subr arc type prefix t)))
285
286
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 ;;; Folder related functions
289
290 (defsubst elmo-archive-get-archive-directory (name)
291   ;; allow fullpath. return format is "/foo/bar/".
292   (if (file-name-absolute-p name)
293       (if (find-file-name-handler name 'copy-file)
294           name
295         (expand-file-name name))
296     (expand-file-name name elmo-archive-folder-path)))
297
298 (defun elmo-archive-get-archive-name (folder type &optional spec)
299   (let ((dir (elmo-archive-get-archive-directory folder))
300         (suffix (elmo-archive-get-suffix type))
301         filename dbdir)
302     (if elmo-archive-treat-file
303         (if (string-match (concat (regexp-quote suffix) "$") folder)
304             (expand-file-name
305              folder
306              elmo-archive-folder-path)
307           (expand-file-name
308            (concat folder suffix)
309            elmo-archive-folder-path))
310       (if (and (let ((handler (find-file-name-handler dir 'copy-file))) ; dir is local.
311                  (or (not handler)
312                      (if (featurep 'xemacs)
313                          (eq handler 'dired-handler-fn))))
314                (or (not (file-exists-p dir))
315                    (file-directory-p dir)))
316           (expand-file-name
317            (concat elmo-archive-basename suffix)
318            dir)
319         ;; for full-path specification.
320         (if (and (find-file-name-handler dir 'copy-file) ; ange-ftp, efs
321                  spec)
322             (progn
323               (setq filename (expand-file-name
324                               (concat elmo-archive-basename suffix)
325                               (setq dbdir (elmo-msgdb-expand-path nil spec))))
326               (if (file-directory-p dbdir)
327                   (); ok.
328                 (if (file-exists-p dbdir)
329                     (error "File %s already exists" dbdir)
330                   (elmo-make-directory dbdir)))
331               (if (not (file-exists-p filename))
332                   (copy-file
333                    (if (file-directory-p dir)
334                        (expand-file-name
335                         (concat elmo-archive-basename suffix)
336                         dir)
337                      dir)
338                    filename))
339               filename)
340           dir)))))
341
342 (defun elmo-archive-folder-exists-p (spec)
343   (file-exists-p
344    (elmo-archive-get-archive-name (nth 1 spec) (nth 2 spec) spec)))
345
346 (defun elmo-archive-folder-creatable-p (spec)
347   t)
348
349 (defun elmo-archive-create-folder (spec)
350   (let* ((dir (directory-file-name ;; remove tail slash.
351                (elmo-archive-get-archive-directory (nth 1 spec))))
352          (type (nth 2 spec))
353          (arc (elmo-archive-get-archive-name (nth 1 spec) type)))
354     (if elmo-archive-treat-file
355         (setq dir (directory-file-name (file-name-directory dir))))
356     (cond ((and (file-exists-p dir)
357                 (not (file-directory-p dir)))
358            ;; file exists
359            (error "Create folder failed; File \"%s\" exists" dir))
360           ((file-directory-p dir)
361            (if (file-exists-p arc)
362                t  ; return value
363              (elmo-archive-create-file arc type spec)))
364           (t
365            (elmo-make-directory dir)
366            (elmo-archive-create-file arc type spec)
367            t))))
368
369 (defun elmo-archive-create-file (archive type spec)
370   (save-excursion
371     (let* ((tmp-dir (directory-file-name
372                      (elmo-msgdb-expand-path nil spec)))
373            (dummy elmo-archive-dummy-file)
374            (method (or (elmo-archive-get-method type 'create)
375                        (elmo-archive-get-method type 'mv)))
376            (args (list archive dummy)))
377       (when (null method)
378         (ding)
379         (error "WARNING: read-only mode: %s (method undefined)" type))
380       (cond
381        ((file-directory-p tmp-dir)
382         ()) ;nop
383        ((file-exists-p tmp-dir)
384         ;; file exists
385         (error "Create directory failed; File \"%s\" exists" tmp-dir))
386        (t
387         (elmo-make-directory tmp-dir)))
388       (elmo-bind-directory
389        tmp-dir
390        (write-region (point) (point) dummy nil 'no-msg)
391        (prog1
392            (elmo-archive-call-method method args)
393          (if (file-exists-p dummy)
394              (delete-file dummy)))
395        ))))
396
397 (defun elmo-archive-delete-folder (spec)
398   (let* ((arc (elmo-archive-get-archive-name (nth 1 spec) (nth 2 spec))))
399     (if (not (file-exists-p arc))
400         (error "no such file: %s" arc)
401       (delete-file arc)
402       t)))
403
404 (defun elmo-archive-rename-folder (old-spec new-spec)
405   (let* ((old-arc (elmo-archive-get-archive-name
406                    (nth 1 old-spec) (nth 2 old-spec)))
407          (new-arc (elmo-archive-get-archive-name
408                    (nth 1 new-spec) (nth 2 new-spec))))
409     (unless (and (eq (nth 2 old-spec) (nth 2 new-spec))
410                  (equal (nth 3 old-spec) (nth 3 new-spec)))
411       (error "not same archive type and prefix"))
412     (if (not (file-exists-p old-arc))
413         (error "no such file: %s" old-arc)
414       (if (file-exists-p new-arc)
415           (error "already exists: %s" new-arc)
416         (rename-file old-arc new-arc)
417         t))))
418
419 (defun elmo-archive-list-folders (spec &optional hierarchy)
420   (let ((folder (concat "$" (nth 1 spec)))
421         (elmo-localdir-folder-path elmo-archive-folder-path))
422     (if elmo-archive-treat-file
423         (let* ((path (elmo-localdir-get-folder-directory spec))
424                (base-folder (or (nth 1 spec) ""))
425                (suffix (nth 2 spec))
426                (prefix (if (string= (nth 3 spec) "")
427                            "" (concat ";" (nth 3 spec))))
428                (dir (if (file-directory-p path)
429                         path (file-name-directory path)))
430                (name (if (file-directory-p path)
431                          "" (file-name-nondirectory path)))
432                (flist (and (file-directory-p dir)
433                            (directory-files dir nil name nil)))
434                (regexp (format "^\\(.*\\)\\(%s\\)$"
435                                (mapconcat
436                                 '(lambda (x) (regexp-quote (cdr x)))
437                                 elmo-archive-suffix-alist
438                                 "\\|"))))
439           (if (string-match "\\(.*\\)/$" base-folder) ; ends with '/'.
440               (setq base-folder (elmo-match-string 1 base-folder))
441             (unless (file-directory-p path)
442               (setq base-folder (or (file-name-directory base-folder)
443                                     base-folder))))
444           (delq
445            nil
446            (mapcar
447             '(lambda (x)
448                (when (and (string-match regexp x)
449                           (eq suffix
450                               (car
451                                (rassoc (elmo-match-string 2 x)
452                                        elmo-archive-suffix-alist))))
453                  (format "$%s;%s%s"
454                          (elmo-concat-path base-folder (elmo-match-string 1 x))
455                          suffix prefix)))
456             flist)))
457       (elmo-localdir-list-folders-subr folder hierarchy))))
458
459
460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
461 ;;; Article file related functions
462 ;;; read(extract) / append(move) / delete(delete) / query(list)
463
464 (defun elmo-archive-read-msg (spec number outbuf)
465   (save-excursion
466     (let* ((type (nth 2 spec))
467            (arc (elmo-archive-get-archive-name (nth 1 spec) type spec))
468            (prefix (nth 3 spec))
469            (method (elmo-archive-get-method type 'cat))
470            (args (list arc (elmo-concat-path
471                             prefix (int-to-string number)))))
472       (set-buffer outbuf)
473       (erase-buffer)
474       (when (file-exists-p arc)
475         (and
476          (as-binary-process
477           (elmo-archive-call-method method args t))
478          (elmo-delete-cr-get-content-type))))))
479
480 (defun elmo-archive-append-msg (spec string &optional msg no-see) ;;; verrrrrry slow!!
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 nil 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 nil 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 nil 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 (defsubst elmo-archive-msgdb-create-entity (method archive number type &optional prefix) ;; verrrry slow!!
779   (let* ((msg (elmo-concat-path prefix (int-to-string number)))
780          (arg-list (list archive msg)))
781     (when (elmo-archive-article-exists-p archive msg type)
782       ;; insert article.
783       (as-binary-process
784        (elmo-archive-call-method method arg-list t))
785       (elmo-archive-msgdb-create-entity-subr number))))
786
787 (defun elmo-archive-msgdb-create-as-numlist (spec numlist new-mark
788                                                   already-mark seen-mark
789                                                   important-mark seen-list)
790   (when numlist
791     (save-excursion ;; 981005
792       (if (and elmo-archive-use-izip-agent
793                (elmo-archive-get-method (nth 2 spec) 'cat-headers))
794           (elmo-archive-msgdb-create-as-numlist-subr2
795            spec numlist new-mark already-mark seen-mark important-mark
796            seen-list)
797         (elmo-archive-msgdb-create-as-numlist-subr1
798          spec numlist new-mark already-mark seen-mark important-mark
799          seen-list)))))
800
801 (defalias 'elmo-archive-msgdb-create 'elmo-archive-msgdb-create-as-numlist)
802
803
804 (defun elmo-archive-msgdb-create-as-numlist-subr1 (spec numlist new-mark
805                                                         already-mark seen-mark
806                                                         important-mark
807                                                         seen-list)
808   (let* ((type (nth 2 spec))
809          (file (elmo-archive-get-archive-name (nth 1 spec) type spec))
810          (method (elmo-archive-get-method type 'cat))
811          (tmp-buf (get-buffer-create " *ELMO ARCHIVE msgdb*"))
812          overview number-alist mark-alist entity
813          i percent num message-id seen gmark)
814     (save-excursion
815       (set-buffer tmp-buf)
816       (setq num (length numlist))
817       (setq i 0)
818       (message "Creating msgdb...")
819       (while numlist
820         (erase-buffer)
821         (setq entity
822               (elmo-archive-msgdb-create-entity
823                method file (car numlist) type (nth 3 spec)))
824         (when entity
825           (setq overview
826                 (elmo-msgdb-append-element
827                  overview entity))
828           (setq number-alist
829                 (elmo-msgdb-number-add
830                  number-alist
831                  (elmo-msgdb-overview-entity-get-number entity)
832                  (car entity)))
833           (setq message-id (car entity))
834           (setq seen (member message-id seen-list))
835           (if (setq gmark
836                     (or (elmo-msgdb-global-mark-get message-id)
837                         (if (elmo-cache-exists-p message-id) ; XXX
838                             (if seen
839                                 nil
840                               already-mark)
841                           (if seen
842                               seen-mark
843                             new-mark))))
844               (setq mark-alist
845                     (elmo-msgdb-mark-append
846                      mark-alist
847                      (elmo-msgdb-overview-entity-get-number entity)
848                      gmark))))
849         (when (> num elmo-display-progress-threshold)
850           (setq i (1+ i))
851           (setq percent (/ (* i 100) num))
852           (elmo-display-progress
853            'elmo-archive-msgdb-create-as-numlist-subr1 "Creating msgdb..."
854            percent))
855         (setq numlist (cdr numlist)))
856       (kill-buffer tmp-buf)
857       (message "Creating msgdb...done.")
858       (list overview number-alist mark-alist)) ))
859
860 ;;; info-zip agent
861 (defun elmo-archive-msgdb-create-as-numlist-subr2 (spec numlist new-mark
862                                                         already-mark seen-mark
863                                                         important-mark
864                                                         seen-list)
865   (let* ((buf (get-buffer-create " *ELMO ARCHIVE headers*"))
866          (delim1 elmo-mmdf-delimiter)           ;; MMDF
867          (delim2 elmo-unixmail-delimiter)       ;; UNIX Mail
868          (type (nth 2 spec))
869          (prefix (nth 3 spec))
870          (method (elmo-archive-get-method type 'cat-headers))
871          (prog (car method))
872          (args (cdr method))
873          (arc (elmo-archive-get-archive-name (nth 1 spec) type))
874          n i percent num result overview number-alist mark-alist
875          msgs case-fold-search)
876     (set-buffer buf)
877     (setq num (length numlist))
878     (setq i 0)
879     (message "Creating msgdb...")
880     (while numlist
881       (setq n (min (1- elmo-archive-fetch-headers-volume)
882                    (1- (length numlist))))
883       (setq msgs (reverse (memq (nth n numlist) (reverse numlist))))
884       (setq numlist (nthcdr (1+ n) numlist))
885       (erase-buffer)
886       (insert
887        (mapconcat
888         'concat
889         (mapcar '(lambda (x) (elmo-concat-path prefix (int-to-string x))) msgs)
890         "\n"))
891       (message "Fetching headers...")
892       (as-binary-process (apply 'call-process-region
893                                 (point-min) (point-max)
894                                 prog t t nil (append args (list arc))))
895       (goto-char (point-min))
896       (cond
897        ((looking-at delim1)     ;; MMDF
898         (setq result (elmo-archive-parse-mmdf msgs
899                                               new-mark
900                                               already-mark seen-mark
901                                               seen-list))
902         (setq overview (append overview (nth 0 result)))
903         (setq number-alist (append number-alist (nth 1 result)))
904         (setq mark-alist (append mark-alist (nth 2 result))))
905 ;      ((looking-at delim2)     ;; UNIX MAIL
906 ;       (setq result (elmo-archive-parse-unixmail msgs))
907 ;       (setq overview (append overview (nth 0 result)))
908 ;       (setq number-alist (append number-alist (nth 1 result)))
909 ;       (setq mark-alist (append mark-alist (nth 2 result))))
910        (t                       ;; unknown format
911         (error "unknown format!")))
912       (when (> num elmo-display-progress-threshold)
913         (setq i (+ n i))
914         (setq percent (/ (* i 100) num))
915         (elmo-display-progress
916          'elmo-archive-msgdb-create-as-numlist-subr2 "Creating msgdb..."
917          percent)))
918     (kill-buffer buf)
919     (list overview number-alist mark-alist)) )
920
921 (defun elmo-archive-parse-mmdf (msgs new-mark
922                                      already-mark
923                                      seen-mark
924                                      seen-list)
925   (let ((delim elmo-mmdf-delimiter)
926         number sp ep rest entity overview number-alist mark-alist ret-val
927         message-id seen gmark)
928     (goto-char (point-min))
929     (setq rest msgs)
930     (while (and rest (re-search-forward delim nil t)
931                 (not (eobp)))
932       (setq number (car rest))
933       (setq sp (1+ (point)))
934       (setq ep (prog2 (re-search-forward delim)
935                    (1+ (- (point) (length delim)))))
936       (if (>= sp ep) ; no article!
937           ()  ; nop
938         (save-excursion
939           (narrow-to-region sp ep)
940           (setq entity (elmo-archive-msgdb-create-entity-subr number))
941           (setq overview
942                 (elmo-msgdb-append-element
943                  overview entity))
944           (setq number-alist
945                 (elmo-msgdb-number-add
946                  number-alist
947                  (elmo-msgdb-overview-entity-get-number entity)
948                  (car entity)))
949           (setq message-id (car entity))
950           (setq seen (member message-id seen-list))
951           (if (setq gmark
952                     (or (elmo-msgdb-global-mark-get message-id)
953                         (if (elmo-cache-exists-p message-id) ; XXX
954                             (if seen
955                                 nil
956                               already-mark)
957                           (if seen
958                               seen-mark
959                             new-mark))))
960               (setq mark-alist
961                     (elmo-msgdb-mark-append
962                      mark-alist
963                      (elmo-msgdb-overview-entity-get-number entity)
964                      gmark)))
965           (setq ret-val (append ret-val (list overview number-alist mark-alist)))
966           (widen)))
967       (forward-line 1)
968       (setq rest (cdr rest)))
969     ret-val))
970
971
972 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
973 ;;; Search functions
974
975 (defsubst elmo-archive-field-condition-match (spec number condition prefix)
976   (save-excursion
977     (let* ((type (nth 2 spec))
978            (arc (elmo-archive-get-archive-name (nth 1 spec) type spec))
979            (method (elmo-archive-get-method type 'cat))
980            (args (list arc (elmo-concat-path prefix (int-to-string number)))))
981       (elmo-set-work-buf
982        (when (file-exists-p arc)
983          (as-binary-process
984           (elmo-archive-call-method method args t))
985          (elmo-set-buffer-multibyte default-enable-multibyte-characters)
986          (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
987          (elmo-buffer-field-condition-match condition))))))
988
989 (defun elmo-archive-search (spec condition &optional from-msgs)
990   (let* (;;(args (elmo-string-to-list key))
991          ;; XXX: I don't know whether `elmo-archive-list-folder'
992          ;;      updates match-data.
993          ;; (msgs (or from-msgs (elmo-archive-list-folder spec)))
994          (msgs (or from-msgs (elmo-archive-list-folder spec)))
995          (num (length msgs))
996          (i 0)
997          (case-fold-search nil)
998          ret-val)
999     (while msgs
1000       (if (elmo-archive-field-condition-match spec (car msgs)
1001                                               condition
1002                                               (nth 3 spec))
1003           (setq ret-val (cons (car msgs) ret-val)))
1004       (when (> num elmo-display-progress-threshold)
1005         (setq i (1+ i))
1006         (elmo-display-progress
1007          'elmo-archive-search "Searching..."
1008          (/ (* i 100) num)))
1009       (setq msgs (cdr msgs)))
1010     (nreverse ret-val)))
1011
1012
1013 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1014 ;;; Misc functions
1015
1016 (defun elmo-archive-check-validity (spec validity-file)
1017   t) ; ok.
1018
1019 (defun elmo-archive-sync-validity (spec validity-file)
1020   t) ; ok.
1021
1022 \f
1023 ;;; method(alist)
1024 (if (null elmo-archive-method-alist)
1025     (let ((mlist elmo-archive-method-list) ; from mew-highlight.el
1026           method type str)
1027       (while mlist
1028         (setq method (car mlist))
1029         (setq mlist (cdr mlist))
1030         (setq str (symbol-name method))
1031         (string-match "elmo-archive-\\([^-].*\\)-method-alist$" str)
1032         (setq type (intern-soft
1033                     (elmo-match-string 1 str)))
1034         (setq elmo-archive-method-alist
1035               (cons (cons type
1036                           (symbol-value method))
1037                     elmo-archive-method-alist)))))
1038
1039 ;;; valid suffix(list)
1040 (if (null elmo-archive-suffixes)
1041     (let ((slist elmo-archive-suffix-alist)
1042           tmp)
1043       (while slist
1044         (setq tmp (car slist))
1045         (setq elmo-archive-suffixes
1046               (nconc elmo-archive-suffixes (list (cdr tmp))))
1047         (setq slist (cdr slist)))))
1048
1049 (defun elmo-archive-use-cache-p (spec number)
1050   elmo-archive-use-cache)
1051
1052 (defun elmo-archive-local-file-p (spec number)
1053   nil)
1054
1055 (defun elmo-archive-get-msg-filename (spec number &optional loc-alist)
1056   (let ((tmp-dir (file-name-as-directory (elmo-msgdb-expand-path nil spec)))
1057         (prefix (nth 3 spec)))
1058     (expand-file-name
1059      (elmo-concat-path prefix (int-to-string number))
1060      tmp-dir)))
1061
1062 (defalias 'elmo-archive-sync-number-alist
1063   'elmo-generic-sync-number-alist)
1064 (defalias 'elmo-archive-list-folder-unread
1065   'elmo-generic-list-folder-unread)
1066 (defalias 'elmo-archive-list-folder-important
1067   'elmo-generic-list-folder-important)
1068 (defalias 'elmo-archive-commit 'elmo-generic-commit)
1069
1070 ;;; End
1071 (run-hooks 'elmo-archive-load-hook)
1072 (provide 'elmo-archive)
1073
1074 ;;; elmo-archive.el ends here