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