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-install t
41 ;; gnus-registry-max-entries 2500
42 ;; gnus-registry-use-long-group-names t)
44 ;; (require 'gnus-registry)
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
72 (defvar gnus-registry-hashtb nil
73 "*The article registry by Message ID.")
75 (defcustom gnus-registry-unfollowed-groups '("delayed" "drafts" "queue")
76 "List of groups that gnus-registry-split-fancy-with-parent won't follow.
77 The group names are matched, they don't have to be fully qualified."
79 :type '(repeat string))
81 (defcustom gnus-registry-install nil
82 "Whether the registry should be installed."
86 (defcustom gnus-registry-clean-empty t
87 "Whether the empty registry entries should be deleted.
88 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 other things about a message.
99 The Subject header is currently the only thing that can be
101 :group 'gnus-registry
104 (defcustom gnus-registry-entry-caching t
105 "Whether the registry should cache extra information."
106 :group 'gnus-registry
109 (defcustom gnus-registry-minimum-subject-length 5
110 "The minimum length of a subject before it's considered trackable."
111 :group 'gnus-registry
114 (defcustom gnus-registry-trim-articles-without-groups t
115 "Whether the registry should clean out message IDs without groups."
116 :group 'gnus-registry
119 (defcustom gnus-registry-cache-file "~/.gnus.registry.eld"
120 "File where the Gnus registry will be stored."
121 :group 'gnus-registry
124 (defcustom gnus-registry-max-entries nil
125 "Maximum number of entries in the registry, nil for unlimited."
126 :group 'gnus-registry
127 :type '(radio (const :format "Unlimited " nil)
128 (integer :format "Maximum number: %v\n" :size 0)))
130 ;; Function(s) missing in Emacs 20
131 (when (memq nil (mapcar 'fboundp '(puthash)))
133 (unless (fboundp 'puthash)
134 ;; alias puthash is missing from Emacs 20 cl-extra.el
135 (defalias 'puthash 'cl-puthash)))
137 (defun gnus-registry-cache-read ()
138 "Read the registry cache file."
140 (let ((file gnus-registry-cache-file))
141 (when (file-exists-p file)
142 (gnus-message 5 "Reading %s..." file)
144 (gnus-message 5 "Reading %s...done" file))))
146 (defun gnus-registry-cache-save ()
147 "Save the registry cache file."
149 (let ((file gnus-registry-cache-file))
151 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
152 (make-local-variable 'version-control)
153 (setq version-control gnus-backup-startup-file)
154 (setq buffer-file-name file)
155 (setq default-directory (file-name-directory buffer-file-name))
156 (buffer-disable-undo)
158 (gnus-message 5 "Saving %s..." file)
159 (if gnus-save-startup-file-via-temp-buffer
160 (let ((coding-system-for-write gnus-ding-file-coding-system)
161 (standard-output (current-buffer)))
162 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
163 (gnus-registry-cache-whitespace file)
165 (let ((coding-system-for-write gnus-ding-file-coding-system)
166 (version-control gnus-backup-startup-file)
168 (working-dir (file-name-directory file))
171 ;; Generate the name of a non-existent file.
172 (while (progn (setq working-file
174 (if (and (eq system-type 'ms-dos)
175 (not (gnus-long-file-names)))
176 "%s#%d.tm#" ; MSDOS limits files to 8+3
177 (if (memq system-type '(vax-vms axp-vms))
180 working-dir (setq i (1+ i))))
181 (file-exists-p working-file)))
185 (gnus-with-output-to-file working-file
186 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
188 ;; These bindings will mislead the current buffer
189 ;; into thinking that it is visiting the startup
191 (let ((buffer-backed-up nil)
192 (buffer-file-name startup-file)
193 (file-precious-flag t)
194 (setmodes (file-modes startup-file)))
195 ;; Backup the current version of the startup file.
198 ;; Replace the existing startup file with the temp file.
199 (rename-file working-file startup-file t)
200 (set-file-modes startup-file setmodes)))
202 (delete-file working-file)
205 (gnus-kill-buffer (current-buffer))
206 (gnus-message 5 "Saving %s...done" file))))
208 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
209 ;; Save the gnus-registry file with extra line breaks.
210 (defun gnus-registry-cache-whitespace (filename)
211 (gnus-message 5 "Adding whitespace to %s" filename)
213 (goto-char (point-min))
214 (while (re-search-forward "^(\\|(\\\"" nil t)
215 (replace-match "\n\\&" t))
216 (goto-char (point-min))
217 (while (re-search-forward " $" nil t)
218 (replace-match "" t t))))
220 (defun gnus-registry-save (&optional force)
221 (when (or gnus-registry-dirty force)
222 (let ((caching gnus-registry-entry-caching))
223 ;; turn off entry caching, so mtime doesn't get recorded
224 (setq gnus-registry-entry-caching nil)
225 ;; remove entry caches
228 (if (hash-table-p value)
229 (remhash key gnus-registry-hashtb)))
230 gnus-registry-hashtb)
231 ;; remove empty entries
232 (when gnus-registry-clean-empty
233 (gnus-registry-clean-empty-function))
234 ;; now trim the registry appropriately
235 (setq gnus-registry-alist (gnus-registry-trim
236 (hashtable-to-alist gnus-registry-hashtb)))
238 (gnus-registry-cache-save)
239 (setq gnus-registry-entry-caching caching)
240 (setq gnus-registry-dirty nil))))
242 (defun gnus-registry-clean-empty-function ()
243 "Remove all empty entries from the registry. Returns count thereof."
247 (unless (gnus-registry-fetch-group key)
249 (remhash key gnus-registry-hashtb)))
250 gnus-registry-hashtb)
253 (defun gnus-registry-read ()
254 (gnus-registry-cache-read)
255 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
256 (setq gnus-registry-dirty nil))
258 (defun gnus-registry-trim (alist)
259 "Trim alist to size, using gnus-registry-max-entries."
260 (if (null gnus-registry-max-entries)
261 alist ; just return the alist
262 ;; else, when given max-entries, trim the alist
263 (let ((timehash (make-hash-table
268 (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
269 gnus-registry-hashtb)
271 ;; we use the return value of this setq, which is the trimmed alist
274 (- (length alist) gnus-registry-max-entries)
278 (cdr (gethash (car a) timehash))
279 (cdr (gethash (car b) timehash))))))))))
281 (defun alist-to-hashtable (alist)
282 "Build a hashtable from the values in ALIST."
283 (let ((ht (make-hash-table
288 (puthash (car kv-pair) (cdr kv-pair) ht))
292 (defun hashtable-to-alist (hash)
293 "Build an alist from the values in HASH."
297 (setq list (cons (cons key value) list)))
301 (defun gnus-registry-action (action data-header from &optional to method)
302 (let* ((id (mail-header-id data-header))
303 (subject (gnus-registry-simplify-subject
304 (mail-header-subject data-header)))
305 (from (gnus-group-guess-full-name from))
306 (to (if to (gnus-group-guess-full-name 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)) ; undo the delete
321 (gnus-registry-add-group id to subject)))
323 (defun gnus-registry-spool-action (id group &optional subject)
324 ;; do not process the draft IDs
325 ; (unless (string-match "totally-fudged-out-message-id" id)
326 ; (let ((group (gnus-group-guess-full-name group)))
327 (when (string-match "\r$" id)
328 (setq id (substring id 0 -1)))
329 (gnus-message 5 "Registry: article %s spooled to %s"
332 (gnus-registry-add-group id group subject))
335 ;; Function for nn{mail|imap}-split-fancy: look up all references in
336 ;; the cache and if a match is found, return that group.
337 (defun gnus-registry-split-fancy-with-parent ()
338 "Split this message into the same group as its parent. The parent
339 is obtained from the registry. This function can be used as an entry
340 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
341 this: (: gnus-registry-split-fancy-with-parent)
343 For a message to be split, it looks for the parent message in the
344 References or In-Reply-To header and then looks in the registry to
345 see which group that message was put in. This group is returned.
347 See the Info node `(gnus)Fancy Mail Splitting' for more details."
348 (let ((refstr (or (message-fetch-field "references")
349 (message-fetch-field "in-reply-to")))
350 (nnmail-split-fancy-with-parent-ignore-groups
351 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
352 nnmail-split-fancy-with-parent-ignore-groups
353 (list nnmail-split-fancy-with-parent-ignore-groups)))
357 (setq references (nreverse (gnus-split-references refstr)))
359 (setq res (or (gnus-registry-fetch-group x) res))
360 (when (or (gnus-registry-grep-in-list
362 gnus-registry-unfollowed-groups)
363 (gnus-registry-grep-in-list
365 nnmail-split-fancy-with-parent-ignore-groups))
368 ;; there were no references, now try the extra tracking
369 (when gnus-registry-track-extra
370 (let ((subject (gnus-registry-simplify-subject
371 (message-fetch-field "subject"))))
373 (< gnus-registry-minimum-subject-length (length subject)))
376 (let ((this-subject (cdr
377 (gnus-registry-fetch-extra key 'subject))))
378 (when (and this-subject
379 (equal subject this-subject))
380 (setq res (gnus-registry-fetch-group key))
382 ;; raise level of messaging if gnus-registry-track-extra
383 (if gnus-registry-track-extra 5 9)
384 "%s (extra tracking) traced subject %s to group %s"
385 "gnus-registry-split-fancy-with-parent"
387 (if res res "nil")))))
388 gnus-registry-hashtb)))))
391 "gnus-registry-split-fancy-with-parent traced %s to group %s"
392 refstr (if res res "nil"))
395 (defun gnus-registry-register-message-ids ()
396 "Register the Message-ID of every article in the group"
397 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
398 (dolist (article gnus-newsgroup-articles)
399 (let ((id (gnus-registry-fetch-message-id-fast article)))
400 (unless (gnus-registry-fetch-group id)
401 (gnus-message 9 "Registry: Registering article %d with group %s"
402 article gnus-newsgroup-name)
403 (gnus-registry-add-group
404 (gnus-registry-fetch-message-id-fast article)
406 (gnus-registry-fetch-simplified-message-subject-fast article)))))))
408 (defun gnus-registry-fetch-message-id-fast (article)
409 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
410 (if (and (numberp article)
411 (assoc article (gnus-data-list nil)))
412 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
415 (defun gnus-registry-simplify-subject (subject)
416 (if (stringp subject)
417 (gnus-simplify-subject subject)
420 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
421 "Fetch the Subject quickly, using the internal gnus-data-list function"
422 (if (and (numberp article)
423 (assoc article (gnus-data-list nil)))
424 (gnus-registry-simplify-subject
425 (mail-header-subject (gnus-data-header
426 (assoc article (gnus-data-list nil)))))
429 (defun gnus-registry-grep-in-list (word list)
435 (string-match x word))
438 (defun gnus-registry-fetch-extra (id &optional entry)
439 "Get the extra data of a message, based on the message ID.
440 Returns the first place where the trail finds a nonstring."
441 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
443 (hash-table-p entry-cache)
444 (gethash id entry-cache))
445 (gethash id entry-cache)
446 ;; else, if there is no caching possible...
447 (let ((trail (gethash id gnus-registry-hashtb)))
449 (dolist (crumb trail)
450 (unless (stringp crumb)
451 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
453 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
454 "Get the extra data of a message, or a specific entry in it.
455 Update the entry cache if needed."
457 (let ((entry-cache (gethash entry gnus-registry-hashtb))
459 (when gnus-registry-entry-caching
460 ;; create the hash table
461 (unless (hash-table-p entry-cache)
462 (setq entry-cache (make-hash-table
465 (puthash entry entry-cache gnus-registry-hashtb))
467 ;; get the entree from the hash table or from the alist
468 (setq entree (gethash id entry-cache)))
471 (setq entree (assq entry alist))
472 (when gnus-registry-entry-caching
473 (puthash id entree entry-cache)))
477 (defun gnus-registry-store-extra (id extra)
478 "Store the extra data of a message, based on the message ID.
479 The message must have at least one group name."
480 (when (gnus-registry-group-count id)
481 ;; we now know the trail has at least 1 group name, so it's not empty
482 (let ((trail (gethash id gnus-registry-hashtb))
483 (old-extra (gnus-registry-fetch-extra id)))
484 (puthash id (cons extra (delete old-extra trail))
485 gnus-registry-hashtb)
486 (setq gnus-registry-dirty t))))
488 (defun gnus-registry-store-extra-entry (id key value)
489 "Put a specific entry in the extras field of the registry entry for id."
490 (let* ((extra (gnus-registry-fetch-extra id))
491 (alist (cons (cons key value)
492 (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
493 (gnus-registry-store-extra id alist)))
495 (defun gnus-registry-fetch-group (id)
496 "Get the group of a message, based on the message ID.
497 Returns the first place where the trail finds a group name."
498 (when (gnus-registry-group-count id)
499 ;; we now know the trail has at least 1 group name
500 (let ((trail (gethash id gnus-registry-hashtb)))
501 (dolist (crumb trail)
502 (when (stringp crumb)
503 (return (gnus-group-short-name crumb)))))))
505 (defun gnus-registry-group-count (id)
506 "Get the number of groups of a message, based on the message ID."
507 (let ((trail (gethash id gnus-registry-hashtb)))
508 (if (and trail (listp trail))
509 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
512 (defun gnus-registry-delete-group (id group)
513 "Delete a group for a message, based on the message ID."
516 (let ((trail (gethash id gnus-registry-hashtb))
517 (group (gnus-group-short-name group)))
518 (puthash id (if trail
521 gnus-registry-hashtb))
522 ;; now, clear the entry if there are no more groups
523 (when gnus-registry-trim-articles-without-groups
524 (unless (gnus-registry-group-count id)
525 (gnus-registry-delete-id id)))
526 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
528 (defun gnus-registry-delete-id (id)
529 "Delete a message ID from the registry."
531 (remhash id gnus-registry-hashtb)
534 (when (hash-table-p value)
536 gnus-registry-hashtb)))
538 (defun gnus-registry-add-group (id group &optional subject)
539 "Add a group for a message, based on the message ID."
540 ;; make sure there are no duplicate entries
543 (not (string-match "totally-fudged-out-message-id" id)))
544 (let ((full-group group)
545 (group (if gnus-registry-use-long-group-names
547 (gnus-group-short-name group))))
548 (gnus-registry-delete-group id group)
549 (unless gnus-registry-use-long-group-names
550 (gnus-registry-delete-group id full-group))
551 (let ((trail (gethash id gnus-registry-hashtb)))
552 (puthash id (if trail
555 gnus-registry-hashtb)
557 (when gnus-registry-track-extra
558 (gnus-registry-store-extra-entry
561 (gnus-registry-simplify-subject subject)))
563 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
565 (defun gnus-registry-clear ()
566 "Clear the Gnus registry."
568 (setq gnus-registry-alist nil)
569 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
570 (setq gnus-registry-dirty t))
572 (defun gnus-registry-install-hooks ()
573 "Install the registry hooks."
575 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
576 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
577 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
578 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
580 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
581 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
583 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
585 (defun gnus-registry-unload-hook ()
586 "Uninstall the registry hooks."
588 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
589 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
590 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
591 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
593 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
594 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
596 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
598 (when gnus-registry-install
599 (gnus-registry-install-hooks)
600 (gnus-registry-read))
602 ;; TODO: a lot of things
604 (provide 'gnus-registry)
606 ;;; gnus-registry.el ends here