Add arch taglines
[gnus] / lisp / gnus-registry.el
1 ;;; gnus-registry.el --- article registry for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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   :group 'gnus)
70
71 (defvar gnus-registry-hashtb nil
72   "*The article registry by Message ID.")
73
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."
77   :group 'gnus-registry
78   :type '(repeat string))
79
80 (defcustom gnus-registry-install nil
81   "Whether the registry should be installed."
82   :group 'gnus-registry
83   :type 'boolean)
84
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
88 and no extra data."
89   :group 'gnus-registry
90   :type 'boolean)
91
92 (defcustom gnus-registry-use-long-group-names nil
93   "Whether the registry should use long group names (BUGGY)."
94   :group 'gnus-registry
95   :type 'boolean)
96
97 (defcustom gnus-registry-track-extra nil
98   "Whether the registry should track extra data about a message.
99 The Subject and Sender (From:) headers are currently tracked this
100 way."
101   :group 'gnus-registry
102   :type      
103   '(set :tag "Tracking choices"
104     (const :tag "Track by subject (Subject: header)" subject)
105     (const :tag "Track by sender (From: header)"  sender)))
106
107 (defcustom gnus-registry-entry-caching t
108   "Whether the registry should cache extra information."
109   :group 'gnus-registry
110   :type 'boolean)
111
112 (defcustom gnus-registry-minimum-subject-length 5
113   "The minimum length of a subject before it's considered trackable."
114   :group 'gnus-registry
115   :type 'integer)
116
117 (defcustom gnus-registry-trim-articles-without-groups t
118   "Whether the registry should clean out message IDs without groups."
119   :group 'gnus-registry
120   :type 'boolean)
121
122 (defcustom gnus-registry-cache-file 
123   (nnheader-concat 
124    (or gnus-dribble-directory gnus-home-directory "~/") 
125    ".gnus.registry.eld")
126   "File where the Gnus registry will be stored."
127   :group 'gnus-registry
128   :type 'file)
129
130 (defcustom gnus-registry-max-entries nil
131   "Maximum number of entries in the registry, nil for unlimited."
132   :group 'gnus-registry
133   :type '(radio (const :format "Unlimited " nil)
134                 (integer :format "Maximum number: %v\n" :size 0)))
135
136 (defun gnus-registry-track-subject-p ()
137   (memq 'subject gnus-registry-track-extra))
138
139 (defun gnus-registry-track-sender-p ()
140   (memq 'sender gnus-registry-track-extra))
141
142 (defun gnus-registry-cache-read ()
143   "Read the registry cache file."
144   (interactive)
145   (let ((file gnus-registry-cache-file))
146     (when (file-exists-p file)
147       (gnus-message 5 "Reading %s..." file)
148       (gnus-load file)
149       (gnus-message 5 "Reading %s...done" file))))
150
151 (defun gnus-registry-cache-save ()
152   "Save the registry cache file."
153   (interactive)
154   (let ((file gnus-registry-cache-file))
155     (save-excursion
156       (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
157       (make-local-variable 'version-control)
158     (setq version-control gnus-backup-startup-file)
159     (setq buffer-file-name file)
160     (setq default-directory (file-name-directory buffer-file-name))
161     (buffer-disable-undo)
162     (erase-buffer)
163     (gnus-message 5 "Saving %s..." file)
164     (if gnus-save-startup-file-via-temp-buffer
165         (let ((coding-system-for-write gnus-ding-file-coding-system)
166               (standard-output (current-buffer)))
167           (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
168           (gnus-registry-cache-whitespace file)
169           (save-buffer))
170       (let ((coding-system-for-write gnus-ding-file-coding-system)
171             (version-control gnus-backup-startup-file)
172             (startup-file file)
173             (working-dir (file-name-directory file))
174             working-file
175             (i -1))
176         ;; Generate the name of a non-existent file.
177         (while (progn (setq working-file
178                             (format
179                              (if (and (eq system-type 'ms-dos)
180                                       (not (gnus-long-file-names)))
181                                  "%s#%d.tm#" ; MSDOS limits files to 8+3
182                                (if (memq system-type '(vax-vms axp-vms))
183                                    "%s$tmp$%d"
184                                  "%s#tmp#%d"))
185                              working-dir (setq i (1+ i))))
186                       (file-exists-p working-file)))
187         
188         (unwind-protect
189             (progn
190               (gnus-with-output-to-file working-file
191                 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
192               
193               ;; These bindings will mislead the current buffer
194               ;; into thinking that it is visiting the startup
195               ;; file.
196               (let ((buffer-backed-up nil)
197                     (buffer-file-name startup-file)
198                     (file-precious-flag t)
199                     (setmodes (file-modes startup-file)))
200                 ;; Backup the current version of the startup file.
201                 (backup-buffer)
202                 
203                 ;; Replace the existing startup file with the temp file.
204                 (rename-file working-file startup-file t)
205                 (set-file-modes startup-file setmodes)))
206           (condition-case nil
207               (delete-file working-file)
208             (file-error nil)))))
209     
210     (gnus-kill-buffer (current-buffer))
211     (gnus-message 5 "Saving %s...done" file))))
212
213 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
214 ;; Save the gnus-registry file with extra line breaks.
215 (defun gnus-registry-cache-whitespace (filename)
216   (gnus-message 7 "Adding whitespace to %s" filename)
217   (save-excursion
218     (goto-char (point-min))
219     (while (re-search-forward "^(\\|(\\\"" nil t)
220       (replace-match "\n\\&" t))
221     (goto-char (point-min))
222     (while (re-search-forward " $" nil t)
223       (replace-match "" t t))))
224
225 (defun gnus-registry-save (&optional force)
226   (when (or gnus-registry-dirty force)
227     (let ((caching gnus-registry-entry-caching))
228       ;; turn off entry caching, so mtime doesn't get recorded
229       (setq gnus-registry-entry-caching nil)
230       ;; remove entry caches
231       (maphash
232        (lambda (key value)
233          (if (hash-table-p value)
234              (remhash key gnus-registry-hashtb)))
235        gnus-registry-hashtb)
236       ;; remove empty entries
237       (when gnus-registry-clean-empty 
238         (gnus-registry-clean-empty-function))
239       ;; now trim the registry appropriately
240       (setq gnus-registry-alist (gnus-registry-trim 
241                                  (hashtable-to-alist gnus-registry-hashtb)))
242       ;; really save
243       (gnus-registry-cache-save)
244       (setq gnus-registry-entry-caching caching)
245       (setq gnus-registry-dirty nil))))
246
247 (defun gnus-registry-clean-empty-function ()
248   "Remove all empty entries from the registry.  Returns count thereof."
249   (let ((count 0))
250     (maphash
251      (lambda (key value)
252        (unless (or
253                 (gnus-registry-fetch-group key)
254                 ;; TODO: look for specific extra data here!
255                 ;; in this example, we look for 'label
256                 (gnus-registry-fetch-extra key 'label)) 
257          (incf count)
258          (remhash key gnus-registry-hashtb)))
259      gnus-registry-hashtb)
260     count))
261
262 (defun gnus-registry-read ()
263   (gnus-registry-cache-read)
264   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
265   (setq gnus-registry-dirty nil))
266
267 (defun gnus-registry-trim (alist)
268   "Trim alist to size, using gnus-registry-max-entries."
269   (if (null gnus-registry-max-entries)
270       alist                             ; just return the alist
271     ;; else, when given max-entries, trim the alist
272     (let ((timehash (make-hash-table                        
273                      :size 4096
274                      :test 'equal)))
275       (maphash
276        (lambda (key value)
277          (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
278        gnus-registry-hashtb)
279
280       ;; we use the return value of this setq, which is the trimmed alist
281       (setq alist
282             (nthcdr
283              (- (length alist) gnus-registry-max-entries)
284              (sort alist 
285                    (lambda (a b)
286                      (time-less-p 
287                       (cdr (gethash (car a) timehash))
288                       (cdr (gethash (car b) timehash))))))))))
289
290 (defun alist-to-hashtable (alist)
291   "Build a hashtable from the values in ALIST."
292   (let ((ht (make-hash-table                        
293              :size 4096
294              :test 'equal)))
295     (mapc
296      (lambda (kv-pair)
297        (puthash (car kv-pair) (cdr kv-pair) ht))
298      alist)
299      ht))
300
301 (defun hashtable-to-alist (hash)
302   "Build an alist from the values in HASH."
303   (let ((list nil))
304     (maphash
305      (lambda (key value)
306        (setq list (cons (cons key value) list)))
307      hash)
308     list))
309
310 (defun gnus-registry-action (action data-header from &optional to method)
311   (let* ((id (mail-header-id data-header))
312          (subject (gnus-registry-simplify-subject 
313                    (mail-header-subject data-header)))
314          (sender (mail-header-from data-header))
315          (from (gnus-group-guess-full-name-from-command-method from))
316          (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
317          (to-name (if to to "the Bit Bucket"))
318          (old-entry (gethash id gnus-registry-hashtb)))
319     (gnus-message 7 "Registry: article %s %s from %s to %s"
320                   id
321                   (if method "respooling" "going")
322                   from
323                   to)
324
325     ;; All except copy will need a delete
326     (gnus-registry-delete-group id from)
327
328     (when (equal 'copy action) 
329       (gnus-registry-add-group id from subject sender)) ; undo the delete
330
331     (gnus-registry-add-group id to subject sender)))
332
333 (defun gnus-registry-spool-action (id group &optional subject sender)
334   (let ((group (gnus-group-guess-full-name-from-command-method group)))
335     (when (and (stringp id) (string-match "\r$" id))
336       (setq id (substring id 0 -1)))
337     (gnus-message 7 "Registry: article %s spooled to %s"
338                   id
339                   group)
340     (gnus-registry-add-group id group subject sender)))
341
342 ;; Function for nn{mail|imap}-split-fancy: look up all references in
343 ;; the cache and if a match is found, return that group.
344 (defun gnus-registry-split-fancy-with-parent ()
345   "Split this message into the same group as its parent.  The parent
346 is obtained from the registry.  This function can be used as an entry
347 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
348 this: (: gnus-registry-split-fancy-with-parent) 
349
350 This function tracks ALL backends, unlike
351 `nnmail-split-fancy-with-parent' which tracks only nnmail
352 messages.
353
354 For a message to be split, it looks for the parent message in the
355 References or In-Reply-To header and then looks in the registry to
356 see which group that message was put in.  This group is returned.
357
358 See the Info node `(gnus)Fancy Mail Splitting' for more details."
359   (let ((refstr (or (message-fetch-field "references")
360                     (message-fetch-field "in-reply-to")))
361         (nnmail-split-fancy-with-parent-ignore-groups
362          (if (listp nnmail-split-fancy-with-parent-ignore-groups)
363              nnmail-split-fancy-with-parent-ignore-groups
364            (list nnmail-split-fancy-with-parent-ignore-groups)))
365         references res)
366     (if refstr
367         (progn
368           (setq references (nreverse (gnus-split-references refstr)))
369           (mapcar (lambda (x)
370                     (setq res (or (gnus-registry-fetch-group x) res))
371                     (when (or (gnus-registry-grep-in-list
372                                res
373                                gnus-registry-unfollowed-groups)
374                               (gnus-registry-grep-in-list 
375                                res
376                                nnmail-split-fancy-with-parent-ignore-groups))
377                       (setq res nil)))
378                   references))
379
380       ;; else: there were no references, now try the extra tracking
381       (let ((sender (message-fetch-field "from"))
382             (subject (gnus-registry-simplify-subject
383                       (message-fetch-field "subject")))
384             (single-match t))
385         (when (and single-match
386                    (gnus-registry-track-sender-p)
387                    sender)
388           (maphash
389            (lambda (key value)
390              (let ((this-sender (cdr 
391                                  (gnus-registry-fetch-extra key 'sender))))
392                (when (and single-match
393                           this-sender
394                           (equal sender this-sender))
395                  ;; too many matches, bail
396                  (unless (equal res (gnus-registry-fetch-group key))
397                    (setq single-match nil))
398                  (setq res (gnus-registry-fetch-group key))
399                  (when (and sender res)
400                    (gnus-message
401                     ;; raise level of messaging if gnus-registry-track-extra
402                     (if gnus-registry-track-extra 7 9)
403                     "%s (extra tracking) traced sender %s to group %s"
404                     "gnus-registry-split-fancy-with-parent"
405                     sender
406                     res)))))
407            gnus-registry-hashtb))
408         (when (and single-match
409                    (gnus-registry-track-subject-p)
410                    subject
411                    (< gnus-registry-minimum-subject-length (length subject)))
412           (maphash
413            (lambda (key value)
414              (let ((this-subject (cdr 
415                                   (gnus-registry-fetch-extra key 'subject))))
416                (when (and single-match
417                           this-subject
418                           (equal subject this-subject))
419                  ;; too many matches, bail
420                  (unless (equal res (gnus-registry-fetch-group key))
421                    (setq single-match nil))
422                  (setq res (gnus-registry-fetch-group key))
423                  (when (and subject res)
424                    (gnus-message
425                     ;; raise level of messaging if gnus-registry-track-extra
426                     (if gnus-registry-track-extra 7 9)
427                     "%s (extra tracking) traced subject %s to group %s"
428                     "gnus-registry-split-fancy-with-parent"
429                     subject
430                     res)))))
431            gnus-registry-hashtb))
432         (unless single-match
433           (gnus-message
434            3
435            "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
436            refstr)
437           (setq res nil))))
438     (when (and refstr res)
439       (gnus-message
440        5
441        "gnus-registry-split-fancy-with-parent traced %s to group %s"
442        refstr res))
443
444     (when (and res gnus-registry-use-long-group-names)
445       (let ((m1 (gnus-find-method-for-group res))
446             (m2 (or gnus-command-method 
447                     (gnus-find-method-for-group gnus-newsgroup-name)))
448             (short-res (gnus-group-short-name res)))
449       (if (gnus-methods-equal-p m1 m2)
450           (progn
451             (gnus-message
452              9 
453              "gnus-registry-split-fancy-with-parent stripped group %s to %s"
454              res
455              short-res)
456             (setq res short-res))
457         ;; else...
458         (gnus-message
459          7
460          "gnus-registry-split-fancy-with-parent ignored foreign group %s"
461          res)
462         (setq res nil))))
463     res))
464
465 (defun gnus-registry-register-message-ids ()
466   "Register the Message-ID of every article in the group"
467   (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
468     (dolist (article gnus-newsgroup-articles)
469       (let ((id (gnus-registry-fetch-message-id-fast article)))
470         (unless (gnus-registry-fetch-group id)
471           (gnus-message 9 "Registry: Registering article %d with group %s" 
472                         article gnus-newsgroup-name)
473           (gnus-registry-add-group 
474            (gnus-registry-fetch-message-id-fast article)
475            gnus-newsgroup-name
476            (gnus-registry-fetch-simplified-message-subject-fast article)
477            (gnus-registry-fetch-sender-fast article)))))))
478
479 (defun gnus-registry-fetch-message-id-fast (article)
480   "Fetch the Message-ID quickly, using the internal gnus-data-list function"
481   (if (and (numberp article)
482            (assoc article (gnus-data-list nil)))
483       (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
484     nil))
485
486 (defun gnus-registry-simplify-subject (subject)
487   (if (stringp subject)
488       (gnus-simplify-subject subject)
489     nil))
490
491 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
492   "Fetch the Subject quickly, using the internal gnus-data-list function"
493   (if (and (numberp article)
494            (assoc article (gnus-data-list nil)))
495       (gnus-registry-simplify-subject
496        (mail-header-subject (gnus-data-header
497                              (assoc article (gnus-data-list nil)))))
498     nil))
499
500 (defun gnus-registry-fetch-sender-fast (article)
501   "Fetch the Sender quickly, using the internal gnus-data-list function"
502   (if (and (numberp article)
503            (assoc article (gnus-data-list nil)))
504       (mail-header-from (gnus-data-header
505                          (assoc article (gnus-data-list nil))))
506     nil))
507
508 (defun gnus-registry-grep-in-list (word list)
509   (when word
510     (memq nil
511           (mapcar 'not
512                   (mapcar 
513                    (lambda (x)
514                      (string-match x word))
515                    list)))))
516
517 (defun gnus-registry-fetch-extra (id &optional entry)
518   "Get the extra data of a message, based on the message ID.
519 Returns the first place where the trail finds a nonstring."
520   (let ((entry-cache (gethash entry gnus-registry-hashtb)))
521     (if (and entry
522              (hash-table-p entry-cache)
523              (gethash id entry-cache))
524         (gethash id entry-cache)
525       ;; else, if there is no caching possible...
526       (let ((trail (gethash id gnus-registry-hashtb)))
527         (when (listp trail)
528           (dolist (crumb trail)
529             (unless (stringp crumb)
530               (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
531
532 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
533   "Get the extra data of a message, or a specific entry in it.
534 Update the entry cache if needed."
535   (if (and entry id)
536       (let ((entry-cache (gethash entry gnus-registry-hashtb))
537             entree)
538         (when gnus-registry-entry-caching
539           ;; create the hash table
540           (unless (hash-table-p entry-cache)
541             (setq entry-cache (make-hash-table
542                                :size 4096
543                                :test 'equal))
544             (puthash entry entry-cache gnus-registry-hashtb))
545
546           ;; get the entree from the hash table or from the alist
547           (setq entree (gethash id entry-cache)))
548         
549         (unless entree
550           (setq entree (assq entry alist))
551           (when gnus-registry-entry-caching
552             (puthash id entree entry-cache)))
553         entree)
554     alist))
555
556 (defun gnus-registry-store-extra (id extra)
557   "Store the extra data of a message, based on the message ID.
558 The message must have at least one group name."
559   (when (gnus-registry-group-count id)
560     ;; we now know the trail has at least 1 group name, so it's not empty
561     (let ((trail (gethash id gnus-registry-hashtb))
562           (old-extra (gnus-registry-fetch-extra id))
563           entry-cache)
564       (dolist (crumb trail)
565         (unless (stringp crumb)
566           (dolist (entry crumb)
567             (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
568           (when entry-cache
569             (remhash id entry-cache))))
570       (puthash id (cons extra (delete old-extra trail))
571                gnus-registry-hashtb)
572       (setq gnus-registry-dirty t)))))
573
574 (defun gnus-registry-store-extra-entry (id key value)
575   "Put a specific entry in the extras field of the registry entry for id."
576   (let* ((extra (gnus-registry-fetch-extra id))
577          (alist (cons (cons key value)
578                  (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
579     (gnus-registry-store-extra id alist)))
580
581 (defun gnus-registry-fetch-group (id)
582   "Get the group of a message, based on the message ID.
583 Returns the first place where the trail finds a group name."
584   (when (gnus-registry-group-count id)
585     ;; we now know the trail has at least 1 group name
586     (let ((trail (gethash id gnus-registry-hashtb)))
587       (dolist (crumb trail)
588         (when (stringp crumb)
589           (return (if gnus-registry-use-long-group-names 
590                        crumb 
591                      (gnus-group-short-name crumb))))))))
592
593 (defun gnus-registry-group-count (id)
594   "Get the number of groups of a message, based on the message ID."
595   (let ((trail (gethash id gnus-registry-hashtb)))
596     (if (and trail (listp trail))
597         (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
598       0)))
599
600 (defun gnus-registry-delete-group (id group)
601   "Delete a group for a message, based on the message ID."
602   (when group
603     (when id
604       (let ((trail (gethash id gnus-registry-hashtb))
605             (group (gnus-group-short-name group)))
606         (puthash id (if trail
607                         (delete group trail)
608                       nil)
609                  gnus-registry-hashtb))
610       ;; now, clear the entry if there are no more groups
611       (when gnus-registry-trim-articles-without-groups
612         (unless (gnus-registry-group-count id)
613           (gnus-registry-delete-id id)))
614       (gnus-registry-store-extra-entry id 'mtime (current-time)))))
615
616 (defun gnus-registry-delete-id (id)
617   "Delete a message ID from the registry."
618   (when (stringp id)
619     (remhash id gnus-registry-hashtb)
620     (maphash
621      (lambda (key value)
622        (when (hash-table-p value)
623          (remhash id value)))
624      gnus-registry-hashtb)))
625
626 (defun gnus-registry-add-group (id group &optional subject sender)
627   "Add a group for a message, based on the message ID."
628   (when group
629     (when (and id
630                (not (string-match "totally-fudged-out-message-id" id)))
631       (let ((full-group group)
632             (group (if gnus-registry-use-long-group-names 
633                        group 
634                      (gnus-group-short-name group))))
635         (gnus-registry-delete-group id group)
636
637         (unless gnus-registry-use-long-group-names ;; unnecessary in this case
638           (gnus-registry-delete-group id full-group))
639
640         (let ((trail (gethash id gnus-registry-hashtb)))
641           (puthash id (if trail
642                           (cons group trail)
643                         (list group))
644                    gnus-registry-hashtb)
645
646           (when (and (gnus-registry-track-subject-p)
647                      subject)
648             (gnus-registry-store-extra-entry
649              id 
650              'subject 
651              (gnus-registry-simplify-subject subject)))
652           (when (and (gnus-registry-track-sender-p)
653                      sender)
654             (gnus-registry-store-extra-entry
655              id 
656              'sender
657              sender))
658           
659           (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
660
661 (defun gnus-registry-clear ()
662   "Clear the Gnus registry."
663   (interactive)
664   (setq gnus-registry-alist nil)
665   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
666   (setq gnus-registry-dirty t))
667
668 ;;;###autoload
669 (defun gnus-registry-initialize ()
670   (interactive)
671   (setq gnus-registry-install t)
672   (gnus-registry-install-hooks)
673   (gnus-registry-read))
674
675 ;;;###autoload
676 (defun gnus-registry-install-hooks ()
677   "Install the registry hooks."
678   (interactive)
679   (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action) 
680   (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
681   (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
682   (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
683   
684   (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
685   (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
686
687   (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
688
689 (defun gnus-registry-unload-hook ()
690   "Uninstall the registry hooks."
691   (interactive)
692   (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action) 
693   (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
694   (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
695   (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
696   
697   (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
698   (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
699
700   (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
701
702 (when gnus-registry-install
703   (gnus-registry-install-hooks)
704   (gnus-registry-read))
705
706 ;; TODO: a lot of things
707
708 (provide 'gnus-registry)
709
710 ;;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
711 ;;; gnus-registry.el ends here