1 ;;; elmo-flag.el --- global flag handling.
3 ;; Copyright (C) 2003 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.
29 (require 'elmo-localdir)
30 (eval-when-compile (require 'cl))
33 (defcustom elmo-global-flags '(important)
34 "A list of flag symbol which is managed globally by the flag folder."
35 :type '(repeat symbol)
38 (defcustom elmo-local-flags '(unread any digest)
39 "A list of flag symbol which is not treated as global flag."
40 :type '(repeat symbol)
43 (defvar elmo-global-flag-folder-alist nil
44 "Internal variable to hold global-flag-folder structures.")
47 (defconst elmo-flag-char-regexp "]!#$&'+,./0-9:;<=>?@A-Z[^_`a-z|}~-"))
49 (defun elmo-flag-valid-p (flag)
50 (string-match (eval-when-compile
51 (concat "^[" elmo-flag-char-regexp "]+$"))
52 (if (stringp flag) flag (symbol-name flag))))
55 (luna-define-class elmo-flag-folder (elmo-localdir-folder)
56 (flag minfo minfo-hash max-number))
57 (luna-define-internal-accessors 'elmo-flag-folder))
59 (luna-define-method elmo-folder-initialize ((folder
62 (unless (string-match (eval-when-compile
63 (concat "^flag\\(/\\(["
67 (error "Error in folder name `%s'" (elmo-folder-name-internal folder)))
68 (if (match-beginning 1)
69 (setq name (match-string 2 name))
70 (setq name (symbol-name (car elmo-global-flags)))
71 (elmo-folder-set-name-internal
73 (concat (elmo-folder-name-internal folder) "/" name)))
74 (or (cdr (assq (intern name) elmo-global-flag-folder-alist))
75 (let ((flag (intern name))
77 (elmo-flag-folder-set-flag-internal folder flag)
78 (unless (elmo-global-flag-p flag)
79 (setq elmo-global-flags
80 (nconc elmo-global-flags (list flag))))
81 ;; must be AFTER set flag slot.
82 (setq msgdb-path (elmo-folder-msgdb-path folder))
83 (unless (file-directory-p msgdb-path)
84 (elmo-make-directory msgdb-path))
85 (elmo-localdir-folder-set-dir-name-internal
88 (elmo-localdir-folder-set-directory-internal
91 (if (file-exists-p (expand-file-name "max" msgdb-path))
92 (elmo-flag-folder-set-max-number-internal
94 (elmo-object-load (expand-file-name "max" msgdb-path))))
95 (elmo-flag-folder-set-minfo
97 (and (file-exists-p (expand-file-name ".minfo" msgdb-path))
98 (elmo-object-load (expand-file-name ".minfo" msgdb-path))))
99 (setq elmo-global-flag-folder-alist
100 (cons (cons flag folder) elmo-global-flag-folder-alist))
103 (defun elmo-flag-folder-set-minfo (folder minfo)
104 (let ((hash (elmo-make-hash (length minfo))))
106 (elmo-set-hash-val (nth 1 elem) elem hash)
107 (elmo-set-hash-val (concat "#" (number-to-string (nth 2 elem)))
109 (dolist (pair (car elem))
110 (elmo-set-hash-val (concat (number-to-string (cdr pair))
113 (elmo-flag-folder-set-minfo-internal folder minfo)
114 (elmo-flag-folder-set-minfo-hash-internal folder hash)))
116 (luna-define-method elmo-folder-expand-msgdb-path ((folder elmo-flag-folder))
117 (expand-file-name (concat "flag/"
118 (elmo-replace-string-as-filename
120 (elmo-flag-folder-flag-internal folder))))
121 elmo-msgdb-directory))
123 (luna-define-method elmo-folder-commit :after ((folder
126 (expand-file-name ".minfo" (elmo-folder-msgdb-path folder))
127 (elmo-flag-folder-minfo-internal folder))
128 (if (elmo-flag-folder-max-number-internal folder)
130 (expand-file-name "max" (elmo-folder-msgdb-path folder))
131 (elmo-flag-folder-max-number-internal folder))))
133 (luna-define-method elmo-folder-list-subfolders ((folder elmo-flag-folder)
135 (mapcar (lambda (flag)
137 (elmo-folder-prefix-internal folder)
138 (symbol-name (elmo-folder-type-internal folder))
143 (defun elmo-flag-folder-delete-message (folder number
144 &optional keep-referrer)
145 (let* ((elem (elmo-get-hash-val (concat "#" (number-to-string number))
146 (elmo-flag-folder-minfo-hash-internal
149 (dolist (pair (car elem))
150 (when (and (car pair) (cdr pair))
151 (elmo-clear-hash-val (concat (number-to-string (cdr pair)) ":"
153 (elmo-flag-folder-minfo-hash-internal
155 (unless keep-referrer
156 (setq target-folder (elmo-get-folder (car pair)))
157 (elmo-folder-open target-folder 'load-msgdb)
158 ;; Unset the flag of the original folder.
159 ;; (XXX Should the message-id checked?)
160 (elmo-message-unset-flag target-folder (cdr pair)
161 (elmo-flag-folder-flag-internal folder))
162 (elmo-folder-close target-folder))))
163 (elmo-clear-hash-val (concat "#" (number-to-string number))
164 (elmo-flag-folder-minfo-hash-internal
166 (elmo-clear-hash-val (nth 1 elem) (elmo-flag-folder-minfo-hash-internal
168 (elmo-flag-folder-set-minfo-internal
170 (delq elem (elmo-flag-folder-minfo-internal folder))))
173 (luna-define-method elmo-folder-delete-messages-internal ((folder
176 (dolist (number numbers)
177 (elmo-flag-folder-delete-message folder number)
178 (elmo-localdir-delete-message folder number))
179 (elmo-folder-commit folder)
182 ;; Same as localdir except that the flag is always the flag.
183 (luna-define-method elmo-folder-msgdb-create ((folder elmo-flag-folder)
187 (let ((dir (elmo-localdir-folder-directory-internal folder))
188 (new-msgdb (elmo-make-msgdb))
190 (len (length numbers)))
191 (message "Creating msgdb...")
193 (when (setq entity (elmo-localdir-msgdb-create-entity
194 new-msgdb dir (car numbers)))
195 (elmo-msgdb-append-entity new-msgdb entity
196 (list (elmo-flag-folder-flag-internal
198 (when (> len elmo-display-progress-threshold)
200 (elmo-display-progress
201 'elmo-flag-folder-msgdb-create "Creating msgdb..."
203 (setq numbers (cdr numbers)))
204 (message "Creating msgdb...done")
207 (luna-define-method elmo-folder-append-messages ((folder elmo-flag-folder)
210 &optional same-number)
211 (dolist (number numbers)
212 (elmo-global-flag-set (elmo-flag-folder-flag-internal folder)
213 src-folder number (elmo-message-field
217 (elmo-folder-set-flag src-folder
219 (elmo-flag-folder-flag-internal folder))
222 (luna-define-method elmo-folder-append-buffer ((folder elmo-flag-folder)
223 &optional flag number)
224 (error "Cannot append to the flag folder"))
226 (luna-define-method elmo-folder-unset-flag :before ((folder elmo-flag-folder)
230 (when (eq flag (elmo-flag-folder-flag-internal folder))
231 (error "Cannot unset flag `%s' in this folder" flag)))
235 (defmacro elmo-flag-get-folder (flag)
236 "Get the flag folder structure for FLAG."
237 `(when (memq ,flag elmo-global-flags)
238 (elmo-get-folder (concat "'flag/" (symbol-name ,flag)))))
240 (defun elmo-flag-folder-referrer (folder number)
241 "Return a list of referrer message information.
242 Each element is a cons cell like following:
244 FNAME is the name of the folder which the message is contained.
245 NUMBER is the number of the message."
246 (when (eq (elmo-folder-type-internal folder) 'flag)
247 (car (elmo-get-hash-val (concat "#" (number-to-string number))
248 (elmo-flag-folder-minfo-hash-internal
252 (defun elmo-global-flag-p (flag)
253 "Return non-nil when FLAG is global."
254 (memq flag elmo-global-flags))
256 (defun elmo-global-flags (fname number)
257 "Return a list of global flags for the message.
258 FNAME is the name string of the folder.
259 NUMBER is the number of the message."
260 (let ((flag-list elmo-global-flags)
263 (setq folder (elmo-flag-get-folder (car flag-list)))
264 (when (elmo-get-hash-val
265 (concat (number-to-string number) ":" fname)
266 (elmo-flag-folder-minfo-hash-internal folder))
267 (setq matches (cons (elmo-flag-folder-flag-internal folder)
269 (setq flag-list (cdr flag-list)))
272 (defun elmo-folder-list-global-flag-messages (folder flag)
273 "List messages which have global flag.
274 FOLDER is the elmo folder structure.
275 FLAG is the symbol of the flag."
276 (when (elmo-global-flag-p flag)
277 (let ((flag-folder (elmo-flag-get-folder flag))
279 (dolist (elem (elmo-flag-folder-minfo-internal flag-folder))
280 (if (setq number (elmo-message-number folder (nth 1 elem)))
281 (setq result (cons number result))))
285 ;; minfo is a list of following cell.
286 ;; ((((FNAME . NUMBER)...(FNAME . NUMBER)) MESSAGE-ID NUMBER-IN-FLAG-FOLDER)
287 ;; minfo-index is the hash table of above with following indice;
288 (defun elmo-global-flags-set (flags folder number message-id)
289 "Set global flags to the message.
290 FLAGS is a list of symbol of the flag.
291 FOLDER is the elmo folder structure.
292 NUMBER is the message number."
294 (elmo-global-flag-set flag folder number message-id)))
296 (defun elmo-local-flag-p (flag)
297 "Return non-nil when flag is not appropriate for global flag."
298 (memq flag elmo-local-flags))
300 (defsubst elmo-global-flag-set-internal (flag folder number message-id)
301 (when (elmo-local-flag-p flag)
302 (error "Cannot treat `%s' as global flag" flag))
304 (let ((flag-folder (elmo-flag-get-folder flag))
305 cache new-file new-number elem)
306 (if (setq elem (elmo-get-hash-val
308 (elmo-flag-folder-minfo-hash-internal
310 ;; Same ID already exists.
311 (when (and folder number
312 (not (member (cons (elmo-folder-name-internal folder)
313 number) (car elem))))
315 (cons (cons (elmo-folder-name-internal folder)
317 (setq new-number (nth 2 elem))
318 (elmo-set-hash-val (concat (number-to-string number)
319 ":" (elmo-folder-name-internal
322 (elmo-flag-folder-minfo-hash-internal
324 ;; Append new element.
325 (elmo-flag-folder-set-max-number-internal
327 (+ (or (elmo-flag-folder-max-number-internal flag-folder)
328 ;; This is the first time.
329 (car (elmo-folder-status flag-folder)))
335 (elmo-flag-folder-max-number-internal flag-folder)))
336 (elmo-localdir-folder-directory-internal flag-folder)))
338 (setq cache (and message-id (elmo-file-cache-get message-id)))
339 (if (and cache (eq (elmo-file-cache-status cache) 'entire))
340 (elmo-copy-file (elmo-file-cache-path cache)
342 (when (and folder number)
343 (elmo-message-fetch folder number
344 (elmo-make-fetch-strategy 'entire))
345 (write-region-as-binary (point-min) (point-max) new-file nil
347 (elmo-flag-folder-set-minfo-internal
351 (when (and folder number)
352 (list (cons (elmo-folder-name-internal folder)
356 (elmo-flag-folder-minfo-internal flag-folder)))
357 (when (and folder number)
358 (elmo-set-hash-val (concat (number-to-string number)
359 ":" (elmo-folder-name-internal
362 (elmo-flag-folder-minfo-hash-internal
364 (elmo-set-hash-val message-id elem
365 (elmo-flag-folder-minfo-hash-internal
367 (elmo-set-hash-val (concat "#" (number-to-string new-number)) elem
368 (elmo-flag-folder-minfo-hash-internal
370 (elmo-folder-commit flag-folder)
373 (defun elmo-global-flag-set (flag folder number message-id)
374 "Set global flag to the message.
375 FLAG is a symbol of the flag.
376 FOLDER is the elmo folder structure.
377 NUMBER is the message number.
378 MESSAGE-ID is the message-id of the message."
379 (when (elmo-global-flag-p flag)
380 (elmo-global-flag-set-internal flag folder number message-id)))
382 (defun elmo-global-flag-detach (flag folder number &optional delete-if-none)
383 "Detach the message from the global flag.
384 FOLDER is the folder structure.
385 NUMBERS is the message number.
386 If optional DELETE-IF-NONE is non-nil, delete message from flag folder when
387 the message is not flagged in any folder.
388 If DELETE-IF-NONE is a symbol `always',
389 delete message without flagged in other folder."
390 (unless (and (eq (elmo-folder-type-internal folder) 'flag)
391 (eq (elmo-flag-folder-flag-internal folder) flag))
392 (let ((flag-folder (elmo-flag-get-folder flag))
395 (setq key (concat (number-to-string number) ":"
396 (elmo-folder-name-internal folder))
397 elem (elmo-get-hash-val
399 (elmo-flag-folder-minfo-hash-internal flag-folder)))
401 (setcar elem (delete (cons (elmo-folder-name-internal folder)
403 (elmo-clear-hash-val key (elmo-flag-folder-minfo-hash-internal
405 ;; Does not have any referrer, remove.
406 (when (and delete-if-none
407 (or (eq delete-if-none 'always)
409 (elmo-flag-folder-delete-message flag-folder (nth 2 elem)
411 (elmo-localdir-delete-message flag-folder (nth 2 elem))
412 (elmo-folder-commit flag-folder)))))))
414 (defun elmo-global-flag-detach-messages (folder numbers &optional
416 "Detach all messages specified from all global flags.
417 FOLDER is the folder structure.
418 NUMBERS is the message number list.
419 If optional DELETE-IF-NONE is non-nil, delete message from flag folder when
420 the message is not flagged in any folder."
421 (unless (eq (elmo-folder-type-internal folder) 'flag)
422 (dolist (flag elmo-global-flags)
423 (dolist (number numbers)
424 (elmo-global-flag-detach flag folder number delete-if-none)))))
426 (defun elmo-global-flag-replace-referrer (old-folder new-folder)
427 (dolist (flag elmo-global-flags)
428 (let* ((folder (elmo-flag-get-folder flag))
429 (minfo (elmo-flag-folder-minfo-internal folder))
431 (dolist (entry minfo)
432 (let ((pair (assoc old-folder (nth 0 entry))))
434 (setcar pair new-folder)
437 (elmo-flag-folder-set-minfo folder minfo)
438 (elmo-folder-commit folder)))))
440 (defun elmo-get-global-flags (&optional flags ignore-preserved)
442 Return value is a subset of optional argument FLAGS.
443 If FLAGS is `t', all global flags becomes candidates.
444 If optional IGNORE-PRESERVED is non-nil, preserved flags
445 \(answered, cached, new, unread\) are not included."
446 (let ((result (copy-sequence (if (eq flags t)
447 (setq flags elmo-global-flags)
450 (unless (elmo-global-flag-p (car flags))
451 (setq result (delq (car flags) result)))
452 (setq flags (cdr flags)))
453 (when ignore-preserved
454 (dolist (flag elmo-preserved-flags)
455 (setq result (delq flag result))))
458 (defun elmo-global-flags-initialize (&optional additional-flags)
459 (let ((dir (expand-file-name "flag" elmo-msgdb-directory)))
460 (setq elmo-global-flags
467 (and (file-directory-p dir)
469 (intern (elmo-recover-string-from-filename x)))
472 (directory-files dir))))))
475 ;;; To migrate from global mark folder
476 (defvar elmo-global-mark-filename "global-mark"
477 "Obsolete variable. (Just for migration)")
479 (defun elmo-global-mark-migrate ()
480 "Migrate from 'mark to 'flag. For automatic migration."
481 (when (and (file-exists-p (expand-file-name elmo-global-mark-filename
482 elmo-msgdb-directory))
483 (elmo-global-flag-p 'important)
484 (not (file-exists-p (elmo-folder-msgdb-path
485 (elmo-flag-get-folder 'important)))))
486 (elmo-global-mark-upgrade)))
488 (defun elmo-global-mark-upgrade ()
489 "Upgrade old `global-mark' structure."
491 (when (file-exists-p (expand-file-name
492 elmo-global-mark-filename elmo-msgdb-directory))
493 (message "Upgrading flag structure...")
494 (when (elmo-global-flag-p 'important)
498 elmo-global-mark-filename elmo-msgdb-directory)))
499 (folder (elmo-flag-get-folder 'important))
501 (dolist (elem global-marks)
502 (setq file-cache (elmo-file-cache-get (car elem)))
503 (when (eq (elmo-file-cache-status file-cache) 'entire)
504 (elmo-global-flag-set 'important nil nil (car elem))))))
505 (message "Upgrading flag structure...done")))
507 (luna-define-method elmo-folder-delete :around ((folder elmo-flag-folder))
508 (let ((flag (elmo-flag-folder-flag-internal folder)))
509 (when (luna-call-next-method)
510 (setq elmo-global-flags (delq flag elmo-global-flags))
511 (setq elmo-global-flag-folder-alist
512 (delq (assq flag elmo-global-flag-folder-alist)
513 elmo-global-flag-folder-alist))
517 (product-provide (provide 'elmo-flag) (require 'elmo-version))
519 ;;; elmo-flag.el ends here