a385d0abf3a0c06746d95db60e448b1d5fad5492
[gnus] / lisp / gnus-dup.el
1 ;;; gnus-dup.el --- suppression of duplicate articles in Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 ;; 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
30 ;; servers.
31
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35
36 (require 'gnus)
37 (require 'gnus-art)
38
39 (defgroup gnus-duplicate nil
40   "Suppression of duplicate articles."
41   :group 'gnus)
42
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
48   :type 'boolean)
49
50 (defcustom gnus-duplicate-list-length 10000
51   "*The number of Message-IDs to keep in the duplicate suppression list."
52   :group 'gnus-duplicate
53   :type 'integer)
54
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
58   :type 'file)
59
60 ;;; Internal variables
61
62 (defvar gnus-dup-list nil)
63 (defvar gnus-dup-hashtb nil)
64
65 (defvar gnus-dup-list-dirty nil)
66
67 ;;;
68 ;;; Starting and stopping
69 ;;;
70
71 (gnus-add-shutdown 'gnus-dup-close 'gnus)
72
73 (defun gnus-dup-close ()
74   "Possibly save the duplicate suppression list and shut down the subsystem."
75   (gnus-dup-save)
76   (setq gnus-dup-list nil
77         gnus-dup-hashtb nil
78         gnus-dup-list-dirty nil))
79
80 (defun gnus-dup-open ()
81   "Possibly read the duplicate suppression list and start the subsystem."
82   (if gnus-save-duplicate-list
83       (gnus-dup-read)
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))
89     (while list
90       (intern (pop list)))))
91
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)))
97
98 (defun gnus-dup-save ()
99   "Save the duplicate suppression list."
100   (when (and gnus-save-duplicate-list
101              gnus-dup-list-dirty)
102     (with-temp-file gnus-duplicate-file
103       (gnus-prin1 `(setq gnus-dup-list ',gnus-dup-list))))
104   (setq gnus-dup-list-dirty nil))
105
106 ;;;
107 ;;; Interface functions
108 ;;;
109
110 (defun gnus-dup-enter-articles ()
111   "Enter articles from the current group for future duplicate suppression."
112   (unless gnus-dup-list
113     (gnus-dup-open))
114   (setq gnus-dup-list-dirty t)          ; mark list for saving
115   (let ((data gnus-newsgroup-data)
116         datum msgid)
117     ;; Enter the Message-IDs of all read articles into the list
118     ;; and hash table.
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)))
131     (when end
132       (setcdr end nil))))
133
134 (defun gnus-dup-suppress-articles ()
135   "Mark duplicate articles as read."
136   (unless gnus-dup-list
137     (gnus-dup-open))
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)))
142         number header)
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))
149         (if (not auto)
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"))
154
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))))
159     (when id
160       (setq gnus-dup-list-dirty t)
161       (setq gnus-dup-list (delete id gnus-dup-list))
162       (unintern id gnus-dup-hashtb))))
163
164 (provide 'gnus-dup)
165
166 ;;; gnus-dup.el ends here