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, which works with all
28 ;; backends, not just nnmail (e.g. NNTP). The major issue is that it
29 ;; doesn't go across backends, so for instance if an article is in
30 ;; nnml:sys and you see a reference to it in nnimap splitting, the
31 ;; article will end up in nnimap:sys
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
92 (defcustom gnus-registry-use-long-group-names nil
93 "Whether the registry should use long group names (BUGGY)."
97 (defcustom gnus-registry-track-extra nil
98 "Whether the registry should track extra data about a message.
99 The Subject and Sender (From:) headers are currently tracked this
101 :group 'gnus-registry
103 '(set :tag "Tracking choices"
104 (const :tag "Track by subject (Subject: header)" subject)
105 (const :tag "Track by sender (From: header)" sender)))
107 (defcustom gnus-registry-entry-caching t
108 "Whether the registry should cache extra information."
109 :group 'gnus-registry
112 (defcustom gnus-registry-minimum-subject-length 5
113 "The minimum length of a subject before it's considered trackable."
114 :group 'gnus-registry
117 (defcustom gnus-registry-trim-articles-without-groups t
118 "Whether the registry should clean out message IDs without groups."
119 :group 'gnus-registry
122 (defcustom gnus-registry-cache-file
124 (or gnus-dribble-directory gnus-home-directory "~/")
125 ".gnus.registry.eld")
126 "File where the Gnus registry will be stored."
127 :group 'gnus-registry
130 (defcustom gnus-registry-max-entries nil
131 "Maximum number of entries in the registry, nil for unlimited."
132 :group 'gnus-registry
133 :type '(radio (const :format "Unlimited " nil)
134 (integer :format "Maximum number: %v\n" :size 0)))
136 (defun gnus-registry-track-subject-p ()
137 (memq 'subject gnus-registry-track-extra))
139 (defun gnus-registry-track-sender-p ()
140 (memq 'sender gnus-registry-track-extra))
142 (defun gnus-registry-cache-read ()
143 "Read the registry cache file."
145 (let ((file gnus-registry-cache-file))
146 (when (file-exists-p file)
147 (gnus-message 5 "Reading %s..." file)
149 (gnus-message 5 "Reading %s...done" file))))
151 (defun gnus-registry-cache-save ()
152 "Save the registry cache file."
154 (let ((file gnus-registry-cache-file))
156 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
157 (make-local-variable 'version-control)
158 (setq version-control gnus-backup-startup-file)
159 (setq buffer-file-name file)
160 (setq default-directory (file-name-directory buffer-file-name))
161 (buffer-disable-undo)
163 (gnus-message 5 "Saving %s..." file)
164 (if gnus-save-startup-file-via-temp-buffer
165 (let ((coding-system-for-write gnus-ding-file-coding-system)
166 (standard-output (current-buffer)))
167 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
168 (gnus-registry-cache-whitespace file)
170 (let ((coding-system-for-write gnus-ding-file-coding-system)
171 (version-control gnus-backup-startup-file)
173 (working-dir (file-name-directory file))
176 ;; Generate the name of a non-existent file.
177 (while (progn (setq working-file
179 (if (and (eq system-type 'ms-dos)
180 (not (gnus-long-file-names)))
181 "%s#%d.tm#" ; MSDOS limits files to 8+3
182 (if (memq system-type '(vax-vms axp-vms))
185 working-dir (setq i (1+ i))))
186 (file-exists-p working-file)))
190 (gnus-with-output-to-file working-file
191 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
193 ;; These bindings will mislead the current buffer
194 ;; into thinking that it is visiting the startup
196 (let ((buffer-backed-up nil)
197 (buffer-file-name startup-file)
198 (file-precious-flag t)
199 (setmodes (file-modes startup-file)))
200 ;; Backup the current version of the startup file.
203 ;; Replace the existing startup file with the temp file.
204 (rename-file working-file startup-file t)
205 (set-file-modes startup-file setmodes)))
207 (delete-file working-file)
210 (gnus-kill-buffer (current-buffer))
211 (gnus-message 5 "Saving %s...done" file))))
213 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
214 ;; Save the gnus-registry file with extra line breaks.
215 (defun gnus-registry-cache-whitespace (filename)
216 (gnus-message 5 "Adding whitespace to %s" filename)
218 (goto-char (point-min))
219 (while (re-search-forward "^(\\|(\\\"" nil t)
220 (replace-match "\n\\&" t))
221 (goto-char (point-min))
222 (while (re-search-forward " $" nil t)
223 (replace-match "" t t))))
225 (defun gnus-registry-save (&optional force)
226 (when (or gnus-registry-dirty force)
227 (let ((caching gnus-registry-entry-caching))
228 ;; turn off entry caching, so mtime doesn't get recorded
229 (setq gnus-registry-entry-caching nil)
230 ;; remove entry caches
233 (if (hash-table-p value)
234 (remhash key gnus-registry-hashtb)))
235 gnus-registry-hashtb)
236 ;; remove empty entries
237 (when gnus-registry-clean-empty
238 (gnus-registry-clean-empty-function))
239 ;; now trim the registry appropriately
240 (setq gnus-registry-alist (gnus-registry-trim
241 (hashtable-to-alist gnus-registry-hashtb)))
243 (gnus-registry-cache-save)
244 (setq gnus-registry-entry-caching caching)
245 (setq gnus-registry-dirty nil))))
247 (defun gnus-registry-clean-empty-function ()
248 "Remove all empty entries from the registry. Returns count thereof."
253 (gnus-registry-fetch-group key)
254 ;; TODO: look for specific extra data here!
255 ;; in this example, we look for 'label
256 (gnus-registry-fetch-extra key 'label))
258 (remhash key gnus-registry-hashtb)))
259 gnus-registry-hashtb)
262 (defun gnus-registry-read ()
263 (gnus-registry-cache-read)
264 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
265 (setq gnus-registry-dirty nil))
267 (defun gnus-registry-trim (alist)
268 "Trim alist to size, using gnus-registry-max-entries."
269 (if (null gnus-registry-max-entries)
270 alist ; just return the alist
271 ;; else, when given max-entries, trim the alist
272 (let ((timehash (make-hash-table
277 (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
278 gnus-registry-hashtb)
280 ;; we use the return value of this setq, which is the trimmed alist
283 (- (length alist) gnus-registry-max-entries)
287 (cdr (gethash (car a) timehash))
288 (cdr (gethash (car b) timehash))))))))))
290 (defun alist-to-hashtable (alist)
291 "Build a hashtable from the values in ALIST."
292 (let ((ht (make-hash-table
297 (puthash (car kv-pair) (cdr kv-pair) ht))
301 (defun hashtable-to-alist (hash)
302 "Build an alist from the values in HASH."
306 (setq list (cons (cons key value) list)))
310 (defun gnus-registry-action (action data-header from &optional to method)
311 (let* ((id (mail-header-id data-header))
312 (subject (gnus-registry-simplify-subject
313 (mail-header-subject data-header)))
314 (sender (mail-header-from data-header))
315 (from (gnus-group-guess-full-name-from-command-method from))
316 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
317 (to-name (if to to "the Bit Bucket"))
318 (old-entry (gethash id gnus-registry-hashtb)))
319 (gnus-message 5 "Registry: article %s %s from %s to %s"
321 (if method "respooling" "going")
325 ;; All except copy will need a delete
326 (gnus-registry-delete-group id from)
328 (when (equal 'copy action)
329 (gnus-registry-add-group id from subject sender)) ; undo the delete
331 (gnus-registry-add-group id to subject sender)))
333 (defun gnus-registry-spool-action (id group &optional subject sender)
334 (let ((group (gnus-group-guess-full-name-from-command-method group)))
335 (when (and (stringp id) (string-match "\r$" id))
336 (setq id (substring id 0 -1)))
337 (gnus-message 5 "Registry: article %s spooled to %s"
340 (gnus-registry-add-group id group subject sender)))
342 ;; Function for nn{mail|imap}-split-fancy: look up all references in
343 ;; the cache and if a match is found, return that group.
344 (defun gnus-registry-split-fancy-with-parent ()
345 "Split this message into the same group as its parent. The parent
346 is obtained from the registry. This function can be used as an entry
347 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
348 this: (: gnus-registry-split-fancy-with-parent)
350 This function tracks ALL backends, unlike
351 `nnmail-split-fancy-with-parent' which tracks only nnmail
354 For a message to be split, it looks for the parent message in the
355 References or In-Reply-To header and then looks in the registry to
356 see which group that message was put in. This group is returned.
358 See the Info node `(gnus)Fancy Mail Splitting' for more details."
359 (let ((refstr (or (message-fetch-field "references")
360 (message-fetch-field "in-reply-to")))
361 (nnmail-split-fancy-with-parent-ignore-groups
362 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
363 nnmail-split-fancy-with-parent-ignore-groups
364 (list nnmail-split-fancy-with-parent-ignore-groups)))
368 (setq references (nreverse (gnus-split-references refstr)))
370 (setq res (or (gnus-registry-fetch-group x) res))
371 (when (or (gnus-registry-grep-in-list
373 gnus-registry-unfollowed-groups)
374 (gnus-registry-grep-in-list
376 nnmail-split-fancy-with-parent-ignore-groups))
380 ;; else: there were no references, now try the extra tracking
381 (let ((sender (message-fetch-field "from"))
382 (subject (gnus-registry-simplify-subject
383 (message-fetch-field "subject")))
385 (when (and single-match
386 (gnus-registry-track-sender-p)
390 (let ((this-sender (cdr
391 (gnus-registry-fetch-extra key 'sender))))
392 (when (and single-match
394 (equal sender this-sender))
395 ;; too many matches, bail
396 (unless (equal res (gnus-registry-fetch-group key))
397 (setq single-match nil))
398 (setq res (gnus-registry-fetch-group key))
399 (when (and sender res)
401 ;; raise level of messaging if gnus-registry-track-extra
402 (if gnus-registry-track-extra 5 9)
403 "%s (extra tracking) traced sender %s to group %s"
404 "gnus-registry-split-fancy-with-parent"
407 gnus-registry-hashtb))
408 (when (and single-match
409 (gnus-registry-track-subject-p)
411 (< gnus-registry-minimum-subject-length (length subject)))
414 (let ((this-subject (cdr
415 (gnus-registry-fetch-extra key 'subject))))
416 (when (and single-match
418 (equal subject this-subject))
419 ;; too many matches, bail
420 (unless (equal res (gnus-registry-fetch-group key))
421 (setq single-match nil))
422 (setq res (gnus-registry-fetch-group key))
423 (when (and subject res)
425 ;; raise level of messaging if gnus-registry-track-extra
426 (if gnus-registry-track-extra 5 9)
427 "%s (extra tracking) traced subject %s to group %s"
428 "gnus-registry-split-fancy-with-parent"
431 gnus-registry-hashtb))
435 "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
438 (when (and refstr res)
441 "gnus-registry-split-fancy-with-parent traced %s to group %s"
444 (when (and res gnus-registry-use-long-group-names)
445 (let ((m1 (gnus-find-method-for-group res))
446 (m2 (or gnus-command-method
447 (gnus-find-method-for-group gnus-newsgroup-name)))
448 (short-res (gnus-group-short-name res)))
449 (if (gnus-methods-equal-p m1 m2)
453 "gnus-registry-split-fancy-with-parent stripped group %s to %s"
456 (setq res short-res))
460 "gnus-registry-split-fancy-with-parent ignored foreign group %s"
465 (defun gnus-registry-register-message-ids ()
466 "Register the Message-ID of every article in the group"
467 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
468 (dolist (article gnus-newsgroup-articles)
469 (let ((id (gnus-registry-fetch-message-id-fast article)))
470 (unless (gnus-registry-fetch-group id)
471 (gnus-message 9 "Registry: Registering article %d with group %s"
472 article gnus-newsgroup-name)
473 (gnus-registry-add-group
474 (gnus-registry-fetch-message-id-fast article)
476 (gnus-registry-fetch-simplified-message-subject-fast article)
477 (gnus-registry-fetch-sender-fast article)))))))
479 (defun gnus-registry-fetch-message-id-fast (article)
480 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
481 (if (and (numberp article)
482 (assoc article (gnus-data-list nil)))
483 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
486 (defun gnus-registry-simplify-subject (subject)
487 (if (stringp subject)
488 (gnus-simplify-subject subject)
491 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
492 "Fetch the Subject quickly, using the internal gnus-data-list function"
493 (if (and (numberp article)
494 (assoc article (gnus-data-list nil)))
495 (gnus-registry-simplify-subject
496 (mail-header-subject (gnus-data-header
497 (assoc article (gnus-data-list nil)))))
500 (defun gnus-registry-fetch-sender-fast (article)
501 "Fetch the Sender quickly, using the internal gnus-data-list function"
502 (if (and (numberp article)
503 (assoc article (gnus-data-list nil)))
504 (mail-header-from (gnus-data-header
505 (assoc article (gnus-data-list nil))))
508 (defun gnus-registry-grep-in-list (word list)
514 (string-match x word))
517 (defun gnus-registry-fetch-extra (id &optional entry)
518 "Get the extra data of a message, based on the message ID.
519 Returns the first place where the trail finds a nonstring."
520 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
522 (hash-table-p entry-cache)
523 (gethash id entry-cache))
524 (gethash id entry-cache)
525 ;; else, if there is no caching possible...
526 (let ((trail (gethash id gnus-registry-hashtb)))
528 (dolist (crumb trail)
529 (unless (stringp crumb)
530 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
532 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
533 "Get the extra data of a message, or a specific entry in it.
534 Update the entry cache if needed."
536 (let ((entry-cache (gethash entry gnus-registry-hashtb))
538 (when gnus-registry-entry-caching
539 ;; create the hash table
540 (unless (hash-table-p entry-cache)
541 (setq entry-cache (make-hash-table
544 (puthash entry entry-cache gnus-registry-hashtb))
546 ;; get the entree from the hash table or from the alist
547 (setq entree (gethash id entry-cache)))
550 (setq entree (assq entry alist))
551 (when gnus-registry-entry-caching
552 (puthash id entree entry-cache)))
556 (defun gnus-registry-store-extra (id extra)
557 "Store the extra data of a message, based on the message ID.
558 The message must have at least one group name."
559 (when (gnus-registry-group-count id)
560 ;; we now know the trail has at least 1 group name, so it's not empty
561 (let ((trail (gethash id gnus-registry-hashtb))
562 (old-extra (gnus-registry-fetch-extra id))
564 (dolist (crumb trail)
565 (unless (stringp crumb)
566 (dolist (entry crumb)
567 (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
569 (remhash id entry-cache))))
570 (puthash id (cons extra (delete old-extra trail))
571 gnus-registry-hashtb)
572 (setq gnus-registry-dirty t)))))
574 (defun gnus-registry-store-extra-entry (id key value)
575 "Put a specific entry in the extras field of the registry entry for id."
576 (let* ((extra (gnus-registry-fetch-extra id))
577 (alist (cons (cons key value)
578 (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
579 (gnus-registry-store-extra id alist)))
581 (defun gnus-registry-fetch-group (id)
582 "Get the group of a message, based on the message ID.
583 Returns the first place where the trail finds a group name."
584 (when (gnus-registry-group-count id)
585 ;; we now know the trail has at least 1 group name
586 (let ((trail (gethash id gnus-registry-hashtb)))
587 (dolist (crumb trail)
588 (when (stringp crumb)
589 (return (if gnus-registry-use-long-group-names
591 (gnus-group-short-name crumb))))))))
593 (defun gnus-registry-group-count (id)
594 "Get the number of groups of a message, based on the message ID."
595 (let ((trail (gethash id gnus-registry-hashtb)))
596 (if (and trail (listp trail))
597 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
600 (defun gnus-registry-delete-group (id group)
601 "Delete a group for a message, based on the message ID."
604 (let ((trail (gethash id gnus-registry-hashtb))
605 (group (gnus-group-short-name group)))
606 (puthash id (if trail
609 gnus-registry-hashtb))
610 ;; now, clear the entry if there are no more groups
611 (when gnus-registry-trim-articles-without-groups
612 (unless (gnus-registry-group-count id)
613 (gnus-registry-delete-id id)))
614 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
616 (defun gnus-registry-delete-id (id)
617 "Delete a message ID from the registry."
619 (remhash id gnus-registry-hashtb)
622 (when (hash-table-p value)
624 gnus-registry-hashtb)))
626 (defun gnus-registry-add-group (id group &optional subject sender)
627 "Add a group for a message, based on the message ID."
630 (not (string-match "totally-fudged-out-message-id" id)))
631 (let ((full-group group)
632 (group (if gnus-registry-use-long-group-names
634 (gnus-group-short-name group))))
635 (gnus-registry-delete-group id group)
637 (unless gnus-registry-use-long-group-names ;; unnecessary in this case
638 (gnus-registry-delete-group id full-group))
640 (let ((trail (gethash id gnus-registry-hashtb)))
641 (puthash id (if trail
644 gnus-registry-hashtb)
646 (when (and (gnus-registry-track-subject-p)
648 (gnus-registry-store-extra-entry
651 (gnus-registry-simplify-subject subject)))
652 (when (and (gnus-registry-track-sender-p)
654 (gnus-registry-store-extra-entry
659 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
661 (defun gnus-registry-clear ()
662 "Clear the Gnus registry."
664 (setq gnus-registry-alist nil)
665 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
666 (setq gnus-registry-dirty t))
669 (defun gnus-registry-initialize ()
671 (setq gnus-registry-install t)
672 (gnus-registry-install-hooks)
673 (gnus-registry-read))
676 (defun gnus-registry-install-hooks ()
677 "Install the registry hooks."
679 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
680 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
681 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
682 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
684 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
685 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
687 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
689 (defun gnus-registry-unload-hook ()
690 "Uninstall the registry hooks."
692 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
693 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
694 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
695 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
697 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
698 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
700 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
702 (when gnus-registry-install
703 (gnus-registry-install-hooks)
704 (gnus-registry-read))
706 ;; TODO: a lot of things
708 (provide 'gnus-registry)
710 ;;; gnus-registry.el ends here