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 (let (print-length print-level)
136 (prin1 object (current-buffer)))
137 ;;;(princ "\n" (current-buffer))
138 (elmo-save-buffer filename mime-charset)))
142 (defconst elmo-condition-atom-regexp "[^/ \")|&]*")
144 (defun elmo-read-search-condition (default)
145 "Read search condition string interactively."
146 (elmo-read-search-condition-internal "Search by" default))
148 (defun elmo-read-search-condition-internal (prompt default)
149 (let* ((completion-ignore-case t)
150 (field (completing-read
151 (format "%s (%s): " prompt default)
155 "From" "Subject" "To" "Cc" "Body"
156 "Since" "Before" "ToCc"
157 "!From" "!Subject" "!To" "!Cc" "!Body"
158 "!Since" "!Before" "!ToCc")
159 elmo-msgdb-extra-fields))))
161 (setq field (if (string= field "")
165 ((or (string= field "AND") (string= field "OR"))
167 (elmo-read-search-condition-internal
168 (concat field "(1) Search by") default)
169 (if (string= field "AND") "&" "|")
170 (elmo-read-search-condition-internal
171 (concat field "(2) Search by") default)
173 ((string-match "Since\\|Before" field)
174 (let ((default (format-time-string "%Y-%m-%d")))
175 (setq value (completing-read
176 (format "Value for '%s' [%s]: " field default)
179 (list (format "%s" (car x)))))
180 elmo-date-descriptions)))
181 (concat (downcase field) ":"
182 (if (equal value "") default value))))
184 (setq value (read-from-minibuffer (format "Value for '%s': " field)))
185 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
187 (setq value (prin1-to-string value)))
188 (concat (downcase field) ":" value)))))
190 (defsubst elmo-condition-parse-error ()
191 (error "Syntax error in '%s'" (buffer-string)))
193 (defun elmo-parse-search-condition (condition)
195 Return value is a cons cell of (STRUCTURE . REST)"
198 (goto-char (point-min))
199 (cons (elmo-condition-parse) (buffer-substring (point) (point-max)))))
201 ;; condition ::= or-expr
202 (defun elmo-condition-parse ()
203 (or (elmo-condition-parse-or-expr)
204 (elmo-condition-parse-error)))
206 ;; or-expr ::= and-expr /
207 ;; and-expr "|" or-expr
208 (defun elmo-condition-parse-or-expr ()
209 (let ((left (elmo-condition-parse-and-expr)))
210 (if (looking-at "| *")
212 (goto-char (match-end 0))
213 (list 'or left (elmo-condition-parse-or-expr)))
216 ;; and-expr ::= primitive /
217 ;; primitive "&" and-expr
218 (defun elmo-condition-parse-and-expr ()
219 (let ((left (elmo-condition-parse-primitive)))
220 (if (looking-at "& *")
222 (goto-char (match-end 0))
223 (list 'and left (elmo-condition-parse-and-expr)))
226 ;; primitive ::= "(" expr ")" /
227 ;; ["!"] search-key SPACE* ":" SPACE* search-value
228 (defun elmo-condition-parse-primitive ()
231 (goto-char (match-end 0))
232 (prog1 (elmo-condition-parse)
233 (unless (looking-at ") *")
234 (elmo-condition-parse-error))
235 (goto-char (match-end 0))))
236 ;; search-key ::= [A-Za-z-]+
237 ;; ;; "since" / "before" / "last" / "first" /
238 ;; ;; "body" / field-name
239 ((looking-at "\\(!\\)? *\\([A-Za-z-]+\\) *: *")
240 (goto-char (match-end 0))
241 (let ((search-key (vector
242 (if (match-beginning 1) 'unmatch 'match)
243 (elmo-match-buffer 2)
244 (elmo-condition-parse-search-value))))
246 (if (string= (aref search-key 1) "tocc")
247 (if (eq (aref search-key 0) 'match)
249 (vector 'match "to" (aref search-key 2))
250 (vector 'match "cc" (aref search-key 2)))
252 (vector 'unmatch "to" (aref search-key 2))
253 (vector 'unmatch "cc" (aref search-key 2))))
256 ;; search-value ::= quoted / time / number / atom
257 ;; quoted ::= <elisp string expression>
258 ;; time ::= "yesterday" / "lastweek" / "lastmonth" / "lastyear" /
259 ;; number SPACE* "daysago" /
260 ;; number "-" month "-" number ; ex. 10-May-2000
261 ;; number "-" number "-" number ; ex. 2000-05-10
263 ;; month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
264 ;; "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
265 ;; atom ::= ATOM_CHARS*
266 ;; SPACE ::= <ascii space character, 0x20>
267 ;; ATOM_CHARS ::= <any character except specials>
268 ;; specials ::= SPACE / <"> / </> / <)> / <|> / <&>
269 ;; ;; These characters should be quoted.
270 (defun elmo-condition-parse-search-value ()
273 (read (current-buffer)))
274 ((or (looking-at "yesterday") (looking-at "lastweek")
275 (looking-at "lastmonth") (looking-at "lastyear")
276 (looking-at "[0-9]+ *daysago")
277 (looking-at "[0-9]+-[A-Za-z]+-[0-9]+")
278 (looking-at "[0-9]+-[0-9]+-[0-9]+")
279 (looking-at "[0-9]+")
280 (looking-at elmo-condition-atom-regexp))
281 (prog1 (elmo-match-buffer 0)
282 (goto-char (match-end 0))))
283 (t (error "Syntax error '%s'" (buffer-string)))))
286 (defsubst elmo-buffer-replace (regexp &optional newtext)
287 (goto-char (point-min))
288 (while (re-search-forward regexp nil t)
289 (replace-match (or newtext ""))))
291 (defsubst elmo-delete-char (char string &optional unibyte)
294 (let ((coding-system-for-read 'no-conversion)
295 (coding-system-for-write 'no-conversion))
296 (if unibyte (elmo-set-buffer-multibyte nil))
298 (goto-char (point-min))
299 (while (search-forward (char-to-string char) nil t)
303 (defsubst elmo-delete-cr-buffer ()
304 "Delete CR from buffer."
306 (goto-char (point-min))
307 (while (search-forward "\r\n" nil t)
308 (replace-match "\n")) ))
310 (defsubst elmo-delete-cr-get-content-type ()
312 (goto-char (point-min))
313 (while (search-forward "\r\n" nil t)
314 (replace-match "\n"))
315 (goto-char (point-min))
316 (or (std11-field-body "content-type")
319 (defun elmo-delete-cr (string)
323 (goto-char (point-min))
324 (while (search-forward "\r\n" nil t)
325 (replace-match "\n"))
328 (defun elmo-uniq-list (lst)
329 "Distractively uniqfy elements of LST."
338 (defun elmo-list-insert (list element after)
339 (let* ((match (memq after list))
340 (rest (and match (cdr (memq after list)))))
343 (setcdr match (list element))
345 (nconc list (list element)))))
347 (defun elmo-string-partial-p (string)
348 (and (stringp string) (string-match "message/partial" string)))
350 (defun elmo-get-file-string (filename &optional remove-final-newline)
352 (let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
353 insert-file-contents-post-hook)
354 (when (file-exists-p filename)
356 (as-binary-input-file (insert-file-contents filename)))
357 (when (and remove-final-newline
359 (= (char-after (1- (point-max))) ?\n))
360 (goto-char (point-max))
361 (delete-backward-char 1))
364 (defun elmo-save-string (string filename)
367 (as-binary-output-file
369 (write-region (point-min) (point-max)
370 filename nil 'no-msg))
373 (defun elmo-max-of-list (nlist)
377 (if (< max-num (car l))
378 (setq max-num (car l)))
382 (defun elmo-concat-path (path filename)
383 (if (not (string= path ""))
384 (if (string= elmo-path-sep (substring path (- (length path) 1)))
385 (concat path filename)
386 (concat path elmo-path-sep filename))
389 (defvar elmo-passwd-alist nil)
391 (defun elmo-passwd-alist-load ()
393 (let ((filename (expand-file-name elmo-passwd-alist-file-name
394 elmo-msgdb-directory))
395 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*"))
396 insert-file-contents-pre-hook ; To avoid autoconv-xmas...
397 insert-file-contents-post-hook
399 (if (not (file-readable-p filename))
401 (set-buffer tmp-buffer)
402 (insert-file-contents filename)
405 (read (current-buffer))
407 (kill-buffer tmp-buffer)
410 (defun elmo-passwd-alist-clear ()
411 "Clear password cache."
413 (setq elmo-passwd-alist nil))
415 (defun elmo-passwd-alist-save ()
416 "Save password into file."
419 (let ((filename (expand-file-name elmo-passwd-alist-file-name
420 elmo-msgdb-directory))
421 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*"))
422 print-length print-level)
423 (set-buffer tmp-buffer)
425 (prin1 elmo-passwd-alist tmp-buffer)
426 (princ "\n" tmp-buffer)
427 ;;; (if (and (file-exists-p filename)
428 ;;; (not (equal 384 (file-modes filename))))
429 ;;; (error "%s is not safe.chmod 600 %s!" filename filename))
430 (if (file-writable-p filename)
432 (write-region (point-min) (point-max)
433 filename nil 'no-msg)
434 (set-file-modes filename 384))
435 (message "%s is not writable." filename))
436 (kill-buffer tmp-buffer))))
438 (defun elmo-get-passwd (key)
439 "Get password from password pool."
441 (if (not elmo-passwd-alist)
442 (setq elmo-passwd-alist (elmo-passwd-alist-load)))
443 (setq pair (assoc key elmo-passwd-alist))
445 (elmo-base64-decode-string (cdr pair))
446 (setq pass (elmo-read-passwd (format "Password for %s: "
448 (setq elmo-passwd-alist
449 (append elmo-passwd-alist
451 (elmo-base64-encode-string pass)))))
452 (if elmo-passwd-life-time
453 (run-with-timer elmo-passwd-life-time nil
454 (` (lambda () (elmo-remove-passwd (, key))))))
457 (defun elmo-remove-passwd (key)
458 "Remove password from password pool (for failure)."
460 (if (setq pass-cons (assoc key elmo-passwd-alist))
463 (fillarray (cdr pass-cons) 0))
464 (setq elmo-passwd-alist
465 (delete pass-cons elmo-passwd-alist))))))
467 (defmacro elmo-read-char-exclusive ()
468 (cond ((featurep 'xemacs)
469 '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
474 (key-press-event-p (setq event (next-command-event)))
475 (setq key (or (event-to-character event)
476 (cdr (assq (event-key event) table)))))))
478 ((fboundp 'read-char-exclusive)
479 '(read-char-exclusive))
483 (defun elmo-read-passwd (prompt &optional stars)
484 "Read a single line of text from user without echoing, and return it."
488 (cursor-in-echo-area t)
489 (log-message-max-size 0)
490 message-log-max done msg truncate)
492 (if (or (not stars) (string= "" ans))
494 (setq msg (concat prompt (make-string (length ans) ?.)))
496 (1+ (- (length msg) (window-width (minibuffer-window)))))
498 (setq msg (concat "$" (substring msg (1+ truncate))))))
500 (setq c (elmo-read-char-exclusive))
504 ((or (= c ?\r) (= c ?\n) (= c ?\e))
508 ((and (/= c ?\b) (/= c ?\177))
509 (setq ans (concat ans (char-to-string c))))
511 (setq ans (substring ans 0 -1)))))
520 (defun elmo-string-to-list (string)
523 (goto-char (point-min))
525 (goto-char (point-max))
527 (goto-char (point-min))
528 (read (current-buffer))))
530 (defun elmo-list-to-string (list)
539 (if (symbolp (car tlist))
540 (symbol-name (car tlist))
545 (setq tlist (cdr tlist)))
555 (defun elmo-plug-on-by-servers (alist &optional servers)
556 (let ((server-list (or servers elmo-plug-on-servers)))
559 (if (elmo-plugged-p (car server-list))
561 (setq server-list (cdr server-list))))))
563 (defun elmo-plug-on-by-exclude-servers (alist &optional servers)
564 (let ((server-list (or servers elmo-plug-on-exclude-servers))
565 server other-servers)
567 (when (and (not (member (setq server (caaar alist)) server-list))
568 (not (member server other-servers)))
569 (push server other-servers))
570 (setq alist (cdr alist)))
571 (elmo-plug-on-by-servers alist other-servers)))
573 (defun elmo-plugged-p (&optional server port stream-type alist label-exp)
574 (let ((alist (or alist elmo-plugged-alist))
576 (cond ((and (not port) (not server))
577 (cond ((eq elmo-plugged-condition 'one)
581 (if (nth 2 (car alist))
583 (setq alist (cdr alist))))
585 ((eq elmo-plugged-condition 'all)
589 (if (not (nth 2 (car alist)))
590 (throw 'plugged nil))
591 (setq alist (cdr alist)))
594 ((functionp elmo-plugged-condition)
595 (funcall elmo-plugged-condition alist))
598 ((not port) ;; server
601 (when (string= server (caaar alist))
602 (if (nth 2 (car alist))
604 (setq alist (cdr alist)))))
606 (setq plugged-info (assoc (list server port stream-type) alist))
607 (if (not plugged-info)
608 ;; add elmo-plugged-alist automatically
610 (elmo-set-plugged elmo-plugged server port stream-type
611 nil nil nil label-exp)
613 (if (and elmo-auto-change-plugged
614 (> elmo-auto-change-plugged 0)
615 (nth 3 plugged-info) ;; time
616 (elmo-time-expire (nth 3 plugged-info)
617 elmo-auto-change-plugged))
619 (nth 2 plugged-info)))))))
621 (defun elmo-set-plugged (plugged &optional server port stream-type time
623 (let ((alist (or alist elmo-plugged-alist))
625 (cond ((and (not port) (not server))
626 (setq elmo-plugged plugged)
627 ;; set plugged all element of elmo-plugged-alist.
629 (setcdr (cdar alist) (list plugged time))
630 (setq alist (cdr alist))))
632 ;; set plugged all port of server
634 (when (string= server (caaar alist))
635 (setcdr (cdar alist) (list plugged time)))
636 (setq alist (cdr alist))))
638 ;; set plugged one port of server
639 (setq plugged-info (assoc (list server port stream-type) alist))
640 (setq label (if label-exp
642 (nth 1 plugged-info)))
644 ;; if add is non-nil, don't reset plug state.
646 (setcdr plugged-info (list label plugged time)))
648 (setq elmo-plugged-alist
652 (list (list server port stream-type)
653 label plugged time))))))))
656 (defun elmo-delete-plugged (&optional server port alist)
657 (let* ((alist (or alist elmo-plugged-alist))
659 (cond ((and (not port) (not server))
662 ;; delete plugged all port of server
664 (when (string= server (caaar alist2))
665 (setq alist (delete (car alist2) alist)))
666 (setq alist2 (cdr alist2))))
668 ;; delete plugged one port of server
670 (delete (assoc (cons server port) alist) alist))))
673 (defun elmo-disk-usage (path)
674 "Get disk usage (bytes) in PATH."
676 (condition-case () (file-attributes path) (error nil))))
678 (if (nth 0 file-attr) ; directory
679 (let ((files (condition-case ()
680 (directory-files path t "^[^\\.]")
683 ;; (result (nth 7 file-attr))) ... directory size
685 (setq result (+ result (or (elmo-disk-usage (car files)) 0)))
686 (setq files (cdr files)))
688 (float (nth 7 file-attr)))
691 (defun elmo-get-last-accessed-time (path &optional dir)
692 "Return the last accessed time of PATH."
693 (let ((last-accessed (nth 4 (file-attributes (or (and dir
698 (setq last-accessed (+ (* (nth 0 last-accessed)
699 (float 65536)) (nth 1 last-accessed)))
702 (defun elmo-get-last-modification-time (path &optional dir)
703 "Return the last accessed time of PATH."
704 (let ((last-modified (nth 5 (file-attributes (or (and dir
708 (setq last-modified (+ (* (nth 0 last-modified)
709 (float 65536)) (nth 1 last-modified)))))
711 (defun elmo-make-directory (path &optional mode)
712 "Create directory recursively."
713 (let ((parent (directory-file-name (file-name-directory path))))
714 (if (null (file-directory-p parent))
715 (elmo-make-directory parent))
716 (make-directory path)
717 (set-file-modes path (or mode
718 (+ (* 64 7) (* 8 0) 0))))) ; chmod 0700
720 (defun elmo-delete-directory (path &optional no-hierarchy)
721 "Delete directory recursively."
722 (if (stringp path) ; nil is not permitted.
723 (let ((dirent (directory-files path))
724 relpath abspath hierarchy)
726 (setq relpath (car dirent)
728 abspath (expand-file-name relpath path))
729 (when (not (string-match "^\\.\\.?$" relpath))
730 (if (eq (nth 0 (file-attributes abspath)) t)
733 (elmo-delete-directory abspath no-hierarchy))
734 (delete-file abspath))))
736 (delete-directory path)))))
738 (defun elmo-delete-match-files (path regexp &optional remove-if-empty)
739 "Delete directory files specified by PATH.
740 If optional REMOVE-IF-EMPTY is non-nil, delete directory itself if
741 the directory becomes empty after deletion."
742 (when (stringp path) ; nil is not permitted.
743 (dolist (file (directory-files path t regexp))
747 (delete-directory path) ; should be removed if empty.
750 (defun elmo-list-filter (l1 l2)
753 ;; t means filter all.
756 (elmo-delete-if (lambda (x) (not (memq x l1))) l2)
760 (defsubst elmo-list-delete-if-smaller (list number)
761 (let ((ret-val (copy-sequence list)))
763 (if (< (car list) number)
764 (setq ret-val (delq (car list) ret-val)))
765 (setq list (cdr list)))
768 (defun elmo-list-diff (list1 list2 &optional mes)
771 (let ((clist1 (copy-sequence list1))
772 (clist2 (copy-sequence list2)))
774 (setq clist1 (delq (car list2) clist1))
775 (setq list2 (cdr list2)))
777 (setq clist2 (delq (car list1) clist2))
778 (setq list1 (cdr list1)))
780 (message (concat mes "done")))
781 (list clist1 clist2)))
783 (defun elmo-list-bigger-diff (list1 list2 &optional mes)
784 "Returns a list (- +). + is bigger than max of LIST1, in LIST2."
789 (max-of-l2 (or (nth (max 0 (1- (length l2))) l2) 0))
793 (setq num (+ (length l1)))
795 (if (memq (car l1) l2)
796 (if (eq (car l1) (car l2))
799 (if (> (car l1) max-of-l2)
800 (setq diff1 (nconc diff1 (list (car l1))))))
804 (setq percent (/ (* i 100) num))
805 (if (eq (% percent 5) 0)
806 (elmo-display-progress
807 'elmo-list-bigger-diff "%s%d%%" percent mes))))
809 (cons diff1 (list l2)))))
811 (defmacro elmo-filter-condition-p (filter)
812 `(or (vectorp ,filter) (consp ,filter)))
814 (defmacro elmo-filter-type (filter)
815 (` (aref (, filter) 0)))
817 (defmacro elmo-filter-key (filter)
818 (` (aref (, filter) 1)))
820 (defmacro elmo-filter-value (filter)
821 (` (aref (, filter) 2)))
823 (defsubst elmo-buffer-field-primitive-condition-match (condition
827 (goto-char (point-min))
829 ((string= (elmo-filter-key condition) "last")
830 (setq result (<= (length (memq number number-list))
831 (string-to-int (elmo-filter-value condition)))))
832 ((string= (elmo-filter-key condition) "first")
833 (setq result (< (- (length number-list)
834 (length (memq number number-list)))
835 (string-to-int (elmo-filter-value condition)))))
836 ((string= (elmo-filter-key condition) "since")
837 (let ((field-date (elmo-date-make-sortable-string
839 (std11-field-body "date")
840 (current-time-zone) nil)))
841 (specified-date (elmo-date-make-sortable-string
842 (elmo-date-get-datevec
843 (elmo-filter-value condition)))))
845 (or (string= field-date specified-date)
846 (string< specified-date field-date)))))
847 ((string= (elmo-filter-key condition) "before")
850 (elmo-date-make-sortable-string
852 (std11-field-body "date")
853 (current-time-zone) nil))
854 (elmo-date-make-sortable-string
855 (elmo-date-get-datevec
856 (elmo-filter-value condition))))))
857 ((string= (elmo-filter-key condition) "body")
858 (and (re-search-forward "^$" nil t) ; goto body
859 (setq result (search-forward (elmo-filter-value condition)
862 (let ((fval (std11-field-body (elmo-filter-key condition))))
863 (if (eq (length fval) 0) (setq fval nil))
864 (if fval (setq fval (eword-decode-string fval)))
865 (setq result (and fval (string-match
866 (elmo-filter-value condition) fval))))))
867 (if (eq (elmo-filter-type condition) 'unmatch)
868 (setq result (not result)))
871 (defun elmo-condition-in-msgdb-p-internal (condition fields)
874 (if (not (member (elmo-filter-key condition) fields))
876 ((or (eq (car condition) 'and)
877 (eq (car condition) 'or))
878 (elmo-condition-in-msgdb-p-internal (nth 1 condition) fields)
879 (elmo-condition-in-msgdb-p-internal (nth 2 condition) fields))))
881 (defun elmo-condition-in-msgdb-p (condition)
883 (elmo-condition-in-msgdb-p-internal condition
885 elmo-msgdb-extra-fields
886 '("last" "first" "from"
887 "subject" "to" "cc" "since"
890 (defun elmo-buffer-field-condition-match (condition number number-list)
893 (elmo-buffer-field-primitive-condition-match
894 condition number number-list))
895 ((eq (car condition) 'and)
896 (and (elmo-buffer-field-condition-match
897 (nth 1 condition) number number-list)
898 (elmo-buffer-field-condition-match
899 (nth 2 condition) number number-list)))
900 ((eq (car condition) 'or)
901 (or (elmo-buffer-field-condition-match
902 (nth 1 condition) number number-list)
903 (elmo-buffer-field-condition-match
904 (nth 2 condition) number number-list)))))
906 (defsubst elmo-file-field-primitive-condition-match (file
911 (goto-char (point-min))
913 ((string= (elmo-filter-key condition) "last")
914 (setq result (<= (length (memq number number-list))
915 (string-to-int (elmo-filter-value condition))))
916 (if (eq (elmo-filter-type condition) 'unmatch)
917 (setq result (not result))))
918 ((string= (elmo-filter-key condition) "first")
919 (setq result (< (- (length number-list)
920 (length (memq number number-list)))
921 (string-to-int (elmo-filter-value condition))))
922 (if (eq (elmo-filter-type condition) 'unmatch)
923 (setq result (not result))))
926 (as-binary-input-file (insert-file-contents file))
927 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
928 ;; Should consider charset?
929 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
931 (elmo-buffer-field-primitive-condition-match
932 condition number number-list)))))
935 (defun elmo-file-field-condition-match (file condition number number-list)
938 (elmo-file-field-primitive-condition-match
939 file condition number number-list))
940 ((eq (car condition) 'and)
941 (and (elmo-file-field-condition-match
942 file (nth 1 condition) number number-list)
943 (elmo-file-field-condition-match
944 file (nth 2 condition) number number-list)))
945 ((eq (car condition) 'or)
946 (or (elmo-file-field-condition-match
947 file (nth 1 condition) number number-list)
948 (elmo-file-field-condition-match
949 file (nth 2 condition) number number-list)))))
951 (defmacro elmo-get-hash-val (string hashtable)
952 `(and (stringp ,string)
953 (let ((sym (intern-soft ,string ,hashtable)))
955 (symbol-value sym)))))
957 (defmacro elmo-set-hash-val (string value hashtable)
958 (list 'set (list 'intern string hashtable) value))
960 (defmacro elmo-clear-hash-val (string hashtable)
961 (static-if (fboundp 'unintern)
962 (list 'unintern string hashtable)
963 (list 'makunbound (list 'intern string hashtable))))
965 (defmacro elmo-unintern (string)
966 "`unintern' symbol named STRING, When can use `unintern'.
967 Emacs 19.28 or earlier does not have `unintern'."
968 (static-if (fboundp 'unintern)
969 (list 'unintern string)))
971 (defun elmo-make-hash (&optional hashsize)
972 "Make a new hash table which have HASHSIZE size."
976 ;; Prime numbers as lengths tend to result in good
977 ;; hashing; lengths one less than a power of two are
981 (while (< (- i 1) hashsize)
984 elmo-hash-maximum-size)
985 elmo-hash-minimum-size)
986 elmo-hash-minimum-size)
989 (defsubst elmo-mime-string (string)
990 "Normalize MIME encoded STRING."
993 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
995 (encode-mime-charset-string
996 (eword-decode-and-unfold-unstructured-field-body
999 (elmo-set-buffer-multibyte nil)
1002 (defsubst elmo-collect-field (beg end downcase-field-name)
1005 (narrow-to-region beg end)
1006 (goto-char (point-min))
1007 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1009 (while (re-search-forward regexp nil t)
1010 (setq name (buffer-substring-no-properties
1011 (match-beginning 1)(1- (match-end 1))))
1012 (if downcase-field-name
1013 (setq name (downcase name)))
1014 (setq body (buffer-substring-no-properties
1015 (match-end 0) (std11-field-end)))
1016 (or (assoc name dest)
1017 (setq dest (cons (cons name body) dest))))
1020 (defsubst elmo-collect-field-from-string (string downcase-field-name)
1023 (goto-char (point-min))
1024 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1026 (while (re-search-forward regexp nil t)
1027 (setq name (buffer-substring-no-properties
1028 (match-beginning 1)(1- (match-end 1))))
1029 (if downcase-field-name
1030 (setq name (downcase name)))
1031 (setq body (buffer-substring-no-properties
1032 (match-end 0) (std11-field-end)))
1033 (or (assoc name dest)
1034 (setq dest (cons (cons name body) dest))))
1037 (defun elmo-safe-filename (folder)
1038 (elmo-replace-in-string
1039 (elmo-replace-in-string
1040 (elmo-replace-in-string folder "/" " ")
1044 (defvar elmo-filename-replace-chars nil)
1046 (defsubst elmo-replace-string-as-filename (msgid)
1047 "Replace string as filename."
1048 (setq msgid (elmo-replace-in-string msgid " " " "))
1049 (if (null elmo-filename-replace-chars)
1050 (setq elmo-filename-replace-chars
1051 (regexp-quote (mapconcat
1052 'car elmo-filename-replace-string-alist ""))))
1053 (while (string-match (concat "[" elmo-filename-replace-chars "]")
1056 (substring msgid 0 (match-beginning 0))
1059 (match-beginning 0) (match-end 0))
1060 elmo-filename-replace-string-alist))
1061 (substring msgid (match-end 0)))))
1064 (defsubst elmo-recover-string-from-filename (filename)
1065 "Recover string from FILENAME."
1067 (while (string-match " " filename)
1068 (setq tmp (substring filename
1070 (+ (match-end 0) 1)))
1071 (if (string= tmp " ")
1073 (setq tmp (car (rassoc tmp
1074 elmo-filename-replace-string-alist))))
1077 (substring filename 0 (match-beginning 0))
1079 (setq filename (substring filename (+ (match-end 0) 1))))
1080 (concat result filename)))
1082 (defsubst elmo-copy-file (src dst &optional ok-if-already-exists)
1084 (elmo-add-name-to-file src dst ok-if-already-exists)
1085 (error (copy-file src dst ok-if-already-exists t))))
1087 (defsubst elmo-buffer-exists-p (buffer)
1088 (if (bufferp buffer)
1089 (buffer-live-p buffer)
1090 (get-buffer buffer)))
1092 (defsubst elmo-kill-buffer (buffer)
1093 (when (elmo-buffer-exists-p buffer)
1094 (kill-buffer buffer)))
1096 (defun elmo-delete-if (pred lst)
1097 "Return new list contain items which don't satisfy PRED in LST."
1100 (unless (funcall pred (car lst))
1101 (setq result (nconc result (list (car lst)))))
1102 (setq lst (cdr lst)))
1105 (defun elmo-list-delete (list1 list2 &optional delete-function)
1106 "Delete by side effect any occurrences equal to elements of LIST1 from LIST2.
1107 Return the modified LIST2. Deletion is done with `delete'.
1108 Write `(setq foo (elmo-list-delete bar foo))' to be sure of changing
1110 If optional DELETE-FUNCTION is speficied, it is used as delete procedure."
1111 (setq delete-function (or delete-function 'delete))
1113 (setq list2 (funcall delete-function (car list1) list2))
1114 (setq list1 (cdr list1)))
1117 (defun elmo-list-member (list1 list2)
1118 "If any element of LIST1 is member of LIST2, return t."
1121 (if (member (car list1) list2)
1123 (setq list1 (cdr list1)))))
1125 (defun elmo-count-matches (regexp beg end)
1129 (while (re-search-forward regexp end t)
1130 (setq count (1+ count)))
1133 (if (fboundp 'display-error)
1134 (defalias 'elmo-display-error 'display-error)
1135 (defun elmo-display-error (error-object stream)
1136 "A tiny function to display ERROR-OBJECT to the STREAM."
1138 (errobj error-object)
1141 (setq err-mes (concat err-mes (format
1142 (if (stringp (car errobj))
1146 (setq errobj (cdr errobj))
1147 (if errobj (setq err-mes (concat err-mes (if first ": " ", "))))
1149 (princ err-mes stream))))
1151 (if (fboundp 'define-error)
1152 (defalias 'elmo-define-error 'define-error)
1153 (defun elmo-define-error (error doc &optional parents)
1155 (setq parents 'error))
1156 (let ((conds (get parents 'error-conditions)))
1158 (error "Not an error symbol: %s" error))
1160 (list 'error-message doc
1161 'error-conditions (cons error conds))))))
1163 (cond ((fboundp 'progress-feedback-with-label)
1164 (defalias 'elmo-display-progress 'progress-feedback-with-label))
1165 ((fboundp 'lprogress-display)
1166 (defalias 'elmo-display-progress 'lprogress-display))
1168 (defun elmo-display-progress (label format &optional value &rest args)
1169 "Print a progress message."
1170 (if (and (null format) (null args))
1172 (apply (function message) (concat format " %d%%")
1173 (nconc args (list value)))))))
1175 (defvar elmo-progress-counter-alist nil)
1177 (defmacro elmo-progress-counter-value (counter)
1178 (` (aref (cdr (, counter)) 0)))
1180 (defmacro elmo-progress-counter-all-value (counter)
1181 (` (aref (cdr (, counter)) 1)))
1183 (defmacro elmo-progress-counter-format (counter)
1184 (` (aref (cdr (, counter)) 2)))
1186 (defmacro elmo-progress-counter-set-value (counter value)
1187 (` (aset (cdr (, counter)) 0 (, value))))
1189 (defun elmo-progress-set (label all-value &optional format)
1190 (unless (assq label elmo-progress-counter-alist)
1191 (setq elmo-progress-counter-alist
1192 (cons (cons label (vector 0 all-value (or format "")))
1193 elmo-progress-counter-alist))))
1195 (defun elmo-progress-clear (label)
1196 (let ((counter (assq label elmo-progress-counter-alist)))
1198 (elmo-display-progress label
1199 (elmo-progress-counter-format counter)
1201 (setq elmo-progress-counter-alist
1202 (delq counter elmo-progress-counter-alist)))))
1204 (defun elmo-progress-notify (label &optional value op &rest args)
1205 (let ((counter (assq label elmo-progress-counter-alist)))
1207 (let* ((value (or value 1))
1208 (cur-value (elmo-progress-counter-value counter))
1209 (all-value (elmo-progress-counter-all-value counter))
1210 (new-value (if (eq op 'set) value (+ cur-value value)))
1211 (cur-rate (/ (* cur-value 100) all-value))
1212 (new-rate (/ (* new-value 100) all-value)))
1213 (elmo-progress-counter-set-value counter new-value)
1214 (unless (= cur-rate new-rate)
1215 (apply 'elmo-display-progress
1217 (elmo-progress-counter-format counter)
1220 (when (>= new-rate 100)
1221 (elmo-progress-clear label))))))
1223 (defun elmo-time-expire (before-time diff-time)
1224 (let* ((current (current-time))
1225 (rest (when (< (nth 1 current) (nth 1 before-time))
1229 (list (- (+ (car current) (if rest -1 0)) (car before-time))
1230 (- (+ (or rest 0) (nth 1 current)) (nth 1 before-time))))
1231 (and (eq (car diff) 0)
1232 (< diff-time (nth 1 diff)))))
1234 (if (fboundp 'std11-fetch-field)
1235 (defalias 'elmo-field-body 'std11-fetch-field) ;;no narrow-to-region
1236 (defalias 'elmo-field-body 'std11-field-body))
1238 (defun elmo-address-quote-specials (word)
1239 "Make quoted string of WORD if needed."
1240 (let ((lal (std11-lexical-analyze word)))
1241 (if (or (assq 'specials lal)
1242 (assq 'domain-literal lal))
1243 (prin1-to-string word)
1246 (defmacro elmo-string (string)
1247 "STRING without text property."
1248 (` (let ((obj (copy-sequence (, string))))
1249 (and obj (set-text-properties 0 (length obj) nil obj))
1252 (defun elmo-flatten (list-of-list)
1253 "Flatten LIST-OF-LIST."
1254 (unless (null list-of-list)
1255 (append (if (and (car list-of-list)
1256 (listp (car list-of-list)))
1258 (list (car list-of-list)))
1259 (elmo-flatten (cdr list-of-list)))))
1261 (defun elmo-y-or-n-p (prompt &optional auto default)
1262 "Same as `y-or-n-p'.
1263 But if optional argument AUTO is non-nil, DEFAULT is returned."
1268 (defun elmo-string-member (string slist)
1269 "Return t if STRING is a member of the SLIST."
1272 (if (and (stringp (car slist))
1273 (string= string (car slist)))
1275 (setq slist (cdr slist)))))
1277 (defun elmo-string-match-member (str list &optional case-ignore)
1278 (let ((case-fold-search case-ignore))
1281 (if (string-match (car list) str)
1282 (throw 'member (car list)))
1283 (setq list (cdr list))))))
1285 (defun elmo-string-matched-member (str list &optional case-ignore)
1286 (let ((case-fold-search case-ignore))
1289 (if (string-match str (car list))
1290 (throw 'member (car list)))
1291 (setq list (cdr list))))))
1293 (defsubst elmo-string-delete-match (string pos)
1294 (concat (substring string
1295 0 (match-beginning pos))
1300 (defun elmo-string-match-assoc (key alist &optional case-ignore)
1301 (let ((case-fold-search case-ignore)
1305 (setq a (car alist))
1308 (string-match key (car a)))
1310 (setq alist (cdr alist))))))
1312 (defun elmo-string-matched-assoc (key alist &optional case-ignore)
1313 (let ((case-fold-search case-ignore)
1317 (setq a (car alist))
1320 (string-match (car a) key))
1322 (setq alist (cdr alist))))))
1324 (defun elmo-string-assoc (key alist)
1328 (setq a (car alist))
1331 (string= key (car a)))
1333 (setq alist (cdr alist))))))
1335 (defun elmo-string-assoc-all (key alist)
1338 (if (string= key (car (car alist)))
1342 (setq alist (cdr alist)))
1345 (defun elmo-string-rassoc (key alist)
1349 (setq a (car alist))
1352 (string= key (cdr a)))
1354 (setq alist (cdr alist))))))
1356 (defun elmo-string-rassoc-all (key alist)
1359 (if (string= key (cdr (car alist)))
1363 (setq alist (cdr alist)))
1366 (defun elmo-expand-newtext (newtext original)
1367 (let ((len (length newtext))
1369 c expanded beg N did-expand)
1372 (while (and (< pos len)
1373 (not (= (aref newtext pos) ?\\)))
1374 (setq pos (1+ pos)))
1376 (push (substring newtext beg pos) expanded))
1378 ;; We hit a \; expand it.
1381 c (aref newtext pos))
1382 (if (not (or (= c ?\&)
1385 ;; \ followed by some character we don't expand.
1386 (push (char-to-string c) expanded)
1391 (when (match-beginning N)
1392 (push (substring original (match-beginning N) (match-end N))
1394 (setq pos (1+ pos)))
1396 (apply (function concat) (nreverse expanded))
1399 ;;; Folder parser utils.
1400 (defun elmo-parse-token (string &optional seps)
1401 "Parse atom from STRING using SEPS as a string of separator char list."
1402 (let ((len (length string))
1403 (seps (and seps (string-to-char-list seps)))
1409 (while (and (< i len) (or in (null sep)))
1410 (setq c (aref string i))
1412 ((and in (eq c ?\\))
1414 content (cons (aref string i) content)
1419 (in (setq content (cons c content)
1423 (t (setq content (cons c content)
1425 (if in (error "Parse error in quoted"))
1426 (cons (if (null content) "" (char-list-to-string (nreverse content)))
1427 (substring string i)))))
1429 (defun elmo-parse-prefixed-element (prefix string &optional seps)
1430 (if (and (not (eq (length string) 0))
1431 (eq (aref string 0) prefix))
1432 (elmo-parse-token (substring string 1) seps)
1435 ;;; Number set defined by OKAZAKI Tetsurou <okazaki@be.to>
1437 ;; number ::= [0-9]+
1440 ;; number-range ::= "(" beg " . " end ")" ;; cons cell
1441 ;; number-set-elem ::= number / number-range
1442 ;; number-set ::= "(" *number-set-elem ")" ;; list
1444 (defun elmo-number-set-member (number number-set)
1445 "Return non-nil if NUMBER is an element of NUMBER-SET.
1446 The value is actually the tail of NUMBER-RANGE whose car contains NUMBER."
1447 (or (memq number number-set)
1449 (while (and number-set (not found))
1450 (if (and (consp (car number-set))
1451 (and (<= (car (car number-set)) number)
1452 (<= number (cdr (car number-set)))))
1454 (setq number-set (cdr number-set))))
1457 (defun elmo-number-set-append-list (number-set list)
1458 "Append LIST of numbers to the NUMBER-SET.
1459 NUMBER-SET is altered."
1460 (let ((appended number-set))
1462 (setq appended (elmo-number-set-append appended (car list)))
1463 (setq list (cdr list)))
1466 (defun elmo-number-set-append (number-set number)
1467 "Append NUMBER to the NUMBER-SET.
1468 NUMBER-SET is altered."
1469 (let ((number-set-1 number-set)
1471 (while (and number-set (not found))
1472 (setq elem (car number-set))
1475 (eq (+ 1 (cdr elem)) number))
1476 (setcdr elem number)
1478 ((and (integerp elem)
1479 (eq (+ 1 elem) number))
1480 (setcar number-set (cons elem number))
1482 ((or (and (integerp elem) (eq elem number))
1484 (<= (car elem) number)
1485 (<= number (cdr elem))))
1487 (setq number-set (cdr number-set)))
1489 (setq number-set-1 (nconc number-set-1 (list number))))
1492 (defun elmo-number-set-to-number-list (number-set)
1493 "Return a number list which corresponds to NUMBER-SET."
1494 (let (number-list elem i)
1496 (setq elem (car number-set))
1500 (while (<= i (cdr elem))
1501 (setq number-list (cons i number-list))
1504 (setq number-list (cons elem number-list))))
1505 (setq number-set (cdr number-set)))
1506 (nreverse number-list)))
1508 (defcustom elmo-list-subdirectories-ignore-regexp "^\\(\\.\\.?\\|[0-9]+\\)$"
1509 "*Regexp to filter subfolders."
1513 (defun elmo-list-subdirectories-1 (basedir curdir one-level)
1514 (let ((root (zerop (length curdir)))
1515 (w32-get-true-file-link-count t) ; for Meadow
1518 (dolist (file (directory-files (setq dir (expand-file-name curdir basedir))))
1519 (when (and (not (string-match
1520 elmo-list-subdirectories-ignore-regexp
1522 (car (setq attr (file-attributes
1523 (expand-file-name file dir)))))
1524 (when (eq one-level 'check) (throw 'done t))
1526 (concat curdir (and (not root) elmo-path-sep) file))
1528 (setq dirs (nconc dirs
1529 (if (if elmo-have-link-count (< 2 (nth 1 attr))
1531 (elmo-list-subdirectories-1
1534 (if one-level 'check))))
1536 (list (list relpath))
1539 (elmo-list-subdirectories-1
1543 (list relpath)))))))
1546 (defun elmo-list-subdirectories (directory file one-level)
1547 (let ((subdirs (elmo-list-subdirectories-1 directory file one-level)))
1548 (if (zerop (length file))
1550 (cons file subdirs))))
1552 (defun elmo-mapcar-list-of-list (func list-of-list)
1555 (cond ((listp x) (elmo-mapcar-list-of-list func x))
1556 (t (funcall func x))))
1559 (defun elmo-parse (string regexp &optional matchn)
1560 (or matchn (setq matchn 1))
1562 (store-match-data nil)
1563 (while (string-match regexp string (match-end 0))
1564 (setq list (cons (substring string (match-beginning matchn)
1565 (match-end matchn)) list)))
1569 ;; parsistent mark handling
1571 ;; (FIXME: this should be treated in the msgdb.)
1573 (defvar elmo-msgdb-global-mark-alist nil)
1575 (defun elmo-msgdb-global-mark-delete (msgid)
1576 (let* ((path (expand-file-name
1577 elmo-msgdb-global-mark-filename
1578 elmo-msgdb-directory))
1579 (malist (or elmo-msgdb-global-mark-alist
1580 (setq elmo-msgdb-global-mark-alist
1581 (elmo-object-load path))))
1583 (when (setq match (assoc msgid malist))
1584 (setq elmo-msgdb-global-mark-alist
1585 (delete match elmo-msgdb-global-mark-alist))
1586 (elmo-object-save path elmo-msgdb-global-mark-alist))))
1588 (defun elmo-msgdb-global-mark-set (msgid mark)
1589 (let* ((path (expand-file-name
1590 elmo-msgdb-global-mark-filename
1591 elmo-msgdb-directory))
1592 (malist (or elmo-msgdb-global-mark-alist
1593 (setq elmo-msgdb-global-mark-alist
1594 (elmo-object-load path))))
1596 (if (setq match (assoc msgid malist))
1598 (setq elmo-msgdb-global-mark-alist
1599 (nconc elmo-msgdb-global-mark-alist
1600 (list (cons msgid mark)))))
1601 (elmo-object-save path elmo-msgdb-global-mark-alist)))
1603 (defun elmo-msgdb-global-mark-get (msgid)
1604 (cdr (assoc msgid (or elmo-msgdb-global-mark-alist
1605 (setq elmo-msgdb-global-mark-alist
1608 elmo-msgdb-global-mark-filename
1609 elmo-msgdb-directory)))))))
1612 (defmacro elmo-make-file-cache (path status)
1613 "PATH is the cache file name.
1614 STATUS is one of 'section, 'entire or nil.
1615 nil means no cache exists.
1616 'section means partial section cache exists.
1617 'entire means entire cache exists.
1618 If the cache is partial file-cache, TYPE is 'partial."
1619 (` (cons (, path) (, status))))
1621 (defmacro elmo-file-cache-path (file-cache)
1622 "Returns the file path of the FILE-CACHE."
1623 (` (car (, file-cache))))
1625 (defmacro elmo-file-cache-status (file-cache)
1626 "Returns the status of the FILE-CACHE."
1627 (` (cdr (, file-cache))))
1629 (defsubst elmo-cache-to-msgid (filename)
1630 (concat "<" (elmo-recover-string-from-filename filename) ">"))
1632 (defsubst elmo-cache-get-path-subr (msgid)
1633 (let ((chars '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?A ?B ?C ?D ?E ?F))
1634 (clist (string-to-char-list msgid))
1637 (setq sum (+ sum (car clist)))
1638 (setq clist (cdr clist)))
1640 (nth (% (/ sum 16) 2) chars)
1641 (nth (% sum 16) chars))))
1643 (defun elmo-file-cache-get-path (msgid &optional section)
1644 "Get cache path for MSGID.
1645 If optional argument SECTION is specified, partial cache path is returned."
1646 (if (setq msgid (elmo-msgid-to-cache msgid))
1649 (format "%s/%s/%s/%s"
1650 elmo-cache-directory
1651 (elmo-cache-get-path-subr msgid)
1655 elmo-cache-directory
1656 (elmo-cache-get-path-subr msgid)
1659 (defmacro elmo-file-cache-expand-path (path section)
1660 "Return file name for the file-cache corresponds to the section.
1661 PATH is the file-cache path.
1662 SECTION is the section string."
1663 (` (expand-file-name (or (, section) "") (, path))))
1665 (defun elmo-file-cache-delete (path)
1666 "Delete a cache on PATH."
1667 (unless (elmo-msgdb-global-mark-get
1668 (elmo-cache-to-msgid (file-name-nondirectory path)))
1669 (when (file-exists-p path)
1670 (if (file-directory-p path)
1672 (dolist (file (directory-files path t "^[^\\.]"))
1674 (delete-directory path))
1678 (defun elmo-file-cache-exists-p (msgid)
1679 "Returns 'section or 'entire if a cache which corresponds to MSGID exists."
1680 (elmo-file-cache-status (elmo-file-cache-get msgid)))
1682 (defun elmo-file-cache-save (cache-path section)
1683 "Save current buffer as cache on PATH.
1684 Return t if cache is saved successfully."
1686 (let ((path (if section (expand-file-name section cache-path)
1689 (if (and (null section)
1690 (file-directory-p path))
1692 (setq files (directory-files path t "^[^\\.]"))
1694 (delete-file (car files))
1695 (setq files (cdr files)))
1696 (delete-directory path))
1698 (not (file-directory-p cache-path)))
1699 (delete-file cache-path)))
1701 (setq dir (directory-file-name (file-name-directory path)))
1702 (if (not (file-exists-p dir))
1703 (elmo-make-directory dir))
1704 (write-region-as-binary (point-min) (point-max)
1710 (defun elmo-file-cache-load (cache-path section)
1711 "Load cache on PATH into the current buffer.
1712 Return t if cache is loaded successfully."
1715 (when (and cache-path
1716 (if (elmo-cache-path-section-p cache-path)
1719 (setq cache-file (elmo-file-cache-expand-path
1722 (file-exists-p cache-file))
1723 (insert-file-contents-as-binary cache-file)
1728 (defun elmo-cache-path-section-p (path)
1729 "Return non-nil when PATH is `section' cache path."
1730 (file-directory-p path))
1732 (defun elmo-file-cache-get (msgid &optional section)
1733 "Returns the current file-cache object associated with MSGID.
1734 MSGID is the message-id of the message.
1735 If optional argument SECTION is specified, get partial file-cache object
1736 associated with SECTION."
1738 (let ((path (elmo-cache-get-path msgid)))
1739 (if (and path (file-exists-p path))
1740 (if (elmo-cache-path-section-p path)
1742 (if (file-exists-p (setq path (expand-file-name
1744 (cons path 'section))
1745 ;; section is not specified but sectional.
1746 (cons path 'section))
1749 (cons path 'entire)))
1756 (defun elmo-cache-expire ()
1758 (let* ((completion-ignore-case t)
1759 (method (completing-read (format "Expire by (%s): "
1760 elmo-cache-expire-default-method)
1763 (if (string= method "")
1764 (setq method elmo-cache-expire-default-method))
1765 (funcall (intern (concat "elmo-cache-expire-by-" method)))))
1767 (defun elmo-read-float-value-from-minibuffer (prompt &optional initial)
1768 (let ((str (read-from-minibuffer prompt initial)))
1770 ((string-match "[0-9]*\\.[0-9]+" str)
1771 (string-to-number str))
1772 ((string-match "[0-9]+" str)
1773 (string-to-number (concat str ".0")))
1774 (t (error "%s is not number" str)))))
1776 (defun elmo-cache-expire-by-size (&optional kbytes)
1777 "Expire cache file by size.
1778 If KBYTES is kilo bytes (This value must be float)."
1780 (let ((size (or kbytes
1781 (and (interactive-p)
1782 (elmo-read-float-value-from-minibuffer
1783 "Enter cache disk size (Kbytes): "
1785 (if (integerp elmo-cache-expire-default-size)
1786 (float elmo-cache-expire-default-size)
1787 elmo-cache-expire-default-size))))
1788 (if (integerp elmo-cache-expire-default-size)
1789 (float elmo-cache-expire-default-size))))
1793 (message "Checking disk usage...")
1794 (setq total (/ (elmo-disk-usage
1795 elmo-cache-directory) Kbytes))
1796 (setq beginning total)
1797 (message "Checking disk usage...done")
1798 (let ((cfl (elmo-cache-get-sorted-cache-file-list))
1802 (while (and (<= size total)
1803 (setq oldest (elmo-cache-get-oldest-cache-file-entity cfl)))
1804 (setq cur-file (expand-file-name (car (cdr oldest)) (car oldest)))
1805 (setq cur-size (/ (elmo-disk-usage cur-file) Kbytes))
1806 (when (elmo-file-cache-delete cur-file)
1807 (setq count (+ count 1))
1808 (message "%d cache(s) are expired." count))
1809 (setq deleted (+ deleted cur-size))
1810 (setq total (- total cur-size)))
1811 (message "%d cache(s) are expired from disk (%d Kbytes/%d Kbytes)."
1812 count deleted beginning))))
1814 (defun elmo-cache-make-file-entity (filename path)
1815 (cons filename (elmo-get-last-accessed-time filename path)))
1817 (defun elmo-cache-get-oldest-cache-file-entity (cache-file-list)
1818 (let ((cfl cache-file-list)
1819 flist firsts oldest-entity wonlist)
1821 (setq flist (cdr (car cfl)))
1822 (setq firsts (append firsts (list
1823 (cons (car (car cfl))
1825 (setq cfl (cdr cfl)))
1828 (if (and (not oldest-entity)
1829 (cdr (cdr (car firsts))))
1830 (setq oldest-entity (car firsts)))
1831 (if (and (cdr (cdr (car firsts)))
1832 (cdr (cdr oldest-entity))
1833 (> (cdr (cdr oldest-entity)) (cdr (cdr (car firsts)))))
1834 (setq oldest-entity (car firsts)))
1835 (setq firsts (cdr firsts)))
1836 (setq wonlist (assoc (car oldest-entity) cache-file-list))
1838 (setcdr wonlist (delete (car (cdr wonlist)) (cdr wonlist))))
1841 (defun elmo-cache-get-sorted-cache-file-list ()
1842 (let ((dirs (directory-files
1843 elmo-cache-directory
1848 (setq num (length dirs))
1849 (message "Collecting cache info...")
1851 (setq elist (mapcar (lambda (x)
1852 (elmo-cache-make-file-entity x (car dirs)))
1853 (directory-files (car dirs) nil "^[^\\.]")))
1854 (setq ret-val (append ret-val
1862 (when (> num elmo-display-progress-threshold)
1864 (elmo-display-progress
1865 'elmo-cache-get-sorted-cache-file-list "Collecting cache info..."
1867 (setq dirs (cdr dirs)))
1868 (message "Collecting cache info...done")
1871 (defun elmo-cache-expire-by-age (&optional days)
1872 (let ((age (or (and days (int-to-string days))
1873 (and (interactive-p)
1874 (read-from-minibuffer
1875 (format "Enter days (%s): "
1876 elmo-cache-expire-default-age)))
1877 (int-to-string elmo-cache-expire-default-age)))
1878 (dirs (directory-files
1879 elmo-cache-directory
1883 (if (string= age "")
1884 (setq age elmo-cache-expire-default-age)
1885 (setq age (string-to-int age)))
1886 (setq curtime (current-time))
1887 (setq curtime (+ (* (nth 0 curtime)
1888 (float 65536)) (nth 1 curtime)))
1890 (let ((files (directory-files (car dirs) t "^[^\\.]"))
1891 (limit-age (* age 86400)))
1893 (when (> (- curtime (elmo-get-last-accessed-time (car files)))
1895 (when (elmo-file-cache-delete (car files))
1896 (setq count (+ 1 count))
1897 (message "%d cache file(s) are expired." count)))
1898 (setq files (cdr files))))
1899 (setq dirs (cdr dirs)))))
1903 (defun elmo-msgid-to-cache (msgid)
1906 (string-match "<\\(.+\\)>$" msgid))
1907 (elmo-replace-string-as-filename (elmo-match-string 1 msgid)))))
1909 (defun elmo-cache-get-path (msgid &optional folder number)
1910 "Get path for cache file associated with MSGID, FOLDER, and NUMBER."
1911 (if (setq msgid (elmo-msgid-to-cache msgid))
1915 (format "%s/%s/%s@%s"
1916 (elmo-cache-get-path-subr msgid)
1919 (elmo-safe-filename folder))
1921 (elmo-cache-get-path-subr msgid)
1923 elmo-cache-directory))))
1928 (static-if (fboundp 'display-warning)
1929 (defmacro elmo-warning (&rest args)
1930 "Display a warning with `elmo' group."
1931 `(display-warning 'elmo (format ,@args)))
1932 (defconst elmo-warning-buffer-name "*elmo warning*")
1933 (defun elmo-warning (&rest args)
1934 "Display a warning. ARGS are passed to `format'."
1935 (with-current-buffer (get-buffer-create elmo-warning-buffer-name)
1936 (goto-char (point-max))
1937 (funcall 'insert (apply 'format (append args '("\n"))))
1938 (ignore-errors (recenter 1))
1939 (display-buffer elmo-warning-buffer-name))))
1941 (defvar elmo-obsolete-variable-alist nil)
1943 (defcustom elmo-obsolete-variable-show-warnings t
1944 "Show warning window if obsolete variable is treated."
1948 (defun elmo-define-obsolete-variable (obsolete var)
1949 "Define obsolete variable.
1950 OBSOLETE is a symbol for obsolete variable.
1951 VAR is a symbol for new variable.
1952 Definition is stored in `elmo-obsolete-variable-alist'."
1953 (let ((pair (assq var elmo-obsolete-variable-alist)))
1955 (setcdr pair obsolete)
1956 (setq elmo-obsolete-variable-alist
1957 (cons (cons var obsolete)
1958 elmo-obsolete-variable-alist)))))
1960 (defun elmo-resque-obsolete-variable (obsolete var)
1961 "Resque obsolete variable OBSOLETE as VAR.
1962 If `elmo-obsolete-variable-show-warnings' is non-nil, show warning message."
1963 (when (boundp obsolete)
1964 (static-if (and (fboundp 'defvaralias)
1965 (subrp (symbol-function 'defvaralias)))
1966 (defvaralias var obsolete)
1967 (set var (symbol-value obsolete)))
1968 (if elmo-obsolete-variable-show-warnings
1969 (elmo-warning "%s is obsolete. Use %s instead."
1970 (symbol-name obsolete)
1971 (symbol-name var)))))
1973 (defun elmo-resque-obsolete-variables (&optional alist)
1974 "Resque obsolete variables in ALIST.
1975 ALIST is a list of cons cell of
1976 \(OBSOLETE-VARIABLE-SYMBOL . NEW-VARIABLE-SYMBOL\).
1977 If ALIST is nil, `elmo-obsolete-variable-alist' is used."
1978 (dolist (pair elmo-obsolete-variable-alist)
1979 (elmo-resque-obsolete-variable (cdr pair)
1983 (defvar elmo-dop-queue-filename "queue"
1984 "*Disconnected operation queue is saved in this file.")
1986 (defun elmo-dop-queue-load ()
1987 (setq elmo-dop-queue
1989 (expand-file-name elmo-dop-queue-filename
1990 elmo-msgdb-directory))))
1992 (defun elmo-dop-queue-save ()
1994 (expand-file-name elmo-dop-queue-filename
1995 elmo-msgdb-directory)
1999 (product-provide (provide 'elmo-util) (require 'elmo-version))
2001 ;;; elmo-util.el ends here