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