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 "%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 (let ((default (format-time-string "%Y-%m-%d")))
174 (setq value (completing-read
175 (format "Value for '%s' [%s]: " field default)
178 (list (format "%s" (car x)))))
179 elmo-date-descriptions)))
180 (concat (downcase field) ":"
181 (if (equal value "") default value))))
183 (setq value (read-from-minibuffer (format "Value for '%s': " field)))
184 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
186 (setq value (prin1-to-string value)))
187 (concat (downcase field) ":" value)))))
189 (defsubst elmo-condition-parse-error ()
190 (error "Syntax error in '%s'" (buffer-string)))
192 (defun elmo-parse-search-condition (condition)
194 Return value is a cons cell of (STRUCTURE . REST)"
197 (goto-char (point-min))
198 (cons (elmo-condition-parse) (buffer-substring (point) (point-max)))))
200 ;; condition ::= or-expr
201 (defun elmo-condition-parse ()
202 (or (elmo-condition-parse-or-expr)
203 (elmo-condition-parse-error)))
205 ;; or-expr ::= and-expr /
206 ;; and-expr "|" or-expr
207 (defun elmo-condition-parse-or-expr ()
208 (let ((left (elmo-condition-parse-and-expr)))
209 (if (looking-at "| *")
211 (goto-char (match-end 0))
212 (list 'or left (elmo-condition-parse-or-expr)))
215 ;; and-expr ::= primitive /
216 ;; primitive "&" and-expr
217 (defun elmo-condition-parse-and-expr ()
218 (let ((left (elmo-condition-parse-primitive)))
219 (if (looking-at "& *")
221 (goto-char (match-end 0))
222 (list 'and left (elmo-condition-parse-and-expr)))
225 ;; primitive ::= "(" expr ")" /
226 ;; ["!"] search-key SPACE* ":" SPACE* search-value
227 (defun elmo-condition-parse-primitive ()
230 (goto-char (match-end 0))
231 (prog1 (elmo-condition-parse)
232 (unless (looking-at ") *")
233 (elmo-condition-parse-error))
234 (goto-char (match-end 0))))
235 ;; search-key ::= [A-Za-z-]+
236 ;; ;; "since" / "before" / "last" / "first" /
237 ;; ;; "body" / field-name
238 ((looking-at "\\(!\\)? *\\([A-Za-z-]+\\) *: *")
239 (goto-char (match-end 0))
240 (let ((search-key (vector
241 (if (match-beginning 1) 'unmatch 'match)
242 (elmo-match-buffer 2)
243 (elmo-condition-parse-search-value))))
245 (if (string= (aref search-key 1) "tocc")
246 (if (eq (aref search-key 0) 'match)
248 (vector 'match "to" (aref search-key 2))
249 (vector 'match "cc" (aref search-key 2)))
251 (vector 'unmatch "to" (aref search-key 2))
252 (vector 'unmatch "cc" (aref search-key 2))))
255 ;; search-value ::= quoted / time / number / atom
256 ;; quoted ::= <elisp string expression>
257 ;; time ::= "yesterday" / "lastweek" / "lastmonth" / "lastyear" /
258 ;; number SPACE* "daysago" /
259 ;; number "-" month "-" number ; ex. 10-May-2000
260 ;; number "-" number "-" number ; ex. 2000-05-10
262 ;; month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
263 ;; "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
264 ;; atom ::= ATOM_CHARS*
265 ;; SPACE ::= <ascii space character, 0x20>
266 ;; ATOM_CHARS ::= <any character except specials>
267 ;; specials ::= SPACE / <"> / </> / <)> / <|> / <&>
268 ;; ;; These characters should be quoted.
269 (defun elmo-condition-parse-search-value ()
272 (elmo-read (current-buffer)))
273 ((or (looking-at "yesterday") (looking-at "lastweek")
274 (looking-at "lastmonth") (looking-at "lastyear")
275 (looking-at "[0-9]+ *daysago")
276 (looking-at "[0-9]+-[A-Za-z]+-[0-9]+")
277 (looking-at "[0-9]+-[0-9]+-[0-9]+")
278 (looking-at "[0-9]+")
279 (looking-at elmo-condition-atom-regexp))
280 (prog1 (elmo-match-buffer 0)
281 (goto-char (match-end 0))))
282 (t (error "Syntax error '%s'" (buffer-string)))))
285 (defsubst elmo-buffer-replace (regexp &optional newtext)
286 (goto-char (point-min))
287 (while (re-search-forward regexp nil t)
288 (replace-match (or newtext ""))))
290 (defsubst elmo-delete-char (char string &optional unibyte)
293 (let ((coding-system-for-read 'no-conversion)
294 (coding-system-for-write 'no-conversion))
295 (if unibyte (elmo-set-buffer-multibyte nil))
297 (goto-char (point-min))
298 (while (search-forward (char-to-string char) nil t)
302 (defsubst elmo-delete-cr-buffer ()
303 "Delete CR from buffer."
305 (goto-char (point-min))
306 (while (search-forward "\r\n" nil t)
307 (replace-match "\n")) ))
309 (defsubst elmo-delete-cr-get-content-type ()
311 (goto-char (point-min))
312 (while (search-forward "\r\n" nil t)
313 (replace-match "\n"))
314 (goto-char (point-min))
315 (or (std11-field-body "content-type")
318 (defun elmo-delete-cr (string)
322 (goto-char (point-min))
323 (while (search-forward "\r\n" nil t)
324 (replace-match "\n"))
327 (defun elmo-uniq-list (lst)
328 "Distractively uniqfy elements of LST."
337 (defun elmo-list-insert (list element after)
338 (let* ((match (memq after list))
339 (rest (and match (cdr (memq after list)))))
342 (setcdr match (list element))
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 "%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)))
689 (defun elmo-get-last-accessed-time (path &optional dir)
690 "Return the last accessed time of PATH."
691 (let ((last-accessed (nth 4 (file-attributes (or (and dir
696 (setq last-accessed (+ (* (nth 0 last-accessed)
697 (float 65536)) (nth 1 last-accessed)))
700 (defun elmo-get-last-modification-time (path &optional dir)
701 "Return the last accessed time of PATH."
702 (let ((last-modified (nth 5 (file-attributes (or (and dir
706 (setq last-modified (+ (* (nth 0 last-modified)
707 (float 65536)) (nth 1 last-modified)))))
709 (defun elmo-make-directory (path &optional mode)
710 "Create directory recursively."
711 (let ((parent (directory-file-name (file-name-directory path))))
712 (if (null (file-directory-p parent))
713 (elmo-make-directory parent))
714 (make-directory path)
715 (set-file-modes path (or mode
716 (+ (* 64 7) (* 8 0) 0))))) ; chmod 0700
718 (defun elmo-delete-directory (path &optional no-hierarchy)
719 "Delete directory recursively."
720 (if (stringp path) ; nil is not permitted.
721 (let ((dirent (directory-files path))
722 relpath abspath hierarchy)
724 (setq relpath (car dirent)
726 abspath (expand-file-name relpath path))
727 (when (not (string-match "^\\.\\.?$" relpath))
728 (if (eq (nth 0 (file-attributes abspath)) t)
731 (elmo-delete-directory abspath no-hierarchy))
732 (delete-file abspath))))
734 (delete-directory path)))))
736 (defun elmo-delete-match-files (path regexp &optional remove-if-empty)
737 "Delete directory files specified by PATH.
738 If optional REMOVE-IF-EMPTY is non-nil, delete directory itself if
739 the directory becomes empty after deletion."
740 (when (stringp path) ; nil is not permitted.
741 (dolist (file (directory-files path t regexp))
745 (delete-directory path) ; should be removed if empty.
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-condition-p (filter)
810 `(or (vectorp ,filter) (consp ,filter)))
812 (defmacro elmo-filter-type (filter)
813 (` (aref (, filter) 0)))
815 (defmacro elmo-filter-key (filter)
816 (` (aref (, filter) 1)))
818 (defmacro elmo-filter-value (filter)
819 (` (aref (, filter) 2)))
821 (defsubst elmo-buffer-field-primitive-condition-match (condition
825 (goto-char (point-min))
827 ((string= (elmo-filter-key condition) "last")
828 (setq result (<= (length (memq number number-list))
829 (string-to-int (elmo-filter-value condition)))))
830 ((string= (elmo-filter-key condition) "first")
831 (setq result (< (- (length number-list)
832 (length (memq number number-list)))
833 (string-to-int (elmo-filter-value condition)))))
834 ((string= (elmo-filter-key condition) "since")
835 (let ((field-date (elmo-date-make-sortable-string
837 (std11-field-body "date")
838 (current-time-zone) nil)))
839 (specified-date (elmo-date-make-sortable-string
840 (elmo-date-get-datevec
841 (elmo-filter-value condition)))))
843 (or (string= field-date specified-date)
844 (string< specified-date field-date)))))
845 ((string= (elmo-filter-key condition) "before")
848 (elmo-date-make-sortable-string
850 (std11-field-body "date")
851 (current-time-zone) nil))
852 (elmo-date-make-sortable-string
853 (elmo-date-get-datevec
854 (elmo-filter-value condition))))))
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-in-msgdb-p-internal (condition fields)
872 (if (not (member (elmo-filter-key condition) fields))
874 ((or (eq (car condition) 'and)
875 (eq (car condition) 'or))
876 (elmo-condition-in-msgdb-p-internal (nth 1 condition) fields)
877 (elmo-condition-in-msgdb-p-internal (nth 2 condition) fields))))
879 (defun elmo-condition-in-msgdb-p (condition)
881 (elmo-condition-in-msgdb-p-internal condition
883 elmo-msgdb-extra-fields
884 '("last" "first" "from"
885 "subject" "to" "cc" "since"
888 (defun elmo-buffer-field-condition-match (condition number number-list)
891 (elmo-buffer-field-primitive-condition-match
892 condition number number-list))
893 ((eq (car condition) 'and)
894 (and (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)))
898 ((eq (car condition) 'or)
899 (or (elmo-buffer-field-condition-match
900 (nth 1 condition) number number-list)
901 (elmo-buffer-field-condition-match
902 (nth 2 condition) number number-list)))))
904 (defsubst elmo-file-field-primitive-condition-match (file
909 (goto-char (point-min))
911 ((string= (elmo-filter-key condition) "last")
912 (setq result (<= (length (memq number number-list))
913 (string-to-int (elmo-filter-value condition))))
914 (if (eq (elmo-filter-type condition) 'unmatch)
915 (setq result (not result))))
916 ((string= (elmo-filter-key condition) "first")
917 (setq result (< (- (length number-list)
918 (length (memq number number-list)))
919 (string-to-int (elmo-filter-value condition))))
920 (if (eq (elmo-filter-type condition) 'unmatch)
921 (setq result (not result))))
924 (as-binary-input-file (insert-file-contents file))
925 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
926 ;; Should consider charset?
927 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
929 (elmo-buffer-field-primitive-condition-match
930 condition number number-list)))))
933 (defun elmo-file-field-condition-match (file condition number number-list)
936 (elmo-file-field-primitive-condition-match
937 file condition number number-list))
938 ((eq (car condition) 'and)
939 (and (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)))
943 ((eq (car condition) 'or)
944 (or (elmo-file-field-condition-match
945 file (nth 1 condition) number number-list)
946 (elmo-file-field-condition-match
947 file (nth 2 condition) number number-list)))))
949 (defmacro elmo-get-hash-val (string hashtable)
950 `(and (stringp ,string)
951 (let ((sym (intern-soft ,string ,hashtable)))
953 (symbol-value sym)))))
955 (defmacro elmo-set-hash-val (string value hashtable)
956 (list 'set (list 'intern string hashtable) value))
958 (defmacro elmo-clear-hash-val (string hashtable)
959 (static-if (fboundp 'unintern)
960 (list 'unintern string hashtable)
961 (list 'makunbound (list 'intern string hashtable))))
963 (defmacro elmo-unintern (string)
964 "`unintern' symbol named STRING, When can use `unintern'.
965 Emacs 19.28 or earlier does not have `unintern'."
966 (static-if (fboundp 'unintern)
967 (list 'unintern string)))
969 (defun elmo-make-hash (&optional hashsize)
970 "Make a new hash table which have HASHSIZE size."
974 ;; Prime numbers as lengths tend to result in good
975 ;; hashing; lengths one less than a power of two are
979 (while (< (- i 1) hashsize)
982 elmo-hash-maximum-size)
983 elmo-hash-minimum-size)
984 elmo-hash-minimum-size)
987 (defsubst elmo-mime-string (string)
988 "Normalize MIME encoded STRING."
991 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
993 (encode-mime-charset-string
994 (eword-decode-and-unfold-unstructured-field-body
997 (elmo-set-buffer-multibyte nil)
1000 (defsubst elmo-collect-field (beg end downcase-field-name)
1003 (narrow-to-region beg end)
1004 (goto-char (point-min))
1005 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1007 (while (re-search-forward regexp nil t)
1008 (setq name (buffer-substring-no-properties
1009 (match-beginning 1)(1- (match-end 1))))
1010 (if downcase-field-name
1011 (setq name (downcase name)))
1012 (setq body (buffer-substring-no-properties
1013 (match-end 0) (std11-field-end)))
1014 (or (assoc name dest)
1015 (setq dest (cons (cons name body) dest))))
1018 (defsubst elmo-collect-field-from-string (string downcase-field-name)
1021 (goto-char (point-min))
1022 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1024 (while (re-search-forward regexp nil t)
1025 (setq name (buffer-substring-no-properties
1026 (match-beginning 1)(1- (match-end 1))))
1027 (if downcase-field-name
1028 (setq name (downcase name)))
1029 (setq body (buffer-substring-no-properties
1030 (match-end 0) (std11-field-end)))
1031 (or (assoc name dest)
1032 (setq dest (cons (cons name body) dest))))
1035 (defun elmo-safe-filename (folder)
1036 (elmo-replace-in-string
1037 (elmo-replace-in-string
1038 (elmo-replace-in-string folder "/" " ")
1042 (defvar elmo-filename-replace-chars nil)
1044 (defsubst elmo-replace-string-as-filename (msgid)
1045 "Replace string as filename."
1046 (setq msgid (elmo-replace-in-string msgid " " " "))
1047 (if (null elmo-filename-replace-chars)
1048 (setq elmo-filename-replace-chars
1049 (regexp-quote (mapconcat
1050 'car elmo-filename-replace-string-alist ""))))
1051 (while (string-match (concat "[" elmo-filename-replace-chars "]")
1054 (substring msgid 0 (match-beginning 0))
1057 (match-beginning 0) (match-end 0))
1058 elmo-filename-replace-string-alist))
1059 (substring msgid (match-end 0)))))
1062 (defsubst elmo-recover-string-from-filename (filename)
1063 "Recover string from FILENAME."
1065 (while (string-match " " filename)
1066 (setq tmp (substring filename
1068 (+ (match-end 0) 1)))
1069 (if (string= tmp " ")
1071 (setq tmp (car (rassoc tmp
1072 elmo-filename-replace-string-alist))))
1075 (substring filename 0 (match-beginning 0))
1077 (setq filename (substring filename (+ (match-end 0) 1))))
1078 (concat result filename)))
1080 (defsubst elmo-copy-file (src dst &optional ok-if-already-exists)
1082 (elmo-add-name-to-file src dst ok-if-already-exists)
1083 (error (copy-file src dst ok-if-already-exists t))))
1085 (defsubst elmo-buffer-exists-p (buffer)
1086 (if (bufferp buffer)
1087 (buffer-live-p buffer)
1088 (get-buffer buffer)))
1090 (defsubst elmo-kill-buffer (buffer)
1091 (when (elmo-buffer-exists-p buffer)
1092 (kill-buffer buffer)))
1094 (defun elmo-delete-if (pred lst)
1095 "Return new list contain items which don't satisfy PRED in LST."
1098 (unless (funcall pred (car lst))
1099 (setq result (nconc result (list (car lst)))))
1100 (setq lst (cdr lst)))
1103 (defun elmo-list-delete (list1 list2 &optional delete-function)
1104 "Delete by side effect any occurrences equal to elements of LIST1 from LIST2.
1105 Return the modified LIST2. Deletion is done with `delete'.
1106 Write `(setq foo (elmo-list-delete bar foo))' to be sure of changing
1108 If optional DELETE-FUNCTION is speficied, it is used as delete procedure."
1109 (setq delete-function (or delete-function 'delete))
1111 (setq list2 (funcall delete-function (car list1) list2))
1112 (setq list1 (cdr list1)))
1115 (defun elmo-list-member (list1 list2)
1116 "If any element of LIST1 is member of LIST2, return t."
1119 (if (member (car list1) list2)
1121 (setq list1 (cdr list1)))))
1123 (defun elmo-count-matches (regexp beg end)
1127 (while (re-search-forward regexp end t)
1128 (setq count (1+ count)))
1131 (if (fboundp 'display-error)
1132 (defalias 'elmo-display-error 'display-error)
1133 (defun elmo-display-error (error-object stream)
1134 "A tiny function to display ERROR-OBJECT to the STREAM."
1136 (errobj error-object)
1139 (setq err-mes (concat err-mes (format
1140 (if (stringp (car errobj))
1144 (setq errobj (cdr errobj))
1145 (if errobj (setq err-mes (concat err-mes (if first ": " ", "))))
1147 (princ err-mes stream))))
1149 (if (fboundp 'define-error)
1150 (defalias 'elmo-define-error 'define-error)
1151 (defun elmo-define-error (error doc &optional parents)
1153 (setq parents 'error))
1154 (let ((conds (get parents 'error-conditions)))
1156 (error "Not an error symbol: %s" error))
1158 (list 'error-message doc
1159 'error-conditions (cons error conds))))))
1161 (cond ((fboundp 'progress-feedback-with-label)
1162 (defalias 'elmo-display-progress 'progress-feedback-with-label))
1163 ((fboundp 'lprogress-display)
1164 (defalias 'elmo-display-progress 'lprogress-display))
1166 (defun elmo-display-progress (label format &optional value &rest args)
1167 "Print a progress message."
1168 (if (and (null format) (null args))
1170 (apply (function message) (concat format " %d%%")
1171 (nconc args (list value)))))))
1173 (defvar elmo-progress-counter-alist nil)
1175 (defmacro elmo-progress-counter-value (counter)
1176 (` (aref (cdr (, counter)) 0)))
1178 (defmacro elmo-progress-counter-all-value (counter)
1179 (` (aref (cdr (, counter)) 1)))
1181 (defmacro elmo-progress-counter-format (counter)
1182 (` (aref (cdr (, counter)) 2)))
1184 (defmacro elmo-progress-counter-set-value (counter value)
1185 (` (aset (cdr (, counter)) 0 (, value))))
1187 (defun elmo-progress-set (label all-value &optional format)
1188 (unless (assq label elmo-progress-counter-alist)
1189 (setq elmo-progress-counter-alist
1190 (cons (cons label (vector 0 all-value (or format "")))
1191 elmo-progress-counter-alist))))
1193 (defun elmo-progress-clear (label)
1194 (let ((counter (assq label elmo-progress-counter-alist)))
1196 (elmo-display-progress label
1197 (elmo-progress-counter-format counter)
1199 (setq elmo-progress-counter-alist
1200 (delq counter elmo-progress-counter-alist)))))
1202 (defun elmo-progress-notify (label &optional value op &rest args)
1203 (let ((counter (assq label elmo-progress-counter-alist)))
1205 (let* ((value (or value 1))
1206 (cur-value (elmo-progress-counter-value counter))
1207 (all-value (elmo-progress-counter-all-value counter))
1208 (new-value (if (eq op 'set) value (+ cur-value value)))
1209 (cur-rate (/ (* cur-value 100) all-value))
1210 (new-rate (/ (* new-value 100) all-value)))
1211 (elmo-progress-counter-set-value counter new-value)
1212 (unless (= cur-rate new-rate)
1213 (apply 'elmo-display-progress
1215 (elmo-progress-counter-format counter)
1218 (when (>= new-rate 100)
1219 (elmo-progress-clear label))))))
1221 (defun elmo-time-expire (before-time diff-time)
1222 (let* ((current (current-time))
1223 (rest (when (< (nth 1 current) (nth 1 before-time))
1227 (list (- (+ (car current) (if rest -1 0)) (car before-time))
1228 (- (+ (or rest 0) (nth 1 current)) (nth 1 before-time))))
1229 (and (eq (car diff) 0)
1230 (< diff-time (nth 1 diff)))))
1232 (if (fboundp 'std11-fetch-field)
1233 (defalias 'elmo-field-body 'std11-fetch-field) ;;no narrow-to-region
1234 (defalias 'elmo-field-body 'std11-field-body))
1236 (defun elmo-address-quote-specials (word)
1237 "Make quoted string of WORD if needed."
1238 (let ((lal (std11-lexical-analyze word)))
1239 (if (or (assq 'specials lal)
1240 (assq 'domain-literal lal))
1241 (prin1-to-string word)
1244 (defmacro elmo-string (string)
1245 "STRING without text property."
1246 (` (let ((obj (copy-sequence (, string))))
1247 (and obj (set-text-properties 0 (length obj) nil obj))
1250 (defun elmo-flatten (list-of-list)
1251 "Flatten LIST-OF-LIST."
1252 (unless (null list-of-list)
1253 (append (if (and (car list-of-list)
1254 (listp (car list-of-list)))
1256 (list (car list-of-list)))
1257 (elmo-flatten (cdr list-of-list)))))
1259 (defun elmo-y-or-n-p (prompt &optional auto default)
1260 "Same as `y-or-n-p'.
1261 But if optional argument AUTO is non-nil, DEFAULT is returned."
1266 (defun elmo-string-member (string slist)
1267 "Return t if STRING is a member of the SLIST."
1270 (if (and (stringp (car slist))
1271 (string= string (car slist)))
1273 (setq slist (cdr slist)))))
1275 (defun elmo-string-match-member (str list &optional case-ignore)
1276 (let ((case-fold-search case-ignore))
1279 (if (string-match (car list) str)
1280 (throw 'member (car list)))
1281 (setq list (cdr list))))))
1283 (defun elmo-string-matched-member (str list &optional case-ignore)
1284 (let ((case-fold-search case-ignore))
1287 (if (string-match str (car list))
1288 (throw 'member (car list)))
1289 (setq list (cdr list))))))
1291 (defsubst elmo-string-delete-match (string pos)
1292 (concat (substring string
1293 0 (match-beginning pos))
1298 (defun elmo-string-match-assoc (key alist &optional case-ignore)
1299 (let ((case-fold-search case-ignore)
1303 (setq a (car alist))
1306 (string-match key (car a)))
1308 (setq alist (cdr alist))))))
1310 (defun elmo-string-matched-assoc (key alist &optional case-ignore)
1311 (let ((case-fold-search case-ignore)
1315 (setq a (car alist))
1318 (string-match (car a) key))
1320 (setq alist (cdr alist))))))
1322 (defun elmo-string-assoc (key alist)
1326 (setq a (car alist))
1329 (string= key (car a)))
1331 (setq alist (cdr alist))))))
1333 (defun elmo-string-assoc-all (key alist)
1336 (if (string= key (car (car alist)))
1340 (setq alist (cdr alist)))
1343 (defun elmo-string-rassoc (key alist)
1347 (setq a (car alist))
1350 (string= key (cdr a)))
1352 (setq alist (cdr alist))))))
1354 (defun elmo-string-rassoc-all (key alist)
1357 (if (string= key (cdr (car alist)))
1361 (setq alist (cdr alist)))
1364 (defun elmo-expand-newtext (newtext original)
1365 (let ((len (length newtext))
1367 c expanded beg N did-expand)
1370 (while (and (< pos len)
1371 (not (= (aref newtext pos) ?\\)))
1372 (setq pos (1+ pos)))
1374 (push (substring newtext beg pos) expanded))
1376 ;; We hit a \; expand it.
1379 c (aref newtext pos))
1380 (if (not (or (= c ?\&)
1383 ;; \ followed by some character we don't expand.
1384 (push (char-to-string c) expanded)
1389 (when (match-beginning N)
1390 (push (substring original (match-beginning N) (match-end N))
1392 (setq pos (1+ pos)))
1394 (apply (function concat) (nreverse expanded))
1397 ;;; Folder parser utils.
1398 (defun elmo-parse-token (string &optional seps)
1399 "Parse atom from STRING using SEPS as a string of separator char list."
1400 (let ((len (length string))
1401 (seps (and seps (string-to-char-list seps)))
1407 (while (and (< i len) (or in (null sep)))
1408 (setq c (aref string i))
1410 ((and in (eq c ?\\))
1412 content (cons (aref string i) content)
1417 (in (setq content (cons c content)
1421 (t (setq content (cons c content)
1423 (if in (error "Parse error in quoted"))
1424 (cons (if (null content) "" (char-list-to-string (nreverse content)))
1425 (substring string i)))))
1427 (defun elmo-parse-prefixed-element (prefix string &optional seps)
1428 (if (and (not (eq (length string) 0))
1429 (eq (aref string 0) prefix))
1430 (elmo-parse-token (substring string 1) seps)
1433 ;;; Number set defined by OKAZAKI Tetsurou <okazaki@be.to>
1435 ;; number ::= [0-9]+
1438 ;; number-range ::= "(" beg " . " end ")" ;; cons cell
1439 ;; number-set-elem ::= number / number-range
1440 ;; number-set ::= "(" *number-set-elem ")" ;; list
1442 (defun elmo-number-set-member (number number-set)
1443 "Return non-nil if NUMBER is an element of NUMBER-SET.
1444 The value is actually the tail of NUMBER-RANGE whose car contains NUMBER."
1445 (or (memq number number-set)
1447 (while (and number-set (not found))
1448 (if (and (consp (car number-set))
1449 (and (<= (car (car number-set)) number)
1450 (<= number (cdr (car number-set)))))
1452 (setq number-set (cdr number-set))))
1455 (defun elmo-number-set-append-list (number-set list)
1456 "Append LIST of numbers to the NUMBER-SET.
1457 NUMBER-SET is altered."
1458 (let ((appended number-set))
1460 (setq appended (elmo-number-set-append appended (car list)))
1461 (setq list (cdr list)))
1464 (defun elmo-number-set-append (number-set number)
1465 "Append NUMBER to the NUMBER-SET.
1466 NUMBER-SET is altered."
1467 (let ((number-set-1 number-set)
1469 (while (and number-set (not found))
1470 (setq elem (car number-set))
1473 (eq (+ 1 (cdr elem)) number))
1474 (setcdr elem number)
1476 ((and (integerp elem)
1477 (eq (+ 1 elem) number))
1478 (setcar number-set (cons elem number))
1480 ((or (and (integerp elem) (eq elem number))
1482 (<= (car elem) number)
1483 (<= number (cdr elem))))
1485 (setq number-set (cdr number-set)))
1487 (setq number-set-1 (nconc number-set-1 (list number))))
1490 (defun elmo-number-set-to-number-list (number-set)
1491 "Return a number list which corresponds to NUMBER-SET."
1492 (let (number-list elem i)
1494 (setq elem (car number-set))
1498 (while (<= i (cdr elem))
1499 (setq number-list (cons i number-list))
1502 (setq number-list (cons elem number-list))))
1503 (setq number-set (cdr number-set)))
1504 (nreverse number-list)))
1506 (defcustom elmo-list-subdirectories-ignore-regexp "^\\(\\.\\.?\\|[0-9]+\\)$"
1507 "*Regexp to filter subfolders."
1511 (defun elmo-list-subdirectories-1 (basedir curdir one-level)
1512 (let ((root (zerop (length curdir)))
1513 (w32-get-true-file-link-count t) ; for Meadow
1516 (dolist (file (directory-files (setq dir (expand-file-name curdir basedir))))
1517 (when (and (not (string-match
1518 elmo-list-subdirectories-ignore-regexp
1520 (car (setq attr (file-attributes
1521 (expand-file-name file dir)))))
1522 (when (eq one-level 'check) (throw 'done t))
1524 (concat curdir (and (not root) elmo-path-sep) file))
1526 (setq dirs (nconc dirs
1527 (if (if elmo-have-link-count (< 2 (nth 1 attr))
1529 (elmo-list-subdirectories-1
1532 (if one-level 'check))))
1534 (list (list relpath))
1537 (elmo-list-subdirectories-1
1541 (list relpath)))))))
1544 (defun elmo-list-subdirectories (directory file one-level)
1545 (let ((subdirs (elmo-list-subdirectories-1 directory file one-level)))
1546 (if (zerop (length file))
1548 (cons file subdirs))))
1550 (defun elmo-mapcar-list-of-list (func list-of-list)
1553 (cond ((listp x) (elmo-mapcar-list-of-list func x))
1554 (t (funcall func x))))
1557 (defun elmo-parse (string regexp &optional matchn)
1558 (or matchn (setq matchn 1))
1560 (store-match-data nil)
1561 (while (string-match regexp string (match-end 0))
1562 (setq list (cons (substring string (match-beginning matchn)
1563 (match-end matchn)) list)))
1567 (defmacro elmo-make-file-cache (path status)
1568 "PATH is the cache file name.
1569 STATUS is one of 'section, 'entire or nil.
1570 nil means no cache exists.
1571 'section means partial section cache exists.
1572 'entire means entire cache exists.
1573 If the cache is partial file-cache, TYPE is 'partial."
1574 (` (cons (, path) (, status))))
1576 (defmacro elmo-file-cache-path (file-cache)
1577 "Returns the file path of the FILE-CACHE."
1578 (` (car (, file-cache))))
1580 (defmacro elmo-file-cache-status (file-cache)
1581 "Returns the status of the FILE-CACHE."
1582 (` (cdr (, file-cache))))
1584 (defsubst elmo-cache-to-msgid (filename)
1585 (concat "<" (elmo-recover-string-from-filename filename) ">"))
1587 (defsubst elmo-cache-get-path-subr (msgid)
1588 (let ((chars '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?A ?B ?C ?D ?E ?F))
1589 (clist (string-to-char-list msgid))
1592 (setq sum (+ sum (car clist)))
1593 (setq clist (cdr clist)))
1595 (nth (% (/ sum 16) 2) chars)
1596 (nth (% sum 16) chars))))
1598 (defun elmo-file-cache-get-path (msgid &optional section)
1599 "Get cache path for MSGID.
1600 If optional argument SECTION is specified, partial cache path is returned."
1601 (if (setq msgid (elmo-msgid-to-cache msgid))
1604 (format "%s/%s/%s/%s"
1605 elmo-cache-directory
1606 (elmo-cache-get-path-subr msgid)
1610 elmo-cache-directory
1611 (elmo-cache-get-path-subr msgid)
1614 (defmacro elmo-file-cache-expand-path (path section)
1615 "Return file name for the file-cache corresponds to the section.
1616 PATH is the file-cache path.
1617 SECTION is the section string."
1618 (` (expand-file-name (or (, section) "") (, path))))
1620 (defun elmo-file-cache-delete (path)
1621 "Delete a cache on PATH."
1622 (when (file-exists-p path)
1623 (if (file-directory-p path)
1625 (dolist (file (directory-files path t "^[^\\.]"))
1627 (delete-directory path))
1631 (defun elmo-file-cache-exists-p (msgid)
1632 "Returns 'section or 'entire if a cache which corresponds to MSGID exists."
1633 (elmo-file-cache-status (elmo-file-cache-get msgid)))
1635 (defun elmo-file-cache-save (cache-path section)
1636 "Save current buffer as cache on PATH.
1637 Return t if cache is saved successfully."
1639 (let ((path (if section (expand-file-name section cache-path)
1642 (if (and (null section)
1643 (file-directory-p path))
1645 (setq files (directory-files path t "^[^\\.]"))
1647 (delete-file (car files))
1648 (setq files (cdr files)))
1649 (delete-directory path))
1651 (not (file-directory-p cache-path)))
1652 (delete-file cache-path)))
1654 (setq dir (directory-file-name (file-name-directory path)))
1655 (if (not (file-exists-p dir))
1656 (elmo-make-directory dir))
1657 (write-region-as-binary (point-min) (point-max)
1663 (defun elmo-file-cache-load (cache-path section)
1664 "Load cache on PATH into the current buffer.
1665 Return t if cache is loaded successfully."
1668 (when (and cache-path
1669 (if (elmo-cache-path-section-p cache-path)
1672 (setq cache-file (elmo-file-cache-expand-path
1675 (file-exists-p cache-file))
1676 (insert-file-contents-as-binary cache-file)
1681 (defun elmo-cache-path-section-p (path)
1682 "Return non-nil when PATH is `section' cache path."
1683 (file-directory-p path))
1685 (defun elmo-file-cache-get (msgid &optional section)
1686 "Returns the current file-cache object associated with MSGID.
1687 MSGID is the message-id of the message.
1688 If optional argument SECTION is specified, get partial file-cache object
1689 associated with SECTION."
1691 (let ((path (elmo-cache-get-path msgid)))
1692 (if (and path (file-exists-p path))
1693 (if (elmo-cache-path-section-p path)
1695 (if (file-exists-p (setq path (expand-file-name
1697 (cons path 'section))
1698 ;; section is not specified but sectional.
1699 (cons path 'section))
1702 (cons path 'entire)))
1709 (defun elmo-cache-expire ()
1711 (let* ((completion-ignore-case t)
1712 (method (completing-read (format "Expire by (%s): "
1713 elmo-cache-expire-default-method)
1716 (if (string= method "")
1717 (setq method elmo-cache-expire-default-method))
1718 (funcall (intern (concat "elmo-cache-expire-by-" method)))))
1720 (defun elmo-read-float-value-from-minibuffer (prompt &optional initial)
1721 (let ((str (read-from-minibuffer prompt initial)))
1723 ((string-match "[0-9]*\\.[0-9]+" str)
1724 (string-to-number str))
1725 ((string-match "[0-9]+" str)
1726 (string-to-number (concat str ".0")))
1727 (t (error "%s is not number" str)))))
1729 (defun elmo-cache-expire-by-size (&optional kbytes)
1730 "Expire cache file by size.
1731 If KBYTES is kilo bytes (This value must be float)."
1733 (let ((size (or kbytes
1734 (and (interactive-p)
1735 (elmo-read-float-value-from-minibuffer
1736 "Enter cache disk size (Kbytes): "
1738 (if (integerp elmo-cache-expire-default-size)
1739 (float elmo-cache-expire-default-size)
1740 elmo-cache-expire-default-size))))
1741 (if (integerp elmo-cache-expire-default-size)
1742 (float elmo-cache-expire-default-size))))
1746 (message "Checking disk usage...")
1747 (setq total (/ (elmo-disk-usage
1748 elmo-cache-directory) Kbytes))
1749 (setq beginning total)
1750 (message "Checking disk usage...done")
1751 (let ((cfl (elmo-cache-get-sorted-cache-file-list))
1755 (while (and (<= size total)
1756 (setq oldest (elmo-cache-get-oldest-cache-file-entity cfl)))
1757 (setq cur-file (expand-file-name (car (cdr oldest)) (car oldest)))
1758 (setq cur-size (/ (elmo-disk-usage cur-file) Kbytes))
1759 (when (elmo-file-cache-delete cur-file)
1760 (setq count (+ count 1))
1761 (message "%d cache(s) are expired." count))
1762 (setq deleted (+ deleted cur-size))
1763 (setq total (- total cur-size)))
1764 (message "%d cache(s) are expired from disk (%d Kbytes/%d Kbytes)."
1765 count deleted beginning))))
1767 (defun elmo-cache-make-file-entity (filename path)
1768 (cons filename (elmo-get-last-accessed-time filename path)))
1770 (defun elmo-cache-get-oldest-cache-file-entity (cache-file-list)
1771 (let ((cfl cache-file-list)
1772 flist firsts oldest-entity wonlist)
1774 (setq flist (cdr (car cfl)))
1775 (setq firsts (append firsts (list
1776 (cons (car (car cfl))
1778 (setq cfl (cdr cfl)))
1781 (if (and (not oldest-entity)
1782 (cdr (cdr (car firsts))))
1783 (setq oldest-entity (car firsts)))
1784 (if (and (cdr (cdr (car firsts)))
1785 (cdr (cdr oldest-entity))
1786 (> (cdr (cdr oldest-entity)) (cdr (cdr (car firsts)))))
1787 (setq oldest-entity (car firsts)))
1788 (setq firsts (cdr firsts)))
1789 (setq wonlist (assoc (car oldest-entity) cache-file-list))
1791 (setcdr wonlist (delete (car (cdr wonlist)) (cdr wonlist))))
1794 (defun elmo-cache-get-sorted-cache-file-list ()
1795 (let ((dirs (directory-files
1796 elmo-cache-directory
1801 (setq num (length dirs))
1802 (message "Collecting cache info...")
1804 (setq elist (mapcar (lambda (x)
1805 (elmo-cache-make-file-entity x (car dirs)))
1806 (directory-files (car dirs) nil "^[^\\.]")))
1807 (setq ret-val (append ret-val
1815 (when (> num elmo-display-progress-threshold)
1817 (elmo-display-progress
1818 'elmo-cache-get-sorted-cache-file-list "Collecting cache info..."
1820 (setq dirs (cdr dirs)))
1821 (message "Collecting cache info...done")
1824 (defun elmo-cache-expire-by-age (&optional days)
1825 (let ((age (or (and days (int-to-string days))
1826 (and (interactive-p)
1827 (read-from-minibuffer
1828 (format "Enter days (%s): "
1829 elmo-cache-expire-default-age)))
1830 (int-to-string elmo-cache-expire-default-age)))
1831 (dirs (directory-files
1832 elmo-cache-directory
1836 (if (string= age "")
1837 (setq age elmo-cache-expire-default-age)
1838 (setq age (string-to-int age)))
1839 (setq curtime (current-time))
1840 (setq curtime (+ (* (nth 0 curtime)
1841 (float 65536)) (nth 1 curtime)))
1843 (let ((files (directory-files (car dirs) t "^[^\\.]"))
1844 (limit-age (* age 86400)))
1846 (when (> (- curtime (elmo-get-last-accessed-time (car files)))
1848 (when (elmo-file-cache-delete (car files))
1849 (setq count (+ 1 count))
1850 (message "%d cache file(s) are expired." count)))
1851 (setq files (cdr files))))
1852 (setq dirs (cdr dirs)))))
1856 (defun elmo-msgid-to-cache (msgid)
1859 (string-match "<\\(.+\\)>$" msgid))
1860 (elmo-replace-string-as-filename (elmo-match-string 1 msgid)))))
1862 (defun elmo-cache-get-path (msgid &optional folder number)
1863 "Get path for cache file associated with MSGID, FOLDER, and NUMBER."
1864 (if (setq msgid (elmo-msgid-to-cache msgid))
1868 (format "%s/%s/%s@%s"
1869 (elmo-cache-get-path-subr msgid)
1872 (elmo-safe-filename folder))
1874 (elmo-cache-get-path-subr msgid)
1876 elmo-cache-directory))))
1881 (static-if (fboundp 'display-warning)
1882 (defmacro elmo-warning (&rest args)
1883 "Display a warning with `elmo' group."
1884 `(display-warning 'elmo (format ,@args)))
1885 (defconst elmo-warning-buffer-name "*elmo warning*")
1886 (defun elmo-warning (&rest args)
1887 "Display a warning. ARGS are passed to `format'."
1888 (with-current-buffer (get-buffer-create elmo-warning-buffer-name)
1889 (goto-char (point-max))
1890 (funcall 'insert (apply 'format (append args '("\n"))))
1891 (ignore-errors (recenter 1))
1892 (display-buffer elmo-warning-buffer-name))))
1894 (defvar elmo-obsolete-variable-alist nil)
1896 (defcustom elmo-obsolete-variable-show-warnings t
1897 "Show warning window if obsolete variable is treated."
1901 (defun elmo-define-obsolete-variable (obsolete var)
1902 "Define obsolete variable.
1903 OBSOLETE is a symbol for obsolete variable.
1904 VAR is a symbol for new variable.
1905 Definition is stored in `elmo-obsolete-variable-alist'."
1906 (let ((pair (assq var elmo-obsolete-variable-alist)))
1908 (setcdr pair obsolete)
1909 (setq elmo-obsolete-variable-alist
1910 (cons (cons var obsolete)
1911 elmo-obsolete-variable-alist)))))
1913 (defun elmo-resque-obsolete-variable (obsolete var)
1914 "Resque obsolete variable OBSOLETE as VAR.
1915 If `elmo-obsolete-variable-show-warnings' is non-nil, show warning message."
1916 (when (boundp obsolete)
1917 (static-if (and (fboundp 'defvaralias)
1918 (subrp (symbol-function 'defvaralias)))
1919 (defvaralias var obsolete)
1920 (set var (symbol-value obsolete)))
1921 (if elmo-obsolete-variable-show-warnings
1922 (elmo-warning "%s is obsolete. Use %s instead."
1923 (symbol-name obsolete)
1924 (symbol-name var)))))
1926 (defun elmo-resque-obsolete-variables (&optional alist)
1927 "Resque obsolete variables in ALIST.
1928 ALIST is a list of cons cell of
1929 \(OBSOLETE-VARIABLE-SYMBOL . NEW-VARIABLE-SYMBOL\).
1930 If ALIST is nil, `elmo-obsolete-variable-alist' is used."
1931 (dolist (pair elmo-obsolete-variable-alist)
1932 (elmo-resque-obsolete-variable (cdr pair)
1936 (defvar elmo-dop-queue-filename "queue"
1937 "*Disconnected operation queue is saved in this file.")
1939 (defun elmo-dop-queue-load ()
1940 (setq elmo-dop-queue
1942 (expand-file-name elmo-dop-queue-filename
1943 elmo-msgdb-directory))))
1945 (defun elmo-dop-queue-save ()
1947 (expand-file-name elmo-dop-queue-filename
1948 elmo-msgdb-directory)
1952 (product-provide (provide 'elmo-util) (require 'elmo-version))
1954 ;;; elmo-util.el ends here