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, works with other backends
29 ;; besides nnmail. The major issue is that it doesn't go across
30 ;; backends, so for instance if an article is in nnml:sys and you see
31 ;; a reference to it in nnimap splitting, the article will end up in
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 nil
74 "*The article registry by Message ID.")
76 (defcustom gnus-registry-unfollowed-groups
'("delayed" "drafts" "queue")
77 "List of groups that gnus-registry-split-fancy-with-parent won't follow.
78 The group names are matched, they don't have to be fully qualified."
80 :type
'(repeat string
))
82 (defcustom gnus-registry-install nil
83 "Whether the registry should be installed."
87 (defcustom gnus-registry-clean-empty t
88 "Whether the empty registry entries should be deleted.
89 Registry entries are considered empty when they have no groups."
93 (defcustom gnus-registry-use-long-group-names nil
94 "Whether the registry should use long group names (BUGGY)."
98 (defcustom gnus-registry-track-extra nil
99 "Whether the registry should track extra data about a message.
100 The Subject and Sender (From:) headers are currently tracked this
102 :group
'gnus-registry
104 '(set :tag
"Tracking choices"
105 (const :tag
"Track by subject (Subject: header)" subject
)
106 (const :tag
"Track by sender (From: header)" sender
)))
108 (defcustom gnus-registry-entry-caching t
109 "Whether the registry should cache extra information."
110 :group
'gnus-registry
113 (defcustom gnus-registry-minimum-subject-length
5
114 "The minimum length of a subject before it's considered trackable."
115 :group
'gnus-registry
118 (defcustom gnus-registry-trim-articles-without-groups t
119 "Whether the registry should clean out message IDs without groups."
120 :group
'gnus-registry
123 (defcustom gnus-registry-cache-file
"~/.gnus.registry.eld"
124 "File where the Gnus registry will be stored."
125 :group
'gnus-registry
128 (defcustom gnus-registry-max-entries nil
129 "Maximum number of entries in the registry, nil for unlimited."
130 :group
'gnus-registry
131 :type
'(radio (const :format
"Unlimited " nil
)
132 (integer :format
"Maximum number: %v")))
134 ;; Function(s) missing in Emacs 20
135 (when (memq nil
(mapcar 'fboundp
'(puthash)))
137 (unless (fboundp 'puthash
)
138 ;; alias puthash is missing from Emacs 20 cl-extra.el
139 (defalias 'puthash
'cl-puthash
)))
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
)
175 (let ((coding-system-for-write gnus-ding-file-coding-system
)
176 (version-control gnus-backup-startup-file
)
178 (working-dir (file-name-directory file
))
181 ;; Generate the name of a non-existent file.
182 (while (progn (setq working-file
184 (if (and (eq system-type
'ms-dos
)
185 (not (gnus-long-file-names)))
186 "%s#%d.tm#" ; MSDOS limits files to 8+3
187 (if (memq system-type
'(vax-vms axp-vms
))
190 working-dir
(setq i
(1+ i
))))
191 (file-exists-p working-file
)))
195 (gnus-with-output-to-file working-file
196 (gnus-gnus-to-quick-newsrc-format t
"gnus registry startup file" 'gnus-registry-alist
))
198 ;; These bindings will mislead the current buffer
199 ;; into thinking that it is visiting the startup
201 (let ((buffer-backed-up nil
)
202 (buffer-file-name startup-file
)
203 (file-precious-flag t
)
204 (setmodes (file-modes startup-file
)))
205 ;; Backup the current version of the startup file.
208 ;; Replace the existing startup file with the temp file.
209 (rename-file working-file startup-file t
)
210 (set-file-modes startup-file setmodes
)))
212 (delete-file working-file
)
215 (gnus-kill-buffer (current-buffer))
216 (gnus-message 5 "Saving %s...done" file
))))
218 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
219 ;; Save the gnus-registry file with extra line breaks.
220 (defun gnus-registry-cache-whitespace (filename)
221 (gnus-message 5 "Adding whitespace to %s" filename
)
223 (goto-char (point-min))
224 (while (re-search-forward "^(\\|(\\\"" nil t
)
225 (replace-match "\n\\&" t
))
226 (goto-char (point-min))
227 (while (re-search-forward " $" nil t
)
228 (replace-match "" t t
))))
230 (defun gnus-registry-save (&optional force
)
231 (when (or gnus-registry-dirty force
)
232 (let ((caching gnus-registry-entry-caching
))
233 ;; turn off entry caching, so mtime doesn't get recorded
234 (setq gnus-registry-entry-caching nil
)
235 ;; remove entry caches
238 (if (hash-table-p value
)
239 (remhash key gnus-registry-hashtb
)))
240 gnus-registry-hashtb
)
241 ;; remove empty entries
242 (when gnus-registry-clean-empty
243 (gnus-registry-clean-empty-function))
244 ;; now trim the registry appropriately
245 (setq gnus-registry-alist
(gnus-registry-trim
246 (hashtable-to-alist gnus-registry-hashtb
)))
248 (gnus-registry-cache-save)
249 (setq gnus-registry-entry-caching caching
)
250 (setq gnus-registry-dirty nil
))))
252 (defun gnus-registry-clean-empty-function ()
253 "Remove all empty entries from the registry. Returns count thereof."
257 (unless (gnus-registry-fetch-group key
)
259 (remhash key gnus-registry-hashtb
)))
260 gnus-registry-hashtb
)
263 (defun gnus-registry-read ()
264 (gnus-registry-cache-read)
265 (setq gnus-registry-hashtb
(alist-to-hashtable gnus-registry-alist
))
266 (setq gnus-registry-dirty nil
))
268 (defun gnus-registry-trim (alist)
269 "Trim alist to size, using gnus-registry-max-entries."
270 (if (null gnus-registry-max-entries
)
271 alist
; just return the alist
272 ;; else, when given max-entries, trim the alist
273 (let* ((timehash (make-hash-table
276 (trim-length (- (length alist
) gnus-registry-max-entries
))
277 (trim-length (if (natnump trim-length
) trim-length
0)))
280 (puthash key
(gnus-registry-fetch-extra key
'mtime
) timehash
))
281 gnus-registry-hashtb
)
283 ;; we use the return value of this setq, which is the trimmed alist
290 (cdr (gethash (car a
) timehash
))
291 (cdr (gethash (car b
) timehash
))))))))))
293 (defun alist-to-hashtable (alist)
294 "Build a hashtable from the values in ALIST."
295 (let ((ht (make-hash-table
300 (puthash (car kv-pair
) (cdr kv-pair
) ht
))
304 (defun hashtable-to-alist (hash)
305 "Build an alist from the values in HASH."
309 (setq list
(cons (cons key value
) list
)))
313 (defun gnus-registry-action (action data-header from
&optional to method
)
314 (let* ((id (mail-header-id data-header
))
315 (subject (gnus-registry-simplify-subject
316 (mail-header-subject data-header
)))
317 (sender (mail-header-from data-header
))
318 (from (gnus-group-guess-full-name-from-command-method from
))
319 (to (if to
(gnus-group-guess-full-name-from-command-method to
) nil
))
320 (to-name (if to to
"the Bit Bucket"))
321 (old-entry (gethash id gnus-registry-hashtb
)))
322 (gnus-message 5 "Registry: article %s %s from %s to %s"
324 (if method
"respooling" "going")
328 ;; All except copy will need a delete
329 (gnus-registry-delete-group id from
)
331 (when (equal 'copy action
)
332 (gnus-registry-add-group id from subject sender
)) ; undo the delete
334 (gnus-registry-add-group id to subject sender
)))
336 (defun gnus-registry-spool-action (id group
&optional subject sender
)
337 (let ((group (gnus-group-guess-full-name-from-command-method group
)))
338 (when (and (stringp id
) (string-match "\r$" id
))
339 (setq id
(substring id
0 -
1)))
340 (gnus-message 5 "Registry: article %s spooled to %s"
343 (gnus-registry-add-group id group subject sender
)))
345 ;; Function for nn{mail|imap}-split-fancy: look up all references in
346 ;; the cache and if a match is found, return that group.
347 (defun gnus-registry-split-fancy-with-parent ()
348 "Split this message into the same group as its parent. The parent
349 is obtained from the registry. This function can be used as an entry
350 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
351 this: (: gnus-registry-split-fancy-with-parent)
353 For a message to be split, it looks for the parent message in the
354 References or In-Reply-To header and then looks in the registry to
355 see which group that message was put in. This group is returned.
357 See the Info node `(gnus)Fancy Mail Splitting' for more details."
358 (let ((refstr (or (message-fetch-field "references")
359 (message-fetch-field "in-reply-to")))
360 (nnmail-split-fancy-with-parent-ignore-groups
361 (if (listp nnmail-split-fancy-with-parent-ignore-groups
)
362 nnmail-split-fancy-with-parent-ignore-groups
363 (list nnmail-split-fancy-with-parent-ignore-groups
)))
367 (setq references
(nreverse (gnus-split-references refstr
)))
369 (setq res
(or (gnus-registry-fetch-group x
) res
))
370 (when (or (gnus-registry-grep-in-list
372 gnus-registry-unfollowed-groups
)
373 (gnus-registry-grep-in-list
375 nnmail-split-fancy-with-parent-ignore-groups
))
379 ;; else: there were no references, now try the extra tracking
380 (let ((sender (message-fetch-field "from"))
381 (subject (gnus-registry-simplify-subject
382 (message-fetch-field "subject")))
384 (when (and single-match
385 (gnus-registry-track-sender-p)
389 (let ((this-sender (cdr
390 (gnus-registry-fetch-extra key
'sender
))))
391 (when (and single-match
393 (equal sender this-sender
))
394 ;; too many matches, bail
395 (unless (equal res
(gnus-registry-fetch-group key
))
396 (setq single-match nil
))
397 (setq res
(gnus-registry-fetch-group key
))
399 ;; raise level of messaging if gnus-registry-track-extra
400 (if gnus-registry-track-extra
5 9)
401 "%s (extra tracking) traced sender %s to group %s"
402 "gnus-registry-split-fancy-with-parent"
404 (if res res
"nil")))))
405 gnus-registry-hashtb
))
406 (when (and single-match
407 (gnus-registry-track-subject-p)
409 (< gnus-registry-minimum-subject-length
(length subject
)))
412 (let ((this-subject (cdr
413 (gnus-registry-fetch-extra key
'subject
))))
414 (when (and single-match
416 (equal subject this-subject
))
417 ;; too many matches, bail
418 (unless (equal res
(gnus-registry-fetch-group key
))
419 (setq single-match nil
))
420 (setq res
(gnus-registry-fetch-group key
))
422 ;; raise level of messaging if gnus-registry-track-extra
423 (if gnus-registry-track-extra
5 9)
424 "%s (extra tracking) traced subject %s to group %s"
425 "gnus-registry-split-fancy-with-parent"
427 (if res res
"nil")))))
428 gnus-registry-hashtb
))
432 "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
437 "gnus-registry-split-fancy-with-parent traced %s to group %s"
438 refstr
(if res res
"nil"))
440 (when (and res gnus-registry-use-long-group-names
)
441 (let ((m1 (gnus-find-method-for-group res
))
442 (m2 (or gnus-command-method
443 (gnus-find-method-for-group gnus-newsgroup-name
)))
444 (short-res (gnus-group-short-name res
)))
445 (if (gnus-methods-equal-p m1 m2
)
449 "gnus-registry-split-fancy-with-parent stripped group %s to %s"
452 (setq res short-res
))
456 "gnus-registry-split-fancy-with-parent ignored foreign group %s"
461 (defun gnus-registry-register-message-ids ()
462 "Register the Message-ID of every article in the group"
463 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name
)
464 (dolist (article gnus-newsgroup-articles
)
465 (let ((id (gnus-registry-fetch-message-id-fast article
)))
466 (unless (gnus-registry-fetch-group id
)
467 (gnus-message 9 "Registry: Registering article %d with group %s"
468 article gnus-newsgroup-name
)
469 (gnus-registry-add-group
470 (gnus-registry-fetch-message-id-fast article
)
472 (gnus-registry-fetch-simplified-message-subject-fast article
)
473 (gnus-registry-fetch-sender-fast article
)))))))
475 (defun gnus-registry-fetch-message-id-fast (article)
476 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
477 (if (and (numberp article
)
478 (assoc article
(gnus-data-list nil
)))
479 (mail-header-id (gnus-data-header (assoc article
(gnus-data-list nil
))))
482 (defun gnus-registry-simplify-subject (subject)
483 (if (stringp subject
)
484 (gnus-simplify-subject subject
)
487 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
488 "Fetch the Subject quickly, using the internal gnus-data-list function"
489 (if (and (numberp article
)
490 (assoc article
(gnus-data-list nil
)))
491 (gnus-registry-simplify-subject
492 (mail-header-subject (gnus-data-header
493 (assoc article
(gnus-data-list nil
)))))
496 (defun gnus-registry-fetch-sender-fast (article)
497 "Fetch the Sender quickly, using the internal gnus-data-list function"
498 (if (and (numberp article
)
499 (assoc article
(gnus-data-list nil
)))
500 (mail-header-from (gnus-data-header
501 (assoc article
(gnus-data-list nil
))))
504 (defun gnus-registry-grep-in-list (word list
)
510 (string-match x word
))
513 (defun gnus-registry-fetch-extra (id &optional entry
)
514 "Get the extra data of a message, based on the message ID.
515 Returns the first place where the trail finds a nonstring."
516 (let ((entry-cache (gethash entry gnus-registry-hashtb
)))
518 (hash-table-p entry-cache
)
519 (gethash id entry-cache
))
520 (gethash id entry-cache
)
521 ;; else, if there is no caching possible...
522 (let ((trail (gethash id gnus-registry-hashtb
)))
524 (dolist (crumb trail
)
525 (unless (stringp crumb
)
526 (return (gnus-registry-fetch-extra-entry crumb entry id
)))))))))
528 (defun gnus-registry-fetch-extra-entry (alist &optional entry id
)
529 "Get the extra data of a message, or a specific entry in it.
530 Update the entry cache if needed."
532 (let ((entry-cache (gethash entry gnus-registry-hashtb
))
534 (when gnus-registry-entry-caching
535 ;; create the hash table
536 (unless (hash-table-p entry-cache
)
537 (setq entry-cache
(make-hash-table
540 (puthash entry entry-cache gnus-registry-hashtb
))
542 ;; get the entree from the hash table or from the alist
543 (setq entree
(gethash id entry-cache
)))
546 (setq entree
(assq entry alist
))
547 (when gnus-registry-entry-caching
548 (puthash id entree entry-cache
)))
552 (defun gnus-registry-store-extra (id extra
)
553 "Store the extra data of a message, based on the message ID.
554 The message must have at least one group name."
555 (when (gnus-registry-group-count id
)
556 ;; we now know the trail has at least 1 group name, so it's not empty
557 (let ((trail (gethash id gnus-registry-hashtb
))
558 (old-extra (gnus-registry-fetch-extra id
))
560 (dolist (crumb trail
)
561 (unless (stringp crumb
)
562 (dolist (entry crumb
)
563 (setq entry-cache
(gethash (car entry
) gnus-registry-hashtb
))
565 (remhash id entry-cache
))))
566 (puthash id
(cons extra
(delete old-extra trail
))
567 gnus-registry-hashtb
)
568 (setq gnus-registry-dirty t
)))))
570 (defun gnus-registry-store-extra-entry (id key value
)
571 "Put a specific entry in the extras field of the registry entry for id."
572 (let* ((extra (gnus-registry-fetch-extra id
))
573 (alist (cons (cons key value
)
574 (gnus-assq-delete-all key
(gnus-registry-fetch-extra id
)))))
575 (gnus-registry-store-extra id alist
)))
577 (defun gnus-registry-fetch-group (id)
578 "Get the group of a message, based on the message ID.
579 Returns the first place where the trail finds a group name."
580 (when (gnus-registry-group-count id
)
581 ;; we now know the trail has at least 1 group name
582 (let ((trail (gethash id gnus-registry-hashtb
)))
583 (dolist (crumb trail
)
584 (when (stringp crumb
)
585 (return (if gnus-registry-use-long-group-names
587 (gnus-group-short-name crumb
))))))))
589 (defun gnus-registry-group-count (id)
590 "Get the number of groups of a message, based on the message ID."
591 (let ((trail (gethash id gnus-registry-hashtb
)))
592 (if (and trail
(listp trail
))
593 (apply '+ (mapcar (lambda (x) (if (stringp x
) 1 0)) trail
))
596 (defun gnus-registry-delete-group (id group
)
597 "Delete a group for a message, based on the message ID."
600 (let ((trail (gethash id gnus-registry-hashtb
))
601 (group (gnus-group-short-name group
)))
602 (puthash id
(if trail
605 gnus-registry-hashtb
))
606 ;; now, clear the entry if there are no more groups
607 (when gnus-registry-trim-articles-without-groups
608 (unless (gnus-registry-group-count id
)
609 (gnus-registry-delete-id id
)))
610 ;; is this ID still in the registry?
611 (when (gethash id gnus-registry-hashtb
)
612 (gnus-registry-store-extra-entry id
'mtime
(current-time))))))
614 (defun gnus-registry-delete-id (id)
615 "Delete a message ID from the registry."
617 (remhash id gnus-registry-hashtb
)
620 (when (hash-table-p value
)
622 gnus-registry-hashtb
)))
624 (defun gnus-registry-add-group (id group
&optional subject sender
)
625 "Add a group for a message, based on the message ID."
628 (not (string-match "totally-fudged-out-message-id" id
)))
629 (let ((full-group group
)
630 (group (if gnus-registry-use-long-group-names
632 (gnus-group-short-name group
))))
633 (gnus-registry-delete-group id group
)
635 (unless gnus-registry-use-long-group-names
;; unnecessary in this case
636 (gnus-registry-delete-group id full-group
))
638 (let ((trail (gethash id gnus-registry-hashtb
)))
639 (puthash id
(if trail
642 gnus-registry-hashtb
)
644 (when (and (gnus-registry-track-subject-p)
646 (gnus-registry-store-extra-entry
649 (gnus-registry-simplify-subject subject
)))
650 (when (and (gnus-registry-track-sender-p)
652 (gnus-registry-store-extra-entry
657 (gnus-registry-store-extra-entry id
'mtime
(current-time)))))))
659 (defun gnus-registry-clear ()
660 "Clear the Gnus registry."
662 (setq gnus-registry-alist nil
)
663 (setq gnus-registry-hashtb
(alist-to-hashtable gnus-registry-alist
))
664 (setq gnus-registry-dirty t
))
667 (defun gnus-registry-initialize ()
669 (setq gnus-registry-install t
)
670 (gnus-registry-install-hooks)
671 (gnus-registry-read))
674 (defun gnus-registry-install-hooks ()
675 "Install the registry hooks."
677 (add-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
678 (add-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
679 (add-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
680 (add-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
682 (add-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
683 (add-hook 'gnus-read-newsrc-el-hook
'gnus-registry-read
)
685 (add-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
687 (defun gnus-registry-unload-hook ()
688 "Uninstall the registry hooks."
690 (remove-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
691 (remove-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
692 (remove-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
693 (remove-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
695 (remove-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
696 (remove-hook 'gnus-read-newsrc-el-hook
'gnus-registry-read
)
698 (remove-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
700 (add-hook 'gnus-registry-unload-hook
'gnus-registry-unload-hook
)
702 (when gnus-registry-install
703 (gnus-registry-install-hooks)
704 (gnus-registry-read))
706 ;; TODO: a lot of things
708 (provide 'gnus-registry
)
710 ;;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
711 ;;; gnus-registry.el ends here