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