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