1 ;;; wl-expire.el -- Message expire modules for Wanderlust.
3 ;; Copyright 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
4 ;; Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Author: Masahiro MURATA <muse@ba2.so-net.ne.jp>
7 ;; Keywords: mail, net news
8 ;; Time-stamp: <00/04/15 13:53:01 teranisi>
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
39 (require 'elmo-archive))
43 (defvar wl-expired-alist nil)
44 (defvar wl-expired-alist-file-name "expired-alist")
45 (defvar wl-expired-log-alist nil)
46 (defvar wl-expired-log-alist-file-name "expired-log")
48 (defun wl-expired-alist-load ()
49 (elmo-object-load (expand-file-name
50 wl-expired-alist-file-name
53 (defun wl-expired-alist-save (&optional alist)
54 (elmo-object-save (expand-file-name
55 wl-expired-alist-file-name
57 (or alist wl-expired-alist)))
59 (defsubst wl-expire-msg-p (msg-num mark-alist)
60 (cond ((consp wl-summary-expire-reserve-marks)
61 (let ((mark (nth 1 (assq msg-num mark-alist))))
62 (not (or (member mark wl-summary-expire-reserve-marks)
63 (and wl-summary-buffer-disp-msg
64 (eq msg-num wl-summary-buffer-current-msg))))))
65 ((eq wl-summary-expire-reserve-marks 'all)
66 (not (or (assq msg-num mark-alist)
67 (and wl-summary-buffer-disp-msg
68 (eq msg-num wl-summary-buffer-current-msg)))))
69 ((eq wl-summary-expire-reserve-marks 'none)
72 (error "invalid marks: %s" wl-summary-expire-reserve-marks))))
74 (defmacro wl-expire-make-sortable-date (date)
75 (` (timezone-make-sortable-date
76 (aref (, date) 0) (aref (, date) 1) (aref (, date) 2)
77 (timezone-make-time-string
78 (aref (, date) 3) (aref (, date) 4) (aref (, date) 5)))))
80 (defsubst wl-expire-date-p (key-datevec date)
81 (let ((datevec (condition-case nil
82 (timezone-fix-time date nil nil)
85 datevec (> (aref datevec 1) 0)
87 (wl-expire-make-sortable-date datevec)
88 (wl-expire-make-sortable-date key-datevec)))))
90 (defun wl-expire-delete-reserve-marked-msgs-from-list (msgs mark-alist)
93 (unless (wl-expire-msg-p (car dlist) mark-alist)
94 (setq msgs (delq (car dlist) msgs)))
95 (setq dlist (cdr dlist)))
98 (defun wl-expire-delete (folder delete-list msgdb &optional no-reserve-marks)
99 "Delete message for expire."
100 (unless no-reserve-marks
102 (wl-expire-delete-reserve-marked-msgs-from-list
103 delete-list (elmo-msgdb-get-mark-alist msgdb))))
106 (format "Expiring (delete) %s msgs..."
107 (length delete-list))))
109 (if (elmo-delete-msgs folder
113 (elmo-msgdb-delete-msgs folder
117 (wl-expire-append-log folder delete-list nil 'delete)
118 (message "%s" (concat mess "done")))
119 (error (concat mess "failed!")))))
120 (cons delete-list (length delete-list)))
122 (defun wl-expire-refile (folder refile-list msgdb dst-folder
123 &optional no-reserve-marks preserve-number copy)
124 "Refile message for expire. If COPY is non-nil, copy message."
125 (when (not (string= folder dst-folder))
126 (unless no-reserve-marks
128 (wl-expire-delete-reserve-marked-msgs-from-list
129 refile-list (elmo-msgdb-get-mark-alist msgdb))))
131 (let* ((doingmes (if copy
133 "Expiring (move %s)"))
134 (mess (format (concat doingmes " %s msgs...")
135 dst-folder (length refile-list))))
137 (unless (or (elmo-folder-exists-p dst-folder)
138 (elmo-create-folder dst-folder))
139 (error "%s: create folder failed" dst-folder))
140 (if wl-expire-add-seen-list
141 (elmo-msgdb-add-msgs-to-seen-list
145 (concat wl-summary-important-mark
146 wl-summary-read-uncached-mark)))
147 (if (elmo-move-msgs folder
155 (wl-expire-append-log folder refile-list dst-folder (if copy 'copy 'move))
156 (message "%s" (concat mess "done")))
157 (error (concat mess "failed!")))))
158 (cons refile-list (length refile-list))))
160 (defun wl-expire-refile-with-copy-reserve-msg
161 (folder refile-list msgdb dst-folder
162 &optional no-reserve-marks preserve-number copy)
163 "Refile message for expire.
164 If REFILE-LIST includes reserve mark message, so copy."
165 (when (not (string= folder dst-folder))
166 (let ((msglist refile-list)
167 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
168 (number-alist (elmo-msgdb-get-number-alist msgdb))
170 (copy-reserve-message)
173 (message "Expiring (move %s) %s msgs..."
174 dst-folder (length refile-list))
175 (unless (or (elmo-folder-exists-p dst-folder)
176 (elmo-create-folder dst-folder))
177 (error "%s: create folder failed" dst-folder))
178 (while (setq msg (wl-pop msglist))
179 (unless (wl-expire-msg-p msg mark-alist)
180 (setq msg-id (cdr (assq msg number-alist)))
181 (if (assoc msg-id wl-expired-alist)
182 ;; reserve mark message already refiled or expired
183 (setq refile-list (delq msg refile-list))
184 ;; reserve mark message not refiled
185 (wl-append wl-expired-alist (list (cons msg-id dst-folder)))
186 (setq copy-reserve-message t))))
188 (if wl-expire-add-seen-list
189 (elmo-msgdb-add-msgs-to-seen-list
193 (concat wl-summary-important-mark
194 wl-summary-read-uncached-mark)))
197 (elmo-move-msgs folder
204 (error "expire: move msgs to %s failed" dst-folder))
205 (wl-expire-append-log folder refile-list dst-folder
206 (if copy-reserve-message 'copy 'move))
207 (setq copy-len (length refile-list))
208 (when copy-reserve-message
210 (wl-expire-delete-reserve-marked-msgs-from-list
215 (elmo-delete-msgs folder
219 (elmo-msgdb-delete-msgs folder
223 (wl-expire-append-log folder refile-list nil 'delete))))))
224 (let ((mes (format "Expiring (move %s) %s msgs..."
225 dst-folder (length refile-list))))
227 (message (concat mes "done"))
228 (error (concat mes "failed!"))))
229 (cons refile-list copy-len))))
231 (defun wl-expire-archive-get-folder (src-folder &optional fmt)
232 "Get archive folder name from src-folder."
233 (let* ((spec (elmo-folder-get-spec src-folder))
234 (fmt (or fmt wl-expire-archive-folder-name-fmt))
235 (archive-spec (char-to-string
236 (car (rassq 'archive elmo-spec-alist))))
237 dst-folder-base dst-folder-fmt prefix)
238 (cond ((eq (car spec) 'localdir)
239 (setq dst-folder-base (concat archive-spec (nth 1 spec))))
240 ((stringp (nth 1 spec))
241 (setq dst-folder-base
242 (elmo-concat-path (format "%s%s" archive-spec (car spec))
245 (setq dst-folder-base
246 (elmo-concat-path (format "%s%s" archive-spec (car spec))
247 (elmo-replace-msgid-as-filename
249 (setq dst-folder-fmt (format fmt
251 wl-expire-archive-folder-type))
252 (setq dst-folder-base (format "%s;%s"
254 wl-expire-archive-folder-type))
255 (when (and wl-expire-archive-folder-prefix
256 (stringp (nth 1 spec)))
257 (cond ((eq wl-expire-archive-folder-prefix 'short)
258 (setq prefix (file-name-nondirectory (nth 1 spec))))
260 (setq prefix (nth 1 spec))))
261 (setq dst-folder-fmt (concat dst-folder-fmt ";" prefix))
262 (setq dst-folder-base (concat dst-folder-base ";" prefix)))
263 (cons dst-folder-base dst-folder-fmt)))
265 (defsubst wl-expire-archive-get-max-number (dst-folder-base &optional regexp)
266 (let ((files (reverse (sort (elmo-list-folders dst-folder-base)
268 (regexp (or regexp wl-expire-archive-folder-num-regexp))
272 (when (string-match regexp (car files))
273 (setq filenum (elmo-match-string 1 (car files)))
274 (setq in-folder (elmo-max-of-folder (car files)))
275 (throw 'done (cons in-folder filenum)))
276 (setq files (cdr files))))))
278 (defun wl-expire-archive-number-delete-old (dst-folder-base
279 preserve-number msgs mark-alist
280 &optional no-confirm regexp file)
281 (let ((len 0) (max-num 0)
283 (if (or (and file (setq folder-info
284 (cons (elmo-max-of-folder file) nil)))
285 (setq folder-info (wl-expire-archive-get-max-number dst-folder-base
288 (setq len (cdar folder-info))
289 (when preserve-number
290 ;; delete small number than max number of dst-folder
291 (setq max-num (caar folder-info))
292 (while (and msgs (>= max-num (car msgs)))
293 (wl-append dels (list (car msgs)))
294 (setq msgs (cdr msgs)))
295 (setq dels (wl-expire-delete-reserve-marked-msgs-from-list
298 (or (or no-confirm (not wl-expire-delete-oldmsg-confirm))
300 (if (eq major-mode 'wl-summary-mode)
301 (wl-thread-jump-to-msg (car dels)))
302 (y-or-n-p (format "Delete old messages %s? "
305 (list msgs dels max-num (cdr folder-info) len))
306 (list msgs dels 0 "0" 0))))
308 (defun wl-expire-archive-number1 (folder delete-list msgdb
309 &optional preserve-number no-delete)
310 "Standard function for `wl-summary-expire'.
311 Refile to archive folder followed message number."
312 (let* ((elmo-archive-treat-file t) ;; treat archive folder as a file.
313 (dst-folder-fmt (funcall
314 wl-expire-archive-get-folder-func folder))
315 (dst-folder-base (car dst-folder-fmt))
316 (dst-folder-fmt (cdr dst-folder-fmt))
317 (refile-func (if no-delete
319 'wl-expire-refile-with-copy-reserve-msg))
321 prev-arcnum arcnum msg arcmsg-list
322 deleted-list ret-val)
323 (setq tmp (wl-expire-archive-number-delete-old
324 dst-folder-base preserve-number delete-list
325 (elmo-msgdb-get-mark-alist msgdb)
327 (when (and (not no-delete)
328 (setq dels (nth 1 tmp)))
329 (wl-append deleted-list (car (wl-expire-delete folder dels msgdb))))
330 (setq delete-list (car tmp))
333 (if (setq msg (wl-pop delete-list))
334 (setq arcnum (/ msg wl-expire-archive-files))
336 (when (and prev-arcnum
337 (not (eq arcnum prev-arcnum)))
338 (setq dst-folder (format dst-folder-fmt
339 (* prev-arcnum wl-expire-archive-files)))
343 folder arcmsg-list msgdb dst-folder t preserve-number
345 (wl-append deleted-list (car ret-val)))
346 (setq arcmsg-list nil))
349 (wl-append arcmsg-list (list msg))
350 (setq prev-arcnum arcnum)))
354 (defun wl-expire-archive-number2 (folder delete-list msgdb
355 &optional preserve-number no-delete)
356 "Standard function for `wl-summary-expire'.
357 Refile to archive folder followed the number of message in one archive folder."
358 (let* ((elmo-archive-treat-file t) ;; treat archive folder as a file.
359 (dst-folder-fmt (funcall
360 wl-expire-archive-get-folder-func folder))
361 (dst-folder-base (car dst-folder-fmt))
362 (dst-folder-fmt (cdr dst-folder-fmt))
363 (refile-func (if no-delete
365 'wl-expire-refile-with-copy-reserve-msg))
368 arc-len msg arcmsg-list
369 deleted-list ret-val)
370 (setq tmp (wl-expire-archive-number-delete-old
371 dst-folder-base preserve-number delete-list
372 (elmo-msgdb-get-mark-alist msgdb)
374 (when (and (not no-delete)
375 (setq dels (nth 1 tmp)))
376 (wl-append deleted-list (car (wl-expire-delete folder dels msgdb))))
377 (setq delete-list (car tmp)
378 filenum (string-to-int (nth 3 tmp))
383 (if (setq msg (wl-pop delete-list))
385 (setq len (1+ wl-expire-archive-files)))
386 (when (> len wl-expire-archive-files)
388 (setq dst-folder (format dst-folder-fmt filenum))
392 folder arcmsg-list msgdb dst-folder t preserve-number
394 (wl-append deleted-list (car ret-val)))
395 (setq arc-len (+ arc-len (cdr ret-val))))
396 (setq arcmsg-list nil)
397 (if (< arc-len wl-expire-archive-files)
398 (setq len (1+ arc-len))
399 (setq filenum (+ filenum wl-expire-archive-files)
400 len (- len arc-len) ;; maybe 1
401 arc-len (1- len) ;; maybe 0
405 (wl-append arcmsg-list (list msg))))
409 (defun wl-expire-archive-date (folder delete-list msgdb
410 &optional preserve-number no-delete)
411 "Standard function for `wl-summary-expire'.
412 Refile to archive folder followed message date."
413 (let* ((elmo-archive-treat-file t) ;; treat archive folder as a file.
414 (number-alist (elmo-msgdb-get-number-alist msgdb))
415 (overview (elmo-msgdb-get-overview msgdb))
416 (dst-folder-fmt (funcall
417 wl-expire-archive-get-folder-func
419 wl-expire-archive-date-folder-name-fmt
421 (dst-folder-base (car dst-folder-fmt))
422 (dst-folder-fmt (cdr dst-folder-fmt))
423 (refile-func (if no-delete
425 'wl-expire-refile-with-copy-reserve-msg))
426 tmp dels dst-folder date time
427 msg arcmsg-alist arcmsg-list
428 deleted-list ret-val)
429 (setq tmp (wl-expire-archive-number-delete-old
430 dst-folder-base preserve-number delete-list
431 (elmo-msgdb-get-mark-alist msgdb)
433 wl-expire-archive-date-folder-num-regexp))
434 (when (and (not no-delete)
435 (setq dels (nth 1 tmp)))
436 (wl-append deleted-list (car (wl-expire-delete folder dels msgdb))))
437 (setq delete-list (car tmp))
438 (while (setq msg (wl-pop delete-list))
439 (setq date (elmo-msgdb-overview-entity-get-date
440 (assoc (cdr (assq msg number-alist)) overview)))
443 (timezone-fix-time date nil nil)
444 (error [0 0 0 0 0 0 0])))
445 (if (= (aref time 1) 0) ;; if (month == 0)
446 (aset time 0 0)) ;; year = 0
447 (setq dst-folder (format dst-folder-fmt
448 (aref time 0) ;; year
449 (aref time 1) ;; month
452 (wl-append-assoc-list
457 (setq dst-folder (caar arcmsg-alist))
458 (setq arcmsg-list (cdar arcmsg-alist))
462 folder arcmsg-list msgdb dst-folder t preserve-number
464 (wl-append deleted-list (car ret-val)))
465 (setq arcmsg-alist (cdr arcmsg-alist)))
469 (defsubst wl-expire-folder-p (folder)
470 (wl-get-assoc-list-value wl-expire-alist folder))
472 (defun wl-summary-expire (&optional folder-name notsummary nolist)
474 (let ((folder (or folder-name wl-summary-buffer-folder-name))
475 (alist wl-expire-alist)
477 (when (and (or (setq expires (wl-expire-folder-p folder))
478 (progn (and (interactive-p)
479 (message "no match %s in wl-expire-alist"
482 (or (not (interactive-p))
483 (y-or-n-p (format "Expire %s? " folder))))
484 (let* ((msgdb (or wl-summary-buffer-msgdb
485 (elmo-msgdb-load folder)))
486 (number-alist (elmo-msgdb-get-number-alist msgdb))
487 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
488 expval rm-type val-type value more args
491 (setq expval (car expires)
492 rm-type (nth 1 expires)
494 (setq val-type (car expval)
497 (run-hooks 'wl-summary-expire-pre-hook)
500 ((eq val-type 'number)
501 (let* ((msgs (if (not nolist)
502 (elmo-list-folder folder)
503 (mapcar 'car number-alist)))
504 (msglen (length msgs))
505 (more (or more (1+ value)))
507 (when (>= msglen more)
508 (setq count (- msglen value))
509 (while (and msgs (> count 0))
510 (when (assq (car msgs) number-alist) ;; don't expire new message
511 (wl-append delete-list (list (car msgs)))
512 (when (or (not wl-expire-number-with-reserve-marks)
513 (wl-expire-msg-p (car msgs) mark-alist))
514 (setq count (1- count))))
515 (setq msgs (cdr msgs))))))
517 (let* ((overview (elmo-msgdb-get-overview msgdb))
518 (key-date (elmo-date-get-offset-datevec
519 (timezone-fix-time (current-time-string)
520 (current-time-zone) nil)
523 (when (wl-expire-date-p
525 (elmo-msgdb-overview-entity-get-date
527 (wl-append delete-list
528 (list (elmo-msgdb-overview-entity-get-number
530 (setq overview (cdr overview)))))
532 (error "%s: not supported" val-type)))
535 (setq wl-expired-alist (wl-expired-alist-load)))
537 (cond ((eq rm-type nil) nil)
538 ((eq rm-type 'remove)
539 (car (wl-expire-delete folder delete-list msgdb)))
541 (car (wl-expire-refile folder delete-list msgdb wl-trash-folder)))
543 (car (wl-expire-refile folder delete-list msgdb rm-type)))
545 (apply rm-type (append (list folder delete-list msgdb)
548 (error "%s: invalid type" rm-type))))
549 (when (and (not notsummary) delete-list)
550 (wl-summary-delete-messages-on-buffer delete-list)
551 (wl-summary-folder-info-update)
552 (wl-summary-set-message-modified)
553 (wl-summary-set-mark-modified)
555 (set-buffer-modified-p nil))
556 (wl-expired-alist-save))
557 (run-hooks 'wl-summary-expire-hook)
559 (message "Expiring %s is done" folder)
561 (message "No expire"))))
565 (defun wl-folder-expire-entity (entity)
568 (let ((flist (nth 2 entity)))
570 (wl-folder-expire-entity (car flist))
571 (setq flist (cdr flist)))))
573 (when (wl-expire-folder-p entity)
574 (let ((update-msgdb (cond
575 ((consp wl-expire-folder-update-msgdb)
576 (wl-string-match-member
578 wl-expire-folder-update-msgdb))
580 wl-expire-folder-update-msgdb)))
581 (wl-summary-highlight (if (or (wl-summary-sticky-p entity)
582 (wl-summary-always-sticky-folder-p
584 wl-summary-highlight))
585 wl-auto-select-first ret-val)
586 (save-window-excursion
589 (wl-summary-goto-folder-subr entity 'force-update nil))
590 (setq ret-val (wl-summary-expire entity (not update-msgdb)))
592 (wl-summary-save-status 'keep)
594 (wl-folder-check-entity entity))))))))))
598 (defun wl-folder-expire-current-entity ()
601 (or (wl-folder-get-folder-name-by-id
602 (get-text-property (point) 'wl-folder-entity-id))
603 (wl-folder-get-realname (wl-folder-folder-name)))))
604 (when (and entity-name
605 (or (not (interactive-p))
606 (y-or-n-p (format "Expire %s? " entity-name))))
607 (wl-folder-expire-entity
608 (wl-folder-search-entity-by-name entity-name
610 (if (get-buffer wl-summary-buffer-name)
611 (kill-buffer wl-summary-buffer-name))
612 (message "Expiring %s is done" entity-name))))
616 (defun wl-folder-archive-current-entity ()
619 (or (wl-folder-get-folder-name-by-id
620 (get-text-property (point) 'wl-folder-entity-id))
621 (wl-folder-get-realname (wl-folder-folder-name)))))
622 (when (and entity-name
623 (or (not (interactive-p))
624 (y-or-n-p (format "Archive %s? " entity-name))))
625 (wl-folder-archive-entity
626 (wl-folder-search-entity-by-name entity-name
628 (message "Archiving %s is done" entity-name))))
630 (defun wl-archive-number1 (folder archive-list msgdb)
631 (wl-expire-archive-number1 folder archive-list msgdb t t))
633 (defun wl-archive-number2 (folder archive-list msgdb)
634 (wl-expire-archive-number2 folder archive-list msgdb t t))
636 (defun wl-archive-date (folder archive-list msgdb)
637 (wl-expire-archive-date folder archive-list msgdb t t))
639 (defun wl-archive-folder (folder archive-list msgdb dst-folder)
640 (let* ((elmo-archive-treat-file t) ;; treat archive folder as a file.
643 (car (wl-expire-archive-number-delete-old
645 (elmo-msgdb-get-mark-alist msgdb)
651 folder archive-list msgdb dst-folder t t t)) ;; copy!!
652 (wl-append copied-list ret-val)))
656 (defun wl-summary-archive (&optional arg folder-name notsummary nolist)
658 (let* ((folder (or folder-name wl-summary-buffer-folder-name))
659 (msgdb (or wl-summary-buffer-msgdb
660 (elmo-msgdb-load folder)))
661 (msgs (if (not nolist)
662 (elmo-list-folder folder)
663 (mapcar 'car (elmo-msgdb-get-number-alist msgdb))))
664 (alist wl-archive-alist)
665 func dst-folder archive-list)
667 (let ((wl-default-spec (char-to-string
668 (car (rassq 'archive elmo-spec-alist)))))
669 (setq dst-folder (wl-summary-read-folder
670 (concat wl-default-spec (substring folder 1))
672 (run-hooks 'wl-summary-archive-pre-hook)
674 (wl-archive-folder folder msgs msgdb dst-folder)
675 (when (and (catch 'match
677 (when (string-match (caar alist) folder)
678 (setq func (cadar alist))
680 (setq alist (cdr alist)))
682 (message "No match %s in wl-archive-alist" folder))
684 (or (not (interactive-p))
685 (y-or-n-p (format "Archive %s? " folder))))
687 (funcall func folder msgs msgdb))
688 (run-hooks 'wl-summary-archive-hook)
690 (message "Archiving %s is done" folder)
692 (message "No archive")))))))
694 (defun wl-folder-archive-entity (entity)
697 (let ((flist (nth 2 entity)))
699 (wl-folder-archive-entity (car flist))
700 (setq flist (cdr flist)))))
702 (wl-summary-archive nil entity t))))
706 (defun wl-expire-append-log (src-folder msgs dst-folder action)
707 (when wl-expire-use-log
709 (let ((tmp-buf (get-buffer-create " *wl-expire work*"))
710 (filename (expand-file-name wl-expired-log-alist-file-name
715 (insert (format "%s\t%s -> %s\t%s\n"
717 src-folder dst-folder msgs))
718 (insert (format "%s\t%s\t%s\n"
721 (if (file-writable-p filename)
722 (write-region (point-min) (point-max)
724 (message (format "%s is not writable." filename)))
725 (kill-buffer tmp-buf)))))
729 ;;; wl-expire.el ends here