(gnus-registry-split-fancy-with-parent):
[gnus] / 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 (defcustom gnus-registry-unregistered-group-regex "^nntp"
53   "Group name regex that gnus-registry-register-message-ids won't process."
54   :group 'gnus-registry
55   :type 'regexp)
56
57 ;; Function(s) missing in Emacs 20
58 (when (memq nil (mapcar 'fboundp '(puthash)))
59   (require 'cl)
60   (unless (fboundp 'puthash)
61     ;; alias puthash is missing from Emacs 20 cl-extra.el
62     (defalias 'puthash 'cl-puthash)))
63
64 (defun gnus-registry-translate-to-alist ()
65   (setq gnus-registry-alist (hashtable-to-alist gnus-registry-hashtb)))
66
67 (defun gnus-registry-translate-from-alist ()
68   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist)))
69
70 (defun alist-to-hashtable (alist)
71   "Build a hashtable from the values in ALIST."
72   (let ((ht (make-hash-table                        
73              :size 4096
74              :test 'equal)))
75     (mapc
76      (lambda (kv-pair)
77        (puthash (car kv-pair) (cdr kv-pair) ht))
78      alist)
79      ht))
80
81 (defun hashtable-to-alist (hash)
82   "Build an alist from the values in HASH."
83   (let ((list nil))
84     (maphash
85      (lambda (key value)
86        (setq list (cons (cons key value) list)))
87      hash)
88     list))
89
90 (defun gnus-register-action (action data-header from &optional to method)
91   (let* ((id (mail-header-id data-header))
92         (from (gnus-group-guess-full-name from))
93         (to (if to (gnus-group-guess-full-name to) nil))
94         (to-name (if to to "the Bit Bucket"))
95         (old-entry (gethash id gnus-registry-hashtb)))
96     (gnus-message 5 "Registry: article %s %s from %s to %s"
97                   id
98                   (if method "respooling" "going")
99                   from
100                   to)
101
102     ;; All except copy will need a delete
103     (gnus-registry-delete-group id from)
104
105     (when (equal 'copy action) 
106       (gnus-registry-add-group id from)) ; undo the delete
107
108     (gnus-registry-add-group id to)))
109
110 (defun gnus-register-spool-action (id group)
111   ;; do not process the draft IDs
112 ;  (unless (string-match "totally-fudged-out-message-id" id)
113 ;    (let ((group (gnus-group-guess-full-name group)))
114   (when (string-match "\r$" id)
115     (setq id (substring id 0 -1)))
116   (gnus-message 5 "Registry: article %s spooled to %s"
117                 id
118                 group)
119   (gnus-registry-add-group id group))
120 ;)
121
122 ;; Function for nn{mail|imap}-split-fancy: look up all references in
123 ;; the cache and if a match is found, return that group.
124 (defun gnus-registry-split-fancy-with-parent ()
125   "Split this message into the same group as its parent.  The parent
126 is obtained from the registry.  This function can be used as an entry
127 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
128 this: (: gnus-registry-split-fancy-with-parent) 
129
130 For a message to be split, it looks for the parent message in the
131 References or In-Reply-To header and then looks in the registry to
132 see which group that message was put in.  This group is returned.
133
134 See the Info node `(gnus)Fancy Mail Splitting' for more details."
135   (let ((refstr (or (message-fetch-field "references")
136                     (message-fetch-field "in-reply-to")))
137         (nnmail-split-fancy-with-parent-ignore-groups
138          (if (listp nnmail-split-fancy-with-parent-ignore-groups)
139              nnmail-split-fancy-with-parent-ignore-groups
140            (list nnmail-split-fancy-with-parent-ignore-groups)))
141         references res)
142     (when refstr
143       (setq references (nreverse (gnus-split-references refstr)))
144       (mapcar (lambda (x)
145                 (setq res (or (gnus-registry-fetch-group x) res))
146                 (when (or (gnus-registry-grep-in-list 
147                            res
148                            gnus-registry-unfollowed-groups)
149                           (gnus-registry-grep-in-list 
150                            res 
151                            nnmail-split-fancy-with-parent-ignore-groups))
152                   (setq res nil)))
153               references)
154       (gnus-message 
155        5 
156        "gnus-registry-split-fancy-with-parent traced %s to group %s"
157        refstr (if res res "nil"))
158       res)))
159
160 (defun gnus-registry-register-message-ids ()
161   "Register the Message-ID of every article in the group"
162   (unless (and gnus-registry-unregistered-group-regex
163                (string-match gnus-registry-unregistered-group-regex gnus-newsgroup-name))
164     (dolist (article gnus-newsgroup-articles)
165       (let ((id (gnus-registry-fetch-message-id-fast article)))
166         (unless (gnus-registry-fetch-group id)
167           (gnus-message 9 "Registry: Registering article %d with group %s" 
168                         article gnus-newsgroup-name)
169           (gnus-registry-add-group (gnus-registry-fetch-message-id-fast article)
170                                    gnus-newsgroup-name))))))
171
172 (defun gnus-registry-fetch-message-id-fast (article)
173   "Fetch the Message-ID quickly, using the internal gnus-data-list function"
174   (if (and (numberp article)
175            (assoc article (gnus-data-list nil)))
176       (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
177     nil))
178
179 (defun gnus-registry-grep-in-list (word list)
180   (when word
181     (memq nil
182           (mapcar 'not
183                   (mapcar 
184                    (lambda (x)
185                      (string-match x word))
186                    list)))))
187
188 (defun gnus-registry-fetch-extra (id)
189   "Get the extra data of a message, based on the message ID.
190 Returns the first place where the trail finds a nonstring."
191   (let ((trail (gethash id gnus-registry-hashtb)))
192     (dolist (crumb trail)
193       (unless (stringp crumb)
194         (return crumb)))))
195
196 (defun gnus-registry-store-extra (id extra)
197   "Store the extra data of a message, based on the message ID.
198 The message must have at least one group name."
199   (when (gnus-registry-group-count id)
200     ;; we now know the trail has at least 1 group name, so it's not empty
201     (let ((trail (gethash id gnus-registry-hashtb))
202           (old-extra (gnus-registry-fetch-extra id)))
203       (puthash id (cons extra (delete old-extra trail))
204                gnus-registry-hashtb))))
205
206 (defun gnus-registry-fetch-group (id)
207   "Get the group of a message, based on the message ID.
208 Returns the first place where the trail finds a group name."
209   (when (gnus-registry-group-count id)
210     ;; we now know the trail has at least 1 group name
211     (let ((trail (gethash id gnus-registry-hashtb)))
212       (dolist (crumb trail)
213         (when (stringp crumb)
214           (return crumb))))))
215
216 (defun gnus-registry-group-count (id)
217   "Get the number of groups of a message, based on the message ID."
218   (let ((trail (gethash id gnus-registry-hashtb)))
219     (if (and trail (listp trail))
220         (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
221       0)))
222
223 (defun gnus-registry-delete-group (id group)
224   "Delete a group for a message, based on the message ID."
225   (when group
226     (when id
227       (let ((trail (gethash id gnus-registry-hashtb))
228             (group (gnus-group-short-name group)))
229         (puthash id (if trail
230                         (delete group trail)
231                       nil)
232                  gnus-registry-hashtb))
233       ;; now, clear the entry if there are no more groups
234       (unless (gnus-registry-group-count id)
235         (remhash id gnus-registry-hashtb)))))
236
237 (defun gnus-registry-add-group (id group &rest extra)
238   "Add a group for a message, based on the message ID."
239   ;; make sure there are no duplicate entries
240   (when group
241     (when (and id
242                (not (string-match "totally-fudged-out-message-id" id)))
243       (let ((group (gnus-group-short-name group)))
244         (gnus-registry-delete-group id group)   
245         (let ((trail (gethash id gnus-registry-hashtb)))
246           (puthash id (if trail
247                           (cons group trail)
248                         (list group))
249                    gnus-registry-hashtb)
250           (when extra (gnus-registry-store-extra id extra)))))))
251
252 (defun gnus-registry-clear ()
253   "Clear the Gnus registry."
254   (interactive)
255   (setq gnus-registry-alist nil 
256         gnus-registry-headers-alist nil)
257   (gnus-registry-translate-from-alist))
258
259 ; also does copy, respool, and crosspost
260 (add-hook 'gnus-summary-article-move-hook 'gnus-register-action) 
261 (add-hook 'gnus-summary-article-delete-hook 'gnus-register-action)
262 (add-hook 'gnus-summary-article-expire-hook 'gnus-register-action)
263 (add-hook 'nnmail-spool-hook 'gnus-register-spool-action)
264
265 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-translate-to-alist)
266 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-translate-from-alist)
267
268 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids)
269
270 ;; TODO: a lot of things
271
272 (provide 'gnus-registry)
273
274 ;;; gnus-registry.el ends here