(mml2015-epg-encrypt): Save the recipient keys in message-options.
[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-wash-for-keywords (&optional force)
496   (interactive)
497   (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article))
498         word words)
499     (if (or (not (gnus-registry-fetch-extra id 'keywords))
500             force)
501         (save-excursion
502           (set-buffer gnus-article-buffer)
503           (article-goto-body)
504           (save-window-excursion
505             (save-restriction
506               (narrow-to-region (point) (point-max))
507               (with-syntax-table gnus-adaptive-word-syntax-table
508                 (while (re-search-forward "\\b\\w+\\b" nil t)
509                   (setq word (gnus-registry-remove-alist-text-properties
510                               (downcase (buffer-substring
511                                          (match-beginning 0) (match-end 0)))))
512                   (if (> (length word) 3)
513                       (push word words))))))
514           (gnus-registry-store-extra-entry id 'keywords words)))))
515
516 (defun gnus-registry-find-keywords (keyword)
517   (interactive "skeyword: ")
518   (let (articles)
519     (maphash
520      (lambda (key value)
521        (when (gnus-registry-grep-in-list
522               keyword
523               (cdr (gnus-registry-fetch-extra key 'keywords)))
524          (push key articles)))
525      gnus-registry-hashtb)
526     articles))
527
528 (defun gnus-registry-register-message-ids ()
529   "Register the Message-ID of every article in the group"
530   (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
531     (dolist (article gnus-newsgroup-articles)
532       (let ((id (gnus-registry-fetch-message-id-fast article)))
533         (unless (gnus-registry-fetch-group id)
534           (gnus-message 9 "Registry: Registering article %d with group %s"
535                         article gnus-newsgroup-name)
536           (gnus-registry-add-group
537            (gnus-registry-fetch-message-id-fast article)
538            gnus-newsgroup-name
539            (gnus-registry-fetch-simplified-message-subject-fast article)
540            (gnus-registry-fetch-sender-fast article)))))))
541
542 (defun gnus-registry-fetch-message-id-fast (article)
543   "Fetch the Message-ID quickly, using the internal gnus-data-list function"
544   (if (and (numberp article)
545            (assoc article (gnus-data-list nil)))
546       (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
547     nil))
548
549 (defun gnus-registry-simplify-subject (subject)
550   (if (stringp subject)
551       (gnus-simplify-subject subject)
552     nil))
553
554 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
555   "Fetch the Subject quickly, using the internal gnus-data-list function"
556   (if (and (numberp article)
557            (assoc article (gnus-data-list nil)))
558       (gnus-string-remove-all-properties
559        (gnus-registry-simplify-subject
560         (mail-header-subject (gnus-data-header
561                               (assoc article (gnus-data-list nil))))))
562     nil))
563
564 (defun gnus-registry-fetch-sender-fast (article)
565   "Fetch the Sender quickly, using the internal gnus-data-list function"
566   (if (and (numberp article)
567            (assoc article (gnus-data-list nil)))
568       (gnus-string-remove-all-properties
569        (mail-header-from (gnus-data-header
570                           (assoc article (gnus-data-list nil)))))
571     nil))
572
573 (defun gnus-registry-grep-in-list (word list)
574   (when word
575     (memq nil
576           (mapcar 'not
577                   (mapcar
578                    (lambda (x)
579                      (string-match x word))
580                    list)))))
581
582 (defun gnus-registry-fetch-extra (id &optional entry)
583   "Get the extra data of a message, based on the message ID.
584 Returns the first place where the trail finds a nonstring."
585   (let ((entry-cache (gethash entry gnus-registry-hashtb)))
586     (if (and entry
587              (hash-table-p entry-cache)
588              (gethash id entry-cache))
589         (gethash id entry-cache)
590       ;; else, if there is no caching possible...
591       (let ((trail (gethash id gnus-registry-hashtb)))
592         (when (listp trail)
593           (dolist (crumb trail)
594             (unless (stringp crumb)
595               (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
596
597 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
598   "Get the extra data of a message, or a specific entry in it.
599 Update the entry cache if needed."
600   (if (and entry id)
601       (let ((entry-cache (gethash entry gnus-registry-hashtb))
602             entree)
603         (when gnus-registry-entry-caching
604           ;; create the hash table
605           (unless (hash-table-p entry-cache)
606             (setq entry-cache (make-hash-table
607                                :size 4096
608                                :test 'equal))
609             (puthash entry entry-cache gnus-registry-hashtb))
610
611           ;; get the entree from the hash table or from the alist
612           (setq entree (gethash id entry-cache)))
613
614         (unless entree
615           (setq entree (assq entry alist))
616           (when gnus-registry-entry-caching
617             (puthash id entree entry-cache)))
618         entree)
619     alist))
620
621 (defun gnus-registry-store-extra (id extra)
622   "Store the extra data of a message, based on the message ID.
623 The message must have at least one group name."
624   (when (gnus-registry-group-count id)
625     ;; we now know the trail has at least 1 group name, so it's not empty
626     (let ((trail (gethash id gnus-registry-hashtb))
627           (old-extra (gnus-registry-fetch-extra id))
628           entry-cache)
629       (dolist (crumb trail)
630         (unless (stringp crumb)
631           (dolist (entry crumb)
632             (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
633           (when entry-cache
634             (remhash id entry-cache))))
635       (puthash id (cons extra (delete old-extra trail))
636                gnus-registry-hashtb)
637       (setq gnus-registry-dirty t)))))
638
639 (defun gnus-registry-store-extra-entry (id key value)
640   "Put a specific entry in the extras field of the registry entry for id."
641   (let* ((extra (gnus-registry-fetch-extra id))
642          (alist (gnus-registry-remove-alist-text-properties 
643                  (cons (cons key value)
644                        (gnus-assq-delete-all key (gnus-registry-fetch-extra id))))))
645     (gnus-registry-store-extra id alist)))
646
647 (defun gnus-registry-fetch-group (id)
648   "Get the group of a message, based on the message ID.
649 Returns the first place where the trail finds a group name."
650   (when (gnus-registry-group-count id)
651     ;; we now know the trail has at least 1 group name
652     (let ((trail (gethash id gnus-registry-hashtb)))
653       (dolist (crumb trail)
654         (when (stringp crumb)
655           (return (if gnus-registry-use-long-group-names
656                        crumb
657                      (gnus-group-short-name crumb))))))))
658
659 (defun gnus-registry-fetch-groups (id)
660   "Get the groups of a message, based on the message ID."
661   (let ((trail (gethash id gnus-registry-hashtb))
662         groups)
663     (dolist (crumb trail)
664       (when (stringp crumb)
665         ;; push the group name into the list
666         (setq 
667          groups
668          (cons
669           (if (or (not (stringp crumb)) gnus-registry-use-long-group-names)
670               crumb
671             (gnus-group-short-name crumb))
672          groups))))
673     ;; return the list of groups
674     groups))
675
676 (defun gnus-registry-group-count (id)
677   "Get the number of groups of a message, based on the message ID."
678   (let ((trail (gethash id gnus-registry-hashtb)))
679     (if (and trail (listp trail))
680         (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
681       0)))
682
683 (defun gnus-registry-delete-group (id group)
684   "Delete a group for a message, based on the message ID."
685   (when (and group id)
686       (let ((trail (gethash id gnus-registry-hashtb))
687             (short-group (gnus-group-short-name group)))
688         (puthash id (if trail
689                         (delete short-group (delete group trail))
690                       nil)
691                  gnus-registry-hashtb))
692       ;; now, clear the entry if there are no more groups
693       (when gnus-registry-trim-articles-without-groups
694         (unless (gnus-registry-group-count id)
695           (gnus-registry-delete-id id)))
696       ;; is this ID still in the registry?
697       (when (gethash id gnus-registry-hashtb)
698         (gnus-registry-store-extra-entry id 'mtime (current-time)))))
699
700 (defun gnus-registry-delete-id (id)
701   "Delete a message ID from the registry."
702   (when (stringp id)
703     (remhash id gnus-registry-hashtb)
704     (maphash
705      (lambda (key value)
706        (when (hash-table-p value)
707          (remhash id value)))
708      gnus-registry-hashtb)))
709
710 (defun gnus-registry-add-group (id group &optional subject sender)
711   "Add a group for a message, based on the message ID."
712   (when group
713     (when (and id
714                (not (string-match "totally-fudged-out-message-id" id)))
715       (let ((full-group group)
716             (group (if gnus-registry-use-long-group-names
717                        group
718                      (gnus-group-short-name group))))
719         (gnus-registry-delete-group id group)
720
721         (unless gnus-registry-use-long-group-names ;; unnecessary in this case
722           (gnus-registry-delete-group id full-group))
723
724         (let ((trail (gethash id gnus-registry-hashtb)))
725           (puthash id (if trail
726                           (cons group trail)
727                         (list group))
728                    gnus-registry-hashtb)
729
730           (when (and (gnus-registry-track-subject-p)
731                      subject)
732             (gnus-registry-store-extra-entry
733              id
734              'subject
735              (gnus-registry-simplify-subject subject)))
736           (when (and (gnus-registry-track-sender-p)
737                      sender)
738             (gnus-registry-store-extra-entry
739              id
740              'sender
741              sender))
742
743           (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
744
745 (defun gnus-registry-clear ()
746   "Clear the Gnus registry."
747   (interactive)
748   (setq gnus-registry-alist nil)
749   (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
750   (setq gnus-registry-dirty t))
751
752 ;;;###autoload
753 (defun gnus-registry-initialize ()
754   (interactive)
755   (setq gnus-registry-install t)
756   (gnus-registry-install-hooks)
757   (gnus-registry-read))
758
759 ;;;###autoload
760 (defun gnus-registry-install-hooks ()
761   "Install the registry hooks."
762   (interactive)
763   (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
764   (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
765   (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
766   (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
767
768   (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
769   (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
770
771   (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
772
773 (defun gnus-registry-unload-hook ()
774   "Uninstall the registry hooks."
775   (interactive)
776   (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
777   (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
778   (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
779   (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
780
781   (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
782   (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
783
784   (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
785
786 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
787
788 (when gnus-registry-install
789   (gnus-registry-install-hooks)
790   (gnus-registry-read))
791
792 ;; TODO: a lot of things
793
794 (provide 'gnus-registry)
795
796 ;;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
797 ;;; gnus-registry.el ends here