* gnus-util.el (gnus-split-references): accept a nil references
[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     ;; the references string must be valid and parse to valid references
373     (if (and refstr (gnus-extract-references refstr))
374         (progn
375           (setq references (nreverse (gnus-extract-references refstr)))
376           (mapcar (lambda (x)
377                     (setq res (or (gnus-registry-fetch-group x) res))
378                     (when (or (gnus-registry-grep-in-list
379                                res
380                                gnus-registry-unfollowed-groups)
381                               (gnus-registry-grep-in-list 
382                                res
383                                nnmail-split-fancy-with-parent-ignore-groups))
384                       (setq res nil)))
385                   references))
386
387       ;; else: there were no references, now try the extra tracking
388       (let ((sender (message-fetch-field "from"))
389             (subject (gnus-registry-simplify-subject
390                       (message-fetch-field "subject")))
391             (single-match t))
392         (when (and single-match
393                    (gnus-registry-track-sender-p)
394                    sender)
395           (maphash
396            (lambda (key value)
397              (let ((this-sender (cdr
398                                  (gnus-registry-fetch-extra key 'sender))))
399                (when (and single-match
400                           this-sender
401                           (equal sender this-sender))
402                  ;; too many matches, bail
403                  (unless (equal res (gnus-registry-fetch-group key))
404                    (setq single-match nil))
405                  (setq res (gnus-registry-fetch-group key))
406                  (when (and sender res)
407                    (gnus-message
408                     ;; raise level of messaging if gnus-registry-track-extra
409                     (if gnus-registry-track-extra 7 9)
410                     "%s (extra tracking) traced sender %s to group %s"
411                     "gnus-registry-split-fancy-with-parent"
412                     sender
413                     res)))))
414            gnus-registry-hashtb))
415         (when (and single-match
416                    (gnus-registry-track-subject-p)
417                    subject
418                    (< gnus-registry-minimum-subject-length (length subject)))
419           (maphash
420            (lambda (key value)
421              (let ((this-subject (cdr 
422                                   (gnus-registry-fetch-extra key 'subject))))
423                (when (and single-match
424                           this-subject
425                           (equal subject this-subject))
426                  ;; too many matches, bail
427                  (unless (equal res (gnus-registry-fetch-group key))
428                    (setq single-match nil))
429                  (setq res (gnus-registry-fetch-group key))
430                  (when (and subject res)
431                    (gnus-message
432                     ;; raise level of messaging if gnus-registry-track-extra
433                     (if gnus-registry-track-extra 7 9)
434                     "%s (extra tracking) traced subject %s to group %s"
435                     "gnus-registry-split-fancy-with-parent"
436                     subject
437                     res)))))
438            gnus-registry-hashtb))
439         (unless single-match
440           (gnus-message
441            3
442            "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
443            refstr)
444           (setq res nil))))
445     (when (and refstr res)
446       (gnus-message
447        5
448        "gnus-registry-split-fancy-with-parent traced %s to group %s"
449        refstr res))
450
451     (when (and res gnus-registry-use-long-group-names)
452       (let ((m1 (gnus-find-method-for-group res))
453             (m2 (or gnus-command-method 
454                     (gnus-find-method-for-group gnus-newsgroup-name)))
455             (short-res (gnus-group-short-name res)))
456       (if (gnus-methods-equal-p m1 m2)
457           (progn
458             (gnus-message
459              9 
460              "gnus-registry-split-fancy-with-parent stripped group %s to %s"
461              res
462              short-res)
463             (setq res short-res))
464         ;; else...
465         (gnus-message
466          7
467          "gnus-registry-split-fancy-with-parent ignored foreign group %s"
468          res)
469         (setq res nil))))
470     res))
471
472 (defun gnus-registry-register-message-ids ()
473   "Register the Message-ID of every article in the group"
474   (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
475     (dolist (article gnus-newsgroup-articles)
476       (let ((id (gnus-registry-fetch-message-id-fast article)))
477         (unless (gnus-registry-fetch-group id)
478           (gnus-message 9 "Registry: Registering article %d with group %s" 
479                         article gnus-newsgroup-name)
480           (gnus-registry-add-group 
481            (gnus-registry-fetch-message-id-fast article)
482            gnus-newsgroup-name
483            (gnus-registry-fetch-simplified-message-subject-fast article)
484            (gnus-registry-fetch-sender-fast article)))))))
485
486 (defun gnus-registry-fetch-message-id-fast (article)
487   "Fetch the Message-ID quickly, using the internal gnus-data-list function"
488   (if (and (numberp article)
489            (assoc article (gnus-data-list nil)))
490       (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
491     nil))
492
493 (defun gnus-registry-simplify-subject (subject)
494   (if (stringp subject)
495       (gnus-simplify-subject subject)
496     nil))
497
498 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
499   "Fetch the Subject quickly, using the internal gnus-data-list function"
500   (if (and (numberp article)
501            (assoc article (gnus-data-list nil)))
502       (gnus-registry-simplify-subject
503        (mail-header-subject (gnus-data-header
504                              (assoc article (gnus-data-list nil)))))
505     nil))
506
507 (defun gnus-registry-fetch-sender-fast (article)
508   "Fetch the Sender quickly, using the internal gnus-data-list function"
509   (if (and (numberp article)
510            (assoc article (gnus-data-list nil)))
511       (mail-header-from (gnus-data-header
512                          (assoc article (gnus-data-list nil))))
513     nil))
514
515 (defun gnus-registry-grep-in-list (word list)
516   (when word
517     (memq nil
518           (mapcar 'not
519                   (mapcar 
520                    (lambda (x)
521                      (string-match x word))
522                    list)))))
523
524 (defun gnus-registry-fetch-extra (id &optional entry)
525   "Get the extra data of a message, based on the message ID.
526 Returns the first place where the trail finds a nonstring."
527   (let ((entry-cache (gethash entry gnus-registry-hashtb)))
528     (if (and entry
529              (hash-table-p entry-cache)
530              (gethash id entry-cache))
531         (gethash id entry-cache)
532       ;; else, if there is no caching possible...
533       (let ((trail (gethash id gnus-registry-hashtb)))
534         (when (listp trail)
535           (dolist (crumb trail)
536             (unless (stringp crumb)
537               (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
538
539 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
540   "Get the extra data of a message, or a specific entry in it.
541 Update the entry cache if needed."
542   (if (and entry id)
543       (let ((entry-cache (gethash entry gnus-registry-hashtb))
544             entree)
545         (when gnus-registry-entry-caching
546           ;; create the hash table
547           (unless (hash-table-p entry-cache)
548             (setq entry-cache (make-hash-table
549                                :size 4096
550                                :test 'equal))
551             (puthash entry entry-cache gnus-registry-hashtb))
552
553           ;; get the entree from the hash table or from the alist
554           (setq entree (gethash id entry-cache)))
555         
556         (unless entree
557           (setq entree (assq entry alist))
558           (when gnus-registry-entry-caching
559             (puthash id entree entry-cache)))
560         entree)
561     alist))
562
563 (defun gnus-registry-store-extra (id extra)
564   "Store the extra data of a message, based on the message ID.
565 The message must have at least one group name."
566   (when (gnus-registry-group-count id)
567     ;; we now know the trail has at least 1 group name, so it's not empty
568     (let ((trail (gethash id gnus-registry-hashtb))
569           (old-extra (gnus-registry-fetch-extra id))
570           entry-cache)
571       (dolist (crumb trail)
572         (unless (stringp crumb)
573           (dolist (entry crumb)
574             (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
575           (when entry-cache
576             (remhash id entry-cache))))
577       (puthash id (cons extra (delete old-extra trail))
578                gnus-registry-hashtb)
579       (setq gnus-registry-dirty t)))))
580
581 (defun gnus-registry-store-extra-entry (id key value)
582   "Put a specific entry in the extras field of the registry entry for id."
583   (let* ((extra (gnus-registry-fetch-extra id))
584          (alist (cons (cons key value)
585                  (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
586     (gnus-registry-store-extra id alist)))
587
588 (defun gnus-registry-fetch-group (id)
589   "Get the group of a message, based on the message ID.
590 Returns the first place where the trail finds a group name."
591   (when (gnus-registry-group-count id)
592     ;; we now know the trail has at least 1 group name
593     (let ((trail (gethash id gnus-registry-hashtb)))
594       (dolist (crumb trail)
595         (when (stringp crumb)
596           (return (if gnus-registry-use-long-group-names 
597                        crumb 
598                      (gnus-group-short-name crumb))))))))
599
600 (defun gnus-registry-group-count (id)
601   "Get the number of groups of a message, based on the message ID."
602   (let ((trail (gethash id gnus-registry-hashtb)))
603     (if (and trail (listp trail))
604         (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
605       0)))
606
607 (defun gnus-registry-delete-group (id group)
608   "Delete a group for a message, based on the message ID."
609   (when group
610     (when id
611       (let ((trail (gethash id gnus-registry-hashtb))
612             (group (gnus-group-short-name group)))
613         (puthash id (if trail
614                         (delete group trail)
615                       nil)
616                  gnus-registry-hashtb))
617       ;; now, clear the entry if there are no more groups
618       (when gnus-registry-trim-articles-without-groups
619         (unless (gnus-registry-group-count id)
620           (gnus-registry-delete-id id)))
621       (gnus-registry-store-extra-entry id 'mtime (current-time)))))
622
623 (defun gnus-registry-delete-id (id)
624   "Delete a message ID from the registry."
625   (when (stringp id)
626     (remhash id gnus-registry-hashtb)
627     (maphash
628      (lambda (key value)
629        (when (hash-table-p value)
630          (remhash id value)))
631      gnus-registry-hashtb)))
632
633 (defun gnus-registry-add-group (id group &optional subject sender)
634   "Add a group for a message, based on the message ID."
635   (when group
636     (when (and id
637                (not (string-match "totally-fudged-out-message-id" id)))
638       (let ((full-group group)
639             (group (if gnus-registry-use-long-group-names 
640                        group 
641                      (gnus-group-short-name group))))
642         (gnus-registry-delete-group id group)
643
644         (unless gnus-registry-use-long-group-names ;; unnecessary in this case
645           (gnus-registry-delete-group id full-group))
646
647         (let ((trail (gethash id gnus-registry-hashtb)))
648           (puthash id (if trail
649                           (cons group trail)
650                         (list group))
651                    gnus-registry-hashtb)
652
653           (when (and (gnus-registry-track-subject-p)
654                      subject)
655             (gnus-registry-store-extra-entry
656              id 
657              'subject 
658              (gnus-registry-simplify-subject subject)))
659           (when (and (gnus-registry-track-sender-p)
660                      sender)
661             (gnus-registry-store-extra-entry
662              id 
663              'sender
664              sender))
665           
666           (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
667
668 (defun gnus-registry-clear ()
669   "Clear the Gnus registry."
670   (interactive)
671   (setq gnus-registry-alist nil)
672   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
673   (setq gnus-registry-dirty t))
674
675 ;;;###autoload
676 (defun gnus-registry-initialize ()
677   (interactive)
678   (setq gnus-registry-install t)
679   (gnus-registry-install-hooks)
680   (gnus-registry-read))
681
682 ;;;###autoload
683 (defun gnus-registry-install-hooks ()
684   "Install the registry hooks."
685   (interactive)
686   (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action) 
687   (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
688   (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
689   (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
690   
691   (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
692   (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
693
694   (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
695
696 (defun gnus-registry-unload-hook ()
697   "Uninstall the registry hooks."
698   (interactive)
699   (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action) 
700   (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
701   (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
702   (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
703   
704   (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
705   (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
706
707   (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
708
709 (when gnus-registry-install
710   (gnus-registry-install-hooks)
711   (gnus-registry-read))
712
713 ;; TODO: a lot of things
714
715 (provide 'gnus-registry)
716
717 ;;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
718 ;;; gnus-registry.el ends here