1 ;;; gnus-registry.el --- article registry for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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
72 (defvar gnus-registry-hashtb (make-hash-table
75 "*The article registry by Message ID.")
77 (defcustom gnus-registry-unfollowed-groups '("delayed" "drafts" "queue")
78 "List of groups that gnus-registry-split-fancy-with-parent won't follow.
79 The group names are matched, they don't have to be fully qualified."
81 :type '(repeat string))
83 (defcustom gnus-registry-install nil
84 "Whether the registry should be installed."
88 (defcustom gnus-registry-clean-empty t
89 "Whether the empty registry entries should be deleted.
90 Registry entries are considered empty when they have no groups
95 (defcustom gnus-registry-use-long-group-names nil
96 "Whether the registry should use long group names (BUGGY)."
100 (defcustom gnus-registry-track-extra nil
101 "Whether the registry should track extra data about a message.
102 The Subject and Sender (From:) headers are currently tracked this
104 :group 'gnus-registry
106 '(set :tag "Tracking choices"
107 (const :tag "Track by subject (Subject: header)" subject)
108 (const :tag "Track by sender (From: header)" sender)))
110 (defcustom gnus-registry-entry-caching t
111 "Whether the registry should cache extra information."
112 :group 'gnus-registry
115 (defcustom gnus-registry-minimum-subject-length 5
116 "The minimum length of a subject before it's considered trackable."
117 :group 'gnus-registry
120 (defcustom gnus-registry-trim-articles-without-groups t
121 "Whether the registry should clean out message IDs without groups."
122 :group 'gnus-registry
125 (defcustom gnus-registry-cache-file
127 (or gnus-dribble-directory gnus-home-directory "~/")
128 ".gnus.registry.eld")
129 "File where the Gnus registry will be stored."
130 :group 'gnus-registry
133 (defcustom gnus-registry-max-entries nil
134 "Maximum number of entries in the registry, nil for unlimited."
135 :group 'gnus-registry
136 :type '(radio (const :format "Unlimited " nil)
137 (integer :format "Maximum number: %v")))
139 (defun gnus-registry-track-subject-p ()
140 (memq 'subject gnus-registry-track-extra))
142 (defun gnus-registry-track-sender-p ()
143 (memq 'sender gnus-registry-track-extra))
145 (defun gnus-registry-cache-read ()
146 "Read the registry cache file."
148 (let ((file gnus-registry-cache-file))
149 (when (file-exists-p file)
150 (gnus-message 5 "Reading %s..." file)
152 (gnus-message 5 "Reading %s...done" file))))
154 (defun gnus-registry-cache-save ()
155 "Save the registry cache file."
157 (let ((file gnus-registry-cache-file))
159 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
160 (make-local-variable 'version-control)
161 (setq version-control gnus-backup-startup-file)
162 (setq buffer-file-name file)
163 (setq default-directory (file-name-directory buffer-file-name))
164 (buffer-disable-undo)
166 (gnus-message 5 "Saving %s..." file)
167 (if gnus-save-startup-file-via-temp-buffer
168 (let ((coding-system-for-write gnus-ding-file-coding-system)
169 (standard-output (current-buffer)))
170 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
171 (gnus-registry-cache-whitespace file)
173 (let ((coding-system-for-write gnus-ding-file-coding-system)
174 (version-control gnus-backup-startup-file)
176 (working-dir (file-name-directory file))
179 ;; Generate the name of a non-existent file.
180 (while (progn (setq working-file
182 (if (and (eq system-type 'ms-dos)
183 (not (gnus-long-file-names)))
184 "%s#%d.tm#" ; MSDOS limits files to 8+3
185 (if (memq system-type '(vax-vms axp-vms))
188 working-dir (setq i (1+ i))))
189 (file-exists-p working-file)))
193 (gnus-with-output-to-file working-file
194 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
196 ;; These bindings will mislead the current buffer
197 ;; into thinking that it is visiting the startup
199 (let ((buffer-backed-up nil)
200 (buffer-file-name startup-file)
201 (file-precious-flag t)
202 (setmodes (file-modes startup-file)))
203 ;; Backup the current version of the startup file.
206 ;; Replace the existing startup file with the temp file.
207 (rename-file working-file startup-file t)
208 (gnus-set-file-modes startup-file setmodes)))
210 (delete-file working-file)
213 (gnus-kill-buffer (current-buffer))
214 (gnus-message 5 "Saving %s...done" file))))
216 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
217 ;; Save the gnus-registry file with extra line breaks.
218 (defun gnus-registry-cache-whitespace (filename)
219 (gnus-message 7 "Adding whitespace to %s" filename)
221 (goto-char (point-min))
222 (while (re-search-forward "^(\\|(\\\"" nil t)
223 (replace-match "\n\\&" t))
224 (goto-char (point-min))
225 (while (re-search-forward " $" nil t)
226 (replace-match "" t t))))
228 (defun gnus-registry-save (&optional force)
229 (when (or gnus-registry-dirty force)
230 (let ((caching gnus-registry-entry-caching))
231 ;; turn off entry caching, so mtime doesn't get recorded
232 (setq gnus-registry-entry-caching nil)
233 ;; remove entry caches
236 (if (hash-table-p value)
237 (remhash key gnus-registry-hashtb)))
238 gnus-registry-hashtb)
239 ;; remove empty entries
240 (when gnus-registry-clean-empty
241 (gnus-registry-clean-empty-function))
242 ;; now trim the registry appropriately
243 (setq gnus-registry-alist (gnus-registry-trim
244 (hashtable-to-alist gnus-registry-hashtb)))
246 (gnus-registry-cache-save)
247 (setq gnus-registry-entry-caching caching)
248 (setq gnus-registry-dirty nil))))
250 (defun gnus-registry-clean-empty-function ()
251 "Remove all empty entries from the registry. Returns count thereof."
256 (gnus-registry-fetch-group key)
257 ;; TODO: look for specific extra data here!
258 ;; in this example, we look for 'label
259 (gnus-registry-fetch-extra key 'label))
261 (remhash key gnus-registry-hashtb)))
262 gnus-registry-hashtb)
265 (defun gnus-registry-read ()
266 (gnus-registry-cache-read)
267 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
268 (setq gnus-registry-dirty nil))
270 (defun gnus-registry-trim (alist)
271 "Trim alist to size, using gnus-registry-max-entries."
272 (if (null gnus-registry-max-entries)
273 alist ; just return the alist
274 ;; else, when given max-entries, trim the alist
275 (let* ((timehash (make-hash-table
278 (trim-length (- (length alist) gnus-registry-max-entries))
279 (trim-length (if (natnump trim-length) trim-length 0)))
282 (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
283 gnus-registry-hashtb)
285 ;; we use the return value of this setq, which is the trimmed alist
292 (cdr (gethash (car a) timehash))
293 (cdr (gethash (car b) timehash))))))))))
295 (defun alist-to-hashtable (alist)
296 "Build a hashtable from the values in ALIST."
297 (let ((ht (make-hash-table
302 (puthash (car kv-pair) (cdr kv-pair) ht))
306 (defun hashtable-to-alist (hash)
307 "Build an alist from the values in HASH."
311 (setq list (cons (cons key value) list)))
315 (defun gnus-registry-action (action data-header from &optional to method)
316 (let* ((id (mail-header-id data-header))
317 (subject (gnus-registry-simplify-subject
318 (mail-header-subject data-header)))
319 (sender (mail-header-from data-header))
320 (from (gnus-group-guess-full-name-from-command-method from))
321 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
322 (to-name (if to to "the Bit Bucket"))
323 (old-entry (gethash id gnus-registry-hashtb)))
324 (gnus-message 7 "Registry: article %s %s from %s to %s"
326 (if method "respooling" "going")
330 ;; All except copy will need a delete
331 (gnus-registry-delete-group id from)
333 (when (equal 'copy action)
334 (gnus-registry-add-group id from subject sender)) ; undo the delete
336 (gnus-registry-add-group id to subject sender)))
338 (defun gnus-registry-spool-action (id group &optional subject sender)
339 (let ((group (gnus-group-guess-full-name-from-command-method group)))
340 (when (and (stringp id) (string-match "\r$" id))
341 (setq id (substring id 0 -1)))
342 (gnus-message 7 "Registry: article %s spooled to %s"
345 (gnus-registry-add-group id group subject sender)))
347 ;; Function for nn{mail|imap}-split-fancy: look up all references in
348 ;; the cache and if a match is found, return that group.
349 (defun gnus-registry-split-fancy-with-parent ()
350 "Split this message into the same group as its parent. The parent
351 is obtained from the registry. This function can be used as an entry
352 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
353 this: (: gnus-registry-split-fancy-with-parent)
355 This function tracks ALL backends, unlike
356 `nnmail-split-fancy-with-parent' which tracks only nnmail
359 For a message to be split, it looks for the parent message in the
360 References or In-Reply-To header and then looks in the registry to
361 see which group that message was put in. This group is returned.
363 See the Info node `(gnus)Fancy Mail Splitting' for more details."
364 (let* ((refstr (or (message-fetch-field "references") "")) ; guarantee string
365 (reply-to (message-fetch-field "in-reply-to")) ; grab reply-to
366 ;; now, if reply-to is valid, append it to the References
368 (concat refstr " " reply-to)
370 (nnmail-split-fancy-with-parent-ignore-groups
371 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
372 nnmail-split-fancy-with-parent-ignore-groups
373 (list nnmail-split-fancy-with-parent-ignore-groups)))
375 ;; the references string must be valid and parse to valid references
376 (if (and refstr (gnus-extract-references refstr))
378 (setq references (nreverse (gnus-extract-references refstr)))
380 (setq res (or (gnus-registry-fetch-group x) res))
381 (when (or (gnus-registry-grep-in-list
383 gnus-registry-unfollowed-groups)
384 (gnus-registry-grep-in-list
386 nnmail-split-fancy-with-parent-ignore-groups))
390 ;; else: there were no references, now try the extra tracking
391 (let ((sender (message-fetch-field "from"))
392 (subject (gnus-registry-simplify-subject
393 (message-fetch-field "subject")))
395 (when (and single-match
396 (gnus-registry-track-sender-p)
400 (let ((this-sender (cdr
401 (gnus-registry-fetch-extra key 'sender))))
402 (when (and single-match
404 (equal sender this-sender))
405 ;; too many matches, bail
406 (unless (equal res (gnus-registry-fetch-group key))
407 (setq single-match nil))
408 (setq res (gnus-registry-fetch-group key))
409 (when (and sender res)
411 ;; raise level of messaging if gnus-registry-track-extra
412 (if gnus-registry-track-extra 7 9)
413 "%s (extra tracking) traced sender %s to group %s"
414 "gnus-registry-split-fancy-with-parent"
417 gnus-registry-hashtb))
418 (when (and single-match
419 (gnus-registry-track-subject-p)
421 (< gnus-registry-minimum-subject-length (length subject)))
424 (let ((this-subject (cdr
425 (gnus-registry-fetch-extra key 'subject))))
426 (when (and single-match
428 (equal subject this-subject))
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 subject res)
435 ;; raise level of messaging if gnus-registry-track-extra
436 (if gnus-registry-track-extra 7 9)
437 "%s (extra tracking) traced subject %s to group %s"
438 "gnus-registry-split-fancy-with-parent"
441 gnus-registry-hashtb))
445 "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
448 (when (and refstr res)
451 "gnus-registry-split-fancy-with-parent traced %s to group %s"
454 (when (and res gnus-registry-use-long-group-names)
455 (let ((m1 (gnus-find-method-for-group res))
456 (m2 (or gnus-command-method
457 (gnus-find-method-for-group gnus-newsgroup-name)))
458 (short-res (gnus-group-short-name res)))
459 (if (gnus-methods-equal-p m1 m2)
463 "gnus-registry-split-fancy-with-parent stripped group %s to %s"
466 (setq res short-res))
470 "gnus-registry-split-fancy-with-parent ignored foreign group %s"
475 (defun gnus-registry-register-message-ids ()
476 "Register the Message-ID of every article in the group"
477 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
478 (dolist (article gnus-newsgroup-articles)
479 (let ((id (gnus-registry-fetch-message-id-fast article)))
480 (unless (gnus-registry-fetch-group id)
481 (gnus-message 9 "Registry: Registering article %d with group %s"
482 article gnus-newsgroup-name)
483 (gnus-registry-add-group
484 (gnus-registry-fetch-message-id-fast article)
486 (gnus-registry-fetch-simplified-message-subject-fast article)
487 (gnus-registry-fetch-sender-fast article)))))))
489 (defun gnus-registry-fetch-message-id-fast (article)
490 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
491 (if (and (numberp article)
492 (assoc article (gnus-data-list nil)))
493 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
496 (defun gnus-registry-simplify-subject (subject)
497 (if (stringp subject)
498 (gnus-simplify-subject subject)
501 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
502 "Fetch the Subject quickly, using the internal gnus-data-list function"
503 (if (and (numberp article)
504 (assoc article (gnus-data-list nil)))
505 (gnus-registry-simplify-subject
506 (mail-header-subject (gnus-data-header
507 (assoc article (gnus-data-list nil)))))
510 (defun gnus-registry-fetch-sender-fast (article)
511 "Fetch the Sender quickly, using the internal gnus-data-list function"
512 (if (and (numberp article)
513 (assoc article (gnus-data-list nil)))
514 (mail-header-from (gnus-data-header
515 (assoc article (gnus-data-list nil))))
518 (defun gnus-registry-grep-in-list (word list)
524 (string-match x word))
527 (defun gnus-registry-fetch-extra (id &optional entry)
528 "Get the extra data of a message, based on the message ID.
529 Returns the first place where the trail finds a nonstring."
530 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
532 (hash-table-p entry-cache)
533 (gethash id entry-cache))
534 (gethash id entry-cache)
535 ;; else, if there is no caching possible...
536 (let ((trail (gethash id gnus-registry-hashtb)))
538 (dolist (crumb trail)
539 (unless (stringp crumb)
540 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
542 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
543 "Get the extra data of a message, or a specific entry in it.
544 Update the entry cache if needed."
546 (let ((entry-cache (gethash entry gnus-registry-hashtb))
548 (when gnus-registry-entry-caching
549 ;; create the hash table
550 (unless (hash-table-p entry-cache)
551 (setq entry-cache (make-hash-table
554 (puthash entry entry-cache gnus-registry-hashtb))
556 ;; get the entree from the hash table or from the alist
557 (setq entree (gethash id entry-cache)))
560 (setq entree (assq entry alist))
561 (when gnus-registry-entry-caching
562 (puthash id entree entry-cache)))
566 (defun gnus-registry-store-extra (id extra)
567 "Store the extra data of a message, based on the message ID.
568 The message must have at least one group name."
569 (when (gnus-registry-group-count id)
570 ;; we now know the trail has at least 1 group name, so it's not empty
571 (let ((trail (gethash id gnus-registry-hashtb))
572 (old-extra (gnus-registry-fetch-extra id))
574 (dolist (crumb trail)
575 (unless (stringp crumb)
576 (dolist (entry crumb)
577 (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
579 (remhash id entry-cache))))
580 (puthash id (cons extra (delete old-extra trail))
581 gnus-registry-hashtb)
582 (setq gnus-registry-dirty t)))))
584 (defun gnus-registry-store-extra-entry (id key value)
585 "Put a specific entry in the extras field of the registry entry for id."
586 (let* ((extra (gnus-registry-fetch-extra id))
587 (alist (cons (cons key value)
588 (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
589 (gnus-registry-store-extra id alist)))
591 (defun gnus-registry-fetch-group (id)
592 "Get the group of a message, based on the message ID.
593 Returns the first place where the trail finds a group name."
594 (when (gnus-registry-group-count id)
595 ;; we now know the trail has at least 1 group name
596 (let ((trail (gethash id gnus-registry-hashtb)))
597 (dolist (crumb trail)
598 (when (stringp crumb)
599 (return (if gnus-registry-use-long-group-names
601 (gnus-group-short-name crumb))))))))
603 (defun gnus-registry-group-count (id)
604 "Get the number of groups of a message, based on the message ID."
605 (let ((trail (gethash id gnus-registry-hashtb)))
606 (if (and trail (listp trail))
607 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
610 (defun gnus-registry-delete-group (id group)
611 "Delete a group for a message, based on the message ID."
614 (let ((trail (gethash id gnus-registry-hashtb))
615 (group (gnus-group-short-name group)))
616 (puthash id (if trail
619 gnus-registry-hashtb))
620 ;; now, clear the entry if there are no more groups
621 (when gnus-registry-trim-articles-without-groups
622 (unless (gnus-registry-group-count id)
623 (gnus-registry-delete-id id)))
624 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
626 (defun gnus-registry-delete-id (id)
627 "Delete a message ID from the registry."
629 (remhash id gnus-registry-hashtb)
632 (when (hash-table-p value)
634 gnus-registry-hashtb)))
636 (defun gnus-registry-add-group (id group &optional subject sender)
637 "Add a group for a message, based on the message ID."
640 (not (string-match "totally-fudged-out-message-id" id)))
641 (let ((full-group group)
642 (group (if gnus-registry-use-long-group-names
644 (gnus-group-short-name group))))
645 (gnus-registry-delete-group id group)
647 (unless gnus-registry-use-long-group-names ;; unnecessary in this case
648 (gnus-registry-delete-group id full-group))
650 (let ((trail (gethash id gnus-registry-hashtb)))
651 (puthash id (if trail
654 gnus-registry-hashtb)
656 (when (and (gnus-registry-track-subject-p)
658 (gnus-registry-store-extra-entry
661 (gnus-registry-simplify-subject subject)))
662 (when (and (gnus-registry-track-sender-p)
664 (gnus-registry-store-extra-entry
669 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
671 (defun gnus-registry-clear ()
672 "Clear the Gnus registry."
674 (setq gnus-registry-alist nil)
675 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
676 (setq gnus-registry-dirty t))
679 (defun gnus-registry-initialize ()
681 (setq gnus-registry-install t)
682 (gnus-registry-install-hooks)
683 (gnus-registry-read))
686 (defun gnus-registry-install-hooks ()
687 "Install the registry hooks."
689 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
690 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
691 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
692 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
694 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
695 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
697 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
699 (defun gnus-registry-unload-hook ()
700 "Uninstall the registry hooks."
702 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
703 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
704 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
705 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
707 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
708 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
710 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
712 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
714 (when gnus-registry-install
715 (gnus-registry-install-hooks)
716 (gnus-registry-read))
718 ;; TODO: a lot of things
720 (provide 'gnus-registry)
722 ;;; gnus-registry.el ends here