* gnus-registry.el (gnus-register-action)
[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-hashtb nil
37   "*The article registry by Message ID.")
38
39 (defvar gnus-registry-headers-hashtb nil
40   "*The article header registry by Message ID.")
41 ;; (setq gnus-registry-hashtb (make-hash-table 
42 ;;                          :size 4096
43 ;;                          :test 'equal)) ; we test message ID strings equality
44
45 ;; sample data-header
46 ;; (defvar tzz-header '(49 "Re[2]: good news" "\"Jonathan Pryor\" <offerlm@aol.com>" "Mon, 17 Feb 2003 10:41:46 +-0800" "<88288020@dytqq>" "" 896 18 "lockgroove.bwh.harvard.edu spam.asian:49" nil))
47
48 ;; (maphash (lambda (key value) (message "key: %s value: %s" key value)) gnus-registry-hashtb)
49 ;; (clrhash gnus-registry-hashtb)
50 ;; (setq gnus-registry-alist nil)
51
52 ;; Function(s) missing in Emacs 20
53 (when (memq nil (mapcar 'fboundp '(puthash)))
54   (require 'cl)
55   (unless (fboundp 'puthash)
56     ;; alias puthash is missing from Emacs 20 cl-extra.el
57     (defalias 'puthash 'cl-puthash)))
58
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 gnus-registry-headers-hashtb)))
62
63 (defun gnus-registry-translate-from-alist ()
64   (setq gnus-registry-hashtb (alist-to-hashtable gnus-registry-alist))
65   (setq gnus-registry-headers-hashtb (alist-to-hashtable gnus-registry-headers-alist)))
66
67 (defun alist-to-hashtable (alist)
68   "Build a hashtable from the values in ALIST."
69   (let ((ht (make-hash-table                        
70              :size 4096
71              :test 'equal)))
72     (mapc
73      (lambda (kv-pair)
74        (puthash (car kv-pair) (cdr kv-pair) ht))
75      alist)
76      ht))
77
78 (defun hashtable-to-alist (hash)
79   "Build an alist from the values in HASH."
80   (let ((list nil))
81     (maphash
82      (lambda (key value)
83        (setq list (cons (cons key value) list)))
84      hash)
85     list))
86
87 (defun gnus-register-action (action data-header from &optional to method)
88   (let* ((id (mail-header-id data-header)))
89     (gnus-message 5 "Registry: article %s %s from %s to %s"
90              id
91              (if method "respooling" "going")
92              (gnus-group-guess-full-name from)
93              (if to (gnus-group-guess-full-name to) "the Bit Bucket"))
94     (unless (gethash id gnus-registry-headers-hashtb)
95       (puthash id (list data-header) gnus-registry-headers-hashtb))
96     (puthash id (cons (list action from to method)
97                       (gethash id gnus-registry-hashtb)) gnus-registry-hashtb)))
98
99 (defun gnus-register-spool-action (id group)
100   (when (string-match "\r$" id)
101     (setq id (substring id 0 -1)))
102   (gnus-message 5 "Registry: article %s spooled to %s"
103            id
104            (gnus-group-prefixed-name 
105             group 
106             gnus-internal-registry-spool-current-method 
107             t))
108   (puthash id (cons (list 'spool nil group nil) 
109                     (gethash id gnus-registry-hashtb)) gnus-registry-hashtb))
110
111 (add-hook 'gnus-summary-article-move-hook 'gnus-register-action) ; also does copy, respool, and crosspost
112 (add-hook 'gnus-summary-article-delete-hook 'gnus-register-action)
113 (add-hook 'gnus-summary-article-expire-hook 'gnus-register-action)
114 (add-hook 'nnmail-spool-hook 'gnus-register-spool-action)
115
116 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-translate-to-alist)
117 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-translate-from-alist)
118
119 ;; TODO: a lot of things
120
121 (provide 'gnus-registry)
122
123 ;;; gnus-registry.el ends here