*** empty log message ***
[gnus] / lisp / gnus-dup.el
1 ;;; gnus-dup.el --- suppression of duplicate articles in Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This package tries to mark articles as read the second time the
27 ;; user reads a copy.  This is useful if the server doesn't support
28 ;; Xref properly, or if the user reads the same group from several
29 ;; servers.
30
31 ;;; Code:
32
33 (require 'gnus-load)
34 (require 'gnus-art)
35 (require 'gnus)
36
37 (defgroup gnus-duplicate nil
38   "Suppression of duplicate articles."
39   :group 'gnus)
40
41 (defcustom gnus-save-duplicate-list nil
42   "*If non-nil, save the duplicate list when shutting down Gnus.
43 If nil, duplicate suppression will only work on duplicates
44 seen in the same session."
45   :group 'gnus-duplicate
46   :type 'boolean)
47
48 (defcustom gnus-duplicate-list-length 10000
49   "*The number of Message-IDs to keep in the duplicate suppression list."
50   :group 'gnus-duplicate
51   :type 'integer)
52
53 (defcustom gnus-duplicate-file (nnheader-concat gnus-directory "suppression")
54   "*The name of the file to store the duplicate suppression list."
55   :group 'gnus-duplicate
56   :type 'file)
57
58 ;;; Internal variables
59
60 (defvar gnus-dup-list nil)
61 (defvar gnus-dup-hashtb nil)
62
63 (defvar gnus-dup-list-dirty nil)
64
65 ;;;
66 ;;; Starting and stopping
67 ;;;
68
69 (gnus-add-shutdown 'gnus-dup-close 'gnus)
70
71 (defun gnus-dup-close ()
72   "Possibly save the duplicate suppression list and shut down the subsystem."
73   (gnus-dup-save)
74   (setq gnus-dup-list nil
75         gnus-dup-hashtb nil
76         gnus-dup-list-dirty nil))
77
78 (defun gnus-dup-open ()
79   "Possibly read the duplicate suppression list and start the subsystem."
80   (if gnus-save-duplicate-list
81       (gnus-dup-read)
82     (setq gnus-dup-list nil))
83   (setq gnus-dup-hashtb (gnus-make-hashtable gnus-duplicate-list-length))
84   ;; Enter all Message-IDs into the hash table.
85   (let ((list gnus-dup-list)
86         (obarray gnus-dup-hashtb))
87     (while list 
88       (intern (pop list)))))
89
90 (defun gnus-dup-read ()
91   "Read the duplicate suppression list."
92   (setq gnus-dup-list nil)
93   (when (file-exists-p gnus-duplicate-file)
94     (load gnus-duplicate-file t t t)))
95
96 (defun gnus-dup-save ()
97   "Save the duplicate suppression list."
98   (when (and gnus-save-duplicate-list
99              gnus-dup-list-dirty)
100     (nnheader-temp-write gnus-duplicate-file
101       (gnus-prin1 `(setq gnus-dup-list ',gnus-dup-list))))
102   (setq gnus-dup-list-dirty nil))
103
104 ;;;
105 ;;; Interface functions
106 ;;;
107
108 (defun gnus-dup-enter-articles ()
109   "Enter articles from the current group for future duplicate suppression."
110   (unless gnus-dup-list
111     (gnus-dup-open))
112   (setq gnus-dup-list-dirty t)          ; mark list for saving
113   (let ((data gnus-newsgroup-data)
114         datum)
115     ;; Enter the Message-IDs of all read articles into the list
116     ;; and hash table.
117     (while (setq datum (pop data))
118       (when (and (not (gnus-data-pseudo-p datum))
119                  (gnus-data-read-p datum)
120                  (not (intern-soft (mail-header-id (gnus-data-header datum))
121                                    gnus-dup-hashtb)))
122         (intern (car (push (mail-header-id (gnus-data-header datum))
123                            gnus-dup-list))
124                 gnus-dup-hashtb))))
125   ;; Chop off excess Message-IDs from the list.
126   (let ((end (nthcdr gnus-duplicate-list-length gnus-dup-list)))
127     (when end 
128       (setcdr end nil))))
129
130 (defun gnus-dup-suppress-articles ()
131   "Mark duplicate articles as read."
132   (unless gnus-dup-list
133     (gnus-dup-open))
134   (gnus-message 6 "Suppressing duplicates...")
135   (let ((headers gnus-newsgroup-headers)
136         number header)
137     (while (setq header (pop headers))
138       (when (intern-soft (mail-header-id header) gnus-dup-hashtb)
139         (setq gnus-newsgroup-unreads 
140               (delq (setq number (mail-header-number header))
141                     gnus-newsgroup-unreads))
142         (push (cons number gnus-duplicate-mark)
143               gnus-newsgroup-reads))))
144   (gnus-message 6 "Suppressing duplicates...done"))
145
146 (defun gnus-dup-unsuppress-article (article)
147   "Stop suppression of ARTICLE."
148   (let ((id (mail-header-id (gnus-data-header (gnus-data-find article)))))
149     (when id
150       (setq gnus-dup-list-dirty t)
151       (setq gnus-dup-list (delete id gnus-dup-list))
152       (unintern id gnus-dup-hashtb))))
153
154 (provide 'gnus-dup)
155
156 ;;; gnus-dup.el ends here