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