1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This is the gnus-registry.el package, which works with all
29 ;; backends, not just nnmail (e.g. NNTP). The major issue is that it
30 ;; doesn't go across backends, so for instance if an article is in
31 ;; nnml:sys and you see a reference to it in nnimap splitting, the
32 ;; article will end up in nnimap:sys
34 ;; gnus-registry.el intercepts article respooling, moving, deleting,
35 ;; and copying for all backends. If it doesn't work correctly for
36 ;; you, submit a bug report and I'll be glad to fix it. It needs
37 ;; documentation in the manual (also on my to-do list).
39 ;; Put this in your startup file (~/.gnus.el for instance)
41 ;; (setq gnus-registry-max-entries 2500
42 ;; gnus-registry-use-long-group-names t)
44 ;; (gnus-registry-initialize)
46 ;; Then use this in your fancy-split:
48 ;; (: gnus-registry-split-fancy-with-parent)
52 ;; - get the correct group on spool actions
54 ;; - articles that are spooled to a different backend should be handled
58 (eval-when-compile (require 'cl))
65 (defvar gnus-registry-dirty t
66 "Boolean set to t when the registry is modified")
68 (defgroup gnus-registry nil
73 (defvar gnus-registry-hashtb (make-hash-table
76 "*The article registry by Message ID.")
78 (defcustom gnus-registry-unfollowed-groups '("delayed" "drafts" "queue")
79 "List of groups that gnus-registry-split-fancy-with-parent won't follow.
80 The group names are matched, they don't have to be fully qualified."
82 :type '(repeat string))
84 (defcustom gnus-registry-install nil
85 "Whether the registry should be installed."
89 (defcustom gnus-registry-clean-empty t
90 "Whether the empty registry entries should be deleted.
91 Registry entries are considered empty when they have no groups
96 (defcustom gnus-registry-use-long-group-names nil
97 "Whether the registry should use long group names (BUGGY)."
101 (defcustom gnus-registry-track-extra nil
102 "Whether the registry should track extra data about a message.
103 The Subject and Sender (From:) headers are currently tracked this
105 :group 'gnus-registry
107 '(set :tag "Tracking choices"
108 (const :tag "Track by subject (Subject: header)" subject)
109 (const :tag "Track by sender (From: header)" sender)))
111 (defcustom gnus-registry-entry-caching t
112 "Whether the registry should cache extra information."
113 :group 'gnus-registry
116 (defcustom gnus-registry-minimum-subject-length 5
117 "The minimum length of a subject before it's considered trackable."
118 :group 'gnus-registry
121 (defcustom gnus-registry-trim-articles-without-groups t
122 "Whether the registry should clean out message IDs without groups."
123 :group 'gnus-registry
126 (defcustom gnus-registry-cache-file
128 (or gnus-dribble-directory gnus-home-directory "~/")
129 ".gnus.registry.eld")
130 "File where the Gnus registry will be stored."
131 :group 'gnus-registry
134 (defcustom gnus-registry-max-entries nil
135 "Maximum number of entries in the registry, nil for unlimited."
136 :group 'gnus-registry
137 :type '(radio (const :format "Unlimited " nil)
138 (integer :format "Maximum number: %v")))
140 (defun gnus-registry-track-subject-p ()
141 (memq 'subject gnus-registry-track-extra))
143 (defun gnus-registry-track-sender-p ()
144 (memq 'sender gnus-registry-track-extra))
146 (defun gnus-registry-cache-read ()
147 "Read the registry cache file."
149 (let ((file gnus-registry-cache-file))
150 (when (file-exists-p file)
151 (gnus-message 5 "Reading %s..." file)
153 (gnus-message 5 "Reading %s...done" file))))
155 (defun gnus-registry-cache-save ()
156 "Save the registry cache file."
158 (let ((file gnus-registry-cache-file))
160 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
161 (make-local-variable 'version-control)
162 (setq version-control gnus-backup-startup-file)
163 (setq buffer-file-name file)
164 (setq default-directory (file-name-directory buffer-file-name))
165 (buffer-disable-undo)
167 (gnus-message 5 "Saving %s..." file)
168 (if gnus-save-startup-file-via-temp-buffer
169 (let ((coding-system-for-write gnus-ding-file-coding-system)
170 (standard-output (current-buffer)))
171 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
172 (gnus-registry-cache-whitespace file)
174 (let ((coding-system-for-write gnus-ding-file-coding-system)
175 (version-control gnus-backup-startup-file)
177 (working-dir (file-name-directory file))
180 ;; Generate the name of a non-existent file.
181 (while (progn (setq working-file
183 (if (and (eq system-type 'ms-dos)
184 (not (gnus-long-file-names)))
185 "%s#%d.tm#" ; MSDOS limits files to 8+3
186 (if (memq system-type '(vax-vms axp-vms))
189 working-dir (setq i (1+ i))))
190 (file-exists-p working-file)))
194 (gnus-with-output-to-file working-file
195 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
197 ;; These bindings will mislead the current buffer
198 ;; into thinking that it is visiting the startup
200 (let ((buffer-backed-up nil)
201 (buffer-file-name startup-file)
202 (file-precious-flag t)
203 (setmodes (file-modes startup-file)))
204 ;; Backup the current version of the startup file.
207 ;; Replace the existing startup file with the temp file.
208 (rename-file working-file startup-file t)
209 (gnus-set-file-modes startup-file setmodes)))
211 (delete-file working-file)
214 (gnus-kill-buffer (current-buffer))
215 (gnus-message 5 "Saving %s...done" file))))
217 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
218 ;; Save the gnus-registry file with extra line breaks.
219 (defun gnus-registry-cache-whitespace (filename)
220 (gnus-message 7 "Adding whitespace to %s" filename)
222 (goto-char (point-min))
223 (while (re-search-forward "^(\\|(\\\"" nil t)
224 (replace-match "\n\\&" t))
225 (goto-char (point-min))
226 (while (re-search-forward " $" nil t)
227 (replace-match "" t t))))
229 (defun gnus-registry-save (&optional force)
230 (when (or gnus-registry-dirty force)
231 (let ((caching gnus-registry-entry-caching))
232 ;; turn off entry caching, so mtime doesn't get recorded
233 (setq gnus-registry-entry-caching nil)
234 ;; remove entry caches
237 (if (hash-table-p value)
238 (remhash key gnus-registry-hashtb)))
239 gnus-registry-hashtb)
240 ;; remove empty entries
241 (when gnus-registry-clean-empty
242 (gnus-registry-clean-empty-function))
243 ;; now trim the registry appropriately
244 (setq gnus-registry-alist (gnus-registry-trim
245 (hashtable-to-alist gnus-registry-hashtb)))
247 (gnus-registry-cache-save)
248 (setq gnus-registry-entry-caching caching)
249 (setq gnus-registry-dirty nil))))
251 (defun gnus-registry-clean-empty-function ()
252 "Remove all empty entries from the registry. Returns count thereof."
258 (dolist (group (gnus-registry-fetch-groups key))
259 (when (gnus-parameter-registry-ignore group)
262 "gnus-registry: deleted ignored group %s from key %s"
264 (gnus-registry-delete-group key group)))
266 (unless (gnus-registry-group-count key)
267 (gnus-registry-delete-id key))
270 (gnus-registry-fetch-group key)
271 ;; TODO: look for specific extra data here!
272 ;; in this example, we look for 'label
273 (gnus-registry-fetch-extra key 'label)
276 (gnus-registry-delete-id key))
278 (unless (stringp key)
281 "gnus-registry key %s was not a string, removing"
283 (gnus-registry-delete-id key))))
285 gnus-registry-hashtb)
288 (defun gnus-registry-read ()
289 (gnus-registry-cache-read)
290 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
291 (setq gnus-registry-dirty nil))
293 (defun gnus-registry-trim (alist)
294 "Trim alist to size, using gnus-registry-max-entries.
295 Also, drop all gnus-registry-ignored-groups matches."
296 (if (null gnus-registry-max-entries)
297 alist ; just return the alist
298 ;; else, when given max-entries, trim the alist
299 (let* ((timehash (make-hash-table
302 (trim-length (- (length alist) gnus-registry-max-entries))
303 (trim-length (if (natnump trim-length) trim-length 0)))
306 (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
307 gnus-registry-hashtb)
309 ;; we use the return value of this setq, which is the trimmed alist
316 (or (cdr (gethash (car a) timehash)) '(0 0 0))
317 (or (cdr (gethash (car b) timehash)) '(0 0 0))))))))))
319 (defun alist-to-hashtable (alist)
320 "Build a hashtable from the values in ALIST."
321 (let ((ht (make-hash-table
326 (puthash (car kv-pair) (cdr kv-pair) ht))
330 (defun hashtable-to-alist (hash)
331 "Build an alist from the values in HASH."
335 (setq list (cons (cons key value) list)))
339 (defun gnus-registry-action (action data-header from &optional to method)
340 (let* ((id (mail-header-id data-header))
341 (subject (gnus-registry-simplify-subject
342 (mail-header-subject data-header)))
343 (sender (mail-header-from data-header))
344 (from (gnus-group-guess-full-name-from-command-method from))
345 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
346 (to-name (if to to "the Bit Bucket"))
347 (old-entry (gethash id gnus-registry-hashtb)))
348 (gnus-message 7 "Registry: article %s %s from %s to %s"
350 (if method "respooling" "going")
354 ;; All except copy will need a delete
355 (gnus-registry-delete-group id from)
357 (when (equal 'copy action)
358 (gnus-registry-add-group id from subject sender)) ; undo the delete
360 (gnus-registry-add-group id to subject sender)))
362 (defun gnus-registry-spool-action (id group &optional subject sender)
363 (let ((group (gnus-group-guess-full-name-from-command-method group)))
364 (when (and (stringp id) (string-match "\r$" id))
365 (setq id (substring id 0 -1)))
366 (gnus-message 7 "Registry: article %s spooled to %s"
369 (gnus-registry-add-group id group subject sender)))
371 ;; Function for nn{mail|imap}-split-fancy: look up all references in
372 ;; the cache and if a match is found, return that group.
373 (defun gnus-registry-split-fancy-with-parent ()
374 "Split this message into the same group as its parent. The parent
375 is obtained from the registry. This function can be used as an entry
376 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
377 this: (: gnus-registry-split-fancy-with-parent)
379 This function tracks ALL backends, unlike
380 `nnmail-split-fancy-with-parent' which tracks only nnmail
383 For a message to be split, it looks for the parent message in the
384 References or In-Reply-To header and then looks in the registry to
385 see which group that message was put in. This group is returned.
387 See the Info node `(gnus)Fancy Mail Splitting' for more details."
388 (let* ((refstr (or (message-fetch-field "references") "")) ; guarantee string
389 (reply-to (message-fetch-field "in-reply-to")) ; grab reply-to
390 ;; now, if reply-to is valid, append it to the References
392 (concat refstr " " reply-to)
394 (nnmail-split-fancy-with-parent-ignore-groups
395 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
396 nnmail-split-fancy-with-parent-ignore-groups
397 (list nnmail-split-fancy-with-parent-ignore-groups)))
399 ;; the references string must be valid and parse to valid references
400 (if (and refstr (gnus-extract-references refstr))
402 (setq references (nreverse (gnus-extract-references refstr)))
404 (setq res (or (gnus-registry-fetch-group x) res))
405 (when (or (gnus-registry-grep-in-list
407 gnus-registry-unfollowed-groups)
408 (gnus-registry-grep-in-list
410 nnmail-split-fancy-with-parent-ignore-groups))
414 ;; else: there were no references, now try the extra tracking
415 (let ((sender (message-fetch-field "from"))
416 (subject (gnus-registry-simplify-subject
417 (message-fetch-field "subject")))
419 (when (and single-match
420 (gnus-registry-track-sender-p)
424 (let ((this-sender (cdr
425 (gnus-registry-fetch-extra key 'sender))))
426 (when (and single-match
428 (equal sender this-sender))
429 ;; too many matches, bail
430 (unless (equal res (gnus-registry-fetch-group key))
431 (setq single-match nil))
432 (setq res (gnus-registry-fetch-group key))
433 (when (and sender res)
435 ;; raise level of messaging if gnus-registry-track-extra
436 (if gnus-registry-track-extra 7 9)
437 "%s (extra tracking) traced sender %s to group %s"
438 "gnus-registry-split-fancy-with-parent"
441 gnus-registry-hashtb))
442 (when (and single-match
443 (gnus-registry-track-subject-p)
445 (< gnus-registry-minimum-subject-length (length subject)))
448 (let ((this-subject (cdr
449 (gnus-registry-fetch-extra key 'subject))))
450 (when (and single-match
452 (equal subject this-subject))
453 ;; too many matches, bail
454 (unless (equal res (gnus-registry-fetch-group key))
455 (setq single-match nil))
456 (setq res (gnus-registry-fetch-group key))
457 (when (and subject res)
459 ;; raise level of messaging if gnus-registry-track-extra
460 (if gnus-registry-track-extra 7 9)
461 "%s (extra tracking) traced subject %s to group %s"
462 "gnus-registry-split-fancy-with-parent"
465 gnus-registry-hashtb))
469 "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
472 (when (and refstr res)
475 "gnus-registry-split-fancy-with-parent traced %s to group %s"
478 (when (and res gnus-registry-use-long-group-names)
479 (let ((m1 (gnus-find-method-for-group res))
480 (m2 (or gnus-command-method
481 (gnus-find-method-for-group gnus-newsgroup-name)))
482 (short-res (gnus-group-short-name res)))
483 (if (gnus-methods-equal-p m1 m2)
487 "gnus-registry-split-fancy-with-parent stripped group %s to %s"
490 (setq res short-res))
494 "gnus-registry-split-fancy-with-parent ignored foreign group %s"
499 (defun gnus-registry-register-message-ids ()
500 "Register the Message-ID of every article in the group"
501 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
502 (dolist (article gnus-newsgroup-articles)
503 (let ((id (gnus-registry-fetch-message-id-fast article)))
504 (unless (gnus-registry-fetch-group id)
505 (gnus-message 9 "Registry: Registering article %d with group %s"
506 article gnus-newsgroup-name)
507 (gnus-registry-add-group
508 (gnus-registry-fetch-message-id-fast article)
510 (gnus-registry-fetch-simplified-message-subject-fast article)
511 (gnus-registry-fetch-sender-fast article)))))))
513 (defun gnus-registry-fetch-message-id-fast (article)
514 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
515 (if (and (numberp article)
516 (assoc article (gnus-data-list nil)))
517 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
520 (defun gnus-registry-simplify-subject (subject)
521 (if (stringp subject)
522 (gnus-simplify-subject subject)
525 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
526 "Fetch the Subject quickly, using the internal gnus-data-list function"
527 (if (and (numberp article)
528 (assoc article (gnus-data-list nil)))
529 (gnus-registry-simplify-subject
530 (mail-header-subject (gnus-data-header
531 (assoc article (gnus-data-list nil)))))
534 (defun gnus-registry-fetch-sender-fast (article)
535 "Fetch the Sender quickly, using the internal gnus-data-list function"
536 (if (and (numberp article)
537 (assoc article (gnus-data-list nil)))
538 (mail-header-from (gnus-data-header
539 (assoc article (gnus-data-list nil))))
542 (defun gnus-registry-grep-in-list (word list)
548 (string-match x word))
551 (defun gnus-registry-fetch-extra (id &optional entry)
552 "Get the extra data of a message, based on the message ID.
553 Returns the first place where the trail finds a nonstring."
554 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
556 (hash-table-p entry-cache)
557 (gethash id entry-cache))
558 (gethash id entry-cache)
559 ;; else, if there is no caching possible...
560 (let ((trail (gethash id gnus-registry-hashtb)))
562 (dolist (crumb trail)
563 (unless (stringp crumb)
564 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
566 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
567 "Get the extra data of a message, or a specific entry in it.
568 Update the entry cache if needed."
570 (let ((entry-cache (gethash entry gnus-registry-hashtb))
572 (when gnus-registry-entry-caching
573 ;; create the hash table
574 (unless (hash-table-p entry-cache)
575 (setq entry-cache (make-hash-table
578 (puthash entry entry-cache gnus-registry-hashtb))
580 ;; get the entree from the hash table or from the alist
581 (setq entree (gethash id entry-cache)))
584 (setq entree (assq entry alist))
585 (when gnus-registry-entry-caching
586 (puthash id entree entry-cache)))
590 (defun gnus-registry-store-extra (id extra)
591 "Store the extra data of a message, based on the message ID.
592 The message must have at least one group name."
593 (when (gnus-registry-group-count id)
594 ;; we now know the trail has at least 1 group name, so it's not empty
595 (let ((trail (gethash id gnus-registry-hashtb))
596 (old-extra (gnus-registry-fetch-extra id))
598 (dolist (crumb trail)
599 (unless (stringp crumb)
600 (dolist (entry crumb)
601 (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
603 (remhash id entry-cache))))
604 (puthash id (cons extra (delete old-extra trail))
605 gnus-registry-hashtb)
606 (setq gnus-registry-dirty t)))))
608 (defun gnus-registry-store-extra-entry (id key value)
609 "Put a specific entry in the extras field of the registry entry for id."
610 (let* ((extra (gnus-registry-fetch-extra id))
611 (alist (cons (cons key value)
612 (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
613 (gnus-registry-store-extra id alist)))
615 (defun gnus-registry-fetch-group (id)
616 "Get the group of a message, based on the message ID.
617 Returns the first place where the trail finds a group name."
618 (when (gnus-registry-group-count id)
619 ;; we now know the trail has at least 1 group name
620 (let ((trail (gethash id gnus-registry-hashtb)))
621 (dolist (crumb trail)
622 (when (stringp crumb)
623 (return (if gnus-registry-use-long-group-names
625 (gnus-group-short-name crumb))))))))
627 (defun gnus-registry-fetch-groups (id)
628 "Get the groups of a message, based on the message ID."
629 (let ((trail (gethash id gnus-registry-hashtb))
631 (dolist (crumb trail)
632 (when (stringp crumb)
633 ;; push the group name into the list
637 (if (or (not (stringp crumb)) gnus-registry-use-long-group-names)
639 (gnus-group-short-name crumb))
641 ;; return the list of groups
644 (defun gnus-registry-group-count (id)
645 "Get the number of groups of a message, based on the message ID."
646 (let ((trail (gethash id gnus-registry-hashtb)))
647 (if (and trail (listp trail))
648 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
651 (defun gnus-registry-delete-group (id group)
652 "Delete a group for a message, based on the message ID."
654 (let ((trail (gethash id gnus-registry-hashtb))
655 (short-group (gnus-group-short-name group)))
656 (puthash id (if trail
657 (delete short-group (delete group trail))
659 gnus-registry-hashtb))
660 ;; now, clear the entry if there are no more groups
661 (when gnus-registry-trim-articles-without-groups
662 (unless (gnus-registry-group-count id)
663 (gnus-registry-delete-id id)))
664 ;; is this ID still in the registry?
665 (when (gethash id gnus-registry-hashtb)
666 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
668 (defun gnus-registry-delete-id (id)
669 "Delete a message ID from the registry."
671 (remhash id gnus-registry-hashtb)
674 (when (hash-table-p value)
676 gnus-registry-hashtb)))
678 (defun gnus-registry-add-group (id group &optional subject sender)
679 "Add a group for a message, based on the message ID."
682 (not (string-match "totally-fudged-out-message-id" id)))
683 (let ((full-group group)
684 (group (if gnus-registry-use-long-group-names
686 (gnus-group-short-name group))))
687 (gnus-registry-delete-group id group)
689 (unless gnus-registry-use-long-group-names ;; unnecessary in this case
690 (gnus-registry-delete-group id full-group))
692 (let ((trail (gethash id gnus-registry-hashtb)))
693 (puthash id (if trail
696 gnus-registry-hashtb)
698 (when (and (gnus-registry-track-subject-p)
700 (gnus-registry-store-extra-entry
703 (gnus-registry-simplify-subject subject)))
704 (when (and (gnus-registry-track-sender-p)
706 (gnus-registry-store-extra-entry
711 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
713 (defun gnus-registry-clear ()
714 "Clear the Gnus registry."
716 (setq gnus-registry-alist nil)
717 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
718 (setq gnus-registry-dirty t))
721 (defun gnus-registry-initialize ()
723 (setq gnus-registry-install t)
724 (gnus-registry-install-hooks)
725 (gnus-registry-read))
728 (defun gnus-registry-install-hooks ()
729 "Install the registry hooks."
731 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
732 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
733 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
734 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
736 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
737 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
739 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
741 (defun gnus-registry-unload-hook ()
742 "Uninstall the registry hooks."
744 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
745 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
746 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
747 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
749 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
750 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
752 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
754 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
756 (when gnus-registry-install
757 (gnus-registry-install-hooks)
758 (gnus-registry-read))
760 ;; TODO: a lot of things
762 (provide 'gnus-registry)
764 ;;; gnus-registry.el ends here