File: wl-expire.el

package info (click to toggle)
wl-beta 2.15.9+0.20190205-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 6,260 kB
  • sloc: lisp: 51,446; sh: 191; makefile: 141
file content (840 lines) | stat: -rw-r--r-- 28,546 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
;;; wl-expire.el --- Message expire modules for Wanderlust.

;; Copyright (C) 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>

;; Author: Masahiro MURATA <muse@ba2.so-net.ne.jp>
;; Keywords: mail, net news

;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;

;;; Commentary:
;;

;;; Code:

(require 'wl-summary)
(require 'wl-thread)
(require 'wl-folder)
(require 'elmo)

(eval-when-compile
  (require 'wl-util)
  (require 'elmo-archive))

;; Variables

(defvar wl-expired-alist nil)
(defvar wl-expired-alist-file-name "expired-alist")
(defvar wl-expired-log-alist nil)
(defvar wl-expired-log-alist-file-name "expired-log")
(defvar wl-expire-test nil)	;; for debug (no execute)

(defun wl-expired-alist-load ()
  (elmo-object-load (expand-file-name
		     wl-expired-alist-file-name
		     elmo-msgdb-directory)))

(defun wl-expired-alist-save (&optional alist)
  (elmo-object-save (expand-file-name
		     wl-expired-alist-file-name
		     elmo-msgdb-directory)
		    (or alist wl-expired-alist)))

(defsubst wl-expire-msg-p (msg-num mark-alist)
  (cond ((consp wl-summary-expire-reserve-marks)
	 (let ((mark (nth 1 (assq msg-num mark-alist))))
	   (not (or (member mark wl-summary-expire-reserve-marks)
		    (and wl-summary-buffer-disp-msg
			 (eq msg-num wl-summary-buffer-current-msg))))))
	((eq wl-summary-expire-reserve-marks 'all)
	 (not (or (assq msg-num mark-alist)
		  (and wl-summary-buffer-disp-msg
		       (eq msg-num wl-summary-buffer-current-msg)))))
	((eq wl-summary-expire-reserve-marks 'none)
	 t)
	(t
	 (error "Invalid marks: %s" wl-summary-expire-reserve-marks))))

(defsubst wl-expire-make-sortable-date (date)
  (timezone-make-sortable-date
   (aref date 0) (aref date 1) (aref date 2)
   (timezone-make-time-string
    (aref date 3) (aref date 4) (aref date 5))))

;; New functions to avoid accessing to the msgdb directly.
(defsubst wl-expire-message-p (folder number)
  "Return non-nil when a message in the FOLDER with NUMBER can be expired."
  (cond ((consp wl-summary-expire-reserve-marks)
	 (let ((mark (wl-summary-message-mark folder number)))
	   (not (or (member mark wl-summary-expire-reserve-marks)
		    (and wl-summary-buffer-disp-msg
			 (eq number wl-summary-buffer-current-msg))))))
	((eq wl-summary-expire-reserve-marks 'all)
	 (not (or (wl-summary-message-mark folder number)
		  (and wl-summary-buffer-disp-msg
		       (eq number wl-summary-buffer-current-msg)))))
	((eq wl-summary-expire-reserve-marks 'none)
	 t)
	(t
	 (error "Invalid marks: %s" wl-summary-expire-reserve-marks))))

(defun wl-expire-delete-reserved-messages (msgs folder)
  "Delete a number from NUMBERS when a message with the number is reserved."
  (let ((dlist msgs))
    (while dlist
      (unless (wl-expire-message-p folder (car dlist))
	(setq msgs (delq (car dlist) msgs)))
      (setq dlist (cdr dlist)))
    msgs))
;; End New functions.

(defun wl-expire-delete (folder delete-list &optional no-reserve-marks)
  "Delete message for expire."
  (unless no-reserve-marks
    (setq delete-list
	  (wl-expire-delete-reserved-messages delete-list folder)))
  (when delete-list
    (let ((mess
	   (format "Expiring (delete) %s msgs..."
		   (length delete-list))))
      (message "%s" mess)
      (if (elmo-folder-move-messages folder delete-list 'null)
	  (progn
	    (wl-expire-append-log
	     (elmo-folder-name-internal folder)
	     delete-list nil 'delete)
	    (message "%sdone" mess))
	(error "%sfailed!" mess))))
  (cons delete-list (length delete-list)))

(defun wl-expire-refile (folder refile-list dst-folder
				&optional no-reserve-marks preserve-number copy)
  "Refile message for expire. If COPY is non-nil, copy message."
  (when (not (string= (elmo-folder-name-internal folder) dst-folder))
    (unless no-reserve-marks
      (setq refile-list
	    (wl-expire-delete-reserved-messages refile-list folder)))
    (when refile-list
      (let* ((dst-name dst-folder)
	     (dst-folder (wl-folder-get-elmo-folder dst-folder))
	     (action (format (if copy "Copying to %s" "Expiring (move to %s)")
			     dst-name)))
	(elmo-with-progress-display
	    (elmo-folder-move-messages (length refile-list))
	    action
	  (if wl-expire-test
	      nil
	    (unless (or (elmo-folder-exists-p dst-folder)
			(elmo-folder-create dst-folder))
	      (error "Create folder failed: %s" dst-name))
	    (unless (elmo-folder-move-messages folder
					       refile-list
					       dst-folder
					       copy
					       preserve-number)
	      (error "%s is failed" action))
	    (wl-expire-append-log
	     (elmo-folder-name-internal folder)
	     refile-list
	     dst-name
	     (if copy 'copy 'move))))))
    (cons refile-list (length refile-list))))

(defun wl-expire-refile-with-copy-reserve-msg
  (folder refile-list dst-folder
	  &optional no-reserve-marks preserve-number copy)
  "Refile message for expire.
If REFILE-LIST includes reserve mark message, so copy."
  (when (not (string= (elmo-folder-name-internal folder) dst-folder))
    (let ((msglist refile-list)
	  (dst-folder (wl-folder-get-elmo-folder dst-folder))
	  (ret-val t)
	  (copy-reserve-message)
	  (copy-len 0)
	  msg msg-id)
      (message "Expiring (move %s) %s msgs..."
	       (elmo-folder-name-internal dst-folder) (length refile-list))
      (if wl-expire-test
	  (setq copy-len (length refile-list))
	(unless (or (elmo-folder-exists-p dst-folder)
		  (elmo-folder-create dst-folder))
	(error "%s: create folder failed" (elmo-folder-name-internal
					   dst-folder)))
	(while (setq msg (wl-pop msglist))
	  (unless (wl-expire-message-p folder msg)
	    (setq msg-id (elmo-message-field folder msg 'message-id))
	    (if (assoc msg-id wl-expired-alist)
		;; reserve mark message already refiled or expired
		(setq refile-list (delq msg refile-list))
	      ;; reserve mark message not refiled
	      (wl-append wl-expired-alist (list
					   (cons msg-id
						 (elmo-folder-name-internal
						  dst-folder))))
	      (setq copy-reserve-message t))))
	(when refile-list
	  (unless
	      (setq ret-val
		    (elmo-folder-move-messages folder
					       refile-list
					       dst-folder
					       copy-reserve-message
					       preserve-number))
	    (error "Expire: move msgs to %s failed"
		   (elmo-folder-name-internal dst-folder)))
	  (wl-expire-append-log (elmo-folder-name-internal folder)
				refile-list
				(elmo-folder-name-internal dst-folder)
				(if copy-reserve-message 'copy 'move))
	  (setq copy-len (length refile-list))
	  (when copy-reserve-message
	    (setq refile-list
		  (wl-expire-delete-reserved-messages refile-list folder))
	    (when refile-list
	      (if (setq ret-val
			(elmo-folder-move-messages folder refile-list 'null))
		  (progn
		    (wl-expire-append-log
		     (elmo-folder-name-internal folder)
		     refile-list nil 'delete))))))
	(let ((mes (format "Expiring (move %s) %s msgs..."
			   (elmo-folder-name-internal dst-folder)
			   (length refile-list))))
	  (if ret-val
	      (message "%sdone" mes)
	    (error "%sfailed!" mes))))
      (cons refile-list copy-len))))

(defun wl-expire-archive-get-folder (src-folder &optional fmt dst-folder-arg)
  "Get archive folder name from SRC-FOLDER."
  (let* ((fmt (or fmt wl-expire-archive-folder-name-fmt))
	 (src-folde-name (substring
			  (elmo-folder-name-internal src-folder)
			  (length (elmo-folder-prefix-internal src-folder))))
	 (archive-spec (char-to-string
			(car (rassq 'archive elmo-folder-type-alist))))
	 dst-folder-base dst-folder-fmt prefix)
    (cond (dst-folder-arg
	   (setq dst-folder-base (concat archive-spec dst-folder-arg)))
	  ((eq (elmo-folder-type-internal src-folder) 'localdir)
	   (setq dst-folder-base
		 (concat archive-spec src-folde-name)))
	  (t
	   (setq dst-folder-base
		 (elmo-concat-path
		  (format "%s%s" archive-spec (elmo-folder-type-internal
					       src-folder))
		  src-folde-name))))
    (setq dst-folder-fmt (format fmt
				 dst-folder-base
				 wl-expire-archive-folder-type))
    (setq dst-folder-base (format "%s;%s"
				  dst-folder-base
				  wl-expire-archive-folder-type))
    (when wl-expire-archive-folder-prefix
      (cond ((eq wl-expire-archive-folder-prefix 'short)
	     (setq prefix (file-name-nondirectory
			   src-folde-name)))
	    (t
	     (setq prefix src-folde-name)))
      (setq dst-folder-fmt (concat dst-folder-fmt ";" prefix))
      (setq dst-folder-base (concat dst-folder-base ";" prefix)))
    (cons dst-folder-base dst-folder-fmt)))

(defsubst wl-expire-archive-get-max-number (dst-folder-base &optional regexp)
  (let ((files (reverse (sort (elmo-folder-list-subfolders
			       (elmo-make-folder dst-folder-base))
			      'string<)))
	(regexp (or regexp wl-expire-archive-folder-num-regexp))
	filenum in-folder)
    (catch 'done
      (while files
	(when (string-match regexp (car files))
	  (setq filenum (match-string 1 (car files)))
	  (setq in-folder (elmo-folder-status
			   (wl-folder-get-elmo-folder (car files))))
	  (throw 'done (cons in-folder filenum)))
	(setq files (cdr files))))))

(defun wl-expire-archive-number-delete-old (dst-folder-base
					    preserve-number msgs folder
					    &optional no-confirm regexp file)
  (let ((len 0) (max-num 0)
	folder-info dels)
    (if (or (and file (setq folder-info
			    (cons (elmo-folder-status
				   (wl-folder-get-elmo-folder file))
				  nil)))
	    (setq folder-info (wl-expire-archive-get-max-number
			       dst-folder-base
			       regexp)))
	(progn
	  (setq len (cdar folder-info))
	  (when preserve-number
	    ;; delete small number than max number of dst-folder
	    (setq max-num (caar folder-info))
	    (while (and msgs (>= max-num (car msgs)))
	      (wl-append dels (list (car msgs)))
	      (setq msgs (cdr msgs)))
	    (setq dels (wl-expire-delete-reserved-messages dels folder))
	    (unless (and dels
			 (or (or no-confirm (not
					     wl-expire-delete-oldmsg-confirm))
			     (progn
			       (if (eq major-mode 'wl-summary-mode)
				   (wl-thread-jump-to-msg (car dels)))
			       (y-or-n-p (format "Delete old messages %s? "
						 dels)))))
	      (setq dels nil)))
	  (list msgs dels max-num (cdr folder-info) len))
      (list msgs dels 0 "0" 0))))

(defun wl-expire-archive-number1 (folder delete-list
				  &optional preserve-number dst-folder-arg
					    no-delete)
  "Standard function for `wl-summary-expire'.
Refile to archive folder followed message number."
  (let* ((elmo-archive-treat-file t)	;; treat archive folder as a file.
	 (dst-folder-expand (and dst-folder-arg
				 (elmo-expand-newtext
				  dst-folder-arg
				  (elmo-folder-name-internal folder))))
	 (dst-folder-fmt (funcall
			  wl-expire-archive-get-folder-function
			  folder nil dst-folder-expand))
	 (dst-folder-base (car dst-folder-fmt))
	 (dst-folder-fmt (cdr dst-folder-fmt))
	 (refile-func (if no-delete
			  'wl-expire-refile
			'wl-expire-refile-with-copy-reserve-msg))
	 tmp dels dst-folder
	 prev-arcnum arcnum msg arcmsg-list
	 deleted-list ret-val)
    (setq tmp (wl-expire-archive-number-delete-old
	       dst-folder-base preserve-number delete-list
	       folder
	       no-delete))
    (when (and (not no-delete)
	       (setq dels (nth 1 tmp)))
      (wl-append deleted-list (car (wl-expire-delete folder dels))))
    (setq delete-list (car tmp))
    (catch 'done
      (while t
	(if (setq msg (wl-pop delete-list))
	    (setq arcnum (/ msg wl-expire-archive-files))
	  (setq arcnum nil))
	(when (and prev-arcnum
		   (not (eq arcnum prev-arcnum)))
	  (setq dst-folder (format dst-folder-fmt
				   (* prev-arcnum wl-expire-archive-files)))
	  (and (setq ret-val
		     (funcall
		      refile-func
		      folder arcmsg-list dst-folder t preserve-number
		      no-delete))
	       (wl-append deleted-list (car ret-val)))
	  (setq arcmsg-list nil))
	(if (null msg)
	    (throw 'done t))
	(wl-append arcmsg-list (list msg))
	(setq prev-arcnum arcnum)))
    deleted-list))

(defun wl-expire-archive-number2 (folder delete-list
				  &optional preserve-number dst-folder-arg
					    no-delete)
  "Standard function for `wl-summary-expire'.
Refile to archive folder followed the number of message in one archive folder."
  (let* ((elmo-archive-treat-file t)	;; treat archive folder as a file.
	 (dst-folder-expand (and dst-folder-arg
				 (elmo-expand-newtext
				  dst-folder-arg
				  (elmo-folder-name-internal folder))))
	 (dst-folder-fmt (funcall
			  wl-expire-archive-get-folder-function
			  folder nil dst-folder-expand))
	 (dst-folder-base (car dst-folder-fmt))
	 (dst-folder-fmt (cdr dst-folder-fmt))
	 (refile-func (if no-delete
			  'wl-expire-refile
			'wl-expire-refile-with-copy-reserve-msg))
	 (len 0) (filenum 0)
	 tmp dels dst-folder
	 arc-len msg arcmsg-list
	 deleted-list ret-val)
    (setq tmp (wl-expire-archive-number-delete-old
	       dst-folder-base preserve-number delete-list
	       folder
	       no-delete))
    (when (and (not no-delete)
	       (setq dels (nth 1 tmp)))
      (wl-append deleted-list (car (wl-expire-delete folder dels))))
    (setq delete-list (car tmp)
	  filenum (string-to-number (nth 3 tmp))
	  len (nth 4 tmp)
	  arc-len len)
    (catch 'done
      (while t
	(if (setq msg (wl-pop delete-list))
	    (setq len (1+ len))
	  (setq len (1+ wl-expire-archive-files)))
	(when (> len wl-expire-archive-files)
	  (when arcmsg-list
	    (setq dst-folder (format dst-folder-fmt filenum))
	    (and (setq ret-val
		       (funcall
			refile-func
			folder arcmsg-list dst-folder t preserve-number
			no-delete))
		 (wl-append deleted-list (car ret-val)))
	    (setq arc-len (+ arc-len (cdr ret-val))))
	  (setq arcmsg-list nil)
	  (if (< arc-len wl-expire-archive-files)
	      (setq len (1+ arc-len))
	    (setq filenum (+ filenum wl-expire-archive-files)
		  len (- len arc-len)	;; maybe 1
		  arc-len (1- len)	;; maybe 0
		  )))
	(if (null msg)
	    (throw 'done t))
	(wl-append arcmsg-list (list msg))))
    deleted-list))

(defun wl-expire-archive-date (folder delete-list
			       &optional preserve-number dst-folder-arg
					 no-delete)
  "Standard function for `wl-summary-expire'.
Refile to archive folder followed message date."
  (let* ((elmo-archive-treat-file t)	;; treat archive folder as a file.
	 (dst-folder-expand (and dst-folder-arg
				 (elmo-expand-newtext
				  dst-folder-arg
				  (elmo-folder-name-internal folder))))
	 (dst-folder-fmt (funcall
			  wl-expire-archive-get-folder-function
			  folder
			  wl-expire-archive-date-folder-name-fmt
			  dst-folder-expand
			  ))
	 (dst-folder-base (car dst-folder-fmt))
	 (dst-folder-fmt (cdr dst-folder-fmt))
	 (refile-func (if no-delete
			  'wl-expire-refile
			'wl-expire-refile-with-copy-reserve-msg))
	 tmp dels dst-folder date time
	 msg arcmsg-alist arcmsg-list
	 deleted-list ret-val)
    (setq tmp (wl-expire-archive-number-delete-old
	       dst-folder-base preserve-number delete-list
	       folder
	       no-delete
	       wl-expire-archive-date-folder-num-regexp))
    (when (and (not no-delete)
	       (setq dels (nth 1 tmp)))
      (wl-append deleted-list (car (wl-expire-delete folder dels))))
    (setq delete-list (car tmp))
    (while (setq msg (wl-pop delete-list))
      (setq time (or (elmo-time-to-datevec
		      (elmo-message-field folder msg 'date))
		     (make-vector 7 0)))
      (if (= (aref time 1) 0)	;; if (month == 0)
	  (aset time 0 0))	;;    year = 0
      (setq dst-folder (format dst-folder-fmt
			       (aref time 0)  ;; year
			       (aref time 1)  ;; month
			       ))
      (setq arcmsg-alist
	    (wl-append-assoc-list
	     dst-folder
	     msg
	     arcmsg-alist)))
    (while arcmsg-alist
      (setq dst-folder (caar arcmsg-alist))
      (setq arcmsg-list (cdar arcmsg-alist))
      (and (setq ret-val
		 (funcall
		  refile-func
		  folder arcmsg-list dst-folder t preserve-number
		  no-delete))
	   (wl-append deleted-list (car ret-val)))
      (setq arcmsg-alist (cdr arcmsg-alist)))
    deleted-list))

;;; wl-expire-localdir-date
(defvar wl-expire-localdir-date-folder-name-fmt "%s/%%04d_%%02d")

(defcustom wl-expire-localdir-get-folder-function
  'wl-expire-localdir-get-folder
  "*A function to get localdir folder name."
  :type 'function
  :group 'wl-expire)

(defun wl-expire-localdir-get-folder (src-folder fmt dst-folder-arg)
  "Get localdir folder name from src-folder."
  (let* ((src-folder-name (substring
			   (elmo-folder-name-internal src-folder)
			   (length (elmo-folder-prefix-internal src-folder))))
	 (dst-folder-spec (char-to-string
			   (car (rassq 'localdir elmo-folder-type-alist))))
	 dst-folder-base dst-folder-fmt)
    (cond (dst-folder-arg
	   (setq dst-folder-base (concat dst-folder-spec dst-folder-arg)))
	  ((eq (elmo-folder-type-internal src-folder) 'localdir)
	   (setq dst-folder-base (concat dst-folder-spec src-folder-name)))
	  (t
	   (setq dst-folder-base
		 (elmo-concat-path
		  (format "%s%s"
			  dst-folder-spec
			  (elmo-folder-type-internal src-folder))
		  src-folder-name))))
    (setq dst-folder-fmt
	  (format fmt dst-folder-base))
    (cons dst-folder-base dst-folder-fmt)))

(defun wl-expire-localdir-date (folder delete-list
				       &optional preserve-number dst-folder-arg
				       no-delete)
  "Function for `wl-summary-expire'.
Refile to localdir folder by message date.
ex. +ml/wl/1999_11/, +ml/wl/1999_12/."
  (let* ((dst-folder-expand (and dst-folder-arg
				 (elmo-expand-newtext
				  dst-folder-arg
				  (elmo-folder-name-internal folder))))
	 (dst-folder-fmt (funcall
			  wl-expire-localdir-get-folder-function
			  folder
			  wl-expire-localdir-date-folder-name-fmt
			  dst-folder-expand))
	 (dst-folder-base (car dst-folder-fmt))
	 (dst-folder-fmt (cdr dst-folder-fmt))
	 (refile-func (if no-delete
			  'wl-expire-refile
			'wl-expire-refile-with-copy-reserve-msg))
	 tmp dels dst-folder date time
	 msg arcmsg-alist arcmsg-list
	 deleted-list ret-val)
    (while (setq msg (wl-pop delete-list))
      (setq time (or (elmo-time-to-datevec
		      (elmo-message-field folder msg 'date))
		     (make-vector 7 0)))
      (if (= (aref time 1) 0)	;; if (month == 0)
	  (aset time 0 0))	;;    year = 0
      (setq dst-folder (format dst-folder-fmt
			       (aref time 0);; year
			       (aref time 1);; month
			       ))
      (setq arcmsg-alist
	    (wl-append-assoc-list
	     dst-folder
	     msg
	     arcmsg-alist)))
    (while arcmsg-alist
      (setq dst-folder (caar arcmsg-alist))
      (setq arcmsg-list (cdar arcmsg-alist))
      (and (setq ret-val
		 (funcall
		  refile-func
		  folder arcmsg-list dst-folder t preserve-number
		  no-delete))
	   (wl-append deleted-list (car ret-val)))
      (setq arcmsg-alist (cdr arcmsg-alist)))
    deleted-list))

(defun wl-expire-hide (folder hide-list &optional no-reserve-marks)
  "Hide message for expire."
  (unless no-reserve-marks
    (setq hide-list
	  (wl-expire-delete-reserved-messages hide-list folder)))
  (let ((mess (format "Hiding %s msgs..." (length hide-list))))
    (message "%s" mess)
    (elmo-folder-detach-messages folder hide-list)
    (elmo-folder-kill-messages folder hide-list)
    (elmo-folder-commit folder)
    (message "%sdone" mess)
    (cons hide-list (length hide-list))))

(defsubst wl-expire-folder-p (entity)
  "Return non-nil, when ENTITY matched `wl-expire-alist'."
  (wl-get-assoc-list-value wl-expire-alist entity))

(defsubst wl-archive-folder-p (entity)
  "Return non-nil, when ENTITY matched `wl-archive-alist'."
  (wl-get-assoc-list-value wl-archive-alist entity))

(defun wl-summary-expire (&optional folder notsummary all)
  "Expire messages of current summary."
  (interactive
   (list wl-summary-buffer-elmo-folder
	 nil
	 current-prefix-arg))
  (let* ((folder (or folder wl-summary-buffer-elmo-folder))
	 (folder-name (elmo-folder-name-internal folder))
	 (rule (wl-expire-folder-p folder-name)))
    (if (not rule)
	(and (interactive-p)
	     (error "No match %s in `wl-expire-alist'" folder-name))
      (when (or (not (interactive-p))
		(y-or-n-p (format "Expire %s? " folder-name)))
	(save-excursion
	  (run-hooks 'wl-summary-expire-pre-hook)
	  (let ((expired (apply #'wl-expire-folder folder all rule)))
	    (when (and (not wl-expire-test)
		       (not notsummary)
		       expired)
	      (wl-summary-delete-messages-on-buffer expired)
	      (wl-summary-folder-info-update)
	      (wl-summary-set-message-modified)
	      (sit-for 0)
	      (set-buffer-modified-p nil))
	    (run-hooks 'wl-summary-expire-hook)
	    (if expired
		(message "Expiring %s is done" folder-name)
	      (and (interactive-p)
		   (message "No expire")))
	    expired))))))

(defun wl-expire-folder (folder all condition action &rest args)
  (let ((folder-name (elmo-folder-name-internal folder))
	(val-type (car condition))
	(value (nth 1 condition))
	targets)
    (cond
     ((eq val-type nil))
     ((eq val-type 'number)
      (let* ((msgs (elmo-folder-list-messages folder (not all) (not all)))
	     (msglen (length msgs))
	     count)
	(when (>= msglen (or (nth 2 condition) (1+ value)))
	  (setq count (- msglen value))
	  (while (and msgs (> count 0))
	    (when (elmo-message-entity folder (car msgs))
	      ;; don't expire new message
	      (wl-append targets (list (car msgs)))
	      (when (or (not wl-expire-number-with-reserve-marks)
			(wl-expire-message-p folder (car msgs)))
		(setq count (1- count))))
	    (setq msgs (cdr msgs))))))
     ((eq val-type 'date)
      (let ((key-date (elmo-datevec-to-time
		       (elmo-date-get-offset-datevec
			(timezone-fix-time (current-time-string)
					   (current-time-zone) nil)
			value t))))
	(elmo-folder-do-each-message-entity (entity folder)
	  (when (elmo-time<
		 (elmo-message-entity-field entity 'date)
		 key-date)
	    (wl-append targets
		       (list (elmo-message-entity-number entity)))))))
     (t
      (error "%s: not supported" val-type)))
    (when targets
      (or wl-expired-alist
	  (setq wl-expired-alist (wl-expired-alist-load)))
      ;; evaluate string-match for elmo-expand-newtext
      (wl-expire-folder-p folder-name)
      (prog1
	  (cond ((eq action nil) nil)
		((eq action 'remove)
		 (car (wl-expire-delete folder targets)))
		((eq action 'trash)
		 (car (wl-expire-refile folder targets wl-trash-folder)))
		((eq action 'hide)
		 (car (wl-expire-hide folder targets)))
		((stringp action)
		 (car (wl-expire-refile
		       folder
		       targets
		       (elmo-expand-newtext action folder-name))))
		((fboundp action)
		 (apply action folder targets args))
		(t
		 (error "%s: invalid type" action)))
	(wl-expired-alist-save)))))

(defun wl-folder-expire-entity (entity)
  (cond
   ((consp entity)
    (let ((flist (nth 2 entity)))
      (while flist
	(wl-folder-expire-entity (car flist))
	(setq flist (cdr flist)))))
   ((stringp entity)
    (when (wl-expire-folder-p entity)
      (let ((folder (wl-folder-get-elmo-folder entity))
	    (summary (wl-summary-get-buffer entity))
	    (update-msgdb (cond
			   ((consp wl-expire-folder-update-msgdb)
			    (wl-string-match-member
			     entity
			     wl-expire-folder-update-msgdb))
			   (t
			    wl-expire-folder-update-msgdb))))
	(when update-msgdb
	  (wl-folder-sync-entity entity))
	(if summary
	    (save-selected-window
	      (with-current-buffer summary
		(let ((win (get-buffer-window summary t)))
		  (when win
		    (select-window win)))
		(when (wl-summary-expire folder)
		  (wl-summary-save-status))))
	  (when (wl-summary-expire folder 'no-summary)
	    (wl-folder-check-entity entity))))))))

;; Command

(defun wl-folder-expire-current-entity ()
  (interactive)
  (let ((entity-name (wl-folder-get-entity-from-buffer))
	(type (if (wl-folder-buffer-group-p)
		  'group
		'folder)))
    (when (and entity-name
	       (or (not (interactive-p))
		   (y-or-n-p (format "Expire %s? " entity-name))))
      (wl-folder-expire-entity
       (wl-folder-search-entity-by-name entity-name
					wl-folder-entity
					type))
      (message "Expiring %s is done" entity-name))))

;;; Archive

(defun wl-folder-archive-current-entity ()
  (interactive)
  (let ((entity-name (wl-folder-get-entity-from-buffer))
	(type (if (wl-folder-buffer-group-p)
		  'group
		'folder)))
    (when (and entity-name
	       (or (not (interactive-p))
		   (y-or-n-p (format "Archive %s? " entity-name))))
      (wl-folder-archive-entity
       (wl-folder-search-entity-by-name entity-name
					wl-folder-entity
					type))
      (message "Archiving %s is done" entity-name))))

(defun wl-archive-number1 (folder archive-list &optional dst-folder-arg)
  (wl-expire-archive-number1 folder archive-list t dst-folder-arg t))

(defun wl-archive-number2 (folder archive-list &optional dst-folder-arg)
  (wl-expire-archive-number2 folder archive-list t dst-folder-arg t))

(defun wl-archive-date (folder archive-list &optional dst-folder-arg)
  (wl-expire-archive-date folder archive-list t dst-folder-arg t))

(defun wl-archive-folder (folder archive-list dst-folder)
  (let* ((elmo-archive-treat-file t)	;; treat archive folder as a file.
	 copied-list ret-val)
    (setq archive-list
	  (car (wl-expire-archive-number-delete-old
		nil t archive-list
		folder
		t ;; no-confirm
		nil dst-folder)))
    (when archive-list
      (and (setq ret-val
		 (wl-expire-refile
		  folder archive-list dst-folder t t t)) ;; copy!!
	   (wl-append copied-list ret-val)))
    copied-list))

(defun wl-summary-archive (&optional arg folder notsummary nolist)
  ""
  (interactive "P")
  (let* ((folder (or folder wl-summary-buffer-elmo-folder))
	 (msgs (if (not nolist)
		   (elmo-folder-list-messages folder)
		 (elmo-folder-list-messages folder 'visible 'in-msgdb)))
	 (alist wl-archive-alist)
	 archives func args dst-folder archive-list)
    (if arg
	(let ((wl-default-spec (char-to-string
				(car (rassq 'archive
					    elmo-folder-type-alist)))))
	  (setq dst-folder (wl-summary-read-folder
			    (concat wl-default-spec
				    (substring
				     (elmo-folder-name-internal folder) 1))
			    "for archive"))))
    (run-hooks 'wl-summary-archive-pre-hook)
    (if dst-folder
	(wl-archive-folder folder msgs dst-folder)
      (when (and (or (setq archives (wl-archive-folder-p
				     (elmo-folder-name-internal folder)))
		     (progn (and (interactive-p)
				 (message "No match %s in wl-archive-alist"
					  (elmo-folder-name-internal folder)))
			    nil))
		 (or (not (interactive-p))
		     (y-or-n-p (format "Archive %s? "
				       (elmo-folder-name-internal folder)))))
	(setq func (car archives)
	      args (cdr archives))
	(setq archive-list
	      (apply func (append (list folder msgs) args)))
	(run-hooks 'wl-summary-archive-hook)
	(if archive-list
	    (message "Archiving %s is done" (elmo-folder-name-internal folder))
	  (and (interactive-p)
	       (message "No archive")))))))

(defun wl-folder-archive-entity (entity)
  (cond
   ((consp entity)
    (let ((flist (nth 2 entity)))
      (while flist
	(wl-folder-archive-entity (car flist))
	(setq flist (cdr flist)))))
   ((stringp entity)
    (wl-summary-archive nil (wl-folder-get-elmo-folder entity) t))))

;; append log

(defun wl-expire-append-log (src-folder msgs dst-folder action)
  (when wl-expire-use-log
    (save-excursion
      (let ((tmp-buf (get-buffer-create " *wl-expire work*"))
	    (filename (expand-file-name wl-expired-log-alist-file-name
					elmo-msgdb-directory)))
	(set-buffer tmp-buf)
	(erase-buffer)
	(if dst-folder
	    (insert (format "%s\t%s -> %s\t%s\n"
			    action
			    src-folder dst-folder msgs))
	  (insert (format "%s\t%s\t%s\n"
			  action
			  src-folder msgs)))
	(if (file-writable-p filename)
	    (write-region (point-min) (point-max)
			  filename t 'no-msg)
	  (message "%s is not writable." filename))
	(kill-buffer tmp-buf)))))

(require 'product)
(product-provide (provide 'wl-expire) (require 'wl-version))

;;; wl-expire.el ends here