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 (defvar gnus-registry-headers-hashtb nil
44 "*The article header registry by Message ID.")
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."
50 :type '(repeat string))
52 ;; Function(s) missing in Emacs 20
53 (when (memq nil (mapcar 'fboundp '(puthash)))
55 (unless (fboundp 'puthash)
56 ;; alias puthash is missing from Emacs 20 cl-extra.el
57 (defalias 'puthash 'cl-puthash)))
59 (defun gnus-registry-translate-to-alist ()
60 (setq gnus-registry-alist (hashtable-to-alist gnus-registry-hashtb))
61 (setq gnus-registry-headers-alist (hashtable-to-alist
62 gnus-registry-headers-hashtb)))
64 (defun gnus-registry-translate-from-alist ()
65 (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
66 (setq gnus-registry-headers-hashtb (alist-to-hashtable
67 gnus-registry-headers-alist)))
69 (defun alist-to-hashtable (alist)
70 "Build a hashtable from the values in ALIST."
71 (let ((ht (make-hash-table
76 (puthash (car kv-pair) (cdr kv-pair) ht))
80 (defun hashtable-to-alist (hash)
81 "Build an alist from the values in HASH."
85 (setq list (cons (cons key value) list)))
89 (defun gnus-register-action (action data-header from &optional to method)
90 (let* ((id (mail-header-id data-header))
91 (from (gnus-group-guess-full-name from))
92 (to (if to (gnus-group-guess-full-name to) nil))
93 (to-name (if to to "the Bit Bucket")))
94 (gnus-message 5 "Registry: article %s %s from %s to %s"
96 (if method "respooling" "going")
99 (unless (gethash id gnus-registry-headers-hashtb)
100 (puthash id (list data-header) gnus-registry-headers-hashtb))
101 (puthash id (cons (list action from to)
102 (gethash id gnus-registry-hashtb))
103 gnus-registry-hashtb)))
105 (defun gnus-register-spool-action (id group)
106 ;; do not process the draft IDs
107 ; (unless (string-match "totally-fudged-out-message-id" id)
108 (let ((group (gnus-group-guess-full-name group)))
109 (when (string-match "\r$" id)
110 (setq id (substring id 0 -1)))
111 (gnus-message 5 "Registry: article %s spooled to %s"
114 (puthash id (cons (list 'spool nil group)
115 (gethash id gnus-registry-hashtb))
116 gnus-registry-hashtb)))
119 ;; Function for nn{mail|imap}-split-fancy: look up all references in
120 ;; the cache and if a match is found, return that group.
121 (defun gnus-registry-split-fancy-with-parent ()
122 "Split this message into the same group as its parent. The parent
123 is obtained from the registry. This function can be used as an entry
124 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
125 this: (: gnus-registry-split-fancy-with-parent)
127 For a message to be split, it looks for the parent message in the
128 References or In-Reply-To header and then looks in the registry to
129 see which group that message was put in. This group is returned.
131 See the Info node `(gnus)Fancy Mail Splitting' for more details."
132 (let ((refstr (or (message-fetch-field "references")
133 (message-fetch-field "in-reply-to")))
137 (setq references (nreverse (gnus-split-references refstr)))
139 (setq res (or (gnus-registry-fetch-group x) res))
140 (when (or (gnus-registry-grep-in-list
142 gnus-registry-unfollowed-groups)
143 (gnus-registry-grep-in-list
145 nnmail-split-fancy-with-parent-ignore-groups))
150 (defun gnus-registry-grep-in-list (word list)
155 (string-match x word))
159 (defun gnus-registry-fetch-group (id)
160 "Get the group of a message, based on the message ID.
161 Returns the first place where the trail finds a spool action."
162 (let ((trail (gethash id gnus-registry-hashtb)))
163 (dolist (crumb trail)
164 (let ((action (nth 0 crumb))
167 (when (eq action 'spool)
170 (defun gnus-registry-clear ()
171 "Clear the Gnus registry."
173 (setq gnus-registry-alist nil
174 gnus-registry-headers-alist nil)
175 (gnus-registry-translate-from-alist))
177 ; also does copy, respool, and crosspost
178 (add-hook 'gnus-summary-article-move-hook 'gnus-register-action)
179 (add-hook 'gnus-summary-article-delete-hook 'gnus-register-action)
180 (add-hook 'gnus-summary-article-expire-hook 'gnus-register-action)
181 (add-hook 'nnmail-spool-hook 'gnus-register-spool-action)
183 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-translate-to-alist)
184 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-translate-from-alist)
186 ;; TODO: a lot of things
188 (provide 'gnus-registry)
190 ;;; gnus-registry.el ends here