98d20a635458629ee815490a764e0407ce16848f
[elisp/gnus.git-] / lisp / gnus-dup.el
1 ;;; gnus-dup.el --- suppression of duplicate articles in Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004
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 ((obarray gnus-dup-hashtb))
88     (mapc 'intern gnus-dup-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     (with-temp-file 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 (msgid)
114     ;; Enter the Message-IDs of all read articles into the list
115     ;; and hash table.
116     (dolist (datum gnus-newsgroup-data)
117       (when (and (not (gnus-data-pseudo-p datum))
118                  (> (gnus-data-number datum) 0)
119                  (not (memq (gnus-data-number datum) gnus-newsgroup-unreads))
120                  (not (= (gnus-data-mark datum) gnus-canceled-mark))
121                  (setq msgid (mail-header-id (gnus-data-header datum)))
122                  (not (nnheader-fake-message-id-p msgid))
123                  (not (intern-soft msgid gnus-dup-hashtb)))
124         (push msgid gnus-dup-list)
125         (intern msgid gnus-dup-hashtb))))
126   ;; Chop off excess Message-IDs from the list.
127   (let ((end (nthcdr gnus-duplicate-list-length gnus-dup-list)))
128     (when end
129       (mapc (lambda (id) (unintern id gnus-dup-hashtb)) (cdr end))
130       (setcdr end nil))))
131
132 (defun gnus-dup-suppress-articles ()
133   "Mark duplicate articles as read."
134   (unless gnus-dup-list
135     (gnus-dup-open))
136   (gnus-message 6 "Suppressing duplicates...")
137   (let ((auto (and gnus-newsgroup-auto-expire
138                    (memq gnus-duplicate-mark gnus-auto-expirable-marks)))
139         number)
140     (dolist (header gnus-newsgroup-headers)
141       (when (and (intern-soft (mail-header-id header) gnus-dup-hashtb)
142                  (gnus-summary-article-unread-p (mail-header-number header)))
143         (setq gnus-newsgroup-unreads
144               (delq (setq number (mail-header-number header))
145                     gnus-newsgroup-unreads))
146         (if (not auto)
147             (push (cons number gnus-duplicate-mark) gnus-newsgroup-reads)
148           (push number gnus-newsgroup-expirable)
149           (push (cons number gnus-expirable-mark) gnus-newsgroup-reads)))))
150   (gnus-message 6 "Suppressing duplicates...done"))
151
152 (defun gnus-dup-unsuppress-article (article)
153   "Stop suppression of ARTICLE."
154   (let* ((header (gnus-data-header (gnus-data-find article)))
155          (id     (when header (mail-header-id header))))
156     (when id
157       (setq gnus-dup-list-dirty t)
158       (setq gnus-dup-list (delete id gnus-dup-list))
159       (unintern id gnus-dup-hashtb))))
160
161 (provide 'gnus-dup)
162
163 ;;; arch-tag: 903e94db-7b00-4d19-83ee-cf34a81fa5fb
164 ;;; gnus-dup.el ends here