Synch to Oort Gnus 200304302331.
[elisp/gnus.git-] / lisp / gnus-registry.el
1 ;;; gnus-registry.el --- article registry for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-int)
33 (require 'gnus-sum)
34 (require 'nnmail)
35
36 (defgroup gnus-registry nil
37   "The Gnus registry."
38   :group 'gnus)
39
40 (defvar gnus-registry-hashtb nil
41   "*The article registry by Message ID.")
42
43 (defvar gnus-registry-headers-hashtb nil
44   "*The article header registry by Message ID.  Unused for now.")
45
46 (defcustom gnus-registry-unfollowed-groups '("delayed" "drafts" "queue")
47   "List of groups that gnus-registry-split-fancy-with-parent won't follow.
48 The group names are matched, they don't have to be fully qualified."
49   :group 'gnus-registry
50   :type '(repeat string))
51
52 ;; Function(s) missing in Emacs 20
53 (when (memq nil (mapcar 'fboundp '(puthash)))
54   (require 'cl)
55   (unless (fboundp 'puthash)
56     ;; alias puthash is missing from Emacs 20 cl-extra.el
57     (defalias 'puthash 'cl-puthash)))
58
59 (defun gnus-registry-translate-to-alist ()
60   (setq gnus-registry-alist (hashtable-to-alist gnus-registry-hashtb)))
61
62 (defun gnus-registry-translate-from-alist ()
63   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist)))
64
65 (defun alist-to-hashtable (alist)
66   "Build a hashtable from the values in ALIST."
67   (let ((ht (make-hash-table                        
68              :size 4096
69              :test 'equal)))
70     (mapc
71      (lambda (kv-pair)
72        (puthash (car kv-pair) (cdr kv-pair) ht))
73      alist)
74      ht))
75
76 (defun hashtable-to-alist (hash)
77   "Build an alist from the values in HASH."
78   (let ((list nil))
79     (maphash
80      (lambda (key value)
81        (setq list (cons (cons key value) list)))
82      hash)
83     list))
84
85 (defun gnus-register-action (action data-header from &optional to method)
86   (let* ((id (mail-header-id data-header))
87         (from (gnus-group-guess-full-name from))
88         (to (if to (gnus-group-guess-full-name to) nil))
89         (to-name (if to to "the Bit Bucket"))
90         (old-entry (gethash id gnus-registry-hashtb)))
91     (gnus-message 5 "Registry: article %s %s from %s to %s"
92                   id
93                   (if method "respooling" "going")
94                   from
95                   to)
96
97     ;; All except copy will need a delete
98     (gnus-registry-delete-group id from)
99
100     (when (equal 'copy action) 
101       (gnus-registry-add-group id from)) ; undo the delete
102
103     (gnus-registry-add-group id to)))
104
105 (defun gnus-register-spool-action (id group)
106   ;; do not process the draft IDs
107 ;  (unless (string-match "totally-fudged-out-message-id" id)
108 ;    (let ((group (gnus-group-guess-full-name group)))
109   (when (string-match "\r$" id)
110     (setq id (substring id 0 -1)))
111   (gnus-message 5 "Registry: article %s spooled to %s"
112                 id
113                 group)
114   (gnus-registry-add-group id group))
115 ;)
116
117 ;; Function for nn{mail|imap}-split-fancy: look up all references in
118 ;; the cache and if a match is found, return that group.
119 (defun gnus-registry-split-fancy-with-parent ()
120   "Split this message into the same group as its parent.  The parent
121 is obtained from the registry.  This function can be used as an entry
122 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
123 this: (: gnus-registry-split-fancy-with-parent) 
124
125 For a message to be split, it looks for the parent message in the
126 References or In-Reply-To header and then looks in the registry to
127 see which group that message was put in.  This group is returned.
128
129 See the Info node `(gnus)Fancy Mail Splitting' for more details."
130   (let ((refstr (or (message-fetch-field "references")
131                     (message-fetch-field "in-reply-to")))
132         (references nil)
133         (res nil))
134     (when refstr
135       (setq references (nreverse (gnus-split-references refstr)))
136       (mapcar (lambda (x)
137                 (setq res (or (gnus-registry-fetch-group x) res))
138                 (when (or (gnus-registry-grep-in-list 
139                            res
140                            gnus-registry-unfollowed-groups)
141                           (gnus-registry-grep-in-list 
142                            res 
143                            nnmail-split-fancy-with-parent-ignore-groups))
144                   (setq res nil)))
145               references)
146       (gnus-message 
147        5 
148        "gnus-registry-split-fancy-with-parent traced %s to group %s"
149        refstr (if res res "nil"))
150       res)))
151
152 (defun gnus-registry-register-message-ids ()
153   "Register the Message-ID of every article in the group"
154   (dolist (article gnus-newsgroup-articles)
155     (let ((id (gnus-registry-fetch-message-id-fast article)))
156       (unless (gnus-registry-fetch-group id)
157         (gnus-message 9 "Registry: Registering article %d with group %s" 
158                       article gnus-newsgroup-name)
159         (gnus-registry-add-group (gnus-registry-fetch-message-id-fast article)
160                                  gnus-newsgroup-name)))))
161
162 (defun gnus-registry-fetch-message-id-fast (article)
163   "Fetch the Message-ID quickly, using the internal gnus-data-list function"
164   (if (and (numberp article)
165            (assoc article (gnus-data-list nil)))
166       (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
167     nil))
168
169 (defun gnus-registry-grep-in-list (word list)
170   (when word
171     (memq nil
172           (mapcar 'not
173                   (mapcar 
174                    (lambda (x)
175                      (string-match x word))
176                    list)))))
177
178 (defun gnus-registry-fetch-group (id)
179   "Get the group of a message, based on the message ID.
180 Returns the first place where the trail finds a spool action."
181   (when id
182     (let ((trail (gethash id gnus-registry-hashtb)))
183       (if trail
184           (car trail)
185         nil))))
186
187 (defun gnus-registry-delete-group (id group)
188   "Get the group of a message, based on the message ID.
189 Returns the first place where the trail finds a spool action."
190   (when group
191     (when id
192       (let ((trail (gethash id gnus-registry-hashtb))
193             (group (gnus-group-short-name group)))
194         (puthash id (if trail
195                         (delete group trail)
196                       nil)
197                  gnus-registry-hashtb))
198       ;; now, clear the entry if it's empty
199       (unless (gethash id gnus-registry-hashtb)
200         (remhash id gnus-registry-hashtb)))))
201
202 (defun gnus-registry-add-group (id group)
203   "Get the group of a message, based on the message ID.
204 Returns the first place where the trail finds a spool action."
205   ;; make sure there are no duplicate entries
206   (when group
207     (when id
208       (let ((group (gnus-group-short-name group)))
209         (gnus-registry-delete-group id group)   
210         (let ((trail (gethash id gnus-registry-hashtb)))
211           (puthash id (if trail
212                           (cons group trail)
213                         (list group))
214                    gnus-registry-hashtb))))))
215
216 (defun gnus-registry-clear ()
217   "Clear the Gnus registry."
218   (interactive)
219   (setq gnus-registry-alist nil 
220         gnus-registry-headers-alist nil)
221   (gnus-registry-translate-from-alist))
222
223 ; also does copy, respool, and crosspost
224 (add-hook 'gnus-summary-article-move-hook 'gnus-register-action) 
225 (add-hook 'gnus-summary-article-delete-hook 'gnus-register-action)
226 (add-hook 'gnus-summary-article-expire-hook 'gnus-register-action)
227 (add-hook 'nnmail-spool-hook 'gnus-register-spool-action)
228
229 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-translate-to-alist)
230 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-translate-from-alist)
231
232 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids)
233
234 ;; TODO: a lot of things
235
236 (provide 'gnus-registry)
237
238 ;;; gnus-registry.el ends here