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 (defalias 'elmo-read 'read)
80 (defmacro elmo-set-work-buf (&rest body)
81 "Execute BODY on work buffer. Work buffer remains."
83 (set-buffer (get-buffer-create elmo-work-buf-name))
84 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
88 (defmacro elmo-bind-directory (dir &rest body)
89 "Set current directory DIR and execute BODY."
90 (` (let ((default-directory (file-name-as-directory (, dir))))
93 (defun elmo-object-load (filename &optional mime-charset no-err)
94 "Load OBJECT from the file specified by FILENAME.
95 File content is decoded with MIME-CHARSET."
96 (if (not (file-readable-p filename))
100 (insert-file-contents filename))
102 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
103 (decode-mime-charset-region (point-min) (point-max) mime-charset))
105 (read (current-buffer))
106 (error (unless no-err
107 (message "Warning: Loading object from %s failed."
109 (elmo-object-save filename nil))
112 (defsubst elmo-save-buffer (filename &optional mime-charset)
113 "Save current buffer to the file specified by FILENAME.
114 Directory of the file is created if it doesn't exist.
115 File content is encoded with MIME-CHARSET."
116 (let ((dir (directory-file-name (file-name-directory filename))))
117 (if (file-directory-p dir)
119 (unless (file-exists-p dir)
120 (elmo-make-directory dir)))
121 (if (file-writable-p filename)
124 ;;; (elmo-set-buffer-multibyte default-enable-multibyte-characters)
125 (encode-mime-charset-region (point-min) (point-max) mime-charset))
126 (as-binary-output-file
127 (write-region (point-min) (point-max) filename nil 'no-msg)))
128 (message (format "%s is not writable." filename)))))
130 (defun elmo-object-save (filename object &optional mime-charset)
131 "Save OBJECT to the file specified by FILENAME.
132 Directory of the file is created if it doesn't exist.
133 File content is encoded with MIME-CHARSET."
135 (prin1 object (current-buffer))
136 ;;;(princ "\n" (current-buffer))
137 (elmo-save-buffer filename mime-charset)))
141 (defconst elmo-condition-atom-regexp "[^/ \")|&]*")
143 (defun elmo-read-search-condition (default)
144 "Read search condition string interactively."
145 (elmo-read-search-condition-internal "Search by" default))
147 (defun elmo-read-search-condition-internal (prompt default)
148 (let* ((completion-ignore-case t)
149 (field (completing-read
150 (format "%s (%s): " prompt default)
154 "From" "Subject" "To" "Cc" "Body"
155 "Since" "Before" "ToCc"
156 "!From" "!Subject" "!To" "!Cc" "!Body"
157 "!Since" "!Before" "!ToCc")
158 elmo-msgdb-extra-fields))))
160 (setq field (if (string= field "")
164 ((or (string= field "AND") (string= field "OR"))
166 (elmo-read-search-condition-internal
167 (concat field "(1) Search by") default)
168 (if (string= field "AND") "&" "|")
169 (elmo-read-search-condition-internal
170 (concat field "(2) Search by") default)
172 ((string-match "Since\\|Before" field)
173 (concat (downcase field) ":"
174 (completing-read (format "Value for '%s': " field)
177 (list (format "%s" (car x)))))
178 elmo-date-descriptions))))
180 (setq value (read-from-minibuffer (format "Value for '%s': " field)))
181 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
183 (setq value (prin1-to-string value)))
184 (concat (downcase field) ":" value)))))
186 (defsubst elmo-condition-parse-error ()
187 (error "Syntax error in '%s'" (buffer-string)))
189 (defun elmo-parse-search-condition (condition)
191 Return value is a cons cell of (STRUCTURE . REST)"
194 (goto-char (point-min))
195 (cons (elmo-condition-parse) (buffer-substring (point) (point-max)))))
197 ;; condition ::= or-expr
198 (defun elmo-condition-parse ()
199 (or (elmo-condition-parse-or-expr)
200 (elmo-condition-parse-error)))
202 ;; or-expr ::= and-expr /
203 ;; and-expr "|" or-expr
204 (defun elmo-condition-parse-or-expr ()
205 (let ((left (elmo-condition-parse-and-expr)))
206 (if (looking-at "| *")
208 (goto-char (match-end 0))
209 (list 'or left (elmo-condition-parse-or-expr)))
212 ;; and-expr ::= primitive /
213 ;; primitive "&" and-expr
214 (defun elmo-condition-parse-and-expr ()
215 (let ((left (elmo-condition-parse-primitive)))
216 (if (looking-at "& *")
218 (goto-char (match-end 0))
219 (list 'and left (elmo-condition-parse-and-expr)))
222 ;; primitive ::= "(" expr ")" /
223 ;; ["!"] search-key SPACE* ":" SPACE* search-value
224 (defun elmo-condition-parse-primitive ()
227 (goto-char (match-end 0))
228 (prog1 (elmo-condition-parse)
229 (unless (looking-at ") *")
230 (elmo-condition-parse-error))
231 (goto-char (match-end 0))))
232 ;; search-key ::= [A-Za-z-]+
233 ;; ;; "since" / "before" / "last" / "first" /
234 ;; ;; "body" / field-name
235 ((looking-at "\\(!\\)? *\\([A-Za-z-]+\\) *: *")
236 (goto-char (match-end 0))
237 (let ((search-key (vector
238 (if (match-beginning 1) 'unmatch 'match)
239 (elmo-match-buffer 2)
240 (elmo-condition-parse-search-value))))
242 (if (string= (aref search-key 1) "tocc")
243 (if (eq (aref search-key 0) 'match)
245 (vector 'match "to" (aref search-key 2))
246 (vector 'match "cc" (aref search-key 2)))
248 (vector 'unmatch "to" (aref search-key 2))
249 (vector 'unmatch "cc" (aref search-key 2))))
252 ;; search-value ::= quoted / time / number / atom
253 ;; quoted ::= <elisp string expression>
254 ;; time ::= "yesterday" / "lastweek" / "lastmonth" / "lastyear" /
255 ;; number SPACE* "daysago" /
256 ;; number "-" month "-" number ; ex. 10-May-2000
258 ;; month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
259 ;; "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
260 ;; atom ::= ATOM_CHARS*
261 ;; SPACE ::= <ascii space character, 0x20>
262 ;; ATOM_CHARS ::= <any character except specials>
263 ;; specials ::= SPACE / <"> / </> / <)> / <|> / <&>
264 ;; ;; These characters should be quoted.
265 (defun elmo-condition-parse-search-value ()
268 (elmo-read (current-buffer)))
269 ((or (looking-at "yesterday") (looking-at "lastweek")
270 (looking-at "lastmonth") (looking-at "lastyear")
271 (looking-at "[0-9]+ *daysago")
272 (looking-at "[0-9]+-[A-Za-z]+-[0-9]+")
273 (looking-at "[0-9]+")
274 (looking-at elmo-condition-atom-regexp))
275 (prog1 (elmo-match-buffer 0)
276 (goto-char (match-end 0))))
277 (t (error "Syntax error '%s'" (buffer-string)))))
280 (defsubst elmo-buffer-replace (regexp &optional newtext)
281 (goto-char (point-min))
282 (while (re-search-forward regexp nil t)
283 (replace-match (or newtext ""))))
285 (defsubst elmo-delete-char (char string &optional unibyte)
288 (let ((coding-system-for-read 'no-conversion)
289 (coding-system-for-write 'no-conversion))
290 (if unibyte (elmo-set-buffer-multibyte nil))
292 (goto-char (point-min))
293 (while (search-forward (char-to-string char) nil t)
297 (defsubst elmo-delete-cr-buffer ()
298 "Delete CR from buffer."
300 (goto-char (point-min))
301 (while (search-forward "\r\n" nil t)
302 (replace-match "\n")) ))
304 (defsubst elmo-delete-cr-get-content-type ()
306 (goto-char (point-min))
307 (while (search-forward "\r\n" nil t)
308 (replace-match "\n"))
309 (goto-char (point-min))
310 (or (std11-field-body "content-type")
313 (defun elmo-delete-cr (string)
317 (goto-char (point-min))
318 (while (search-forward "\r\n" nil t)
319 (replace-match "\n"))
322 (defun elmo-uniq-list (lst)
323 "Distractively uniqfy elements of LST."
332 (defun elmo-list-insert (list element after)
333 "Insert an ELEMENT to the LIST, just after AFTER."
338 (if (eq (car li) after)
339 (setq p li pn curn li nil)
343 (setcdr (nthcdr pn list) (cons element (cdr p)))
344 (nconc list (list element)))))
346 (defun elmo-string-partial-p (string)
347 (and (stringp string) (string-match "message/partial" string)))
349 (defun elmo-get-file-string (filename &optional remove-final-newline)
351 (let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
352 insert-file-contents-post-hook)
353 (when (file-exists-p filename)
355 (as-binary-input-file (insert-file-contents filename)))
356 (when (and remove-final-newline
358 (= (char-after (1- (point-max))) ?\n))
359 (goto-char (point-max))
360 (delete-backward-char 1))
363 (defun elmo-save-string (string filename)
366 (as-binary-output-file
368 (write-region (point-min) (point-max)
369 filename nil 'no-msg))
372 (defun elmo-max-of-list (nlist)
376 (if (< max-num (car l))
377 (setq max-num (car l)))
381 (defun elmo-concat-path (path filename)
382 (if (not (string= path ""))
383 (if (string= elmo-path-sep (substring path (- (length path) 1)))
384 (concat path filename)
385 (concat path elmo-path-sep filename))
388 (defvar elmo-passwd-alist nil)
390 (defun elmo-passwd-alist-load ()
392 (let ((filename (expand-file-name elmo-passwd-alist-file-name
393 elmo-msgdb-directory))
394 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*"))
395 insert-file-contents-pre-hook ; To avoid autoconv-xmas...
396 insert-file-contents-post-hook
398 (if (not (file-readable-p filename))
400 (set-buffer tmp-buffer)
401 (insert-file-contents filename)
404 (read (current-buffer))
406 (kill-buffer tmp-buffer)
409 (defun elmo-passwd-alist-clear ()
410 "Clear password cache."
412 (setq elmo-passwd-alist nil))
414 (defun elmo-passwd-alist-save ()
415 "Save password into file."
418 (let ((filename (expand-file-name elmo-passwd-alist-file-name
419 elmo-msgdb-directory))
420 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*")))
421 (set-buffer tmp-buffer)
423 (prin1 elmo-passwd-alist tmp-buffer)
424 (princ "\n" tmp-buffer)
425 ;;; (if (and (file-exists-p filename)
426 ;;; (not (equal 384 (file-modes filename))))
427 ;;; (error "%s is not safe.chmod 600 %s!" filename filename))
428 (if (file-writable-p filename)
430 (write-region (point-min) (point-max)
431 filename nil 'no-msg)
432 (set-file-modes filename 384))
433 (message (format "%s is not writable." filename)))
434 (kill-buffer tmp-buffer))))
436 (defun elmo-get-passwd (key)
437 "Get password from password pool."
439 (if (not elmo-passwd-alist)
440 (setq elmo-passwd-alist (elmo-passwd-alist-load)))
441 (setq pair (assoc key elmo-passwd-alist))
443 (elmo-base64-decode-string (cdr pair))
444 (setq pass (elmo-read-passwd (format "Password for %s: "
446 (setq elmo-passwd-alist
447 (append elmo-passwd-alist
449 (elmo-base64-encode-string pass)))))
450 (if elmo-passwd-life-time
451 (run-with-timer elmo-passwd-life-time nil
452 (` (lambda () (elmo-remove-passwd (, key))))))
455 (defun elmo-remove-passwd (key)
456 "Remove password from password pool (for failure)."
458 (if (setq pass-cons (assoc key elmo-passwd-alist))
461 (fillarray (cdr pass-cons) 0))
462 (setq elmo-passwd-alist
463 (delete pass-cons elmo-passwd-alist))))))
465 (defmacro elmo-read-char-exclusive ()
466 (cond ((featurep 'xemacs)
467 '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
472 (key-press-event-p (setq event (next-command-event)))
473 (setq key (or (event-to-character event)
474 (cdr (assq (event-key event) table)))))))
476 ((fboundp 'read-char-exclusive)
477 '(read-char-exclusive))
481 (defun elmo-read-passwd (prompt &optional stars)
482 "Read a single line of text from user without echoing, and return it."
486 (cursor-in-echo-area t)
487 (log-message-max-size 0)
488 message-log-max done msg truncate)
490 (if (or (not stars) (string= "" ans))
492 (setq msg (concat prompt (make-string (length ans) ?.)))
494 (1+ (- (length msg) (window-width (minibuffer-window)))))
496 (setq msg (concat "$" (substring msg (1+ truncate))))))
498 (setq c (elmo-read-char-exclusive))
502 ((or (= c ?\r) (= c ?\n) (= c ?\e))
506 ((and (/= c ?\b) (/= c ?\177))
507 (setq ans (concat ans (char-to-string c))))
509 (setq ans (substring ans 0 -1)))))
518 (defun elmo-string-to-list (string)
521 (goto-char (point-min))
523 (goto-char (point-max))
525 (goto-char (point-min))
526 (read (current-buffer))))
528 (defun elmo-list-to-string (list)
537 (if (symbolp (car tlist))
538 (symbol-name (car tlist))
543 (setq tlist (cdr tlist)))
553 (defun elmo-plug-on-by-servers (alist &optional servers)
554 (let ((server-list (or servers elmo-plug-on-servers)))
557 (if (elmo-plugged-p (car server-list))
559 (setq server-list (cdr server-list))))))
561 (defun elmo-plug-on-by-exclude-servers (alist &optional servers)
562 (let ((server-list (or servers elmo-plug-on-exclude-servers))
563 server other-servers)
565 (when (and (not (member (setq server (caaar alist)) server-list))
566 (not (member server other-servers)))
567 (push server other-servers))
568 (setq alist (cdr alist)))
569 (elmo-plug-on-by-servers alist other-servers)))
571 (defun elmo-plugged-p (&optional server port stream-type alist label-exp)
572 (let ((alist (or alist elmo-plugged-alist))
574 (cond ((and (not port) (not server))
575 (cond ((eq elmo-plugged-condition 'one)
579 (if (nth 2 (car alist))
581 (setq alist (cdr alist))))
583 ((eq elmo-plugged-condition 'all)
587 (if (not (nth 2 (car alist)))
588 (throw 'plugged nil))
589 (setq alist (cdr alist)))
592 ((functionp elmo-plugged-condition)
593 (funcall elmo-plugged-condition alist))
596 ((not port) ;; server
599 (when (string= server (caaar alist))
600 (if (nth 2 (car alist))
602 (setq alist (cdr alist)))))
604 (setq plugged-info (assoc (list server port stream-type) alist))
605 (if (not plugged-info)
606 ;; add elmo-plugged-alist automatically
608 (elmo-set-plugged elmo-plugged server port stream-type
609 nil nil nil label-exp)
611 (if (and elmo-auto-change-plugged
612 (> elmo-auto-change-plugged 0)
613 (nth 3 plugged-info) ;; time
614 (elmo-time-expire (nth 3 plugged-info)
615 elmo-auto-change-plugged))
617 (nth 2 plugged-info)))))))
619 (defun elmo-set-plugged (plugged &optional server port stream-type time
621 (let ((alist (or alist elmo-plugged-alist))
623 (cond ((and (not port) (not server))
624 (setq elmo-plugged plugged)
625 ;; set plugged all element of elmo-plugged-alist.
627 (setcdr (cdar alist) (list plugged time))
628 (setq alist (cdr alist))))
630 ;; set plugged all port of server
632 (when (string= server (caaar alist))
633 (setcdr (cdar alist) (list plugged time)))
634 (setq alist (cdr alist))))
636 ;; set plugged one port of server
637 (setq plugged-info (assoc (list server port stream-type) alist))
638 (setq label (if label-exp
640 (nth 1 plugged-info)))
642 ;; if add is non-nil, don't reset plug state.
644 (setcdr plugged-info (list label plugged time)))
646 (setq elmo-plugged-alist
650 (list (list server port stream-type)
651 label plugged time))))))))
654 (defun elmo-delete-plugged (&optional server port alist)
655 (let* ((alist (or alist elmo-plugged-alist))
657 (cond ((and (not port) (not server))
660 ;; delete plugged all port of server
662 (when (string= server (caaar alist2))
663 (setq alist (delete (car alist2) alist)))
664 (setq alist2 (cdr alist2))))
666 ;; delete plugged one port of server
668 (delete (assoc (cons server port) alist) alist))))
671 (defun elmo-disk-usage (path)
672 "Get disk usage (bytes) in PATH."
674 (condition-case () (file-attributes path) (error nil))))
676 (if (nth 0 file-attr) ; directory
677 (let ((files (condition-case ()
678 (directory-files path t "^[^\\.]")
681 ;; (result (nth 7 file-attr))) ... directory size
683 (setq result (+ result (or (elmo-disk-usage (car files)) 0)))
684 (setq files (cdr files)))
686 (float (nth 7 file-attr))))))
688 (defun elmo-get-last-accessed-time (path &optional dir)
689 "Return the last accessed time of PATH."
690 (let ((last-accessed (nth 4 (file-attributes (or (and dir
695 (setq last-accessed (+ (* (nth 0 last-accessed)
696 (float 65536)) (nth 1 last-accessed)))
699 (defun elmo-get-last-modification-time (path &optional dir)
700 "Return the last accessed time of PATH."
701 (let ((last-modified (nth 5 (file-attributes (or (and dir
705 (setq last-modified (+ (* (nth 0 last-modified)
706 (float 65536)) (nth 1 last-modified)))))
708 (defun elmo-make-directory (path)
709 "Create directory recursively."
710 (let ((parent (directory-file-name (file-name-directory path))))
711 (if (null (file-directory-p parent))
712 (elmo-make-directory parent))
713 (make-directory path)
714 (if (string= path (expand-file-name elmo-msgdb-directory))
715 (set-file-modes path (+ (* 64 7) (* 8 0) 0))))) ; chmod 0700
717 (defun elmo-delete-directory (path &optional no-hierarchy)
718 "Delete directory recursively."
719 (if (stringp path) ; nil is not permitted.
720 (let ((dirent (directory-files path))
721 relpath abspath hierarchy)
723 (setq relpath (car dirent)
725 abspath (expand-file-name relpath path))
726 (when (not (string-match "^\\.\\.?$" relpath))
727 (if (eq (nth 0 (file-attributes abspath)) t)
730 (elmo-delete-directory abspath no-hierarchy))
731 (delete-file abspath))))
733 (delete-directory path)))))
735 (defun elmo-list-filter (l1 l2)
738 ;; t means filter all.
741 (elmo-delete-if (lambda (x) (not (memq x l1))) l2)
745 (defsubst elmo-list-delete-if-smaller (list number)
746 (let ((ret-val (copy-sequence list)))
748 (if (< (car list) number)
749 (setq ret-val (delq (car list) ret-val)))
750 (setq list (cdr list)))
753 (defun elmo-list-diff (list1 list2 &optional mes)
756 (let ((clist1 (copy-sequence list1))
757 (clist2 (copy-sequence list2)))
759 (setq clist1 (delq (car list2) clist1))
760 (setq list2 (cdr list2)))
762 (setq clist2 (delq (car list1) clist2))
763 (setq list1 (cdr list1)))
765 (message (concat mes "done.")))
766 (list clist1 clist2)))
768 (defun elmo-list-bigger-diff (list1 list2 &optional mes)
769 "Returns a list (- +). + is bigger than max of LIST1, in LIST2."
774 (max-of-l2 (or (nth (max 0 (1- (length l2))) l2) 0))
778 (setq num (+ (length l1)))
780 (if (memq (car l1) l2)
781 (if (eq (car l1) (car l2))
784 (if (> (car l1) max-of-l2)
785 (setq diff1 (nconc diff1 (list (car l1))))))
789 (setq percent (/ (* i 100) num))
790 (if (eq (% percent 5) 0)
791 (elmo-display-progress
792 'elmo-list-bigger-diff "%s%d%%" percent mes))))
794 (cons diff1 (list l2)))))
796 (defmacro elmo-filter-type (filter)
797 (` (aref (, filter) 0)))
799 (defmacro elmo-filter-key (filter)
800 (` (aref (, filter) 1)))
802 (defmacro elmo-filter-value (filter)
803 (` (aref (, filter) 2)))
805 (defsubst elmo-buffer-field-primitive-condition-match (condition
809 (goto-char (point-min))
811 ((string= (elmo-filter-key condition) "last")
812 (setq result (<= (length (memq number number-list))
813 (string-to-int (elmo-filter-value condition)))))
814 ((string= (elmo-filter-key condition) "first")
815 (setq result (< (- (length number-list)
816 (length (memq number number-list)))
817 (string-to-int (elmo-filter-value condition)))))
818 ((string= (elmo-filter-key condition) "since")
819 (let* ((date (elmo-date-get-datevec (elmo-filter-value condition)))
820 (field-date (timezone-make-date-sortable
822 (std11-field-body "date")
823 (current-time-zone) nil)))
824 (specified-date (timezone-make-sortable-date
828 (timezone-make-time-string
833 (or (string= field-date specified-date)
834 (string< specified-date field-date)))))
835 ((string= (elmo-filter-key condition) "before")
836 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
839 (timezone-make-date-sortable
841 (std11-field-body "date")
842 (current-time-zone) nil))
843 (timezone-make-sortable-date (aref date 0)
846 (timezone-make-time-string
850 ((string= (elmo-filter-key condition) "body")
851 (and (re-search-forward "^$" nil t) ; goto body
852 (setq result (search-forward (elmo-filter-value condition)
855 (let ((fval (std11-field-body (elmo-filter-key condition))))
856 (if (eq (length fval) 0) (setq fval nil))
857 (if fval (setq fval (eword-decode-string fval)))
858 (setq result (and fval (string-match
859 (elmo-filter-value condition) fval))))))
860 (if (eq (elmo-filter-type condition) 'unmatch)
861 (setq result (not result)))
864 (defun elmo-condition-in-msgdb-p-internal (condition fields)
867 (if (not (member (elmo-filter-key condition) fields))
869 ((or (eq (car condition) 'and)
870 (eq (car condition) 'or))
871 (elmo-condition-in-msgdb-p-internal (nth 1 condition) fields)
872 (elmo-condition-in-msgdb-p-internal (nth 2 condition) fields))))
874 (defun elmo-condition-in-msgdb-p (condition)
876 (elmo-condition-in-msgdb-p-internal condition
878 elmo-msgdb-extra-fields
879 '("last" "first" "from"
880 "subject" "to" "cc" "since"
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 (if (eq (elmo-filter-type condition) 'unmatch)
910 (setq result (not result))))
911 ((string= (elmo-filter-key condition) "first")
912 (setq result (< (- (length number-list)
913 (length (memq number number-list)))
914 (string-to-int (elmo-filter-value condition))))
915 (if (eq (elmo-filter-type condition) 'unmatch)
916 (setq result (not result))))
919 (as-binary-input-file (insert-file-contents file))
920 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
921 ;; Should consider charset?
922 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
924 (elmo-buffer-field-primitive-condition-match
925 condition number number-list)))))
928 (defun elmo-file-field-condition-match (file condition number number-list)
931 (elmo-file-field-primitive-condition-match
932 file condition number number-list))
933 ((eq (car condition) 'and)
934 (and (elmo-file-field-condition-match
935 file (nth 1 condition) number number-list)
936 (elmo-file-field-condition-match
937 file (nth 2 condition) number number-list)))
938 ((eq (car condition) 'or)
939 (or (elmo-file-field-condition-match
940 file (nth 1 condition) number number-list)
941 (elmo-file-field-condition-match
942 file (nth 2 condition) number number-list)))))
944 (defmacro elmo-get-hash-val (string hashtable)
945 (let ((sym (list 'intern-soft string hashtable)))
946 (list 'if (list 'boundp sym)
947 (list 'symbol-value sym))))
949 (defmacro elmo-set-hash-val (string value hashtable)
950 (list 'set (list 'intern string hashtable) value))
952 (defmacro elmo-clear-hash-val (string hashtable)
953 (static-if (fboundp 'unintern)
954 (list 'unintern string hashtable)
955 (list 'makunbound (list 'intern string hashtable))))
957 (defmacro elmo-unintern (string)
958 "`unintern' symbol named STRING, When can use `unintern'.
959 Emacs 19.28 or earlier does not have `unintern'."
960 (static-if (fboundp 'unintern)
961 (list 'unintern string)))
963 (defun elmo-make-hash (&optional hashsize)
964 "Make a new hash table which have HASHSIZE size."
968 ;; Prime numbers as lengths tend to result in good
969 ;; hashing; lengths one less than a power of two are
973 (while (< (- i 1) hashsize)
976 elmo-hash-maximum-size)
977 elmo-hash-minimum-size)
978 elmo-hash-minimum-size)
981 (defsubst elmo-mime-string (string)
982 "Normalize MIME encoded STRING."
986 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
987 (setq str (eword-decode-string
988 (decode-mime-charset-string string elmo-mime-charset)))
989 (setq str (encode-mime-charset-string str elmo-mime-charset))
990 (elmo-set-buffer-multibyte nil)
993 (defsubst elmo-collect-field (beg end downcase-field-name)
996 (narrow-to-region beg end)
997 (goto-char (point-min))
998 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1000 (while (re-search-forward regexp nil t)
1001 (setq name (buffer-substring-no-properties
1002 (match-beginning 1)(1- (match-end 1))))
1003 (if downcase-field-name
1004 (setq name (downcase name)))
1005 (setq body (buffer-substring-no-properties
1006 (match-end 0) (std11-field-end)))
1007 (or (assoc name dest)
1008 (setq dest (cons (cons name body) dest))))
1011 (defsubst elmo-collect-field-from-string (string downcase-field-name)
1014 (goto-char (point-min))
1015 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1017 (while (re-search-forward regexp nil t)
1018 (setq name (buffer-substring-no-properties
1019 (match-beginning 1)(1- (match-end 1))))
1020 (if downcase-field-name
1021 (setq name (downcase name)))
1022 (setq body (buffer-substring-no-properties
1023 (match-end 0) (std11-field-end)))
1024 (or (assoc name dest)
1025 (setq dest (cons (cons name body) dest))))
1028 (defun elmo-safe-filename (folder)
1029 (elmo-replace-in-string
1030 (elmo-replace-in-string
1031 (elmo-replace-in-string folder "/" " ")
1035 (defvar elmo-filename-replace-chars nil)
1037 (defsubst elmo-replace-string-as-filename (msgid)
1038 "Replace string as filename."
1039 (setq msgid (elmo-replace-in-string msgid " " " "))
1040 (if (null elmo-filename-replace-chars)
1041 (setq elmo-filename-replace-chars
1042 (regexp-quote (mapconcat
1043 'car elmo-filename-replace-string-alist ""))))
1044 (while (string-match (concat "[" elmo-filename-replace-chars "]")
1047 (substring msgid 0 (match-beginning 0))
1050 (match-beginning 0) (match-end 0))
1051 elmo-filename-replace-string-alist))
1052 (substring msgid (match-end 0)))))
1055 (defsubst elmo-recover-string-from-filename (filename)
1056 "Recover string from FILENAME."
1058 (while (string-match " " filename)
1059 (setq tmp (substring filename
1061 (+ (match-end 0) 1)))
1062 (if (string= tmp " ")
1064 (setq tmp (car (rassoc tmp
1065 elmo-filename-replace-string-alist))))
1068 (substring filename 0 (match-beginning 0))
1070 (setq filename (substring filename (+ (match-end 0) 1))))
1071 (concat result filename)))
1073 (defsubst elmo-copy-file (src dst &optional ok-if-already-exists)
1075 (elmo-add-name-to-file src dst ok-if-already-exists)
1076 (error (copy-file src dst ok-if-already-exists t))))
1078 (defsubst elmo-buffer-exists-p (buffer)
1079 (if (bufferp buffer)
1080 (buffer-live-p buffer)
1081 (get-buffer buffer)))
1083 (defsubst elmo-kill-buffer (buffer)
1084 (when (elmo-buffer-exists-p buffer)
1085 (kill-buffer buffer)))
1087 (defun elmo-delete-if (pred lst)
1088 "Return new list contain items which don't satisfy PRED in LST."
1091 (unless (funcall pred (car lst))
1092 (setq result (nconc result (list (car lst)))))
1093 (setq lst (cdr lst)))
1096 (defun elmo-list-delete (list1 list2)
1097 "Delete by side effect any occurrences equal to elements of LIST1 from LIST2.
1098 Return the modified LIST2. Deletion is done with `delete'.
1099 Write `(setq foo (elmo-list-delete bar foo))' to be sure of changing
1100 the value of `foo'."
1102 (setq list2 (delete (car list1) list2))
1103 (setq list1 (cdr list1)))
1106 (defun elmo-list-member (list1 list2)
1107 "If any element of LIST1 is member of LIST2, return t."
1110 (if (member (car list1) list2)
1112 (setq list1 (cdr list1)))))
1114 (defun elmo-count-matches (regexp beg end)
1118 (while (re-search-forward regexp end t)
1119 (setq count (1+ count)))
1122 (if (fboundp 'display-error)
1123 (defalias 'elmo-display-error 'display-error)
1124 (defun elmo-display-error (error-object stream)
1125 "A tiny function to display ERROR-OBJECT to the STREAM."
1127 (errobj error-object)
1130 (setq err-mes (concat err-mes (format
1131 (if (stringp (car errobj))
1135 (setq errobj (cdr errobj))
1136 (if errobj (setq err-mes (concat err-mes (if first ": " ", "))))
1138 (princ err-mes stream))))
1140 (if (fboundp 'define-error)
1141 (defalias 'elmo-define-error 'define-error)
1142 (defun elmo-define-error (error doc &optional parents)
1144 (setq parents 'error))
1145 (let ((conds (get parents 'error-conditions)))
1147 (error "Not an error symbol: %s" error))
1149 (list 'error-message doc
1150 'error-conditions (cons error conds))))))
1152 (cond ((fboundp 'progress-feedback-with-label)
1153 (defalias 'elmo-display-progress 'progress-feedback-with-label))
1154 ((fboundp 'lprogress-display)
1155 (defalias 'elmo-display-progress 'lprogress-display))
1157 (defun elmo-display-progress (label format &optional value &rest args)
1158 "Print a progress message."
1159 (if (and (null format) (null args))
1161 (apply (function message) (concat format " %d%%")
1162 (nconc args (list value)))))))
1164 (defvar elmo-progress-counter-alist nil)
1166 (defmacro elmo-progress-counter-value (counter)
1167 (` (aref (cdr (, counter)) 0)))
1169 (defmacro elmo-progress-counter-all-value (counter)
1170 (` (aref (cdr (, counter)) 1)))
1172 (defmacro elmo-progress-counter-format (counter)
1173 (` (aref (cdr (, counter)) 2)))
1175 (defmacro elmo-progress-counter-set-value (counter value)
1176 (` (aset (cdr (, counter)) 0 (, value))))
1178 (defun elmo-progress-set (label all-value &optional format)
1179 (unless (assq label elmo-progress-counter-alist)
1180 (setq elmo-progress-counter-alist
1181 (cons (cons label (vector 0 all-value (or format "")))
1182 elmo-progress-counter-alist))))
1184 (defun elmo-progress-clear (label)
1185 (let ((counter (assq label elmo-progress-counter-alist)))
1187 (elmo-display-progress label "" 100)
1188 (setq elmo-progress-counter-alist
1189 (delq counter elmo-progress-counter-alist)))))
1191 (defun elmo-progress-notify (label &optional value op &rest args)
1192 (let ((counter (assq label elmo-progress-counter-alist)))
1194 (let* ((value (or value 1))
1195 (cur-value (elmo-progress-counter-value counter))
1196 (all-value (elmo-progress-counter-all-value counter))
1197 (new-value (if (eq op 'set) value (+ cur-value value)))
1198 (cur-rate (/ (* cur-value 100) all-value))
1199 (new-rate (/ (* new-value 100) all-value)))
1200 (elmo-progress-counter-set-value counter new-value)
1201 (unless (= cur-rate new-rate)
1202 (apply 'elmo-display-progress
1204 (elmo-progress-counter-format counter)
1207 (when (>= new-rate 100)
1208 (elmo-progress-clear label))))))
1210 (defun elmo-time-expire (before-time diff-time)
1211 (let* ((current (current-time))
1212 (rest (when (< (nth 1 current) (nth 1 before-time))
1216 (list (- (+ (car current) (if rest -1 0)) (car before-time))
1217 (- (+ (or rest 0) (nth 1 current)) (nth 1 before-time))))
1218 (and (eq (car diff) 0)
1219 (< diff-time (nth 1 diff)))))
1221 (if (fboundp 'std11-fetch-field)
1222 (defalias 'elmo-field-body 'std11-fetch-field) ;;no narrow-to-region
1223 (defalias 'elmo-field-body 'std11-field-body))
1225 (defun elmo-address-quote-specials (word)
1226 "Make quoted string of WORD if needed."
1227 (let ((lal (std11-lexical-analyze word)))
1228 (if (or (assq 'specials lal)
1229 (assq 'domain-literal lal))
1230 (prin1-to-string word)
1233 (defmacro elmo-string (string)
1234 "STRING without text property."
1235 (` (let ((obj (copy-sequence (, string))))
1236 (set-text-properties 0 (length obj) nil obj)
1239 (defun elmo-flatten (list-of-list)
1240 "Flatten LIST-OF-LIST."
1241 (unless (null list-of-list)
1242 (append (if (and (car list-of-list)
1243 (listp (car list-of-list)))
1245 (list (car list-of-list)))
1246 (elmo-flatten (cdr list-of-list)))))
1248 (defun elmo-y-or-n-p (prompt &optional auto default)
1249 "Same as `y-or-n-p'.
1250 But if optional argument AUTO is non-nil, DEFAULT is returned."
1255 (defun elmo-string-member (string slist)
1256 "Return t if STRING is a member of the SLIST."
1259 (if (and (stringp (car slist))
1260 (string= string (car slist)))
1262 (setq slist (cdr slist)))))
1264 (defun elmo-string-match-member (str list &optional case-ignore)
1265 (let ((case-fold-search case-ignore))
1268 (if (string-match (car list) str)
1269 (throw 'member (car list)))
1270 (setq list (cdr list))))))
1272 (defun elmo-string-matched-member (str list &optional case-ignore)
1273 (let ((case-fold-search case-ignore))
1276 (if (string-match str (car list))
1277 (throw 'member (car list)))
1278 (setq list (cdr list))))))
1280 (defsubst elmo-string-delete-match (string pos)
1281 (concat (substring string
1282 0 (match-beginning pos))
1287 (defun elmo-string-match-assoc (key alist &optional case-ignore)
1288 (let ((case-fold-search case-ignore)
1292 (setq a (car alist))
1295 (string-match key (car a)))
1297 (setq alist (cdr alist))))))
1299 (defun elmo-string-matched-assoc (key alist &optional case-ignore)
1300 (let ((case-fold-search case-ignore)
1304 (setq a (car alist))
1307 (string-match (car a) key))
1309 (setq alist (cdr alist))))))
1311 (defun elmo-string-assoc (key alist)
1315 (setq a (car alist))
1318 (string= key (car a)))
1320 (setq alist (cdr alist))))))
1322 (defun elmo-string-assoc-all (key alist)
1325 (if (string= key (car (car alist)))
1329 (setq alist (cdr alist)))
1332 (defun elmo-string-rassoc (key alist)
1336 (setq a (car alist))
1339 (string= key (cdr a)))
1341 (setq alist (cdr alist))))))
1343 (defun elmo-string-rassoc-all (key alist)
1346 (if (string= key (cdr (car alist)))
1350 (setq alist (cdr alist)))
1353 ;;; Folder parser utils.
1354 (defun elmo-parse-token (string &optional seps)
1355 "Parse atom from STRING using SEPS as a string of separator char list."
1356 (let ((len (length string))
1357 (seps (and seps (string-to-char-list seps)))
1363 (while (and (< i len) (or in (null sep)))
1364 (setq c (aref string i))
1366 ((and in (eq c ?\\))
1368 content (cons (aref string i) content)
1373 (in (setq content (cons c content)
1377 (t (setq content (cons c content)
1379 (if in (error "Parse error in quoted"))
1380 (cons (if (null content) "" (char-list-to-string (nreverse content)))
1381 (substring string i)))))
1383 (defun elmo-parse-prefixed-element (prefix string &optional seps)
1384 (if (and (not (eq (length string) 0))
1385 (eq (aref string 0) prefix))
1386 (elmo-parse-token (substring string 1) seps)
1389 ;;; Number set defined by OKAZAKI Tetsurou <okazaki@be.to>
1391 ;; number ::= [0-9]+
1394 ;; number-range ::= "(" beg " . " end ")" ;; cons cell
1395 ;; number-set-elem ::= number / number-range
1396 ;; number-set ::= "(" *number-set-elem ")" ;; list
1398 (defun elmo-number-set-member (number number-set)
1399 "Return non-nil if NUMBER is an element of NUMBER-SET.
1400 The value is actually the tail of NUMBER-RANGE whose car contains NUMBER."
1401 (or (memq number number-set)
1403 (while (and number-set (not found))
1404 (if (and (consp (car number-set))
1405 (and (<= (car (car number-set)) number)
1406 (<= number (cdr (car number-set)))))
1408 (setq number-set (cdr number-set))))
1411 (defun elmo-number-set-append-list (number-set list)
1412 "Append LIST of numbers to the NUMBER-SET.
1413 NUMBER-SET is altered."
1414 (let ((appended number-set))
1416 (setq appended (elmo-number-set-append appended (car list)))
1417 (setq list (cdr list)))
1420 (defun elmo-number-set-append (number-set number)
1421 "Append NUMBER to the NUMBER-SET.
1422 NUMBER-SET is altered."
1423 (let ((number-set-1 number-set)
1425 (while (and number-set (not found))
1426 (setq elem (car number-set))
1429 (eq (+ 1 (cdr elem)) number))
1430 (setcdr elem number)
1432 ((and (integerp elem)
1433 (eq (+ 1 elem) number))
1434 (setcar number-set (cons elem number))
1436 ((or (and (integerp elem) (eq elem number))
1438 (<= (car elem) number)
1439 (<= number (cdr elem))))
1441 (setq number-set (cdr number-set)))
1443 (setq number-set-1 (nconc number-set-1 (list number))))
1446 (defun elmo-number-set-to-number-list (number-set)
1447 "Return a number list which corresponds to NUMBER-SET."
1448 (let (number-list elem i)
1450 (setq elem (car number-set))
1454 (while (<= i (cdr elem))
1455 (setq number-list (cons i number-list))
1458 (setq number-list (cons elem number-list))))
1459 (setq number-set (cdr number-set)))
1460 (nreverse number-list)))
1462 (defcustom elmo-list-subdirectories-ignore-regexp "^\\(\\.\\.?\\|[0-9]+\\)$"
1463 "*Regexp to filter subfolders."
1467 (defun elmo-list-subdirectories-1 (basedir curdir one-level)
1468 (let ((root (zerop (length curdir)))
1469 (w32-get-true-file-link-count t) ; for Meadow
1472 (dolist (file (directory-files (setq dir (expand-file-name curdir basedir))))
1473 (when (and (not (string-match
1474 elmo-list-subdirectories-ignore-regexp
1476 (car (setq attr (file-attributes
1477 (expand-file-name file dir)))))
1478 (when (eq one-level 'check) (throw 'done t))
1480 (concat curdir (and (not root) elmo-path-sep) file))
1482 (setq dirs (nconc dirs
1483 (if (if elmo-have-link-count (< 2 (nth 1 attr))
1485 (elmo-list-subdirectories-1
1488 (if one-level 'check))))
1490 (list (list relpath))
1493 (elmo-list-subdirectories-1
1497 (list relpath)))))))
1500 (defun elmo-list-subdirectories (directory file one-level)
1501 (let ((subdirs (elmo-list-subdirectories-1 directory file one-level)))
1502 (if (zerop (length file))
1504 (cons file subdirs))))
1506 (defun elmo-mapcar-list-of-list (func list-of-list)
1509 (cond ((listp x) (elmo-mapcar-list-of-list func x))
1510 (t (funcall func x))))
1513 (defun elmo-parse (string regexp &optional matchn)
1514 (or matchn (setq matchn 1))
1516 (store-match-data nil)
1517 (while (string-match regexp string (match-end 0))
1518 (setq list (cons (substring string (match-beginning matchn)
1519 (match-end matchn)) list)))
1523 (defmacro elmo-make-file-cache (path status)
1524 "PATH is the cache file name.
1525 STATUS is one of 'section, 'entire or nil.
1526 nil means no cache exists.
1527 'section means partial section cache exists.
1528 'entire means entire cache exists.
1529 If the cache is partial file-cache, TYPE is 'partial."
1530 (` (cons (, path) (, status))))
1532 (defmacro elmo-file-cache-path (file-cache)
1533 "Returns the file path of the FILE-CACHE."
1534 (` (car (, file-cache))))
1536 (defmacro elmo-file-cache-status (file-cache)
1537 "Returns the status of the FILE-CACHE."
1538 (` (cdr (, file-cache))))
1540 (defsubst elmo-cache-to-msgid (filename)
1541 (concat "<" (elmo-recover-string-from-filename filename) ">"))
1543 (defsubst elmo-cache-get-path-subr (msgid)
1544 (let ((chars '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?A ?B ?C ?D ?E ?F))
1545 (clist (string-to-char-list msgid))
1548 (setq sum (+ sum (car clist)))
1549 (setq clist (cdr clist)))
1551 (nth (% (/ sum 16) 2) chars)
1552 (nth (% sum 16) chars))))
1554 (defun elmo-file-cache-get-path (msgid &optional section)
1555 "Get cache path for MSGID.
1556 If optional argument SECTION is specified, partial cache path is returned."
1557 (if (setq msgid (elmo-msgid-to-cache msgid))
1560 (format "%s/%s/%s/%s"
1561 elmo-cache-directory
1562 (elmo-cache-get-path-subr msgid)
1566 elmo-cache-directory
1567 (elmo-cache-get-path-subr msgid)
1570 (defmacro elmo-file-cache-expand-path (path section)
1571 "Return file name for the file-cache corresponds to the section.
1572 PATH is the file-cache path.
1573 SECTION is the section string."
1574 (` (expand-file-name (or (, section) "") (, path))))
1576 (defun elmo-file-cache-delete (path)
1577 "Delete a cache on PATH."
1579 (when (file-exists-p path)
1580 (if (file-directory-p path)
1582 (setq files (directory-files path t "^[^\\.]"))
1584 (delete-file (car files))
1585 (setq files (cdr files)))
1586 (delete-directory path))
1587 (delete-file path)))))
1589 (defun elmo-file-cache-exists-p (msgid)
1590 "Returns 'section or 'entire if a cache which corresponds to MSGID exists."
1591 (elmo-file-cache-status (elmo-file-cache-get msgid)))
1593 (defun elmo-file-cache-save (cache-path section)
1594 "Save current buffer as cache on PATH.
1595 Return t if cache is saved successfully."
1597 (let ((path (if section (expand-file-name section cache-path)
1600 (if (and (null section)
1601 (file-directory-p path))
1603 (setq files (directory-files path t "^[^\\.]"))
1605 (delete-file (car files))
1606 (setq files (cdr files)))
1607 (delete-directory path))
1609 (not (file-directory-p cache-path)))
1610 (delete-file cache-path)))
1612 (setq dir (directory-file-name (file-name-directory path)))
1613 (if (not (file-exists-p dir))
1614 (elmo-make-directory dir))
1615 (write-region-as-binary (point-min) (point-max)
1621 (defun elmo-file-cache-load (cache-path section)
1622 "Load cache on PATH into the current buffer.
1623 Return t if cache is loaded successfully."
1626 (when (and cache-path
1627 (if (elmo-cache-path-section-p cache-path)
1630 (setq cache-file (elmo-file-cache-expand-path
1633 (file-exists-p cache-file))
1634 (insert-file-contents-as-binary cache-file)
1639 (defun elmo-cache-path-section-p (path)
1640 "Return non-nil when PATH is `section' cache path."
1641 (file-directory-p path))
1643 (defun elmo-file-cache-get (msgid &optional section)
1644 "Returns the current file-cache object associated with MSGID.
1645 MSGID is the message-id of the message.
1646 If optional argument SECTION is specified, get partial file-cache object
1647 associated with SECTION."
1649 (let ((path (elmo-cache-get-path msgid)))
1650 (if (and path (file-exists-p path))
1651 (if (elmo-cache-path-section-p path)
1653 (if (file-exists-p (setq path (expand-file-name
1655 (cons path 'section))
1656 ;; section is not specified but sectional.
1657 (cons path 'section))
1660 (cons path 'entire)))
1667 (defun elmo-cache-expire ()
1669 (let* ((completion-ignore-case t)
1670 (method (completing-read (format "Expire by (%s): "
1671 elmo-cache-expire-default-method)
1674 (if (string= method "")
1675 (setq method elmo-cache-expire-default-method))
1676 (funcall (intern (concat "elmo-cache-expire-by-" method)))))
1678 (defun elmo-read-float-value-from-minibuffer (prompt &optional initial)
1679 (let ((str (read-from-minibuffer prompt initial)))
1681 ((string-match "[0-9]*\\.[0-9]+" str)
1682 (string-to-number str))
1683 ((string-match "[0-9]+" str)
1684 (string-to-number (concat str ".0")))
1685 (t (error "%s is not number" str)))))
1687 (defun elmo-cache-expire-by-size (&optional kbytes)
1688 "Expire cache file by size.
1689 If KBYTES is kilo bytes (This value must be float)."
1691 (let ((size (or kbytes
1692 (and (interactive-p)
1693 (elmo-read-float-value-from-minibuffer
1694 "Enter cache disk size (Kbytes): "
1696 (if (integerp elmo-cache-expire-default-size)
1697 (float elmo-cache-expire-default-size)
1698 elmo-cache-expire-default-size))))
1699 (if (integerp elmo-cache-expire-default-size)
1700 (float elmo-cache-expire-default-size))))
1704 (message "Checking disk usage...")
1705 (setq total (/ (elmo-disk-usage
1706 elmo-cache-directory) Kbytes))
1707 (setq beginning total)
1708 (message "Checking disk usage...done")
1709 (let ((cfl (elmo-cache-get-sorted-cache-file-list))
1713 (while (and (<= size total)
1714 (setq oldest (elmo-cache-get-oldest-cache-file-entity cfl)))
1715 (setq cur-file (expand-file-name (car (cdr oldest)) (car oldest)))
1716 (setq cur-size (/ (elmo-disk-usage cur-file) Kbytes))
1717 (when (elmo-file-cache-delete cur-file)
1718 (setq count (+ count 1))
1719 (message "%d cache(s) are expired." count))
1720 (setq deleted (+ deleted cur-size))
1721 (setq total (- total cur-size)))
1722 (message "%d cache(s) are expired from disk (%d Kbytes/%d Kbytes)."
1723 count deleted beginning))))
1725 (defun elmo-cache-make-file-entity (filename path)
1726 (cons filename (elmo-get-last-accessed-time filename path)))
1728 (defun elmo-cache-get-oldest-cache-file-entity (cache-file-list)
1729 (let ((cfl cache-file-list)
1730 flist firsts oldest-entity wonlist)
1732 (setq flist (cdr (car cfl)))
1733 (setq firsts (append firsts (list
1734 (cons (car (car cfl))
1736 (setq cfl (cdr cfl)))
1739 (if (and (not oldest-entity)
1740 (cdr (cdr (car firsts))))
1741 (setq oldest-entity (car firsts)))
1742 (if (and (cdr (cdr (car firsts)))
1743 (cdr (cdr oldest-entity))
1744 (> (cdr (cdr oldest-entity)) (cdr (cdr (car firsts)))))
1745 (setq oldest-entity (car firsts)))
1746 (setq firsts (cdr firsts)))
1747 (setq wonlist (assoc (car oldest-entity) cache-file-list))
1749 (setcdr wonlist (delete (car (cdr wonlist)) (cdr wonlist))))
1752 (defun elmo-cache-get-sorted-cache-file-list ()
1753 (let ((dirs (directory-files
1754 elmo-cache-directory
1759 (setq num (length dirs))
1760 (message "Collecting cache info...")
1762 (setq elist (mapcar (lambda (x)
1763 (elmo-cache-make-file-entity x (car dirs)))
1764 (directory-files (car dirs) nil "^[^\\.]")))
1765 (setq ret-val (append ret-val
1773 (when (> num elmo-display-progress-threshold)
1775 (elmo-display-progress
1776 'elmo-cache-get-sorted-cache-file-list "Collecting cache info..."
1778 (setq dirs (cdr dirs)))
1779 (message "Collecting cache info...done")
1782 (defun elmo-cache-expire-by-age (&optional days)
1783 (let ((age (or (and days (int-to-string days))
1784 (and (interactive-p)
1785 (read-from-minibuffer
1786 (format "Enter days (%s): "
1787 elmo-cache-expire-default-age)))
1788 (int-to-string elmo-cache-expire-default-age)))
1789 (dirs (directory-files
1790 elmo-cache-directory
1794 (if (string= age "")
1795 (setq age elmo-cache-expire-default-age)
1796 (setq age (string-to-int age)))
1797 (setq curtime (current-time))
1798 (setq curtime (+ (* (nth 0 curtime)
1799 (float 65536)) (nth 1 curtime)))
1801 (let ((files (directory-files (car dirs) t "^[^\\.]"))
1802 (limit-age (* age 86400)))
1804 (when (> (- curtime (elmo-get-last-accessed-time (car files)))
1806 (when (elmo-file-cache-delete (car files))
1807 (setq count (+ 1 count))
1808 (message "%d cache file(s) are expired." count)))
1809 (setq files (cdr files))))
1810 (setq dirs (cdr dirs)))))
1814 (defun elmo-msgid-to-cache (msgid)
1817 (string-match "<\\(.+\\)>$" msgid))
1818 (elmo-replace-string-as-filename (elmo-match-string 1 msgid)))))
1820 (defun elmo-cache-get-path (msgid &optional folder number)
1821 "Get path for cache file associated with MSGID, FOLDER, and NUMBER."
1822 (if (setq msgid (elmo-msgid-to-cache msgid))
1826 (format "%s/%s/%s@%s"
1827 (elmo-cache-get-path-subr msgid)
1830 (elmo-safe-filename folder))
1832 (elmo-cache-get-path-subr msgid)
1834 elmo-cache-directory))))
1839 (defconst elmo-warning-buffer-name "*elmo warning*")
1841 (defun elmo-warning (&rest args)
1842 "Display a warning, making warning message by passing all args to `insert'."
1843 (with-current-buffer (get-buffer-create elmo-warning-buffer-name)
1844 (goto-char (point-max))
1845 (apply 'insert (append args '("\n")))
1847 (display-buffer elmo-warning-buffer-name))
1849 (defvar elmo-obsolete-variable-alist nil)
1851 (defcustom elmo-obsolete-variable-show-warnings t
1852 "Show warning window if obsolete variable is treated."
1856 (defun elmo-define-obsolete-variable (obsolete var)
1857 "Define obsolete variable.
1858 OBSOLETE is a symbol for obsolete variable.
1859 VAR is a symbol for new variable.
1860 Definition is stored in `elmo-obsolete-variable-alist'."
1861 (let ((pair (assq var elmo-obsolete-variable-alist)))
1863 (setcdr pair obsolete)
1864 (setq elmo-obsolete-variable-alist
1865 (cons (cons var obsolete)
1866 elmo-obsolete-variable-alist)))))
1868 (defun elmo-resque-obsolete-variable (obsolete var)
1869 "Resque obsolete variable OBSOLETE as VAR.
1870 If `elmo-obsolete-variable-show-warnings' is non-nil, show warning message."
1871 (when (boundp obsolete)
1872 (static-if (and (fboundp 'defvaralias)
1873 (subrp (symbol-function 'defvaralias)))
1874 (defvaralias var obsolete)
1875 (set var (symbol-value obsolete)))
1876 (if elmo-obsolete-variable-show-warnings
1877 (elmo-warning (format "%s is obsolete. Use %s instead."
1878 (symbol-name obsolete)
1879 (symbol-name var))))))
1881 (defun elmo-resque-obsolete-variables (&optional alist)
1882 "Resque obsolete variables in ALIST.
1883 ALIST is a list of cons cell of
1884 \(OBSOLETE-VARIABLE-SYMBOL . NEW-VARIABLE-SYMBOL\).
1885 If ALIST is nil, `elmo-obsolete-variable-alist' is used."
1886 (dolist (pair elmo-obsolete-variable-alist)
1887 (elmo-resque-obsolete-variable (cdr pair)
1891 (defvar elmo-dop-queue-filename "queue"
1892 "*Disconnected operation queue is saved in this file.")
1894 (defun elmo-dop-queue-load ()
1895 (setq elmo-dop-queue
1897 (expand-file-name elmo-dop-queue-filename
1898 elmo-msgdb-directory))))
1900 (defun elmo-dop-queue-save ()
1902 (expand-file-name elmo-dop-queue-filename
1903 elmo-msgdb-directory)
1907 (product-provide (provide 'elmo-util) (require 'elmo-version))
1909 ;;; elmo-util.el ends here