b43e488531c1c543ceb8cfd8d6016dbb86556c72
[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 (defvar gnus-registry-dirty t
37  "Boolean set to t when the registry is modified")
38
39 (defgroup gnus-registry nil
40   "The Gnus registry."
41   :group 'gnus)
42
43 (defvar gnus-registry-hashtb nil
44   "*The article registry by Message ID.")
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-install nil
53   "Whether the registry should be installed."
54   :group 'gnus-registry
55   :type 'boolean)
56
57 (defcustom gnus-registry-cache-file "~/.gnus.registry.eld"
58   "File where the Gnus registry will be stored."
59   :group 'gnus-registry
60   :type 'file)
61
62 ;; Function(s) missing in Emacs 20
63 (when (memq nil (mapcar 'fboundp '(puthash)))
64   (require 'cl)
65   (unless (fboundp 'puthash)
66     ;; alias puthash is missing from Emacs 20 cl-extra.el
67     (defalias 'puthash 'cl-puthash)))
68
69 (defun gnus-registry-cache-read ()
70   "Read the registry cache file."
71   (interactive)
72   (let ((file gnus-registry-cache-file))
73     (when (file-exists-p file)
74       (gnus-message 5 "Reading %s..." file)
75       (gnus-load file)
76       (gnus-message 5 "Reading %s...done" file))))
77
78 (defun gnus-registry-cache-save ()
79   "Save the registry cache file."
80   (interactive)
81   (let ((file gnus-registry-cache-file))
82     (save-excursion
83       (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
84       (make-local-variable 'version-control)
85     (setq version-control gnus-backup-startup-file)
86     (setq buffer-file-name file)
87     (setq default-directory (file-name-directory buffer-file-name))
88     (buffer-disable-undo)
89     (erase-buffer)
90     (gnus-message 5 "Saving %s..." file)
91     (if gnus-save-startup-file-via-temp-buffer
92         (let ((coding-system-for-write gnus-ding-file-coding-system)
93               (standard-output (current-buffer)))
94           (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
95           (gnus-registry-cache-whitespace file)
96           (save-buffer))
97       (let ((coding-system-for-write gnus-ding-file-coding-system)
98             (version-control gnus-backup-startup-file)
99             (startup-file file)
100             (working-dir (file-name-directory file))
101             working-file
102             (i -1))
103         ;; Generate the name of a non-existent file.
104         (while (progn (setq working-file
105                             (format
106                              (if (and (eq system-type 'ms-dos)
107                                       (not (gnus-long-file-names)))
108                                  "%s#%d.tm#" ; MSDOS limits files to 8+3
109                                (if (memq system-type '(vax-vms axp-vms))
110                                    "%s$tmp$%d"
111                                  "%s#tmp#%d"))
112                              working-dir (setq i (1+ i))))
113                       (file-exists-p working-file)))
114         
115         (unwind-protect
116             (progn
117               (gnus-with-output-to-file working-file
118                 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
119               
120               ;; These bindings will mislead the current buffer
121               ;; into thinking that it is visiting the startup
122               ;; file.
123               (let ((buffer-backed-up nil)
124                     (buffer-file-name startup-file)
125                     (file-precious-flag t)
126                     (setmodes (file-modes startup-file)))
127                 ;; Backup the current version of the startup file.
128                 (backup-buffer)
129                 
130                 ;; Replace the existing startup file with the temp file.
131                 (rename-file working-file startup-file t)
132                 (set-file-modes startup-file setmodes)))
133           (condition-case nil
134               (delete-file working-file)
135             (file-error nil)))))
136     
137     (gnus-kill-buffer (current-buffer))
138     (gnus-message 5 "Saving %s...done" file))))
139
140 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
141 ;; Save the gnus-registry file with extra line breaks.
142 (defun gnus-registry-cache-whitespace (filename)
143   (gnus-message 4 "Adding whitespace to %s" filename)
144   (save-excursion
145     (goto-char (point-min))
146     (while (re-search-forward "^(\\|(\\\"" nil t)
147       (replace-match "\n\\&" t))
148     (goto-char (point-min))
149     (while (re-search-forward " $" nil t)
150       (replace-match "" t t))))
151
152 (defun gnus-registry-save ()
153 ;; TODO: delete entries with 0 groups
154   (when gnus-registry-dirty
155     (setq gnus-registry-alist (hashtable-to-alist gnus-registry-hashtb))
156     (gnus-registry-cache-save)
157     (setq gnus-registry-dirty nil)))
158
159 (defun gnus-registry-read ()
160   (gnus-registry-cache-read)
161   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
162   (setq gnus-registry-dirty nil))
163
164 (defun alist-to-hashtable (alist)
165   "Build a hashtable from the values in ALIST."
166   (let ((ht (make-hash-table                        
167              :size 4096
168              :test 'equal)))
169     (mapc
170      (lambda (kv-pair)
171        (puthash (car kv-pair) (cdr kv-pair) ht))
172      alist)
173      ht))
174
175 (defun hashtable-to-alist (hash)
176   "Build an alist from the values in HASH."
177   (let ((list nil))
178     (maphash
179      (lambda (key value)
180        (setq list (cons (cons key value) list)))
181      hash)
182     list))
183
184 (defun gnus-register-action (action data-header from &optional to method)
185   (let* ((id (mail-header-id data-header))
186         (from (gnus-group-guess-full-name from))
187         (to (if to (gnus-group-guess-full-name to) nil))
188         (to-name (if to to "the Bit Bucket"))
189         (old-entry (gethash id gnus-registry-hashtb)))
190     (gnus-message 5 "Registry: article %s %s from %s to %s"
191                   id
192                   (if method "respooling" "going")
193                   from
194                   to)
195
196     ;; All except copy will need a delete
197     (gnus-registry-delete-group id from)
198
199     (when (equal 'copy action) 
200       (gnus-registry-add-group id from)) ; undo the delete
201
202     (gnus-registry-add-group id to)))
203
204 (defun gnus-register-spool-action (id group)
205   ;; do not process the draft IDs
206 ;  (unless (string-match "totally-fudged-out-message-id" id)
207 ;    (let ((group (gnus-group-guess-full-name group)))
208   (when (string-match "\r$" id)
209     (setq id (substring id 0 -1)))
210   (gnus-message 5 "Registry: article %s spooled to %s"
211                 id
212                 group)
213   (gnus-registry-add-group id group))
214 ;)
215
216 ;; Function for nn{mail|imap}-split-fancy: look up all references in
217 ;; the cache and if a match is found, return that group.
218 (defun gnus-registry-split-fancy-with-parent ()
219   "Split this message into the same group as its parent.  The parent
220 is obtained from the registry.  This function can be used as an entry
221 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
222 this: (: gnus-registry-split-fancy-with-parent) 
223
224 For a message to be split, it looks for the parent message in the
225 References or In-Reply-To header and then looks in the registry to
226 see which group that message was put in.  This group is returned.
227
228 See the Info node `(gnus)Fancy Mail Splitting' for more details."
229   (let ((refstr (or (message-fetch-field "references")
230                     (message-fetch-field "in-reply-to")))
231         (nnmail-split-fancy-with-parent-ignore-groups
232          (if (listp nnmail-split-fancy-with-parent-ignore-groups)
233              nnmail-split-fancy-with-parent-ignore-groups
234            (list nnmail-split-fancy-with-parent-ignore-groups)))
235         references res)
236     (when refstr
237       (setq references (nreverse (gnus-split-references refstr)))
238       (mapcar (lambda (x)
239                 (setq res (or (gnus-registry-fetch-group x) res))
240                 (when (or (gnus-registry-grep-in-list 
241                            res
242                            gnus-registry-unfollowed-groups)
243                           (gnus-registry-grep-in-list 
244                            res 
245                            nnmail-split-fancy-with-parent-ignore-groups))
246                   (setq res nil)))
247               references)
248       (gnus-message 
249        5 
250        "gnus-registry-split-fancy-with-parent traced %s to group %s"
251        refstr (if res res "nil"))
252       res)))
253
254 (defun gnus-registry-register-message-ids ()
255   "Register the Message-ID of every article in the group"
256   (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
257     (dolist (article gnus-newsgroup-articles)
258       (let ((id (gnus-registry-fetch-message-id-fast article)))
259         (unless (gnus-registry-fetch-group id)
260           (gnus-message 9 "Registry: Registering article %d with group %s" 
261                         article gnus-newsgroup-name)
262           (gnus-registry-add-group (gnus-registry-fetch-message-id-fast article)
263                                    gnus-newsgroup-name))))))
264
265 (defun gnus-registry-fetch-message-id-fast (article)
266   "Fetch the Message-ID quickly, using the internal gnus-data-list function"
267   (if (and (numberp article)
268            (assoc article (gnus-data-list nil)))
269       (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
270     nil))
271
272 (defun gnus-registry-grep-in-list (word list)
273   (when word
274     (memq nil
275           (mapcar 'not
276                   (mapcar 
277                    (lambda (x)
278                      (string-match x word))
279                    list)))))
280
281 (defun gnus-registry-fetch-extra (id &optional entry)
282   "Get the extra data of a message, based on the message ID.
283 Returns the first place where the trail finds a nonstring."
284   (let ((trail (gethash id gnus-registry-hashtb)))
285     (dolist (crumb trail)
286       (unless (stringp crumb)
287         (return (gnus-registry-fetch-extra-entry crumb entry))))))
288
289 (defun gnus-registry-fetch-extra-entry (alist &optional entry)
290   "Get the extra data of a message, or a specific entry in it."
291   (if entry
292       (assq entry alist)
293     alist))
294
295 (defun gnus-registry-store-extra (id extra)
296   "Store the extra data of a message, based on the message ID.
297 The message must have at least one group name."
298   (when (gnus-registry-group-count id)
299     ;; we now know the trail has at least 1 group name, so it's not empty
300     (let ((trail (gethash id gnus-registry-hashtb))
301           (old-extra (gnus-registry-fetch-extra id)))
302       (puthash id (cons extra (delete old-extra trail))
303                gnus-registry-hashtb)
304       (setq gnus-registry-dirty t))))
305
306 (defun gnus-registry-store-extra-entry (id key value)
307   "Put a specific entry in the extras field of the registry entry for id."
308   (let* ((extra (gnus-registry-fetch-extra id))
309          (alist (cons (cons key value)
310                  (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))))
311     (gnus-registry-store-extra id alist)))
312
313 (defun gnus-registry-fetch-group (id)
314   "Get the group of a message, based on the message ID.
315 Returns the first place where the trail finds a group name."
316   (when (gnus-registry-group-count id)
317     ;; we now know the trail has at least 1 group name
318     (let ((trail (gethash id gnus-registry-hashtb)))
319       (dolist (crumb trail)
320         (when (stringp crumb)
321           (return crumb))))))
322
323 (defun gnus-registry-group-count (id)
324   "Get the number of groups of a message, based on the message ID."
325   (let ((trail (gethash id gnus-registry-hashtb)))
326     (if (and trail (listp trail))
327         (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
328       0)))
329
330 (defun gnus-registry-delete-group (id group)
331   "Delete a group for a message, based on the message ID."
332   (when group
333     (when id
334       (let ((trail (gethash id gnus-registry-hashtb))
335             (group (gnus-group-short-name group)))
336         (puthash id (if trail
337                         (delete group trail)
338                       nil)
339                  gnus-registry-hashtb))
340       ;; now, clear the entry if there are no more groups
341       (unless (gnus-registry-group-count id)
342         (remhash id gnus-registry-hashtb))
343       (gnus-registry-store-extra-entry id 'mtime (current-time)))))
344
345 (defun gnus-registry-add-group (id group &rest extra)
346   "Add a group for a message, based on the message ID."
347   ;; make sure there are no duplicate entries
348   (when group
349     (when (and id
350                (not (string-match "totally-fudged-out-message-id" id)))
351       (let ((group (gnus-group-short-name group)))
352         (gnus-registry-delete-group id group)   
353         (let ((trail (gethash id gnus-registry-hashtb)))
354           (puthash id (if trail
355                           (cons group trail)
356                         (list group))
357                    gnus-registry-hashtb)
358           (when extra (gnus-registry-store-extra id extra))
359           (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
360
361 (defun gnus-registry-clear ()
362   "Clear the Gnus registry."
363   (interactive)
364   (setq gnus-registry-alist nil)
365   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
366   (setq gnus-registry-dirty t))
367
368 (defun gnus-registry-install-hooks ()
369   "Install the registry hooks."
370   (interactive)
371   (add-hook 'gnus-summary-article-move-hook 'gnus-register-action) 
372   (add-hook 'gnus-summary-article-delete-hook 'gnus-register-action)
373   (add-hook 'gnus-summary-article-expire-hook 'gnus-register-action)
374   (add-hook 'nnmail-spool-hook 'gnus-register-spool-action)
375   
376   (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
377   (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
378
379   (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
380
381 (when gnus-registry-install
382   (gnus-registry-install-hooks))
383
384 ;; TODO: a lot of things
385
386 (provide 'gnus-registry)
387
388 ;;; gnus-registry.el ends here