* elmo-net.el (elmo-network-session-password-key): Don't set default to 'plain.
[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 ;; [\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 (spec &optional nonsort)
235   "*Returns list of number-file(int, not string) in archive FILE.
236 TYPE specifies the archiver's symbol."
237   (let* ((type (nth 2 spec))
238          (prefix (nth 3 spec))
239          (file (elmo-archive-get-archive-name (nth 1 spec) type spec))
240          (method (elmo-archive-get-method type 'ls))
241          (args (list file))
242          (file-regexp (format (elmo-archive-get-regexp type)
243                               (elmo-concat-path (regexp-quote prefix) "")))
244          (killed (and elmo-use-killed-list
245                       (elmo-msgdb-killed-list-load
246                        (elmo-msgdb-expand-path spec))))
247          numbers buf file-list header-end)
248     (when (file-exists-p file)
249       (save-excursion
250         (set-buffer (setq buf (get-buffer-create " *ELMO ARCHIVE ls*")))
251         (unless (elmo-archive-call-method method args t)
252           (error "%s exited abnormally!" method))
253         (goto-char (point-min))
254         (when (re-search-forward elmo-archive-header-regexp nil t)
255           (forward-line 1)
256           (setq header-end (point))
257           (when (re-search-forward elmo-archive-header-regexp nil t)
258               (beginning-of-line)
259               (narrow-to-region header-end (point))
260               (goto-char (point-min))))
261         (while (and (re-search-forward file-regexp nil t)
262                     (not (eobp)))  ; for GNU tar 981010
263           (setq file-list (nconc file-list (list (string-to-int
264                                                   (match-string 1))))))
265         (kill-buffer buf)))
266     (if nonsort
267         (cons (or (elmo-max-of-list file-list) 0)
268               (if killed
269                   (- (length file-list)
270                      (elmo-msgdb-killed-list-length killed))
271                 (length file-list)))
272       (setq numbers (sort file-list '<))
273       (elmo-living-messages numbers killed))))
274
275 (defun elmo-archive-list-folder (spec)
276   (elmo-archive-list-folder-subr spec))
277
278 (defun elmo-archive-max-of-folder (spec)
279   (elmo-archive-list-folder-subr spec t))
280
281
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 ;;; Folder related functions
284
285 (defsubst elmo-archive-get-archive-directory (name)
286   ;; allow fullpath. return format is "/foo/bar/".
287   (if (file-name-absolute-p name)
288       (if (find-file-name-handler name 'copy-file)
289           name
290         (expand-file-name name))
291     (expand-file-name name elmo-archive-folder-path)))
292
293 (defun elmo-archive-get-archive-name (folder type &optional spec)
294   (let ((dir (elmo-archive-get-archive-directory folder))
295         (suffix (elmo-archive-get-suffix type))
296         filename dbdir)
297     (if elmo-archive-treat-file
298         (if (string-match (concat (regexp-quote suffix) "$") folder)
299             (expand-file-name
300              folder
301              elmo-archive-folder-path)
302           (expand-file-name
303            (concat folder suffix)
304            elmo-archive-folder-path))
305       (if (and (let ((handler (find-file-name-handler dir 'copy-file))) ; dir is local.
306                  (or (not handler)
307                      (if (featurep 'xemacs)
308                          (eq handler 'dired-handler-fn))))
309                (or (not (file-exists-p dir))
310                    (file-directory-p dir)))
311           (expand-file-name
312            (concat elmo-archive-basename suffix)
313            dir)
314         ;; for full-path specification.
315         (if (and (find-file-name-handler dir 'copy-file) ; ange-ftp, efs
316                  spec)
317             (progn
318               (setq filename (expand-file-name
319                               (concat elmo-archive-basename suffix)
320                               (setq dbdir (elmo-msgdb-expand-path spec))))
321               (if (file-directory-p dbdir)
322                   (); ok.
323                 (if (file-exists-p dbdir)
324                     (error "File %s already exists" dbdir)
325                   (elmo-make-directory dbdir)))
326               (if (not (file-exists-p filename))
327                   (copy-file
328                    (if (file-directory-p dir)
329                        (expand-file-name
330                         (concat elmo-archive-basename suffix)
331                         dir)
332                      dir)
333                    filename))
334               filename)
335           dir)))))
336
337 (defun elmo-archive-folder-exists-p (spec)
338   (file-exists-p
339    (elmo-archive-get-archive-name (nth 1 spec) (nth 2 spec) spec)))
340
341 (defun elmo-archive-folder-creatable-p (spec)
342   t)
343
344 (defun elmo-archive-create-folder (spec)
345   (let* ((dir (directory-file-name      ; remove tail slash.
346                (elmo-archive-get-archive-directory (nth 1 spec))))
347          (type (nth 2 spec))
348          (arc (elmo-archive-get-archive-name (nth 1 spec) type)))
349     (if elmo-archive-treat-file
350         (setq dir (directory-file-name (file-name-directory dir))))
351     (cond ((and (file-exists-p dir)
352                 (not (file-directory-p dir)))
353            ;; file exists
354            (error "Create folder failed; File \"%s\" exists" dir))
355           ((file-directory-p dir)
356            (if (file-exists-p arc)
357                t                        ; return value
358              (elmo-archive-create-file arc type spec)))
359           (t
360            (elmo-make-directory dir)
361            (elmo-archive-create-file arc type spec)
362            t))))
363
364 (defun elmo-archive-create-file (archive type spec)
365   (save-excursion
366     (let* ((tmp-dir (directory-file-name
367                      (elmo-msgdb-expand-path spec)))
368            (dummy elmo-archive-dummy-file)
369            (method (or (elmo-archive-get-method type 'create)
370                        (elmo-archive-get-method type 'mv)))
371            (args (list archive dummy)))
372       (when (null method)
373         (ding)
374         (error "WARNING: read-only mode: %s (method undefined)" type))
375       (cond
376        ((file-directory-p tmp-dir)
377         ()) ;nop
378        ((file-exists-p tmp-dir)
379         ;; file exists
380         (error "Create directory failed; File \"%s\" exists" tmp-dir))
381        (t
382         (elmo-make-directory tmp-dir)))
383       (elmo-bind-directory
384        tmp-dir
385        (write-region (point) (point) dummy nil 'no-msg)
386        (prog1
387            (elmo-archive-call-method method args)
388          (if (file-exists-p dummy)
389              (delete-file dummy)))
390        ))))
391
392 (defun elmo-archive-delete-folder (spec)
393   (let* ((arc (elmo-archive-get-archive-name (nth 1 spec) (nth 2 spec))))
394     (if (not (file-exists-p arc))
395         (error "No such file: %s" arc)
396       (delete-file arc)
397       t)))
398
399 (defun elmo-archive-rename-folder (old-spec new-spec)
400   (let* ((old-arc (elmo-archive-get-archive-name
401                    (nth 1 old-spec) (nth 2 old-spec)))
402          (new-arc (elmo-archive-get-archive-name
403                    (nth 1 new-spec) (nth 2 new-spec))))
404     (unless (and (eq (nth 2 old-spec) (nth 2 new-spec))
405                  (equal (nth 3 old-spec) (nth 3 new-spec)))
406       (error "Not same archive type and prefix"))
407     (if (not (file-exists-p old-arc))
408         (error "No such file: %s" old-arc)
409       (if (file-exists-p new-arc)
410           (error "Already exists: %s" new-arc)
411         (rename-file old-arc new-arc)
412         t))))
413
414 (defun elmo-archive-list-folders (spec &optional hierarchy)
415   (let ((folder (concat "$" (nth 1 spec)))
416         (elmo-localdir-folder-path elmo-archive-folder-path))
417     (if elmo-archive-treat-file
418         (let* ((path (elmo-localdir-get-folder-directory spec))
419                (base-folder (or (nth 1 spec) ""))
420                (suffix (nth 2 spec))
421                (prefix (if (string= (nth 3 spec) "")
422                            "" (concat ";" (nth 3 spec))))
423                (dir (if (file-directory-p path)
424                         path (file-name-directory path)))
425                (name (if (file-directory-p path)
426                          "" (file-name-nondirectory path)))
427                (flist (and (file-directory-p dir)
428                            (directory-files dir nil name nil)))
429                (regexp (format "^\\(.*\\)\\(%s\\)$"
430                                (mapconcat
431                                 '(lambda (x) (regexp-quote (cdr x)))
432                                 elmo-archive-suffix-alist
433                                 "\\|"))))
434           (if (string-match "\\(.*\\)/$" base-folder) ; ends with '/'.
435               (setq base-folder (elmo-match-string 1 base-folder))
436             (unless (file-directory-p path)
437               (setq base-folder (or (file-name-directory base-folder)
438                                     base-folder))))
439           (delq
440            nil
441            (mapcar
442             '(lambda (x)
443                (when (and (string-match regexp x)
444                           (eq suffix
445                               (car
446                                (rassoc (elmo-match-string 2 x)
447                                        elmo-archive-suffix-alist))))
448                  (format "$%s;%s%s"
449                          (elmo-concat-path base-folder (elmo-match-string 1 x))
450                          suffix prefix)))
451             flist)))
452       (elmo-localdir-list-folders-subr folder hierarchy))))
453
454
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
456 ;;; Article file related functions
457 ;;; read(extract) / append(move) / delete(delete) / query(list)
458
459 (defun elmo-archive-read-msg (spec number outbuf)
460   (save-excursion
461     (let* ((type (nth 2 spec))
462            (arc (elmo-archive-get-archive-name (nth 1 spec) type spec))
463            (prefix (nth 3 spec))
464            (method (elmo-archive-get-method type 'cat))
465            (args (list arc (elmo-concat-path
466                             prefix (int-to-string number)))))
467       (set-buffer outbuf)
468       (erase-buffer)
469       (when (file-exists-p arc)
470         (and
471          (as-binary-process
472           (elmo-archive-call-method method args t))
473          (elmo-delete-cr-get-content-type))))))
474
475 ;; verrrrrry slow!!
476 (defun elmo-archive-append-msg (spec string &optional msg no-see)
477   (let* ((type (nth 2 spec))
478          (prefix (nth 3 spec))
479          (arc (elmo-archive-get-archive-name (nth 1 spec) type))
480          (method (elmo-archive-get-method type 'mv))
481          (tmp-buffer (get-buffer-create " *ELMO ARCHIVE mv*"))
482          (next-num (or msg
483                        (1+ (if (file-exists-p arc)
484                                (car (elmo-archive-max-of-folder spec)) 0))))
485          (tmp-dir (elmo-msgdb-expand-path spec))
486          newfile)
487     (when (null method)
488       (ding)
489       (error "WARNING: read-only mode: %s (method undefined)" type))
490     (save-excursion
491       (set-buffer tmp-buffer)
492       (erase-buffer)
493       (let ((tmp-dir (expand-file-name prefix tmp-dir)))
494         (when (not (file-directory-p tmp-dir))
495           (elmo-make-directory (directory-file-name tmp-dir))))
496       (setq newfile (elmo-concat-path
497                      prefix
498                      (int-to-string next-num)))
499       (unwind-protect
500           (elmo-bind-directory
501            tmp-dir
502            (if (and (or (functionp method) (car method))
503                     (file-writable-p newfile))
504                (progn
505                  (insert string)
506                  (as-binary-output-file
507                   (write-region (point-min) (point-max) newfile nil 'no-msg))
508                  (elmo-archive-call-method method (list arc newfile)))
509              nil))
510         (kill-buffer tmp-buffer)))))
511
512 ;; (localdir, maildir, localnews, archive) -> archive
513 (defun elmo-archive-copy-msgs (dst-spec msgs src-spec
514                                         &optional loc-alist same-number)
515   (let* ((dst-type (nth 2 dst-spec))
516          (arc (elmo-archive-get-archive-name (nth 1 dst-spec) dst-type))
517          (prefix (nth 3 dst-spec))
518          (p-method (elmo-archive-get-method dst-type 'mv-pipe))
519          (n-method (elmo-archive-get-method dst-type 'mv))
520          (new (unless same-number
521                 (1+ (car (elmo-archive-max-of-folder dst-spec)))))
522          (src-dir (elmo-localdir-get-folder-directory src-spec))
523          (tmp-dir
524           (file-name-as-directory (elmo-msgdb-expand-path dst-spec)))
525          (do-link t)
526          src tmp newfile tmp-msgs)
527     (when (not (elmo-archive-folder-exists-p dst-spec))
528       (elmo-archive-create-folder dst-spec))
529     (when (null (or p-method n-method))
530       (ding)
531       (error "WARNING: read-only mode: %s (method undefined)" dst-type))
532     (when (and same-number
533                (not (eq (car src-spec) 'maildir))
534                (string-match (concat prefix "$") src-dir)
535                (or
536                 (elmo-archive-get-method dst-type 'cp-pipe)
537                 (elmo-archive-get-method dst-type 'cp)))
538       (setq tmp-dir (substring src-dir 0 (match-beginning 0)))
539       (setq p-method (elmo-archive-get-method dst-type 'cp-pipe)
540             n-method (elmo-archive-get-method dst-type 'cp))
541       (setq tmp-msgs (mapcar '(lambda (x)
542                                 (elmo-concat-path prefix (int-to-string x)))
543                              msgs))
544       (setq do-link nil))
545     (when do-link
546       (let ((tmp-dir (expand-file-name prefix tmp-dir)))
547         (when (not (file-directory-p tmp-dir))
548           (elmo-make-directory (directory-file-name tmp-dir))))
549       (while msgs
550         (setq newfile (elmo-concat-path prefix (int-to-string
551                                                 (if same-number
552                                                     (car msgs)
553                                                   new))))
554         (setq tmp-msgs (nconc tmp-msgs (list newfile)))
555         (elmo-copy-file
556          ;; src file
557          (elmo-call-func src-spec "get-msg-filename" (car msgs) loc-alist)
558          ;; tmp file
559          (expand-file-name newfile tmp-dir))
560         (setq msgs (cdr msgs))
561         (unless same-number (setq new (1+ new)))))
562     (save-excursion
563       (elmo-bind-directory
564        tmp-dir
565        (cond
566         ((functionp n-method)
567          (funcall n-method (cons arc tmp-msgs)))
568         (p-method
569          (let ((p-prog (car p-method))
570                (p-prog-arg (cdr p-method)))
571            (elmo-archive-exec-msgs-subr1
572             p-prog (append p-prog-arg (list arc)) tmp-msgs)))
573         (t
574          (let ((n-prog (car n-method))
575                (n-prog-arg (cdr n-method)))
576            (elmo-archive-exec-msgs-subr2
577             n-prog (append n-prog-arg (list arc)) tmp-msgs (length arc)))))))))
578
579 ;;; archive -> (localdir, localnews, archive)
580 (defun elmo-archive-copy-msgs-froms (dst-spec msgs src-spec
581                                               &optional loc-alist same-number)
582   (let* ((src-type (nth 2 src-spec))
583          (arc (elmo-archive-get-archive-name (nth 1 src-spec) src-type))
584          (prefix (nth 3 src-spec))
585          (p-method (elmo-archive-get-method src-type 'ext-pipe))
586          (n-method (elmo-archive-get-method src-type 'ext))
587          (tmp-dir
588           (file-name-as-directory (elmo-msgdb-expand-path src-spec)))
589          (tmp-msgs (mapcar '(lambda (x) (elmo-concat-path
590                                          prefix
591                                          (int-to-string x)))
592                            msgs))
593          result)
594     (unwind-protect
595         (setq result
596               (and
597                ;; extract messages
598                (save-excursion
599                  (elmo-bind-directory
600                   tmp-dir
601                   (cond
602                    ((functionp n-method)
603                     (funcall n-method (cons arc tmp-msgs)))
604                    (p-method
605                     (let ((p-prog (car p-method))
606                           (p-prog-arg (cdr p-method)))
607                       (elmo-archive-exec-msgs-subr1
608                        p-prog (append p-prog-arg (list arc)) tmp-msgs)))
609                    (t
610                     (let ((n-prog (car n-method))
611                           (n-prog-arg (cdr n-method)))
612                       (elmo-archive-exec-msgs-subr2
613                        n-prog (append n-prog-arg (list arc)) tmp-msgs (length arc)))))))
614                ;; call elmo-*-copy-msgs of destination folder
615                (elmo-call-func dst-spec "copy-msgs"
616                                msgs src-spec loc-alist same-number)))
617       ;; clean up tmp-dir
618       (elmo-bind-directory
619        tmp-dir
620        (while tmp-msgs
621          (if (file-exists-p (car tmp-msgs))
622              (delete-file (car tmp-msgs)))
623          (setq tmp-msgs (cdr tmp-msgs))))
624       result)))
625
626 (defun elmo-archive-delete-msgs (spec msgs)
627   (save-excursion
628     (let* ((type (nth 2 spec))
629            (prefix (nth 3 spec))
630            (arc (elmo-archive-get-archive-name (nth 1 spec) type))
631            (p-method (elmo-archive-get-method type 'rm-pipe))
632            (n-method (elmo-archive-get-method type 'rm))
633            (msgs (mapcar '(lambda (x) (elmo-concat-path
634                                        prefix
635                                        (int-to-string x)))
636                          msgs)))
637       (cond ((functionp n-method)
638              (funcall n-method (cons arc msgs)))
639             (p-method
640              (let ((p-prog (car p-method))
641                    (p-prog-arg (cdr p-method)))
642                (elmo-archive-exec-msgs-subr1
643                 p-prog (append p-prog-arg (list arc)) msgs)))
644             (n-method
645              (let ((n-prog (car n-method))
646                    (n-prog-arg (cdr n-method)))
647                (elmo-archive-exec-msgs-subr2
648                 n-prog (append n-prog-arg (list arc)) msgs (length arc))))
649             (t
650              (ding)
651              (error "WARNING: not delete: %s (method undefined)" type))) )))
652
653 (defun elmo-archive-exec-msgs-subr1 (prog args msgs)
654   (let ((buf (get-buffer-create " *ELMO ARCHIVE exec*")))
655     (set-buffer buf)
656     (insert (mapconcat 'concat msgs "\n")) ;string
657     (unwind-protect
658         (= 0
659            (apply 'call-process-region (point-min) (point-max)
660                   prog nil nil nil args))
661       (kill-buffer buf))))
662
663 (defun elmo-archive-exec-msgs-subr2 (prog args msgs arc-length)
664   (let ((max-len (- elmo-archive-cmdstr-max-length arc-length))
665         (n (length msgs))
666         rest i sum)
667     (setq rest msgs) ;string
668     (setq i 1)
669     (setq sum 0)
670     (catch 'done
671       (while (and rest (<= i n))
672         (mapcar '(lambda (x)
673                    (let* ((len (length x))
674                           (files (member x (reverse rest))))
675                      ;; total(previous) + current + white space
676                      (if (<= max-len (+ sum len 1))
677                          (progn
678                            (unless
679                                (elmo-archive-call-process
680                                 prog (append args files))
681                              (throw 'done nil))
682                            (setq sum 0) ;; reset
683                            (setq rest (nthcdr i rest)))
684                        (setq sum (+ sum len 1)))
685                      (setq i (1+ i)))) msgs))
686       (throw 'done
687              (or (not rest)
688                  (elmo-archive-call-process prog (append args rest))))
689       )))
690
691 (defsubst elmo-archive-article-exists-p (arc msg type)
692   (if (not elmo-archive-check-existance-strict)
693       t ; nop
694     (save-excursion ; added 980915
695       (let* ((method (elmo-archive-get-method type 'ls))
696              (args (list arc msg))
697              (buf (get-buffer-create " *ELMO ARCHIVE query*"))
698              (error-msg "\\(no file\\|0 files\\)")
699              ret-val)
700         (set-buffer buf)
701         (erase-buffer)
702         (elmo-archive-call-method method args t)
703         ;; pointer: point-max
704         (setq ret-val (not (re-search-backward error-msg nil t)))
705         (kill-buffer buf)
706         ret-val))))
707
708 (defun elmo-archive-tgz-common-func (args exec-type &optional copy)
709   (let* ((arc (car args))
710          (tmp-msgs (cdr args))
711          (decompress (elmo-archive-get-method 'tgz 'decompress))
712          (compress (elmo-archive-get-method 'tgz 'compress))
713          (exec (elmo-archive-get-method 'tgz exec-type))
714          (suffix (elmo-archive-get-suffix 'tgz))
715          (tar-suffix (elmo-archive-get-suffix 'tar))
716          arc-tar ret-val
717          )
718     (when (null (and decompress compress exec))
719       (ding)
720       (error "WARNING: special method undefined: %s of %s"
721              (or (if (null decompress) 'decompress)
722                  (if (null compress) 'compress)
723                  (if (null exec) exec-type))
724              'tgz))
725     (unless tar-suffix
726       (ding)
727       (error "WARNING: `tar' suffix undefined"))
728     (if (string-match (concat (regexp-quote suffix) "$") arc)
729         (setq arc-tar
730               (concat (substring arc 0 (match-beginning 0)) tar-suffix))
731       (error "%s: not match suffix [%s]" arc suffix))
732     (and
733      ;; decompress
734      (elmo-archive-call-process
735       (car decompress) (append (cdr decompress) (list arc)))
736      ;; append (or delete)
737      (elmo-archive-exec-msgs-subr2
738       (car exec) (append (cdr exec) (list arc-tar)) tmp-msgs (length arc-tar))
739      ;; compress
740      (setq ret-val
741            (elmo-archive-call-process
742             (car compress) (append (cdr compress) (list arc-tar)))))
743     ;; delete tmporary messages
744     (if (and (not copy)
745              (eq exec-type 'append))
746         (while tmp-msgs
747           (if (file-exists-p (car tmp-msgs))
748               (delete-file (car tmp-msgs)))
749           (setq tmp-msgs (cdr tmp-msgs))))
750     ret-val))
751
752 (defun elmo-archive-tgz-cp-func (args &optional output)
753   (elmo-archive-tgz-common-func args 'append t))
754
755 (defun elmo-archive-tgz-mv-func (args &optional output)
756   (elmo-archive-tgz-common-func args 'append))
757
758 (defun elmo-archive-tgz-rm-func (args &optional output)
759   (elmo-archive-tgz-common-func args 'delete))
760
761 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
762 ;;; MessageDB functions (from elmo-localdir.el)
763
764 (defsubst elmo-archive-msgdb-create-entity-subr (number)
765   (let (header-end)
766     (elmo-set-buffer-multibyte default-enable-multibyte-characters)
767     (goto-char (point-min))
768     (if (re-search-forward "\\(^--.*$\\)\\|\\(\n\n\\)" nil t)
769         (setq header-end (point))
770       (setq header-end (point-max)))
771     (narrow-to-region (point-min) header-end)
772     (elmo-msgdb-create-overview-from-buffer number)))
773
774 ;; verrrry slow!!
775 (defsubst elmo-archive-msgdb-create-entity (method archive number type &optional prefix)
776   (let* ((msg (elmo-concat-path prefix (int-to-string number)))
777          (arg-list (list archive msg)))
778     (when (elmo-archive-article-exists-p archive msg type)
779       ;; insert article.
780       (as-binary-process
781        (elmo-archive-call-method method arg-list t))
782       (elmo-archive-msgdb-create-entity-subr number))))
783
784 (defun elmo-archive-msgdb-create-as-numlist (spec numlist new-mark
785                                                   already-mark seen-mark
786                                                   important-mark seen-list)
787   (when numlist
788     (save-excursion ;; 981005
789       (if (and elmo-archive-use-izip-agent
790                (elmo-archive-get-method (nth 2 spec) 'cat-headers))
791           (elmo-archive-msgdb-create-as-numlist-subr2
792            spec numlist new-mark already-mark seen-mark important-mark
793            seen-list)
794         (elmo-archive-msgdb-create-as-numlist-subr1
795          spec numlist new-mark already-mark seen-mark important-mark
796          seen-list)))))
797
798 (defalias 'elmo-archive-msgdb-create 'elmo-archive-msgdb-create-as-numlist)
799
800
801 (defun elmo-archive-msgdb-create-as-numlist-subr1 (spec numlist new-mark
802                                                         already-mark seen-mark
803                                                         important-mark
804                                                         seen-list)
805   (let* ((type (nth 2 spec))
806          (file (elmo-archive-get-archive-name (nth 1 spec) type spec))
807          (method (elmo-archive-get-method type 'cat))
808          (tmp-buf (get-buffer-create " *ELMO ARCHIVE msgdb*"))
809          overview number-alist mark-alist entity
810          i percent num message-id seen gmark)
811     (save-excursion
812       (set-buffer tmp-buf)
813       (setq num (length numlist))
814       (setq i 0)
815       (message "Creating msgdb...")
816       (while numlist
817         (erase-buffer)
818         (setq entity
819               (elmo-archive-msgdb-create-entity
820                method file (car numlist) type (nth 3 spec)))
821         (when entity
822           (setq overview
823                 (elmo-msgdb-append-element
824                  overview entity))
825           (setq number-alist
826                 (elmo-msgdb-number-add
827                  number-alist
828                  (elmo-msgdb-overview-entity-get-number entity)
829                  (car entity)))
830           (setq message-id (car entity))
831           (setq seen (member message-id seen-list))
832           (if (setq gmark
833                     (or (elmo-msgdb-global-mark-get message-id)
834                         (if (elmo-cache-exists-p message-id) ; XXX
835                             (if seen
836                                 nil
837                               already-mark)
838                           (if seen
839                               seen-mark
840                             new-mark))))
841               (setq mark-alist
842                     (elmo-msgdb-mark-append
843                      mark-alist
844                      (elmo-msgdb-overview-entity-get-number entity)
845                      gmark))))
846         (when (> num elmo-display-progress-threshold)
847           (setq i (1+ i))
848           (setq percent (/ (* i 100) num))
849           (elmo-display-progress
850            'elmo-archive-msgdb-create-as-numlist-subr1 "Creating msgdb..."
851            percent))
852         (setq numlist (cdr numlist)))
853       (kill-buffer tmp-buf)
854       (message "Creating msgdb...done")
855       (list overview number-alist mark-alist)) ))
856
857 ;;; info-zip agent
858 (defun elmo-archive-msgdb-create-as-numlist-subr2 (spec numlist new-mark
859                                                         already-mark seen-mark
860                                                         important-mark
861                                                         seen-list)
862   (let* ((buf (get-buffer-create " *ELMO ARCHIVE headers*"))
863          (delim1 elmo-mmdf-delimiter)           ;; MMDF
864          (delim2 elmo-unixmail-delimiter)       ;; UNIX Mail
865          (type (nth 2 spec))
866          (prefix (nth 3 spec))
867          (method (elmo-archive-get-method type 'cat-headers))
868          (prog (car method))
869          (args (cdr method))
870          (arc (elmo-archive-get-archive-name (nth 1 spec) type))
871          n i percent num result overview number-alist mark-alist
872          msgs case-fold-search)
873     (set-buffer buf)
874     (setq num (length numlist))
875     (setq i 0)
876     (message "Creating msgdb...")
877     (while numlist
878       (setq n (min (1- elmo-archive-fetch-headers-volume)
879                    (1- (length numlist))))
880       (setq msgs (reverse (memq (nth n numlist) (reverse numlist))))
881       (setq numlist (nthcdr (1+ n) numlist))
882       (erase-buffer)
883       (insert
884        (mapconcat
885         'concat
886         (mapcar '(lambda (x) (elmo-concat-path prefix (int-to-string x))) msgs)
887         "\n"))
888       (message "Fetching headers...")
889       (as-binary-process (apply 'call-process-region
890                                 (point-min) (point-max)
891                                 prog t t nil (append args (list arc))))
892       (goto-char (point-min))
893       (cond
894        ((looking-at delim1)     ;; MMDF
895         (setq result (elmo-archive-parse-mmdf msgs
896                                               new-mark
897                                               already-mark seen-mark
898                                               seen-list))
899         (setq overview (append overview (nth 0 result)))
900         (setq number-alist (append number-alist (nth 1 result)))
901         (setq mark-alist (append mark-alist (nth 2 result))))
902 ;;;    ((looking-at delim2)     ;; UNIX MAIL
903 ;;;     (setq result (elmo-archive-parse-unixmail msgs))
904 ;;;     (setq overview (append overview (nth 0 result)))
905 ;;;     (setq number-alist (append number-alist (nth 1 result)))
906 ;;;     (setq mark-alist (append mark-alist (nth 2 result))))
907        (t                       ;; unknown format
908         (error "Unknown format!")))
909       (when (> num elmo-display-progress-threshold)
910         (setq i (+ n i))
911         (setq percent (/ (* i 100) num))
912         (elmo-display-progress
913          'elmo-archive-msgdb-create-as-numlist-subr2 "Creating msgdb..."
914          percent)))
915     (kill-buffer buf)
916     (list overview number-alist mark-alist)) )
917
918 (defun elmo-archive-parse-mmdf (msgs new-mark
919                                      already-mark
920                                      seen-mark
921                                      seen-list)
922   (let ((delim elmo-mmdf-delimiter)
923         number sp ep rest entity overview number-alist mark-alist ret-val
924         message-id seen gmark)
925     (goto-char (point-min))
926     (setq rest msgs)
927     (while (and rest (re-search-forward delim nil t)
928                 (not (eobp)))
929       (setq number (car rest))
930       (setq sp (1+ (point)))
931       (setq ep (prog2 (re-search-forward delim)
932                    (1+ (- (point) (length delim)))))
933       (if (>= sp ep)                    ; no article!
934           ()                            ; nop
935         (save-excursion
936           (narrow-to-region sp ep)
937           (setq entity (elmo-archive-msgdb-create-entity-subr number))
938           (setq overview
939                 (elmo-msgdb-append-element
940                  overview entity))
941           (setq number-alist
942                 (elmo-msgdb-number-add
943                  number-alist
944                  (elmo-msgdb-overview-entity-get-number entity)
945                  (car entity)))
946           (setq message-id (car entity))
947           (setq seen (member message-id seen-list))
948           (if (setq gmark
949                     (or (elmo-msgdb-global-mark-get message-id)
950                         (if (elmo-cache-exists-p message-id) ; XXX
951                             (if seen
952                                 nil
953                               already-mark)
954                           (if seen
955                               seen-mark
956                             new-mark))))
957               (setq mark-alist
958                     (elmo-msgdb-mark-append
959                      mark-alist
960                      (elmo-msgdb-overview-entity-get-number entity)
961                      gmark)))
962           (setq ret-val (append ret-val (list overview number-alist mark-alist)))
963           (widen)))
964       (forward-line 1)
965       (setq rest (cdr rest)))
966     ret-val))
967
968
969 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
970 ;;; Search functions
971
972 (defsubst elmo-archive-field-condition-match (spec number number-list
973                                                    condition prefix)
974   (save-excursion
975     (let* ((type (nth 2 spec))
976            (arc (elmo-archive-get-archive-name (nth 1 spec) type spec))
977            (method (elmo-archive-get-method type 'cat))
978            (args (list arc (elmo-concat-path prefix (int-to-string number)))))
979       (elmo-set-work-buf
980        (when (file-exists-p arc)
981          (as-binary-process
982           (elmo-archive-call-method method args t))
983          (elmo-set-buffer-multibyte default-enable-multibyte-characters)
984          (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
985          (elmo-buffer-field-condition-match condition number number-list))))))
986
987 (defun elmo-archive-search (spec condition &optional from-msgs)
988   (let* (;;(args (elmo-string-to-list key))
989          ;; XXX: I don't know whether `elmo-archive-list-folder'
990          ;;      updates match-data.
991          ;; (msgs (or from-msgs (elmo-archive-list-folder spec)))
992          (msgs (or from-msgs (elmo-archive-list-folder spec)))
993          (num (length msgs))
994          (i 0)
995          (case-fold-search nil)
996          number-list ret-val)
997     (setq number-list msgs)
998     (while msgs
999       (if (elmo-archive-field-condition-match spec (car msgs) number-list
1000                                               condition
1001                                               (nth 3 spec))
1002           (setq ret-val (cons (car msgs) ret-val)))
1003       (when (> num elmo-display-progress-threshold)
1004         (setq i (1+ i))
1005         (elmo-display-progress
1006          'elmo-archive-search "Searching..."
1007          (/ (* i 100) num)))
1008       (setq msgs (cdr msgs)))
1009     (nreverse ret-val)))
1010
1011
1012 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1013 ;;; Misc functions
1014
1015 (defun elmo-archive-check-validity (spec validity-file)
1016   t) ; ok.
1017
1018 (defun elmo-archive-sync-validity (spec validity-file)
1019   t) ; ok.
1020
1021 \f
1022 ;;; method(alist)
1023 (if (null elmo-archive-method-alist)
1024     (let ((mlist elmo-archive-method-list) ; from mew-highlight.el
1025           method type str)
1026       (while mlist
1027         (setq method (car mlist))
1028         (setq mlist (cdr mlist))
1029         (setq str (symbol-name method))
1030         (string-match "elmo-archive-\\([^-].*\\)-method-alist$" str)
1031         (setq type (intern-soft
1032                     (elmo-match-string 1 str)))
1033         (setq elmo-archive-method-alist
1034               (cons (cons type
1035                           (symbol-value method))
1036                     elmo-archive-method-alist)))))
1037
1038 ;;; valid suffix(list)
1039 (if (null elmo-archive-suffixes)
1040     (let ((slist elmo-archive-suffix-alist)
1041           tmp)
1042       (while slist
1043         (setq tmp (car slist))
1044         (setq elmo-archive-suffixes
1045               (nconc elmo-archive-suffixes (list (cdr tmp))))
1046         (setq slist (cdr slist)))))
1047
1048 (defun elmo-archive-use-cache-p (spec number)
1049   elmo-archive-use-cache)
1050
1051 (defun elmo-archive-local-file-p (spec number)
1052   nil)
1053
1054 (defun elmo-archive-get-msg-filename (spec number &optional loc-alist)
1055   (let ((tmp-dir (file-name-as-directory (elmo-msgdb-expand-path spec)))
1056         (prefix (nth 3 spec)))
1057     (expand-file-name
1058      (elmo-concat-path prefix (int-to-string number))
1059      tmp-dir)))
1060
1061 (defalias 'elmo-archive-sync-number-alist
1062   'elmo-generic-sync-number-alist)
1063 (defalias 'elmo-archive-list-folder-unread
1064   'elmo-generic-list-folder-unread)
1065 (defalias 'elmo-archive-list-folder-important
1066   'elmo-generic-list-folder-important)
1067 (defalias 'elmo-archive-commit 'elmo-generic-commit)
1068 (defalias 'elmo-archive-folder-diff 'elmo-generic-folder-diff)
1069
1070 ;;; End
1071 (run-hooks 'elmo-archive-load-hook)
1072
1073 (require 'product)
1074 (product-provide (provide 'elmo-archive) (require 'elmo-version))
1075
1076 ;;; elmo-archive.el ends here