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