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