Revision: miles@gnu.org--gnu-2004/gnus--devo--0--patch-182
[gnus] / lisp / gnus-registry.el
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.
4
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
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
32
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).
37
38 ;; Put this in your startup file (~/.gnus.el for instance)
39
40 ;; (setq gnus-registry-max-entries 2500
41 ;;       gnus-registry-use-long-group-names t)
42
43 ;; (gnus-registry-initialize)
44
45 ;; Then use this in your fancy-split:
46
47 ;; (: gnus-registry-split-fancy-with-parent)
48
49 ;; TODO:
50
51 ;; - get the correct group on spool actions
52
53 ;; - articles that are spooled to a different backend should be handled
54
55 ;;; Code:
56
57 (eval-when-compile (require 'cl))
58
59 (require 'gnus)
60 (require 'gnus-int)
61 (require 'gnus-sum)
62 (require 'nnmail)
63
64 (defvar gnus-registry-dirty t
65  "Boolean set to t when the registry is modified")
66
67 (defgroup gnus-registry nil
68   "The Gnus registry."
69   :version "21.4"
70   :group 'gnus)
71
72 (defvar gnus-registry-hashtb (make-hash-table                       
73                               :size 256
74                               :test 'equal)
75   "*The article registry by Message ID.")
76
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."
80   :group 'gnus-registry
81   :type '(repeat string))
82
83 (defcustom gnus-registry-install nil
84   "Whether the registry should be installed."
85   :group 'gnus-registry
86   :type 'boolean)
87
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
91 and no extra data."
92   :group 'gnus-registry
93   :type 'boolean)
94
95 (defcustom gnus-registry-use-long-group-names nil
96   "Whether the registry should use long group names (BUGGY)."
97   :group 'gnus-registry
98   :type 'boolean)
99
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
103 way."
104   :group 'gnus-registry
105   :type      
106   '(set :tag "Tracking choices"
107     (const :tag "Track by subject (Subject: header)" subject)
108     (const :tag "Track by sender (From: header)"  sender)))
109
110 (defcustom gnus-registry-entry-caching t
111   "Whether the registry should cache extra information."
112   :group 'gnus-registry
113   :type 'boolean)
114
115 (defcustom gnus-registry-minimum-subject-length 5
116   "The minimum length of a subject before it's considered trackable."
117   :group 'gnus-registry
118   :type 'integer)
119
120 (defcustom gnus-registry-trim-articles-without-groups t
121   "Whether the registry should clean out message IDs without groups."
122   :group 'gnus-registry
123   :type 'boolean)
124
125 (defcustom gnus-registry-cache-file 
126   (nnheader-concat 
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
131   :type 'file)
132
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")))
138
139 ;; Function(s) missing in Emacs 20
140 (when (memq nil (mapcar 'fboundp '(puthash)))
141   (require 'cl)
142   (unless (fboundp 'puthash)
143     ;; alias puthash is missing from Emacs 20 cl-extra.el
144     (defalias 'puthash 'cl-puthash)))
145
146 (defun gnus-registry-track-subject-p ()
147   (memq 'subject gnus-registry-track-extra))
148
149 (defun gnus-registry-track-sender-p ()
150   (memq 'sender gnus-registry-track-extra))
151
152 (defun gnus-registry-cache-read ()
153   "Read the registry cache file."
154   (interactive)
155   (let ((file gnus-registry-cache-file))
156     (when (file-exists-p file)
157       (gnus-message 5 "Reading %s..." file)
158       (gnus-load file)
159       (gnus-message 5 "Reading %s...done" file))))
160
161 (defun gnus-registry-cache-save ()
162   "Save the registry cache file."
163   (interactive)
164   (let ((file gnus-registry-cache-file))
165     (save-excursion
166       (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
167       (make-local-variable 'version-control)
168     (setq version-control gnus-backup-startup-file)
169     (setq buffer-file-name file)
170     (setq default-directory (file-name-directory buffer-file-name))
171     (buffer-disable-undo)
172     (erase-buffer)
173     (gnus-message 5 "Saving %s..." file)
174     (if gnus-save-startup-file-via-temp-buffer
175         (let ((coding-system-for-write gnus-ding-file-coding-system)
176               (standard-output (current-buffer)))
177           (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
178           (gnus-registry-cache-whitespace file)
179           (save-buffer))
180       (let ((coding-system-for-write gnus-ding-file-coding-system)
181             (version-control gnus-backup-startup-file)
182             (startup-file file)
183             (working-dir (file-name-directory file))
184             working-file
185             (i -1))
186         ;; Generate the name of a non-existent file.
187         (while (progn (setq working-file
188                             (format
189                              (if (and (eq system-type 'ms-dos)
190                                       (not (gnus-long-file-names)))
191                                  "%s#%d.tm#" ; MSDOS limits files to 8+3
192                                (if (memq system-type '(vax-vms axp-vms))
193                                    "%s$tmp$%d"
194                                  "%s#tmp#%d"))
195                              working-dir (setq i (1+ i))))
196                       (file-exists-p working-file)))
197         
198         (unwind-protect
199             (progn
200               (gnus-with-output-to-file working-file
201                 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
202               
203               ;; These bindings will mislead the current buffer
204               ;; into thinking that it is visiting the startup
205               ;; file.
206               (let ((buffer-backed-up nil)
207                     (buffer-file-name startup-file)
208                     (file-precious-flag t)
209                     (setmodes (file-modes startup-file)))
210                 ;; Backup the current version of the startup file.
211                 (backup-buffer)
212                 
213                 ;; Replace the existing startup file with the temp file.
214                 (rename-file working-file startup-file t)
215                 (gnus-set-file-modes startup-file setmodes)))
216           (condition-case nil
217               (delete-file working-file)
218             (file-error nil)))))
219     
220     (gnus-kill-buffer (current-buffer))
221     (gnus-message 5 "Saving %s...done" file))))
222
223 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
224 ;; Save the gnus-registry file with extra line breaks.
225 (defun gnus-registry-cache-whitespace (filename)
226   (gnus-message 7 "Adding whitespace to %s" filename)
227   (save-excursion
228     (goto-char (point-min))
229     (while (re-search-forward "^(\\|(\\\"" nil t)
230       (replace-match "\n\\&" t))
231     (goto-char (point-min))
232     (while (re-search-forward " $" nil t)
233       (replace-match "" t t))))
234
235 (defun gnus-registry-save (&optional force)
236   (when (or gnus-registry-dirty force)
237     (let ((caching gnus-registry-entry-caching))
238       ;; turn off entry caching, so mtime doesn't get recorded
239       (setq gnus-registry-entry-caching nil)
240       ;; remove entry caches
241       (maphash
242        (lambda (key value)
243          (if (hash-table-p value)
244              (remhash key gnus-registry-hashtb)))
245        gnus-registry-hashtb)
246       ;; remove empty entries
247       (when gnus-registry-clean-empty 
248         (gnus-registry-clean-empty-function))
249       ;; now trim the registry appropriately
250       (setq gnus-registry-alist (gnus-registry-trim 
251                                  (hashtable-to-alist gnus-registry-hashtb)))
252       ;; really save
253       (gnus-registry-cache-save)
254       (setq gnus-registry-entry-caching caching)
255       (setq gnus-registry-dirty nil))))
256
257 (defun gnus-registry-clean-empty-function ()
258   "Remove all empty entries from the registry.  Returns count thereof."
259   (let ((count 0))
260     (maphash
261      (lambda (key value)
262        (unless (or
263                 (gnus-registry-fetch-group key)
264                 ;; TODO: look for specific extra data here!
265                 ;; in this example, we look for 'label
266                 (gnus-registry-fetch-extra key 'label)) 
267          (incf count)
268          (remhash key gnus-registry-hashtb)))
269      gnus-registry-hashtb)
270     count))
271
272 (defun gnus-registry-read ()
273   (gnus-registry-cache-read)
274   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
275   (setq gnus-registry-dirty nil))
276
277 (defun gnus-registry-trim (alist)
278   "Trim alist to size, using gnus-registry-max-entries."
279   (if (null gnus-registry-max-entries)
280       alist                             ; just return the alist
281     ;; else, when given max-entries, trim the alist
282     (let* ((timehash (make-hash-table
283                       :size 4096
284                       :test 'equal))
285            (trim-length (- (length alist) gnus-registry-max-entries))
286            (trim-length (if (natnump trim-length) trim-length 0)))
287       (maphash
288        (lambda (key value)
289          (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
290        gnus-registry-hashtb)
291
292       ;; we use the return value of this setq, which is the trimmed alist
293       (setq alist
294             (nthcdr
295              trim-length
296              (sort alist 
297                    (lambda (a b)
298                      (time-less-p 
299                       (cdr (gethash (car a) timehash))
300                       (cdr (gethash (car b) timehash))))))))))
301
302 (defun alist-to-hashtable (alist)
303   "Build a hashtable from the values in ALIST."
304   (let ((ht (make-hash-table                        
305              :size 4096
306              :test 'equal)))
307     (mapc
308      (lambda (kv-pair)
309        (puthash (car kv-pair) (cdr kv-pair) ht))
310      alist)
311      ht))
312
313 (defun hashtable-to-alist (hash)
314   "Build an alist from the values in HASH."
315   (let ((list nil))
316     (maphash
317      (lambda (key value)
318        (setq list (cons (cons key value) list)))
319      hash)
320     list))
321
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"
332                   id
333                   (if method "respooling" "going")
334                   from
335                   to)
336
337     ;; All except copy will need a delete
338     (gnus-registry-delete-group id from)
339
340     (when (equal 'copy action) 
341       (gnus-registry-add-group id from subject sender)) ; undo the delete
342
343     (gnus-registry-add-group id to subject sender)))
344
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"
350                   id
351                   group)
352     (gnus-registry-add-group id group subject sender)))
353
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) 
361
362 This function tracks ALL backends, unlike
363 `nnmail-split-fancy-with-parent' which tracks only nnmail
364 messages.
365
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.
369
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
374          (refstr (if reply-to 
375                      (concat refstr " " reply-to)
376                    refstr))
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)))
381         references res)
382     ;; the references string must be valid and parse to valid references
383     (if (and refstr (gnus-extract-references refstr))
384         (progn
385           (setq references (nreverse (gnus-extract-references refstr)))
386           (mapcar (lambda (x)
387                     (setq res (or (gnus-registry-fetch-group x) res))
388                     (when (or (gnus-registry-grep-in-list
389                                res
390                                gnus-registry-unfollowed-groups)
391                               (gnus-registry-grep-in-list 
392                                res
393                                nnmail-split-fancy-with-parent-ignore-groups))
394                       (setq res nil)))
395                   references))
396
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")))
401             (single-match t))
402         (when (and single-match
403                    (gnus-registry-track-sender-p)
404                    sender)
405           (maphash
406            (lambda (key value)
407              (let ((this-sender (cdr
408                                  (gnus-registry-fetch-extra key 'sender))))
409                (when (and single-match
410                           this-sender
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)
417                    (gnus-message
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"
422                     sender
423                     res)))))
424            gnus-registry-hashtb))
425         (when (and single-match
426                    (gnus-registry-track-subject-p)
427                    subject
428                    (< gnus-registry-minimum-subject-length (length subject)))
429           (maphash
430            (lambda (key value)
431              (let ((this-subject (cdr 
432                                   (gnus-registry-fetch-extra key 'subject))))
433                (when (and single-match
434                           this-subject
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)
441                    (gnus-message
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"
446                     subject
447                     res)))))
448            gnus-registry-hashtb))
449         (unless single-match
450           (gnus-message
451            3
452            "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
453            refstr)
454           (setq res nil))))
455     (when (and refstr res)
456       (gnus-message
457        5
458        "gnus-registry-split-fancy-with-parent traced %s to group %s"
459        refstr res))
460
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)
467           (progn
468             (gnus-message
469              9 
470              "gnus-registry-split-fancy-with-parent stripped group %s to %s"
471              res
472              short-res)
473             (setq res short-res))
474         ;; else...
475         (gnus-message
476          7
477          "gnus-registry-split-fancy-with-parent ignored foreign group %s"
478          res)
479         (setq res nil))))
480     res))
481
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)
492            gnus-newsgroup-name
493            (gnus-registry-fetch-simplified-message-subject-fast article)
494            (gnus-registry-fetch-sender-fast article)))))))
495
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))))
501     nil))
502
503 (defun gnus-registry-simplify-subject (subject)
504   (if (stringp subject)
505       (gnus-simplify-subject subject)
506     nil))
507
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)))))
515     nil))
516
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))))
523     nil))
524
525 (defun gnus-registry-grep-in-list (word list)
526   (when word
527     (memq nil
528           (mapcar 'not
529                   (mapcar 
530                    (lambda (x)
531                      (string-match x word))
532                    list)))))
533
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)))
538     (if (and entry
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)))
544         (when (listp trail)
545           (dolist (crumb trail)
546             (unless (stringp crumb)
547               (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
548
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."
552   (if (and entry id)
553       (let ((entry-cache (gethash entry gnus-registry-hashtb))
554             entree)
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
559                                :size 4096
560                                :test 'equal))
561             (puthash entry entry-cache gnus-registry-hashtb))
562
563           ;; get the entree from the hash table or from the alist
564           (setq entree (gethash id entry-cache)))
565         
566         (unless entree
567           (setq entree (assq entry alist))
568           (when gnus-registry-entry-caching
569             (puthash id entree entry-cache)))
570         entree)
571     alist))
572
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))
580           entry-cache)
581       (dolist (crumb trail)
582         (unless (stringp crumb)
583           (dolist (entry crumb)
584             (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
585           (when entry-cache
586             (remhash id entry-cache))))
587       (puthash id (cons extra (delete old-extra trail))
588                gnus-registry-hashtb)
589       (setq gnus-registry-dirty t)))))
590
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)))
597
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 
607                        crumb 
608                      (gnus-group-short-name crumb))))))))
609
610 (defun gnus-registry-group-count (id)
611   "Get the number of groups of a message, based on the message ID."
612   (let ((trail (gethash id gnus-registry-hashtb)))
613     (if (and trail (listp trail))
614         (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
615       0)))
616
617 (defun gnus-registry-delete-group (id group)
618   "Delete a group for a message, based on the message ID."
619   (when group
620     (when id
621       (let ((trail (gethash id gnus-registry-hashtb))
622             (group (gnus-group-short-name group)))
623         (puthash id (if trail
624                         (delete group trail)
625                       nil)
626                  gnus-registry-hashtb))
627       ;; now, clear the entry if there are no more groups
628       (when gnus-registry-trim-articles-without-groups
629         (unless (gnus-registry-group-count id)
630           (gnus-registry-delete-id id)))
631       (gnus-registry-store-extra-entry id 'mtime (current-time)))))
632
633 (defun gnus-registry-delete-id (id)
634   "Delete a message ID from the registry."
635   (when (stringp id)
636     (remhash id gnus-registry-hashtb)
637     (maphash
638      (lambda (key value)
639        (when (hash-table-p value)
640          (remhash id value)))
641      gnus-registry-hashtb)))
642
643 (defun gnus-registry-add-group (id group &optional subject sender)
644   "Add a group for a message, based on the message ID."
645   (when group
646     (when (and id
647                (not (string-match "totally-fudged-out-message-id" id)))
648       (let ((full-group group)
649             (group (if gnus-registry-use-long-group-names 
650                        group 
651                      (gnus-group-short-name group))))
652         (gnus-registry-delete-group id group)
653
654         (unless gnus-registry-use-long-group-names ;; unnecessary in this case
655           (gnus-registry-delete-group id full-group))
656
657         (let ((trail (gethash id gnus-registry-hashtb)))
658           (puthash id (if trail
659                           (cons group trail)
660                         (list group))
661                    gnus-registry-hashtb)
662
663           (when (and (gnus-registry-track-subject-p)
664                      subject)
665             (gnus-registry-store-extra-entry
666              id 
667              'subject 
668              (gnus-registry-simplify-subject subject)))
669           (when (and (gnus-registry-track-sender-p)
670                      sender)
671             (gnus-registry-store-extra-entry
672              id 
673              'sender
674              sender))
675           
676           (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
677
678 (defun gnus-registry-clear ()
679   "Clear the Gnus registry."
680   (interactive)
681   (setq gnus-registry-alist nil)
682   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
683   (setq gnus-registry-dirty t))
684
685 ;;;###autoload
686 (defun gnus-registry-initialize ()
687   (interactive)
688   (setq gnus-registry-install t)
689   (gnus-registry-install-hooks)
690   (gnus-registry-read))
691
692 ;;;###autoload
693 (defun gnus-registry-install-hooks ()
694   "Install the registry hooks."
695   (interactive)
696   (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action) 
697   (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
698   (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
699   (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
700   
701   (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
702   (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
703
704   (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
705
706 (defun gnus-registry-unload-hook ()
707   "Uninstall the registry hooks."
708   (interactive)
709   (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action) 
710   (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
711   (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
712   (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
713   
714   (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
715   (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
716
717   (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
718
719 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
720
721 (when gnus-registry-install
722   (gnus-registry-install-hooks)
723   (gnus-registry-read))
724
725 ;; TODO: a lot of things
726
727 (provide 'gnus-registry)
728
729 ;;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
730 ;;; gnus-registry.el ends here