1 ;;; gnus-dup.el --- suppression of duplicate articles in Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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.
27 ;; This package tries to mark articles as read the second time the
28 ;; user reads a copy. This is useful if the server doesn't support
29 ;; Xref properly, or if the user reads the same group from several
34 (eval-when-compile (require 'cl))
39 (defgroup gnus-duplicate nil
40 "Suppression of duplicate articles."
43 (defcustom gnus-save-duplicate-list nil
44 "*If non-nil, save the duplicate list when shutting down Gnus.
45 If nil, duplicate suppression will only work on duplicates
46 seen in the same session."
47 :group 'gnus-duplicate
50 (defcustom gnus-duplicate-list-length 10000
51 "*The number of Message-IDs to keep in the duplicate suppression list."
52 :group 'gnus-duplicate
55 (defcustom gnus-duplicate-file (nnheader-concat gnus-directory "suppression")
56 "*The name of the file to store the duplicate suppression list."
57 :group 'gnus-duplicate
60 ;;; Internal variables
62 (defvar gnus-dup-list nil)
63 (defvar gnus-dup-hashtb nil)
65 (defvar gnus-dup-list-dirty nil)
68 ;;; Starting and stopping
71 (gnus-add-shutdown 'gnus-dup-close 'gnus)
73 (defun gnus-dup-close ()
74 "Possibly save the duplicate suppression list and shut down the subsystem."
76 (setq gnus-dup-list nil
78 gnus-dup-list-dirty nil))
80 (defun gnus-dup-open ()
81 "Possibly read the duplicate suppression list and start the subsystem."
82 (if gnus-save-duplicate-list
84 (setq gnus-dup-list nil))
85 (setq gnus-dup-hashtb (gnus-make-hashtable gnus-duplicate-list-length))
86 ;; Enter all Message-IDs into the hash table.
87 (let ((list gnus-dup-list)
88 (obarray gnus-dup-hashtb))
90 (intern (pop list)))))
92 (defun gnus-dup-read ()
93 "Read the duplicate suppression list."
94 (setq gnus-dup-list nil)
95 (when (file-exists-p gnus-duplicate-file)
96 (load gnus-duplicate-file t t t)))
98 (defun gnus-dup-save ()
99 "Save the duplicate suppression list."
100 (when (and gnus-save-duplicate-list
102 (with-temp-file gnus-duplicate-file
103 (gnus-prin1 `(setq gnus-dup-list ',gnus-dup-list))))
104 (setq gnus-dup-list-dirty nil))
107 ;;; Interface functions
110 (defun gnus-dup-enter-articles ()
111 "Enter articles from the current group for future duplicate suppression."
112 (unless gnus-dup-list
114 (setq gnus-dup-list-dirty t) ; mark list for saving
115 (let ((data gnus-newsgroup-data)
117 ;; Enter the Message-IDs of all read articles into the list
119 (while (setq datum (pop data))
120 (when (and (not (gnus-data-pseudo-p datum))
121 (> (gnus-data-number datum) 0)
122 (not (memq (gnus-data-number datum) gnus-newsgroup-unreads))
123 (not (= (gnus-data-mark datum) gnus-canceled-mark))
124 (setq msgid (mail-header-id (gnus-data-header datum)))
125 (not (nnheader-fake-message-id-p msgid))
126 (not (intern-soft msgid gnus-dup-hashtb)))
127 (push msgid gnus-dup-list)
128 (intern msgid gnus-dup-hashtb))))
129 ;; Chop off excess Message-IDs from the list.
130 (let ((end (nthcdr gnus-duplicate-list-length gnus-dup-list)))
134 (defun gnus-dup-suppress-articles ()
135 "Mark duplicate articles as read."
136 (unless gnus-dup-list
138 (gnus-message 6 "Suppressing duplicates...")
139 (let ((headers gnus-newsgroup-headers)
140 (auto (and gnus-newsgroup-auto-expire
141 (memq gnus-duplicate-mark gnus-auto-expirable-marks)))
143 (while (setq header (pop headers))
144 (when (and (intern-soft (mail-header-id header) gnus-dup-hashtb)
145 (gnus-summary-article-unread-p (mail-header-number header)))
146 (setq gnus-newsgroup-unreads
147 (delq (setq number (mail-header-number header))
148 gnus-newsgroup-unreads))
150 (push (cons number gnus-duplicate-mark) gnus-newsgroup-reads)
151 (push number gnus-newsgroup-expirable)
152 (push (cons number gnus-expirable-mark) gnus-newsgroup-reads)))))
153 (gnus-message 6 "Suppressing duplicates...done"))
155 (defun gnus-dup-unsuppress-article (article)
156 "Stop suppression of ARTICLE."
157 (let* ((header (gnus-data-header (gnus-data-find article)))
158 (id (when header (mail-header-id header))))
160 (setq gnus-dup-list-dirty t)
161 (setq gnus-dup-list (delete id gnus-dup-list))
162 (unintern id gnus-dup-hashtb))))
166 ;;; gnus-dup.el ends here