1 ;;; elmo-util.el -- Utilities for Elmo.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
38 (require 'eword-decode)
42 (defmacro elmo-set-buffer-multibyte (flag)
43 "Set the multibyte flag of the current buffer to FLAG."
45 (list 'setq 'mc-flag flag))
48 ((and (boundp 'emacs-major-version) (>= emacs-major-version 20))
49 (list 'set-buffer-multibyte flag))
53 (defvar elmo-work-buf-name " *elmo work*")
54 (defvar elmo-temp-buf-name " *elmo temp*")
56 (or (boundp 'default-enable-multibyte-characters)
57 (defvar default-enable-multibyte-characters (featurep 'mule)
58 "The mock variable except for Emacs 20."))
60 (defun elmo-base64-encode-string (string &optional no-line-break))
61 (defun elmo-base64-decode-string (string))
63 ;; base64 encoding/decoding
65 (fset 'elmo-base64-encode-string
66 (mel-find-function 'mime-encode-string "base64"))
67 (fset 'elmo-base64-decode-string
68 (mel-find-function 'mime-decode-string "base64"))
70 ;; Any Emacsen may have add-name-to-file(), because loadup.el requires it. :-p
71 ;; Check make-symbolic-link() instead. -- 981002 by Fuji
72 (if (fboundp 'make-symbolic-link) ;; xxx
73 (defalias 'elmo-add-name-to-file 'add-name-to-file)
74 (defun elmo-add-name-to-file
75 (filename newname &optional ok-if-already-exists)
76 (copy-file filename newname ok-if-already-exists t)))
78 ;; Nemacs's `read' is different.
79 (static-if (fboundp 'nemacs-version)
80 (defun elmo-read (obj)
83 (or (bobp) (forward-char -1)))))
84 (defalias 'elmo-read 'read))
86 (defmacro elmo-set-work-buf (&rest body)
87 "Execute BODY on work buffer. Work buffer remains."
89 (set-buffer (get-buffer-create elmo-work-buf-name))
90 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
94 (defmacro elmo-bind-directory (dir &rest body)
95 "Set current directory DIR and execute BODY."
96 (` (let ((default-directory (file-name-as-directory (, dir))))
99 (defun elmo-object-load (filename &optional mime-charset no-err)
100 "Load OBJECT from the file specified by FILENAME.
101 File content is decoded with MIME-CHARSET."
102 (if (not (file-readable-p filename))
105 (as-binary-input-file
106 (insert-file-contents filename))
108 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
109 (decode-mime-charset-region (point-min) (point-max) mime-charset))
111 (read (current-buffer))
112 (error (unless no-err
113 (message "Warning: Loading object from %s failed."
115 (elmo-object-save filename nil))
118 (defsubst elmo-save-buffer (filename &optional mime-charset)
119 "Save current buffer to the file specified by FILENAME.
120 Directory of the file is created if it doesn't exist.
121 File content is encoded with MIME-CHARSET."
122 (let ((dir (directory-file-name (file-name-directory filename))))
123 (if (file-directory-p dir)
125 (unless (file-exists-p dir)
126 (elmo-make-directory dir)))
127 (if (file-writable-p filename)
130 ;;; (elmo-set-buffer-multibyte default-enable-multibyte-characters)
131 (encode-mime-charset-region (point-min) (point-max) mime-charset))
132 (as-binary-output-file
133 (write-region (point-min) (point-max) filename nil 'no-msg)))
134 (message (format "%s is not writable." filename)))))
136 (defun elmo-object-save (filename object &optional mime-charset)
137 "Save OBJECT to the file specified by FILENAME.
138 Directory of the file is created if it doesn't exist.
139 File content is encoded with MIME-CHARSET."
141 (prin1 object (current-buffer))
142 ;;;(princ "\n" (current-buffer))
143 (elmo-save-buffer filename mime-charset)))
145 (defun elmo-get-network-stream-type (stream-type stream-type-alist)
147 (while stream-type-alist
148 (if (eq (nth 1 (car stream-type-alist)) stream-type)
149 (throw 'found (car stream-type-alist)))
150 (setq stream-type-alist (cdr stream-type-alist)))))
154 (defconst elmo-condition-atom-regexp "[^/ \")|&]*")
156 (defun elmo-read-search-condition (default)
157 "Read search condition string interactively."
158 (elmo-read-search-condition-internal "Search by" default))
160 (defun elmo-read-search-condition-internal (prompt default)
161 (let* ((completion-ignore-case t)
162 (field (completing-read
163 (format "%s (%s): " prompt default)
167 "From" "Subject" "To" "Cc" "Body"
168 "Since" "Before" "ToCc"
169 "!From" "!Subject" "!To" "!Cc" "!Body"
170 "!Since" "!Before" "!ToCc")
171 elmo-msgdb-extra-fields)) nil t))
173 (setq field (if (string= field "")
177 ((or (string= field "AND") (string= field "OR"))
179 (elmo-read-search-condition-internal
180 (concat field "(1) Search by") default)
181 (if (string= field "AND") "&" "|")
182 (elmo-read-search-condition-internal
183 (concat field "(2) Search by") default)
185 ((string-match "Since\\|Before" field)
186 (concat (downcase field) ":"
187 (completing-read (format "Value for '%s': " field)
190 (list (format "%s" (car x)))))
191 elmo-date-descriptions))))
193 (setq value (read-from-minibuffer (format "Value for '%s': " field)))
194 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
196 (setq value (prin1-to-string value)))
197 (concat (downcase field) ":" value)))))
199 (defsubst elmo-condition-parse-error ()
200 (error "Syntax error in '%s'" (buffer-string)))
202 (defun elmo-parse-search-condition (condition)
204 Return value is a cons cell of (STRUCTURE . REST)"
207 (goto-char (point-min))
208 (cons (elmo-condition-parse) (buffer-substring (point) (point-max)))))
210 ;; condition ::= or-expr
211 (defun elmo-condition-parse ()
212 (or (elmo-condition-parse-or-expr)
213 (elmo-condition-parse-error)))
215 ;; or-expr ::= and-expr /
216 ;; and-expr "|" or-expr
217 (defun elmo-condition-parse-or-expr ()
218 (let ((left (elmo-condition-parse-and-expr)))
219 (if (looking-at "| *")
221 (goto-char (match-end 0))
222 (list 'or left (elmo-condition-parse-or-expr)))
225 ;; and-expr ::= primitive /
226 ;; primitive "&" and-expr
227 (defun elmo-condition-parse-and-expr ()
228 (let ((left (elmo-condition-parse-primitive)))
229 (if (looking-at "& *")
231 (goto-char (match-end 0))
232 (list 'and left (elmo-condition-parse-and-expr)))
235 ;; primitive ::= "(" expr ")" /
236 ;; ["!"] search-key SPACE* ":" SPACE* search-value
237 (defun elmo-condition-parse-primitive ()
240 (goto-char (match-end 0))
241 (prog1 (elmo-condition-parse)
242 (unless (looking-at ") *")
243 (elmo-condition-parse-error))
244 (goto-char (match-end 0))))
245 ;; search-key ::= [A-Za-z-]+
246 ;; ;; "since" / "before" / "last" / "first" /
247 ;; ;; "body" / field-name
248 ((looking-at "\\(!\\)? *\\([A-Za-z-]+\\) *: *")
249 (goto-char (match-end 0))
250 (let ((search-key (vector
251 (if (match-beginning 1) 'unmatch 'match)
252 (elmo-match-buffer 2)
253 (elmo-condition-parse-search-value))))
255 (if (string= (aref search-key 1) "tocc")
256 (if (eq (aref search-key 0) 'match)
258 (vector 'match "to" (aref search-key 2))
259 (vector 'match "cc" (aref search-key 2)))
261 (vector 'unmatch "to" (aref search-key 2))
262 (vector 'unmatch "cc" (aref search-key 2))))
265 ;; search-value ::= quoted / time / number / atom
266 ;; quoted ::= <elisp string expression>
267 ;; time ::= "yesterday" / "lastweek" / "lastmonth" / "lastyear" /
268 ;; number SPACE* "daysago" /
269 ;; number "-" month "-" number ; ex. 10-May-2000
271 ;; month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
272 ;; "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
273 ;; atom ::= ATOM_CHARS*
274 ;; SPACE ::= <ascii space character, 0x20>
275 ;; ATOM_CHARS ::= <any character except specials>
276 ;; specials ::= SPACE / <"> / </> / <)> / <|> / <&>
277 ;; ;; These characters should be quoted.
278 (defun elmo-condition-parse-search-value ()
281 (elmo-read (current-buffer)))
282 ((or (looking-at "yesterday") (looking-at "lastweek")
283 (looking-at "lastmonth") (looking-at "lastyear")
284 (looking-at "[0-9]+ *daysago")
285 (looking-at "[0-9]+-[A-Za-z]+-[0-9]+")
286 (looking-at "[0-9]+")
287 (looking-at elmo-condition-atom-regexp))
288 (prog1 (elmo-match-buffer 0)
289 (goto-char (match-end 0))))
290 (t (error "Syntax error '%s'" (buffer-string)))))
293 (defsubst elmo-buffer-replace (regexp &optional newtext)
294 (goto-char (point-min))
295 (while (re-search-forward regexp nil t)
296 (replace-match (or newtext ""))))
298 (defsubst elmo-delete-char (char string &optional unibyte)
301 (let ((coding-system-for-read 'no-conversion)
302 (coding-system-for-write 'no-conversion))
303 (if unibyte (elmo-set-buffer-multibyte nil))
305 (goto-char (point-min))
306 (while (search-forward (char-to-string char) nil t)
310 (defsubst elmo-delete-cr-buffer ()
311 "Delete CR from buffer."
313 (goto-char (point-min))
314 (while (search-forward "\r\n" nil t)
315 (replace-match "\n")) ))
317 (defsubst elmo-delete-cr-get-content-type ()
319 (goto-char (point-min))
320 (while (search-forward "\r\n" nil t)
321 (replace-match "\n"))
322 (goto-char (point-min))
323 (or (std11-field-body "content-type")
326 (defun elmo-delete-cr (string)
330 (goto-char (point-min))
331 (while (search-forward "\r\n" nil t)
332 (replace-match "\n"))
335 (defun elmo-uniq-list (lst)
336 "Distractively uniqfy elements of LST."
345 (defun elmo-list-insert (list element after)
346 "Insert an ELEMENT to the LIST, just after AFTER."
351 (if (eq (car li) after)
352 (setq p li pn curn li nil)
356 (setcdr (nthcdr pn list) (cons element (cdr p)))
357 (nconc list (list element)))))
359 (defun elmo-string-partial-p (string)
360 (and (stringp string) (string-match "message/partial" string)))
362 (defun elmo-get-file-string (filename &optional remove-final-newline)
364 (let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
365 insert-file-contents-post-hook)
366 (when (file-exists-p filename)
368 (as-binary-input-file (insert-file-contents filename)))
369 (when (and remove-final-newline
371 (= (char-after (1- (point-max))) ?\n))
372 (goto-char (point-max))
373 (delete-backward-char 1))
376 (defun elmo-save-string (string filename)
379 (as-binary-output-file
381 (write-region (point-min) (point-max)
382 filename nil 'no-msg))
385 (defun elmo-max-of-list (nlist)
389 (if (< max-num (car l))
390 (setq max-num (car l)))
394 (defun elmo-concat-path (path filename)
395 (if (not (string= path ""))
396 (if (string= elmo-path-sep (substring path (- (length path) 1)))
397 (concat path filename)
398 (concat path elmo-path-sep filename))
401 (defvar elmo-passwd-alist nil)
403 (defun elmo-passwd-alist-load ()
405 (let ((filename (expand-file-name elmo-passwd-alist-file-name
407 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*"))
408 insert-file-contents-pre-hook ; To avoid autoconv-xmas...
409 insert-file-contents-post-hook
411 (if (not (file-readable-p filename))
413 (set-buffer tmp-buffer)
414 (insert-file-contents filename)
417 (read (current-buffer))
419 (kill-buffer tmp-buffer)
422 (defun elmo-passwd-alist-clear ()
423 "Clear password cache."
425 (setq elmo-passwd-alist nil))
427 (defun elmo-passwd-alist-save ()
428 "Save password into file."
431 (let ((filename (expand-file-name elmo-passwd-alist-file-name
433 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*")))
434 (set-buffer tmp-buffer)
436 (prin1 elmo-passwd-alist tmp-buffer)
437 (princ "\n" tmp-buffer)
438 ;;; (if (and (file-exists-p filename)
439 ;;; (not (equal 384 (file-modes filename))))
440 ;;; (error "%s is not safe.chmod 600 %s!" filename filename))
441 (if (file-writable-p filename)
443 (write-region (point-min) (point-max)
444 filename nil 'no-msg)
445 (set-file-modes filename 384))
446 (message (format "%s is not writable." filename)))
447 (kill-buffer tmp-buffer))))
449 (defun elmo-get-passwd (key)
450 "Get password from password pool."
452 (if (not elmo-passwd-alist)
453 (setq elmo-passwd-alist (elmo-passwd-alist-load)))
454 (setq pair (assoc key elmo-passwd-alist))
456 (elmo-base64-decode-string (cdr pair))
457 (setq pass (elmo-read-passwd (format "Password for %s: "
459 (setq elmo-passwd-alist
460 (append elmo-passwd-alist
462 (elmo-base64-encode-string pass)))))
463 (if elmo-passwd-life-time
464 (run-with-timer elmo-passwd-life-time nil
465 (` (lambda () (elmo-remove-passwd (, key))))))
468 (defun elmo-remove-passwd (key)
469 "Remove password from password pool (for failure)."
471 (if (setq pass-cons (assoc key elmo-passwd-alist))
474 (fillarray (cdr pass-cons) 0))
475 (setq elmo-passwd-alist
476 (delete pass-cons elmo-passwd-alist))))))
478 (defmacro elmo-read-char-exclusive ()
479 (cond ((featurep 'xemacs)
480 '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
485 (key-press-event-p (setq event (next-command-event)))
486 (setq key (or (event-to-character event)
487 (cdr (assq (event-key event) table)))))))
489 ((fboundp 'read-char-exclusive)
490 '(read-char-exclusive))
494 (defun elmo-read-passwd (prompt &optional stars)
495 "Read a single line of text from user without echoing, and return it."
499 (cursor-in-echo-area t)
500 (log-message-max-size 0)
501 message-log-max done msg truncate)
503 (if (or (not stars) (string= "" ans))
505 (setq msg (concat prompt (make-string (length ans) ?.)))
507 (1+ (- (length msg) (window-width (minibuffer-window)))))
509 (setq msg (concat "$" (substring msg (1+ truncate))))))
511 (setq c (elmo-read-char-exclusive))
515 ((or (= c ?\r) (= c ?\n) (= c ?\e))
519 ((and (/= c ?\b) (/= c ?\177))
520 (setq ans (concat ans (char-to-string c))))
522 (setq ans (substring ans 0 -1)))))
531 (defun elmo-string-to-list (string)
534 (goto-char (point-min))
536 (goto-char (point-max))
538 (goto-char (point-min))
539 (read (current-buffer))))
541 (defun elmo-list-to-string (list)
550 (if (symbolp (car tlist))
551 (symbol-name (car tlist))
556 (setq tlist (cdr tlist)))
566 (defun elmo-plug-on-by-servers (alist &optional servers)
567 (let ((server-list (or servers elmo-plug-on-servers)))
570 (if (elmo-plugged-p (car server-list))
572 (setq server-list (cdr server-list))))))
574 (defun elmo-plug-on-by-exclude-servers (alist &optional servers)
575 (let ((server-list (or servers elmo-plug-on-exclude-servers))
576 server other-servers)
578 (when (and (not (member (setq server (caaar alist)) server-list))
579 (not (member server other-servers)))
580 (push server other-servers))
581 (setq alist (cdr alist)))
582 (elmo-plug-on-by-servers alist other-servers)))
584 (defun elmo-plugged-p (&optional server port stream-type alist label-exp)
585 (let ((alist (or alist elmo-plugged-alist))
587 (cond ((and (not port) (not server))
588 (cond ((eq elmo-plugged-condition 'one)
592 (if (nth 2 (car alist))
594 (setq alist (cdr alist))))
596 ((eq elmo-plugged-condition 'all)
600 (if (not (nth 2 (car alist)))
601 (throw 'plugged nil))
602 (setq alist (cdr alist)))
605 ((functionp elmo-plugged-condition)
606 (funcall elmo-plugged-condition alist))
609 ((not port) ;; server
612 (when (string= server (caaar alist))
613 (if (nth 2 (car alist))
615 (setq alist (cdr alist)))))
617 (setq plugged-info (assoc (list server port stream-type) alist))
618 (if (not plugged-info)
619 ;; add elmo-plugged-alist automatically
621 (elmo-set-plugged elmo-plugged server port stream-type
622 nil nil nil label-exp)
624 (if (and elmo-auto-change-plugged
625 (> elmo-auto-change-plugged 0)
626 (nth 3 plugged-info) ;; time
627 (elmo-time-expire (nth 3 plugged-info)
628 elmo-auto-change-plugged))
630 (nth 2 plugged-info)))))))
632 (defun elmo-set-plugged (plugged &optional server port stream-type time
634 (let ((alist (or alist elmo-plugged-alist))
636 (cond ((and (not port) (not server))
637 (setq elmo-plugged plugged)
638 ;; set plugged all element of elmo-plugged-alist.
640 (setcdr (cdar alist) (list plugged time))
641 (setq alist (cdr alist))))
643 ;; set plugged all port of server
645 (when (string= server (caaar alist))
646 (setcdr (cdar alist) (list plugged time)))
647 (setq alist (cdr alist))))
649 ;; set plugged one port of server
650 (setq plugged-info (assoc (list server port stream-type) alist))
651 (setq label (if label-exp
653 (nth 1 plugged-info)))
655 ;; if add is non-nil, don't reset plug state.
657 (setcdr plugged-info (list label plugged time)))
659 (setq elmo-plugged-alist
663 (list (list server port stream-type)
664 label plugged time))))))))
667 (defun elmo-delete-plugged (&optional server port alist)
668 (let* ((alist (or alist elmo-plugged-alist))
670 (cond ((and (not port) (not server))
673 ;; delete plugged all port of server
675 (when (string= server (caaar alist2))
676 (setq alist (delete (car alist2) alist)))
677 (setq alist2 (cdr alist2))))
679 ;; delete plugged one port of server
681 (delete (assoc (cons server port) alist) alist))))
684 (defun elmo-disk-usage (path)
685 "Get disk usage (bytes) in PATH."
687 (condition-case () (file-attributes path) (error nil))))
689 (if (nth 0 file-attr) ; directory
690 (let ((files (condition-case ()
691 (directory-files path t "^[^\\.]")
694 ;; (result (nth 7 file-attr))) ... directory size
696 (setq result (+ result (or (elmo-disk-usage (car files)) 0)))
697 (setq files (cdr files)))
699 (float (nth 7 file-attr))))))
701 (defun elmo-get-last-accessed-time (path &optional dir)
702 "Return the last accessed time of PATH."
703 (let ((last-accessed (nth 4 (file-attributes (or (and dir
708 (setq last-accessed (+ (* (nth 0 last-accessed)
709 (float 65536)) (nth 1 last-accessed)))
712 (defun elmo-get-last-modification-time (path &optional dir)
713 "Return the last accessed time of PATH."
714 (let ((last-modified (nth 5 (file-attributes (or (and dir
718 (setq last-modified (+ (* (nth 0 last-modified)
719 (float 65536)) (nth 1 last-modified)))))
721 (defun elmo-make-directory (path)
722 "Create directory recursively."
723 (let ((parent (directory-file-name (file-name-directory path))))
724 (if (null (file-directory-p parent))
725 (elmo-make-directory parent))
726 (make-directory path)
727 (if (string= path (expand-file-name elmo-msgdb-dir))
728 (set-file-modes path (+ (* 64 7) (* 8 0) 0))))) ; chmod 0700
730 (defun elmo-delete-directory (path &optional no-hierarchy)
731 "Delete directory recursively."
732 (if (stringp path) ; nil is not permitted.
733 (let ((dirent (directory-files path))
734 relpath abspath hierarchy)
736 (setq relpath (car dirent)
738 abspath (expand-file-name relpath path))
739 (when (not (string-match "^\\.\\.?$" relpath))
740 (if (eq (nth 0 (file-attributes abspath)) t)
743 (elmo-delete-directory abspath no-hierarchy))
744 (delete-file abspath))))
746 (delete-directory path)))))
748 (defun elmo-list-filter (l1 l2)
751 ;; t means filter all.
754 (elmo-delete-if (lambda (x) (not (memq x l1))) l2)
758 (defsubst elmo-list-delete-if-smaller (list number)
759 (let ((ret-val (copy-sequence list)))
761 (if (< (car list) number)
762 (setq ret-val (delq (car list) ret-val)))
763 (setq list (cdr list)))
766 (defun elmo-list-diff (list1 list2 &optional mes)
769 (let ((clist1 (copy-sequence list1))
770 (clist2 (copy-sequence list2)))
772 (setq clist1 (delq (car list2) clist1))
773 (setq list2 (cdr list2)))
775 (setq clist2 (delq (car list1) clist2))
776 (setq list1 (cdr list1)))
778 (message (concat mes "done.")))
779 (list clist1 clist2)))
781 (defun elmo-list-bigger-diff (list1 list2 &optional mes)
782 "Returns a list (- +). + is bigger than max of LIST1, in LIST2."
787 (max-of-l2 (or (nth (max 0 (1- (length l2))) l2) 0))
791 (setq num (+ (length l1)))
793 (if (memq (car l1) l2)
794 (if (eq (car l1) (car l2))
797 (if (> (car l1) max-of-l2)
798 (setq diff1 (nconc diff1 (list (car l1))))))
802 (setq percent (/ (* i 100) num))
803 (if (eq (% percent 5) 0)
804 (elmo-display-progress
805 'elmo-list-bigger-diff "%s%d%%" percent mes))))
807 (cons diff1 (list l2)))))
809 (defmacro elmo-filter-type (filter)
810 (` (aref (, filter) 0)))
812 (defmacro elmo-filter-key (filter)
813 (` (aref (, filter) 1)))
815 (defmacro elmo-filter-value (filter)
816 (` (aref (, filter) 2)))
818 (defsubst elmo-buffer-field-primitive-condition-match (condition
822 (goto-char (point-min))
824 ((string= (elmo-filter-key condition) "last")
825 (setq result (<= (length (memq number number-list))
826 (string-to-int (elmo-filter-value condition)))))
827 ((string= (elmo-filter-key condition) "first")
828 (setq result (< (- (length number-list)
829 (length (memq number number-list)))
830 (string-to-int (elmo-filter-value condition)))))
831 ((string= (elmo-filter-key condition) "since")
832 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
835 (timezone-make-sortable-date (aref date 0)
838 (timezone-make-time-string
842 (timezone-make-date-sortable (std11-field-body "date"))))))
843 ((string= (elmo-filter-key condition) "before")
844 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
847 (timezone-make-date-sortable (std11-field-body "date"))
848 (timezone-make-sortable-date (aref date 0)
851 (timezone-make-time-string
855 ((string= (elmo-filter-key condition) "body")
856 (and (re-search-forward "^$" nil t) ; goto body
857 (setq result (search-forward (elmo-filter-value condition)
860 (let ((fval (std11-field-body (elmo-filter-key condition))))
861 (if (eq (length fval) 0) (setq fval nil))
862 (if fval (setq fval (eword-decode-string fval)))
863 (setq result (and fval (string-match
864 (elmo-filter-value condition) fval))))))
865 (if (eq (elmo-filter-type condition) 'unmatch)
866 (setq result (not result)))
869 (defun elmo-condition-find-key-internal (condition key)
872 (if (string= (elmo-filter-key condition) key)
874 ((or (eq (car condition) 'and)
875 (eq (car condition) 'or))
876 (elmo-condition-find-key-internal (nth 1 condition) key)
877 (elmo-condition-find-key-internal (nth 2 condition) key))))
879 (defun elmo-condition-find-key (condition key)
881 (elmo-condition-find-key-internal condition key)))
883 (defun elmo-buffer-field-condition-match (condition number number-list)
886 (elmo-buffer-field-primitive-condition-match
887 condition number number-list))
888 ((eq (car condition) 'and)
889 (and (elmo-buffer-field-condition-match
890 (nth 1 condition) number number-list)
891 (elmo-buffer-field-condition-match
892 (nth 2 condition) number number-list)))
893 ((eq (car condition) 'or)
894 (or (elmo-buffer-field-condition-match
895 (nth 1 condition) number number-list)
896 (elmo-buffer-field-condition-match
897 (nth 2 condition) number number-list)))))
899 (defsubst elmo-file-field-primitive-condition-match (file
904 (goto-char (point-min))
906 ((string= (elmo-filter-key condition) "last")
907 (setq result (<= (length (memq number number-list))
908 (string-to-int (elmo-filter-value condition)))))
909 ((string= (elmo-filter-key condition) "first")
910 (setq result (< (- (length number-list)
911 (length (memq number number-list)))
912 (string-to-int (elmo-filter-value condition)))))
915 (as-binary-input-file (insert-file-contents file))
916 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
917 ;; Should consider charset?
918 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
920 (elmo-buffer-field-primitive-condition-match
921 condition number number-list)))))
922 (if (eq (elmo-filter-type condition) 'unmatch)
923 (setq result (not result)))
926 (defun elmo-file-field-condition-match (file condition number number-list)
929 (elmo-file-field-primitive-condition-match
930 file condition number number-list))
931 ((eq (car condition) 'and)
932 (and (elmo-file-field-condition-match
933 file (nth 1 condition) number number-list)
934 (elmo-file-field-condition-match
935 file (nth 2 condition) number number-list)))
936 ((eq (car condition) 'or)
937 (or (elmo-file-field-condition-match
938 file (nth 1 condition) number number-list)
939 (elmo-file-field-condition-match
940 file (nth 2 condition) number number-list)))))
942 (defmacro elmo-get-hash-val (string hashtable)
943 (let ((sym (list 'intern-soft string hashtable)))
944 (list 'if (list 'boundp sym)
945 (list 'symbol-value sym))))
947 (defmacro elmo-set-hash-val (string value hashtable)
948 (list 'set (list 'intern string hashtable) value))
950 (defmacro elmo-clear-hash-val (string hashtable)
951 (static-if (fboundp 'unintern)
952 (list 'unintern string hashtable)
953 (list 'makunbound (list 'intern string hashtable))))
955 (defmacro elmo-unintern (string)
956 "`unintern' symbol named STRING, When can use `unintern'.
957 Emacs 19.28 or earlier does not have `unintern'."
958 (static-if (fboundp 'unintern)
959 (list 'unintern string)))
961 (defun elmo-make-hash (&optional hashsize)
962 "Make a new hash table which have HASHSIZE size."
966 ;; Prime numbers as lengths tend to result in good
967 ;; hashing; lengths one less than a power of two are
971 (while (< (- i 1) hashsize)
974 elmo-hash-maximum-size)
975 elmo-hash-minimum-size)
976 elmo-hash-minimum-size)
979 (defsubst elmo-mime-string (string)
980 "Normalize MIME encoded STRING."
984 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
985 (setq str (eword-decode-string
986 (decode-mime-charset-string string elmo-mime-charset)))
987 (setq str (encode-mime-charset-string str elmo-mime-charset))
988 (elmo-set-buffer-multibyte nil)
991 (defsubst elmo-collect-field (beg end downcase-field-name)
994 (narrow-to-region beg end)
995 (goto-char (point-min))
996 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
998 (while (re-search-forward regexp nil t)
999 (setq name (buffer-substring-no-properties
1000 (match-beginning 1)(1- (match-end 1))))
1001 (if downcase-field-name
1002 (setq name (downcase name)))
1003 (setq body (buffer-substring-no-properties
1004 (match-end 0) (std11-field-end)))
1005 (or (assoc name dest)
1006 (setq dest (cons (cons name body) dest))))
1009 (defsubst elmo-collect-field-from-string (string downcase-field-name)
1012 (goto-char (point-min))
1013 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1015 (while (re-search-forward regexp nil t)
1016 (setq name (buffer-substring-no-properties
1017 (match-beginning 1)(1- (match-end 1))))
1018 (if downcase-field-name
1019 (setq name (downcase name)))
1020 (setq body (buffer-substring-no-properties
1021 (match-end 0) (std11-field-end)))
1022 (or (assoc name dest)
1023 (setq dest (cons (cons name body) dest))))
1026 (defun elmo-safe-filename (folder)
1027 (elmo-replace-in-string
1028 (elmo-replace-in-string
1029 (elmo-replace-in-string folder "/" " ")
1033 (defvar elmo-filename-replace-chars nil)
1035 (defsubst elmo-replace-string-as-filename (msgid)
1036 "Replace string as filename."
1037 (setq msgid (elmo-replace-in-string msgid " " " "))
1038 (if (null elmo-filename-replace-chars)
1039 (setq elmo-filename-replace-chars
1040 (regexp-quote (mapconcat
1041 'car elmo-filename-replace-string-alist ""))))
1042 (while (string-match (concat "[" elmo-filename-replace-chars "]")
1045 (substring msgid 0 (match-beginning 0))
1048 (match-beginning 0) (match-end 0))
1049 elmo-filename-replace-string-alist))
1050 (substring msgid (match-end 0)))))
1053 (defsubst elmo-recover-string-from-filename (filename)
1054 "Recover string from FILENAME."
1056 (while (string-match " " filename)
1057 (setq tmp (substring filename
1059 (+ (match-end 0) 1)))
1060 (if (string= tmp " ")
1062 (setq tmp (car (rassoc tmp
1063 elmo-filename-replace-string-alist))))
1066 (substring filename 0 (match-beginning 0))
1068 (setq filename (substring filename (+ (match-end 0) 1))))
1069 (concat result filename)))
1071 (defsubst elmo-copy-file (src dst)
1073 (elmo-add-name-to-file src dst t)
1074 (error (copy-file src dst t))))
1076 (defsubst elmo-buffer-exists-p (buffer)
1077 (if (bufferp buffer)
1078 (buffer-live-p buffer)
1079 (get-buffer buffer)))
1081 (defsubst elmo-kill-buffer (buffer)
1082 (when (elmo-buffer-exists-p buffer)
1083 (kill-buffer buffer)))
1085 (defun elmo-delete-if (pred lst)
1086 "Return new list contain items which don't satisfy PRED in LST."
1089 (unless (funcall pred (car lst))
1090 (setq result (nconc result (list (car lst)))))
1091 (setq lst (cdr lst)))
1094 (defun elmo-list-delete (list1 list2)
1095 "Delete by side effect any occurrences equal to elements of LIST1 from LIST2.
1096 Return the modified LIST2. Deletion is done with `delete'.
1097 Write `(setq foo (elmo-list-delete bar foo))' to be sure of changing
1098 the value of `foo'."
1100 (setq list2 (delete (car list1) list2))
1101 (setq list1 (cdr list1)))
1104 (defun elmo-list-member (list1 list2)
1105 "If any element of LIST1 is member of LIST2, return t."
1108 (if (member (car list1) list2)
1110 (setq list1 (cdr list1)))))
1112 (defun elmo-count-matches (regexp beg end)
1116 (while (re-search-forward regexp end t)
1117 (setq count (1+ count)))
1120 (if (fboundp 'display-error)
1121 (defalias 'elmo-display-error 'display-error)
1122 (defun elmo-display-error (error-object stream)
1123 "A tiny function to display ERROR-OBJECT to the STREAM."
1125 (errobj error-object)
1128 (setq err-mes (concat err-mes (format
1129 (if (stringp (car errobj))
1131 (if (boundp 'nemacs-version)
1133 "%S")) (car errobj))))
1134 (setq errobj (cdr errobj))
1135 (if errobj (setq err-mes (concat err-mes (if first ": " ", "))))
1137 (princ err-mes stream))))
1139 (if (fboundp 'define-error)
1140 (defalias 'elmo-define-error 'define-error)
1141 (defun elmo-define-error (error doc &optional parents)
1143 (setq parents 'error))
1144 (let ((conds (get parents 'error-conditions)))
1146 (error "Not an error symbol: %s" error))
1148 (list 'error-message doc
1149 'error-conditions (cons error conds))))))
1151 (cond ((fboundp 'progress-feedback-with-label)
1152 (defalias 'elmo-display-progress 'progress-feedback-with-label))
1153 ((fboundp 'lprogress-display)
1154 (defalias 'elmo-display-progress 'lprogress-display))
1156 (defun elmo-display-progress (label format &optional value &rest args)
1157 "Print a progress message."
1158 (if (and (null format) (null args))
1160 (apply (function message) (concat format " %d%%")
1161 (nconc args (list value)))))))
1163 (defun elmo-time-expire (before-time diff-time)
1164 (let* ((current (current-time))
1165 (rest (when (< (nth 1 current) (nth 1 before-time))
1169 (list (- (+ (car current) (if rest -1 0)) (car before-time))
1170 (- (+ (or rest 0) (nth 1 current)) (nth 1 before-time))))
1171 (and (eq (car diff) 0)
1172 (< diff-time (nth 1 diff)))))
1174 (if (fboundp 'std11-fetch-field)
1175 (defalias 'elmo-field-body 'std11-fetch-field) ;;no narrow-to-region
1176 (defalias 'elmo-field-body 'std11-field-body))
1178 (defmacro elmo-string (string)
1179 "STRING without text property."
1180 (` (let ((obj (copy-sequence (, string))))
1181 (set-text-properties 0 (length obj) nil obj)
1184 (defun elmo-flatten (list-of-list)
1185 "Flatten LIST-OF-LIST."
1186 (unless (null list-of-list)
1187 (append (if (and (car list-of-list)
1188 (listp (car list-of-list)))
1190 (list (car list-of-list)))
1191 (elmo-flatten (cdr list-of-list)))))
1193 (defun elmo-y-or-n-p (prompt &optional auto default)
1194 "Same as `y-or-n-p'.
1195 But if optional argument AUTO is non-nil, DEFAULT is returned."
1200 (defun elmo-string-member (string slist)
1201 "Return t if STRING is a member of the SLIST."
1204 (if (and (stringp (car slist))
1205 (string= string (car slist)))
1207 (setq slist (cdr slist)))))
1209 (defun elmo-string-match-member (str list &optional case-ignore)
1210 (let ((case-fold-search case-ignore))
1213 (if (string-match (car list) str)
1214 (throw 'member (car list)))
1215 (setq list (cdr list))))))
1217 (defun elmo-string-matched-member (str list &optional case-ignore)
1218 (let ((case-fold-search case-ignore))
1221 (if (string-match str (car list))
1222 (throw 'member (car list)))
1223 (setq list (cdr list))))))
1225 (defsubst elmo-string-delete-match (string pos)
1226 (concat (substring string
1227 0 (match-beginning pos))
1232 (defun elmo-string-match-assoc (key alist &optional case-ignore)
1233 (let ((case-fold-search case-ignore)
1237 (setq a (car alist))
1240 (string-match key (car a)))
1242 (setq alist (cdr alist))))))
1244 (defun elmo-string-matched-assoc (key alist &optional case-ignore)
1245 (let ((case-fold-search case-ignore)
1249 (setq a (car alist))
1252 (string-match (car a) key))
1254 (setq alist (cdr alist))))))
1256 (defun elmo-string-assoc (key alist)
1260 (setq a (car alist))
1263 (string= key (car a)))
1265 (setq alist (cdr alist))))))
1267 (defun elmo-string-rassoc (key alist)
1271 (setq a (car alist))
1274 (string= key (cdr a)))
1276 (setq alist (cdr alist))))))
1278 (defun elmo-string-rassoc-all (key alist)
1281 (if (string= key (cdr (car alist)))
1285 (setq alist (cdr alist)))
1288 ;;; Number set defined by OKAZAKI Tetsurou <okazaki@be.to>
1290 ;; number ::= [0-9]+
1293 ;; number-range ::= "(" beg " . " end ")" ;; cons cell
1294 ;; number-set-elem ::= number / number-range
1295 ;; number-set ::= "(" *number-set-elem ")" ;; list
1297 (defun elmo-number-set-member (number number-set)
1298 "Return non-nil if NUMBER is an element of NUMBER-SET.
1299 The value is actually the tail of NUMBER-RANGE whose car contains NUMBER."
1300 (or (memq number number-set)
1302 (while (and number-set (not found))
1303 (if (and (consp (car number-set))
1304 (and (<= (car (car number-set)) number)
1305 (<= number (cdr (car number-set)))))
1307 (setq number-set (cdr number-set))))
1310 (defun elmo-number-set-append-list (number-set list)
1311 "Append LIST of numbers to the NUMBER-SET.
1312 NUMBER-SET is altered."
1313 (let ((appended number-set))
1315 (setq appended (elmo-number-set-append appended (car list)))
1316 (setq list (cdr list)))
1319 (defun elmo-number-set-append (number-set number)
1320 "Append NUMBER to the NUMBER-SET.
1321 NUMBER-SET is altered."
1322 (let ((number-set-1 number-set)
1324 (while (and number-set (not found))
1325 (setq elem (car number-set))
1328 (eq (+ 1 (cdr elem)) number))
1329 (setcdr elem number)
1331 ((and (integerp elem)
1332 (eq (+ 1 elem) number))
1333 (setcar number-set (cons elem number))
1335 ((or (and (integerp elem) (eq elem number))
1337 (<= (car elem) number)
1338 (<= number (cdr elem))))
1340 (setq number-set (cdr number-set)))
1342 (setq number-set-1 (nconc number-set-1 (list number))))
1345 (defun elmo-number-set-to-number-list (number-set)
1346 "Return a number list which corresponds to NUMBER-SET."
1347 (let (number-list elem i)
1349 (setq elem (car number-set))
1353 (while (<= i (cdr elem))
1354 (setq number-list (cons i number-list))
1357 (setq number-list (cons elem number-list))))
1358 (setq number-set (cdr number-set)))
1359 (nreverse number-list)))
1361 (defcustom elmo-list-subdirectories-ignore-regexp "^\\(\\.\\.?\\|[0-9]+\\)$"
1362 "*Regexp to filter subfolders."
1366 (defun elmo-list-subdirectories-1 (basedir curdir one-level)
1367 (let ((root (zerop (length curdir)))
1368 (w32-get-true-file-link-count t) ; for Meadow
1371 (dolist (file (directory-files (setq dir (expand-file-name curdir basedir))))
1372 (when (and (not (string-match
1373 elmo-list-subdirectories-ignore-regexp
1375 (car (setq attr (file-attributes
1376 (expand-file-name file dir)))))
1377 (when (eq one-level 'check) (throw 'done t))
1379 (concat curdir (and (not root) elmo-path-sep) file))
1381 (setq dirs (nconc dirs
1382 (if (if elmo-have-link-count (< 2 (nth 1 attr))
1384 (elmo-list-subdirectories-1
1387 (if one-level 'check))))
1389 (list (list relpath))
1392 (elmo-list-subdirectories-1
1396 (list relpath)))))))
1399 (defun elmo-list-subdirectories (directory file one-level)
1400 (let ((subdirs (elmo-list-subdirectories-1 directory file one-level)))
1401 (if (zerop (length file))
1403 (cons file subdirs))))
1405 (defun elmo-mapcar-list-of-list (func list-of-list)
1408 (cond ((listp x) (elmo-mapcar-list-of-list func x))
1409 (t (funcall func x))))
1412 (defun elmo-parse (string regexp &optional matchn)
1413 (or matchn (setq matchn 1))
1415 (store-match-data nil)
1416 (while (string-match regexp string (match-end 0))
1417 (setq list (cons (substring string (match-beginning matchn)
1418 (match-end matchn)) list)))
1422 (defmacro elmo-make-file-cache (path status)
1423 "PATH is the cache file name.
1424 STATUS is one of 'section, 'entire or nil.
1425 nil means no cache exists.
1426 'section means partial section cache exists.
1427 'entire means entire cache exists.
1428 If the cache is partial file-cache, TYPE is 'partial."
1429 (` (cons (, path) (, status))))
1431 (defmacro elmo-file-cache-path (file-cache)
1432 "Returns the file path of the FILE-CACHE."
1433 (` (car (, file-cache))))
1435 (defmacro elmo-file-cache-status (file-cache)
1436 "Returns the status of the FILE-CACHE."
1437 (` (cdr (, file-cache))))
1439 (defsubst elmo-cache-to-msgid (filename)
1440 (concat "<" (elmo-recover-string-from-filename filename) ">"))
1442 (defsubst elmo-cache-get-path-subr (msgid)
1443 (let ((chars '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?A ?B ?C ?D ?E ?F))
1444 (clist (string-to-char-list msgid))
1447 (setq sum (+ sum (car clist)))
1448 (setq clist (cdr clist)))
1450 (nth (% (/ sum 16) 2) chars)
1451 (nth (% sum 16) chars))))
1453 (defun elmo-file-cache-get-path (msgid &optional section)
1454 "Get cache path for MSGID.
1455 If optional argument SECTION is specified, partial cache path is returned."
1456 (if (setq msgid (elmo-msgid-to-cache msgid))
1459 (format "%s/%s/%s/%s/%s"
1462 (elmo-cache-get-path-subr msgid)
1465 (format "%s/%s/%s/%s"
1468 (elmo-cache-get-path-subr msgid)
1471 (defmacro elmo-file-cache-expand-path (path section)
1472 "Return file name for the file-cache corresponds to the section.
1473 PATH is the file-cache path.
1474 SECTION is the section string."
1475 (` (expand-file-name (or (, section) "") (, path))))
1477 (defun elmo-file-cache-delete (path)
1478 "Delete a cache on PATH."
1480 (when (file-exists-p path)
1481 (if (file-directory-p path)
1483 (setq files (directory-files path t "^[^\\.]"))
1485 (delete-file (car files))
1486 (setq files (cdr files)))
1487 (delete-directory path))
1488 (delete-file path)))))
1490 (defun elmo-file-cache-exists-p (msgid)
1491 "Returns 'section or 'entire if a cache which corresponds to MSGID exists."
1492 (elmo-file-cache-status (elmo-file-cache-get msgid)))
1494 (defun elmo-file-cache-save (cache-path section)
1495 "Save current buffer as cache on PATH.
1496 Return t if cache is saved successfully."
1498 (let ((path (if section (expand-file-name section cache-path)
1501 (if (and (null section)
1502 (file-directory-p path))
1504 (setq files (directory-files path t "^[^\\.]"))
1506 (delete-file (car files))
1507 (setq files (cdr files)))
1508 (delete-directory path))
1510 (not (file-directory-p cache-path)))
1511 (delete-file cache-path)))
1513 (setq dir (directory-file-name (file-name-directory path)))
1514 (if (not (file-exists-p dir))
1515 (elmo-make-directory dir))
1516 (write-region-as-binary (point-min) (point-max)
1522 (defun elmo-cache-path-section-p (path)
1523 "Return non-nil when PATH is `section' cache path."
1524 (file-directory-p path))
1526 (defun elmo-file-cache-get (msgid &optional section)
1527 "Returns the current file-cache object associated with MSGID.
1528 MSGID is the message-id of the message.
1529 If optional argument SECTION is specified, get partial file-cache object
1530 associated with SECTION."
1532 (let ((path (elmo-cache-get-path msgid)))
1533 (if (and path (file-exists-p path))
1534 (if (elmo-cache-path-section-p path)
1536 (if (file-exists-p (setq path (expand-file-name
1538 (cons path 'section))
1539 ;; section is not specified but sectional.
1540 (cons path 'section))
1543 (cons path 'entire)))
1550 (defun elmo-cache-expire ()
1552 (let* ((completion-ignore-case t)
1553 (method (completing-read (format "Expire by (%s): "
1554 elmo-cache-expire-default-method)
1557 (if (string= method "")
1558 (setq method elmo-cache-expire-default-method))
1559 (funcall (intern (concat "elmo-cache-expire-by-" method)))))
1561 (defun elmo-read-float-value-from-minibuffer (prompt &optional initial)
1562 (let ((str (read-from-minibuffer prompt initial)))
1564 ((string-match "[0-9]*\\.[0-9]+" str)
1565 (string-to-number str))
1566 ((string-match "[0-9]+" str)
1567 (string-to-number (concat str ".0")))
1568 (t (error "%s is not number" str)))))
1570 (defun elmo-cache-expire-by-size (&optional kbytes)
1571 "Expire cache file by size.
1572 If KBYTES is kilo bytes (This value must be float)."
1574 (let ((size (or kbytes
1575 (and (interactive-p)
1576 (elmo-read-float-value-from-minibuffer
1577 "Enter cache disk size (Kbytes): "
1579 (if (integerp elmo-cache-expire-default-size)
1580 (float elmo-cache-expire-default-size)
1581 elmo-cache-expire-default-size))))
1582 (if (integerp elmo-cache-expire-default-size)
1583 (float elmo-cache-expire-default-size))))
1587 (message "Checking disk usage...")
1588 (setq total (/ (elmo-disk-usage
1590 elmo-cache-dirname elmo-msgdb-dir)) Kbytes))
1591 (setq beginning total)
1592 (message "Checking disk usage...done")
1593 (let ((cfl (elmo-cache-get-sorted-cache-file-list))
1597 (while (and (<= size total)
1598 (setq oldest (elmo-cache-get-oldest-cache-file-entity cfl)))
1599 (setq cur-file (expand-file-name (car (cdr oldest)) (car oldest)))
1600 (setq cur-size (/ (elmo-disk-usage cur-file) Kbytes))
1601 (when (elmo-file-cache-delete cur-file)
1602 (setq count (+ count 1))
1603 (message "%d cache(s) are expired." count))
1604 (setq deleted (+ deleted cur-size))
1605 (setq total (- total cur-size)))
1606 (message "%d cache(s) are expired from disk (%d Kbytes/%d Kbytes)."
1607 count deleted beginning))))
1609 (defun elmo-cache-make-file-entity (filename path)
1610 (cons filename (elmo-get-last-accessed-time filename path)))
1612 (defun elmo-cache-get-oldest-cache-file-entity (cache-file-list)
1613 (let ((cfl cache-file-list)
1614 flist firsts oldest-entity wonlist)
1616 (setq flist (cdr (car cfl)))
1617 (setq firsts (append firsts (list
1618 (cons (car (car cfl))
1620 (setq cfl (cdr cfl)))
1623 (if (and (not oldest-entity)
1624 (cdr (cdr (car firsts))))
1625 (setq oldest-entity (car firsts)))
1626 (if (and (cdr (cdr (car firsts)))
1627 (cdr (cdr oldest-entity))
1628 (> (cdr (cdr oldest-entity)) (cdr (cdr (car firsts)))))
1629 (setq oldest-entity (car firsts)))
1630 (setq firsts (cdr firsts)))
1631 (setq wonlist (assoc (car oldest-entity) cache-file-list))
1633 (setcdr wonlist (delete (car (cdr wonlist)) (cdr wonlist))))
1636 (defun elmo-cache-get-sorted-cache-file-list ()
1637 (let ((dirs (directory-files
1638 (expand-file-name elmo-cache-dirname elmo-msgdb-dir)
1643 (setq num (length dirs))
1644 (message "Collecting cache info...")
1646 (setq elist (mapcar (lambda (x)
1647 (elmo-cache-make-file-entity x (car dirs)))
1648 (directory-files (car dirs) nil "^[^\\.]")))
1649 (setq ret-val (append ret-val
1657 (when (> num elmo-display-progress-threshold)
1659 (elmo-display-progress
1660 'elmo-cache-get-sorted-cache-file-list "Collecting cache info..."
1662 (setq dirs (cdr dirs)))
1663 (message "Collecting cache info...done")
1666 (defun elmo-cache-expire-by-age (&optional days)
1667 (let ((age (or (and days (int-to-string days))
1668 (and (interactive-p)
1669 (read-from-minibuffer
1670 (format "Enter days (%s): "
1671 elmo-cache-expire-default-age)))
1672 (int-to-string elmo-cache-expire-default-age)))
1673 (dirs (directory-files
1674 (expand-file-name elmo-cache-dirname elmo-msgdb-dir)
1678 (if (string= age "")
1679 (setq age elmo-cache-expire-default-age)
1680 (setq age (string-to-int age)))
1681 (setq curtime (current-time))
1682 (setq curtime (+ (* (nth 0 curtime)
1683 (float 65536)) (nth 1 curtime)))
1685 (let ((files (directory-files (car dirs) t "^[^\\.]"))
1686 (limit-age (* age 86400)))
1688 (when (> (- curtime (elmo-get-last-accessed-time (car files)))
1690 (when (elmo-file-cache-delete (car files))
1691 (setq count (+ 1 count))
1692 (message "%d cache file(s) are expired." count)))
1693 (setq files (cdr files))))
1694 (setq dirs (cdr dirs)))))
1698 (defun elmo-msgid-to-cache (msgid)
1700 (string-match "<\\(.+\\)>$" msgid))
1701 (elmo-replace-string-as-filename (elmo-match-string 1 msgid))))
1703 (defun elmo-cache-get-path (msgid &optional folder number)
1704 "Get path for cache file associated with MSGID, FOLDER, and NUMBER."
1705 (if (setq msgid (elmo-msgid-to-cache msgid))
1709 (format "%s/%s/%s@%s"
1710 (elmo-cache-get-path-subr msgid)
1713 (elmo-safe-filename folder))
1715 (elmo-cache-get-path-subr msgid)
1717 (expand-file-name elmo-cache-dirname
1723 (defconst elmo-warning-buffer-name "*elmo warning*")
1725 (defun elmo-warning (&rest args)
1726 "Display a warning, making warning message by passing all args to `insert'."
1727 (with-current-buffer (get-buffer-create elmo-warning-buffer-name)
1728 (goto-char (point-max))
1729 (apply 'insert (append args '("\n")))
1731 (display-buffer elmo-warning-buffer-name))
1733 (defvar elmo-obsolete-variable-alist nil)
1734 (defvar elmo-obsolete-variable-show-warnings nil)
1736 (defun elmo-define-obsolete-variable (obsolete var)
1737 "Define obsolete variable.
1738 OBSOLETE is a symbol for obsolete variable.
1739 VAR is a symbol for new variable.
1740 Definition is stored in `elmo-obsolete-variable-alist'."
1741 (let ((pair (assq var elmo-obsolete-variable-alist)))
1743 (setcdr pair obsolete)
1744 (setq elmo-obsolete-variable-alist
1745 (cons (cons var obsolete)
1746 elmo-obsolete-variable-alist)))))
1748 (defun elmo-resque-obsolete-variable (obsolete var)
1749 "Resque obsolete variable OBSOLETE as VAR.
1750 If `elmo-obsolete-variable-show-warnings' is non-nil, show warning message."
1751 (when (boundp obsolete)
1752 (static-if (and (fboundp 'defvaralias)
1753 (subrp (symbol-function 'defvaralias)))
1754 (defvaralias var obsolete)
1755 (set var (symbol-value obsolete)))
1756 (if elmo-obsolete-variable-show-warnings
1757 (elmo-warning (format "%s is obsolete. Use %s instead."
1758 (symbol-name obsolete)
1759 (symbol-name var))))))
1761 (defun elmo-resque-obsolete-variables (&optional alist)
1762 "Resque obsolete variables in ALIST.
1763 ALIST is a list of cons cell of
1764 \(OBSOLETE-VARIABLE-SYMBOL . NEW-VARIABLE-SYMBOL\).
1765 If ALIST is nil, `elmo-obsolete-variable-alist' is used."
1766 (dolist (pair elmo-obsolete-variable-alist)
1767 (elmo-resque-obsolete-variable (cdr pair)
1771 (defvar elmo-dop-queue-filename "queue"
1772 "*Disconnected operation queue is saved in this file.")
1774 (defun elmo-dop-queue-load ()
1775 (setq elmo-dop-queue
1777 (expand-file-name elmo-dop-queue-filename
1780 (defun elmo-dop-queue-save ()
1782 (expand-file-name elmo-dop-queue-filename
1787 (product-provide (provide 'elmo-util) (require 'elmo-version))
1789 ;;; elmo-util.el ends here