bc951128e50bc4a89c7159b1adadda21eda6f3b0
[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                 (gnus-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            (trim-length (- (length alist) gnus-registry-max-entries))
276            (trim-length (if (natnump trim-length) trim-length 0)))
277       (maphash
278        (lambda (key value)
279          (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
280        gnus-registry-hashtb)
281
282       ;; we use the return value of this setq, which is the trimmed alist
283       (setq alist
284             (nthcdr
285              trim-length
286              (sort alist 
287                    (lambda (a b)
288                      (time-less-p 
289                       (cdr (gethash (car a) timehash))
290                       (cdr (gethash (car b) timehash))))))))))
291
292 (defun alist-to-hashtable (alist)
293   "Build a hashtable from the values in ALIST."
294   (let ((ht (make-hash-table                        
295              :size 4096
296              :test 'equal)))
297     (mapc
298      (lambda (kv-pair)
299        (puthash (car kv-pair) (cdr kv-pair) ht))
300      alist)
301      ht))
302
303 (defun hashtable-to-alist (hash)
304   "Build an alist from the values in HASH."
305   (let ((list nil))
306     (maphash
307      (lambda (key value)
308        (setq list (cons (cons key value) list)))
309      hash)
310     list))
311
312 (defun gnus-registry-action (action data-header from &optional to method)
313   (let* ((id (mail-header-id data-header))
314          (subject (gnus-registry-simplify-subject 
315                    (mail-header-subject data-header)))
316          (sender (mail-header-from data-header))
317          (from (gnus-group-guess-full-name-from-command-method from))
318          (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
319          (to-name (if to to "the Bit Bucket"))
320          (old-entry (gethash id gnus-registry-hashtb)))
321     (gnus-message 7 "Registry: article %s %s from %s to %s"
322                   id
323                   (if method "respooling" "going")
324                   from
325                   to)
326
327     ;; All except copy will need a delete
328     (gnus-registry-delete-group id from)
329
330     (when (equal 'copy action) 
331       (gnus-registry-add-group id from subject sender)) ; undo the delete
332
333     (gnus-registry-add-group id to subject sender)))
334
335 (defun gnus-registry-spool-action (id group &optional subject sender)
336   (let ((group (gnus-group-guess-full-name-from-command-method group)))
337     (when (and (stringp id) (string-match "\r$" id))
338       (setq id (substring id 0 -1)))
339     (gnus-message 7 "Registry: article %s spooled to %s"
340                   id
341                   group)
342     (gnus-registry-add-group id group subject sender)))
343
344 ;; Function for nn{mail|imap}-split-fancy: look up all references in
345 ;; the cache and if a match is found, return that group.
346 (defun gnus-registry-split-fancy-with-parent ()
347   "Split this message into the same group as its parent.  The parent
348 is obtained from the registry.  This function can be used as an entry
349 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
350 this: (: gnus-registry-split-fancy-with-parent) 
351
352 This function tracks ALL backends, unlike
353 `nnmail-split-fancy-with-parent' which tracks only nnmail
354 messages.
355
356 For a message to be split, it looks for the parent message in the
357 References or In-Reply-To header and then looks in the registry to
358 see which group that message was put in.  This group is returned.
359
360 See the Info node `(gnus)Fancy Mail Splitting' for more details."
361   (let* ((refstr (or (message-fetch-field "references") "")) ; guarantee string
362          (reply-to (message-fetch-field "in-reply-to"))      ; grab reply-to
363          ;; now, if reply-to is valid, append it to the References
364          (refstr (if reply-to 
365                      (concat refstr " " reply-to)
366                    refstr))
367         (nnmail-split-fancy-with-parent-ignore-groups
368          (if (listp nnmail-split-fancy-with-parent-ignore-groups)
369              nnmail-split-fancy-with-parent-ignore-groups
370            (list nnmail-split-fancy-with-parent-ignore-groups)))
371         references res)
372     (if refstr
373         (progn
374           (setq references (nreverse (gnus-extract-references refstr)))
375           (mapcar (lambda (x)
376                     (setq res (or (gnus-registry-fetch-group x) res))
377                     (when (or (gnus-registry-grep-in-list
378                                res
379                                gnus-registry-unfollowed-groups)
380                               (gnus-registry-grep-in-list 
381                                res
382                                nnmail-split-fancy-with-parent-ignore-groups))
383                       (setq res nil)))
384                   references))
385
386       ;; else: there were no references, now try the extra tracking
387       (let ((sender (message-fetch-field "from"))
388             (subject (gnus-registry-simplify-subject
389                       (message-fetch-field "subject")))
390             (single-match t))
391         (when (and single-match
392                    (gnus-registry-track-sender-p)
393                    sender)
394           (maphash
395            (lambda (key value)
396              (let ((this-sender (cdr
397                                  (gnus-registry-fetch-extra key 'sender))))
398                (when (and single-match
399                           this-sender
400                           (equal sender this-sender))
401                  ;; too many matches, bail
402                  (unless (equal res (gnus-registry-fetch-group key))
403                    (setq single-match nil))
404                  (setq res (gnus-registry-fetch-group key))
405                  (when (and sender res)
406                    (gnus-message
407                     ;; raise level of messaging if gnus-registry-track-extra
408                     (if gnus-registry-track-extra 7 9)
409                     "%s (extra tracking) traced sender %s to group %s"
410                     "gnus-registry-split-fancy-with-parent"
411                     sender
412                     res)))))
413            gnus-registry-hashtb))
414         (when (and single-match
415                    (gnus-registry-track-subject-p)
416                    subject
417                    (< gnus-registry-minimum-subject-length (length subject)))
418           (maphash
419            (lambda (key value)
420              (let ((this-subject (cdr 
421                                   (gnus-registry-fetch-extra key 'subject))))
422                (when (and single-match
423                           this-subject
424                           (equal subject this-subject))
425                  ;; too many matches, bail
426                  (unless (equal res (gnus-registry-fetch-group key))
427                    (setq single-match nil))
428                  (setq res (gnus-registry-fetch-group key))
429                  (when (and subject res)
430                    (gnus-message
431                     ;; raise level of messaging if gnus-registry-track-extra
432                     (if gnus-registry-track-extra 7 9)
433                     "%s (extra tracking) traced subject %s to group %s"
434                     "gnus-registry-split-fancy-with-parent"
435                     subject
436                     res)))))
437            gnus-registry-hashtb))
438         (unless single-match
439           (gnus-message
440            3
441            "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
442            refstr)
443           (setq res nil))))
444     (when (and refstr res)
445       (gnus-message
446        5
447        "gnus-registry-split-fancy-with-parent traced %s to group %s"
448        refstr res))
449
450     (when (and res gnus-registry-use-long-group-names)
451       (let ((m1 (gnus-find-method-for-group res))
452             (m2 (or gnus-command-method 
453                     (gnus-find-method-for-group gnus-newsgroup-name)))
454             (short-res (gnus-group-short-name res)))
455       (if (gnus-methods-equal-p m1 m2)
456           (progn
457             (gnus-message
458              9 
459              "gnus-registry-split-fancy-with-parent stripped group %s to %s"
460              res
461              short-res)
462             (setq res short-res))
463         ;; else...
464         (gnus-message
465          7
466          "gnus-registry-split-fancy-with-parent ignored foreign group %s"
467          res)
468         (setq res nil))))
469     res))
470
471 (defun gnus-registry-register-message-ids ()
472   "Register the Message-ID of every article in the group"
473   (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
474     (dolist (article gnus-newsgroup-articles)
475       (let ((id (gnus-registry-fetch-message-id-fast article)))
476         (unless (gnus-registry-fetch-group id)
477           (gnus-message 9 "Registry: Registering article %d with group %s" 
478                         article gnus-newsgroup-name)
479           (gnus-registry-add-group 
480            (gnus-registry-fetch-message-id-fast article)
481            gnus-newsgroup-name
482            (gnus-registry-fetch-simplified-message-subject-fast article)
483            (gnus-registry-fetch-sender-fast article)))))))
484
485 (defun gnus-registry-fetch-message-id-fast (article)
486   "Fetch the Message-ID quickly, using the internal gnus-data-list function"
487   (if (and (numberp article)
488            (assoc article (gnus-data-list nil)))
489       (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
490     nil))
491
492 (defun gnus-registry-simplify-subject (subject)
493   (if (stringp subject)
494       (gnus-simplify-subject subject)
495     nil))
496
497 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
498   "Fetch the Subject quickly, using the internal gnus-data-list function"
499   (if (and (numberp article)
500            (assoc article (gnus-data-list nil)))
501       (gnus-registry-simplify-subject
502        (mail-header-subject (gnus-data-header
503                              (assoc article (gnus-data-list nil)))))
504     nil))
505
506 (defun gnus-registry-fetch-sender-fast (article)
507   "Fetch the Sender quickly, using the internal gnus-data-list function"
508   (if (and (numberp article)
509            (assoc article (gnus-data-list nil)))
510       (mail-header-from (gnus-data-header
511                          (assoc article (gnus-data-list nil))))
512     nil))
513
514 (defun gnus-registry-grep-in-list (word list)
515   (when word
516     (memq nil
517           (mapcar 'not
518                   (mapcar 
519                    (lambda (x)
520                      (string-match x word))
521                    list)))))
522
523 (defun gnus-registry-fetch-extra (id &optional entry)
524   "Get the extra data of a message, based on the message ID.
525 Returns the first place where the trail finds a nonstring."
526   (let ((entry-cache (gethash entry gnus-registry-hashtb)))
527     (if (and entry
528              (hash-table-p entry-cache)
529              (gethash id entry-cache))
530         (gethash id entry-cache)
531       ;; else, if there is no caching possible...
532       (let ((trail (gethash id gnus-registry-hashtb)))
533         (when (listp trail)
534           (dolist (crumb trail)
535             (unless (stringp crumb)
536               (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
537
538 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
539   "Get the extra data of a message, or a specific entry in it.
540 Update the entry cache if needed."
541   (if (and entry id)
542       (let ((entry-cache (gethash entry gnus-registry-hashtb))
543             entree)
544         (when gnus-registry-entry-caching
545           ;; create the hash table
546           (unless (hash-table-p entry-cache)
547             (setq entry-cache (make-hash-table
548                                :size 4096
549                                :test 'equal))
550             (puthash entry entry-cache gnus-registry-hashtb))
551
552           ;; get the entree from the hash table or from the alist
553           (setq entree (gethash id entry-cache)))
554         
555         (unless entree
556           (setq entree (assq entry alist))
557           (when gnus-registry-entry-caching
558             (puthash id entree entry-cache)))
559         entree)
560     alist))
561
562 (defun gnus-registry-store-extra (id extra)
563   "Store the extra data of a message, based on the message ID.
564 The message must have at least one group name."
565   (when (gnus-registry-group-count id)
566     ;; we now know the trail has at least 1 group name, so it's not empty
567     (let ((trail (gethash id gnus-registry-hashtb))
568           (old-extra (gnus-registry-fetch-extra id))
569           entry-cache)
570       (dolist (crumb trail)
571         (unless (stringp crumb)
572           (dolist (entry crumb)
573             (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
574           (when entry-cache
575             (remhash id entry-cache))))
576       (puthash id (cons extra (delete old-extra trail))
577                gnus-registry-hashtb)
578       (setq gnus-registry-dirty t)))))
579
580 (defun gnus-registry-store-extra-entry (id key value)
581   "Put a specific entry in the extras field of the registry entry for id."
582   (let* ((extra (gnus-registry-fetch-extra id))
583          (alist (cons (cons key value)
584                  (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
585     (gnus-registry-store-extra id alist)))
586
587 (defun gnus-registry-fetch-group (id)
588   "Get the group of a message, based on the message ID.
589 Returns the first place where the trail finds a group name."
590   (when (gnus-registry-group-count id)
591     ;; we now know the trail has at least 1 group name
592     (let ((trail (gethash id gnus-registry-hashtb)))
593       (dolist (crumb trail)
594         (when (stringp crumb)
595           (return (if gnus-registry-use-long-group-names 
596                        crumb 
597                      (gnus-group-short-name crumb))))))))
598
599 (defun gnus-registry-group-count (id)
600   "Get the number of groups of a message, based on the message ID."
601   (let ((trail (gethash id gnus-registry-hashtb)))
602     (if (and trail (listp trail))
603         (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
604       0)))
605
606 (defun gnus-registry-delete-group (id group)
607   "Delete a group for a message, based on the message ID."
608   (when group
609     (when id
610       (let ((trail (gethash id gnus-registry-hashtb))
611             (group (gnus-group-short-name group)))
612         (puthash id (if trail
613                         (delete group trail)
614                       nil)
615                  gnus-registry-hashtb))
616       ;; now, clear the entry if there are no more groups
617       (when gnus-registry-trim-articles-without-groups
618         (unless (gnus-registry-group-count id)
619           (gnus-registry-delete-id id)))
620       (gnus-registry-store-extra-entry id 'mtime (current-time)))))
621
622 (defun gnus-registry-delete-id (id)
623   "Delete a message ID from the registry."
624   (when (stringp id)
625     (remhash id gnus-registry-hashtb)
626     (maphash
627      (lambda (key value)
628        (when (hash-table-p value)
629          (remhash id value)))
630      gnus-registry-hashtb)))
631
632 (defun gnus-registry-add-group (id group &optional subject sender)
633   "Add a group for a message, based on the message ID."
634   (when group
635     (when (and id
636                (not (string-match "totally-fudged-out-message-id" id)))
637       (let ((full-group group)
638             (group (if gnus-registry-use-long-group-names 
639                        group 
640                      (gnus-group-short-name group))))
641         (gnus-registry-delete-group id group)
642
643         (unless gnus-registry-use-long-group-names ;; unnecessary in this case
644           (gnus-registry-delete-group id full-group))
645
646         (let ((trail (gethash id gnus-registry-hashtb)))
647           (puthash id (if trail
648                           (cons group trail)
649                         (list group))
650                    gnus-registry-hashtb)
651
652           (when (and (gnus-registry-track-subject-p)
653                      subject)
654             (gnus-registry-store-extra-entry
655              id 
656              'subject 
657              (gnus-registry-simplify-subject subject)))
658           (when (and (gnus-registry-track-sender-p)
659                      sender)
660             (gnus-registry-store-extra-entry
661              id 
662              'sender
663              sender))
664           
665           (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
666
667 (defun gnus-registry-clear ()
668   "Clear the Gnus registry."
669   (interactive)
670   (setq gnus-registry-alist nil)
671   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
672   (setq gnus-registry-dirty t))
673
674 ;;;###autoload
675 (defun gnus-registry-initialize ()
676   (interactive)
677   (setq gnus-registry-install t)
678   (gnus-registry-install-hooks)
679   (gnus-registry-read))
680
681 ;;;###autoload
682 (defun gnus-registry-install-hooks ()
683   "Install the registry hooks."
684   (interactive)
685   (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action) 
686   (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
687   (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
688   (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
689   
690   (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
691   (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
692
693   (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
694
695 (defun gnus-registry-unload-hook ()
696   "Uninstall the registry hooks."
697   (interactive)
698   (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action) 
699   (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
700   (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
701   (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
702   
703   (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
704   (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
705
706   (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
707
708 (when gnus-registry-install
709   (gnus-registry-install-hooks)
710   (gnus-registry-read))
711
712 ;; TODO: a lot of things
713
714 (provide 'gnus-registry)
715
716 ;;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
717 ;;; gnus-registry.el ends here