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 and Sender (From:) headers are currently tracked this
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 (sender (mail-header-from data-header))
305 (from (gnus-group-guess-full-name-from-command-method from))
306 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
307 (to-name (if to to "the Bit Bucket"))
308 (old-entry (gethash id gnus-registry-hashtb)))
309 (gnus-message 5 "Registry: article %s %s from %s to %s"
311 (if method "respooling" "going")
315 ;; All except copy will need a delete
316 (gnus-registry-delete-group id from)
318 (when (equal 'copy action)
319 (gnus-registry-add-group id from subject sender)) ; undo the delete
321 (gnus-registry-add-group id to subject sender)))
323 (defun gnus-registry-spool-action (id group &optional subject sender)
324 (let ((group (gnus-group-guess-full-name-from-command-method group)))
325 (when (and (stringp id) (string-match "\r$" id))
326 (setq id (substring id 0 -1)))
327 (gnus-message 5 "Registry: article %s spooled to %s"
330 (gnus-registry-add-group id group subject sender)))
332 ;; Function for nn{mail|imap}-split-fancy: look up all references in
333 ;; the cache and if a match is found, return that group.
334 (defun gnus-registry-split-fancy-with-parent ()
335 "Split this message into the same group as its parent. The parent
336 is obtained from the registry. This function can be used as an entry
337 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
338 this: (: gnus-registry-split-fancy-with-parent)
340 For a message to be split, it looks for the parent message in the
341 References or In-Reply-To header and then looks in the registry to
342 see which group that message was put in. This group is returned.
344 See the Info node `(gnus)Fancy Mail Splitting' for more details."
345 (let ((refstr (or (message-fetch-field "references")
346 (message-fetch-field "in-reply-to")))
347 (nnmail-split-fancy-with-parent-ignore-groups
348 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
349 nnmail-split-fancy-with-parent-ignore-groups
350 (list nnmail-split-fancy-with-parent-ignore-groups)))
354 (setq references (nreverse (gnus-split-references refstr)))
356 (setq res (or (gnus-registry-fetch-group x) res))
357 (when (or (gnus-registry-grep-in-list
359 gnus-registry-unfollowed-groups)
360 (gnus-registry-grep-in-list
362 nnmail-split-fancy-with-parent-ignore-groups))
365 ;; there were no references, now try the extra tracking
366 (when gnus-registry-track-extra
367 (let ((sender (message-fetch-field "from"))
368 (subject (gnus-registry-simplify-subject
369 (message-fetch-field "subject"))))
371 (< gnus-registry-minimum-subject-length (length subject)))
374 (let ((this-subject (cdr
375 (gnus-registry-fetch-extra key 'subject))))
376 (when (and this-subject
377 (equal subject this-subject))
378 (setq res (gnus-registry-fetch-group key))
380 ;; raise level of messaging if gnus-registry-track-extra
381 (if gnus-registry-track-extra 5 9)
382 "%s (extra tracking) traced subject %s to group %s"
383 "gnus-registry-split-fancy-with-parent"
385 (if res res "nil")))))
386 gnus-registry-hashtb))
390 (let ((this-sender (cdr
391 (gnus-registry-fetch-extra key 'sender))))
392 (when (and this-sender
393 (equal sender this-sender))
394 (setq res (gnus-registry-fetch-group key))
396 ;; raise level of messaging if gnus-registry-track-extra
397 (if gnus-registry-track-extra 5 9)
398 "%s (extra tracking) traced sender %s to group %s"
399 "gnus-registry-split-fancy-with-parent"
401 (if res res "nil")))))
402 gnus-registry-hashtb)))))
405 "gnus-registry-split-fancy-with-parent traced %s to group %s"
406 refstr (if res res "nil"))
408 (when (and res gnus-registry-use-long-group-names)
409 (let ((m1 (gnus-find-method-for-group res))
410 (m2 (or gnus-command-method
411 (gnus-find-method-for-group gnus-newsgroup-name)))
412 (short-res (gnus-group-short-name res)))
413 (if (gnus-methods-equal-p m1 m2)
417 "gnus-registry-split-fancy-with-parent stripped group %s to %s"
420 (setq res short-res))
424 "gnus-registry-split-fancy-with-parent ignored foreign group %s"
429 (defun gnus-registry-register-message-ids ()
430 "Register the Message-ID of every article in the group"
431 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
432 (dolist (article gnus-newsgroup-articles)
433 (let ((id (gnus-registry-fetch-message-id-fast article)))
434 (unless (gnus-registry-fetch-group id)
435 (gnus-message 9 "Registry: Registering article %d with group %s"
436 article gnus-newsgroup-name)
437 (gnus-registry-add-group
438 (gnus-registry-fetch-message-id-fast article)
440 (gnus-registry-fetch-simplified-message-subject-fast article)
441 (gnus-registry-fetch-sender-fast article)))))))
443 (defun gnus-registry-fetch-message-id-fast (article)
444 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
445 (if (and (numberp article)
446 (assoc article (gnus-data-list nil)))
447 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
450 (defun gnus-registry-simplify-subject (subject)
451 (if (stringp subject)
452 (gnus-simplify-subject subject)
455 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
456 "Fetch the Subject quickly, using the internal gnus-data-list function"
457 (if (and (numberp article)
458 (assoc article (gnus-data-list nil)))
459 (gnus-registry-simplify-subject
460 (mail-header-subject (gnus-data-header
461 (assoc article (gnus-data-list nil)))))
464 (defun gnus-registry-fetch-sender-fast (article)
465 "Fetch the Sender quickly, using the internal gnus-data-list function"
466 (if (and (numberp article)
467 (assoc article (gnus-data-list nil)))
468 (mail-header-from (gnus-data-header
469 (assoc article (gnus-data-list nil))))
472 (defun gnus-registry-grep-in-list (word list)
478 (string-match x word))
481 (defun gnus-registry-fetch-extra (id &optional entry)
482 "Get the extra data of a message, based on the message ID.
483 Returns the first place where the trail finds a nonstring."
484 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
486 (hash-table-p entry-cache)
487 (gethash id entry-cache))
488 (gethash id entry-cache)
489 ;; else, if there is no caching possible...
490 (let ((trail (gethash id gnus-registry-hashtb)))
492 (dolist (crumb trail)
493 (unless (stringp crumb)
494 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
496 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
497 "Get the extra data of a message, or a specific entry in it.
498 Update the entry cache if needed."
500 (let ((entry-cache (gethash entry gnus-registry-hashtb))
502 (when gnus-registry-entry-caching
503 ;; create the hash table
504 (unless (hash-table-p entry-cache)
505 (setq entry-cache (make-hash-table
508 (puthash entry entry-cache gnus-registry-hashtb))
510 ;; get the entree from the hash table or from the alist
511 (setq entree (gethash id entry-cache)))
514 (setq entree (assq entry alist))
515 (when gnus-registry-entry-caching
516 (puthash id entree entry-cache)))
520 (defun gnus-registry-store-extra (id extra)
521 "Store the extra data of a message, based on the message ID.
522 The message must have at least one group name."
523 (when (gnus-registry-group-count id)
524 ;; we now know the trail has at least 1 group name, so it's not empty
525 (let ((trail (gethash id gnus-registry-hashtb))
526 (old-extra (gnus-registry-fetch-extra id))
528 (dolist (crumb trail)
529 (unless (stringp crumb)
530 (dolist (entry crumb)
531 (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
533 (remhash id entry-cache))))
534 (puthash id (cons extra (delete old-extra trail))
535 gnus-registry-hashtb)
536 (setq gnus-registry-dirty t)))))
538 (defun gnus-registry-store-extra-entry (id key value)
539 "Put a specific entry in the extras field of the registry entry for id."
540 (let* ((extra (gnus-registry-fetch-extra id))
541 (alist (cons (cons key value)
542 (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
543 (gnus-registry-store-extra id alist)))
545 (defun gnus-registry-fetch-group (id)
546 "Get the group of a message, based on the message ID.
547 Returns the first place where the trail finds a group name."
548 (when (gnus-registry-group-count id)
549 ;; we now know the trail has at least 1 group name
550 (let ((trail (gethash id gnus-registry-hashtb)))
551 (dolist (crumb trail)
552 (when (stringp crumb)
553 (return (if gnus-registry-use-long-group-names
555 (gnus-group-short-name crumb))))))))
557 (defun gnus-registry-group-count (id)
558 "Get the number of groups of a message, based on the message ID."
559 (let ((trail (gethash id gnus-registry-hashtb)))
560 (if (and trail (listp trail))
561 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
564 (defun gnus-registry-delete-group (id group)
565 "Delete a group for a message, based on the message ID."
568 (let ((trail (gethash id gnus-registry-hashtb))
569 (group (gnus-group-short-name group)))
570 (puthash id (if trail
573 gnus-registry-hashtb))
574 ;; now, clear the entry if there are no more groups
575 (when gnus-registry-trim-articles-without-groups
576 (unless (gnus-registry-group-count id)
577 (gnus-registry-delete-id id)))
578 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
580 (defun gnus-registry-delete-id (id)
581 "Delete a message ID from the registry."
583 (remhash id gnus-registry-hashtb)
586 (when (hash-table-p value)
588 gnus-registry-hashtb)))
590 (defun gnus-registry-add-group (id group &optional subject sender)
591 "Add a group for a message, based on the message ID."
594 (not (string-match "totally-fudged-out-message-id" id)))
595 (let ((full-group group)
596 (group (if gnus-registry-use-long-group-names
598 (gnus-group-short-name group))))
599 (gnus-registry-delete-group id group)
601 (unless gnus-registry-use-long-group-names ;; unnecessary in this case
602 (gnus-registry-delete-group id full-group))
604 (let ((trail (gethash id gnus-registry-hashtb)))
605 (puthash id (if trail
608 gnus-registry-hashtb)
610 (when gnus-registry-track-extra
612 (gnus-registry-store-extra-entry
615 (gnus-registry-simplify-subject subject)))
617 (gnus-registry-store-extra-entry
622 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
624 (defun gnus-registry-clear ()
625 "Clear the Gnus registry."
627 (setq gnus-registry-alist nil)
628 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
629 (setq gnus-registry-dirty t))
632 (defun gnus-registry-initialize ()
634 (setq gnus-registry-install t)
635 (gnus-registry-install-hooks)
636 (gnus-registry-read))
639 (defun gnus-registry-install-hooks ()
640 "Install the registry hooks."
642 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
643 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
644 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
645 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
647 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
648 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
650 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
652 (defun gnus-registry-unload-hook ()
653 "Uninstall the registry hooks."
655 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
656 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
657 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
658 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
660 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
661 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
663 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
665 (when gnus-registry-install
666 (gnus-registry-install-hooks)
667 (gnus-registry-read))
669 ;; TODO: a lot of things
671 (provide 'gnus-registry)
673 ;;; gnus-registry.el ends here