1 ;;; gnus-registry.el --- article registry for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
29 (eval-when-compile (require 'cl))
36 (defgroup gnus-registry nil
40 (defvar gnus-registry-hashtb nil
41 "*The article registry by Message ID.")
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."
47 :type '(repeat string))
49 (defcustom gnus-registry-cache-file "~/.gnus.registry.eld"
50 "File where the Gnus registry will be stored."
54 ;; Function(s) missing in Emacs 20
55 (when (memq nil (mapcar 'fboundp '(puthash)))
57 (unless (fboundp 'puthash)
58 ;; alias puthash is missing from Emacs 20 cl-extra.el
59 (defalias 'puthash 'cl-puthash)))
61 (defun gnus-registry-cache-read ()
62 "Read the registry cache file."
64 (let ((file gnus-registry-cache-file))
65 (when (file-exists-p file)
66 (gnus-message 5 "Reading %s..." file)
68 (gnus-message 5 "Reading %s...done" file))))
70 (defun gnus-registry-cache-save ()
71 "Save the registry cache file."
73 (let ((file gnus-registry-cache-file))
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))
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)
90 (let ((coding-system-for-write gnus-ding-file-coding-system)
91 (version-control gnus-backup-startup-file)
93 (working-dir (file-name-directory file))
96 ;; Generate the name of a non-existent file.
97 (while (progn (setq working-file
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))
105 working-dir (setq i (1+ i))))
106 (file-exists-p working-file)))
110 (gnus-with-output-to-file working-file
111 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
113 ;; These bindings will mislead the current buffer
114 ;; into thinking that it is visiting the startup
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.
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)))
127 (delete-file working-file)
130 (gnus-kill-buffer (current-buffer))
131 (gnus-message 5 "Saving %s...done" file))))
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)
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))))
145 (defun gnus-registry-save ()
146 (setq gnus-registry-alist (hashtable-to-alist gnus-registry-hashtb))
147 (gnus-registry-cache-save))
149 (defun gnus-registry-read ()
150 (gnus-registry-cache-read)
151 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist)))
153 (defun alist-to-hashtable (alist)
154 "Build a hashtable from the values in ALIST."
155 (let ((ht (make-hash-table
160 (puthash (car kv-pair) (cdr kv-pair) ht))
164 (defun hashtable-to-alist (hash)
165 "Build an alist from the values in HASH."
169 (setq list (cons (cons key value) list)))
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"
181 (if method "respooling" "going")
185 ;; All except copy will need a delete
186 (gnus-registry-delete-group id from)
188 (when (equal 'copy action)
189 (gnus-registry-add-group id from)) ; undo the delete
191 (gnus-registry-add-group id to)))
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"
202 (gnus-registry-add-group id group))
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)
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.
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)))
226 (setq references (nreverse (gnus-split-references refstr)))
228 (setq res (or (gnus-registry-fetch-group x) res))
229 (when (or (gnus-registry-grep-in-list
231 gnus-registry-unfollowed-groups)
232 (gnus-registry-grep-in-list
234 nnmail-split-fancy-with-parent-ignore-groups))
239 "gnus-registry-split-fancy-with-parent traced %s to group %s"
240 refstr (if res res "nil"))
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))))))
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))))
261 (defun gnus-registry-grep-in-list (word list)
267 (string-match x word))
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)
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))))
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)
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))
305 (defun gnus-registry-delete-group (id group)
306 "Delete a group for a message, based on the message ID."
309 (let ((trail (gethash id gnus-registry-hashtb))
310 (group (gnus-group-short-name group)))
311 (puthash id (if trail
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)))))
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
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
331 gnus-registry-hashtb)
332 (when extra (gnus-registry-store-extra id extra)))))))
334 (defun gnus-registry-clear ()
335 "Clear the Gnus registry."
337 (setq gnus-registry-alist nil)
338 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist)))
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)
346 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
347 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
349 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids)
351 ;; TODO: a lot of things
353 (provide 'gnus-registry)
355 ;;; gnus-registry.el ends here