1 ;;; elmo-util.el -- Utilities for Elmo.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
38 (require 'eword-decode)
42 (defmacro elmo-set-buffer-multibyte (flag)
43 "Set the multibyte flag of the current buffer to FLAG."
45 (list 'setq 'mc-flag flag))
48 ((and (boundp 'emacs-major-version) (>= emacs-major-version 20))
49 (list 'set-buffer-multibyte flag))
53 (defvar elmo-work-buf-name " *elmo work*")
54 (defvar elmo-temp-buf-name " *elmo temp*")
56 (or (boundp 'default-enable-multibyte-characters)
57 (defvar default-enable-multibyte-characters (featurep 'mule)
58 "The mock variable except for Emacs 20."))
60 (defun elmo-base64-encode-string (string &optional no-line-break))
61 (defun elmo-base64-decode-string (string))
63 ;; base64 encoding/decoding
65 (fset 'elmo-base64-encode-string
66 (mel-find-function 'mime-encode-string "base64"))
67 (fset 'elmo-base64-decode-string
68 (mel-find-function 'mime-decode-string "base64"))
70 ;; Any Emacsen may have add-name-to-file(), because loadup.el requires it. :-p
71 ;; Check make-symbolic-link() instead. -- 981002 by Fuji
72 (if (fboundp 'make-symbolic-link) ;; xxx
73 (defalias 'elmo-add-name-to-file 'add-name-to-file)
74 (defun elmo-add-name-to-file
75 (filename newname &optional ok-if-already-exists)
76 (copy-file filename newname ok-if-already-exists t)))
78 ;; Nemacs's `read' is different.
79 (static-if (fboundp 'nemacs-version)
80 (defun elmo-read (obj)
83 (or (bobp) (forward-char -1)))))
84 (defalias 'elmo-read 'read))
86 (defmacro elmo-set-work-buf (&rest body)
87 "Execute BODY on work buffer. Work buffer remains."
89 (set-buffer (get-buffer-create elmo-work-buf-name))
90 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
94 (defmacro elmo-bind-directory (dir &rest body)
95 "Set current directory DIR and execute BODY."
96 (` (let ((default-directory (file-name-as-directory (, dir))))
99 (defun elmo-object-load (filename &optional mime-charset no-err)
100 "Load OBJECT from the file specified by FILENAME.
101 File content is decoded with MIME-CHARSET."
102 (if (not (file-readable-p filename))
105 (as-binary-input-file
106 (insert-file-contents filename))
108 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
109 (decode-mime-charset-region (point-min) (point-max) mime-charset))
111 (read (current-buffer))
112 (error (unless no-err
113 (message "Warning: Loading object from %s failed."
115 (elmo-object-save filename nil))
118 (defsubst elmo-save-buffer (filename &optional mime-charset)
119 "Save current buffer to the file specified by FILENAME.
120 Directory of the file is created if it doesn't exist.
121 File content is encoded with MIME-CHARSET."
122 (let ((dir (directory-file-name (file-name-directory filename))))
123 (if (file-directory-p dir)
125 (unless (file-exists-p dir)
126 (elmo-make-directory dir)))
127 (if (file-writable-p filename)
130 ;;; (elmo-set-buffer-multibyte default-enable-multibyte-characters)
131 (encode-mime-charset-region (point-min) (point-max) mime-charset))
132 (as-binary-output-file
133 (write-region (point-min) (point-max) filename nil 'no-msg)))
134 (message (format "%s is not writable." filename)))))
136 (defun elmo-object-save (filename object &optional mime-charset)
137 "Save OBJECT to the file specified by FILENAME.
138 Directory of the file is created if it doesn't exist.
139 File content is encoded with MIME-CHARSET."
141 (prin1 object (current-buffer))
142 ;;;(princ "\n" (current-buffer))
143 (elmo-save-buffer filename mime-charset)))
145 (defun elmo-get-network-stream-type (stream-type stream-type-alist)
147 (while stream-type-alist
148 (if (eq (nth 1 (car stream-type-alist)) stream-type)
149 (throw 'found (car stream-type-alist)))
150 (setq stream-type-alist (cdr stream-type-alist)))))
154 (defconst elmo-condition-atom-regexp "[^/ \")|&]*")
156 (defun elmo-read-search-condition (default)
157 "Read search condition string interactively."
158 (elmo-read-search-condition-internal "Search by" default))
160 (defun elmo-read-search-condition-internal (prompt default)
161 (let* ((completion-ignore-case t)
162 (field (completing-read
163 (format "%s (%s): " prompt default)
167 "From" "Subject" "To" "Cc" "Body"
168 "Since" "Before" "ToCc"
169 "!From" "!Subject" "!To" "!Cc" "!Body"
170 "!Since" "!Before" "!ToCc")
171 elmo-msgdb-extra-fields)) nil t))
173 (setq field (if (string= field "")
177 ((or (string= field "AND") (string= field "OR"))
179 (elmo-read-search-condition-internal
180 (concat field "(1) Search by") default)
181 (if (string= field "AND") "&" "|")
182 (elmo-read-search-condition-internal
183 (concat field "(2) Search by") default)
185 ((string-match "Since\\|Before" field)
186 (concat (downcase field) ":"
187 (completing-read (format "Value for '%s': " field)
190 (list (format "%s" (car x)))))
191 elmo-date-descriptions))))
193 (setq value (read-from-minibuffer (format "Value for '%s': " field)))
194 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
196 (setq value (prin1-to-string value)))
197 (concat (downcase field) ":" value)))))
199 (defsubst elmo-condition-parse-error ()
200 (error "Syntax error in '%s'" (buffer-string)))
202 (defun elmo-parse-search-condition (condition)
204 Return value is a cons cell of (STRUCTURE . REST)"
207 (goto-char (point-min))
208 (cons (elmo-condition-parse) (buffer-substring (point) (point-max)))))
210 ;; condition ::= or-expr
211 (defun elmo-condition-parse ()
212 (or (elmo-condition-parse-or-expr)
213 (elmo-condition-parse-error)))
215 ;; or-expr ::= and-expr /
216 ;; and-expr "|" or-expr
217 (defun elmo-condition-parse-or-expr ()
218 (let ((left (elmo-condition-parse-and-expr)))
219 (if (looking-at "| *")
221 (goto-char (match-end 0))
222 (list 'or left (elmo-condition-parse-or-expr)))
225 ;; and-expr ::= primitive /
226 ;; primitive "&" and-expr
227 (defun elmo-condition-parse-and-expr ()
228 (let ((left (elmo-condition-parse-primitive)))
229 (if (looking-at "& *")
231 (goto-char (match-end 0))
232 (list 'and left (elmo-condition-parse-and-expr)))
235 ;; primitive ::= "(" expr ")" /
236 ;; ["!"] search-key SPACE* ":" SPACE* search-value
237 (defun elmo-condition-parse-primitive ()
240 (goto-char (match-end 0))
241 (prog1 (elmo-condition-parse)
242 (unless (looking-at ") *")
243 (elmo-condition-parse-error))
244 (goto-char (match-end 0))))
245 ;; search-key ::= [A-Za-z-]+
246 ;; ;; "since" / "before" / "last" / "first" /
247 ;; ;; "body" / field-name
248 ((looking-at "\\(!\\)? *\\([A-Za-z-]+\\) *: *")
249 (goto-char (match-end 0))
250 (let ((search-key (vector
251 (if (match-beginning 1) 'unmatch 'match)
252 (elmo-match-buffer 2)
253 (elmo-condition-parse-search-value))))
255 (if (string= (aref search-key 1) "tocc")
256 (if (eq (aref search-key 0) 'match)
258 (vector 'match "to" (aref search-key 2))
259 (vector 'match "cc" (aref search-key 2)))
261 (vector 'unmatch "to" (aref search-key 2))
262 (vector 'unmatch "cc" (aref search-key 2))))
265 ;; search-value ::= quoted / time / number / atom
266 ;; quoted ::= <elisp string expression>
267 ;; time ::= "yesterday" / "lastweek" / "lastmonth" / "lastyear" /
268 ;; number SPACE* "daysago" /
269 ;; number "-" month "-" number ; ex. 10-May-2000
271 ;; month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
272 ;; "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
273 ;; atom ::= ATOM_CHARS*
274 ;; SPACE ::= <ascii space character, 0x20>
275 ;; ATOM_CHARS ::= <any character except specials>
276 ;; specials ::= SPACE / <"> / </> / <)> / <|> / <&>
277 ;; ;; These characters should be quoted.
278 (defun elmo-condition-parse-search-value ()
281 (elmo-read (current-buffer)))
282 ((or (looking-at "yesterday") (looking-at "lastweek")
283 (looking-at "lastmonth") (looking-at "lastyear")
284 (looking-at "[0-9]+ *daysago")
285 (looking-at "[0-9]+-[A-Za-z]+-[0-9]+")
286 (looking-at "[0-9]+")
287 (looking-at elmo-condition-atom-regexp))
288 (prog1 (elmo-match-buffer 0)
289 (goto-char (match-end 0))))
290 (t (error "Syntax error '%s'" (buffer-string)))))
293 (defsubst elmo-buffer-replace (regexp &optional newtext)
294 (goto-char (point-min))
295 (while (re-search-forward regexp nil t)
296 (replace-match (or newtext ""))))
298 (defsubst elmo-delete-char (char string &optional unibyte)
301 (let ((coding-system-for-read 'no-conversion)
302 (coding-system-for-write 'no-conversion))
303 (if unibyte (elmo-set-buffer-multibyte nil))
305 (goto-char (point-min))
306 (while (search-forward (char-to-string char) nil t)
310 (defsubst elmo-delete-cr-buffer ()
311 "Delete CR from buffer."
313 (goto-char (point-min))
314 (while (search-forward "\r\n" nil t)
315 (replace-match "\n")) ))
317 (defsubst elmo-delete-cr-get-content-type ()
319 (goto-char (point-min))
320 (while (search-forward "\r\n" nil t)
321 (replace-match "\n"))
322 (goto-char (point-min))
323 (or (std11-field-body "content-type")
326 (defun elmo-delete-cr (string)
330 (goto-char (point-min))
331 (while (search-forward "\r\n" nil t)
332 (replace-match "\n"))
335 (defun elmo-uniq-list (lst)
336 "Distractively uniqfy elements of LST."
345 (defun elmo-list-insert (list element after)
346 "Insert an ELEMENT to the LIST, just after AFTER."
351 (if (eq (car li) after)
352 (setq p li pn curn li nil)
356 (setcdr (nthcdr pn list) (cons element (cdr p)))
357 (nconc list (list element)))))
359 (defun elmo-string-partial-p (string)
360 (and (stringp string) (string-match "message/partial" string)))
362 (defun elmo-get-file-string (filename &optional remove-final-newline)
364 (let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
365 insert-file-contents-post-hook)
366 (when (file-exists-p filename)
368 (as-binary-input-file (insert-file-contents filename)))
369 (when (and remove-final-newline
371 (= (char-after (1- (point-max))) ?\n))
372 (goto-char (point-max))
373 (delete-backward-char 1))
376 (defun elmo-save-string (string filename)
379 (as-binary-output-file
381 (write-region (point-min) (point-max)
382 filename nil 'no-msg))
385 (defun elmo-max-of-list (nlist)
389 (if (< max-num (car l))
390 (setq max-num (car l)))
394 (defun elmo-concat-path (path filename)
395 (if (not (string= path ""))
396 (if (string= elmo-path-sep (substring path (- (length path) 1)))
397 (concat path filename)
398 (concat path elmo-path-sep filename))
401 (defvar elmo-passwd-alist nil)
403 (defun elmo-passwd-alist-load ()
405 (let ((filename (expand-file-name elmo-passwd-alist-file-name
407 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*"))
408 insert-file-contents-pre-hook ; To avoid autoconv-xmas...
409 insert-file-contents-post-hook
411 (if (not (file-readable-p filename))
413 (set-buffer tmp-buffer)
414 (insert-file-contents filename)
417 (read (current-buffer))
419 (kill-buffer tmp-buffer)
422 (defun elmo-passwd-alist-clear ()
423 "Clear password cache."
425 (setq elmo-passwd-alist nil))
427 (defun elmo-passwd-alist-save ()
428 "Save password into file."
431 (let ((filename (expand-file-name elmo-passwd-alist-file-name
433 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*")))
434 (set-buffer tmp-buffer)
436 (prin1 elmo-passwd-alist tmp-buffer)
437 (princ "\n" tmp-buffer)
438 ;;; (if (and (file-exists-p filename)
439 ;;; (not (equal 384 (file-modes filename))))
440 ;;; (error "%s is not safe.chmod 600 %s!" filename filename))
441 (if (file-writable-p filename)
443 (write-region (point-min) (point-max)
444 filename nil 'no-msg)
445 (set-file-modes filename 384))
446 (message (format "%s is not writable." filename)))
447 (kill-buffer tmp-buffer))))
449 (defun elmo-get-passwd (key)
450 "Get password from password pool."
452 (if (not elmo-passwd-alist)
453 (setq elmo-passwd-alist (elmo-passwd-alist-load)))
454 (setq pair (assoc key elmo-passwd-alist))
456 (elmo-base64-decode-string (cdr pair))
457 (setq pass (elmo-read-passwd (format "Password for %s: "
459 (setq elmo-passwd-alist
460 (append elmo-passwd-alist
462 (elmo-base64-encode-string pass)))))
463 (if elmo-passwd-life-time
464 (run-with-timer elmo-passwd-life-time nil
465 (` (lambda () (elmo-remove-passwd (, key))))))
468 (defun elmo-remove-passwd (key)
469 "Remove password from password pool (for failure)."
471 (if (setq pass-cons (assoc key elmo-passwd-alist))
474 (fillarray (cdr pass-cons) 0))
475 (setq elmo-passwd-alist
476 (delete pass-cons elmo-passwd-alist))))))
478 (defmacro elmo-read-char-exclusive ()
479 (cond ((featurep 'xemacs)
480 '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
485 (key-press-event-p (setq event (next-command-event)))
486 (setq key (or (event-to-character event)
487 (cdr (assq (event-key event) table)))))))
489 ((fboundp 'read-char-exclusive)
490 '(read-char-exclusive))
494 (defun elmo-read-passwd (prompt &optional stars)
495 "Read a single line of text from user without echoing, and return it."
499 (cursor-in-echo-area t)
500 (log-message-max-size 0)
501 message-log-max done msg truncate)
503 (if (or (not stars) (string= "" ans))
505 (setq msg (concat prompt (make-string (length ans) ?.)))
507 (1+ (- (length msg) (window-width (minibuffer-window)))))
509 (setq msg (concat "$" (substring msg (1+ truncate))))))
511 (setq c (elmo-read-char-exclusive))
515 ((or (= c ?\r) (= c ?\n) (= c ?\e))
519 ((and (/= c ?\b) (/= c ?\177))
520 (setq ans (concat ans (char-to-string c))))
522 (setq ans (substring ans 0 -1)))))
531 (defun elmo-string-to-list (string)
534 (goto-char (point-min))
536 (goto-char (point-max))
538 (goto-char (point-min))
539 (read (current-buffer))))
541 (defun elmo-list-to-string (list)
550 (if (symbolp (car tlist))
551 (symbol-name (car tlist))
556 (setq tlist (cdr tlist)))
566 (defun elmo-plug-on-by-servers (alist &optional servers)
567 (let ((server-list (or servers elmo-plug-on-servers)))
570 (if (elmo-plugged-p (car server-list))
572 (setq server-list (cdr server-list))))))
574 (defun elmo-plug-on-by-exclude-servers (alist &optional servers)
575 (let ((server-list (or servers elmo-plug-on-exclude-servers))
576 server other-servers)
578 (when (and (not (member (setq server (caaar alist)) server-list))
579 (not (member server other-servers)))
580 (push server other-servers))
581 (setq alist (cdr alist)))
582 (elmo-plug-on-by-servers alist other-servers)))
584 (defun elmo-plugged-p (&optional server port stream-type alist label-exp)
585 (let ((alist (or alist elmo-plugged-alist))
587 (cond ((and (not port) (not server))
588 (cond ((eq elmo-plugged-condition 'one)
592 (if (nth 2 (car alist))
594 (setq alist (cdr alist))))
596 ((eq elmo-plugged-condition 'all)
600 (if (not (nth 2 (car alist)))
601 (throw 'plugged nil))
602 (setq alist (cdr alist)))
605 ((functionp elmo-plugged-condition)
606 (funcall elmo-plugged-condition alist))
609 ((not port) ;; server
612 (when (string= server (caaar alist))
613 (if (nth 2 (car alist))
615 (setq alist (cdr alist)))))
617 (setq plugged-info (assoc (list server port stream-type) alist))
618 (if (not plugged-info)
619 ;; add elmo-plugged-alist automatically
621 (elmo-set-plugged elmo-plugged server port stream-type
622 nil nil nil label-exp)
624 (if (and elmo-auto-change-plugged
625 (> elmo-auto-change-plugged 0)
626 (nth 3 plugged-info) ;; time
627 (elmo-time-expire (nth 3 plugged-info)
628 elmo-auto-change-plugged))
630 (nth 2 plugged-info)))))))
632 (defun elmo-set-plugged (plugged &optional server port stream-type time
634 (let ((alist (or alist elmo-plugged-alist))
636 (cond ((and (not port) (not server))
637 (setq elmo-plugged plugged)
638 ;; set plugged all element of elmo-plugged-alist.
640 (setcdr (cdar alist) (list plugged time))
641 (setq alist (cdr alist))))
643 ;; set plugged all port of server
645 (when (string= server (caaar alist))
646 (setcdr (cdar alist) (list plugged time)))
647 (setq alist (cdr alist))))
649 ;; set plugged one port of server
650 (setq plugged-info (assoc (list server port stream-type) alist))
651 (setq label (if label-exp
653 (nth 1 plugged-info)))
655 ;; if add is non-nil, don't reset plug state.
657 (setcdr plugged-info (list label plugged time)))
659 (setq elmo-plugged-alist
663 (list (list server port stream-type)
664 label plugged time))))))))
667 (defun elmo-delete-plugged (&optional server port alist)
668 (let* ((alist (or alist elmo-plugged-alist))
670 (cond ((and (not port) (not server))
673 ;; delete plugged all port of server
675 (when (string= server (caaar alist2))
676 (setq alist (delete (car alist2) alist)))
677 (setq alist2 (cdr alist2))))
679 ;; delete plugged one port of server
681 (delete (assoc (cons server port) alist) alist))))
684 (defun elmo-disk-usage (path)
685 "Get disk usage (bytes) in PATH."
687 (condition-case () (file-attributes path) (error nil))))
689 (if (nth 0 file-attr) ; directory
690 (let ((files (condition-case ()
691 (directory-files path t "^[^\\.]")
694 ;; (result (nth 7 file-attr))) ... directory size
696 (setq result (+ result (or (elmo-disk-usage (car files)) 0)))
697 (setq files (cdr files)))
699 (float (nth 7 file-attr))))))
701 (defun elmo-get-last-accessed-time (path &optional dir)
702 "Return the last accessed time of PATH."
703 (let ((last-accessed (nth 4 (file-attributes (or (and dir
708 (setq last-accessed (+ (* (nth 0 last-accessed)
709 (float 65536)) (nth 1 last-accessed)))
712 (defun elmo-get-last-modification-time (path &optional dir)
713 "Return the last accessed time of PATH."
714 (let ((last-modified (nth 5 (file-attributes (or (and dir
718 (setq last-modified (+ (* (nth 0 last-modified)
719 (float 65536)) (nth 1 last-modified)))))
721 (defun elmo-make-directory (path)
722 "Create directory recursively."
723 (let ((parent (directory-file-name (file-name-directory path))))
724 (if (null (file-directory-p parent))
725 (elmo-make-directory parent))
726 (make-directory path)
727 (if (string= path (expand-file-name elmo-msgdb-dir))
728 (set-file-modes path (+ (* 64 7) (* 8 0) 0))))) ; chmod 0700
730 (defun elmo-delete-directory (path &optional no-hierarchy)
731 "Delete directory recursively."
732 (if (stringp path) ; nil is not permitted.
733 (let ((dirent (directory-files path))
734 relpath abspath hierarchy)
736 (setq relpath (car dirent)
738 abspath (expand-file-name relpath path))
739 (when (not (string-match "^\\.\\.?$" relpath))
740 (if (eq (nth 0 (file-attributes abspath)) t)
743 (elmo-delete-directory abspath no-hierarchy))
744 (delete-file abspath))))
746 (delete-directory path)))))
748 (defun elmo-list-filter (l1 l2)
751 ;; t means filter all.
754 (elmo-delete-if (lambda (x) (not (memq x l1))) l2)
758 (defsubst elmo-list-delete-if-smaller (list number)
759 (let ((ret-val (copy-sequence list)))
761 (if (< (car list) number)
762 (setq ret-val (delq (car list) ret-val)))
763 (setq list (cdr list)))
766 (defun elmo-list-diff (list1 list2 &optional mes)
769 (let ((clist1 (copy-sequence list1))
770 (clist2 (copy-sequence list2)))
772 (setq clist1 (delq (car list2) clist1))
773 (setq list2 (cdr list2)))
775 (setq clist2 (delq (car list1) clist2))
776 (setq list1 (cdr list1)))
778 (message (concat mes "done.")))
779 (list clist1 clist2)))
781 (defun elmo-list-bigger-diff (list1 list2 &optional mes)
782 "Returns a list (- +). + is bigger than max of LIST1, in LIST2."
787 (max-of-l2 (or (nth (max 0 (1- (length l2))) l2) 0))
791 (setq num (+ (length l1)))
793 (if (memq (car l1) l2)
794 (if (eq (car l1) (car l2))
797 (if (> (car l1) max-of-l2)
798 (setq diff1 (nconc diff1 (list (car l1))))))
802 (setq percent (/ (* i 100) num))
803 (if (eq (% percent 5) 0)
804 (elmo-display-progress
805 'elmo-list-bigger-diff "%s%d%%" percent mes))))
807 (cons diff1 (list l2)))))
809 (defmacro elmo-filter-type (filter)
810 (` (aref (, filter) 0)))
812 (defmacro elmo-filter-key (filter)
813 (` (aref (, filter) 1)))
815 (defmacro elmo-filter-value (filter)
816 (` (aref (, filter) 2)))
818 (defsubst elmo-buffer-field-primitive-condition-match (condition
822 (goto-char (point-min))
824 ((string= (elmo-filter-key condition) "last")
825 (setq result (<= (length (memq number number-list))
826 (string-to-int (elmo-filter-value condition)))))
827 ((string= (elmo-filter-key condition) "first")
828 (setq result (< (- (length number-list)
829 (length (memq number number-list)))
830 (string-to-int (elmo-filter-value condition)))))
831 ((string= (elmo-filter-key condition) "since")
832 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
835 (timezone-make-sortable-date (aref date 0)
838 (timezone-make-time-string
842 (timezone-make-date-sortable (std11-field-body "date"))))))
843 ((string= (elmo-filter-key condition) "before")
844 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
847 (timezone-make-date-sortable (std11-field-body "date"))
848 (timezone-make-sortable-date (aref date 0)
851 (timezone-make-time-string
855 ((string= (elmo-filter-key condition) "body")
856 (and (re-search-forward "^$" nil t) ; goto body
857 (setq result (search-forward (elmo-filter-value condition)
860 (let ((fval (std11-field-body (elmo-filter-key condition))))
861 (if (eq (length fval) 0) (setq fval nil))
862 (if fval (setq fval (eword-decode-string fval)))
863 (setq result (and fval (string-match
864 (elmo-filter-value condition) fval))))))
865 (if (eq (elmo-filter-type condition) 'unmatch)
866 (setq result (not result)))
869 (defun elmo-condition-find-key-internal (condition key)
872 (if (string= (elmo-filter-key condition) key)
874 ((or (eq (car condition) 'and)
875 (eq (car condition) 'or))
876 (elmo-condition-find-key-internal (nth 1 condition) key)
877 (elmo-condition-find-key-internal (nth 2 condition) key))))
879 (defun elmo-condition-find-key (condition key)
881 (elmo-condition-find-key-internal condition key)))
883 (defun elmo-buffer-field-condition-match (condition number number-list)
886 (elmo-buffer-field-primitive-condition-match
887 condition number number-list))
888 ((eq (car condition) 'and)
889 (and (elmo-buffer-field-condition-match
890 (nth 1 condition) number number-list)
891 (elmo-buffer-field-condition-match
892 (nth 2 condition) number number-list)))
893 ((eq (car condition) 'or)
894 (or (elmo-buffer-field-condition-match
895 (nth 1 condition) number number-list)
896 (elmo-buffer-field-condition-match
897 (nth 2 condition) number number-list)))))
899 (defsubst elmo-file-field-condition-match (file condition number number-list)
901 (as-binary-input-file (insert-file-contents file))
902 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
903 ;; Should consider charset?
904 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
905 (elmo-buffer-field-condition-match condition number number-list)))
907 (defmacro elmo-get-hash-val (string hashtable)
908 (let ((sym (list 'intern-soft string hashtable)))
909 (list 'if (list 'boundp sym)
910 (list 'symbol-value sym))))
912 (defmacro elmo-set-hash-val (string value hashtable)
913 (list 'set (list 'intern string hashtable) value))
915 (defmacro elmo-clear-hash-val (string hashtable)
916 (static-if (fboundp 'unintern)
917 (list 'unintern string hashtable)
918 (list 'makunbound (list 'intern string hashtable))))
920 (defmacro elmo-unintern (string)
921 "`unintern' symbol named STRING, When can use `unintern'.
922 Emacs 19.28 or earlier does not have `unintern'."
923 (static-if (fboundp 'unintern)
924 (list 'unintern string)))
926 (defun elmo-make-hash (&optional hashsize)
927 "Make a new hash table which have HASHSIZE size."
931 ;; Prime numbers as lengths tend to result in good
932 ;; hashing; lengths one less than a power of two are
936 (while (< (- i 1) hashsize)
939 elmo-hash-maximum-size)
940 elmo-hash-minimum-size)
941 elmo-hash-minimum-size)
944 (defsubst elmo-mime-string (string)
945 "Normalize MIME encoded STRING."
949 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
950 (setq str (eword-decode-string
951 (decode-mime-charset-string string elmo-mime-charset)))
952 (setq str (encode-mime-charset-string str elmo-mime-charset))
953 (elmo-set-buffer-multibyte nil)
956 (defsubst elmo-collect-field (beg end downcase-field-name)
959 (narrow-to-region beg end)
960 (goto-char (point-min))
961 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
963 (while (re-search-forward regexp nil t)
964 (setq name (buffer-substring-no-properties
965 (match-beginning 1)(1- (match-end 1))))
966 (if downcase-field-name
967 (setq name (downcase name)))
968 (setq body (buffer-substring-no-properties
969 (match-end 0) (std11-field-end)))
970 (or (assoc name dest)
971 (setq dest (cons (cons name body) dest))))
974 (defsubst elmo-collect-field-from-string (string downcase-field-name)
977 (goto-char (point-min))
978 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
980 (while (re-search-forward regexp nil t)
981 (setq name (buffer-substring-no-properties
982 (match-beginning 1)(1- (match-end 1))))
983 (if downcase-field-name
984 (setq name (downcase name)))
985 (setq body (buffer-substring-no-properties
986 (match-end 0) (std11-field-end)))
987 (or (assoc name dest)
988 (setq dest (cons (cons name body) dest))))
991 (defun elmo-safe-filename (folder)
992 (elmo-replace-in-string
993 (elmo-replace-in-string
994 (elmo-replace-in-string folder "/" " ")
998 (defvar elmo-filename-replace-chars nil)
1000 (defsubst elmo-replace-string-as-filename (msgid)
1001 "Replace string as filename."
1002 (setq msgid (elmo-replace-in-string msgid " " " "))
1003 (if (null elmo-filename-replace-chars)
1004 (setq elmo-filename-replace-chars
1005 (regexp-quote (mapconcat
1006 'car elmo-filename-replace-string-alist ""))))
1007 (while (string-match (concat "[" elmo-filename-replace-chars "]")
1010 (substring msgid 0 (match-beginning 0))
1013 (match-beginning 0) (match-end 0))
1014 elmo-filename-replace-string-alist))
1015 (substring msgid (match-end 0)))))
1018 (defsubst elmo-recover-string-from-filename (filename)
1019 "Recover string from FILENAME."
1021 (while (string-match " " filename)
1022 (setq tmp (substring filename
1024 (+ (match-end 0) 1)))
1025 (if (string= tmp " ")
1027 (setq tmp (car (rassoc tmp
1028 elmo-filename-replace-string-alist))))
1031 (substring filename 0 (match-beginning 0))
1033 (setq filename (substring filename (+ (match-end 0) 1))))
1034 (concat result filename)))
1036 (defsubst elmo-copy-file (src dst)
1038 (elmo-add-name-to-file src dst t)
1039 (error (copy-file src dst t))))
1041 (defsubst elmo-buffer-exists-p (buffer)
1042 (if (bufferp buffer)
1043 (buffer-live-p buffer)
1044 (get-buffer buffer)))
1046 (defsubst elmo-kill-buffer (buffer)
1047 (when (elmo-buffer-exists-p buffer)
1048 (kill-buffer buffer)))
1050 (defun elmo-delete-if (pred lst)
1051 "Return new list contain items which don't satisfy PRED in LST."
1054 (unless (funcall pred (car lst))
1055 (setq result (nconc result (list (car lst)))))
1056 (setq lst (cdr lst)))
1059 (defun elmo-list-delete (list1 list2)
1060 "Delete by side effect any occurrences equal to elements of LIST1 from LIST2.
1061 Return the modified LIST2. Deletion is done with `delete'.
1062 Write `(setq foo (elmo-list-delete bar foo))' to be sure of changing
1063 the value of `foo'."
1065 (setq list2 (delete (car list1) list2))
1066 (setq list1 (cdr list1)))
1069 (defun elmo-list-member (list1 list2)
1070 "If any element of LIST1 is member of LIST2, return t."
1073 (if (member (car list1) list2)
1075 (setq list1 (cdr list1)))))
1077 (defun elmo-count-matches (regexp beg end)
1081 (while (re-search-forward regexp end t)
1082 (setq count (1+ count)))
1085 (if (fboundp 'display-error)
1086 (defalias 'elmo-display-error 'display-error)
1087 (defun elmo-display-error (error-object stream)
1088 "A tiny function to display ERROR-OBJECT to the STREAM."
1090 (errobj error-object)
1093 (setq err-mes (concat err-mes (format
1094 (if (stringp (car errobj))
1096 (if (boundp 'nemacs-version)
1098 "%S")) (car errobj))))
1099 (setq errobj (cdr errobj))
1100 (if errobj (setq err-mes (concat err-mes (if first ": " ", "))))
1102 (princ err-mes stream))))
1104 (if (fboundp 'define-error)
1105 (defalias 'elmo-define-error 'define-error)
1106 (defun elmo-define-error (error doc &optional parents)
1108 (setq parents 'error))
1109 (let ((conds (get parents 'error-conditions)))
1111 (error "Not an error symbol: %s" error))
1113 (list 'error-message doc
1114 'error-conditions (cons error conds))))))
1116 (cond ((fboundp 'progress-feedback-with-label)
1117 (defalias 'elmo-display-progress 'progress-feedback-with-label))
1118 ((fboundp 'lprogress-display)
1119 (defalias 'elmo-display-progress 'lprogress-display))
1121 (defun elmo-display-progress (label format &optional value &rest args)
1122 "Print a progress message."
1123 (if (and (null format) (null args))
1125 (apply (function message) (concat format " %d%%")
1126 (nconc args (list value)))))))
1128 (defun elmo-time-expire (before-time diff-time)
1129 (let* ((current (current-time))
1130 (rest (when (< (nth 1 current) (nth 1 before-time))
1134 (list (- (+ (car current) (if rest -1 0)) (car before-time))
1135 (- (+ (or rest 0) (nth 1 current)) (nth 1 before-time))))
1136 (and (eq (car diff) 0)
1137 (< diff-time (nth 1 diff)))))
1139 (if (fboundp 'std11-fetch-field)
1140 (defalias 'elmo-field-body 'std11-fetch-field) ;;no narrow-to-region
1141 (defalias 'elmo-field-body 'std11-field-body))
1143 (defmacro elmo-string (string)
1144 "STRING without text property."
1145 (` (let ((obj (copy-sequence (, string))))
1146 (set-text-properties 0 (length obj) nil obj)
1149 (defun elmo-flatten (list-of-list)
1150 "Flatten LIST-OF-LIST."
1151 (unless (null list-of-list)
1152 (append (if (and (car list-of-list)
1153 (listp (car list-of-list)))
1155 (list (car list-of-list)))
1156 (elmo-flatten (cdr list-of-list)))))
1158 (defun elmo-y-or-n-p (prompt &optional auto default)
1159 "Same as `y-or-n-p'.
1160 But if optional argument AUTO is non-nil, DEFAULT is returned."
1165 (defun elmo-string-member (string slist)
1166 "Return t if STRING is a member of the SLIST."
1169 (if (and (stringp (car slist))
1170 (string= string (car slist)))
1172 (setq slist (cdr slist)))))
1174 (defun elmo-string-match-member (str list &optional case-ignore)
1175 (let ((case-fold-search case-ignore))
1178 (if (string-match (car list) str)
1179 (throw 'member (car list)))
1180 (setq list (cdr list))))))
1182 (defun elmo-string-matched-member (str list &optional case-ignore)
1183 (let ((case-fold-search case-ignore))
1186 (if (string-match str (car list))
1187 (throw 'member (car list)))
1188 (setq list (cdr list))))))
1190 (defsubst elmo-string-delete-match (string pos)
1191 (concat (substring string
1192 0 (match-beginning pos))
1197 (defun elmo-string-match-assoc (key alist &optional case-ignore)
1198 (let ((case-fold-search case-ignore)
1202 (setq a (car alist))
1205 (string-match key (car a)))
1207 (setq alist (cdr alist))))))
1209 (defun elmo-string-matched-assoc (key alist &optional case-ignore)
1210 (let ((case-fold-search case-ignore)
1214 (setq a (car alist))
1217 (string-match (car a) key))
1219 (setq alist (cdr alist))))))
1221 (defun elmo-string-assoc (key alist)
1225 (setq a (car alist))
1228 (string= key (car a)))
1230 (setq alist (cdr alist))))))
1232 (defun elmo-string-rassoc (key alist)
1236 (setq a (car alist))
1239 (string= key (cdr a)))
1241 (setq alist (cdr alist))))))
1243 (defun elmo-string-rassoc-all (key alist)
1246 (if (string= key (cdr (car alist)))
1250 (setq alist (cdr alist)))
1253 ;;; Number set defined by OKAZAKI Tetsurou <okazaki@be.to>
1255 ;; number ::= [0-9]+
1258 ;; number-range ::= "(" beg " . " end ")" ;; cons cell
1259 ;; number-set-elem ::= number / number-range
1260 ;; number-set ::= "(" *number-set-elem ")" ;; list
1262 (defun elmo-number-set-member (number number-set)
1263 "Return non-nil if NUMBER is an element of NUMBER-SET.
1264 The value is actually the tail of NUMBER-RANGE whose car contains NUMBER."
1265 (or (memq number number-set)
1267 (while (and number-set (not found))
1268 (if (and (consp (car number-set))
1269 (and (<= (car (car number-set)) number)
1270 (<= number (cdr (car number-set)))))
1272 (setq number-set (cdr number-set))))
1275 (defun elmo-number-set-append-list (number-set list)
1276 "Append LIST of numbers to the NUMBER-SET.
1277 NUMBER-SET is altered."
1278 (let ((appended number-set))
1280 (setq appended (elmo-number-set-append appended (car list)))
1281 (setq list (cdr list)))
1284 (defun elmo-number-set-append (number-set number)
1285 "Append NUMBER to the NUMBER-SET.
1286 NUMBER-SET is altered."
1287 (let ((number-set-1 number-set)
1289 (while (and number-set (not found))
1290 (setq elem (car number-set))
1293 (eq (+ 1 (cdr elem)) number))
1294 (setcdr elem number)
1296 ((and (integerp elem)
1297 (eq (+ 1 elem) number))
1298 (setcar number-set (cons elem number))
1300 ((or (and (integerp elem) (eq elem number))
1302 (<= (car elem) number)
1303 (<= number (cdr elem))))
1305 (setq number-set (cdr number-set)))
1307 (setq number-set-1 (nconc number-set-1 (list number))))
1310 (defun elmo-number-set-to-number-list (number-set)
1311 "Return a number list which corresponds to NUMBER-SET."
1312 (let (number-list elem i)
1314 (setq elem (car number-set))
1318 (while (<= i (cdr elem))
1319 (setq number-list (cons i number-list))
1322 (setq number-list (cons elem number-list))))
1323 (setq number-set (cdr number-set)))
1324 (nreverse number-list)))
1326 (defcustom elmo-list-subdirectories-ignore-regexp "^\\(\\.\\.?\\|[0-9]+\\)$"
1327 "*Regexp to filter subfolders."
1331 (defun elmo-list-subdirectories-1 (basedir curdir one-level)
1332 (let ((root (zerop (length curdir)))
1333 (w32-get-true-file-link-count t) ; for Meadow
1336 (dolist (file (directory-files (setq dir (expand-file-name curdir basedir))))
1337 (when (and (not (string-match
1338 elmo-list-subdirectories-ignore-regexp
1340 (car (setq attr (file-attributes
1341 (expand-file-name file dir)))))
1342 (when (eq one-level 'check) (throw 'done t))
1344 (concat curdir (and (not root) elmo-path-sep) file))
1346 (setq dirs (nconc dirs
1347 (if (if elmo-have-link-count (< 2 (nth 1 attr))
1349 (elmo-list-subdirectories-1
1352 (if one-level 'check))))
1354 (list (list relpath))
1357 (elmo-list-subdirectories-1
1361 (list relpath)))))))
1364 (defun elmo-list-subdirectories (directory file one-level)
1365 (let ((subdirs (elmo-list-subdirectories-1 directory file one-level)))
1366 (if (zerop (length file))
1368 (cons file subdirs))))
1370 (defun elmo-mapcar-list-of-list (func list-of-list)
1373 (cond ((listp x) (elmo-mapcar-list-of-list func x))
1374 (t (funcall func x))))
1377 (defun elmo-parse (string regexp &optional matchn)
1378 (or matchn (setq matchn 1))
1380 (store-match-data nil)
1381 (while (string-match regexp string (match-end 0))
1382 (setq list (cons (substring string (match-beginning matchn)
1383 (match-end matchn)) list)))
1387 (defmacro elmo-make-file-cache (path status)
1388 "PATH is the cache file name.
1389 STATUS is one of 'section, 'entire or nil.
1390 nil means no cache exists.
1391 'section means partial section cache exists.
1392 'entire means entire cache exists.
1393 If the cache is partial file-cache, TYPE is 'partial."
1394 (` (cons (, path) (, status))))
1396 (defmacro elmo-file-cache-path (file-cache)
1397 "Returns the file path of the FILE-CACHE."
1398 (` (car (, file-cache))))
1400 (defmacro elmo-file-cache-status (file-cache)
1401 "Returns the status of the FILE-CACHE."
1402 (` (cdr (, file-cache))))
1404 (defsubst elmo-cache-to-msgid (filename)
1405 (concat "<" (elmo-recover-string-from-filename filename) ">"))
1407 (defsubst elmo-cache-get-path-subr (msgid)
1408 (let ((chars '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?A ?B ?C ?D ?E ?F))
1409 (clist (string-to-char-list msgid))
1412 (setq sum (+ sum (car clist)))
1413 (setq clist (cdr clist)))
1415 (nth (% (/ sum 16) 2) chars)
1416 (nth (% sum 16) chars))))
1418 (defun elmo-file-cache-get-path (msgid &optional section)
1419 "Get cache path for MSGID.
1420 If optional argument SECTION is specified, partial cache path is returned."
1421 (if (setq msgid (elmo-msgid-to-cache msgid))
1424 (format "%s/%s/%s/%s/%s"
1427 (elmo-cache-get-path-subr msgid)
1430 (format "%s/%s/%s/%s"
1433 (elmo-cache-get-path-subr msgid)
1436 (defmacro elmo-file-cache-expand-path (path section)
1437 "Return file name for the file-cache corresponds to the section.
1438 PATH is the file-cache path.
1439 SECTION is the section string."
1440 (` (expand-file-name (or (, section) "") (, path))))
1442 (defun elmo-file-cache-delete (path)
1443 "Delete a cache on PATH."
1445 (when (file-exists-p path)
1446 (if (file-directory-p path)
1448 (setq files (directory-files path t "^[^\\.]"))
1450 (delete-file (car files))
1451 (setq files (cdr files)))
1452 (delete-directory path))
1453 (delete-file path)))))
1455 (defun elmo-file-cache-exists-p (msgid)
1456 "Returns 'section or 'entire if a cache which corresponds to MSGID exists."
1457 (elmo-file-cache-status (elmo-file-cache-get msgid)))
1459 (defun elmo-file-cache-save (cache-path section)
1460 "Save current buffer as cache on PATH.
1461 Return t if cache is saved successfully."
1463 (let ((path (if section (expand-file-name section cache-path)
1466 (if (and (null section)
1467 (file-directory-p path))
1469 (setq files (directory-files path t "^[^\\.]"))
1471 (delete-file (car files))
1472 (setq files (cdr files)))
1473 (delete-directory path))
1475 (not (file-directory-p cache-path)))
1476 (delete-file cache-path)))
1478 (setq dir (directory-file-name (file-name-directory path)))
1479 (if (not (file-exists-p dir))
1480 (elmo-make-directory dir))
1481 (write-region-as-binary (point-min) (point-max)
1487 (defun elmo-file-cache-get (msgid &optional section)
1488 "Returns the current file-cache object associated with MSGID.
1489 MSGID is the message-id of the message.
1490 If optional argument SECTION is specified, get partial file-cache object
1491 associated with SECTION."
1493 (let ((path (elmo-cache-get-path msgid)))
1494 (if (and path (file-exists-p path))
1495 (if (file-directory-p path)
1497 (if (file-exists-p (setq path (expand-file-name
1499 (cons path 'section))
1500 ;; section is not specified but sectional.
1501 (cons path 'section))
1504 (cons path 'entire)))
1511 (defun elmo-cache-expire ()
1513 (let* ((completion-ignore-case t)
1514 (method (completing-read (format "Expire by (%s): "
1515 elmo-cache-expire-default-method)
1518 (if (string= method "")
1519 (setq method elmo-cache-expire-default-method))
1520 (funcall (intern (concat "elmo-cache-expire-by-" method)))))
1522 (defun elmo-read-float-value-from-minibuffer (prompt &optional initial)
1523 (let ((str (read-from-minibuffer prompt initial)))
1525 ((string-match "[0-9]*\\.[0-9]+" str)
1526 (string-to-number str))
1527 ((string-match "[0-9]+" str)
1528 (string-to-number (concat str ".0")))
1529 (t (error "%s is not number" str)))))
1531 (defun elmo-cache-expire-by-size (&optional kbytes)
1532 "Expire cache file by size.
1533 If KBYTES is kilo bytes (This value must be float)."
1535 (let ((size (or kbytes
1536 (and (interactive-p)
1537 (elmo-read-float-value-from-minibuffer
1538 "Enter cache disk size (Kbytes): "
1540 (if (integerp elmo-cache-expire-default-size)
1541 (float elmo-cache-expire-default-size)
1542 elmo-cache-expire-default-size))))
1543 (if (integerp elmo-cache-expire-default-size)
1544 (float elmo-cache-expire-default-size))))
1548 (message "Checking disk usage...")
1549 (setq total (/ (elmo-disk-usage
1551 elmo-cache-dirname elmo-msgdb-dir)) Kbytes))
1552 (setq beginning total)
1553 (message "Checking disk usage...done")
1554 (let ((cfl (elmo-cache-get-sorted-cache-file-list))
1558 (while (and (<= size total)
1559 (setq oldest (elmo-cache-get-oldest-cache-file-entity cfl)))
1560 (setq cur-file (expand-file-name (car (cdr oldest)) (car oldest)))
1561 (setq cur-size (/ (elmo-disk-usage cur-file) Kbytes))
1562 (when (elmo-file-cache-delete cur-file)
1563 (setq count (+ count 1))
1564 (message "%d cache(s) are expired." count))
1565 (setq deleted (+ deleted cur-size))
1566 (setq total (- total cur-size)))
1567 (message "%d cache(s) are expired from disk (%d Kbytes/%d Kbytes)."
1568 count deleted beginning))))
1570 (defun elmo-cache-make-file-entity (filename path)
1571 (cons filename (elmo-get-last-accessed-time filename path)))
1573 (defun elmo-cache-get-oldest-cache-file-entity (cache-file-list)
1574 (let ((cfl cache-file-list)
1575 flist firsts oldest-entity wonlist)
1577 (setq flist (cdr (car cfl)))
1578 (setq firsts (append firsts (list
1579 (cons (car (car cfl))
1581 (setq cfl (cdr cfl)))
1584 (if (and (not oldest-entity)
1585 (cdr (cdr (car firsts))))
1586 (setq oldest-entity (car firsts)))
1587 (if (and (cdr (cdr (car firsts)))
1588 (cdr (cdr oldest-entity))
1589 (> (cdr (cdr oldest-entity)) (cdr (cdr (car firsts)))))
1590 (setq oldest-entity (car firsts)))
1591 (setq firsts (cdr firsts)))
1592 (setq wonlist (assoc (car oldest-entity) cache-file-list))
1594 (setcdr wonlist (delete (car (cdr wonlist)) (cdr wonlist))))
1597 (defun elmo-cache-get-sorted-cache-file-list ()
1598 (let ((dirs (directory-files
1599 (expand-file-name elmo-cache-dirname elmo-msgdb-dir)
1604 (setq num (length dirs))
1605 (message "Collecting cache info...")
1607 (setq elist (mapcar (lambda (x)
1608 (elmo-cache-make-file-entity x (car dirs)))
1609 (directory-files (car dirs) nil "^[^\\.]")))
1610 (setq ret-val (append ret-val
1618 (when (> num elmo-display-progress-threshold)
1620 (elmo-display-progress
1621 'elmo-cache-get-sorted-cache-file-list "Collecting cache info..."
1623 (setq dirs (cdr dirs)))
1624 (message "Collecting cache info...done")
1627 (defun elmo-cache-expire-by-age (&optional days)
1628 (let ((age (or (and days (int-to-string days))
1629 (and (interactive-p)
1630 (read-from-minibuffer
1631 (format "Enter days (%s): "
1632 elmo-cache-expire-default-age)))
1633 (int-to-string elmo-cache-expire-default-age)))
1634 (dirs (directory-files
1635 (expand-file-name elmo-cache-dirname elmo-msgdb-dir)
1639 (if (string= age "")
1640 (setq age elmo-cache-expire-default-age)
1641 (setq age (string-to-int age)))
1642 (setq curtime (current-time))
1643 (setq curtime (+ (* (nth 0 curtime)
1644 (float 65536)) (nth 1 curtime)))
1646 (let ((files (directory-files (car dirs) t "^[^\\.]"))
1647 (limit-age (* age 86400)))
1649 (when (> (- curtime (elmo-get-last-accessed-time (car files)))
1651 (when (elmo-file-cache-delete (car files))
1652 (setq count (+ 1 count))
1653 (message "%d cache file(s) are expired." count)))
1654 (setq files (cdr files))))
1655 (setq dirs (cdr dirs)))))
1659 (defun elmo-msgid-to-cache (msgid)
1661 (string-match "<\\(.+\\)>$" msgid))
1662 (elmo-replace-string-as-filename (elmo-match-string 1 msgid))))
1664 (defun elmo-cache-get-path (msgid &optional folder number)
1665 "Get path for cache file associated with MSGID, FOLDER, and NUMBER."
1666 (if (setq msgid (elmo-msgid-to-cache msgid))
1670 (format "%s/%s/%s@%s"
1671 (elmo-cache-get-path-subr msgid)
1674 (elmo-safe-filename folder))
1676 (elmo-cache-get-path-subr msgid)
1678 (expand-file-name elmo-cache-dirname
1684 (defconst elmo-warning-buffer-name "*elmo warning*")
1686 (defun elmo-warning (&rest args)
1687 "Display a warning, making warning message by passing all args to `insert'."
1688 (with-current-buffer (get-buffer-create elmo-warning-buffer-name)
1689 (goto-char (point-max))
1690 (apply 'insert (append args '("\n")))
1692 (display-buffer elmo-warning-buffer-name))
1694 (defvar elmo-obsolete-variable-alist nil)
1695 (defvar elmo-obsolete-variable-show-warnings nil)
1697 (defun elmo-define-obsolete-variable (obsolete var)
1698 "Define obsolete variable.
1699 OBSOLETE is a symbol for obsolete variable.
1700 VAR is a symbol for new variable.
1701 Definition is stored in `elmo-obsolete-variable-alist'."
1702 (let ((pair (assq var elmo-obsolete-variable-alist)))
1704 (setcdr pair obsolete)
1705 (setq elmo-obsolete-variable-alist
1706 (cons (cons var obsolete)
1707 elmo-obsolete-variable-alist)))))
1709 (defun elmo-resque-obsolete-variable (obsolete var)
1710 "Resque obsolete variable OBSOLETE as VAR.
1711 If `elmo-obsolete-variable-show-warnings' is non-nil, show warning message."
1712 (when (boundp obsolete)
1713 (static-if (and (fboundp 'defvaralias)
1714 (subrp (symbol-function 'defvaralias)))
1715 (defvaralias var obsolete)
1716 (set var (symbol-value obsolete)))
1717 (if elmo-obsolete-variable-show-warnings
1718 (elmo-warning (format "%s is obsolete. Use %s instead."
1719 (symbol-name obsolete)
1720 (symbol-name var))))))
1722 (defun elmo-resque-obsolete-variables (&optional alist)
1723 "Resque obsolete variables in ALIST.
1724 ALIST is a list of cons cell of
1725 \(OBSOLETE-VARIABLE-SYMBOL . NEW-VARIABLE-SYMBOL\).
1726 If ALIST is nil, `elmo-obsolete-variable-alist' is used."
1727 (dolist (pair elmo-obsolete-variable-alist)
1728 (elmo-resque-obsolete-variable (cdr pair)
1733 (product-provide (provide 'elmo-util) (require 'elmo-version))
1735 ;;; elmo-util.el ends here