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)
44 (autoload 'md5 "md5"))
46 (defvar elmo-work-buf-name " *elmo work*")
47 (defvar elmo-temp-buf-name " *elmo temp*")
49 (or (boundp 'default-enable-multibyte-characters)
50 (defvar default-enable-multibyte-characters (featurep 'mule)
51 "The mock variable except for Emacs 20."))
53 (defun elmo-base64-encode-string (string &optional no-line-break))
54 (defun elmo-base64-decode-string (string))
56 ;; base64 encoding/decoding
58 (fset 'elmo-base64-encode-string
59 (mel-find-function 'mime-encode-string "base64"))
60 (fset 'elmo-base64-decode-string
61 (mel-find-function 'mime-decode-string "base64"))
63 ;; Any Emacsen may have add-name-to-file(), because loadup.el requires it. :-p
64 ;; Check make-symbolic-link() instead. -- 981002 by Fuji
65 (if (fboundp 'make-symbolic-link) ;; xxx
66 (defalias 'elmo-add-name-to-file 'add-name-to-file)
67 (defun elmo-add-name-to-file
68 (filename newname &optional ok-if-already-exists)
69 (copy-file filename newname ok-if-already-exists t)))
71 (defmacro elmo-set-work-buf (&rest body)
72 "Execute BODY on work buffer. Work buffer remains."
74 (set-buffer (get-buffer-create elmo-work-buf-name))
75 (set-buffer-multibyte default-enable-multibyte-characters)
79 (put 'elmo-set-work-buf 'lisp-indent-function 0)
80 (def-edebug-spec elmo-set-work-buf t)
82 (defmacro elmo-bind-directory (dir &rest body)
83 "Set current directory DIR and execute BODY."
84 (` (let ((default-directory (file-name-as-directory (, dir))))
87 (put 'elmo-bind-directory 'lisp-indent-function 1)
88 (def-edebug-spec elmo-bind-directory
91 (defconst elmo-multibyte-buffer-name " *elmo-multibyte-buffer*")
93 (defmacro elmo-with-enable-multibyte (&rest body)
94 "Evaluate BODY with `default-enable-multibyte-character'."
95 `(with-current-buffer (get-buffer-create elmo-multibyte-buffer-name)
98 (put 'elmo-with-enable-multibyte 'lisp-indent-function 0)
99 (def-edebug-spec elmo-with-enable-multibyte t)
101 (defun elmo-object-load (filename &optional mime-charset no-err)
102 "Load OBJECT from the file specified by FILENAME.
103 File content is decoded with MIME-CHARSET."
104 (if (not (file-readable-p filename))
107 (as-binary-input-file
108 (insert-file-contents filename))
110 (set-buffer-multibyte default-enable-multibyte-characters)
111 (decode-mime-charset-region (point-min) (point-max) mime-charset))
113 (read (current-buffer))
114 (error (unless no-err
115 (message "Warning: Loading object from %s failed."
117 (elmo-object-save filename nil))
120 (defsubst elmo-save-buffer (filename &optional mime-charset)
121 "Save current buffer to the file specified by FILENAME.
122 Directory of the file is created if it doesn't exist.
123 File content is encoded with MIME-CHARSET."
124 (let ((dir (directory-file-name (file-name-directory filename))))
125 (if (file-directory-p dir)
127 (unless (file-exists-p dir)
128 (elmo-make-directory dir)))
129 (if (file-writable-p filename)
132 ;;; (set-buffer-multibyte default-enable-multibyte-characters)
133 (encode-mime-charset-region (point-min) (point-max) mime-charset))
134 (as-binary-output-file
135 (write-region (point-min) (point-max) filename nil 'no-msg)))
136 (message "%s is not writable." filename))))
138 (defun elmo-object-save (filename object &optional mime-charset)
139 "Save OBJECT to the file specified by FILENAME.
140 Directory of the file is created if it doesn't exist.
141 File content is encoded with MIME-CHARSET."
143 (let (print-length print-level)
144 (prin1 object (current-buffer)))
145 ;;; (princ "\n" (current-buffer))
146 (elmo-save-buffer filename mime-charset)))
150 (defconst elmo-condition-atom-regexp "[^/ \")|&]*")
152 (defsubst elmo-condition-parse-error ()
153 (error "Syntax error in '%s'" (buffer-string)))
155 (defun elmo-parse-search-condition (condition)
157 Return value is a cons cell of (STRUCTURE . REST)"
160 (goto-char (point-min))
161 (cons (elmo-condition-parse) (buffer-substring (point) (point-max)))))
163 ;; condition ::= or-expr
164 (defun elmo-condition-parse ()
165 (or (elmo-condition-parse-or-expr)
166 (elmo-condition-parse-error)))
168 ;; or-expr ::= and-expr /
169 ;; and-expr "|" or-expr
170 (defun elmo-condition-parse-or-expr ()
171 (let ((left (elmo-condition-parse-and-expr)))
172 (if (looking-at "| *")
174 (goto-char (match-end 0))
175 (list 'or left (elmo-condition-parse-or-expr)))
178 ;; and-expr ::= primitive /
179 ;; primitive "&" and-expr
180 (defun elmo-condition-parse-and-expr ()
181 (let ((left (elmo-condition-parse-primitive)))
182 (if (looking-at "& *")
184 (goto-char (match-end 0))
185 (list 'and left (elmo-condition-parse-and-expr)))
188 ;; primitive ::= "(" expr ")" /
189 ;; ["!"] search-key SPACE* ":" SPACE* search-value
190 (defun elmo-condition-parse-primitive ()
193 (goto-char (match-end 0))
194 (prog1 (elmo-condition-parse)
195 (unless (looking-at ") *")
196 (elmo-condition-parse-error))
197 (goto-char (match-end 0))))
198 ;; search-key ::= [A-Za-z-]+
199 ;; ;; "since" / "before" / "last" / "first" /
200 ;; ;; "body" / "flag" / field-name
201 ((looking-at "\\(!\\)? *\\([A-Za-z-]+\\) *: *")
202 (goto-char (match-end 0))
203 (let ((search-key (vector
204 (if (match-beginning 1) 'unmatch 'match)
205 (downcase (elmo-match-buffer 2))
206 (elmo-condition-parse-search-value))))
208 (if (string= (aref search-key 1) "tocc")
209 (if (eq (aref search-key 0) 'match)
211 (vector 'match "to" (aref search-key 2))
212 (vector 'match "cc" (aref search-key 2)))
214 (vector 'unmatch "to" (aref search-key 2))
215 (vector 'unmatch "cc" (aref search-key 2))))
218 ;; search-value ::= quoted / time / number / atom
219 ;; quoted ::= <elisp string expression>
220 ;; time ::= "yesterday" / "lastweek" / "lastmonth" / "lastyear" /
221 ;; number SPACE* "daysago" /
222 ;; number "-" month "-" number ; ex. 10-May-2000
223 ;; number "-" number "-" number ; ex. 2000-05-10
225 ;; month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
226 ;; "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
227 ;; atom ::= ATOM_CHARS*
228 ;; SPACE ::= <ascii space character, 0x20>
229 ;; ATOM_CHARS ::= <any character except specials>
230 ;; specials ::= SPACE / <"> / </> / <)> / <|> / <&>
231 ;; ;; These characters should be quoted.
232 (defun elmo-condition-parse-search-value ()
235 (read (current-buffer)))
236 ((or (looking-at elmo-condition-atom-regexp)
237 (looking-at "yesterday") (looking-at "lastweek")
238 (looking-at "lastmonth") (looking-at "lastyear")
239 (looking-at "[0-9]+ *daysago")
240 (looking-at "[0-9]+-[A-Za-z]+-[0-9]+")
241 (looking-at "[0-9]+-[0-9]+-[0-9]+")
242 (looking-at "[0-9]+"))
243 (prog1 (elmo-match-buffer 0)
244 (goto-char (match-end 0))))
245 (t (error "Syntax error '%s'" (buffer-string)))))
248 (defsubst elmo-buffer-replace (regexp &optional newtext)
249 (goto-char (point-min))
250 (while (re-search-forward regexp nil t)
251 (replace-match (or newtext ""))))
253 (defsubst elmo-delete-char (char string &optional unibyte)
256 (let ((coding-system-for-read 'no-conversion)
257 (coding-system-for-write 'no-conversion))
258 (if unibyte (set-buffer-multibyte nil))
260 (goto-char (point-min))
261 (while (search-forward (char-to-string char) nil t)
265 (defsubst elmo-delete-cr-buffer ()
266 "Delete CR from buffer."
268 (goto-char (point-min))
269 (while (search-forward "\r\n" nil t)
270 (replace-match "\n")) ))
272 (defsubst elmo-delete-cr-get-content-type ()
274 (goto-char (point-min))
275 (while (search-forward "\r\n" nil t)
276 (replace-match "\n"))
277 (goto-char (point-min))
278 (or (std11-field-body "content-type")
281 (defun elmo-delete-cr (string)
285 (goto-char (point-min))
286 (while (search-forward "\r\n" nil t)
287 (replace-match "\n"))
290 (defun elmo-last (list)
291 (and list (nth (1- (length list)) list)))
293 (defun elmo-set-list (vars vals)
296 (set (car vars) (car vals)))
297 (setq vars (cdr vars)
300 (defun elmo-uniq-list (lst &optional delete-function)
301 "Distractively uniqfy elements of LST."
302 (setq delete-function (or delete-function #'delete))
308 (funcall delete-function
313 (defun elmo-uniq-sorted-list (list &optional equal-function)
314 "Distractively uniqfy elements of sorted LIST."
315 (setq equal-function (or equal-function #'equal))
318 (while (funcall equal-function (car list) (cadr list))
319 (setcdr list (cddr list)))
320 (setq list (cdr list))))
323 (defun elmo-list-insert (list element after)
324 (let* ((match (memq after list))
325 (rest (and match (cdr (memq after list)))))
328 (setcdr match (list element))
330 (nconc list (list element)))))
332 (defun elmo-get-file-string (filename &optional remove-final-newline)
334 (let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
335 insert-file-contents-post-hook)
336 (when (file-exists-p filename)
338 (as-binary-input-file (insert-file-contents filename)))
339 (when (and remove-final-newline
341 (= (char-after (1- (point-max))) ?\n))
342 (goto-char (point-max))
343 (delete-backward-char 1))
346 (defun elmo-save-string (string filename)
349 (as-binary-output-file
351 (write-region (point-min) (point-max)
352 filename nil 'no-msg))
355 (defun elmo-max-of-list (nlist)
359 (if (< max-num (car l))
360 (setq max-num (car l)))
364 (defun elmo-concat-path (path filename)
365 (if (not (string= path ""))
366 (elmo-replace-in-string
367 (if (string= elmo-path-sep (substring path (- (length path) 1)))
368 (concat path filename)
369 (concat path elmo-path-sep filename))
370 (concat (regexp-quote elmo-path-sep)(regexp-quote elmo-path-sep))
374 (defvar elmo-passwd-alist nil)
376 (defun elmo-passwd-alist-load ()
378 (let ((filename (expand-file-name elmo-passwd-alist-file-name
379 elmo-msgdb-directory))
380 insert-file-contents-pre-hook ; To avoid autoconv-xmas...
381 insert-file-contents-post-hook
383 (if (not (file-readable-p filename))
385 (insert-file-contents filename)
387 (read (current-buffer))
390 (defun elmo-passwd-alist-clear ()
391 "Clear password cache."
393 (dolist (pair elmo-passwd-alist)
394 (when (stringp (cdr-safe pair))
395 (fillarray (cdr pair) 0)))
396 (setq elmo-passwd-alist nil))
398 (defun elmo-passwd-alist-save ()
399 "Save password into file."
402 (let ((filename (expand-file-name elmo-passwd-alist-file-name
403 elmo-msgdb-directory))
404 print-length print-level)
405 (prin1 elmo-passwd-alist (current-buffer))
406 (princ "\n" (current-buffer))
407 ;;; (if (and (file-exists-p filename)
408 ;;; (not (equal 384 (file-modes filename))))
409 ;;; (error "%s is not safe.chmod 600 %s!" filename filename))
410 (if (file-writable-p filename)
412 (write-region (point-min) (point-max)
413 filename nil 'no-msg)
414 (set-file-modes filename 384))
415 (message "%s is not writable." filename)))))
417 (defun elmo-get-passwd (key)
418 "Get password from password pool."
420 (if (not elmo-passwd-alist)
421 (setq elmo-passwd-alist (elmo-passwd-alist-load)))
422 (setq pair (assoc key elmo-passwd-alist))
424 (elmo-base64-decode-string (cdr pair))
425 (setq pass (elmo-read-passwd (format "Password for %s: "
427 (setq elmo-passwd-alist
428 (append elmo-passwd-alist
430 (elmo-base64-encode-string pass)))))
431 (if elmo-passwd-life-time
432 (run-with-timer elmo-passwd-life-time nil
433 (` (lambda () (elmo-remove-passwd (, key))))))
436 (defun elmo-remove-passwd (key)
437 "Remove password from password pool (for failure)."
439 (while (setq pass-cons (assoc key elmo-passwd-alist))
441 (fillarray (cdr pass-cons) 0)
442 (setq elmo-passwd-alist
443 (delete pass-cons elmo-passwd-alist))))))
445 (defmacro elmo-read-char-exclusive ()
446 (cond ((featurep 'xemacs)
447 '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
452 (key-press-event-p (setq event (next-command-event)))
453 (setq key (or (event-to-character event)
454 (cdr (assq (event-key event) table)))))))
456 ((fboundp 'read-char-exclusive)
457 '(read-char-exclusive))
461 (defun elmo-read-passwd (prompt &optional stars)
462 "Read a single line of text from user without echoing, and return it."
466 (cursor-in-echo-area t)
467 (log-message-max-size 0)
468 message-log-max done msg truncate)
470 (if (or (not stars) (string= "" ans))
472 (setq msg (concat prompt (make-string (length ans) ?.)))
474 (1+ (- (length msg) (window-width (minibuffer-window)))))
476 (setq msg (concat "$" (substring msg (1+ truncate))))))
478 (setq c (elmo-read-char-exclusive))
482 ((or (= c ?\r) (= c ?\n) (= c ?\e))
486 ((and (/= c ?\b) (/= c ?\177))
487 (setq ans (concat ans (char-to-string c))))
489 (setq ans (substring ans 0 -1)))))
498 (defun elmo-string-to-list (string)
501 (goto-char (point-min))
503 (goto-char (point-max))
505 (goto-char (point-min))
506 (read (current-buffer))))
508 (defun elmo-list-to-string (list)
517 (if (symbolp (car tlist))
518 (symbol-name (car tlist))
523 (setq tlist (cdr tlist)))
533 (defun elmo-plug-on-by-servers (alist &optional servers)
534 (let ((server-list (or servers elmo-plug-on-servers)))
537 (if (elmo-plugged-p (car server-list))
539 (setq server-list (cdr server-list))))))
541 (defun elmo-plug-on-by-exclude-servers (alist &optional servers)
542 (let ((server-list (or servers elmo-plug-on-exclude-servers))
543 server other-servers)
545 (when (and (not (member (setq server (caaar alist)) server-list))
546 (not (member server other-servers)))
547 (push server other-servers))
548 (setq alist (cdr alist)))
549 (elmo-plug-on-by-servers alist other-servers)))
551 (defun elmo-plugged-p (&optional server port stream-type alist label-exp)
552 (let ((alist (or alist elmo-plugged-alist))
554 (cond ((and (not port) (not server))
555 (cond ((eq elmo-plugged-condition 'one)
559 (if (nth 2 (car alist))
561 (setq alist (cdr alist))))
563 ((eq elmo-plugged-condition 'all)
567 (if (not (nth 2 (car alist)))
568 (throw 'plugged nil))
569 (setq alist (cdr alist)))
572 ((functionp elmo-plugged-condition)
573 (funcall elmo-plugged-condition alist))
576 ((not port) ;; server
579 (when (string= server (caaar alist))
580 (if (nth 2 (car alist))
582 (setq alist (cdr alist)))))
584 (setq plugged-info (assoc (list server port stream-type) alist))
585 (if (not plugged-info)
586 ;; add elmo-plugged-alist automatically
588 (elmo-set-plugged elmo-plugged server port stream-type
589 nil nil nil label-exp)
591 (if (and elmo-auto-change-plugged
592 (> elmo-auto-change-plugged 0)
593 (nth 3 plugged-info) ;; time
594 (elmo-time-expire (nth 3 plugged-info)
595 elmo-auto-change-plugged))
597 (nth 2 plugged-info)))))))
599 (defun elmo-set-plugged (plugged &optional server port stream-type time
601 (let ((alist (or alist elmo-plugged-alist))
603 (cond ((and (not port) (not server))
604 (setq elmo-plugged plugged)
605 ;; set plugged all element of elmo-plugged-alist.
607 (setcdr (cdar alist) (list plugged time))
608 (setq alist (cdr alist))))
610 ;; set plugged all port of server
612 (when (string= server (caaar alist))
613 (setcdr (cdar alist) (list plugged time)))
614 (setq alist (cdr alist))))
616 ;; set plugged one port of server
617 (setq plugged-info (assoc (list server port stream-type) alist))
618 (setq label (if label-exp
620 (nth 1 plugged-info)))
622 ;; if add is non-nil, don't reset plug state.
624 (setcdr plugged-info (list label plugged time)))
626 (setq elmo-plugged-alist
630 (list (list server port stream-type)
631 label plugged time))))))))
634 (defun elmo-delete-plugged (&optional server port alist)
635 (let* ((alist (or alist elmo-plugged-alist))
637 (cond ((and (not port) (not server))
640 ;; delete plugged all port of server
642 (when (string= server (caaar alist2))
643 (setq alist (delete (car alist2) alist)))
644 (setq alist2 (cdr alist2))))
646 ;; delete plugged one port of server
648 (delete (assoc (cons server port) alist) alist))))
651 (defun elmo-disk-usage (path)
652 "Get disk usage (bytes) in PATH."
654 (condition-case () (file-attributes path) (error nil))))
656 (if (nth 0 file-attr) ; directory
657 (let ((files (condition-case ()
658 (directory-files path t "^[^\\.]")
661 ;; (result (nth 7 file-attr))) ... directory size
663 (setq result (+ result (or (elmo-disk-usage (car files)) 0)))
664 (setq files (cdr files)))
666 (float (nth 7 file-attr)))
669 (defun elmo-get-last-accessed-time (path &optional dir)
670 "Return the last accessed time of PATH."
671 (let ((last-accessed (nth 4 (file-attributes (or (and dir
676 (setq last-accessed (+ (* (nth 0 last-accessed)
677 (float 65536)) (nth 1 last-accessed)))
680 (defun elmo-get-last-modification-time (path &optional dir)
681 "Return the last accessed time of PATH."
682 (let ((last-modified (nth 5 (file-attributes (or (and dir
686 (setq last-modified (+ (* (nth 0 last-modified)
687 (float 65536)) (nth 1 last-modified)))))
689 (defun elmo-make-directory (path &optional mode)
690 "Create directory recursively."
691 (let ((parent (directory-file-name (file-name-directory path))))
692 (if (null (file-directory-p parent))
693 (elmo-make-directory parent))
694 (make-directory path)
695 (set-file-modes path (or mode
696 (+ (* 64 7) (* 8 0) 0))))) ; chmod 0700
698 (defun elmo-delete-directory (path &optional no-hierarchy)
699 "Delete directory recursively."
700 (if (stringp path) ; nil is not permitted.
701 (let ((dirent (directory-files path))
702 relpath abspath hierarchy)
704 (setq relpath (car dirent)
706 abspath (expand-file-name relpath path))
707 (when (not (string-match "^\\.\\.?$" relpath))
708 (if (eq (nth 0 (file-attributes abspath)) t)
711 (elmo-delete-directory abspath no-hierarchy))
712 (delete-file abspath))))
714 (delete-directory path)))))
716 (defun elmo-delete-match-files (path regexp &optional remove-if-empty)
717 "Delete directory files specified by PATH.
718 If optional REMOVE-IF-EMPTY is non-nil, delete directory itself if
719 the directory becomes empty after deletion."
720 (when (stringp path) ; nil is not permitted.
721 (dolist (file (directory-files path t regexp))
725 (delete-directory path) ; should be removed if empty.
728 (defun elmo-list-filter (l1 l2)
729 "Rerurn a list from L2 in which each element is a member of L1."
730 (elmo-delete-if (lambda (x) (not (memq x l1))) l2))
732 (defsubst elmo-list-delete-if-smaller (list number)
733 (let ((ret-val (copy-sequence list)))
735 (if (< (car list) number)
736 (setq ret-val (delq (car list) ret-val)))
737 (setq list (cdr list)))
740 (defun elmo-list-diff (list1 list2)
741 (let ((clist1 (sort (copy-sequence list1) #'<))
742 (clist2 (sort (copy-sequence list2) #'<))
743 list1-only list2-only)
744 (while (or clist1 clist2)
748 (setq list2-only (cons (car clist2) list2-only))
749 (setq clist2 (cdr clist2))))
752 (setq list1-only (cons (car clist1) list1-only))
753 (setq clist1 (cdr clist1))))
754 ((< (car clist1) (car clist2))
755 (while (and clist1 (< (car clist1) (car clist2)))
756 (setq list1-only (cons (car clist1) list1-only))
757 (setq clist1 (cdr clist1))))
758 ((< (car clist2) (car clist1))
759 (while (and clist2 (< (car clist2) (car clist1)))
760 (setq list2-only (cons (car clist2) list2-only))
761 (setq clist2 (cdr clist2))))
762 ((= (car clist1) (car clist2))
763 (setq clist1 (cdr clist1)
764 clist2 (cdr clist2)))))
765 (list list1-only list2-only)))
767 (defun elmo-list-diff-nonsortable (list1 list2)
768 (let ((clist1 (copy-sequence list1))
769 (clist2 (copy-sequence list2)))
771 (setq clist1 (delq (car list2) clist1))
772 (setq list2 (cdr list2)))
774 (setq clist2 (delq (car list1) clist2))
775 (setq list1 (cdr list1)))
776 (list clist1 clist2)))
778 (defun elmo-list-bigger-diff (list1 list2 &optional mes)
779 "Returns a list (- +). + is bigger than max of LIST1, in LIST2."
784 (max-of-l2 (or (nth (max 0 (1- (length l2))) l2) 0))
788 (setq num (+ (length l1)))
790 (if (memq (car l1) l2)
791 (if (eq (car l1) (car l2))
794 (if (> (car l1) max-of-l2)
795 (setq diff1 (nconc diff1 (list (car l1))))))
799 (setq percent (/ (* i 100) num))
800 (if (eq (% percent 5) 0)
801 (elmo-display-progress
802 'elmo-list-bigger-diff "%s%d%%" percent mes))))
804 (cons diff1 (list l2)))))
806 (defmacro elmo-filter-condition-p (filter)
807 `(or (vectorp ,filter) (consp ,filter)))
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 ((field-date (elmo-date-make-sortable-string
834 (std11-field-body "date")
835 (current-time-zone) nil)))
836 (specified-date (elmo-date-make-sortable-string
837 (elmo-date-get-datevec
838 (elmo-filter-value condition)))))
840 (or (string= field-date specified-date)
841 (string< specified-date field-date)))))
842 ((string= (elmo-filter-key condition) "before")
845 (elmo-date-make-sortable-string
847 (std11-field-body "date")
848 (current-time-zone) nil))
849 (elmo-date-make-sortable-string
850 (elmo-date-get-datevec
851 (elmo-filter-value condition))))))
852 ((string= (elmo-filter-key condition) "body")
853 (and (re-search-forward "^$" nil t) ; goto body
854 (setq result (search-forward (elmo-filter-value condition)
857 (dolist (fval (elmo-multiple-field-body (elmo-filter-key condition)))
858 (if (eq (length fval) 0) (setq fval nil))
859 (if fval (setq fval (eword-decode-string fval)))
860 (setq result (or result
861 (and fval (string-match
862 (elmo-filter-value condition) fval)))))))
863 (if (eq (elmo-filter-type condition) 'unmatch)
864 (setq result (not result)))
867 (defun elmo-condition-in-msgdb-p-internal (condition fields)
870 (if (not (member (elmo-filter-key condition) fields))
872 ((or (eq (car condition) 'and)
873 (eq (car condition) 'or))
874 (elmo-condition-in-msgdb-p-internal (nth 1 condition) fields)
875 (elmo-condition-in-msgdb-p-internal (nth 2 condition) fields))))
877 (defun elmo-condition-in-msgdb-p (condition)
879 (elmo-condition-in-msgdb-p-internal condition
881 elmo-msgdb-extra-fields
882 '("last" "first" "from"
883 "subject" "to" "cc" "since"
886 (defun elmo-buffer-field-condition-match (condition number number-list)
889 (elmo-buffer-field-primitive-condition-match
890 condition number number-list))
891 ((eq (car condition) 'and)
892 (and (elmo-buffer-field-condition-match
893 (nth 1 condition) number number-list)
894 (elmo-buffer-field-condition-match
895 (nth 2 condition) number number-list)))
896 ((eq (car condition) 'or)
897 (or (elmo-buffer-field-condition-match
898 (nth 1 condition) number number-list)
899 (elmo-buffer-field-condition-match
900 (nth 2 condition) number number-list)))))
902 (defsubst elmo-file-field-primitive-condition-match (file
907 (goto-char (point-min))
909 ((string= (elmo-filter-key condition) "last")
910 (setq result (<= (length (memq number number-list))
911 (string-to-int (elmo-filter-value condition))))
912 (if (eq (elmo-filter-type condition) 'unmatch)
913 (setq result (not result))))
914 ((string= (elmo-filter-key condition) "first")
915 (setq result (< (- (length number-list)
916 (length (memq number number-list)))
917 (string-to-int (elmo-filter-value condition))))
918 (if (eq (elmo-filter-type condition) 'unmatch)
919 (setq result (not result))))
922 (as-binary-input-file (insert-file-contents file))
923 (set-buffer-multibyte default-enable-multibyte-characters)
924 ;; Should consider charset?
925 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
927 (elmo-buffer-field-primitive-condition-match
928 condition number number-list)))))
931 (defun elmo-file-field-condition-match (file condition number number-list)
934 (elmo-file-field-primitive-condition-match
935 file condition number number-list))
936 ((eq (car condition) 'and)
937 (and (elmo-file-field-condition-match
938 file (nth 1 condition) number number-list)
939 (elmo-file-field-condition-match
940 file (nth 2 condition) number number-list)))
941 ((eq (car condition) 'or)
942 (or (elmo-file-field-condition-match
943 file (nth 1 condition) number number-list)
944 (elmo-file-field-condition-match
945 file (nth 2 condition) number number-list)))))
947 (defmacro elmo-get-hash-val (string hashtable)
948 (static-if (fboundp 'unintern)
949 `(symbol-value (intern-soft ,string ,hashtable))
950 `(let ((sym (intern-soft ,string ,hashtable)))
952 (symbol-value sym)))))
954 (defmacro elmo-set-hash-val (string value hashtable)
955 `(set (intern ,string ,hashtable) ,value))
957 (defmacro elmo-clear-hash-val (string hashtable)
958 (static-if (fboundp 'unintern)
959 (list 'unintern string hashtable)
960 (list 'makunbound (list 'intern string hashtable))))
962 (defmacro elmo-unintern (string)
963 "`unintern' symbol named STRING, When can use `unintern'.
964 Emacs 19.28 or earlier does not have `unintern'."
965 (static-if (fboundp 'unintern)
966 (list 'unintern string)))
968 (defun elmo-make-hash (&optional hashsize)
969 "Make a new hash table which have HASHSIZE size."
973 ;; Prime numbers as lengths tend to result in good
974 ;; hashing; lengths one less than a power of two are
978 (while (< (- i 1) hashsize)
981 elmo-hash-maximum-size)
982 elmo-hash-minimum-size)
983 elmo-hash-minimum-size)
986 (defsubst elmo-mime-string (string)
987 "Normalize MIME encoded STRING."
989 (elmo-with-enable-multibyte
990 (encode-mime-charset-string
991 (eword-decode-and-unfold-unstructured-field-body string)
992 elmo-mime-charset))))
994 (defsubst elmo-collect-field (beg end downcase-field-name)
997 (narrow-to-region beg end)
998 (goto-char (point-min))
999 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1001 (while (re-search-forward regexp nil t)
1002 (setq name (buffer-substring-no-properties
1003 (match-beginning 1)(1- (match-end 1))))
1004 (if downcase-field-name
1005 (setq name (downcase name)))
1006 (setq body (buffer-substring-no-properties
1007 (match-end 0) (std11-field-end)))
1008 (or (assoc name dest)
1009 (setq dest (cons (cons name body) dest))))
1012 (defsubst elmo-collect-field-from-string (string downcase-field-name)
1015 (goto-char (point-min))
1016 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1018 (while (re-search-forward regexp nil t)
1019 (setq name (buffer-substring-no-properties
1020 (match-beginning 1)(1- (match-end 1))))
1021 (if downcase-field-name
1022 (setq name (downcase name)))
1023 (setq body (buffer-substring-no-properties
1024 (match-end 0) (std11-field-end)))
1025 (or (assoc name dest)
1026 (setq dest (cons (cons name body) dest))))
1029 (defun elmo-safe-filename (folder)
1030 (elmo-replace-in-string
1031 (elmo-replace-in-string
1032 (elmo-replace-in-string folder "/" " ")
1036 (defvar elmo-filename-replace-chars nil)
1038 (defsubst elmo-replace-string-as-filename (msgid)
1039 "Replace string as filename."
1040 (setq msgid (elmo-replace-in-string msgid " " " "))
1041 (if (null elmo-filename-replace-chars)
1042 (setq elmo-filename-replace-chars
1043 (regexp-quote (mapconcat
1044 'car elmo-filename-replace-string-alist ""))))
1045 (while (string-match (concat "[" elmo-filename-replace-chars "]")
1048 (substring msgid 0 (match-beginning 0))
1051 (match-beginning 0) (match-end 0))
1052 elmo-filename-replace-string-alist))
1053 (substring msgid (match-end 0)))))
1056 (defsubst elmo-recover-string-from-filename (filename)
1057 "Recover string from FILENAME."
1059 (while (string-match " " filename)
1060 (setq tmp (substring filename
1062 (+ (match-end 0) 1)))
1063 (if (string= tmp " ")
1065 (setq tmp (car (rassoc tmp
1066 elmo-filename-replace-string-alist))))
1069 (substring filename 0 (match-beginning 0))
1071 (setq filename (substring filename (+ (match-end 0) 1))))
1072 (concat result filename)))
1074 (defsubst elmo-copy-file (src dst &optional ok-if-already-exists)
1076 (elmo-add-name-to-file src dst ok-if-already-exists)
1077 (error (copy-file src dst ok-if-already-exists t))))
1079 (defsubst elmo-buffer-exists-p (buffer)
1080 (if (bufferp buffer)
1081 (buffer-live-p buffer)
1082 (get-buffer buffer)))
1084 (defsubst elmo-kill-buffer (buffer)
1085 (when (elmo-buffer-exists-p buffer)
1086 (kill-buffer buffer)))
1088 (defun elmo-delete-if (pred lst)
1089 "Return new list contain items which don't satisfy PRED in LST."
1092 (unless (funcall pred (car lst))
1093 (setq result (cons (car lst) result)))
1094 (setq lst (cdr lst)))
1097 (defun elmo-list-delete (list1 list2 &optional delete-function)
1098 "Delete by side effect any occurrences equal to elements of LIST1 from LIST2.
1099 Return the modified LIST2. Deletion is done with `delete'.
1100 Write `(setq foo (elmo-list-delete bar foo))' to be sure of changing
1102 If optional DELETE-FUNCTION is speficied, it is used as delete procedure."
1103 (setq delete-function (or delete-function 'delete))
1105 (setq list2 (funcall delete-function (car list1) list2))
1106 (setq list1 (cdr list1)))
1109 (defun elmo-list-member (list1 list2)
1110 "If any element of LIST1 is member of LIST2, return t."
1113 (if (member (car list1) list2)
1115 (setq list1 (cdr list1)))))
1117 (defun elmo-count-matches (regexp beg end)
1121 (while (re-search-forward regexp end t)
1122 (setq count (1+ count)))
1125 (if (fboundp 'display-error)
1126 (defalias 'elmo-display-error 'display-error)
1127 (defun elmo-display-error (error-object stream)
1128 "A tiny function to display ERROR-OBJECT to the STREAM."
1130 (errobj error-object)
1133 (setq err-mes (concat err-mes (format
1134 (if (stringp (car errobj))
1138 (setq errobj (cdr errobj))
1139 (if errobj (setq err-mes (concat err-mes (if first ": " ", "))))
1141 (princ err-mes stream))))
1143 (if (fboundp 'define-error)
1144 (defalias 'elmo-define-error 'define-error)
1145 (defun elmo-define-error (error doc &optional parents)
1147 (setq parents 'error))
1148 (let ((conds (get parents 'error-conditions)))
1150 (error "Not an error symbol: %s" error))
1152 (list 'error-message doc
1153 'error-conditions (cons error conds))))))
1155 (cond ((fboundp 'progress-feedback-with-label)
1156 (defalias 'elmo-display-progress 'progress-feedback-with-label))
1157 ((fboundp 'lprogress-display)
1158 (defalias 'elmo-display-progress 'lprogress-display))
1160 (defun elmo-display-progress (label format &optional value &rest args)
1161 "Print a progress message."
1162 (if (and (null format) (null args))
1164 (apply (function message) (concat format " %d%%")
1165 (nconc args (list value)))))))
1167 (defvar elmo-progress-counter-alist nil)
1169 (defmacro elmo-progress-counter-value (counter)
1170 (` (aref (cdr (, counter)) 0)))
1172 (defmacro elmo-progress-counter-all-value (counter)
1173 (` (aref (cdr (, counter)) 1)))
1175 (defmacro elmo-progress-counter-format (counter)
1176 (` (aref (cdr (, counter)) 2)))
1178 (defmacro elmo-progress-counter-set-value (counter value)
1179 (` (aset (cdr (, counter)) 0 (, value))))
1181 (defun elmo-progress-set (label all-value &optional format)
1182 (unless (assq label elmo-progress-counter-alist)
1183 (setq elmo-progress-counter-alist
1184 (cons (cons label (vector 0 all-value (or format "")))
1185 elmo-progress-counter-alist))))
1187 (defun elmo-progress-clear (label)
1188 (let ((counter (assq label elmo-progress-counter-alist)))
1190 (elmo-display-progress label
1191 (elmo-progress-counter-format counter)
1193 (setq elmo-progress-counter-alist
1194 (delq counter elmo-progress-counter-alist)))))
1196 (defun elmo-progress-notify (label &optional value op &rest args)
1197 (let ((counter (assq label elmo-progress-counter-alist)))
1199 (let* ((value (or value 1))
1200 (cur-value (elmo-progress-counter-value counter))
1201 (all-value (elmo-progress-counter-all-value counter))
1202 (new-value (if (eq op 'set) value (+ cur-value value)))
1203 (cur-rate (/ (* cur-value 100) all-value))
1204 (new-rate (/ (* new-value 100) all-value)))
1205 (elmo-progress-counter-set-value counter new-value)
1206 (unless (= cur-rate new-rate)
1207 (apply 'elmo-display-progress
1209 (elmo-progress-counter-format counter)
1212 (when (>= new-rate 100)
1213 (elmo-progress-clear label))))))
1215 (put 'elmo-with-progress-display 'lisp-indent-function '2)
1216 (def-edebug-spec elmo-with-progress-display
1217 (form (symbolp form &optional form) &rest form))
1219 (defmacro elmo-with-progress-display (condition spec &rest body)
1220 "Evaluate BODY with progress gauge if CONDITION is non-nil.
1221 SPEC is a list as followed (LABEL MAX-VALUE [FORMAT])."
1222 (let ((label (car spec))
1223 (max-value (cadr spec))
1228 (elmo-progress-set (quote ,label) ,max-value ,fmt))
1230 (elmo-progress-clear (quote ,label)))))
1232 (defun elmo-time-expire (before-time diff-time)
1233 (let* ((current (current-time))
1234 (rest (when (< (nth 1 current) (nth 1 before-time))
1238 (list (- (+ (car current) (if rest -1 0)) (car before-time))
1239 (- (+ (or rest 0) (nth 1 current)) (nth 1 before-time))))
1240 (and (eq (car diff) 0)
1241 (< diff-time (nth 1 diff)))))
1243 (if (fboundp 'std11-fetch-field)
1244 (defalias 'elmo-field-body 'std11-fetch-field) ;;no narrow-to-region
1245 (defalias 'elmo-field-body 'std11-field-body))
1247 (defun elmo-unfold-field-body (name)
1248 (let ((value (elmo-field-body name)))
1250 (std11-unfold-string value))))
1252 (defun elmo-decoded-field-body (field-name &optional mode)
1253 (let ((field-body (elmo-field-body field-name)))
1255 (elmo-with-enable-multibyte
1256 (mime-decode-field-body field-body field-name mode)))))
1258 (defun elmo-address-quote-specials (word)
1259 "Make quoted string of WORD if needed."
1260 (let ((lal (std11-lexical-analyze word)))
1261 (if (or (assq 'specials lal)
1262 (assq 'domain-literal lal))
1263 (prin1-to-string word)
1266 (defmacro elmo-string (string)
1267 "STRING without text property."
1268 (` (let ((obj (copy-sequence (, string))))
1269 (and obj (set-text-properties 0 (length obj) nil obj))
1272 (defun elmo-flatten (list-of-list)
1273 "Flatten LIST-OF-LIST."
1274 (unless (null list-of-list)
1275 (append (if (and (car list-of-list)
1276 (listp (car list-of-list)))
1278 (list (car list-of-list)))
1279 (elmo-flatten (cdr list-of-list)))))
1281 (defun elmo-y-or-n-p (prompt &optional auto default)
1282 "Same as `y-or-n-p'.
1283 But if optional argument AUTO is non-nil, DEFAULT is returned."
1288 (defun elmo-string-member (string slist)
1291 (if (and (stringp (car slist))
1292 (string= string (car slist)))
1294 (setq slist (cdr slist)))))
1296 (static-cond ((fboundp 'member-ignore-case)
1297 (defalias 'elmo-string-member-ignore-case 'member-ignore-case))
1298 ((fboundp 'compare-strings)
1299 (defun elmo-string-member-ignore-case (elt list)
1300 "Like `member', but ignores differences in case and text representation.
1301 ELT must be a string. Upper-case and lower-case letters are treated as equal.
1302 Unibyte strings are converted to multibyte for comparison."
1303 (while (and list (not (eq t (compare-strings elt 0 nil (car list) 0 nil t))))
1304 (setq list (cdr list)))
1307 (defun elmo-string-member-ignore-case (elt list)
1308 "Like `member', but ignores differences in case and text representation.
1309 ELT must be a string. Upper-case and lower-case letters are treated as equal."
1310 (let ((str (downcase elt)))
1311 (while (and list (not (string= str (downcase (car list)))))
1312 (setq list (cdr list)))
1315 (defun elmo-string-match-member (str list &optional case-ignore)
1316 (let ((case-fold-search case-ignore))
1319 (if (string-match (car list) str)
1320 (throw 'member (car list)))
1321 (setq list (cdr list))))))
1323 (defun elmo-string-matched-member (str list &optional case-ignore)
1324 (let ((case-fold-search case-ignore))
1327 (if (string-match str (car list))
1328 (throw 'member (car list)))
1329 (setq list (cdr list))))))
1331 (defsubst elmo-string-delete-match (string pos)
1332 (concat (substring string
1333 0 (match-beginning pos))
1338 (defun elmo-string-match-assoc (key alist &optional case-ignore)
1339 (let ((case-fold-search case-ignore)
1343 (setq a (car alist))
1346 (string-match key (car a)))
1348 (setq alist (cdr alist))))))
1350 (defun elmo-string-matched-assoc (key alist &optional case-ignore)
1351 (let ((case-fold-search case-ignore)
1355 (setq a (car alist))
1358 (string-match (car a) key))
1360 (setq alist (cdr alist))))))
1362 (defun elmo-string-assoc (key alist)
1366 (setq a (car alist))
1369 (string= key (car a)))
1371 (setq alist (cdr alist))))))
1373 (defun elmo-string-assoc-all (key alist)
1376 (if (string= key (car (car alist)))
1380 (setq alist (cdr alist)))
1383 (defun elmo-string-rassoc (key alist)
1387 (setq a (car alist))
1390 (string= key (cdr a)))
1392 (setq alist (cdr alist))))))
1394 (defun elmo-string-rassoc-all (key alist)
1397 (if (string= key (cdr (car alist)))
1401 (setq alist (cdr alist)))
1404 (defun elmo-expand-newtext (newtext original)
1405 (let ((len (length newtext))
1407 c expanded beg N did-expand)
1410 (while (and (< pos len)
1411 (not (= (aref newtext pos) ?\\)))
1412 (setq pos (1+ pos)))
1414 (push (substring newtext beg pos) expanded))
1416 ;; We hit a \; expand it.
1419 c (aref newtext pos))
1420 (if (not (or (= c ?\&)
1423 ;; \ followed by some character we don't expand.
1424 (push (char-to-string c) expanded)
1429 (when (match-beginning N)
1430 (push (substring original (match-beginning N) (match-end N))
1432 (setq pos (1+ pos)))
1434 (apply (function concat) (nreverse expanded))
1437 ;;; Folder parser utils.
1438 (defun elmo-parse-token (string &optional seps)
1439 "Parse atom from STRING using SEPS as a string of separator char list."
1440 (let ((len (length string))
1441 (seps (and seps (string-to-char-list seps)))
1447 (while (and (< i len) (or in (null sep)))
1448 (setq c (aref string i))
1450 ((and in (eq c ?\\))
1452 content (cons (aref string i) content)
1457 (in (setq content (cons c content)
1461 (t (setq content (cons c content)
1463 (if in (error "Parse error in quoted"))
1464 (cons (if (null content) "" (char-list-to-string (nreverse content)))
1465 (substring string i)))))
1467 (defun elmo-parse-prefixed-element (prefix string &optional seps)
1468 (if (and (not (eq (length string) 0))
1469 (eq (aref string 0) prefix))
1470 (elmo-parse-token (substring string 1) seps)
1473 ;;; Number set defined by OKAZAKI Tetsurou <okazaki@be.to>
1475 ;; number ::= [0-9]+
1478 ;; number-range ::= "(" beg " . " end ")" ;; cons cell
1479 ;; number-set-elem ::= number / number-range
1480 ;; number-set ::= "(" *number-set-elem ")" ;; list
1482 (defun elmo-number-set-member (number number-set)
1483 "Return non-nil if NUMBER is an element of NUMBER-SET.
1484 The value is actually the tail of NUMBER-RANGE whose car contains NUMBER."
1485 (or (memq number number-set)
1487 (while (and number-set (not found))
1488 (if (and (consp (car number-set))
1489 (and (<= (car (car number-set)) number)
1490 (<= number (cdr (car number-set)))))
1492 (setq number-set (cdr number-set))))
1495 (defun elmo-number-set-append-list (number-set list)
1496 "Append LIST of numbers to the NUMBER-SET.
1497 NUMBER-SET is altered."
1498 (let ((appended number-set))
1500 (setq appended (elmo-number-set-append appended (car list)))
1501 (setq list (cdr list)))
1504 (defun elmo-number-set-append (number-set number)
1505 "Append NUMBER to the NUMBER-SET.
1506 NUMBER-SET is altered."
1507 (let ((number-set-1 number-set)
1509 (while (and number-set (not found))
1510 (setq elem (car number-set))
1513 (eq (+ 1 (cdr elem)) number))
1514 (setcdr elem number)
1516 ((and (integerp elem)
1517 (eq (+ 1 elem) number))
1518 (setcar number-set (cons elem number))
1520 ((or (and (integerp elem) (eq elem number))
1522 (<= (car elem) number)
1523 (<= number (cdr elem))))
1525 (setq number-set (cdr number-set)))
1527 (setq number-set-1 (nconc number-set-1 (list number))))
1530 (defun elmo-number-set-delete-list (number-set list)
1531 "Delete LIST of numbers from the NUMBER-SET.
1532 NUMBER-SET is altered."
1533 (let ((deleted number-set))
1534 (dolist (number list)
1535 (setq deleted (elmo-number-set-delete deleted number)))
1538 (defun elmo-number-set-delete (number-set number)
1539 "Delete NUMBER from the NUMBER-SET.
1540 NUMBER-SET is altered."
1541 (let* ((curr number-set)
1542 (top (cons 'dummy number-set))
1545 (while (and curr (not found))
1546 (setq elem (car curr))
1549 ((eq (car elem) number)
1550 (if (eq (cdr elem) (1+ number))
1551 (setcar curr (cdr elem))
1552 (setcar elem (1+ number)))
1554 ((eq (cdr elem) number)
1555 (if (eq (car elem) (1- number))
1556 (setcar curr (car elem))
1557 (setcdr elem (1- number)))
1559 ((and (> number (car elem))
1560 (< number (cdr elem)))
1565 ;; (beg . (1- number))
1566 (let ((new (cons (car elem) (1- number))))
1567 (if (eq (car new) (cdr new))
1570 ;; ((1+ number) . end)
1571 (let ((new (cons (1+ number) (cdr elem))))
1572 (if (eq (car new) (cdr new))
1576 (when (eq elem number)
1577 (setcdr prev (cdr curr))
1583 (defun elmo-make-number-list (beg end)
1584 (let (number-list i)
1587 (setq number-list (cons i number-list))
1591 (defun elmo-number-set-to-number-list (number-set)
1592 "Return a number list which corresponds to NUMBER-SET."
1593 (let ((number-list (list 'dummy))
1596 (setq elem (car number-set))
1599 (nconc number-list (elmo-make-number-list (car elem) (cdr elem))))
1601 (nconc number-list (list elem))))
1602 (setq number-set (cdr number-set)))
1605 (defcustom elmo-list-subdirectories-ignore-regexp "^\\(\\.\\.?\\|[0-9]+\\)$"
1606 "*Regexp to filter subfolders."
1610 (defun elmo-list-subdirectories-1 (basedir curdir one-level)
1611 (let ((root (zerop (length curdir)))
1612 (w32-get-true-file-link-count t) ; for Meadow
1615 (dolist (file (directory-files (setq dir (expand-file-name curdir basedir))))
1616 (when (and (not (string-match
1617 elmo-list-subdirectories-ignore-regexp
1619 (car (setq attr (file-attributes
1620 (expand-file-name file dir)))))
1621 (when (eq one-level 'check) (throw 'done t))
1623 (concat curdir (and (not root) elmo-path-sep) file))
1625 (setq dirs (nconc dirs
1626 (if (if elmo-have-link-count (< 2 (nth 1 attr))
1628 (elmo-list-subdirectories-1
1631 (if one-level 'check))))
1633 (list (list relpath))
1636 (elmo-list-subdirectories-1
1640 (list relpath)))))))
1643 (defun elmo-list-subdirectories (directory file one-level)
1644 (let ((subdirs (elmo-list-subdirectories-1 directory file one-level)))
1645 (if (zerop (length file))
1647 (cons file subdirs))))
1649 (defun elmo-mapcar-list-of-list (func list-of-list)
1652 (cond ((listp x) (elmo-mapcar-list-of-list func x))
1653 (t (funcall func x))))
1656 (defun elmo-parse (string regexp &optional matchn)
1657 (or matchn (setq matchn 1))
1659 (store-match-data nil)
1660 (while (string-match regexp string (match-end 0))
1661 (setq list (cons (substring string (match-beginning matchn)
1662 (match-end matchn)) list)))
1666 (defmacro elmo-make-file-cache (path status)
1667 "PATH is the cache file name.
1668 STATUS is one of 'section, 'entire or nil.
1669 nil means no cache exists.
1670 'section means partial section cache exists.
1671 'entire means entire cache exists.
1672 If the cache is partial file-cache, TYPE is 'partial."
1673 (` (cons (, path) (, status))))
1675 (defmacro elmo-file-cache-path (file-cache)
1676 "Returns the file path of the FILE-CACHE."
1677 (` (car (, file-cache))))
1679 (defmacro elmo-file-cache-status (file-cache)
1680 "Returns the status of the FILE-CACHE."
1681 (` (cdr (, file-cache))))
1683 (defsubst elmo-cache-to-msgid (filename)
1684 (concat "<" (elmo-recover-string-from-filename filename) ">"))
1686 (defsubst elmo-cache-get-path-subr (msgid)
1687 (let ((chars '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?A ?B ?C ?D ?E ?F))
1688 (clist (string-to-char-list msgid))
1691 (setq sum (+ sum (car clist)))
1692 (setq clist (cdr clist)))
1694 (nth (% (/ sum 16) 2) chars)
1695 (nth (% sum 16) chars))))
1698 (defun elmo-file-cache-get-path (msgid &optional section)
1699 "Get cache path for MSGID.
1700 If optional argument SECTION is specified, partial cache path is returned."
1701 (if (setq msgid (elmo-msgid-to-cache msgid))
1704 (format "%s/%s/%s/%s"
1705 elmo-cache-directory
1706 (elmo-cache-get-path-subr msgid)
1710 elmo-cache-directory
1711 (elmo-cache-get-path-subr msgid)
1714 (defmacro elmo-file-cache-expand-path (path section)
1715 "Return file name for the file-cache corresponds to the section.
1716 PATH is the file-cache path.
1717 SECTION is the section string."
1718 (` (expand-file-name (or (, section) "") (, path))))
1720 (defun elmo-file-cache-delete (path)
1721 "Delete a cache on PATH."
1722 (when (file-exists-p path)
1723 (if (file-directory-p path)
1725 (dolist (file (directory-files path t "^[^\\.]"))
1727 (delete-directory path))
1731 (defun elmo-file-cache-exists-p (msgid)
1732 "Returns 'section or 'entire if a cache which corresponds to MSGID exists."
1733 (elmo-file-cache-status (elmo-file-cache-get msgid)))
1735 (defun elmo-file-cache-save (cache-path section)
1736 "Save current buffer as cache on PATH.
1737 Return t if cache is saved successfully."
1739 (let ((path (if section (expand-file-name section cache-path)
1742 (if (and (null section)
1743 (file-directory-p path))
1745 (setq files (directory-files path t "^[^\\.]"))
1747 (delete-file (car files))
1748 (setq files (cdr files)))
1749 (delete-directory path))
1751 (not (file-directory-p cache-path)))
1752 (delete-file cache-path)))
1754 (setq dir (directory-file-name (file-name-directory path)))
1755 (if (not (file-exists-p dir))
1756 (elmo-make-directory dir))
1757 (write-region-as-binary (point-min) (point-max)
1763 (defun elmo-file-cache-load (cache-path section)
1764 "Load cache on PATH into the current buffer.
1765 Return t if cache is loaded successfully."
1768 (when (and cache-path
1769 (if (elmo-cache-path-section-p cache-path)
1772 (setq cache-file (elmo-file-cache-expand-path
1775 (file-exists-p cache-file))
1776 (insert-file-contents-as-binary cache-file)
1781 (defun elmo-cache-path-section-p (path)
1782 "Return non-nil when PATH is `section' cache path."
1783 (file-directory-p path))
1785 (defun elmo-file-cache-get (msgid &optional section)
1786 "Returns the current file-cache object associated with MSGID.
1787 MSGID is the message-id of the message.
1788 If optional argument SECTION is specified, get partial file-cache object
1789 associated with SECTION."
1791 (let ((path (elmo-cache-get-path msgid)))
1792 (if (and path (file-exists-p path))
1793 (if (elmo-cache-path-section-p path)
1795 (if (file-exists-p (setq path (expand-file-name
1797 (cons path 'section))
1798 ;; section is not specified but sectional.
1799 (cons path 'section))
1802 (cons path 'entire)))
1809 (defun elmo-cache-expire ()
1811 (let* ((completion-ignore-case t)
1812 (method (completing-read (format "Expire by (%s): "
1813 elmo-cache-expire-default-method)
1817 (when (string= method "")
1818 (setq method elmo-cache-expire-default-method))
1819 (funcall (intern (concat "elmo-cache-expire-by-" method)))))
1821 (defun elmo-read-float-value-from-minibuffer (prompt &optional initial)
1822 (let ((str (read-from-minibuffer prompt initial)))
1824 ((string-match "[0-9]*\\.[0-9]+" str)
1825 (string-to-number str))
1826 ((string-match "[0-9]+" str)
1827 (string-to-number (concat str ".0")))
1828 (t (error "%s is not number" str)))))
1830 (defun elmo-cache-expire-by-size (&optional kbytes)
1831 "Expire cache file by size.
1832 If KBYTES is kilo bytes (This value must be float)."
1834 (let ((size (or kbytes
1835 (and (interactive-p)
1836 (elmo-read-float-value-from-minibuffer
1837 "Enter cache disk size (Kbytes): "
1839 (if (integerp elmo-cache-expire-default-size)
1840 (float elmo-cache-expire-default-size)
1841 elmo-cache-expire-default-size))))
1842 (if (integerp elmo-cache-expire-default-size)
1843 (float elmo-cache-expire-default-size))))
1847 (message "Checking disk usage...")
1848 (setq total (/ (elmo-disk-usage
1849 elmo-cache-directory) Kbytes))
1850 (setq beginning total)
1851 (message "Checking disk usage...done")
1852 (let ((cfl (elmo-cache-get-sorted-cache-file-list))
1856 (while (and (<= size total)
1857 (setq oldest (elmo-cache-get-oldest-cache-file-entity cfl)))
1858 (setq cur-file (expand-file-name (car (cdr oldest)) (car oldest)))
1859 (setq cur-size (/ (elmo-disk-usage cur-file) Kbytes))
1860 (when (elmo-file-cache-delete cur-file)
1861 (setq count (+ count 1))
1862 (message "%d cache(s) are expired." count))
1863 (setq deleted (+ deleted cur-size))
1864 (setq total (- total cur-size)))
1865 (message "%d cache(s) are expired from disk (%d Kbytes/%d Kbytes)."
1866 count deleted beginning))))
1868 (defun elmo-cache-make-file-entity (filename path)
1869 (cons filename (elmo-get-last-accessed-time filename path)))
1871 (defun elmo-cache-get-oldest-cache-file-entity (cache-file-list)
1872 (let ((cfl cache-file-list)
1873 flist firsts oldest-entity wonlist)
1875 (setq flist (cdr (car cfl)))
1876 (setq firsts (append firsts (list
1877 (cons (car (car cfl))
1879 (setq cfl (cdr cfl)))
1882 (if (and (not oldest-entity)
1883 (cdr (cdr (car firsts))))
1884 (setq oldest-entity (car firsts)))
1885 (if (and (cdr (cdr (car firsts)))
1886 (cdr (cdr oldest-entity))
1887 (> (cdr (cdr oldest-entity)) (cdr (cdr (car firsts)))))
1888 (setq oldest-entity (car firsts)))
1889 (setq firsts (cdr firsts)))
1890 (setq wonlist (assoc (car oldest-entity) cache-file-list))
1892 (setcdr wonlist (delete (car (cdr wonlist)) (cdr wonlist))))
1895 (defun elmo-cache-get-sorted-cache-file-list ()
1896 (let ((dirs (directory-files
1897 elmo-cache-directory
1902 (setq num (length dirs))
1903 (message "Collecting cache info...")
1905 (setq elist (mapcar (lambda (x)
1906 (elmo-cache-make-file-entity x (car dirs)))
1907 (directory-files (car dirs) nil "^[^\\.]")))
1908 (setq ret-val (append ret-val
1916 (when (> num elmo-display-progress-threshold)
1918 (elmo-display-progress
1919 'elmo-cache-get-sorted-cache-file-list "Collecting cache info..."
1921 (setq dirs (cdr dirs)))
1922 (message "Collecting cache info...done")
1925 (defun elmo-cache-expire-by-age (&optional days)
1926 (let ((age (or (and days (int-to-string days))
1927 (and (interactive-p)
1928 (read-from-minibuffer
1929 (format "Enter days (%s): "
1930 elmo-cache-expire-default-age)))
1931 (int-to-string elmo-cache-expire-default-age)))
1932 (dirs (directory-files
1933 elmo-cache-directory
1937 (if (string= age "")
1938 (setq age elmo-cache-expire-default-age)
1939 (setq age (string-to-int age)))
1940 (setq curtime (current-time))
1941 (setq curtime (+ (* (nth 0 curtime)
1942 (float 65536)) (nth 1 curtime)))
1944 (let ((files (directory-files (car dirs) t "^[^\\.]"))
1945 (limit-age (* age 86400)))
1947 (when (> (- curtime (elmo-get-last-accessed-time (car files)))
1949 (when (elmo-file-cache-delete (car files))
1950 (setq count (+ 1 count))
1951 (message "%d cache file(s) are expired." count)))
1952 (setq files (cdr files))))
1953 (setq dirs (cdr dirs)))))
1957 (defun elmo-msgid-to-cache (msgid)
1960 (string-match "<\\(.+\\)>$" msgid))
1961 (elmo-replace-string-as-filename (elmo-match-string 1 msgid)))))
1963 (defun elmo-cache-get-path (msgid &optional folder number)
1964 "Get path for cache file associated with MSGID, FOLDER, and NUMBER."
1965 (if (setq msgid (elmo-msgid-to-cache msgid))
1969 (format "%s/%s/%s@%s"
1970 (elmo-cache-get-path-subr msgid)
1973 (elmo-safe-filename folder))
1975 (elmo-cache-get-path-subr msgid)
1977 elmo-cache-directory))))
1982 (static-if (fboundp 'display-warning)
1983 (defmacro elmo-warning (&rest args)
1984 "Display a warning with `elmo' group."
1985 `(display-warning 'elmo (format ,@args)))
1986 (defconst elmo-warning-buffer-name "*elmo warning*")
1987 (defun elmo-warning (&rest args)
1988 "Display a warning. ARGS are passed to `format'."
1989 (with-current-buffer (get-buffer-create elmo-warning-buffer-name)
1990 (goto-char (point-max))
1991 (funcall 'insert (apply 'format (append args '("\n"))))
1992 (ignore-errors (recenter 1))
1993 (display-buffer elmo-warning-buffer-name))))
1995 (defvar elmo-obsolete-variable-alist nil)
1997 (defcustom elmo-obsolete-variable-show-warnings t
1998 "Show warning window if obsolete variable is treated."
2002 (defun elmo-define-obsolete-variable (obsolete var)
2003 "Define obsolete variable.
2004 OBSOLETE is a symbol for obsolete variable.
2005 VAR is a symbol for new variable.
2006 Definition is stored in `elmo-obsolete-variable-alist'."
2007 (let ((pair (assq var elmo-obsolete-variable-alist)))
2009 (setcdr pair obsolete)
2010 (setq elmo-obsolete-variable-alist
2011 (cons (cons var obsolete)
2012 elmo-obsolete-variable-alist)))))
2014 (defun elmo-resque-obsolete-variable (obsolete var)
2015 "Resque obsolete variable OBSOLETE as VAR.
2016 If `elmo-obsolete-variable-show-warnings' is non-nil, show warning message."
2017 (when (boundp obsolete)
2018 (static-if (and (fboundp 'defvaralias)
2019 (subrp (symbol-function 'defvaralias)))
2020 (defvaralias var obsolete)
2021 (set var (symbol-value obsolete)))
2022 (if elmo-obsolete-variable-show-warnings
2023 (elmo-warning "%s is obsolete. Use %s instead."
2024 (symbol-name obsolete)
2025 (symbol-name var)))))
2027 (defun elmo-resque-obsolete-variables (&optional alist)
2028 "Resque obsolete variables in ALIST.
2029 ALIST is a list of cons cell of
2030 \(OBSOLETE-VARIABLE-SYMBOL . NEW-VARIABLE-SYMBOL\).
2031 If ALIST is nil, `elmo-obsolete-variable-alist' is used."
2032 (dolist (pair elmo-obsolete-variable-alist)
2033 (elmo-resque-obsolete-variable (cdr pair)
2036 (defsubst elmo-msgdb-get-last-message-id (string)
2042 (goto-char (point-max))
2043 (when (search-backward "<" nil t)
2045 (if (search-forward ">" nil t)
2046 (elmo-replace-in-string
2047 (buffer-substring beg (point)) "\n[ \t]*" ""))))))))
2049 (defun elmo-msgdb-get-message-id-from-buffer ()
2050 (let ((msgid (elmo-field-body "message-id")))
2052 (if (string-match "<\\(.+\\)>$" msgid)
2054 (concat "<" msgid ">")) ; Invaild message-id.
2055 ;; no message-id, so put dummy msgid.
2057 (if (elmo-unfold-field-body "date")
2058 (timezone-make-date-sortable (elmo-unfold-field-body "date"))
2059 (md5 (string-as-unibyte (buffer-string))))
2060 (nth 1 (eword-extract-address-components
2061 (or (elmo-field-body "from") "nobody"))) ">"))))
2063 (defsubst elmo-msgdb-insert-file-header (file)
2064 "Insert the header of the article."
2066 insert-file-contents-pre-hook ; To avoid autoconv-xmas...
2067 insert-file-contents-post-hook
2069 (when (file-exists-p file)
2070 ;; Read until header separator is found.
2071 (while (and (eq elmo-msgdb-file-header-chop-length
2073 (insert-file-contents-as-binary
2075 (incf beg elmo-msgdb-file-header-chop-length))))
2076 (prog1 (not (search-forward "\n\n" nil t))
2077 (goto-char (point-max))))))))
2080 ;; overview handling
2082 (defun elmo-multiple-field-body (name &optional boundary)
2085 (std11-narrow-to-header boundary)
2086 (goto-char (point-min))
2087 (let ((case-fold-search t)
2089 (while (re-search-forward (concat "^" name ":[ \t]*") nil t)
2092 (list (buffer-substring-no-properties
2093 (match-end 0) (std11-field-end))))))
2097 (defvar elmo-dop-queue-filename "queue"
2098 "*Disconnected operation queue is saved in this file.")
2100 (defun elmo-dop-queue-load ()
2101 (setq elmo-dop-queue
2103 (expand-file-name elmo-dop-queue-filename
2104 elmo-msgdb-directory))))
2106 (defun elmo-dop-queue-save ()
2108 (expand-file-name elmo-dop-queue-filename
2109 elmo-msgdb-directory)
2112 (if (and (fboundp 'regexp-opt)
2113 (not (featurep 'xemacs)))
2114 (defalias 'elmo-regexp-opt 'regexp-opt)
2115 (defun elmo-regexp-opt (strings &optional paren)
2116 "Return a regexp to match a string in STRINGS.
2117 Each string should be unique in STRINGS and should not contain any regexps,
2118 quoted or not. If optional PAREN is non-nil, ensure that the returned regexp
2119 is enclosed by at least one regexp grouping construct."
2120 (let ((open-paren (if paren "\\(" "")) (close-paren (if paren "\\)" "")))
2121 (concat open-paren (mapconcat 'regexp-quote strings "\\|")
2125 (product-provide (provide 'elmo-util) (require 'elmo-version))
2127 ;;; elmo-util.el ends here