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