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