1 ;;; gnus-registry.el --- article registry for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs 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 ;; GNU Emacs 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.
27 ;; This is the gnus-registry.el package, works with other backends
28 ;; besides nnmail. The major issue is that it doesn't go across
29 ;; backends, so for instance if an article is in nnml:sys and you see
30 ;; a reference to it in nnimap splitting, the article will end up in
33 ;; gnus-registry.el intercepts article respooling, moving, deleting,
34 ;; and copying for all backends. If it doesn't work correctly for
35 ;; you, submit a bug report and I'll be glad to fix it. It needs
36 ;; documentation in the manual (also on my to-do list).
38 ;; Put this in your startup file (~/.gnus.el for instance)
40 ;; (setq gnus-registry-max-entries 2500
41 ;; gnus-registry-use-long-group-names t)
43 ;; (gnus-registry-initialize)
45 ;; Then use this in your fancy-split:
47 ;; (: gnus-registry-split-fancy-with-parent)
51 ;; - get the correct group on spool actions
53 ;; - articles that are spooled to a different backend should be handled
57 (eval-when-compile (require 'cl))
64 (defvar gnus-registry-dirty t
65 "Boolean set to t when the registry is modified")
67 (defgroup gnus-registry nil
71 (defvar gnus-registry-hashtb nil
72 "*The article registry by Message ID.")
74 (defcustom gnus-registry-unfollowed-groups '("delayed" "drafts" "queue")
75 "List of groups that gnus-registry-split-fancy-with-parent won't follow.
76 The group names are matched, they don't have to be fully qualified."
78 :type '(repeat string))
80 (defcustom gnus-registry-install nil
81 "Whether the registry should be installed."
85 (defcustom gnus-registry-clean-empty t
86 "Whether the empty registry entries should be deleted.
87 Registry entries are considered empty when they have no groups."
91 (defcustom gnus-registry-use-long-group-names nil
92 "Whether the registry should use long group names (BUGGY)."
96 (defcustom gnus-registry-track-extra nil
97 "Whether the registry should track other things about a message.
98 The Subject header is currently the only thing that can be
100 :group 'gnus-registry
103 (defcustom gnus-registry-entry-caching t
104 "Whether the registry should cache extra information."
105 :group 'gnus-registry
108 (defcustom gnus-registry-minimum-subject-length 5
109 "The minimum length of a subject before it's considered trackable."
110 :group 'gnus-registry
113 (defcustom gnus-registry-trim-articles-without-groups t
114 "Whether the registry should clean out message IDs without groups."
115 :group 'gnus-registry
118 (defcustom gnus-registry-cache-file "~/.gnus.registry.eld"
119 "File where the Gnus registry will be stored."
120 :group 'gnus-registry
123 (defcustom gnus-registry-max-entries nil
124 "Maximum number of entries in the registry, nil for unlimited."
125 :group 'gnus-registry
126 :type '(radio (const :format "Unlimited " nil)
127 (integer :format "Maximum number: %v\n" :size 0)))
129 ;; Function(s) missing in Emacs 20
130 (when (memq nil (mapcar 'fboundp '(puthash)))
132 (unless (fboundp 'puthash)
133 ;; alias puthash is missing from Emacs 20 cl-extra.el
134 (defalias 'puthash 'cl-puthash)))
136 (defun gnus-registry-cache-read ()
137 "Read the registry cache file."
139 (let ((file gnus-registry-cache-file))
140 (when (file-exists-p file)
141 (gnus-message 5 "Reading %s..." file)
143 (gnus-message 5 "Reading %s...done" file))))
145 (defun gnus-registry-cache-save ()
146 "Save the registry cache file."
148 (let ((file gnus-registry-cache-file))
150 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
151 (make-local-variable 'version-control)
152 (setq version-control gnus-backup-startup-file)
153 (setq buffer-file-name file)
154 (setq default-directory (file-name-directory buffer-file-name))
155 (buffer-disable-undo)
157 (gnus-message 5 "Saving %s..." file)
158 (if gnus-save-startup-file-via-temp-buffer
159 (let ((coding-system-for-write gnus-ding-file-coding-system)
160 (standard-output (current-buffer)))
161 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
162 (gnus-registry-cache-whitespace file)
164 (let ((coding-system-for-write gnus-ding-file-coding-system)
165 (version-control gnus-backup-startup-file)
167 (working-dir (file-name-directory file))
170 ;; Generate the name of a non-existent file.
171 (while (progn (setq working-file
173 (if (and (eq system-type 'ms-dos)
174 (not (gnus-long-file-names)))
175 "%s#%d.tm#" ; MSDOS limits files to 8+3
176 (if (memq system-type '(vax-vms axp-vms))
179 working-dir (setq i (1+ i))))
180 (file-exists-p working-file)))
184 (gnus-with-output-to-file working-file
185 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
187 ;; These bindings will mislead the current buffer
188 ;; into thinking that it is visiting the startup
190 (let ((buffer-backed-up nil)
191 (buffer-file-name startup-file)
192 (file-precious-flag t)
193 (setmodes (file-modes startup-file)))
194 ;; Backup the current version of the startup file.
197 ;; Replace the existing startup file with the temp file.
198 (rename-file working-file startup-file t)
199 (set-file-modes startup-file setmodes)))
201 (delete-file working-file)
204 (gnus-kill-buffer (current-buffer))
205 (gnus-message 5 "Saving %s...done" file))))
207 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
208 ;; Save the gnus-registry file with extra line breaks.
209 (defun gnus-registry-cache-whitespace (filename)
210 (gnus-message 5 "Adding whitespace to %s" filename)
212 (goto-char (point-min))
213 (while (re-search-forward "^(\\|(\\\"" nil t)
214 (replace-match "\n\\&" t))
215 (goto-char (point-min))
216 (while (re-search-forward " $" nil t)
217 (replace-match "" t t))))
219 (defun gnus-registry-save (&optional force)
220 (when (or gnus-registry-dirty force)
221 (let ((caching gnus-registry-entry-caching))
222 ;; turn off entry caching, so mtime doesn't get recorded
223 (setq gnus-registry-entry-caching nil)
224 ;; remove entry caches
227 (if (hash-table-p value)
228 (remhash key gnus-registry-hashtb)))
229 gnus-registry-hashtb)
230 ;; remove empty entries
231 (when gnus-registry-clean-empty
232 (gnus-registry-clean-empty-function))
233 ;; now trim the registry appropriately
234 (setq gnus-registry-alist (gnus-registry-trim
235 (hashtable-to-alist gnus-registry-hashtb)))
237 (gnus-registry-cache-save)
238 (setq gnus-registry-entry-caching caching)
239 (setq gnus-registry-dirty nil))))
241 (defun gnus-registry-clean-empty-function ()
242 "Remove all empty entries from the registry. Returns count thereof."
246 (unless (gnus-registry-fetch-group key)
248 (remhash key gnus-registry-hashtb)))
249 gnus-registry-hashtb)
252 (defun gnus-registry-read ()
253 (gnus-registry-cache-read)
254 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
255 (setq gnus-registry-dirty nil))
257 (defun gnus-registry-trim (alist)
258 "Trim alist to size, using gnus-registry-max-entries."
259 (if (null gnus-registry-max-entries)
260 alist ; just return the alist
261 ;; else, when given max-entries, trim the alist
262 (let ((timehash (make-hash-table
267 (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
268 gnus-registry-hashtb)
270 ;; we use the return value of this setq, which is the trimmed alist
273 (- (length alist) gnus-registry-max-entries)
277 (cdr (gethash (car a) timehash))
278 (cdr (gethash (car b) timehash))))))))))
280 (defun alist-to-hashtable (alist)
281 "Build a hashtable from the values in ALIST."
282 (let ((ht (make-hash-table
287 (puthash (car kv-pair) (cdr kv-pair) ht))
291 (defun hashtable-to-alist (hash)
292 "Build an alist from the values in HASH."
296 (setq list (cons (cons key value) list)))
300 (defun gnus-registry-action (action data-header from &optional to method)
301 (let* ((id (mail-header-id data-header))
302 (subject (gnus-registry-simplify-subject
303 (mail-header-subject data-header)))
304 (from (gnus-group-guess-full-name-from-command-method from))
305 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
306 (to-name (if to to "the Bit Bucket"))
307 (old-entry (gethash id gnus-registry-hashtb)))
308 (gnus-message 5 "Registry: article %s %s from %s to %s"
310 (if method "respooling" "going")
314 ;; All except copy will need a delete
315 (gnus-registry-delete-group id from)
317 (when (equal 'copy action)
318 (gnus-registry-add-group id from subject)) ; undo the delete
320 (gnus-registry-add-group id to subject)))
322 (defun gnus-registry-spool-action (id group &optional subject)
323 (let ((group (gnus-group-guess-full-name-from-command-method group)))
324 (when (and (stringp id) (string-match "\r$" id))
325 (setq id (substring id 0 -1)))
326 (gnus-message 5 "Registry: article %s spooled to %s"
329 (gnus-registry-add-group id group subject)))
331 ;; Function for nn{mail|imap}-split-fancy: look up all references in
332 ;; the cache and if a match is found, return that group.
333 (defun gnus-registry-split-fancy-with-parent ()
334 "Split this message into the same group as its parent. The parent
335 is obtained from the registry. This function can be used as an entry
336 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
337 this: (: gnus-registry-split-fancy-with-parent)
339 For a message to be split, it looks for the parent message in the
340 References or In-Reply-To header and then looks in the registry to
341 see which group that message was put in. This group is returned.
343 See the Info node `(gnus)Fancy Mail Splitting' for more details."
344 (let ((refstr (or (message-fetch-field "references")
345 (message-fetch-field "in-reply-to")))
346 (nnmail-split-fancy-with-parent-ignore-groups
347 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
348 nnmail-split-fancy-with-parent-ignore-groups
349 (list nnmail-split-fancy-with-parent-ignore-groups)))
353 (setq references (nreverse (gnus-split-references refstr)))
355 (setq res (or (gnus-registry-fetch-group x) res))
356 (when (or (gnus-registry-grep-in-list
358 gnus-registry-unfollowed-groups)
359 (gnus-registry-grep-in-list
361 nnmail-split-fancy-with-parent-ignore-groups))
364 ;; there were no references, now try the extra tracking
365 (when gnus-registry-track-extra
366 (let ((subject (gnus-registry-simplify-subject
367 (message-fetch-field "subject"))))
369 (< gnus-registry-minimum-subject-length (length subject)))
372 (let ((this-subject (cdr
373 (gnus-registry-fetch-extra key 'subject))))
374 (when (and this-subject
375 (equal subject this-subject))
376 (setq res (gnus-registry-fetch-group key))
378 ;; raise level of messaging if gnus-registry-track-extra
379 (if gnus-registry-track-extra 5 9)
380 "%s (extra tracking) traced subject %s to group %s"
381 "gnus-registry-split-fancy-with-parent"
383 (if res res "nil")))))
384 gnus-registry-hashtb)))))
387 "gnus-registry-split-fancy-with-parent traced %s to group %s"
388 refstr (if res res "nil"))
390 (when (and res gnus-registry-use-long-group-names)
391 (let ((m1 (gnus-find-method-for-group res))
392 (m2 (or gnus-command-method
393 (gnus-find-method-for-group gnus-newsgroup-name)))
394 (short-res (gnus-group-short-name res)))
395 (if (gnus-methods-equal-p m1 m2)
399 "gnus-registry-split-fancy-with-parent stripped group %s to %s"
402 (setq res short-res))
406 "gnus-registry-split-fancy-with-parent ignored foreign group %s"
411 (defun gnus-registry-register-message-ids ()
412 "Register the Message-ID of every article in the group"
413 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
414 (dolist (article gnus-newsgroup-articles)
415 (let ((id (gnus-registry-fetch-message-id-fast article)))
416 (unless (gnus-registry-fetch-group id)
417 (gnus-message 9 "Registry: Registering article %d with group %s"
418 article gnus-newsgroup-name)
419 (gnus-registry-add-group
420 (gnus-registry-fetch-message-id-fast article)
422 (gnus-registry-fetch-simplified-message-subject-fast article)))))))
424 (defun gnus-registry-fetch-message-id-fast (article)
425 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
426 (if (and (numberp article)
427 (assoc article (gnus-data-list nil)))
428 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
431 (defun gnus-registry-simplify-subject (subject)
432 (if (stringp subject)
433 (gnus-simplify-subject subject)
436 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
437 "Fetch the Subject quickly, using the internal gnus-data-list function"
438 (if (and (numberp article)
439 (assoc article (gnus-data-list nil)))
440 (gnus-registry-simplify-subject
441 (mail-header-subject (gnus-data-header
442 (assoc article (gnus-data-list nil)))))
445 (defun gnus-registry-grep-in-list (word list)
451 (string-match x word))
454 (defun gnus-registry-fetch-extra (id &optional entry)
455 "Get the extra data of a message, based on the message ID.
456 Returns the first place where the trail finds a nonstring."
457 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
459 (hash-table-p entry-cache)
460 (gethash id entry-cache))
461 (gethash id entry-cache)
462 ;; else, if there is no caching possible...
463 (let ((trail (gethash id gnus-registry-hashtb)))
465 (dolist (crumb trail)
466 (unless (stringp crumb)
467 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
469 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
470 "Get the extra data of a message, or a specific entry in it.
471 Update the entry cache if needed."
473 (let ((entry-cache (gethash entry gnus-registry-hashtb))
475 (when gnus-registry-entry-caching
476 ;; create the hash table
477 (unless (hash-table-p entry-cache)
478 (setq entry-cache (make-hash-table
481 (puthash entry entry-cache gnus-registry-hashtb))
483 ;; get the entree from the hash table or from the alist
484 (setq entree (gethash id entry-cache)))
487 (setq entree (assq entry alist))
488 (when gnus-registry-entry-caching
489 (puthash id entree entry-cache)))
493 (defun gnus-registry-store-extra (id extra)
494 "Store the extra data of a message, based on the message ID.
495 The message must have at least one group name."
496 (when (gnus-registry-group-count id)
497 ;; we now know the trail has at least 1 group name, so it's not empty
498 (let ((trail (gethash id gnus-registry-hashtb))
499 (old-extra (gnus-registry-fetch-extra id))
501 (dolist (crumb trail)
502 (unless (stringp crumb)
503 (dolist (entry crumb)
504 (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
506 (remhash id entry-cache))))
507 (puthash id (cons extra (delete old-extra trail))
508 gnus-registry-hashtb)
509 (setq gnus-registry-dirty t)))))
511 (defun gnus-registry-store-extra-entry (id key value)
512 "Put a specific entry in the extras field of the registry entry for id."
513 (let* ((extra (gnus-registry-fetch-extra id))
514 (alist (cons (cons key value)
515 (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
516 (gnus-registry-store-extra id alist)))
518 (defun gnus-registry-fetch-group (id)
519 "Get the group of a message, based on the message ID.
520 Returns the first place where the trail finds a group name."
521 (when (gnus-registry-group-count id)
522 ;; we now know the trail has at least 1 group name
523 (let ((trail (gethash id gnus-registry-hashtb)))
524 (dolist (crumb trail)
525 (when (stringp crumb)
526 (return (if gnus-registry-use-long-group-names
528 (gnus-group-short-name crumb))))))))
530 (defun gnus-registry-group-count (id)
531 "Get the number of groups of a message, based on the message ID."
532 (let ((trail (gethash id gnus-registry-hashtb)))
533 (if (and trail (listp trail))
534 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
537 (defun gnus-registry-delete-group (id group)
538 "Delete a group for a message, based on the message ID."
541 (let ((trail (gethash id gnus-registry-hashtb))
542 (group (gnus-group-short-name group)))
543 (puthash id (if trail
546 gnus-registry-hashtb))
547 ;; now, clear the entry if there are no more groups
548 (when gnus-registry-trim-articles-without-groups
549 (unless (gnus-registry-group-count id)
550 (gnus-registry-delete-id id)))
551 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
553 (defun gnus-registry-delete-id (id)
554 "Delete a message ID from the registry."
556 (remhash id gnus-registry-hashtb)
559 (when (hash-table-p value)
561 gnus-registry-hashtb)))
563 (defun gnus-registry-add-group (id group &optional subject)
564 "Add a group for a message, based on the message ID."
567 (not (string-match "totally-fudged-out-message-id" id)))
568 (let ((full-group group)
569 (group (if gnus-registry-use-long-group-names
571 (gnus-group-short-name group))))
572 (gnus-registry-delete-group id group)
574 (unless gnus-registry-use-long-group-names ;; unnecessary in this case
575 (gnus-registry-delete-group id full-group))
577 (let ((trail (gethash id gnus-registry-hashtb)))
578 (puthash id (if trail
581 gnus-registry-hashtb)
583 (when gnus-registry-track-extra
584 (gnus-registry-store-extra-entry
587 (gnus-registry-simplify-subject subject)))
589 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
591 (defun gnus-registry-clear ()
592 "Clear the Gnus registry."
594 (setq gnus-registry-alist nil)
595 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
596 (setq gnus-registry-dirty t))
599 (defun gnus-registry-initialize ()
601 (setq gnus-registry-install t)
602 (gnus-registry-install-hooks)
603 (gnus-registry-read))
606 (defun gnus-registry-install-hooks ()
607 "Install the registry hooks."
609 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
610 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
611 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
612 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
614 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
615 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
617 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
619 (defun gnus-registry-unload-hook ()
620 "Uninstall the registry hooks."
622 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
623 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
624 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
625 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
627 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
628 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
630 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
632 (when gnus-registry-install
633 (gnus-registry-install-hooks)
634 (gnus-registry-read))
636 ;; TODO: a lot of things
638 (provide 'gnus-registry)
640 ;;; gnus-registry.el ends here