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