1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 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))
66 (defvar gnus-registry-dirty t
67 "Boolean set to t when the registry is modified")
69 (defgroup gnus-registry nil
74 (defvar gnus-registry-hashtb (make-hash-table
77 "*The article registry by Message ID.")
79 (defcustom gnus-registry-unfollowed-groups '("delayed" "drafts" "queue")
80 "List of groups that gnus-registry-split-fancy-with-parent won't follow.
81 The group names are matched, they don't have to be fully qualified."
83 :type '(repeat string))
85 (defcustom gnus-registry-install nil
86 "Whether the registry should be installed."
90 (defcustom gnus-registry-clean-empty t
91 "Whether the empty registry entries should be deleted.
92 Registry entries are considered empty when they have no groups
97 (defcustom gnus-registry-use-long-group-names nil
98 "Whether the registry should use long group names (BUGGY)."
102 (defcustom gnus-registry-track-extra nil
103 "Whether the registry should track extra data about a message.
104 The Subject and Sender (From:) headers are currently tracked this
106 :group 'gnus-registry
108 '(set :tag "Tracking choices"
109 (const :tag "Track by subject (Subject: header)" subject)
110 (const :tag "Track by sender (From: header)" sender)))
112 (defcustom gnus-registry-entry-caching t
113 "Whether the registry should cache extra information."
114 :group 'gnus-registry
117 (defcustom gnus-registry-minimum-subject-length 5
118 "The minimum length of a subject before it's considered trackable."
119 :group 'gnus-registry
122 (defcustom gnus-registry-trim-articles-without-groups t
123 "Whether the registry should clean out message IDs without groups."
124 :group 'gnus-registry
127 (defcustom gnus-registry-cache-file
129 (or gnus-dribble-directory gnus-home-directory "~/")
130 ".gnus.registry.eld")
131 "File where the Gnus registry will be stored."
132 :group 'gnus-registry
135 (defcustom gnus-registry-max-entries nil
136 "Maximum number of entries in the registry, nil for unlimited."
137 :group 'gnus-registry
138 :type '(radio (const :format "Unlimited " nil)
139 (integer :format "Maximum number: %v")))
141 (defun gnus-registry-track-subject-p ()
142 (memq 'subject gnus-registry-track-extra))
144 (defun gnus-registry-track-sender-p ()
145 (memq 'sender gnus-registry-track-extra))
147 (defun gnus-registry-cache-read ()
148 "Read the registry cache file."
150 (let ((file gnus-registry-cache-file))
151 (when (file-exists-p file)
152 (gnus-message 5 "Reading %s..." file)
154 (gnus-message 5 "Reading %s...done" file))))
156 (defun gnus-registry-cache-save ()
157 "Save the registry cache file."
159 (let ((file gnus-registry-cache-file))
161 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
162 (make-local-variable 'version-control)
163 (setq version-control gnus-backup-startup-file)
164 (setq buffer-file-name file)
165 (setq default-directory (file-name-directory buffer-file-name))
166 (buffer-disable-undo)
168 (gnus-message 5 "Saving %s..." file)
169 (if gnus-save-startup-file-via-temp-buffer
170 (let ((coding-system-for-write gnus-ding-file-coding-system)
171 (standard-output (current-buffer)))
172 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
173 (gnus-registry-cache-whitespace file)
174 (set-text-properties (point-min) (point-max) nil)
176 (let ((coding-system-for-write gnus-ding-file-coding-system)
177 (version-control gnus-backup-startup-file)
179 (working-dir (file-name-directory file))
182 ;; Generate the name of a non-existent file.
183 (while (progn (setq working-file
185 (if (and (eq system-type 'ms-dos)
186 (not (gnus-long-file-names)))
187 "%s#%d.tm#" ; MSDOS limits files to 8+3
188 (if (memq system-type '(vax-vms axp-vms))
191 working-dir (setq i (1+ i))))
192 (file-exists-p working-file)))
196 (gnus-with-output-to-file working-file
197 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
199 ;; These bindings will mislead the current buffer
200 ;; into thinking that it is visiting the startup
202 (let ((buffer-backed-up nil)
203 (buffer-file-name startup-file)
204 (file-precious-flag t)
205 (setmodes (file-modes startup-file)))
206 ;; Backup the current version of the startup file.
209 ;; Replace the existing startup file with the temp file.
210 (rename-file working-file startup-file t)
211 (gnus-set-file-modes startup-file setmodes)))
213 (delete-file working-file)
216 (gnus-kill-buffer (current-buffer))
217 (gnus-message 5 "Saving %s...done" file))))
219 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
220 ;; Save the gnus-registry file with extra line breaks.
221 (defun gnus-registry-cache-whitespace (filename)
222 (gnus-message 7 "Adding whitespace to %s" filename)
224 (goto-char (point-min))
225 (while (re-search-forward "^(\\|(\\\"" nil t)
226 (replace-match "\n\\&" t))
227 (goto-char (point-min))
228 (while (re-search-forward " $" nil t)
229 (replace-match "" t t))))
231 (defun gnus-registry-save (&optional force)
232 (when (or gnus-registry-dirty force)
233 (let ((caching gnus-registry-entry-caching))
234 ;; turn off entry caching, so mtime doesn't get recorded
235 (setq gnus-registry-entry-caching nil)
236 ;; remove entry caches
239 (if (hash-table-p value)
240 (remhash key gnus-registry-hashtb)))
241 gnus-registry-hashtb)
242 ;; remove empty entries
243 (when gnus-registry-clean-empty
244 (gnus-registry-clean-empty-function))
245 ;; now trim the registry appropriately
246 (setq gnus-registry-alist (gnus-registry-trim
247 (gnus-hashtable-to-alist
248 gnus-registry-hashtb)))
250 (gnus-registry-cache-save)
251 (setq gnus-registry-entry-caching caching)
252 (setq gnus-registry-dirty nil))))
254 (defun gnus-registry-clean-empty-function ()
255 "Remove all empty entries from the registry. Returns count thereof."
261 (dolist (group (gnus-registry-fetch-groups key))
262 (when (gnus-parameter-registry-ignore group)
265 "gnus-registry: deleted ignored group %s from key %s"
267 (gnus-registry-delete-group key group)))
269 (unless (gnus-registry-group-count key)
270 (gnus-registry-delete-id key))
273 (gnus-registry-fetch-group key)
274 ;; TODO: look for specific extra data here!
275 ;; in this example, we look for 'label
276 (gnus-registry-fetch-extra key 'label)
279 (gnus-registry-delete-id key))
281 (unless (stringp key)
284 "gnus-registry key %s was not a string, removing"
286 (gnus-registry-delete-id key))))
288 gnus-registry-hashtb)
291 (defun gnus-registry-read ()
292 (gnus-registry-cache-read)
293 (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
294 (setq gnus-registry-dirty nil))
296 (defun gnus-registry-trim (alist)
297 "Trim alist to size, using gnus-registry-max-entries.
298 Also, drop all gnus-registry-ignored-groups matches."
299 (if (null gnus-registry-max-entries)
300 alist ; just return the alist
301 ;; else, when given max-entries, trim the alist
302 (let* ((timehash (make-hash-table
305 (trim-length (- (length alist) gnus-registry-max-entries))
306 (trim-length (if (natnump trim-length) trim-length 0)))
309 (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
310 gnus-registry-hashtb)
312 ;; we use the return value of this setq, which is the trimmed alist
319 (or (cdr (gethash (car a) timehash)) '(0 0 0))
320 (or (cdr (gethash (car b) timehash)) '(0 0 0))))))))))
322 (defun gnus-registry-action (action data-header from &optional to method)
323 (let* ((id (mail-header-id data-header))
324 (subject (gnus-registry-simplify-subject
325 (mail-header-subject data-header)))
326 (sender (mail-header-from data-header))
327 (from (gnus-group-guess-full-name-from-command-method from))
328 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
329 (to-name (if to to "the Bit Bucket"))
330 (old-entry (gethash id gnus-registry-hashtb)))
331 (gnus-message 7 "Registry: article %s %s from %s to %s"
333 (if method "respooling" "going")
337 ;; All except copy will need a delete
338 (gnus-registry-delete-group id from)
340 (when (equal 'copy action)
341 (gnus-registry-add-group id from subject sender)) ; undo the delete
343 (gnus-registry-add-group id to subject sender)))
345 (defun gnus-registry-spool-action (id group &optional subject sender)
346 (let ((group (gnus-group-guess-full-name-from-command-method group)))
347 (when (and (stringp id) (string-match "\r$" id))
348 (setq id (substring id 0 -1)))
349 (gnus-message 7 "Registry: article %s spooled to %s"
352 (gnus-registry-add-group id group subject sender)))
354 ;; Function for nn{mail|imap}-split-fancy: look up all references in
355 ;; the cache and if a match is found, return that group.
356 (defun gnus-registry-split-fancy-with-parent ()
357 "Split this message into the same group as its parent. The parent
358 is obtained from the registry. This function can be used as an entry
359 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
360 this: (: gnus-registry-split-fancy-with-parent)
362 This function tracks ALL backends, unlike
363 `nnmail-split-fancy-with-parent' which tracks only nnmail
366 For a message to be split, it looks for the parent message in the
367 References or In-Reply-To header and then looks in the registry to
368 see which group that message was put in. This group is returned.
370 See the Info node `(gnus)Fancy Mail Splitting' for more details."
371 (let* ((refstr (or (message-fetch-field "references") "")) ; guarantee string
372 (reply-to (message-fetch-field "in-reply-to")) ; grab reply-to
373 ;; now, if reply-to is valid, append it to the References
375 (concat refstr " " reply-to)
377 (nnmail-split-fancy-with-parent-ignore-groups
378 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
379 nnmail-split-fancy-with-parent-ignore-groups
380 (list nnmail-split-fancy-with-parent-ignore-groups)))
382 ;; the references string must be valid and parse to valid references
383 (if (and refstr (gnus-extract-references refstr))
385 (setq references (nreverse (gnus-extract-references refstr)))
387 (setq res (or (gnus-registry-fetch-group x) res))
388 (when (or (gnus-registry-grep-in-list
390 gnus-registry-unfollowed-groups)
391 (gnus-registry-grep-in-list
393 nnmail-split-fancy-with-parent-ignore-groups))
397 ;; else: there were no references, now try the extra tracking
398 (let ((sender (message-fetch-field "from"))
399 (subject (gnus-registry-simplify-subject
400 (message-fetch-field "subject")))
402 (when (and single-match
403 (gnus-registry-track-sender-p)
407 (let ((this-sender (cdr
408 (gnus-registry-fetch-extra key 'sender))))
409 (when (and single-match
411 (equal sender this-sender))
412 ;; too many matches, bail
413 (unless (equal res (gnus-registry-fetch-group key))
414 (setq single-match nil))
415 (setq res (gnus-registry-fetch-group key))
416 (when (and sender res)
418 ;; raise level of messaging if gnus-registry-track-extra
419 (if gnus-registry-track-extra 7 9)
420 "%s (extra tracking) traced sender %s to group %s"
421 "gnus-registry-split-fancy-with-parent"
424 gnus-registry-hashtb))
425 (when (and single-match
426 (gnus-registry-track-subject-p)
428 (< gnus-registry-minimum-subject-length (length subject)))
431 (let ((this-subject (cdr
432 (gnus-registry-fetch-extra key 'subject))))
433 (when (and single-match
435 (equal subject this-subject))
436 ;; too many matches, bail
437 (unless (equal res (gnus-registry-fetch-group key))
438 (setq single-match nil))
439 (setq res (gnus-registry-fetch-group key))
440 (when (and subject res)
442 ;; raise level of messaging if gnus-registry-track-extra
443 (if gnus-registry-track-extra 7 9)
444 "%s (extra tracking) traced subject %s to group %s"
445 "gnus-registry-split-fancy-with-parent"
448 gnus-registry-hashtb))
452 "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
455 (when (and refstr res)
458 "gnus-registry-split-fancy-with-parent traced %s to group %s"
461 (when (and res gnus-registry-use-long-group-names)
462 (let ((m1 (gnus-find-method-for-group res))
463 (m2 (or gnus-command-method
464 (gnus-find-method-for-group gnus-newsgroup-name)))
465 (short-res (gnus-group-short-name res)))
466 (if (gnus-methods-equal-p m1 m2)
470 "gnus-registry-split-fancy-with-parent stripped group %s to %s"
473 (setq res short-res))
477 "gnus-registry-split-fancy-with-parent ignored foreign group %s"
482 (defun gnus-registry-register-message-ids ()
483 "Register the Message-ID of every article in the group"
484 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
485 (dolist (article gnus-newsgroup-articles)
486 (let ((id (gnus-registry-fetch-message-id-fast article)))
487 (unless (gnus-registry-fetch-group id)
488 (gnus-message 9 "Registry: Registering article %d with group %s"
489 article gnus-newsgroup-name)
490 (gnus-registry-add-group
491 (gnus-registry-fetch-message-id-fast article)
493 (gnus-registry-fetch-simplified-message-subject-fast article)
494 (gnus-registry-fetch-sender-fast article)))))))
496 (defun gnus-registry-fetch-message-id-fast (article)
497 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
498 (if (and (numberp article)
499 (assoc article (gnus-data-list nil)))
500 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
503 (defun gnus-registry-simplify-subject (subject)
504 (if (stringp subject)
505 (gnus-simplify-subject subject)
508 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
509 "Fetch the Subject quickly, using the internal gnus-data-list function"
510 (if (and (numberp article)
511 (assoc article (gnus-data-list nil)))
512 (gnus-registry-simplify-subject
513 (mail-header-subject (gnus-data-header
514 (assoc article (gnus-data-list nil)))))
517 (defun gnus-registry-fetch-sender-fast (article)
518 "Fetch the Sender quickly, using the internal gnus-data-list function"
519 (if (and (numberp article)
520 (assoc article (gnus-data-list nil)))
521 (mail-header-from (gnus-data-header
522 (assoc article (gnus-data-list nil))))
525 (defun gnus-registry-grep-in-list (word list)
531 (string-match x word))
534 (defun gnus-registry-fetch-extra (id &optional entry)
535 "Get the extra data of a message, based on the message ID.
536 Returns the first place where the trail finds a nonstring."
537 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
539 (hash-table-p entry-cache)
540 (gethash id entry-cache))
541 (gethash id entry-cache)
542 ;; else, if there is no caching possible...
543 (let ((trail (gethash id gnus-registry-hashtb)))
545 (dolist (crumb trail)
546 (unless (stringp crumb)
547 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
549 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
550 "Get the extra data of a message, or a specific entry in it.
551 Update the entry cache if needed."
553 (let ((entry-cache (gethash entry gnus-registry-hashtb))
555 (when gnus-registry-entry-caching
556 ;; create the hash table
557 (unless (hash-table-p entry-cache)
558 (setq entry-cache (make-hash-table
561 (puthash entry entry-cache gnus-registry-hashtb))
563 ;; get the entree from the hash table or from the alist
564 (setq entree (gethash id entry-cache)))
567 (setq entree (assq entry alist))
568 (when gnus-registry-entry-caching
569 (puthash id entree entry-cache)))
573 (defun gnus-registry-store-extra (id extra)
574 "Store the extra data of a message, based on the message ID.
575 The message must have at least one group name."
576 (when (gnus-registry-group-count id)
577 ;; we now know the trail has at least 1 group name, so it's not empty
578 (let ((trail (gethash id gnus-registry-hashtb))
579 (old-extra (gnus-registry-fetch-extra id))
581 (dolist (crumb trail)
582 (unless (stringp crumb)
583 (dolist (entry crumb)
584 (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
586 (remhash id entry-cache))))
587 (puthash id (cons extra (delete old-extra trail))
588 gnus-registry-hashtb)
589 (setq gnus-registry-dirty t)))))
591 (defun gnus-registry-store-extra-entry (id key value)
592 "Put a specific entry in the extras field of the registry entry for id."
593 (let* ((extra (gnus-registry-fetch-extra id))
594 (alist (cons (cons key value)
595 (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
596 (gnus-registry-store-extra id alist)))
598 (defun gnus-registry-fetch-group (id)
599 "Get the group of a message, based on the message ID.
600 Returns the first place where the trail finds a group name."
601 (when (gnus-registry-group-count id)
602 ;; we now know the trail has at least 1 group name
603 (let ((trail (gethash id gnus-registry-hashtb)))
604 (dolist (crumb trail)
605 (when (stringp crumb)
606 (return (if gnus-registry-use-long-group-names
608 (gnus-group-short-name crumb))))))))
610 (defun gnus-registry-fetch-groups (id)
611 "Get the groups of a message, based on the message ID."
612 (let ((trail (gethash id gnus-registry-hashtb))
614 (dolist (crumb trail)
615 (when (stringp crumb)
616 ;; push the group name into the list
620 (if (or (not (stringp crumb)) gnus-registry-use-long-group-names)
622 (gnus-group-short-name crumb))
624 ;; return the list of groups
627 (defun gnus-registry-group-count (id)
628 "Get the number of groups of a message, based on the message ID."
629 (let ((trail (gethash id gnus-registry-hashtb)))
630 (if (and trail (listp trail))
631 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
634 (defun gnus-registry-delete-group (id group)
635 "Delete a group for a message, based on the message ID."
637 (let ((trail (gethash id gnus-registry-hashtb))
638 (short-group (gnus-group-short-name group)))
639 (puthash id (if trail
640 (delete short-group (delete group trail))
642 gnus-registry-hashtb))
643 ;; now, clear the entry if there are no more groups
644 (when gnus-registry-trim-articles-without-groups
645 (unless (gnus-registry-group-count id)
646 (gnus-registry-delete-id id)))
647 ;; is this ID still in the registry?
648 (when (gethash id gnus-registry-hashtb)
649 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
651 (defun gnus-registry-delete-id (id)
652 "Delete a message ID from the registry."
654 (remhash id gnus-registry-hashtb)
657 (when (hash-table-p value)
659 gnus-registry-hashtb)))
661 (defun gnus-registry-add-group (id group &optional subject sender)
662 "Add a group for a message, based on the message ID."
665 (not (string-match "totally-fudged-out-message-id" id)))
666 (let ((full-group group)
667 (group (if gnus-registry-use-long-group-names
669 (gnus-group-short-name group))))
670 (gnus-registry-delete-group id group)
672 (unless gnus-registry-use-long-group-names ;; unnecessary in this case
673 (gnus-registry-delete-group id full-group))
675 (let ((trail (gethash id gnus-registry-hashtb)))
676 (puthash id (if trail
679 gnus-registry-hashtb)
681 (when (and (gnus-registry-track-subject-p)
683 (gnus-registry-store-extra-entry
686 (gnus-registry-simplify-subject subject)))
687 (when (and (gnus-registry-track-sender-p)
689 (gnus-registry-store-extra-entry
694 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
696 (defun gnus-registry-clear ()
697 "Clear the Gnus registry."
699 (setq gnus-registry-alist nil)
700 (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
701 (setq gnus-registry-dirty t))
704 (defun gnus-registry-initialize ()
706 (setq gnus-registry-install t)
707 (gnus-registry-install-hooks)
708 (gnus-registry-read))
711 (defun gnus-registry-install-hooks ()
712 "Install the registry hooks."
714 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
715 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
716 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
717 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
719 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
720 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
722 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
724 (defun gnus-registry-unload-hook ()
725 "Uninstall the registry hooks."
727 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
728 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
729 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
730 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
732 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
733 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
735 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
737 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
739 (when gnus-registry-install
740 (gnus-registry-install-hooks)
741 (gnus-registry-read))
743 ;; TODO: a lot of things
745 (provide 'gnus-registry)
747 ;;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
748 ;;; gnus-registry.el ends here